Skip to content

Commit 21cbce0

Browse files
authored
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
1 parent 58903d6 commit 21cbce0

32 files changed

+6403
-38
lines changed

conformance-test/conformanceCommon.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import * as libraryMethods from './libraryMethods';
1818
import {Bucket, File, HmacKey, Notification, Storage} from '../src/';
1919
import * as uuid from 'uuid';
2020
import * as assert from 'assert';
21-
import {DecorateRequestOptions} from '@google-cloud/common';
21+
import {DecorateRequestOptions} from '../src/nodejs-common';
2222
import fetch from 'node-fetch';
2323

2424
interface RetryCase {

conformance-test/libraryMethods.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import {Bucket, File, Notification, Storage, HmacKey} from '../src';
1616
import * as path from 'path';
17-
import {ApiError} from '@google-cloud/common';
17+
import {ApiError} from '../src/nodejs-common';
1818

1919
/////////////////////////////////////////////////
2020
//////////////////// BUCKET /////////////////////

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
"precompile": "gts clean"
5151
},
5252
"dependencies": {
53-
"@google-cloud/common": "^3.8.1",
5453
"@google-cloud/paginator": "^3.0.7",
54+
"@google-cloud/projectify": "^2.0.0",
5555
"@google-cloud/promisify": "^2.0.0",
5656
"abort-controller": "^3.0.0",
5757
"arrify": "^2.0.0",
@@ -60,17 +60,20 @@
6060
"configstore": "^5.0.0",
6161
"date-and-time": "^2.0.0",
6262
"duplexify": "^4.0.0",
63+
"ent": "^2.2.0",
6364
"extend": "^3.0.2",
6465
"gaxios": "^4.0.0",
6566
"get-stream": "^6.0.0",
66-
"google-auth-library": "^7.0.0",
67+
"google-auth-library": "^7.14.1",
6768
"hash-stream-validation": "^0.2.2",
6869
"mime": "^3.0.0",
6970
"mime-types": "^2.0.8",
7071
"p-limit": "^3.0.1",
7172
"pumpify": "^2.0.0",
73+
"retry-request": "^4.2.2",
7274
"snakeize": "^0.1.0",
7375
"stream-events": "^1.0.4",
76+
"teeny-request": "^7.1.3",
7477
"xdg-basedir": "^4.0.0"
7578
},
7679
"devDependencies": {
@@ -79,9 +82,9 @@
7982
"@grpc/proto-loader": "^0.6.0",
8083
"@types/async-retry": "^1.4.3",
8184
"@types/compressible": "^2.0.0",
82-
"@types/concat-stream": "^2.0.0",
8385
"@types/configstore": "^5.0.0",
8486
"@types/date-and-time": "^0.13.0",
87+
"@types/ent": "^2.2.1",
8588
"@types/extend": "^3.0.0",
8689
"@types/mime": "^2.0.0",
8790
"@types/mime-types": "^2.1.0",
@@ -91,6 +94,7 @@
9194
"@types/node-fetch": "^2.1.3",
9295
"@types/proxyquire": "^1.3.28",
9396
"@types/pumpify": "^1.4.1",
97+
"@types/request": "^2.48.4",
9498
"@types/sinon": "^10.0.0",
9599
"@types/tmp": "0.2.3",
96100
"@types/uuid": "^8.0.0",

src/acl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
BodyResponseCallback,
1717
DecorateRequestOptions,
1818
Metadata,
19-
} from '@google-cloud/common';
19+
} from './nodejs-common';
2020
import {promisifyAll} from '@google-cloud/promisify';
2121
import arrify = require('arrify');
2222

src/bucket.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
ResponseBody,
2424
ServiceObject,
2525
util,
26-
} from '@google-cloud/common';
26+
} from './nodejs-common';
2727
import {paginator} from '@google-cloud/paginator';
2828
import {promisifyAll} from '@google-cloud/promisify';
2929
import arrify = require('arrify');

src/channel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import {Metadata, ServiceObject, util} from '@google-cloud/common';
15+
import {Metadata, ServiceObject, util} from './nodejs-common';
1616
import {promisifyAll} from '@google-cloud/promisify';
1717

1818
import {Storage} from './storage';

src/file.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
Metadata,
2121
ServiceObject,
2222
util,
23-
} from '@google-cloud/common';
23+
} from './nodejs-common';
2424
import {promisifyAll} from '@google-cloud/promisify';
2525

2626
import compressible = require('compressible');
@@ -63,7 +63,7 @@ import {
6363
ApiError,
6464
Duplexify,
6565
DuplexifyConstructor,
66-
} from '@google-cloud/common/build/src/util';
66+
} from './nodejs-common/util';
6767
// eslint-disable-next-line @typescript-eslint/no-var-requires
6868
const duplexify: DuplexifyConstructor = require('duplexify');
6969
import {normalize, objectKeyToLowercase, unicodeJSONStringify} from './util';

src/hmacKey.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import {Metadata, ServiceObject, Methods} from '@google-cloud/common';
15+
import {Metadata, ServiceObject, Methods} from './nodejs-common';
1616
import {Storage} from './storage';
1717

1818
export interface HmacKeyOptions {

src/iam.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
BodyResponseCallback,
1717
DecorateRequestOptions,
1818
Metadata,
19-
} from '@google-cloud/common';
19+
} from './nodejs-common';
2020
import {promisifyAll} from '@google-cloud/promisify';
2121
import arrify = require('arrify');
2222

src/nodejs-common/index.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*!
2+
* Copyright 2022 Google LLC. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
export {GoogleAuthOptions} from 'google-auth-library';
17+
18+
export {Operation} from './operation';
19+
20+
export {
21+
Service,
22+
ServiceConfig,
23+
ServiceOptions,
24+
StreamRequestOptions,
25+
} from './service';
26+
27+
export {
28+
DeleteCallback,
29+
ExistsCallback,
30+
GetConfig,
31+
InstanceResponseCallback,
32+
Interceptor,
33+
Metadata,
34+
MetadataCallback,
35+
MetadataResponse,
36+
Methods,
37+
ResponseCallback,
38+
ServiceObject,
39+
ServiceObjectConfig,
40+
ServiceObjectParent,
41+
SetMetadataResponse,
42+
} from './service-object';
43+
44+
export {
45+
Abortable,
46+
AbortableDuplex,
47+
ApiError,
48+
BodyResponseCallback,
49+
DecorateRequestOptions,
50+
ResponseBody,
51+
util,
52+
} from './util';

src/nodejs-common/operation.ts

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
/*!
2+
* Copyright 2022 Google LLC. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import {
17+
Metadata,
18+
MetadataCallback,
19+
ServiceObject,
20+
ServiceObjectConfig,
21+
} from './service-object';
22+
import {ApiError} from './util';
23+
import {promisify} from 'util';
24+
25+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
26+
export class Operation<T = any> extends ServiceObject<T> {
27+
completeListeners: number;
28+
hasActiveListeners: boolean;
29+
30+
/**
31+
* An Operation object allows you to interact with APIs that take longer to
32+
* process things.
33+
*
34+
* @constructor
35+
* @alias module:common/operation
36+
*
37+
* @param {object} config - Configuration object.
38+
* @param {module:common/service|module:common/serviceObject|module:common/grpcService|module:common/grpcServiceObject} config.parent - The parent object.
39+
*/
40+
constructor(config: ServiceObjectConfig) {
41+
const methods = {
42+
/**
43+
* Checks to see if an operation exists.
44+
*/
45+
exists: true,
46+
47+
/**
48+
* Retrieves the operation.
49+
*/
50+
get: true,
51+
52+
/**
53+
* Retrieves metadata for the operation.
54+
*/
55+
getMetadata: {
56+
reqOpts: {
57+
name: config.id,
58+
},
59+
},
60+
};
61+
62+
config = Object.assign(
63+
{
64+
baseUrl: '',
65+
},
66+
config
67+
);
68+
69+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
70+
config.methods = (config.methods || methods) as any;
71+
super(config);
72+
this.completeListeners = 0;
73+
this.hasActiveListeners = false;
74+
this.listenForEvents_();
75+
}
76+
77+
/**
78+
* Wraps the `complete` and `error` events in a Promise.
79+
*
80+
* @return {Promise}
81+
*/
82+
promise() {
83+
return new Promise((resolve, reject) => {
84+
this.on('error', reject).on('complete', (metadata: {}) => {
85+
resolve([metadata]);
86+
});
87+
});
88+
}
89+
90+
/**
91+
* Begin listening for events on the operation. This method keeps track of how
92+
* many "complete" listeners are registered and removed, making sure polling
93+
* is handled automatically.
94+
*
95+
* As long as there is one active "complete" listener, the connection is open.
96+
* When there are no more listeners, the polling stops.
97+
*
98+
* @private
99+
*/
100+
protected listenForEvents_() {
101+
this.on('newListener', (event: string) => {
102+
if (event === 'complete') {
103+
this.completeListeners++;
104+
if (!this.hasActiveListeners) {
105+
this.hasActiveListeners = true;
106+
this.startPolling_();
107+
}
108+
}
109+
});
110+
111+
this.on('removeListener', (event: string) => {
112+
if (event === 'complete' && --this.completeListeners === 0) {
113+
this.hasActiveListeners = false;
114+
}
115+
});
116+
}
117+
118+
/**
119+
* Poll for a status update. Returns null for an incomplete
120+
* status, and metadata for a complete status.
121+
*
122+
* @private
123+
*/
124+
protected poll_(callback: MetadataCallback): void {
125+
this.getMetadata((err: ApiError, body: Metadata) => {
126+
if (err || body!.error) {
127+
callback(err || (body!.error as Error));
128+
return;
129+
}
130+
131+
if (!body!.done) {
132+
callback(null);
133+
return;
134+
}
135+
136+
callback(null, body);
137+
});
138+
}
139+
140+
/**
141+
* Poll `getMetadata` to check the operation's status. This runs a loop to
142+
* ping the API on an interval.
143+
*
144+
* Note: This method is automatically called once a "complete" event handler
145+
* is registered on the operation.
146+
*
147+
* @private
148+
*/
149+
protected async startPolling_() {
150+
if (!this.hasActiveListeners) {
151+
return;
152+
}
153+
try {
154+
const metadata = await promisify(this.poll_.bind(this))();
155+
if (!metadata) {
156+
setTimeout(this.startPolling_.bind(this), this.pollIntervalMs || 500);
157+
return;
158+
}
159+
this.emit('complete', metadata);
160+
} catch (err) {
161+
this.emit('error', err);
162+
}
163+
}
164+
}

0 commit comments

Comments
 (0)