1
0
mirror of https://github.com/musix-org/musix-oss synced 2025-06-17 13:56:01 +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,131 @@
/*!
* Copyright 2015 Google Inc. 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.
*/
/// <reference types="node" />
import { TransformOptions } from 'stream';
import { ResourceStream } from './resource-stream';
export interface ParsedArguments extends TransformOptions {
/**
* Query object. This is most commonly an object, but to make the API more
* simple, it can also be a string in some places.
*/
query?: ParsedArguments;
/**
* Callback function.
*/
callback?: Function;
/**
* Auto-pagination enabled.
*/
autoPaginate?: boolean;
/**
* Maximum API calls to make.
*/
maxApiCalls?: number;
/**
* Maximum results to return.
*/
maxResults?: number;
pageSize?: number;
streamOptions?: ParsedArguments;
}
/*! Developer Documentation
*
* paginator is used to auto-paginate `nextQuery` methods as well as
* streamifying them.
*
* Before:
*
* search.query('done=true', function(err, results, nextQuery) {
* search.query(nextQuery, function(err, results, nextQuery) {});
* });
*
* After:
*
* search.query('done=true', function(err, results) {});
*
* Methods to extend should be written to accept callbacks and return a
* `nextQuery`.
*/
export declare class Paginator {
/**
* Cache the original method, then overwrite it on the Class's prototype.
*
* @param {function} Class - The parent class of the methods to extend.
* @param {string|string[]} methodNames - Name(s) of the methods to extend.
*/
extend(Class: Function, methodNames: string | string[]): void;
/**
* Wraps paginated API calls in a readable object stream.
*
* This method simply calls the nextQuery recursively, emitting results to a
* stream. The stream ends when `nextQuery` is null.
*
* `maxResults` will act as a cap for how many results are fetched and emitted
* to the stream.
*
* @param {string} methodName - Name of the method to streamify.
* @return {function} - Wrapped function.
*/
streamify<T = any>(methodName: string): (this: {
[index: string]: Function;
}, ...args: any[]) => ResourceStream<T>;
/**
* Parse a pseudo-array `arguments` for a query and callback.
*
* @param {array} args - The original `arguments` pseduo-array that the original
* method received.
*/
parseArguments_(args: any[]): ParsedArguments;
/**
* This simply checks to see if `autoPaginate` is set or not, if it's true
* then we buffer all results, otherwise simply call the original method.
*
* @param {array} parsedArguments - Parsed arguments from the original method
* call.
* @param {object=|string=} parsedArguments.query - Query object. This is most
* commonly an object, but to make the API more simple, it can also be a
* string in some places.
* @param {function=} parsedArguments.callback - Callback function.
* @param {boolean} parsedArguments.autoPaginate - Auto-pagination enabled.
* @param {boolean} parsedArguments.maxApiCalls - Maximum API calls to make.
* @param {number} parsedArguments.maxResults - Maximum results to return.
* @param {function} originalMethod - The cached method that accepts a callback
* and returns `nextQuery` to receive more results.
*/
run_(parsedArguments: ParsedArguments, originalMethod: Function): any;
/**
* This method simply calls the nextQuery recursively, emitting results to a
* stream. The stream ends when `nextQuery` is null.
*
* `maxResults` will act as a cap for how many results are fetched and emitted
* to the stream.
*
* @param {object=|string=} parsedArguments.query - Query object. This is most
* commonly an object, but to make the API more simple, it can also be a
* string in some places.
* @param {function=} parsedArguments.callback - Callback function.
* @param {boolean} parsedArguments.autoPaginate - Auto-pagination enabled.
* @param {boolean} parsedArguments.maxApiCalls - Maximum API calls to make.
* @param {number} parsedArguments.maxResults - Maximum results to return.
* @param {function} originalMethod - The cached method that accepts a callback
* and returns `nextQuery` to receive more results.
* @return {stream} - Readable object stream.
*/
runAsStream_<T = any>(parsedArguments: ParsedArguments, originalMethod: Function): ResourceStream<T>;
}
declare const paginator: Paginator;
export { paginator };
export { ResourceStream };

205
node_modules/@google-cloud/paginator/build/src/index.js generated vendored Normal file
View File

@ -0,0 +1,205 @@
"use strict";
/*!
* Copyright 2015 Google Inc. 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
/*!
* @module common/paginator
*/
const arrify = require("arrify");
const extend = require("extend");
const resource_stream_1 = require("./resource-stream");
exports.ResourceStream = resource_stream_1.ResourceStream;
/*! Developer Documentation
*
* paginator is used to auto-paginate `nextQuery` methods as well as
* streamifying them.
*
* Before:
*
* search.query('done=true', function(err, results, nextQuery) {
* search.query(nextQuery, function(err, results, nextQuery) {});
* });
*
* After:
*
* search.query('done=true', function(err, results) {});
*
* Methods to extend should be written to accept callbacks and return a
* `nextQuery`.
*/
class Paginator {
/**
* Cache the original method, then overwrite it on the Class's prototype.
*
* @param {function} Class - The parent class of the methods to extend.
* @param {string|string[]} methodNames - Name(s) of the methods to extend.
*/
// tslint:disable-next-line:variable-name
extend(Class, methodNames) {
methodNames = arrify(methodNames);
methodNames.forEach(methodName => {
const originalMethod = Class.prototype[methodName];
// map the original method to a private member
Class.prototype[methodName + '_'] = originalMethod;
// overwrite the original to auto-paginate
// tslint:disable-next-line:no-any
Class.prototype[methodName] = function (...args) {
const parsedArguments = paginator.parseArguments_(args);
return paginator.run_(parsedArguments, originalMethod.bind(this));
};
});
}
/**
* Wraps paginated API calls in a readable object stream.
*
* This method simply calls the nextQuery recursively, emitting results to a
* stream. The stream ends when `nextQuery` is null.
*
* `maxResults` will act as a cap for how many results are fetched and emitted
* to the stream.
*
* @param {string} methodName - Name of the method to streamify.
* @return {function} - Wrapped function.
*/
// tslint:disable-next-line:no-any
streamify(methodName) {
return function (
// tslint:disable-next-line:no-any
...args) {
const parsedArguments = paginator.parseArguments_(args);
const originalMethod = this[methodName + '_'] || this[methodName];
return paginator.runAsStream_(parsedArguments, originalMethod.bind(this));
};
}
/**
* Parse a pseudo-array `arguments` for a query and callback.
*
* @param {array} args - The original `arguments` pseduo-array that the original
* method received.
*/
// tslint:disable-next-line:no-any
parseArguments_(args) {
let query;
let autoPaginate = true;
let maxApiCalls = -1;
let maxResults = -1;
let callback;
const firstArgument = args[0];
const lastArgument = args[args.length - 1];
if (typeof firstArgument === 'function') {
callback = firstArgument;
}
else {
query = firstArgument;
}
if (typeof lastArgument === 'function') {
callback = lastArgument;
}
if (typeof query === 'object') {
query = extend(true, {}, query);
// Check if the user only asked for a certain amount of results.
if (query.maxResults && typeof query.maxResults === 'number') {
// `maxResults` is used API-wide.
maxResults = query.maxResults;
}
else if (typeof query.pageSize === 'number') {
// `pageSize` is Pub/Sub's `maxResults`.
maxResults = query.pageSize;
}
if (query.maxApiCalls && typeof query.maxApiCalls === 'number') {
maxApiCalls = query.maxApiCalls;
delete query.maxApiCalls;
}
// maxResults is the user specified limit.
if (maxResults !== -1 || query.autoPaginate === false) {
autoPaginate = false;
}
}
const parsedArguments = {
query: query || {},
autoPaginate,
maxApiCalls,
maxResults,
callback,
};
parsedArguments.streamOptions = extend(true, {}, parsedArguments.query);
delete parsedArguments.streamOptions.autoPaginate;
delete parsedArguments.streamOptions.maxResults;
delete parsedArguments.streamOptions.pageSize;
return parsedArguments;
}
/**
* This simply checks to see if `autoPaginate` is set or not, if it's true
* then we buffer all results, otherwise simply call the original method.
*
* @param {array} parsedArguments - Parsed arguments from the original method
* call.
* @param {object=|string=} parsedArguments.query - Query object. This is most
* commonly an object, but to make the API more simple, it can also be a
* string in some places.
* @param {function=} parsedArguments.callback - Callback function.
* @param {boolean} parsedArguments.autoPaginate - Auto-pagination enabled.
* @param {boolean} parsedArguments.maxApiCalls - Maximum API calls to make.
* @param {number} parsedArguments.maxResults - Maximum results to return.
* @param {function} originalMethod - The cached method that accepts a callback
* and returns `nextQuery` to receive more results.
*/
run_(parsedArguments, originalMethod) {
const query = parsedArguments.query;
const callback = parsedArguments.callback;
if (!parsedArguments.autoPaginate) {
return originalMethod(query, callback);
}
const results = new Array();
const promise = new Promise((resolve, reject) => {
paginator
.runAsStream_(parsedArguments, originalMethod)
.on('error', reject)
.on('data', (data) => results.push(data))
.on('end', () => resolve(results));
});
if (!callback) {
return promise.then(results => [results]);
}
promise.then(results => callback(null, results), (err) => callback(err));
}
/**
* This method simply calls the nextQuery recursively, emitting results to a
* stream. The stream ends when `nextQuery` is null.
*
* `maxResults` will act as a cap for how many results are fetched and emitted
* to the stream.
*
* @param {object=|string=} parsedArguments.query - Query object. This is most
* commonly an object, but to make the API more simple, it can also be a
* string in some places.
* @param {function=} parsedArguments.callback - Callback function.
* @param {boolean} parsedArguments.autoPaginate - Auto-pagination enabled.
* @param {boolean} parsedArguments.maxApiCalls - Maximum API calls to make.
* @param {number} parsedArguments.maxResults - Maximum results to return.
* @param {function} originalMethod - The cached method that accepts a callback
* and returns `nextQuery` to receive more results.
* @return {stream} - Readable object stream.
*/
// tslint:disable-next-line:no-any
runAsStream_(parsedArguments, originalMethod) {
return new resource_stream_1.ResourceStream(parsedArguments, originalMethod);
}
}
exports.Paginator = Paginator;
const paginator = new Paginator();
exports.paginator = paginator;
//# sourceMappingURL=index.js.map

View File

@ -0,0 +1,40 @@
/*!
* Copyright 2019 Google Inc. 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.
*/
/// <reference types="node" />
import { Transform } from 'stream';
import { ParsedArguments } from './';
interface ResourceEvents<T> {
addListener(event: 'data', listener: (data: T) => void): this;
emit(event: 'data', data: T): boolean;
on(event: 'data', listener: (data: T) => void): this;
once(event: 'data', listener: (data: T) => void): this;
prependListener(event: 'data', listener: (data: T) => void): this;
prependOnceListener(event: 'data', listener: (data: T) => void): this;
removeListener(event: 'data', listener: (data: T) => void): this;
}
export declare class ResourceStream<T> extends Transform implements ResourceEvents<T> {
_ended: boolean;
_maxApiCalls: number;
_nextQuery: {} | null;
_reading: boolean;
_requestFn: Function;
_requestsMade: number;
_resultsToSend: number;
constructor(args: ParsedArguments, requestFn: Function);
end(...args: any[]): void;
_read(): void;
}
export {};

View File

@ -0,0 +1,71 @@
"use strict";
/*!
* Copyright 2019 Google Inc. 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const stream_1 = require("stream");
class ResourceStream extends stream_1.Transform {
constructor(args, requestFn) {
const options = Object.assign({ objectMode: true }, args.streamOptions);
super(options);
this._ended = false;
this._maxApiCalls = args.maxApiCalls === -1 ? Infinity : args.maxApiCalls;
this._nextQuery = args.query;
this._reading = false;
this._requestFn = requestFn;
this._requestsMade = 0;
this._resultsToSend = args.maxResults === -1 ? Infinity : args.maxResults;
}
// tslint:disable-next-line:no-any
end(...args) {
this._ended = true;
return super.end(...args);
}
_read() {
if (this._reading) {
return;
}
this._reading = true;
this._requestFn(this._nextQuery, (err, results, nextQuery) => {
if (err) {
this.destroy(err);
return;
}
this._nextQuery = nextQuery;
if (this._resultsToSend !== Infinity) {
results = results.splice(0, this._resultsToSend);
this._resultsToSend -= results.length;
}
let more = true;
for (const result of results) {
if (this._ended) {
break;
}
more = this.push(result);
}
const isFinished = !this._nextQuery || this._resultsToSend < 1;
const madeMaxCalls = ++this._requestsMade >= this._maxApiCalls;
if (isFinished || madeMaxCalls) {
this.end();
}
if (more && !this._ended) {
setImmediate(() => this._read());
}
this._reading = false;
});
}
}
exports.ResourceStream = ResourceStream;
//# sourceMappingURL=resource-stream.js.map