-
Notifications
You must be signed in to change notification settings - Fork 369
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(deps): Internalize common (#1839)
* chore(deps): internalize nodejs-common * add common unit tests * remove unused type dependency * add common system tests * format setUniformBucketLevelAccess function
- Loading branch information
1 parent
58903d6
commit 21cbce0
Showing
32 changed files
with
6,403 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/*! | ||
* Copyright 2022 Google LLC. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
export {GoogleAuthOptions} from 'google-auth-library'; | ||
|
||
export {Operation} from './operation'; | ||
|
||
export { | ||
Service, | ||
ServiceConfig, | ||
ServiceOptions, | ||
StreamRequestOptions, | ||
} from './service'; | ||
|
||
export { | ||
DeleteCallback, | ||
ExistsCallback, | ||
GetConfig, | ||
InstanceResponseCallback, | ||
Interceptor, | ||
Metadata, | ||
MetadataCallback, | ||
MetadataResponse, | ||
Methods, | ||
ResponseCallback, | ||
ServiceObject, | ||
ServiceObjectConfig, | ||
ServiceObjectParent, | ||
SetMetadataResponse, | ||
} from './service-object'; | ||
|
||
export { | ||
Abortable, | ||
AbortableDuplex, | ||
ApiError, | ||
BodyResponseCallback, | ||
DecorateRequestOptions, | ||
ResponseBody, | ||
util, | ||
} from './util'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/*! | ||
* Copyright 2022 Google LLC. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { | ||
Metadata, | ||
MetadataCallback, | ||
ServiceObject, | ||
ServiceObjectConfig, | ||
} from './service-object'; | ||
import {ApiError} from './util'; | ||
import {promisify} from 'util'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export class Operation<T = any> extends ServiceObject<T> { | ||
completeListeners: number; | ||
hasActiveListeners: boolean; | ||
|
||
/** | ||
* An Operation object allows you to interact with APIs that take longer to | ||
* process things. | ||
* | ||
* @constructor | ||
* @alias module:common/operation | ||
* | ||
* @param {object} config - Configuration object. | ||
* @param {module:common/service|module:common/serviceObject|module:common/grpcService|module:common/grpcServiceObject} config.parent - The parent object. | ||
*/ | ||
constructor(config: ServiceObjectConfig) { | ||
const methods = { | ||
/** | ||
* Checks to see if an operation exists. | ||
*/ | ||
exists: true, | ||
|
||
/** | ||
* Retrieves the operation. | ||
*/ | ||
get: true, | ||
|
||
/** | ||
* Retrieves metadata for the operation. | ||
*/ | ||
getMetadata: { | ||
reqOpts: { | ||
name: config.id, | ||
}, | ||
}, | ||
}; | ||
|
||
config = Object.assign( | ||
{ | ||
baseUrl: '', | ||
}, | ||
config | ||
); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
config.methods = (config.methods || methods) as any; | ||
super(config); | ||
this.completeListeners = 0; | ||
this.hasActiveListeners = false; | ||
this.listenForEvents_(); | ||
} | ||
|
||
/** | ||
* Wraps the `complete` and `error` events in a Promise. | ||
* | ||
* @return {Promise} | ||
*/ | ||
promise() { | ||
return new Promise((resolve, reject) => { | ||
this.on('error', reject).on('complete', (metadata: {}) => { | ||
resolve([metadata]); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Begin listening for events on the operation. This method keeps track of how | ||
* many "complete" listeners are registered and removed, making sure polling | ||
* is handled automatically. | ||
* | ||
* As long as there is one active "complete" listener, the connection is open. | ||
* When there are no more listeners, the polling stops. | ||
* | ||
* @private | ||
*/ | ||
protected listenForEvents_() { | ||
this.on('newListener', (event: string) => { | ||
if (event === 'complete') { | ||
this.completeListeners++; | ||
if (!this.hasActiveListeners) { | ||
this.hasActiveListeners = true; | ||
this.startPolling_(); | ||
} | ||
} | ||
}); | ||
|
||
this.on('removeListener', (event: string) => { | ||
if (event === 'complete' && --this.completeListeners === 0) { | ||
this.hasActiveListeners = false; | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Poll for a status update. Returns null for an incomplete | ||
* status, and metadata for a complete status. | ||
* | ||
* @private | ||
*/ | ||
protected poll_(callback: MetadataCallback): void { | ||
this.getMetadata((err: ApiError, body: Metadata) => { | ||
if (err || body!.error) { | ||
callback(err || (body!.error as Error)); | ||
return; | ||
} | ||
|
||
if (!body!.done) { | ||
callback(null); | ||
return; | ||
} | ||
|
||
callback(null, body); | ||
}); | ||
} | ||
|
||
/** | ||
* Poll `getMetadata` to check the operation's status. This runs a loop to | ||
* ping the API on an interval. | ||
* | ||
* Note: This method is automatically called once a "complete" event handler | ||
* is registered on the operation. | ||
* | ||
* @private | ||
*/ | ||
protected async startPolling_() { | ||
if (!this.hasActiveListeners) { | ||
return; | ||
} | ||
try { | ||
const metadata = await promisify(this.poll_.bind(this))(); | ||
if (!metadata) { | ||
setTimeout(this.startPolling_.bind(this), this.pollIntervalMs || 500); | ||
return; | ||
} | ||
this.emit('complete', metadata); | ||
} catch (err) { | ||
this.emit('error', err); | ||
} | ||
} | ||
} |
Oops, something went wrong.