1
0
mirror of https://github.com/musix-org/musix-oss synced 2025-07-06 22:10:49 +00:00
This commit is contained in:
MatteZ02
2020-03-03 22:30:50 +02:00
parent edfcc6f474
commit 30022c7634
11800 changed files with 1984416 additions and 1 deletions

View File

@ -0,0 +1,36 @@
/**
* Copyright 2020 Google LLC
*
* 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 { APICaller, ApiCallerSettings } from '../apiCaller';
import { APICallback, GRPCCall, SimpleCallbackFunction } from '../apitypes';
import { OngoingCall, OngoingCallPromise } from '../call';
import { CallSettings } from '../gax';
import { GoogleError } from '../googleError';
import { BundleExecutor } from './bundleExecutor';
/**
* An implementation of APICaller for bundled calls.
* Uses BundleExecutor to do bundling.
*/
export declare class BundleApiCaller implements APICaller {
bundler: BundleExecutor;
constructor(bundler: BundleExecutor);
init(settings: ApiCallerSettings, callback?: APICallback): OngoingCallPromise | OngoingCall;
wrap(func: GRPCCall): GRPCCall;
call(apiCall: SimpleCallbackFunction, argument: {}, settings: CallSettings, status: OngoingCallPromise): void;
fail(canceller: OngoingCallPromise, err: GoogleError): void;
result(canceller: OngoingCallPromise): import("../call").CancellablePromise<[import("../apitypes").ResponseType, {
[index: string]: string | number | {};
} | null | undefined, {} | import("..").Operation | null | undefined]>;
}

View File

@ -0,0 +1,54 @@
"use strict";
/**
* Copyright 2020 Google LLC
*
* 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const call_1 = require("../call");
const googleError_1 = require("../googleError");
/**
* An implementation of APICaller for bundled calls.
* Uses BundleExecutor to do bundling.
*/
class BundleApiCaller {
constructor(bundler) {
this.bundler = bundler;
}
init(settings, callback) {
if (callback) {
return new call_1.OngoingCall(callback);
}
return new call_1.OngoingCallPromise(settings.promise);
}
wrap(func) {
return func;
}
call(apiCall, argument, settings, status) {
if (!settings.isBundling) {
throw new googleError_1.GoogleError('Bundling enabled with no isBundling!');
}
status.call((argument, callback) => {
this.bundler.schedule(apiCall, argument, callback);
return status;
}, argument);
}
fail(canceller, err) {
canceller.callback(err);
}
result(canceller) {
return canceller.promise;
}
}
exports.BundleApiCaller = BundleApiCaller;
//# sourceMappingURL=bundleApiCaller.js.map

View File

@ -0,0 +1 @@
{"version":3,"file":"bundleApiCaller.js","sourceRoot":"","sources":["../../../src/bundlingCalls/bundleApiCaller.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;AAIH,kCAAwD;AAExD,gDAA2C;AAK3C;;;GAGG;AACH,MAAa,eAAe;IAG1B,YAAY,OAAuB;QACjC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;IAED,IAAI,CACF,QAA2B,EAC3B,QAAsB;QAEtB,IAAI,QAAQ,EAAE;YACZ,OAAO,IAAI,kBAAW,CAAC,QAAQ,CAAC,CAAC;SAClC;QACD,OAAO,IAAI,yBAAkB,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IAClD,CAAC;IAED,IAAI,CAAC,IAAc;QACjB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,IAAI,CACF,OAA+B,EAC/B,QAAY,EACZ,QAAsB,EACtB,MAA0B;QAE1B,IAAI,CAAC,QAAQ,CAAC,UAAU,EAAE;YACxB,MAAM,IAAI,yBAAW,CAAC,sCAAsC,CAAC,CAAC;SAC/D;QAED,MAAM,CAAC,IAAI,CAAC,CAAC,QAAY,EAAE,QAAsB,EAAE,EAAE;YACnD,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,OAAO,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC;YACnD,OAAO,MAAM,CAAC;QAChB,CAAC,EAAE,QAAQ,CAAC,CAAC;IACf,CAAC;IAED,IAAI,CAAC,SAA6B,EAAE,GAAgB;QAClD,SAAS,CAAC,QAAS,CAAC,GAAG,CAAC,CAAC;IAC3B,CAAC;IAED,MAAM,CAAC,SAA6B;QAClC,OAAO,SAAS,CAAC,OAAO,CAAC;IAC3B,CAAC;CACF;AA5CD,0CA4CC"}

View File

@ -0,0 +1,57 @@
/**
* Copyright 2020 Google LLC
*
* 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 { Descriptor } from '../descriptor';
import { CallSettings } from '../gax';
import { NormalApiCaller } from '../normalCalls/normalApiCaller';
import { BundleApiCaller } from './bundleApiCaller';
/**
* A descriptor for calls that can be bundled into one call.
*/
export declare class BundleDescriptor implements Descriptor {
bundledField: string;
requestDiscriminatorFields: string[];
subresponseField: string | null;
byteLengthFunction: Function;
/**
* Describes the structure of bundled call.
*
* requestDiscriminatorFields may include '.' as a separator, which is used to
* indicate object traversal. This allows fields in nested objects to be used
* to determine what request to bundle.
*
* @property {String} bundledField
* @property {String} requestDiscriminatorFields
* @property {String} subresponseField
* @property {Function} byteLengthFunction
*
* @param {String} bundledField - the repeated field in the request message
* that will have its elements aggregated by bundling.
* @param {String} requestDiscriminatorFields - a list of fields in the
* target request message class that are used to detemrine which request
* messages should be bundled together.
* @param {String} subresponseField - an optional field, when present it
* indicates the field in the response message that should be used to
* demultiplex the response into multiple response messages.
* @param {Function} byteLengthFunction - a function to obtain the byte
* length to be consumed for the bundled field messages. Because Node.JS
* protobuf.js/gRPC uses builtin Objects for the user-visible data and
* internally they are encoded/decoded in protobuf manner, this function
* is actually necessary to calculate the byte length.
* @constructor
*/
constructor(bundledField: string, requestDiscriminatorFields: string[], subresponseField: string | null, byteLengthFunction: Function);
getApiCaller(settings: CallSettings): NormalApiCaller | BundleApiCaller;
}

View File

@ -0,0 +1,70 @@
"use strict";
/**
* Copyright 2020 Google LLC
*
* 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const normalApiCaller_1 = require("../normalCalls/normalApiCaller");
const bundleApiCaller_1 = require("./bundleApiCaller");
const bundleExecutor_1 = require("./bundleExecutor");
/**
* A descriptor for calls that can be bundled into one call.
*/
class BundleDescriptor {
/**
* Describes the structure of bundled call.
*
* requestDiscriminatorFields may include '.' as a separator, which is used to
* indicate object traversal. This allows fields in nested objects to be used
* to determine what request to bundle.
*
* @property {String} bundledField
* @property {String} requestDiscriminatorFields
* @property {String} subresponseField
* @property {Function} byteLengthFunction
*
* @param {String} bundledField - the repeated field in the request message
* that will have its elements aggregated by bundling.
* @param {String} requestDiscriminatorFields - a list of fields in the
* target request message class that are used to detemrine which request
* messages should be bundled together.
* @param {String} subresponseField - an optional field, when present it
* indicates the field in the response message that should be used to
* demultiplex the response into multiple response messages.
* @param {Function} byteLengthFunction - a function to obtain the byte
* length to be consumed for the bundled field messages. Because Node.JS
* protobuf.js/gRPC uses builtin Objects for the user-visible data and
* internally they are encoded/decoded in protobuf manner, this function
* is actually necessary to calculate the byte length.
* @constructor
*/
constructor(bundledField, requestDiscriminatorFields, subresponseField, byteLengthFunction) {
if (!byteLengthFunction && typeof subresponseField === 'function') {
byteLengthFunction = subresponseField;
subresponseField = null;
}
this.bundledField = bundledField;
this.requestDiscriminatorFields = requestDiscriminatorFields;
this.subresponseField = subresponseField;
this.byteLengthFunction = byteLengthFunction;
}
getApiCaller(settings) {
if (settings.isBundling === false) {
return new normalApiCaller_1.NormalApiCaller();
}
return new bundleApiCaller_1.BundleApiCaller(new bundleExecutor_1.BundleExecutor(settings.bundleOptions, this));
}
}
exports.BundleDescriptor = BundleDescriptor;
//# sourceMappingURL=bundleDescriptor.js.map

View File

@ -0,0 +1 @@
{"version":3,"file":"bundleDescriptor.js","sourceRoot":"","sources":["../../../src/bundlingCalls/bundleDescriptor.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;AAIH,oEAA+D;AAE/D,uDAAkD;AAClD,qDAAgD;AAEhD;;GAEG;AACH,MAAa,gBAAgB;IAM3B;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;IACH,YACE,YAAoB,EACpB,0BAAoC,EACpC,gBAA+B,EAC/B,kBAA4B;QAE5B,IAAI,CAAC,kBAAkB,IAAI,OAAO,gBAAgB,KAAK,UAAU,EAAE;YACjE,kBAAkB,GAAG,gBAAgB,CAAC;YACtC,gBAAgB,GAAG,IAAI,CAAC;SACzB;QACD,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,IAAI,CAAC,0BAA0B,GAAG,0BAA0B,CAAC;QAC7D,IAAI,CAAC,gBAAgB,GAAG,gBAAgB,CAAC;QACzC,IAAI,CAAC,kBAAkB,GAAG,kBAAkB,CAAC;IAC/C,CAAC;IAED,YAAY,CAAC,QAAsB;QACjC,IAAI,QAAQ,CAAC,UAAU,KAAK,KAAK,EAAE;YACjC,OAAO,IAAI,iCAAe,EAAE,CAAC;SAC9B;QACD,OAAO,IAAI,iCAAe,CACxB,IAAI,+BAAc,CAAC,QAAQ,CAAC,aAAc,EAAE,IAAI,CAAC,CAClD,CAAC;IACJ,CAAC;CACF;AAzDD,4CAyDC"}

View File

@ -0,0 +1,116 @@
/**
* Copyright 2020 Google LLC
*
* 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 { SimpleCallbackFunction } from '../apitypes';
import { BundleDescriptor } from './bundleDescriptor';
import { Task, TaskCallback } from './task';
/**
* Parameter to configure bundling behavior.
* @typedef {Object} BundleOptions
* @property {number} elementCountThreshold -
* the bundled request will be sent once the count of outstanding elements
* in the repeated field reaches this value.
* @property {number} elementCountLimit -
* represents a hard limit on the number of elements in the repeated field
* of the bundle; if adding a request to a bundle would exceed this value,
* the bundle is sent and the new request is added to a fresh bundle. It is
* invalid for a single request to exceed this limit.
* @property {number} requestByteThreshold -
* the bundled request will be sent once the count of bytes in the request
* reaches this value. Note that this value is pessimistically approximated
* by summing the bytesizes of the elements in the repeated field, and
* therefore may be an under-approximation.
* @property {number} requestByteLimit -
* represents a hard limit on the size of the bundled request; if adding
* a request to a bundle would exceed this value, the bundle is sent and
* the new request is added to a fresh bundle. It is invalid for a single
* request to exceed this limit. Note that this value is pessimistically
* approximated by summing the bytesizes of the elements in the repeated
* field, with a buffer applied to correspond to the resulting
* under-approximation.
* @property {number} delayThreshold -
* the bundled request will be sent this amount of time after the first
* element in the bundle was added to it.
*/
export interface BundleOptions {
elementCountLimit?: number;
requestByteLimit?: number;
elementCountThreshold?: number;
requestByteThreshold?: number;
delayThreshold?: number;
}
/**
* BundleExecutor stores several timers for each bundle (calls are bundled based
* on the options passed, each bundle has unique ID that is calculated based on
* field values). Each timer fires and sends a call after certain amount of
* time, and if a new request comes to the same bundle, the timer can be
* restarted.
*/
export declare class BundleExecutor {
_options: BundleOptions;
_descriptor: BundleDescriptor;
_tasks: {
[index: string]: Task;
};
_timers: {
[index: string]: ReturnType<typeof setTimeout>;
};
_invocations: {
[index: string]: string;
};
_invocationId: number;
/**
* Organizes requests for an api service that requires to bundle them.
*
* @param {BundleOptions} bundleOptions - configures strategy this instance
* uses when executing bundled functions.
* @param {BundleDescriptor} bundleDescriptor - the description of the bundling.
* @constructor
*/
constructor(bundleOptions: BundleOptions, bundleDescriptor: BundleDescriptor);
/**
* Schedule a method call.
*
* @param {function} apiCall - the function for an API call.
* @param {Object} request - the request object to be bundled with others.
* @param {APICallback} callback - the callback to be called when the method finished.
* @return {function()} - the function to cancel the scheduled invocation.
*/
schedule(apiCall: SimpleCallbackFunction, request: {
[index: string]: Array<{}> | string;
}, callback?: TaskCallback): import("../apitypes").GRPCCallResult;
/**
* Clears scheduled timeout if it exists.
*
* @param {String} bundleId - the id for the task whose timeout needs to be
* cleared.
* @private
*/
private _maybeClearTimeout;
/**
* Cancels an event.
*
* @param {String} id - The id for the event in the task.
* @private
*/
private _cancel;
/**
* Invokes a task.
*
* @param {String} bundleId - The id for the task.
* @private
*/
_runNow(bundleId: string): void;
}

View File

@ -0,0 +1,193 @@
"use strict";
/**
* Copyright 2020 Google LLC
*
* 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const status_1 = require("../status");
const googleError_1 = require("../googleError");
const warnings_1 = require("../warnings");
const bundlingUtils_1 = require("./bundlingUtils");
const task_1 = require("./task");
function noop() { }
/**
* BundleExecutor stores several timers for each bundle (calls are bundled based
* on the options passed, each bundle has unique ID that is calculated based on
* field values). Each timer fires and sends a call after certain amount of
* time, and if a new request comes to the same bundle, the timer can be
* restarted.
*/
class BundleExecutor {
/**
* Organizes requests for an api service that requires to bundle them.
*
* @param {BundleOptions} bundleOptions - configures strategy this instance
* uses when executing bundled functions.
* @param {BundleDescriptor} bundleDescriptor - the description of the bundling.
* @constructor
*/
constructor(bundleOptions, bundleDescriptor) {
this._options = bundleOptions;
this._descriptor = bundleDescriptor;
this._tasks = {};
this._timers = {};
this._invocations = {};
this._invocationId = 0;
}
/**
* Schedule a method call.
*
* @param {function} apiCall - the function for an API call.
* @param {Object} request - the request object to be bundled with others.
* @param {APICallback} callback - the callback to be called when the method finished.
* @return {function()} - the function to cancel the scheduled invocation.
*/
schedule(apiCall, request, callback) {
const bundleId = bundlingUtils_1.computeBundleId(request, this._descriptor.requestDiscriminatorFields);
callback = (callback || noop);
if (bundleId === undefined) {
warnings_1.warn('bundling_schedule_bundleid_undefined', 'The request does not have enough information for request bundling. ' +
`Invoking immediately. Request: ${JSON.stringify(request)} ` +
`discriminator fields: ${this._descriptor.requestDiscriminatorFields}`);
return apiCall(request, callback);
}
if (request[this._descriptor.bundledField] === undefined) {
warnings_1.warn('bundling_no_bundled_field', `Request does not contain field ${this._descriptor.bundledField} that must present for bundling. ` +
`Invoking immediately. Request: ${JSON.stringify(request)}`);
return apiCall(request, callback);
}
if (!(bundleId in this._tasks)) {
this._tasks[bundleId] = new task_1.Task(apiCall, request, this._descriptor.bundledField, this._descriptor.subresponseField);
}
let task = this._tasks[bundleId];
callback.id = String(this._invocationId++);
this._invocations[callback.id] = bundleId;
const bundledField = request[this._descriptor.bundledField];
const elementCount = bundledField.length;
let requestBytes = 0;
const self = this;
bundledField.forEach(obj => {
requestBytes += this._descriptor.byteLengthFunction(obj);
});
const countLimit = this._options.elementCountLimit || 0;
const byteLimit = this._options.requestByteLimit || 0;
if ((countLimit > 0 && elementCount > countLimit) ||
(byteLimit > 0 && requestBytes >= byteLimit)) {
let message;
if (countLimit > 0 && elementCount > countLimit) {
message =
'The number of elements ' +
elementCount +
' exceeds the limit ' +
this._options.elementCountLimit;
}
else {
message =
'The required bytes ' +
requestBytes +
' exceeds the limit ' +
this._options.requestByteLimit;
}
const error = new googleError_1.GoogleError(message);
error.code = status_1.Status.INVALID_ARGUMENT;
callback(error);
return {
cancel: noop,
};
}
const existingCount = task.getElementCount();
const existingBytes = task.getRequestByteSize();
if ((countLimit > 0 && elementCount + existingCount >= countLimit) ||
(byteLimit > 0 && requestBytes + existingBytes >= byteLimit)) {
this._runNow(bundleId);
this._tasks[bundleId] = new task_1.Task(apiCall, request, this._descriptor.bundledField, this._descriptor.subresponseField);
task = this._tasks[bundleId];
}
task.extend(bundledField, requestBytes, callback);
const ret = {
cancel() {
self._cancel(callback.id);
},
};
const countThreshold = this._options.elementCountThreshold || 0;
const sizeThreshold = this._options.requestByteThreshold || 0;
if ((countThreshold > 0 && task.getElementCount() >= countThreshold) ||
(sizeThreshold > 0 && task.getRequestByteSize() >= sizeThreshold)) {
this._runNow(bundleId);
return ret;
}
if (!(bundleId in this._timers) && this._options.delayThreshold > 0) {
this._timers[bundleId] = setTimeout(() => {
delete this._timers[bundleId];
this._runNow(bundleId);
}, this._options.delayThreshold);
}
return ret;
}
/**
* Clears scheduled timeout if it exists.
*
* @param {String} bundleId - the id for the task whose timeout needs to be
* cleared.
* @private
*/
_maybeClearTimeout(bundleId) {
if (bundleId in this._timers) {
const timerId = this._timers[bundleId];
delete this._timers[bundleId];
clearTimeout(timerId);
}
}
/**
* Cancels an event.
*
* @param {String} id - The id for the event in the task.
* @private
*/
_cancel(id) {
if (!(id in this._invocations)) {
return;
}
const bundleId = this._invocations[id];
if (!(bundleId in this._tasks)) {
return;
}
const task = this._tasks[bundleId];
delete this._invocations[id];
if (task.cancel(id)) {
this._maybeClearTimeout(bundleId);
delete this._tasks[bundleId];
}
}
/**
* Invokes a task.
*
* @param {String} bundleId - The id for the task.
* @private
*/
_runNow(bundleId) {
if (!(bundleId in this._tasks)) {
warnings_1.warn('bundle_runnow_bundleid_unknown', `No such bundleid: ${bundleId}`);
return;
}
this._maybeClearTimeout(bundleId);
const task = this._tasks[bundleId];
delete this._tasks[bundleId];
task.run().forEach(id => {
delete this._invocations[id];
});
}
}
exports.BundleExecutor = BundleExecutor;
//# sourceMappingURL=bundleExecutor.js.map

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,28 @@
/**
* Copyright 2020 Google LLC
*
* 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 { RequestType } from '../apitypes';
/**
* Compute the identifier of the `obj`. The objects of the same ID
* will be bundled together.
*
* @param {RequestType} obj - The request object.
* @param {String[]} discriminatorFields - The array of field names.
* A field name may include '.' as a separator, which is used to
* indicate object traversal.
* @return {String|undefined} - the identifier string, or undefined if any
* discriminator fields do not exist.
*/
export declare function computeBundleId(obj: RequestType, discriminatorFields: string[]): string | undefined;

View File

@ -0,0 +1,52 @@
"use strict";
/**
* Copyright 2020 Google LLC
*
* 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Provides behavior that supports request bundling.
*/
const at = require("lodash.at");
/**
* Compute the identifier of the `obj`. The objects of the same ID
* will be bundled together.
*
* @param {RequestType} obj - The request object.
* @param {String[]} discriminatorFields - The array of field names.
* A field name may include '.' as a separator, which is used to
* indicate object traversal.
* @return {String|undefined} - the identifier string, or undefined if any
* discriminator fields do not exist.
*/
function computeBundleId(obj, discriminatorFields) {
const ids = [];
let hasIds = false;
for (let i = 0; i < discriminatorFields.length; ++i) {
const id = at(obj, discriminatorFields[i])[0];
if (id === undefined) {
ids.push(null);
}
else {
hasIds = true;
ids.push(id);
}
}
if (!hasIds) {
return undefined;
}
return JSON.stringify(ids);
}
exports.computeBundleId = computeBundleId;
//# sourceMappingURL=bundlingUtils.js.map

View File

@ -0,0 +1 @@
{"version":3,"file":"bundlingUtils.js","sourceRoot":"","sources":["../../../src/bundlingCalls/bundlingUtils.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;AAEH;;GAEG;AAEH,gCAAiC;AAIjC;;;;;;;;;;GAUG;AACH,SAAgB,eAAe,CAC7B,GAAgB,EAChB,mBAA6B;IAE7B,MAAM,GAAG,GAAc,EAAE,CAAC;IAC1B,IAAI,MAAM,GAAG,KAAK,CAAC;IACnB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,mBAAmB,CAAC,MAAM,EAAE,EAAE,CAAC,EAAE;QACnD,MAAM,EAAE,GAAG,EAAE,CAAC,GAA0B,EAAE,mBAAmB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QACrE,IAAI,EAAE,KAAK,SAAS,EAAE;YACpB,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;SAChB;aAAM;YACL,MAAM,GAAG,IAAI,CAAC;YACd,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;SACd;KACF;IACD,IAAI,CAAC,MAAM,EAAE;QACX,OAAO,SAAS,CAAC;KAClB;IACD,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;AAC7B,CAAC;AAnBD,0CAmBC"}

View File

@ -0,0 +1,100 @@
/**
* Copyright 2020 Google LLC
*
* 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 { APICallback, GRPCCallResult, SimpleCallbackFunction } from '../apitypes';
export interface SubResponseInfo {
field: string;
start?: number;
end?: number;
}
export interface TaskElement {
}
export interface TaskData {
elements: TaskElement[];
bytes: number;
callback: TaskCallback;
cancelled?: boolean;
}
export interface TaskCallback extends APICallback {
id?: string;
}
/**
* Creates a deep copy of the object with the consideration of subresponse
* fields for bundling.
*
* @param {Object} obj - The source object.
* @param {Object?} subresponseInfo - The information to copy the subset of
* the field for the response. Do nothing if it's null.
* @param {String} subresponseInfo.field - The field name.
* @param {number} subresponseInfo.start - The offset where the copying
* element should starts with.
* @param {number} subresponseInfo.end - The ending index where the copying
* region of the elements ends.
* @return {Object} The copied object.
* @private
*/
export declare function deepCopyForResponse(obj: any, subresponseInfo: SubResponseInfo | null): any;
export declare class Task {
_apiCall: SimpleCallbackFunction;
_request: {
[index: string]: TaskElement[];
};
_bundledField: string;
_subresponseField?: string | null;
_data: TaskData[];
callCanceller?: GRPCCallResult;
/**
* A task coordinates the execution of a single bundle.
*
* @param {function} apiCall - The function to conduct calling API.
* @param {Object} bundlingRequest - The base request object to be used
* for the actual API call.
* @param {string} bundledField - The name of the field in bundlingRequest
* to be bundled.
* @param {string=} subresponseField - The name of the field in the response
* to be passed to the callback.
* @constructor
* @private
*/
constructor(apiCall: SimpleCallbackFunction, bundlingRequest: {}, bundledField: string, subresponseField?: string | null);
/**
* Returns the number of elements in a task.
* @return {number} The number of elements.
*/
getElementCount(): number;
/**
* Returns the total byte size of the elements in a task.
* @return {number} The byte size.
*/
getRequestByteSize(): number;
/**
* Invokes the actual API call with current elements.
* @return {string[]} - the list of ids for invocations to be run.
*/
run(): string[];
/**
* Appends the list of elements into the task.
* @param {Object[]} elements - the new list of elements.
* @param {number} bytes - the byte size required to encode elements in the API.
* @param {APICallback} callback - the callback of the method call.
*/
extend(elements: TaskElement[], bytes: number, callback: TaskCallback): void;
/**
* Cancels a part of elements.
* @param {string} id - The identifier of the part of elements.
* @return {boolean} Whether the entire task will be canceled or not.
*/
cancel(id: string): boolean;
}

226
node_modules/google-gax/build/src/bundlingCalls/task.js generated vendored Normal file
View File

@ -0,0 +1,226 @@
"use strict";
/**
* Copyright 2020 Google LLC
*
* 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const status_1 = require("../status");
const googleError_1 = require("../googleError");
/**
* Creates a deep copy of the object with the consideration of subresponse
* fields for bundling.
*
* @param {Object} obj - The source object.
* @param {Object?} subresponseInfo - The information to copy the subset of
* the field for the response. Do nothing if it's null.
* @param {String} subresponseInfo.field - The field name.
* @param {number} subresponseInfo.start - The offset where the copying
* element should starts with.
* @param {number} subresponseInfo.end - The ending index where the copying
* region of the elements ends.
* @return {Object} The copied object.
* @private
*/
function deepCopyForResponse(
// tslint:disable-next-line no-any
obj, subresponseInfo) {
// tslint:disable-next-line no-any
let result;
if (obj === null) {
return null;
}
if (obj === undefined) {
return undefined;
}
if (Array.isArray(obj)) {
result = [];
obj.forEach(element => {
result.push(deepCopyForResponse(element, null));
});
return result;
}
// Some objects (such as ByteBuffer) have copy method.
if (obj.copy !== undefined) {
return obj.copy();
}
// ArrayBuffer should be copied through slice().
if (obj instanceof ArrayBuffer) {
return obj.slice(0);
}
if (typeof obj === 'object') {
result = {};
Object.keys(obj).forEach(key => {
if (subresponseInfo &&
key === subresponseInfo.field &&
Array.isArray(obj[key])) {
// Note that subresponses are not deep-copied. This is safe because
// those subresponses are not shared among callbacks.
result[key] = obj[key].slice(subresponseInfo.start, subresponseInfo.end);
}
else {
result[key] = deepCopyForResponse(obj[key], null);
}
});
return result;
}
return obj;
}
exports.deepCopyForResponse = deepCopyForResponse;
class Task {
/**
* A task coordinates the execution of a single bundle.
*
* @param {function} apiCall - The function to conduct calling API.
* @param {Object} bundlingRequest - The base request object to be used
* for the actual API call.
* @param {string} bundledField - The name of the field in bundlingRequest
* to be bundled.
* @param {string=} subresponseField - The name of the field in the response
* to be passed to the callback.
* @constructor
* @private
*/
constructor(apiCall, bundlingRequest, bundledField, subresponseField) {
this._apiCall = apiCall;
this._request = bundlingRequest;
this._bundledField = bundledField;
this._subresponseField = subresponseField;
this._data = [];
}
/**
* Returns the number of elements in a task.
* @return {number} The number of elements.
*/
getElementCount() {
let count = 0;
for (let i = 0; i < this._data.length; ++i) {
count += this._data[i].elements.length;
}
return count;
}
/**
* Returns the total byte size of the elements in a task.
* @return {number} The byte size.
*/
getRequestByteSize() {
let size = 0;
for (let i = 0; i < this._data.length; ++i) {
size += this._data[i].bytes;
}
return size;
}
/**
* Invokes the actual API call with current elements.
* @return {string[]} - the list of ids for invocations to be run.
*/
run() {
if (this._data.length === 0) {
return [];
}
const request = this._request;
const elements = [];
const ids = [];
for (let i = 0; i < this._data.length; ++i) {
elements.push.apply(elements, this._data[i].elements);
ids.push(this._data[i].callback.id);
}
request[this._bundledField] = elements;
const self = this;
this.callCanceller = this._apiCall(request, (err, response) => {
const responses = [];
if (err) {
self._data.forEach(() => {
responses.push(undefined);
});
}
else {
let subresponseInfo = null;
if (self._subresponseField) {
subresponseInfo = {
field: self._subresponseField,
start: 0,
};
}
self._data.forEach(data => {
if (subresponseInfo) {
subresponseInfo.end =
subresponseInfo.start + data.elements.length;
}
responses.push(deepCopyForResponse(response, subresponseInfo));
if (subresponseInfo) {
subresponseInfo.start = subresponseInfo.end;
}
});
}
for (let i = 0; i < self._data.length; ++i) {
if (self._data[i].cancelled) {
const error = new googleError_1.GoogleError('cancelled');
error.code = status_1.Status.CANCELLED;
self._data[i].callback(error);
}
else {
self._data[i].callback(err, responses[i]);
}
}
});
return ids;
}
/**
* Appends the list of elements into the task.
* @param {Object[]} elements - the new list of elements.
* @param {number} bytes - the byte size required to encode elements in the API.
* @param {APICallback} callback - the callback of the method call.
*/
extend(elements, bytes, callback) {
this._data.push({
elements,
bytes,
callback,
});
}
/**
* Cancels a part of elements.
* @param {string} id - The identifier of the part of elements.
* @return {boolean} Whether the entire task will be canceled or not.
*/
cancel(id) {
if (this.callCanceller) {
let allCancelled = true;
this._data.forEach(d => {
if (d.callback.id === id) {
d.cancelled = true;
}
if (!d.cancelled) {
allCancelled = false;
}
});
if (allCancelled) {
this.callCanceller.cancel();
}
return allCancelled;
}
for (let i = 0; i < this._data.length; ++i) {
if (this._data[i].callback.id === id) {
const error = new googleError_1.GoogleError('cancelled');
error.code = status_1.Status.CANCELLED;
this._data[i].callback(error);
this._data.splice(i, 1);
break;
}
}
return this._data.length === 0;
}
}
exports.Task = Task;
//# sourceMappingURL=task.js.map

File diff suppressed because one or more lines are too long