From 7a94c1e578e3f6761b29cb9e4dd5446de44c4837 Mon Sep 17 00:00:00 2001 From: "renovate[bot]" Date: Fri, 3 May 2019 11:07:20 -0700 Subject: [PATCH] chore(deps): update dependency gts to v1 (#459) --- package.json | 2 +- src/app-profile.ts | 65 +- src/chunktransformer.ts | 153 +-- src/cluster.ts | 202 ++-- src/family.ts | 61 +- src/filter.ts | 189 ++-- src/index.ts | 391 +++---- src/instance.ts | 658 ++++++------ src/mutation.ts | 53 +- src/row.ts | 486 +++++---- src/table.ts | 1139 +++++++++++---------- system-test/bigtable.ts | 111 +- system-test/mutate-rows.ts | 18 +- system-test/read-rows-acceptance-tests.ts | 122 +-- system-test/read-rows.ts | 19 +- test/app-profile.ts | 62 +- test/chunktransformer.ts | 609 ++++++----- test/cluster.ts | 28 +- test/family.ts | 24 +- test/filter.ts | 15 +- test/gapic-v2-admin.ts | 1070 +++++++++++-------- test/index.ts | 193 ++-- test/instance.ts | 93 +- test/mutation.ts | 18 +- test/row.ts | 145 +-- test/table.ts | 464 +++++---- 26 files changed, 3462 insertions(+), 2928 deletions(-) diff --git a/package.json b/package.json index 94a999ff3..7e50a052a 100644 --- a/package.json +++ b/package.json @@ -87,7 +87,7 @@ "eslint-config-prettier": "^4.0.0", "eslint-plugin-node": "^9.0.0", "eslint-plugin-prettier": "^3.0.0", - "gts": "^0.9.0", + "gts": "^1.0.0", "intelli-espower-loader": "^1.0.1", "jsdoc": "^3.5.5", "jsdoc-baseline": "git+https://github.com/hegemonic/jsdoc-baseline.git", diff --git a/src/app-profile.ts b/src/app-profile.ts index c7c594ba8..a2fc46a2e 100644 --- a/src/app-profile.ts +++ b/src/app-profile.ts @@ -52,7 +52,8 @@ export class AppProfile { } else { throw new Error(`AppProfile id '${id}' is not formatted correctly. Please use the format 'my-app-profile' or '${ - instance.name}/appProfiles/my-app-profile'.`); + instance.name + }/appProfiles/my-app-profile'.`); } } else { name = `${instance.name}/appProfiles/${id}`; @@ -108,11 +109,12 @@ Please use the format 'my-app-profile' or '${ }; if (is.boolean(options.allowTransactionalWrites)) { appProfile.singleClusterRouting.allowTransactionalWrites = - options.allowTransactionalWrites; + options.allowTransactionalWrites; } } else { throw new Error( - 'An app profile routing policy can only contain "any" or a `Cluster`.'); + 'An app profile routing policy can only contain "any" or a `Cluster`.' + ); } } @@ -172,13 +174,14 @@ Please use the format 'my-app-profile' or '${ } this.bigtable.request( - { - client: 'BigtableInstanceAdminClient', - method: 'deleteAppProfile', - reqOpts, - gaxOpts: options.gaxOptions, - }, - callback); + { + client: 'BigtableInstanceAdminClient', + method: 'deleteAppProfile', + reqOpts, + gaxOpts: options.gaxOptions, + }, + callback + ); } /** @@ -259,21 +262,22 @@ Please use the format 'my-app-profile' or '${ } this.bigtable.request( - { - client: 'BigtableInstanceAdminClient', - method: 'getAppProfile', - reqOpts: { - name: this.name, - }, - gaxOpts: gaxOptions, + { + client: 'BigtableInstanceAdminClient', + method: 'getAppProfile', + reqOpts: { + name: this.name, }, - (...args) => { - if (args[1]) { - this.metadata = args[1]; - } + gaxOpts: gaxOptions, + }, + (...args) => { + if (args[1]) { + this.metadata = args[1]; + } - callback(...args); - }); + callback(...args); + } + ); } /** @@ -323,13 +327,14 @@ Please use the format 'my-app-profile' or '${ } this.bigtable.request( - { - client: 'BigtableInstanceAdminClient', - method: 'updateAppProfile', - reqOpts, - gaxOpts: gaxOptions, - }, - callback); + { + client: 'BigtableInstanceAdminClient', + method: 'updateAppProfile', + reqOpts, + gaxOpts: gaxOptions, + }, + callback + ); } } diff --git a/src/chunktransformer.ts b/src/chunktransformer.ts index 48c643858..5e0f02aa1 100644 --- a/src/chunktransformer.ts +++ b/src/chunktransformer.ts @@ -17,18 +17,18 @@ import {Transform, TransformOptions} from 'stream'; import {Bytes, Mutation} from './mutation'; -export type Value = string|number|boolean|Uint8Array; +export type Value = string | number | boolean | Uint8Array; export interface Chunk { rowContents: Value; commitRow: boolean; resetRow: boolean; - rowKey?: string|Uint8Array; + rowKey?: string | Uint8Array; familyName?: {value: string}; - qualifier?: Qualifier|{value: Value}; - timestampMicros?: number|Long; + qualifier?: Qualifier | {value: Value}; + timestampMicros?: number | Long; labels?: string[]; - value?: string|Buffer; + value?: string | Buffer; valueSize?: number; } export interface Data { @@ -39,9 +39,9 @@ interface Family { [qualifier: string]: Qualifier[]; } export interface Qualifier { - value?: string|Buffer; + value?: string | Buffer; labels?: string[]; - timestamp?: number|Long; + timestamp?: number | Long; size?: number; } export interface Row { @@ -50,7 +50,7 @@ export interface Row { } export interface TransformErrorProps { message: string; - chunk: Chunk|null; + chunk: Chunk | null; } class TransformError extends Error { @@ -88,7 +88,7 @@ export class ChunkTransformer extends Transform { qualifiers?: Qualifier[]; qualifier?: Qualifier; constructor(options: TransformOptions = {}) { - options.objectMode = true; // forcing object mode + options.objectMode = true; // forcing object mode super(options); this.options = options; this._destroyed = false; @@ -103,10 +103,12 @@ export class ChunkTransformer extends Transform { */ _flush(cb: Function): void { if (typeof this.row!.key !== 'undefined') { - this.destroy(new TransformError({ - message: 'Response ended with pending row without commit', - chunk: null, - })); + this.destroy( + new TransformError({ + message: 'Response ended with pending row without commit', + chunk: null, + }) + ); return; } cb(); @@ -149,10 +151,12 @@ export class ChunkTransformer extends Transform { } } if (data.lastScannedRowKey && data.lastScannedRowKey.length > 0) { - this.lastRowKey = - Mutation.convertFromBytes(data.lastScannedRowKey as Bytes, { - userOptions: this.options, - }); + this.lastRowKey = Mutation.convertFromBytes( + data.lastScannedRowKey as Bytes, + { + userOptions: this.options, + } + ); } next(); } @@ -200,10 +204,12 @@ export class ChunkTransformer extends Transform { */ validateValueSizeAndCommitRow(chunk: Chunk): void { if (chunk.valueSize! > 0 && chunk.commitRow) { - this.destroy(new TransformError({ - message: 'A row cannot be have a value size and be a commit row', - chunk, - })); + this.destroy( + new TransformError({ + message: 'A row cannot be have a value size and be a commit row', + chunk, + }) + ); } } @@ -213,14 +219,19 @@ export class ChunkTransformer extends Transform { * @param {chunk} chunk chunk to validate for resetrow */ validateResetRow(chunk: Chunk): void { - const containsData = (chunk.rowKey && chunk.rowKey.length !== 0) || - chunk.familyName || chunk.qualifier || - (chunk.value && chunk.value.length !== 0) || chunk.timestampMicros! > 0; + const containsData = + (chunk.rowKey && chunk.rowKey.length !== 0) || + chunk.familyName || + chunk.qualifier || + (chunk.value && chunk.value.length !== 0) || + chunk.timestampMicros! > 0; if (chunk.resetRow && containsData) { - this.destroy(new TransformError({ - message: 'A reset should have no data', - chunk, - })); + this.destroy( + new TransformError({ + message: 'A reset should have no data', + chunk, + }) + ); } } @@ -230,16 +241,18 @@ export class ChunkTransformer extends Transform { * @param {chunk} chunk chunk to validate * @param {newRowKey} newRowKey newRowKey of the new row */ - validateNewRow(chunk: Chunk, newRowKey: string|Buffer): void { + validateNewRow(chunk: Chunk, newRowKey: string | Buffer): void { const row = this.row; const lastRowKey = this.lastRowKey; - let errorMessage: string|undefined; + let errorMessage: string | undefined; if (typeof row!.key !== 'undefined') { errorMessage = 'A new row cannot have existing state'; } else if ( - typeof chunk.rowKey === 'undefined' || chunk.rowKey.length === 0 || - newRowKey.length === 0) { + typeof chunk.rowKey === 'undefined' || + chunk.rowKey.length === 0 || + newRowKey.length === 0 + ) { errorMessage = 'A row key must be set'; } else if (chunk.resetRow) { errorMessage = 'A new row cannot be reset'; @@ -269,21 +282,31 @@ export class ChunkTransformer extends Transform { userOptions: this.options, }); const oldRowKey = row!.key || ''; - if (newRowKey && chunk.rowKey && (newRowKey as string).length !== 0 && - newRowKey.toString() !== oldRowKey.toString()) { - this.destroy(new TransformError({ - message: 'A commit is required between row keys', - chunk, - })); + if ( + newRowKey && + chunk.rowKey && + (newRowKey as string).length !== 0 && + newRowKey.toString() !== oldRowKey.toString() + ) { + this.destroy( + new TransformError({ + message: 'A commit is required between row keys', + chunk, + }) + ); return; } } - if (chunk.familyName && - (chunk.qualifier === null || chunk.qualifier === undefined)) { - this.destroy(new TransformError({ - message: 'A qualifier must be specified', - chunk, - })); + if ( + chunk.familyName && + (chunk.qualifier === null || chunk.qualifier === undefined) + ) { + this.destroy( + new TransformError({ + message: 'A qualifier must be specified', + chunk, + }) + ); return; } this.validateResetRow(chunk); @@ -335,11 +358,13 @@ export class ChunkTransformer extends Transform { row!.key = newRowKey; row!.data = {} as Data; this.family = row!.data![chunk.familyName.value] = {} as Family; - const qualifierName = - Mutation.convertFromBytes(chunk.qualifier.value as Bytes, { - userOptions: this.options, - }); - this.qualifiers = this.family[qualifierName as {} as string] = []; + const qualifierName = Mutation.convertFromBytes( + chunk.qualifier.value as Bytes, + { + userOptions: this.options, + } + ); + this.qualifiers = this.family[(qualifierName as {}) as string] = []; this.qualifier = { value: Mutation.convertFromBytes(chunk.value! as Bytes, { userOptions: this.options, @@ -366,15 +391,17 @@ export class ChunkTransformer extends Transform { const row = this.row; if (chunk.familyName) { this.family = row!.data![chunk.familyName.value] = - row!.data![chunk.familyName.value] || {}; + row!.data![chunk.familyName.value] || {}; } if (chunk.qualifier) { - const qualifierName = - Mutation.convertFromBytes(chunk.qualifier.value as Bytes, { - userOptions: this.options, - }) as string; + const qualifierName = Mutation.convertFromBytes( + chunk.qualifier.value as Bytes, + { + userOptions: this.options, + } + ) as string; this.qualifiers = this.family![qualifierName] = - this.family![qualifierName] || []; + this.family![qualifierName] || []; } this.qualifier = { value: Mutation.convertFromBytes(chunk.value! as Bytes, { @@ -398,13 +425,17 @@ export class ChunkTransformer extends Transform { if (chunk.resetRow) { return this.reset(); } - const chunkQualifierValue = - Mutation.convertFromBytes(chunk.value! as Bytes, { - userOptions: this.options, - }); + const chunkQualifierValue = Mutation.convertFromBytes( + chunk.value! as Bytes, + { + userOptions: this.options, + } + ); - if (chunkQualifierValue instanceof Buffer && - this.qualifier!.value instanceof Buffer) { + if ( + chunkQualifierValue instanceof Buffer && + this.qualifier!.value instanceof Buffer + ) { this.qualifier!.value = Buffer.concat([ this.qualifier!.value, chunkQualifierValue, diff --git a/src/cluster.ts b/src/cluster.ts index 9709e4d4e..7655c3168 100644 --- a/src/cluster.ts +++ b/src/cluster.ts @@ -23,14 +23,17 @@ import {Bigtable} from '.'; import {Instance} from './instance'; export interface GenericCallback { - (err?: ServiceError|null, apiResponse?: T|null): void; + (err?: ServiceError | null, apiResponse?: T | null): void; } export interface GenericClusterCallback { - (err?: ServiceError|null, cluster?: Cluster|null, apiResponse?: T|null); + (err?: ServiceError | null, cluster?: Cluster | null, apiResponse?: T | null); } export interface GenericOperationCallback { - (err?: ServiceError|null, operation?: GaxOperation|null, - apiResponse?: T|null): void; + ( + err?: ServiceError | null, + operation?: GaxOperation | null, + apiResponse?: T | null + ): void; } export type IEmpty = btTypes.protobuf.IEmpty; @@ -58,8 +61,11 @@ export interface CreateClusterOptions { export interface GetClusterMetadataCallback { // tslint:disable-next-line no-any (...args: any[]): void; - (err: ServiceError|null, metadata?: ICluster|null, - apiResponse?: IOperation|null): void; + ( + err: ServiceError | null, + metadata?: ICluster | null, + apiResponse?: IOperation | null + ): void; } export interface Metadata extends CreateClusterOptions { displayName?: string; @@ -173,13 +179,15 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`); * region_tag:bigtable_create_cluster */ create( - optionsOrCallback?: CreateClusterOptions|CreateClusterCallback, - cb?: CreateClusterCallback): void|Promise { + optionsOrCallback?: CreateClusterOptions | CreateClusterCallback, + cb?: CreateClusterCallback + ): void | Promise { const callback = - typeof optionsOrCallback === 'function' ? optionsOrCallback : cb!; - const options = typeof optionsOrCallback === 'object' && optionsOrCallback ? - optionsOrCallback : - {} as CreateClusterOptions; + typeof optionsOrCallback === 'function' ? optionsOrCallback : cb!; + const options = + typeof optionsOrCallback === 'object' && optionsOrCallback + ? optionsOrCallback + : ({} as CreateClusterOptions); this.instance.createCluster(this.id, options, callback); } @@ -202,25 +210,27 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`); * region_tag:bigtable_delete_cluster */ delete( - gaxOptionsOrCallback?: CallOptions|DeleteClusterCallback, - cb?: DeleteClusterCallback): void|Promise { + gaxOptionsOrCallback?: CallOptions | DeleteClusterCallback, + cb?: DeleteClusterCallback + ): void | Promise { const callback = - typeof gaxOptionsOrCallback === 'function' ? gaxOptionsOrCallback : cb!; + typeof gaxOptionsOrCallback === 'function' ? gaxOptionsOrCallback : cb!; const gaxOptions = - typeof gaxOptionsOrCallback === 'object' && gaxOptionsOrCallback ? - gaxOptionsOrCallback : - {} as CallOptions; + typeof gaxOptionsOrCallback === 'object' && gaxOptionsOrCallback + ? gaxOptionsOrCallback + : ({} as CallOptions); this.bigtable.request( - { - client: 'BigtableInstanceAdminClient', - method: 'deleteCluster', - reqOpts: { - name: this.name, - }, - gaxOpts: gaxOptions, + { + client: 'BigtableInstanceAdminClient', + method: 'deleteCluster', + reqOpts: { + name: this.name, }, - callback); + gaxOpts: gaxOptions, + }, + callback + ); } exists(): Promise; @@ -241,16 +251,17 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`); * region_tag:bigtable_exists_cluster */ exists( - gaxOptionsOrCallback?: CallOptions|ExistsClusterCallback, - cb?: ExistsClusterCallback): void|Promise { + gaxOptionsOrCallback?: CallOptions | ExistsClusterCallback, + cb?: ExistsClusterCallback + ): void | Promise { const callback = - typeof gaxOptionsOrCallback === 'function' ? gaxOptionsOrCallback : cb!; + typeof gaxOptionsOrCallback === 'function' ? gaxOptionsOrCallback : cb!; const gaxOptions = - typeof gaxOptionsOrCallback === 'object' && gaxOptionsOrCallback ? - gaxOptionsOrCallback : - {} as CallOptions; + typeof gaxOptionsOrCallback === 'object' && gaxOptionsOrCallback + ? gaxOptionsOrCallback + : ({} as CallOptions); - this.getMetadata(gaxOptions, (err?: ServiceError|null) => { + this.getMetadata(gaxOptions, (err?: ServiceError | null) => { if (err) { if (err.code === 5) { callback(null, false); @@ -282,26 +293,32 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`); * @example include:samples/document-snippets/cluster.js * region_tag:bigtable_get_cluster */ - get(gaxOptionsOrCallback?: CallOptions|GetClusterCallback, - cb?: GetClusterCallback): void|Promise { + get( + gaxOptionsOrCallback?: CallOptions | GetClusterCallback, + cb?: GetClusterCallback + ): void | Promise { const callback = - typeof gaxOptionsOrCallback === 'function' ? gaxOptionsOrCallback : cb!; + typeof gaxOptionsOrCallback === 'function' ? gaxOptionsOrCallback : cb!; const gaxOptions = - typeof gaxOptionsOrCallback === 'object' && gaxOptionsOrCallback ? - gaxOptionsOrCallback : - {} as CallOptions; + typeof gaxOptionsOrCallback === 'object' && gaxOptionsOrCallback + ? gaxOptionsOrCallback + : ({} as CallOptions); this.getMetadata( - gaxOptions, (err?: ServiceError|null, metadata?: ICluster|null) => { - callback(err, err ? null : this, metadata); - }); + gaxOptions, + (err?: ServiceError | null, metadata?: ICluster | null) => { + callback(err, err ? null : this, metadata); + } + ); } getMetadata(): Promise; getMetadata(gaxOptions: CallOptions): Promise; getMetadata(callback: GetClusterMetadataCallback): void; - getMetadata(gaxOptions: CallOptions, callback: GetClusterMetadataCallback): - void; + getMetadata( + gaxOptions: CallOptions, + callback: GetClusterMetadataCallback + ): void; /** * Get the cluster metadata. * @@ -317,43 +334,50 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`); * region_tag:bigtable_cluster_get_meta */ getMetadata( - gaxOptionsOrCallback?: CallOptions|GetClusterMetadataCallback, - cb?: GetClusterMetadataCallback): void|Promise { + gaxOptionsOrCallback?: CallOptions | GetClusterMetadataCallback, + cb?: GetClusterMetadataCallback + ): void | Promise { const callback = - typeof gaxOptionsOrCallback === 'function' ? gaxOptionsOrCallback : cb!; + typeof gaxOptionsOrCallback === 'function' ? gaxOptionsOrCallback : cb!; const gaxOptions = - typeof gaxOptionsOrCallback === 'object' && gaxOptionsOrCallback ? - gaxOptionsOrCallback : - {} as CallOptions; + typeof gaxOptionsOrCallback === 'object' && gaxOptionsOrCallback + ? gaxOptionsOrCallback + : ({} as CallOptions); this.bigtable.request( - { - client: 'BigtableInstanceAdminClient', - method: 'getCluster', - reqOpts: { - name: this.name, - }, - gaxOpts: gaxOptions, + { + client: 'BigtableInstanceAdminClient', + method: 'getCluster', + reqOpts: { + name: this.name, }, - // tslint:disable-next-line no-any - (...args: any[]) => { - if (args[1]) { - this.metadata = args[1]; - } - - callback(...args); - }); + gaxOpts: gaxOptions, + }, + // tslint:disable-next-line no-any + (...args: any[]) => { + if (args[1]) { + this.metadata = args[1]; + } + + callback(...args); + } + ); } setMetadata(metadata: CreateClusterOptions): Promise; - setMetadata(metadata: CreateClusterOptions, gaxOptions: CallOptions): - Promise; setMetadata( - metadata: CreateClusterOptions, - callback: SetClusterMetadataCallback): void; + metadata: CreateClusterOptions, + gaxOptions: CallOptions + ): Promise; + setMetadata( + metadata: CreateClusterOptions, + callback: SetClusterMetadataCallback + ): void; setMetadata( - metadata: CreateClusterOptions, gaxOptions: CallOptions, - callback: SetClusterMetadataCallback): void; + metadata: CreateClusterOptions, + gaxOptions: CallOptions, + callback: SetClusterMetadataCallback + ): void; /** * Set the cluster metadata. * @@ -371,15 +395,16 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`); * region_tag:bigtable_cluster_set_meta */ setMetadata( - metadata: CreateClusterOptions, - gaxOptionsOrCallback?: CallOptions|SetClusterMetadataCallback, - cb?: SetClusterMetadataCallback): void|Promise { + metadata: CreateClusterOptions, + gaxOptionsOrCallback?: CallOptions | SetClusterMetadataCallback, + cb?: SetClusterMetadataCallback + ): void | Promise { const callback = - typeof gaxOptionsOrCallback === 'function' ? gaxOptionsOrCallback : cb!; + typeof gaxOptionsOrCallback === 'function' ? gaxOptionsOrCallback : cb!; const gaxOptions = - typeof gaxOptionsOrCallback === 'object' && gaxOptionsOrCallback ? - gaxOptionsOrCallback : - {} as CallOptions; + typeof gaxOptionsOrCallback === 'object' && gaxOptionsOrCallback + ? gaxOptionsOrCallback + : ({} as CallOptions); // tslint:disable-next-line no-any const reqOpts: any = { @@ -387,8 +412,10 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`); }; if (metadata.location) { - reqOpts.location = - Cluster.getLocation_(this.bigtable.projectId, metadata.location); + reqOpts.location = Cluster.getLocation_( + this.bigtable.projectId, + metadata.location + ); } if (metadata.nodes) { @@ -400,13 +427,14 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.`); } this.bigtable.request( - { - client: 'BigtableInstanceAdminClient', - method: 'updateCluster', - reqOpts, - gaxOpts: gaxOptions, - }, - callback); + { + client: 'BigtableInstanceAdminClient', + method: 'updateCluster', + reqOpts, + gaxOpts: gaxOptions, + }, + callback + ); } } diff --git a/src/family.ts b/src/family.ts index e5282d06a..6bb92b8bb 100644 --- a/src/family.ts +++ b/src/family.ts @@ -124,7 +124,8 @@ Please use the format 'follows' or '${table.name}/columnFamilies/my-family'.`); if (rules.length === 1) { if (ruleObj.union) { throw new Error( - 'A union must have more than one garbage collection rule.'); + 'A union must have more than one garbage collection rule.' + ); } return rules[0]; } @@ -185,21 +186,22 @@ Please use the format 'follows' or '${table.name}/columnFamilies/my-family'.`); } this.bigtable.request( - { - client: 'BigtableTableAdminClient', - method: 'modifyColumnFamilies', - reqOpts: { - name: this.table.name, - modifications: [ - { - id: this.id, - drop: true, - }, - ], - }, - gaxOpts: gaxOptions, + { + client: 'BigtableTableAdminClient', + method: 'modifyColumnFamilies', + reqOpts: { + name: this.table.name, + modifications: [ + { + id: this.id, + drop: true, + }, + ], }, - callback); + gaxOpts: gaxOptions, + }, + callback + ); } /** @@ -359,20 +361,21 @@ Please use the format 'follows' or '${table.name}/columnFamilies/my-family'.`); }; this.bigtable.request( - { - client: 'BigtableTableAdminClient', - method: 'modifyColumnFamilies', - reqOpts, - gaxOpts: gaxOptions, - }, - (...args) => { - if (args[1]) { - this.metadata = args[1].columnFamilies[this.id]; - args.splice(1, 0, this.metadata); - } - - callback(...args); - }); + { + client: 'BigtableTableAdminClient', + method: 'modifyColumnFamilies', + reqOpts, + gaxOpts: gaxOptions, + }, + (...args) => { + if (args[1]) { + this.metadata = args[1].columnFamilies[this.id]; + args.splice(1, 0, this.metadata); + } + + callback(...args); + } + ); } } diff --git a/src/filter.ts b/src/filter.ts index 18c94de65..af576a083 100644 --- a/src/filter.ts +++ b/src/filter.ts @@ -120,7 +120,7 @@ export class Filter { return Buffer.from(escapedString, encodingToUse); } - throw new TypeError('Can\'t convert to RegExp String from unknown type.'); + throw new TypeError("Can't convert to RegExp String from unknown type."); } /** @@ -215,14 +215,14 @@ export class Filter { arrify(filters).forEach(filterObj => { const key = Object.keys(filterObj)[0]; - if (!is.function(filter[key])) - { throw new FilterError(key); } + if (!is.function(filter[key])) { + throw new FilterError(key); + } - filter[key](filterObj[key]); - }) - ; + filter[key](filterObj[key]); + }); - return filter.toProto(); + return filter.toProto(); } /** @@ -250,9 +250,9 @@ export class Filter { * }; */ all(pass) { - const filterName = pass ? 'passAllFilter' : 'blockAllFilter'; + const filterName = pass ? 'passAllFilter' : 'blockAllFilter'; - this.set(filterName, true); + this.set(filterName, true); } /** @@ -374,30 +374,33 @@ export class Filter { * ]; */ column(column) { - if (!is.object(column)) { - column = { - name: column, - }; - } + if (!is.object(column)) { + column = { + name: column, + }; + } - if (column.name) { - let name = Filter.convertToRegExpString(column.name); + if (column.name) { + let name = Filter.convertToRegExpString(column.name); - name = Mutation.convertToBytes(name); - this.set('columnQualifierRegexFilter', name); - } + name = Mutation.convertToBytes(name); + this.set('columnQualifierRegexFilter', name); + } - if (is.number(column.cellLimit)) { - this.set('cellsPerColumnLimitFilter', column.cellLimit); - } + if (is.number(column.cellLimit)) { + this.set('cellsPerColumnLimitFilter', column.cellLimit); + } - if (column.start || column.end) { - const range: any = - Filter.createRange(column.start, column.end, 'Qualifier'); + if (column.start || column.end) { + const range: any = Filter.createRange( + column.start, + column.end, + 'Qualifier' + ); - range.familyName = column.family; - this.set('columnRangeFilter', range); - } + range.familyName = column.family; + this.set('columnRangeFilter', range); + } } /** @@ -453,11 +456,11 @@ export class Filter { * ]; */ condition(condition) { - this.set('condition', { - predicateFilter: Filter.parse(condition.test), - trueFilter: Filter.parse(condition.pass), - falseFilter: Filter.parse(condition.fail), - }); + this.set('condition', { + predicateFilter: Filter.parse(condition.test), + trueFilter: Filter.parse(condition.pass), + falseFilter: Filter.parse(condition.fail), + }); } /** @@ -478,8 +481,8 @@ export class Filter { * ]; */ family(family) { - family = Filter.convertToRegExpString(family); - this.set('familyNameRegexFilter', family); + family = Filter.convertToRegExpString(family); + this.set('familyNameRegexFilter', family); } /** @@ -521,9 +524,9 @@ export class Filter { * ]; */ interleave(filters) { - this.set('interleave', { - filters: filters.map(Filter.parse), - }); + this.set('interleave', { + filters: filters.map(Filter.parse), + }); } /** @@ -548,7 +551,7 @@ export class Filter { * }; */ label(label) { - this.set('applyLabelTransformer', label); + this.set('applyLabelTransformer', label); } /** @@ -657,30 +660,30 @@ export class Filter { * ]; */ row(row) { - if (!is.object(row)) { - row = { - key: row, - }; - } + if (!is.object(row)) { + row = { + key: row, + }; + } - if (row.key) { - let key = Filter.convertToRegExpString(row.key); + if (row.key) { + let key = Filter.convertToRegExpString(row.key); - key = Mutation.convertToBytes(key); - this.set('rowKeyRegexFilter', key); - } + key = Mutation.convertToBytes(key); + this.set('rowKeyRegexFilter', key); + } - if (row.sample) { - this.set('rowSampleFilter', row.sample); - } + if (row.sample) { + this.set('rowSampleFilter', row.sample); + } - if (is.number(row.cellOffset)) { - this.set('cellsPerRowOffsetFilter', row.cellOffset); - } + if (is.number(row.cellOffset)) { + this.set('cellsPerRowOffsetFilter', row.cellOffset); + } - if (is.number(row.cellLimit)) { - this.set('cellsPerRowLimitFilter', row.cellLimit); - } + if (is.number(row.cellLimit)) { + this.set('cellsPerRowLimitFilter', row.cellLimit); + } } /** @@ -690,10 +693,10 @@ export class Filter { * @param {*} value Filter value. */ set(key, value) { - const filter = {}; + const filter = {}; - filter[key] = value; - this.filters_.push(filter); + filter[key] = value; + this.filters_.push(filter); } /** @@ -751,7 +754,7 @@ export class Filter { * //- */ sink(sink) { - this.set('sink', sink); + this.set('sink', sink); } /** @@ -770,8 +773,8 @@ export class Filter { * ]; */ time(time) { - const range = Mutation.createTimeRange(time.start, time.end); - this.set('timestampRangeFilter', range); + const range = Mutation.createTimeRange(time.start, time.end); + this.set('timestampRangeFilter', range); } /** @@ -779,19 +782,19 @@ export class Filter { * execution of the filters will be the order in which they were specified. */ toProto() { - if (!this.filters_.length) { - return null; - } + if (!this.filters_.length) { + return null; + } - if (this.filters_.length === 1) { - return this.filters_[0]; - } + if (this.filters_.length === 1) { + return this.filters_[0]; + } - return { - chain: { - filters: this.filters_, - }, - }; + return { + chain: { + filters: this.filters_, + }, + }; } /** @@ -892,27 +895,27 @@ export class Filter { * ]; */ value(value) { - if (!is.object(value)) { - value = { - value, - }; - } + if (!is.object(value)) { + value = { + value, + }; + } - if (value.value) { - let valueReg = Filter.convertToRegExpString(value.value); + if (value.value) { + let valueReg = Filter.convertToRegExpString(value.value); - valueReg = Mutation.convertToBytes(valueReg); - this.set('valueRegexFilter', valueReg); - } + valueReg = Mutation.convertToBytes(valueReg); + this.set('valueRegexFilter', valueReg); + } - if (value.start || value.end) { - const range = Filter.createRange(value.start, value.end, 'Value'); + if (value.start || value.end) { + const range = Filter.createRange(value.start, value.end, 'Value'); - this.set('valueRangeFilter', range); - } + this.set('valueRangeFilter', range); + } - if (value.strip) { - this.set('stripValueTransformer', value.strip); - } - } + if (value.strip) { + this.set('stripValueTransformer', value.strip); + } } +} diff --git a/src/index.ts b/src/index.ts index de282f799..da8375d49 100644 --- a/src/index.ts +++ b/src/index.ts @@ -392,20 +392,21 @@ export class Bigtable { } options = Object.assign( - { - libName: 'gccl', - libVersion: PKG.version, - scopes, - 'grpc.max_send_message_length': -1, - 'grpc.max_receive_message_length': -1, - }, - options); + { + libName: 'gccl', + libVersion: PKG.version, + scopes, + 'grpc.max_send_message_length': -1, + 'grpc.max_receive_message_length': -1, + }, + options + ); const defaultBaseUrl = 'bigtable.googleapis.com'; const defaultAdminBaseUrl = 'bigtableadmin.googleapis.com'; const customEndpoint = - options.apiEndpoint || process.env.BIGTABLE_EMULATOR_HOST; + options.apiEndpoint || process.env.BIGTABLE_EMULATOR_HOST; this.customEndpoint = customEndpoint; let customEndpointBaseUrl; @@ -419,32 +420,39 @@ export class Bigtable { this.options = { BigtableClient: Object.assign( - { - servicePath: customEndpoint ? customEndpointBaseUrl : - defaultBaseUrl, - port: customEndpoint ? parseInt(customEndpointPort, 10) : 443, - sslCreds: customEndpoint ? grpc.credentials.createInsecure() : - undefined, - }, - options), + { + servicePath: customEndpoint ? customEndpointBaseUrl : defaultBaseUrl, + port: customEndpoint ? parseInt(customEndpointPort, 10) : 443, + sslCreds: customEndpoint + ? grpc.credentials.createInsecure() + : undefined, + }, + options + ), BigtableInstanceAdminClient: Object.assign( - { - servicePath: customEndpoint ? customEndpointBaseUrl : - defaultAdminBaseUrl, - port: customEndpoint ? parseInt(customEndpointPort, 10) : 443, - sslCreds: customEndpoint ? grpc.credentials.createInsecure() : - undefined, - }, - options), + { + servicePath: customEndpoint + ? customEndpointBaseUrl + : defaultAdminBaseUrl, + port: customEndpoint ? parseInt(customEndpointPort, 10) : 443, + sslCreds: customEndpoint + ? grpc.credentials.createInsecure() + : undefined, + }, + options + ), BigtableTableAdminClient: Object.assign( - { - servicePath: customEndpoint ? customEndpointBaseUrl : - defaultAdminBaseUrl, - port: customEndpoint ? parseInt(customEndpointPort, 10) : 443, - sslCreds: customEndpoint ? grpc.credentials.createInsecure() : - undefined, - }, - options), + { + servicePath: customEndpoint + ? customEndpointBaseUrl + : defaultAdminBaseUrl, + port: customEndpoint ? parseInt(customEndpointPort, 10) : 443, + sslCreds: customEndpoint + ? grpc.credentials.createInsecure() + : undefined, + }, + options + ), }; this.api = {}; @@ -530,52 +538,51 @@ export class Bigtable { * }); */ createInstance(id, options, callback) { - if (is.function(options)) - { - callback = options; - options = {}; - } - - const reqOpts: any = { - parent: this.projectName, - instanceId: id, - instance: { - displayName: options.displayName || id, - labels: options.labels, - }, - }; + if (is.function(options)) { + callback = options; + options = {}; + } - if (options.type) { - reqOpts.instance.type = Instance.getTypeType_(options.type); - } + const reqOpts: any = { + parent: this.projectName, + instanceId: id, + instance: { + displayName: options.displayName || id, + labels: options.labels, + }, + }; - reqOpts.clusters = - arrify(options.clusters).reduce((clusters, cluster) => { - clusters[cluster.id] = { - location: Cluster.getLocation_(this.projectId, cluster.location), - serveNodes: cluster.nodes, - defaultStorageType: Cluster.getStorageType_(cluster.storage), - }; + if (options.type) { + reqOpts.instance.type = Instance.getTypeType_(options.type); + } - return clusters; - }, {}); + reqOpts.clusters = arrify(options.clusters).reduce((clusters, cluster) => { + clusters[cluster.id] = { + location: Cluster.getLocation_(this.projectId, cluster.location), + serveNodes: cluster.nodes, + defaultStorageType: Cluster.getStorageType_(cluster.storage), + }; - this.request( - { - client: 'BigtableInstanceAdminClient', - method: 'createInstance', - reqOpts, - gaxOpts: options.gaxOptions, - }, - (...args) => { - const err = args[0]; + return clusters; + }, {}); - if (!err) { - args.splice(1, 0, this.instance(id)); - } + this.request( + { + client: 'BigtableInstanceAdminClient', + method: 'createInstance', + reqOpts, + gaxOpts: options.gaxOptions, + }, + (...args) => { + const err = args[0]; + + if (!err) { + args.splice(1, 0, this.instance(id)); + } - callback(...args); - }); + callback(...args); + } + ); } /** @@ -607,38 +614,37 @@ export class Bigtable { * }); */ getInstances(gaxOptions?, callback?) { - if (is.function(gaxOptions)) - { - callback = gaxOptions; - gaxOptions = {}; + if (is.function(gaxOptions)) { + callback = gaxOptions; + gaxOptions = {}; + } + + const reqOpts = { + parent: this.projectName, + }; + + this.request( + { + client: 'BigtableInstanceAdminClient', + method: 'listInstances', + reqOpts, + gaxOpts: gaxOptions, + }, + (err, resp) => { + if (err) { + callback(err); + return; } - const reqOpts = { - parent: this.projectName, - }; + const instances = resp.instances.map(instanceData => { + const instance = this.instance(instanceData.name.split('/').pop()); + instance.metadata = instanceData; + return instance; + }); - this.request( - { - client: 'BigtableInstanceAdminClient', - method: 'listInstances', - reqOpts, - gaxOpts: gaxOptions, - }, - (err, resp) => { - if (err) { - callback(err); - return; - } - - const instances = resp.instances.map(instanceData => { - const instance = - this.instance(instanceData.name.split('/').pop()); - instance.metadata = instanceData; - return instance; - }); - - callback(null, instances, resp); - }); + callback(null, instances, resp); + } + ); } /** @@ -648,7 +654,7 @@ export class Bigtable { * @returns {Instance} */ instance(name) { - return new Instance(this, name); + return new Instance(this, name); } /** @@ -661,96 +667,99 @@ export class Bigtable { * @param {function} [callback] Callback function. */ request(config, callback) { - const isStreamMode = !callback; + const isStreamMode = !callback; - let gaxStream; - let stream; + let gaxStream; + let stream; - const prepareGaxRequest = callback => { - this.getProjectId_((err, projectId) => { - if (err) { - callback(err); - return; - } + const prepareGaxRequest = callback => { + this.getProjectId_((err, projectId) => { + if (err) { + callback(err); + return; + } - let gaxClient = this.api[config.client]; + let gaxClient = this.api[config.client]; - if (!gaxClient) { - // Lazily instantiate client. - gaxClient = new v2[config.client](this.options[config.client]); - this.api[config.client] = gaxClient; - } + if (!gaxClient) { + // Lazily instantiate client. + gaxClient = new v2[config.client](this.options[config.client]); + this.api[config.client] = gaxClient; + } - let reqOpts = extend(true, {}, config.reqOpts); + let reqOpts = extend(true, {}, config.reqOpts); - if (this.shouldReplaceProjectIdToken && - projectId !== '{{projectId}}') { - reqOpts = replaceProjectIdToken(reqOpts, projectId); - } + if (this.shouldReplaceProjectIdToken && projectId !== '{{projectId}}') { + reqOpts = replaceProjectIdToken(reqOpts, projectId); + } - const requestFn = gaxClient[config.method].bind( - gaxClient, reqOpts, config.gaxOpts); + const requestFn = gaxClient[config.method].bind( + gaxClient, + reqOpts, + config.gaxOpts + ); - callback(null, requestFn); - }); - }; + callback(null, requestFn); + }); + }; - if (isStreamMode) { - stream = streamEvents(through.obj()); + if (isStreamMode) { + stream = streamEvents(through.obj()); - stream.abort = () => { - if (gaxStream && gaxStream.cancel) { - gaxStream.cancel(); - } - }; + stream.abort = () => { + if (gaxStream && gaxStream.cancel) { + gaxStream.cancel(); + } + }; - stream.once('reading', makeRequestStream); + stream.once('reading', makeRequestStream); - return stream; - } else { - makeRequestCallback(); + return stream; + } else { + makeRequestCallback(); + } + + function makeRequestCallback() { + prepareGaxRequest((err, requestFn) => { + if (err) { + callback(err); + return; } - function makeRequestCallback() { - prepareGaxRequest((err, requestFn) => { - if (err) { - callback(err); - return; - } + requestFn(callback); + }); + } - requestFn(callback); - }); + function makeRequestStream() { + prepareGaxRequest((err, requestFn) => { + if (err) { + stream.destroy(err); + return; } - function makeRequestStream() { - prepareGaxRequest((err, requestFn) => { - if (err) { - stream.destroy(err); - return; - } - - // @TODO: remove `retry-request` when gax supports retryable - // streams. - // https://github.com/googleapis/gax-nodejs/blob/ec0c8b0805c31d8a91ea69cb19fe50f42a38bf87/lib/streaming.js#L230 - const retryOpts = Object.assign( - { - currentRetryAttempt: 0, - noResponseRetries: 0, - objectMode: true, - shouldRetryFn: (Service as any).shouldRetryRequest_, - request() { - gaxStream = requestFn(); - return gaxStream; - }, - }, - config.retryOpts); - - retryRequest(null, retryOpts) - .on('error', stream.destroy.bind(stream)) - .on('request', stream.emit.bind(stream, 'request')) - .pipe(stream); - }); - } + // @TODO: remove `retry-request` when gax supports retryable + // streams. + // https://github.com/googleapis/gax-nodejs/blob/ec0c8b0805c31d8a91ea69cb19fe50f42a38bf87/lib/streaming.js#L230 + const retryOpts = Object.assign( + { + currentRetryAttempt: 0, + noResponseRetries: 0, + objectMode: true, + shouldRetryFn: (Service as any).shouldRetryRequest_, + request() { + gaxStream = requestFn(); + return gaxStream; + }, + }, + config.retryOpts + ); + + retryRequest(null, retryOpts) + .on('error', stream.destroy.bind(stream)) + .on('request', stream.emit.bind(stream, 'request')) + .pipe(stream); + }); + } } /** @@ -764,24 +773,24 @@ export class Bigtable { * @param {string} callback.projectId The detected project ID. */ getProjectId_(callback) { - const projectIdRequired = - this.projectId === '{{projectId}}' && !this.customEndpoint; + const projectIdRequired = + this.projectId === '{{projectId}}' && !this.customEndpoint; - if (!projectIdRequired) { - setImmediate(callback, null, this.projectId); - return; - } + if (!projectIdRequired) { + setImmediate(callback, null, this.projectId); + return; + } - this.auth.getProjectId((err, projectId) => { - if (err) { - callback(err); - return; - } + this.auth.getProjectId((err, projectId) => { + if (err) { + callback(err); + return; + } - this.projectId = projectId; + this.projectId = projectId; - callback(null, this.projectId); - }); + callback(null, this.projectId); + }); } } @@ -822,7 +831,7 @@ promisifyAll(Bigtable, { // eslint-disable-next-line no-class-assign (Bigtable as any) = new Proxy(Bigtable, { apply(target, thisArg, argumentsList) { - return new (target as any)(...argumentsList); + return new (target as any)(...argumentsList); }, }); diff --git a/src/instance.ts b/src/instance.ts index 776d2c8d7..99fc542c0 100644 --- a/src/instance.ts +++ b/src/instance.ts @@ -53,7 +53,8 @@ export class Instance { } else { throw new Error(`Instance id '${id}' is not formatted correctly. Please use the format 'my-instance' or '${ - bigtable.projectName}/instances/my-instance'.`); + bigtable.projectName + }/instances/my-instance'.`); } } else { name = `${bigtable.projectName}/instances/${id}`; @@ -155,41 +156,41 @@ Please use the format 'my-instance' or '${ * region_tag:bigtable_create_app_profile */ createAppProfile(id, options, callback) { - if (is.function(options)) - { - callback = options; - options = {}; - } - if (!options.routing) { - throw new Error('An app profile must contain a routing policy.'); - } + if (is.function(options)) { + callback = options; + options = {}; + } + if (!options.routing) { + throw new Error('An app profile must contain a routing policy.'); + } - const appProfile = AppProfile.formatAppProfile_(options); + const appProfile = AppProfile.formatAppProfile_(options); - const reqOpts: any = { - parent: this.name, - appProfileId: id, - appProfile, - }; + const reqOpts: any = { + parent: this.name, + appProfileId: id, + appProfile, + }; - if (is.boolean(options.ignoreWarnings)) { - reqOpts.ignoreWarnings = options.ignoreWarnings; - } + if (is.boolean(options.ignoreWarnings)) { + reqOpts.ignoreWarnings = options.ignoreWarnings; + } - this.bigtable.request( - { - client: 'BigtableInstanceAdminClient', - method: 'createAppProfile', - reqOpts, - gaxOpts: options.gaxOptions, - }, - (...args) => { - if (args[1]) { - args.splice(1, 0, this.appProfile(id)); - } - - callback(...args); - }); + this.bigtable.request( + { + client: 'BigtableInstanceAdminClient', + method: 'createAppProfile', + reqOpts, + gaxOpts: options.gaxOptions, + }, + (...args) => { + if (args[1]) { + args.splice(1, 0, this.appProfile(id)); + } + + callback(...args); + } + ); } /** @@ -220,49 +221,51 @@ Please use the format 'my-instance' or '${ * region_tag:bigtable_create_cluster */ createCluster(id, options, callback) { - if (is.function(options)) - { - callback = options; - options = {}; - } + if (is.function(options)) { + callback = options; + options = {}; + } - const reqOpts: any = { - parent: this.name, - clusterId: id, - }; + const reqOpts: any = { + parent: this.name, + clusterId: id, + }; - if (!is.empty(options)) { - reqOpts.cluster = {}; - } + if (!is.empty(options)) { + reqOpts.cluster = {}; + } - if (options.location) { - reqOpts.cluster.location = - Cluster.getLocation_(this.bigtable.projectId, options.location); - } + if (options.location) { + reqOpts.cluster.location = Cluster.getLocation_( + this.bigtable.projectId, + options.location + ); + } - if (options.nodes) { - reqOpts.cluster.serveNodes = options.nodes; - } + if (options.nodes) { + reqOpts.cluster.serveNodes = options.nodes; + } + + if (options.storage) { + const storageType = Cluster.getStorageType_(options.storage); + reqOpts.cluster.defaultStorageType = storageType; + } - if (options.storage) { - const storageType = Cluster.getStorageType_(options.storage); - reqOpts.cluster.defaultStorageType = storageType; + this.bigtable.request( + { + client: 'BigtableInstanceAdminClient', + method: 'createCluster', + reqOpts, + gaxOpts: options.gaxOptions, + }, + (...args) => { + if (args[1]) { + args.splice(1, 0, this.cluster(id)); } - this.bigtable.request( - { - client: 'BigtableInstanceAdminClient', - method: 'createCluster', - reqOpts, - gaxOpts: options.gaxOptions, - }, - (...args) => { - if (args[1]) { - args.splice(1, 0, this.cluster(id)); - } - - callback(...args); - }); + callback(...args); + } + ); } /** @@ -290,72 +293,71 @@ Please use the format 'my-instance' or '${ * region_tag:bigtable_create_table */ createTable(id, options, callback) { - if (!id) { - throw new Error('An id is required to create a table.'); - } + if (!id) { + throw new Error('An id is required to create a table.'); + } + + options = options || {}; + + if (is.function(options)) { + callback = options; + options = {}; + } + + const reqOpts: any = { + parent: this.name, + tableId: id, + table: { + // The granularity at which timestamps are stored in the table. + // Currently only milliseconds is supported, so it's not + // configurable. + granularity: 0, + }, + }; - options = options || {}; - - if (is.function(options)) - { - callback = options; - options = {}; - } - - const reqOpts: any = { - parent: this.name, - tableId: id, - table: { - // The granularity at which timestamps are stored in the table. - // Currently only milliseconds is supported, so it's not - // configurable. - granularity: 0, - }, + if (options.splits) { + reqOpts.initialSplits = options.splits.map(key => ({ + key, + })); + } + + if (options.families) { + const columnFamilies = options.families.reduce((families, family) => { + if (is.string(family)) { + family = { + name: family, }; + } - if (options.splits) { - reqOpts.initialSplits = options.splits.map(key => ({ - key, - })); - } - - if (options.families) { - const columnFamilies = - options.families.reduce((families, family) => { - if (is.string(family)) { - family = { - name: family, - }; - } - - const columnFamily: any = (families[family.name] = {}); - - if (family.rule) { - columnFamily.gcRule = Family.formatRule_(family.rule); - } - - return families; - }, {}); - - reqOpts.table.columnFamilies = columnFamilies; - } - - this.bigtable.request( - { - client: 'BigtableTableAdminClient', - method: 'createTable', - reqOpts, - gaxOpts: options.gaxOptions, - }, - (...args) => { - if (args[1]) { - const table = this.table(args[1].name.split('/').pop()); - table.metadata = args[1]; - args.splice(1, 0, table); - } - - callback(...args); - }); + const columnFamily: any = (families[family.name] = {}); + + if (family.rule) { + columnFamily.gcRule = Family.formatRule_(family.rule); + } + + return families; + }, {}); + + reqOpts.table.columnFamilies = columnFamilies; + } + + this.bigtable.request( + { + client: 'BigtableTableAdminClient', + method: 'createTable', + reqOpts, + gaxOpts: options.gaxOptions, + }, + (...args) => { + if (args[1]) { + const table = this.table(args[1].name.split('/').pop()); + table.metadata = args[1]; + args.splice(1, 0, table); + } + + callback(...args); + } + ); } /** @@ -365,7 +367,7 @@ Please use the format 'my-instance' or '${ * @returns {Cluster} */ cluster(id) { - return new Cluster(this, id); + return new Cluster(this, id); } /** @@ -382,21 +384,22 @@ Please use the format 'my-instance' or '${ * region_tag:bigtable_del_instance */ delete(gaxOptions, callback) { - if (is.fn(gaxOptions)) { - callback = gaxOptions; - gaxOptions = {}; - } - - this.bigtable.request( - { - client: 'BigtableInstanceAdminClient', - method: 'deleteInstance', - reqOpts: { - name: this.name, - }, - gaxOpts: gaxOptions, - }, - callback); + if (is.fn(gaxOptions)) { + callback = gaxOptions; + gaxOptions = {}; + } + + this.bigtable.request( + { + client: 'BigtableInstanceAdminClient', + method: 'deleteInstance', + reqOpts: { + name: this.name, + }, + gaxOpts: gaxOptions, + }, + callback + ); } /** @@ -413,24 +416,24 @@ Please use the format 'my-instance' or '${ * region_tag:bigtable_exists_instance */ exists(gaxOptions?, callback?) { - if (is.fn(gaxOptions)) { - callback = gaxOptions; - gaxOptions = {}; - } - - this.getMetadata(gaxOptions, err => { - if (err) { - if (err.code === 5) { - callback(null, false); - return; - } - - callback(err); - return; - } - - callback(null, true); - }); + if (is.fn(gaxOptions)) { + callback = gaxOptions; + gaxOptions = {}; + } + + this.getMetadata(gaxOptions, err => { + if (err) { + if (err.code === 5) { + callback(null, false); + return; + } + + callback(err); + return; + } + + callback(null, true); + }); } /** @@ -447,14 +450,14 @@ Please use the format 'my-instance' or '${ * region_tag:bigtable_get_instance */ get(gaxOptions, callback) { - if (is.fn(gaxOptions)) { - callback = gaxOptions; - gaxOptions = {}; - } + if (is.fn(gaxOptions)) { + callback = gaxOptions; + gaxOptions = {}; + } - this.getMetadata(gaxOptions, (err, metadata) => { - callback(err, err ? null : this, metadata); - }); + this.getMetadata(gaxOptions, (err, metadata) => { + callback(err, err ? null : this, metadata); + }); } /** @@ -471,38 +474,39 @@ Please use the format 'my-instance' or '${ * region_tag:bigtable_get_app_profiles */ getAppProfiles(gaxOptions, callback) { - if (is.function(gaxOptions)) - { - callback = gaxOptions; - gaxOptions = {}; - } - - const reqOpts = { - parent: this.name, - }; - - this.bigtable.request( - { - client: 'BigtableInstanceAdminClient', - method: 'listAppProfiles', - reqOpts, - gaxOpts: gaxOptions, - }, - (err, resp) => { - if (err) { - callback(err); - return; - } - - const appProfiles = resp.map(appProfileObj => { - const appProfile = - this.appProfile(appProfileObj.name.split('/').pop()); - appProfile.metadata = appProfileObj; - return appProfile; - }); - - callback(null, appProfiles, resp); - }); + if (is.function(gaxOptions)) { + callback = gaxOptions; + gaxOptions = {}; + } + + const reqOpts = { + parent: this.name, + }; + + this.bigtable.request( + { + client: 'BigtableInstanceAdminClient', + method: 'listAppProfiles', + reqOpts, + gaxOpts: gaxOptions, + }, + (err, resp) => { + if (err) { + callback(err); + return; + } + + const appProfiles = resp.map(appProfileObj => { + const appProfile = this.appProfile( + appProfileObj.name.split('/').pop() + ); + appProfile.metadata = appProfileObj; + return appProfile; + }); + + callback(null, appProfiles, resp); + } + ); } /** @@ -520,38 +524,37 @@ Please use the format 'my-instance' or '${ * region_tag:bigtable_get_clusters */ getClusters(gaxOptions, callback) { - if (is.function(gaxOptions)) - { - callback = gaxOptions; - gaxOptions = {}; - } - - const reqOpts = { - parent: this.name, - }; - - this.bigtable.request( - { - client: 'BigtableInstanceAdminClient', - method: 'listClusters', - reqOpts, - gaxOpts: gaxOptions, - }, - (err, resp) => { - if (err) { - callback(err); - return; - } - - const clusters = resp.clusters.map(clusterObj => { - const cluster = - this.cluster(clusterObj.name.split('/').pop()); - cluster.metadata = clusterObj; - return cluster; - }); - - callback(null, clusters, resp); - }); + if (is.function(gaxOptions)) { + callback = gaxOptions; + gaxOptions = {}; + } + + const reqOpts = { + parent: this.name, + }; + + this.bigtable.request( + { + client: 'BigtableInstanceAdminClient', + method: 'listClusters', + reqOpts, + gaxOpts: gaxOptions, + }, + (err, resp) => { + if (err) { + callback(err); + return; + } + + const clusters = resp.clusters.map(clusterObj => { + const cluster = this.cluster(clusterObj.name.split('/').pop()); + cluster.metadata = clusterObj; + return cluster; + }); + + callback(null, clusters, resp); + } + ); } /** @@ -568,27 +571,28 @@ Please use the format 'my-instance' or '${ * region_tag:bigtable_get_instance_metadata */ getMetadata(gaxOptions, callback) { - if (is.fn(gaxOptions)) { - callback = gaxOptions; - gaxOptions = {}; - } - - this.bigtable.request( - { - client: 'BigtableInstanceAdminClient', - method: 'getInstance', - reqOpts: { - name: this.name, - }, - gaxOpts: gaxOptions, - }, - (...args) => { - if (args[1]) { - this.metadata = args[1]; - } - - callback(...args); - }); + if (is.fn(gaxOptions)) { + callback = gaxOptions; + gaxOptions = {}; + } + + this.bigtable.request( + { + client: 'BigtableInstanceAdminClient', + method: 'getInstance', + reqOpts: { + name: this.name, + }, + gaxOpts: gaxOptions, + }, + (...args) => { + if (args[1]) { + this.metadata = args[1]; + } + + callback(...args); + } + ); } /** @@ -615,38 +619,37 @@ Please use the format 'my-instance' or '${ * region_tag:bigtable_get_tables */ getTables(options, callback) { - if (is.function(options)) - { - callback = options; - options = {}; - } - - const reqOpts = Object.assign({}, options, { - parent: this.name, - view: Table.VIEWS[options.view || 'unspecified'], - }); - - delete reqOpts.gaxOptions; - - this.bigtable.request( - { - client: 'BigtableTableAdminClient', - method: 'listTables', - reqOpts, - gaxOpts: options.gaxOptions, - }, - (...args) => { - if (args[1]) { - args[1] = args[1].map(tableObj => { - const table = - this.table(tableObj.name.split('/').pop()); - table.metadata = tableObj; - return table; - }); - } - - callback(...args); - }); + if (is.function(options)) { + callback = options; + options = {}; + } + + const reqOpts = Object.assign({}, options, { + parent: this.name, + view: Table.VIEWS[options.view || 'unspecified'], + }); + + delete reqOpts.gaxOptions; + + this.bigtable.request( + { + client: 'BigtableTableAdminClient', + method: 'listTables', + reqOpts, + gaxOpts: options.gaxOptions, + }, + (...args) => { + if (args[1]) { + args[1] = args[1].map(tableObj => { + const table = this.table(tableObj.name.split('/').pop()); + table.metadata = tableObj; + return table; + }); + } + + callback(...args); + } + ); } /** @@ -667,38 +670,39 @@ Please use the format 'my-instance' or '${ * region_tag:bigtable_set_meta_data */ setMetadata(metadata, gaxOptions, callback) { - if (is.fn(gaxOptions)) { - callback = gaxOptions; - gaxOptions = {}; - } - const reqOpts: any = { - instance: Object.assign({name: this.name}, metadata), - updateMask: { - paths: [], - }, - }; - const fieldsForMask = ['displayName', 'type', 'labels']; - - fieldsForMask.forEach(field => { - if (field in reqOpts.instance) { - reqOpts.updateMask.paths.push(snakeCase(field)); - } - }); - - this.bigtable.request( - { - client: 'BigtableInstanceAdminClient', - method: 'partialUpdateInstance', - reqOpts, - gaxOpts: gaxOptions, - }, - (...args) => { - if (args[1]) { - this.metadata = args[1]; - } - - callback(...args); - }); + if (is.fn(gaxOptions)) { + callback = gaxOptions; + gaxOptions = {}; + } + const reqOpts: any = { + instance: Object.assign({name: this.name}, metadata), + updateMask: { + paths: [], + }, + }; + const fieldsForMask = ['displayName', 'type', 'labels']; + + fieldsForMask.forEach(field => { + if (field in reqOpts.instance) { + reqOpts.updateMask.paths.push(snakeCase(field)); + } + }); + + this.bigtable.request( + { + client: 'BigtableInstanceAdminClient', + method: 'partialUpdateInstance', + reqOpts, + gaxOpts: gaxOptions, + }, + (...args) => { + if (args[1]) { + this.metadata = args[1]; + } + + callback(...args); + } + ); } /** @@ -714,7 +718,7 @@ Please use the format 'my-instance' or '${ * const table = instance.table('presidents'); */ table(id) { - return new Table(this, id); + return new Table(this, id); } } @@ -766,8 +770,8 @@ promisifyAll(Instance, { exclude: ['appProfile', 'cluster', 'table'], }); - /** - * Reference to the {@link Instance} class. - * @name module:@google-cloud/bigtable.Instance - * @see Instance - */ +/** + * Reference to the {@link Instance} class. + * @name module:@google-cloud/bigtable.Instance + * @see Instance + */ diff --git a/src/mutation.ts b/src/mutation.ts index cab4e2bad..44a377ba8 100644 --- a/src/mutation.ts +++ b/src/mutation.ts @@ -24,19 +24,19 @@ export type IMutation = btTypes.bigtable.v2.IMutation; export type IMutateRowRequest = btTypes.bigtable.v2.IMutateRowRequest; export type ISetCell = btTypes.bigtable.v2.Mutation.ISetCell; -export type Bytes = string|Buffer; -export type Data = Value|Value[]|MutationSettingsObj; -export type JsonObj = { - [k: string]: string|JsonObj -}; -export type Value = string|number|boolean; +export type Bytes = string | Buffer; +export type Data = Value | Value[] | MutationSettingsObj; +export interface JsonObj { + [k: string]: string | JsonObj; +} +export type Value = string | number | boolean; export interface ParsedColumn { - family: string|null; - qualifier: string|null; + family: string | null; + qualifier: string | null; } export interface ConvertFromBytesOptions { - userOptions?: {decode?: boolean; encoding?: string;}; + userOptions?: {decode?: boolean; encoding?: string}; isPossibleNumber?: boolean; } export interface MutationConstructorObj { @@ -47,19 +47,19 @@ export interface MutationConstructorObj { export interface MutationSettingsObj { follows?: ValueObj; column?: string; - time?: {start: Date|number; end: Date | number;}; + time?: {start: Date | number; end: Date | number}; } export interface TimeRange { - [k: string]: number|string|undefined; + [k: string]: number | string | undefined; startTimestampMicros?: number; endTimestampMicros?: number; } export interface SetCellObj { - [k: string]: string|ISetCell|undefined; + [k: string]: string | ISetCell | undefined; setCell?: ISetCell; } export interface ValueObj { - [k: string]: Buffer|Value|ValueObj; + [k: string]: Buffer | Value | ValueObj; } /** @@ -102,8 +102,10 @@ export class Mutation { * @returns {string|number|buffer} * @private */ - static convertFromBytes(bytes: Bytes, options?: ConvertFromBytesOptions): - Buffer|Value { + static convertFromBytes( + bytes: Bytes, + options?: ConvertFromBytesOptions + ): Buffer | Value { const buf = bytes instanceof Buffer ? bytes : Buffer.from(bytes, 'base64'); if (options && options.isPossibleNumber && buf.length === 8) { // tslint:disable-next-line no-any @@ -130,7 +132,7 @@ export class Mutation { * @returns {buffer} * @private */ - static convertToBytes(data: Buffer|Data): Buffer|Data { + static convertToBytes(data: Buffer | Data): Buffer | Data { if (data instanceof Buffer) { return data; } @@ -287,7 +289,7 @@ export class Mutation { * ]); * @private */ - static encodeDelete(data?: Data|Data[]): IMutation[] { + static encodeDelete(data?: Data | Data[]): IMutation[] { if (!data) { return [ { @@ -303,8 +305,9 @@ export class Mutation { } as MutationSettingsObj; } - const column = - Mutation.parseColumnName((mutation as MutationSettingsObj).column!); + const column = Mutation.parseColumnName( + (mutation as MutationSettingsObj).column! + ); if (!column.qualifier) { return { @@ -314,19 +317,21 @@ export class Mutation { }; } - let timeRange: TimeRange|undefined; + let timeRange: TimeRange | undefined; if ((mutation as MutationSettingsObj).time) { timeRange = Mutation.createTimeRange( - (mutation as MutationSettingsObj).time!.start as Date, - (mutation as MutationSettingsObj).time!.end as Date); + (mutation as MutationSettingsObj).time!.start as Date, + (mutation as MutationSettingsObj).time!.end as Date + ); } return { deleteFromColumn: { familyName: column.family!, - columnQualifier: Mutation.convertToBytes(column.qualifier) as - Uint8Array, + columnQualifier: Mutation.convertToBytes( + column.qualifier + ) as Uint8Array, timeRange, }, }; diff --git a/src/row.ts b/src/row.ts index 60dd3c3e7..e699cd30f 100644 --- a/src/row.ts +++ b/src/row.ts @@ -229,27 +229,26 @@ export class Row { * region_tag:bigtable_create_row */ create(options, callback) { - if (is.function(options)) - { - callback = options; - options = {}; - } - - const entry = { - key: this.id, - data: options.entry, - method: Mutation.methods.INSERT, - }; - this.data = {}; + if (is.function(options)) { + callback = options; + options = {}; + } + + const entry = { + key: this.id, + data: options.entry, + method: Mutation.methods.INSERT, + }; + this.data = {}; - this.table.mutate(entry, options.gaxOptions, (err, apiResponse) => { - if (err) { - callback(err, null, apiResponse); - return; - } + this.table.mutate(entry, options.gaxOptions, (err, apiResponse) => { + if (err) { + callback(err, null, apiResponse); + return; + } - callback(null, this, apiResponse); - }); + callback(null, this, apiResponse); + }); } /** @@ -271,48 +270,49 @@ export class Row { * region_tag:bigtable_create_rules */ createRules(rules, gaxOptions, callback) { - if (is.fn(gaxOptions)) { - callback = gaxOptions; - gaxOptions = {}; - } + if (is.fn(gaxOptions)) { + callback = gaxOptions; + gaxOptions = {}; + } + + if (!rules || rules.length === 0) { + throw new Error('At least one rule must be provided.'); + } + + rules = arrify(rules).map(rule => { + const column = Mutation.parseColumnName(rule.column); + const ruleData: any = { + familyName: column.family, + columnQualifier: Mutation.convertToBytes(column.qualifier!), + }; - if (!rules || rules.length === 0) { - throw new Error('At least one rule must be provided.'); + if (rule.append) { + ruleData.appendValue = Mutation.convertToBytes(rule.append); } - rules = arrify(rules).map(rule => { - const column = Mutation.parseColumnName(rule.column); - const ruleData: any = { - familyName: column.family, - columnQualifier: Mutation.convertToBytes(column.qualifier!), - }; - - if (rule.append) { - ruleData.appendValue = Mutation.convertToBytes(rule.append); - } - - if (rule.increment) { - ruleData.incrementAmount = rule.increment; - } + if (rule.increment) { + ruleData.incrementAmount = rule.increment; + } - return ruleData; - }); + return ruleData; + }); - const reqOpts = { - tableName: this.table.name, - appProfileId: this.bigtable.appProfileId, - rowKey: Mutation.convertToBytes(this.id), - rules, - }; - this.data = {}; - this.bigtable.request( - { - client: 'BigtableClient', - method: 'readModifyWriteRow', - reqOpts, - gaxOpts: gaxOptions, - }, - callback); + const reqOpts = { + tableName: this.table.name, + appProfileId: this.bigtable.appProfileId, + rowKey: Mutation.convertToBytes(this.id), + rules, + }; + this.data = {}; + this.bigtable.request( + { + client: 'BigtableClient', + method: 'readModifyWriteRow', + reqOpts, + gaxOpts: gaxOptions, + }, + callback + ); } /** @@ -329,17 +329,17 @@ export class Row { * region_tag:bigtable_delete_all_cells */ delete(gaxOptions, callback) { - if (is.fn(gaxOptions)) { - callback = gaxOptions; - gaxOptions = {}; - } - - const mutation = { - key: this.id, - method: Mutation.methods.DELETE, - }; - this.data = {}; - this.table.mutate(mutation, gaxOptions, callback); + if (is.fn(gaxOptions)) { + callback = gaxOptions; + gaxOptions = {}; + } + + const mutation = { + key: this.id, + method: Mutation.methods.DELETE, + }; + this.data = {}; + this.table.mutate(mutation, gaxOptions, callback); } /** @@ -357,18 +357,18 @@ export class Row { * region_tag:bigtable_delete_particular_cells */ deleteCells(columns, gaxOptions, callback) { - if (is.fn(gaxOptions)) { - callback = gaxOptions; - gaxOptions = {}; - } - - const mutation = { - key: this.id, - data: arrify(columns), - method: Mutation.methods.DELETE, - }; - this.data = {}; - this.table.mutate(mutation, gaxOptions, callback); + if (is.fn(gaxOptions)) { + callback = gaxOptions; + gaxOptions = {}; + } + + const mutation = { + key: this.id, + data: arrify(columns), + method: Mutation.methods.DELETE, + }; + this.data = {}; + this.table.mutate(mutation, gaxOptions, callback); } /** @@ -385,24 +385,24 @@ export class Row { * region_tag:bigtable_row_exists */ exists(gaxOptions, callback) { - if (is.fn(gaxOptions)) { - callback = gaxOptions; - gaxOptions = {}; - } - - this.getMetadata(gaxOptions, err => { - if (err) { - if (err instanceof RowError) { - callback(null, false); - return; - } - - callback(err); + if (is.fn(gaxOptions)) { + callback = gaxOptions; + gaxOptions = {}; + } + + this.getMetadata(gaxOptions, err => { + if (err) { + if (err instanceof RowError) { + callback(null, false); return; } - callback(null, true); - }); + callback(err); + return; + } + + callback(null, true); + }); } /** @@ -427,35 +427,36 @@ export class Row { * region_tag:bigtable_row_filter */ filter(filter, config, callback) { - const reqOpts = { - tableName: this.table.name, - appProfileId: this.bigtable.appProfileId, - rowKey: Mutation.convertToBytes(this.id), - predicateFilter: Filter.parse(filter), - trueMutations: createFlatMutationsList(config.onMatch), - falseMutations: createFlatMutationsList(config.onNoMatch), - }; - this.data = {}; - this.bigtable.request( - { - client: 'BigtableClient', - method: 'checkAndMutateRow', - reqOpts, - gaxOpts: config.gaxOptions, - }, - (err, apiResponse) => { - if (err) { - callback(err, null, apiResponse); - return; - } - - callback(null, apiResponse.predicateMatched, apiResponse); - }); - - function createFlatMutationsList(entries) { - entries = arrify(entries).map(entry => Mutation.parse(entry).mutations); - return entries.reduce((a, b) => a.concat(b), []); + const reqOpts = { + tableName: this.table.name, + appProfileId: this.bigtable.appProfileId, + rowKey: Mutation.convertToBytes(this.id), + predicateFilter: Filter.parse(filter), + trueMutations: createFlatMutationsList(config.onMatch), + falseMutations: createFlatMutationsList(config.onNoMatch), + }; + this.data = {}; + this.bigtable.request( + { + client: 'BigtableClient', + method: 'checkAndMutateRow', + reqOpts, + gaxOpts: config.gaxOptions, + }, + (err, apiResponse) => { + if (err) { + callback(err, null, apiResponse); + return; + } + + callback(null, apiResponse.predicateMatched, apiResponse); } + ); + + function createFlatMutationsList(entries) { + entries = arrify(entries).map(entry => Mutation.parse(entry).mutations); + return entries.reduce((a, b) => a.concat(b), []); + } } /** @@ -476,74 +477,73 @@ export class Row { * region_tag:bigtable_get_row */ get(columns, options, callback?) { - if (!is.array(columns)) { - callback = options; - options = columns; - columns = []; - } - - if (is.function(options)) - { - callback = options; - options = {}; + if (!is.array(columns)) { + callback = options; + options = columns; + columns = []; + } + + if (is.function(options)) { + callback = options; + options = {}; + } + + let filter; + columns = arrify(columns); + + // if there is column filter + if (columns.length) { + const filters = columns.map(Mutation.parseColumnName).map(column => { + const colmFilters: any = [{family: column.family}]; + if (column.qualifier) { + colmFilters.push({column: column.qualifier}); } + return colmFilters; + }); - let filter; - columns = arrify(columns); - - // if there is column filter - if (columns.length) { - const filters = columns.map(Mutation.parseColumnName).map(column => { - const colmFilters: any = [{family: column.family}]; - if (column.qualifier) { - colmFilters.push({column: column.qualifier}); - } - return colmFilters; - }); - - // if there is more then one filter, make it type inteleave filter - if (filters.length > 1) { - filter = [ - { - interleave: filters, - }, - ]; - } else { - filter = filters[0]; - } - } + // if there is more then one filter, make it type inteleave filter + if (filters.length > 1) { + filter = [ + { + interleave: filters, + }, + ]; + } else { + filter = filters[0]; + } + } - // if there is also a second option.filter append to filter array - if (options.filter) { - filter = arrify(filter).concat(options.filter); - } + // if there is also a second option.filter append to filter array + if (options.filter) { + filter = arrify(filter).concat(options.filter); + } - const getRowsOptions = Object.assign({}, options, { - keys: [this.id], - filter, - }); + const getRowsOptions = Object.assign({}, options, { + keys: [this.id], + filter, + }); - this.table.getRows(getRowsOptions, (err, rows) => { - if (err) { - callback(err); - return; - } + this.table.getRows(getRowsOptions, (err, rows) => { + if (err) { + callback(err); + return; + } - const row = rows[0]; + const row = rows[0]; - if (!row) { - err = new RowError(this.id); - callback(err); - return; - } + if (!row) { + err = new RowError(this.id); + callback(err); + return; + } - this.data = row.data; + this.data = row.data; - // If the user specifies column names, we'll return back the row data - // we received. Otherwise, we'll return the row "this" in a typical - // GrpcServiceObject#get fashion. - callback(null, columns.length ? row.data : this); - }); + // If the user specifies column names, we'll return back the row data + // we received. Otherwise, we'll return the row "this" in a typical + // GrpcServiceObject#get fashion. + callback(null, columns.length ? row.data : this); + }); } /** @@ -563,20 +563,19 @@ export class Row { * region_tag:bigtable_get_row_meta */ getMetadata(options, callback) { - if (is.function(options)) - { - callback = options; - options = {}; - } - - this.get(options, (err, row) => { - if (err) { - callback(err); - return; - } + if (is.function(options)) { + callback = options; + options = {}; + } + + this.get(options, (err, row) => { + if (err) { + callback(err); + return; + } - callback(null, row.metadata); - }); + callback(null, row.metadata); + }); } /** @@ -597,45 +596,42 @@ export class Row { * region_tag:bigtable_row_increment */ increment(column, value, gaxOptions, callback) { - // increment('column', callback) - if (is.function(value)) - { - callback = value; - value = 1; - gaxOptions = {}; - } - - // increment('column', value, callback) - if (is.function(gaxOptions)) - { - callback = gaxOptions; - gaxOptions = {}; - } - - // increment('column', { gaxOptions }, callback) - if (is.object(value)) { - callback = gaxOptions; - gaxOptions = value; - value = 1; - } - - const reqOpts = { - column, - increment: value, - }; - - this.createRules(reqOpts, gaxOptions, (err, resp) => { - if (err) { - callback(err, null, resp); - return; - } - - const data = Row.formatFamilies_(resp.row.families); - const value = - dotProp.get(data, column.replace(':', '.'))[0].value; - - callback(null, value, resp); - }); + // increment('column', callback) + if (is.function(value)) { + callback = value; + value = 1; + gaxOptions = {}; + } + + // increment('column', value, callback) + if (is.function(gaxOptions)) { + callback = gaxOptions; + gaxOptions = {}; + } + + // increment('column', { gaxOptions }, callback) + if (is.object(value)) { + callback = gaxOptions; + gaxOptions = value; + value = 1; + } + + const reqOpts = { + column, + increment: value, + }; + + this.createRules(reqOpts, gaxOptions, (err, resp) => { + if (err) { + callback(err, null, resp); + return; + } + + const data = Row.formatFamilies_(resp.row.families); + const value = dotProp.get(data, column.replace(':', '.'))[0].value; + + callback(null, value, resp); + }); } /** @@ -654,18 +650,18 @@ export class Row { * region_tag:bigtable_row_save */ save(entry, gaxOptions, callback) { - if (is.fn(gaxOptions)) { - callback = gaxOptions; - gaxOptions = {}; - } - - const mutation = { - key: this.id, - data: entry, - method: Mutation.methods.INSERT, - }; - this.data = {}; - this.table.mutate(mutation, gaxOptions, callback); + if (is.fn(gaxOptions)) { + callback = gaxOptions; + gaxOptions = {}; + } + + const mutation = { + key: this.id, + data: entry, + method: Mutation.methods.INSERT, + }; + this.data = {}; + this.table.mutate(mutation, gaxOptions, callback); } } diff --git a/src/table.ts b/src/table.ts index 0b735098b..9b6a9ec8c 100644 --- a/src/table.ts +++ b/src/table.ts @@ -195,48 +195,48 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`); * region_tag:bigtable_create_family */ createFamily(id, options, callback?) { - if (is.function(options)) - { - callback = options; - options = {}; - } + if (is.function(options)) { + callback = options; + options = {}; + } - if (!id) { - throw new Error('An id is required to create a family.'); - } + if (!id) { + throw new Error('An id is required to create a family.'); + } - const mod: any = { - id, - create: {}, - }; + const mod: any = { + id, + create: {}, + }; - if (options.rule) { - mod.create.gcRule = Family.formatRule_(options.rule); - } + if (options.rule) { + mod.create.gcRule = Family.formatRule_(options.rule); + } - const reqOpts = { - name: this.name, - modifications: [mod], - }; + const reqOpts = { + name: this.name, + modifications: [mod], + }; - this.bigtable.request( - { - client: 'BigtableTableAdminClient', - method: 'modifyColumnFamilies', - reqOpts, - gaxOpts: options.gaxOptions, - }, - (err, resp) => { - if (err) { - callback(err, null, resp); - return; - } + this.bigtable.request( + { + client: 'BigtableTableAdminClient', + method: 'modifyColumnFamilies', + reqOpts, + gaxOpts: options.gaxOptions, + }, + (err, resp) => { + if (err) { + callback(err, null, resp); + return; + } - const family = this.family(id); - family.metadata = resp; + const family = this.family(id); + family.metadata = resp; - callback(null, family, resp); - }); + callback(null, family, resp); + } + ); } /** @@ -266,189 +266,195 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`); * region_tag:bigtable_table_readstream */ createReadStream(options?) { - options = options || {}; - const maxRetries = is.number(this.maxRetries) ? this.maxRetries : 3; - - let rowKeys; - const ranges = options.ranges || []; - let filter; - let rowsLimit; - let rowsRead = 0; - let numRequestsMade = 0; - - if (options.start || options.end) { - if (options.ranges || options.prefix || options.prefixes) { - throw new Error( - 'start/end should be used exclusively to ranges/prefix/prefixes.'); - } - ranges.push({ - start: options.start, - end: options.end, - }); + options = options || {}; + const maxRetries = is.number(this.maxRetries) ? this.maxRetries : 3; + + let rowKeys; + const ranges = options.ranges || []; + let filter; + let rowsLimit; + let rowsRead = 0; + let numRequestsMade = 0; + + if (options.start || options.end) { + if (options.ranges || options.prefix || options.prefixes) { + throw new Error( + 'start/end should be used exclusively to ranges/prefix/prefixes.' + ); } + ranges.push({ + start: options.start, + end: options.end, + }); + } - if (options.keys) { - rowKeys = options.keys; - } + if (options.keys) { + rowKeys = options.keys; + } - if (options.prefix) { - if (options.ranges || options.start || options.end || - options.prefixes) { - throw new Error( - 'prefix should be used exclusively to ranges/start/end/prefixes.'); - } - ranges.push(Table.createPrefixRange(options.prefix)); + if (options.prefix) { + if (options.ranges || options.start || options.end || options.prefixes) { + throw new Error( + 'prefix should be used exclusively to ranges/start/end/prefixes.' + ); } + ranges.push(Table.createPrefixRange(options.prefix)); + } - if (options.prefixes) { - if (options.ranges || options.start || options.end || options.prefix) { - throw new Error( - 'prefixes should be used exclusively to ranges/start/end/prefix.'); - } - options.prefixes.forEach(prefix => { - ranges.push(Table.createPrefixRange(prefix)); - }); + if (options.prefixes) { + if (options.ranges || options.start || options.end || options.prefix) { + throw new Error( + 'prefixes should be used exclusively to ranges/start/end/prefix.' + ); } + options.prefixes.forEach(prefix => { + ranges.push(Table.createPrefixRange(prefix)); + }); + } - if (options.filter) { - filter = Filter.parse(options.filter); - } + if (options.filter) { + filter = Filter.parse(options.filter); + } - if (options.limit) { - rowsLimit = options.limit; - } + if (options.limit) { + rowsLimit = options.limit; + } - const userStream = through.obj(); - let chunkTransformer; + const userStream = through.obj(); + let chunkTransformer; - const makeNewRequest = () => { - const lastRowKey = chunkTransformer ? chunkTransformer.lastRowKey : ''; - chunkTransformer = - new ChunkTransformer({decode: options.decode} as any); + const makeNewRequest = () => { + const lastRowKey = chunkTransformer ? chunkTransformer.lastRowKey : ''; + chunkTransformer = new ChunkTransformer({decode: options.decode} as any); - const reqOpts: any = { - tableName: this.name, - appProfileId: this.bigtable.appProfileId, - }; + const reqOpts: any = { + tableName: this.name, + appProfileId: this.bigtable.appProfileId, + }; - const retryOpts = { - currentRetryAttempt: numRequestsMade, - }; + const retryOpts = { + currentRetryAttempt: numRequestsMade, + }; - if (lastRowKey) { - const lessThan = (lhs, rhs) => { - const lhsBytes = Mutation.convertToBytes(lhs); - const rhsBytes = Mutation.convertToBytes(rhs); - return (lhsBytes as Buffer).compare(rhsBytes as Uint8Array) === -1; - }; - const greaterThan = (lhs, rhs) => lessThan(rhs, lhs); - const greaterThanOrEqualTo = (lhs, rhs) => !lessThan(rhs, lhs); - - if (ranges.length === 0) { - ranges.push({ - start: { - value: lastRowKey, - inclusive: false, - }, - }); - } else { - // Readjust and/or remove ranges based on previous valid row reads. - - // Iterate backward since items may need to be removed. - for (let index = ranges.length - 1; index >= 0; index--) { - const range = ranges[index]; - const startValue = is.object(range.start) ? range.start.value : - range.start; - const endValue = is.object(range.end) ? range.end.value : - range.end; - const isWithinStart = - !startValue || greaterThanOrEqualTo(startValue, lastRowKey); - const isWithinEnd = !endValue || lessThan(lastRowKey, endValue); - if (isWithinStart) { - if (isWithinEnd) { - // The lastRowKey is within this range, adjust the start - // value. - range.start = { - value: lastRowKey, - inclusive: false, - }; - } else { - // The lastRowKey is past this range, remove this range. - ranges.splice(index, 1); - } + if (lastRowKey) { + const lessThan = (lhs, rhs) => { + const lhsBytes = Mutation.convertToBytes(lhs); + const rhsBytes = Mutation.convertToBytes(rhs); + return (lhsBytes as Buffer).compare(rhsBytes as Uint8Array) === -1; + }; + const greaterThan = (lhs, rhs) => lessThan(rhs, lhs); + const greaterThanOrEqualTo = (lhs, rhs) => !lessThan(rhs, lhs); + + if (ranges.length === 0) { + ranges.push({ + start: { + value: lastRowKey, + inclusive: false, + }, + }); + } else { + // Readjust and/or remove ranges based on previous valid row reads. + + // Iterate backward since items may need to be removed. + for (let index = ranges.length - 1; index >= 0; index--) { + const range = ranges[index]; + const startValue = is.object(range.start) + ? range.start.value + : range.start; + const endValue = is.object(range.end) ? range.end.value : range.end; + const isWithinStart = + !startValue || greaterThanOrEqualTo(startValue, lastRowKey); + const isWithinEnd = !endValue || lessThan(lastRowKey, endValue); + if (isWithinStart) { + if (isWithinEnd) { + // The lastRowKey is within this range, adjust the start + // value. + range.start = { + value: lastRowKey, + inclusive: false, + }; + } else { + // The lastRowKey is past this range, remove this range. + ranges.splice(index, 1); } } } - - // Remove rowKeys already read. - if (rowKeys) { - rowKeys = rowKeys.filter(rowKey => greaterThan(rowKey, lastRowKey)); - if (rowKeys.length === 0) { - rowKeys = null; - } - } } - if (rowKeys || ranges.length) { - reqOpts.rows = {}; - if (rowKeys) { - reqOpts.rows.rowKeys = rowKeys.map(Mutation.convertToBytes); - } - - if (ranges.length) { - reqOpts.rows.rowRanges = ranges.map( - range => Filter.createRange(range.start, range.end, 'Key')); + // Remove rowKeys already read. + if (rowKeys) { + rowKeys = rowKeys.filter(rowKey => greaterThan(rowKey, lastRowKey)); + if (rowKeys.length === 0) { + rowKeys = null; } } + } + if (rowKeys || ranges.length) { + reqOpts.rows = {}; - if (filter) { - reqOpts.filter = filter; + if (rowKeys) { + reqOpts.rows.rowKeys = rowKeys.map(Mutation.convertToBytes); } - if (rowsLimit) { - reqOpts.rowsLimit = rowsLimit - rowsRead; + if (ranges.length) { + reqOpts.rows.rowRanges = ranges.map(range => + Filter.createRange(range.start, range.end, 'Key') + ); } + } - const requestStream = this.bigtable.request({ - client: 'BigtableClient', - method: 'readRows', - reqOpts, - gaxOpts: options.gaxOptions, - retryOpts, - }); + if (filter) { + reqOpts.filter = filter; + } - requestStream.on('request', () => numRequestsMade++); + if (rowsLimit) { + reqOpts.rowsLimit = rowsLimit - rowsRead; + } - const rowStream = pumpify.obj([ - requestStream, - chunkTransformer, - through.obj((rowData, enc, next) => { - if (chunkTransformer._destroyed || - (userStream as any)._writableState.ended) { - return next(); - } - numRequestsMade = 0; - rowsRead++; - const row = this.row(rowData.key); - row.data = rowData.data; - next(null, row); - }), - ]); - - rowStream.on('error', error => { - rowStream.unpipe(userStream); - if (numRequestsMade <= maxRetries && - RETRYABLE_STATUS_CODES.has(error.code)) { - makeNewRequest(); - } else { - userStream.emit('error', error); + const requestStream = this.bigtable.request({ + client: 'BigtableClient', + method: 'readRows', + reqOpts, + gaxOpts: options.gaxOptions, + retryOpts, + }); + + requestStream.on('request', () => numRequestsMade++); + + const rowStream = pumpify.obj([ + requestStream, + chunkTransformer, + through.obj((rowData, enc, next) => { + if ( + chunkTransformer._destroyed || + (userStream as any)._writableState.ended + ) { + return next(); } - }); - rowStream.pipe(userStream); - }; + numRequestsMade = 0; + rowsRead++; + const row = this.row(rowData.key); + row.data = rowData.data; + next(null, row); + }), + ]); + + rowStream.on('error', error => { + rowStream.unpipe(userStream); + if ( + numRequestsMade <= maxRetries && + RETRYABLE_STATUS_CODES.has(error.code) + ) { + makeNewRequest(); + } else { + userStream.emit('error', error); + } + }); + rowStream.pipe(userStream); + }; - makeNewRequest(); - return userStream; + makeNewRequest(); + return userStream; } /** @@ -465,21 +471,22 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`); * region_tag:bigtable_del_table */ delete(gaxOptions, callback?) { - if (is.fn(gaxOptions)) { - callback = gaxOptions; - gaxOptions = {}; - } + if (is.fn(gaxOptions)) { + callback = gaxOptions; + gaxOptions = {}; + } - this.bigtable.request( - { - client: 'BigtableTableAdminClient', - method: 'deleteTable', - reqOpts: { - name: this.name, - }, - gaxOpts: gaxOptions, - }, - callback); + this.bigtable.request( + { + client: 'BigtableTableAdminClient', + method: 'deleteTable', + reqOpts: { + name: this.name, + }, + gaxOpts: gaxOptions, + }, + callback + ); } /** @@ -499,29 +506,29 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`); * region_tag:bigtable_del_rows */ deleteRows(prefix, gaxOptions, callback?) { - if (is.function(gaxOptions)) - { - callback = gaxOptions; - gaxOptions = {}; - } + if (is.function(gaxOptions)) { + callback = gaxOptions; + gaxOptions = {}; + } - if (!prefix || is.fn(prefix)) { - throw new Error('A prefix is required for deleteRows.'); - } + if (!prefix || is.fn(prefix)) { + throw new Error('A prefix is required for deleteRows.'); + } - const reqOpts = { - name: this.name, - rowKeyPrefix: Mutation.convertToBytes(prefix), - }; + const reqOpts = { + name: this.name, + rowKeyPrefix: Mutation.convertToBytes(prefix), + }; - this.bigtable.request( - { - client: 'BigtableTableAdminClient', - method: 'dropRowRange', - reqOpts, - gaxOpts: gaxOptions, - }, - callback); + this.bigtable.request( + { + client: 'BigtableTableAdminClient', + method: 'dropRowRange', + reqOpts, + gaxOpts: gaxOptions, + }, + callback + ); } /** @@ -538,29 +545,29 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`); * region_tag:bigtable_exists_table */ exists(gaxOptions, callback?) { - if (is.fn(gaxOptions)) { - callback = gaxOptions; - gaxOptions = {}; - } + if (is.fn(gaxOptions)) { + callback = gaxOptions; + gaxOptions = {}; + } - const reqOpts = { - view: 'name', - gaxOptions, - }; + const reqOpts = { + view: 'name', + gaxOptions, + }; - this.getMetadata(reqOpts, err => { - if (err) { - if (err.code === 5) { - callback(null, false); - return; - } + this.getMetadata(reqOpts, err => { + if (err) { + if (err.code === 5) { + callback(null, false); + return; + } - callback(err); - return; - } + callback(err); + return; + } - callback(null, true); - }); + callback(null, true); + }); } /** @@ -575,10 +582,10 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`); * const family = table.family('my-family'); */ family(id: string) { - if (!id) { - throw new Error('A family id must be provided.'); - } - return new Family(this, id); + if (!id) { + throw new Error('A family id must be provided.'); + } + return new Family(this, id); } /** @@ -602,27 +609,27 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`); * region_tag:bigtable_get_table */ get(options, callback?) { - if (is.fn(options)) { - callback = options; - options = {}; - } + if (is.fn(options)) { + callback = options; + options = {}; + } - const autoCreate = !!options.autoCreate; - const gaxOptions = options.gaxOptions; + const autoCreate = !!options.autoCreate; + const gaxOptions = options.gaxOptions; - this.getMetadata({gaxOptions}, (err, metadata) => { - if (err) { - if (err.code === 5 && autoCreate) { - this.create({gaxOptions}, callback); - return; - } + this.getMetadata({gaxOptions}, (err, metadata) => { + if (err) { + if (err.code === 5 && autoCreate) { + this.create({gaxOptions}, callback); + return; + } - callback(err); - return; - } + callback(err); + return; + } - callback(null, this, metadata); - }); + callback(null, this, metadata); + }); } /** @@ -639,26 +646,25 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`); * region_tag:bigtable_get_families */ getFamilies(gaxOptions, callback?) { - if (is.fn(gaxOptions)) { - callback = gaxOptions; - gaxOptions = {}; - } + if (is.fn(gaxOptions)) { + callback = gaxOptions; + gaxOptions = {}; + } - this.getMetadata({gaxOptions}, (err, metadata) => { - if (err) { - callback(err); - return; - } + this.getMetadata({gaxOptions}, (err, metadata) => { + if (err) { + callback(err); + return; + } - const families = - Object.keys(metadata.columnFamilies).map(familyId => { - const family = this.family(familyId); - family.metadata = metadata.columnFamilies[familyId]; - return family; - }); + const families = Object.keys(metadata.columnFamilies).map(familyId => { + const family = this.family(familyId); + family.metadata = metadata.columnFamilies[familyId]; + return family; + }); - callback(null, families, metadata.columnFamilies); - }); + callback(null, families, metadata.columnFamilies); + }); } /** @@ -690,26 +696,25 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`); * }); */ getReplicationStates(gaxOptions, callback?) { - if (is.fn(gaxOptions)) { - callback = gaxOptions; - gaxOptions = {}; - } - const reqOpts = { - view: 'replication', - gaxOptions, - }; - this.getMetadata(reqOpts, (err, metadata) => { - if (err) { - callback(err); - return; - } - const clusterStates = new Map(); - Object.keys(metadata.clusterStates) - .map( - clusterId => clusterStates.set( - clusterId, metadata.clusterStates[clusterId])); - callback(null, clusterStates, metadata); - }); + if (is.fn(gaxOptions)) { + callback = gaxOptions; + gaxOptions = {}; + } + const reqOpts = { + view: 'replication', + gaxOptions, + }; + this.getMetadata(reqOpts, (err, metadata) => { + if (err) { + callback(err); + return; + } + const clusterStates = new Map(); + Object.keys(metadata.clusterStates).map(clusterId => + clusterStates.set(clusterId, metadata.clusterStates[clusterId]) + ); + callback(null, clusterStates, metadata); + }); } /** @@ -728,31 +733,31 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`); * region_tag:bigtable_get_table_meta */ getMetadata(options, callback?) { - if (is.function(options)) - { - callback = options; - options = {}; - } + if (is.function(options)) { + callback = options; + options = {}; + } - const reqOpts = { - name: this.name, - view: (Table as any).VIEWS[options.view || 'unspecified'], - }; - - this.bigtable.request( - { - client: 'BigtableTableAdminClient', - method: 'getTable', - reqOpts, - gaxOpts: options.gaxOptions, - }, - (...args) => { - if (args[1]) { - this.metadata = args[1]; - } - - callback(...args); - }); + const reqOpts = { + name: this.name, + view: (Table as any).VIEWS[options.view || 'unspecified'], + }; + + this.bigtable.request( + { + client: 'BigtableTableAdminClient', + method: 'getTable', + reqOpts, + gaxOpts: options.gaxOptions, + }, + (...args) => { + if (args[1]) { + this.metadata = args[1]; + } + + callback(...args); + } + ); } /** @@ -774,17 +779,18 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`); * region_tag:bigtable_get_rows */ getRows(options, callback?) { - if (is.function(options)) - { - callback = options; - options = {}; - } + if (is.function(options)) { + callback = options; + options = {}; + } - this.createReadStream(options) - .on('error', callback) - .pipe(concat(rows => { - callback(null, rows); - })); + this.createReadStream(options) + .on('error', callback) + .pipe( + concat(rows => { + callback(null, rows); + }) + ); } /** @@ -806,17 +812,17 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`); * region_tag:bigtable_insert_rows */ insert(entries, gaxOptions, callback?) { - if (is.fn(gaxOptions)) { - callback = gaxOptions; - gaxOptions = {}; - } + if (is.fn(gaxOptions)) { + callback = gaxOptions; + gaxOptions = {}; + } - entries = arrify(entries).map(entry => { - entry.method = Mutation.methods.INSERT; - return entry; - }); + entries = arrify(entries).map(entry => { + entry.method = Mutation.methods.INSERT; + return entry; + }); - return this.mutate(entries, {gaxOptions}, callback); + return this.mutate(entries, {gaxOptions}, callback); } /** @@ -841,114 +847,106 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`); * region_tag:bigtable_mutate_rows */ mutate(entries, options?, callback?) { - options = options || {}; + options = options || {}; - if (is.fn(options)) { - callback = options; - options = {}; - } + if (is.fn(options)) { + callback = options; + options = {}; + } - entries = arrify(entries).reduce((a, b) => a.concat(b), []); + entries = arrify(entries).reduce((a, b) => a.concat(b), []); - let numRequestsMade = 0; + let numRequestsMade = 0; - const maxRetries = is.number(this.maxRetries) ? this.maxRetries : 3; - const pendingEntryIndices = - new Set(entries.map((entry, index) => index)); - const entryToIndex = - new Map(entries.map((entry, index) => [entry, index])); - const mutationErrorsByEntryIndex = new Map(); + const maxRetries = is.number(this.maxRetries) ? this.maxRetries : 3; + const pendingEntryIndices = new Set(entries.map((entry, index) => index)); + const entryToIndex = new Map(entries.map((entry, index) => [entry, index])); + const mutationErrorsByEntryIndex = new Map(); - const onBatchResponse = err => { - if (err) { - // The error happened before a request was even made, don't - // retry. - callback(err); - return; - } - if (pendingEntryIndices.size !== 0 && - numRequestsMade <= maxRetries) { - makeNextBatchRequest(); - return; - } + const onBatchResponse = err => { + if (err) { + // The error happened before a request was even made, don't + // retry. + callback(err); + return; + } + if (pendingEntryIndices.size !== 0 && numRequestsMade <= maxRetries) { + makeNextBatchRequest(); + return; + } - if (mutationErrorsByEntryIndex.size !== 0) { - const mutationErrors = - Array.from(mutationErrorsByEntryIndex.values()); - err = new common.util.PartialFailureError({ - errors: mutationErrors, - } as any); - } + if (mutationErrorsByEntryIndex.size !== 0) { + const mutationErrors = Array.from(mutationErrorsByEntryIndex.values()); + err = new common.util.PartialFailureError({ + errors: mutationErrors, + } as any); + } + + callback(err); + }; + + const makeNextBatchRequest = () => { + const entryBatch = entries.filter((entry, index) => { + return pendingEntryIndices.has(index); + }); + + const reqOpts = { + tableName: this.name, + appProfileId: this.bigtable.appProfileId, + entries: options.rawMutation + ? entryBatch + : entryBatch.map(Mutation.parse), + }; - callback(err); - }; - - const makeNextBatchRequest = () => { - const entryBatch = entries.filter((entry, index) => { - return pendingEntryIndices.has(index); - }); - - const reqOpts = { - tableName: this.name, - appProfileId: this.bigtable.appProfileId, - entries: options.rawMutation ? entryBatch : - entryBatch.map(Mutation.parse), - }; - - const retryOpts = { - currentRetryAttempt: numRequestsMade, - }; - - this.bigtable - .request({ - client: 'BigtableClient', - method: 'mutateRows', - reqOpts, - gaxOpts: options.gaxOptions, - retryOpts, - }) - .on('request', () => numRequestsMade++) - .on('error', - err => { - if (numRequestsMade === 0) { - callback( - err); // Likely a "projectId not detected" error. - return; - } - - onBatchResponse(err); - }) - .on('data', - obj => { - obj.entries.forEach(entry => { - const originalEntry = entryBatch[entry.index]; - const originalEntriesIndex = - entryToIndex.get(originalEntry)!; - - // Mutation was successful. - if (entry.status.code === 0) { - pendingEntryIndices.delete(originalEntriesIndex); - mutationErrorsByEntryIndex.delete( - originalEntriesIndex); - return; - } - - if (!RETRYABLE_STATUS_CODES.has(entry.status.code)) { - pendingEntryIndices.delete(originalEntriesIndex); - } - - const status = (common.Service as any) - .decorateStatus_(entry.status); - status.entry = originalEntry; - - mutationErrorsByEntryIndex.set( - originalEntriesIndex, status); - }); - }) - .on('end', onBatchResponse); - }; - - makeNextBatchRequest(); + const retryOpts = { + currentRetryAttempt: numRequestsMade, + }; + + this.bigtable + .request({ + client: 'BigtableClient', + method: 'mutateRows', + reqOpts, + gaxOpts: options.gaxOptions, + retryOpts, + }) + .on('request', () => numRequestsMade++) + .on('error', err => { + if (numRequestsMade === 0) { + callback(err); // Likely a "projectId not detected" error. + return; + } + + onBatchResponse(err); + }) + .on('data', obj => { + obj.entries.forEach(entry => { + const originalEntry = entryBatch[entry.index]; + const originalEntriesIndex = entryToIndex.get(originalEntry)!; + + // Mutation was successful. + if (entry.status.code === 0) { + pendingEntryIndices.delete(originalEntriesIndex); + mutationErrorsByEntryIndex.delete(originalEntriesIndex); + return; + } + + if (!RETRYABLE_STATUS_CODES.has(entry.status.code)) { + pendingEntryIndices.delete(originalEntriesIndex); + } + + const status = (common.Service as any).decorateStatus_( + entry.status + ); + status.entry = originalEntry; + + mutationErrorsByEntryIndex.set(originalEntriesIndex, status); + }); + }) + .on('end', onBatchResponse); + }; + + makeNextBatchRequest(); } /** @@ -963,11 +961,11 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`); * var row = table.row('lincoln'); */ row(key) { - if (!key) { - throw new Error('A row key must be provided.'); - } + if (!key) { + throw new Error('A row key must be provided.'); + } - return new Row(this, key); + return new Row(this, key); } /** @@ -985,16 +983,18 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`); * region_tag:bigtable_sample_row_keys */ sampleRowKeys(gaxOptions, callback?) { - if (is.fn(gaxOptions)) { - callback = gaxOptions; - gaxOptions = {}; - } + if (is.fn(gaxOptions)) { + callback = gaxOptions; + gaxOptions = {}; + } - this.sampleRowKeysStream(gaxOptions) - .on('error', callback) - .pipe(concat(keys => { - callback(null, keys); - })); + this.sampleRowKeysStream(gaxOptions) + .on('error', callback) + .pipe( + concat(keys => { + callback(null, keys); + }) + ); } /** @@ -1023,25 +1023,25 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`); * }); */ sampleRowKeysStream(gaxOptions?) { - const reqOpts = { - tableName: this.name, - appProfileId: this.bigtable.appProfileId, - }; - - return pumpify.obj([ - this.bigtable.request({ - client: 'BigtableClient', - method: 'sampleRowKeys', - reqOpts, - gaxOpts: gaxOptions, - }), - through.obj((key, enc, next) => { - next(null, { - key: key.rowKey, - offset: key.offsetBytes, - }); - }), - ]); + const reqOpts = { + tableName: this.name, + appProfileId: this.bigtable.appProfileId, + }; + + return pumpify.obj([ + this.bigtable.request({ + client: 'BigtableClient', + method: 'sampleRowKeys', + reqOpts, + gaxOpts: gaxOptions, + }), + through.obj((key, enc, next) => { + next(null, { + key: key.rowKey, + offset: key.offsetBytes, + }); + }), + ]); } /** @@ -1064,24 +1064,25 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`); * }); */ truncate(gaxOptions, callback?) { - if (is.fn(gaxOptions)) { - callback = gaxOptions; - gaxOptions = {}; - } + if (is.fn(gaxOptions)) { + callback = gaxOptions; + gaxOptions = {}; + } + + const reqOpts = { + name: this.name, + deleteAllDataFromTable: true, + }; - const reqOpts = { - name: this.name, - deleteAllDataFromTable: true, - }; - - this.bigtable.request( - { - client: 'BigtableTableAdminClient', - method: 'dropRowRange', - reqOpts, - gaxOpts: gaxOptions, - }, - callback); + this.bigtable.request( + { + client: 'BigtableTableAdminClient', + method: 'dropRowRange', + reqOpts, + gaxOpts: gaxOptions, + }, + callback + ); } /** @@ -1094,43 +1095,43 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`); * @param {?Boolean} callback.resp Boolean value. */ waitForReplication(callback) { - // handler for generated consistency-token - const tokenHandler = (err, token) => { - if (err) { - return callback(err); - } + // handler for generated consistency-token + const tokenHandler = (err, token) => { + if (err) { + return callback(err); + } + + // set timeout for 10 minutes + const timeoutAfterTenMinutes = setTimeout(() => { + callback(null, false); + }, 10 * 60 * 1000); + + // method checks if retrial is required & init retrial with 5 sec + // delay + const retryIfNecessary = (err, res) => { + if (err) { + clearTimeout(timeoutAfterTenMinutes); + return callback(err); + } + + if (res === true) { + clearTimeout(timeoutAfterTenMinutes); + return callback(null, true); + } + + setTimeout(launchCheck, 5000); + }; - // set timeout for 10 minutes - const timeoutAfterTenMinutes = setTimeout(() => { - callback(null, false); - }, 10 * 60 * 1000); - - // method checks if retrial is required & init retrial with 5 sec - // delay - const retryIfNecessary = (err, res) => { - if (err) { - clearTimeout(timeoutAfterTenMinutes); - return callback(err); - } - - if (res === true) { - clearTimeout(timeoutAfterTenMinutes); - return callback(null, true); - } - - setTimeout(launchCheck, 5000); - }; - - // method to launch token consistency check - const launchCheck = () => { - this.checkConsistency(token, retryIfNecessary); - }; - - launchCheck(); - }; - - // generate consistency-token - this.generateConsistencyToken(tokenHandler); + // method to launch token consistency check + const launchCheck = () => { + this.checkConsistency(token, retryIfNecessary); + }; + + launchCheck(); + }; + + // generate consistency-token + this.generateConsistencyToken(tokenHandler); } /** @@ -1140,24 +1141,25 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`); * @param {?String} callback.token The generated consistency token. */ generateConsistencyToken(callback) { - const reqOpts = { - name: this.name, - }; - - this.bigtable.request( - { - client: 'BigtableTableAdminClient', - method: 'generateConsistencyToken', - reqOpts, - }, - (err, res) => { - if (err) { - callback(err); - return; - } - - callback(null, res.consistencyToken); - }); + const reqOpts = { + name: this.name, + }; + + this.bigtable.request( + { + client: 'BigtableTableAdminClient', + method: 'generateConsistencyToken', + reqOpts, + }, + (err, res) => { + if (err) { + callback(err); + return; + } + + callback(null, res.consistencyToken); + } + ); } /** @@ -1168,25 +1170,26 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`); * @param {?Boolean} callback.consistent Boolean value. */ checkConsistency(token, callback) { - const reqOpts = { - name: this.name, - consistencyToken: token, - }; - - this.bigtable.request( - { - client: 'BigtableTableAdminClient', - method: 'checkConsistency', - reqOpts, - }, - (err, res) => { - if (err) { - callback(err); - return; - } - - callback(null, res.consistent); - }); + const reqOpts = { + name: this.name, + consistencyToken: token, + }; + + this.bigtable.request( + { + client: 'BigtableTableAdminClient', + method: 'checkConsistency', + reqOpts, + }, + (err, res) => { + if (err) { + callback(err); + return; + } + + callback(null, res.consistent); + } + ); } /** * The view to be applied to the returned table's fields. @@ -1213,6 +1216,6 @@ promisifyAll(Table, { }); export interface PrefixRange { - start: string; - end: {value: string; inclusive: boolean;}; + start: string; + end: {value: string; inclusive: boolean}; } diff --git a/system-test/bigtable.ts b/system-test/bigtable.ts index 3f04d5c3f..51e2fe05f 100644 --- a/system-test/bigtable.ts +++ b/system-test/bigtable.ts @@ -60,17 +60,20 @@ describe('Bigtable', () => { after(async () => { const [instances] = await (bigtable as any).getInstances(); - const testInstances = - instances.filter(i => i.id.match(PREFIX)).filter(i => { - const timeCreated = i.metadata.labels.time_created; - // Only delete stale resources. - const oneHourAgo = new Date(Date.now() - 3600000); - return !timeCreated || timeCreated <= oneHourAgo; - }); + const testInstances = instances + .filter(i => i.id.match(PREFIX)) + .filter(i => { + const timeCreated = i.metadata.labels.time_created; + // Only delete stale resources. + const oneHourAgo = new Date(Date.now() - 3600000); + return !timeCreated || timeCreated <= oneHourAgo; + }); const q = new Q({concurrency: 5}); - await Promise.all(testInstances.map(instance => { - q.add(() => instance.delete()); - })); + await Promise.all( + testInstances.map(instance => { + q.add(() => instance.delete()); + }) + ); }); describe('instances', () => { @@ -137,8 +140,9 @@ describe('Bigtable', () => { it('should get the app profiles metadata', async () => { const [metadata] = await APP_PROFILE.getMetadata(); assert.strictEqual( - metadata.name, - APP_PROFILE.name.replace('{{projectId}}', bigtable.projectId)); + metadata.name, + APP_PROFILE.name.replace('{{projectId}}', bigtable.projectId) + ); }); it('should update an app profile', async () => { @@ -151,7 +155,9 @@ describe('Bigtable', () => { await APP_PROFILE.setMetadata(options); const [updatedAppProfile] = await APP_PROFILE.get(); assert.strictEqual( - updatedAppProfile.metadata.description, options.description); + updatedAppProfile.metadata.description, + options.description + ); assert.deepStrictEqual(updatedAppProfile.metadata.singleClusterRouting, { clusterId: CLUSTER_ID, allowTransactionalWrites: true, @@ -206,16 +212,15 @@ describe('Bigtable', () => { it('should retrieve a list of tables in stream mode', done => { const tables: any[] = []; INSTANCE.getTablesStream() - .on('error', done) - .on('data', - table => { - assert(table instanceof Table); - tables.push(table); - }) - .on('end', () => { - assert(tables.length > 0); - done(); - }); + .on('error', done) + .on('data', table => { + assert(table instanceof Table); + tables.push(table); + }) + .on('end', () => { + assert(tables.length > 0); + done(); + }); }); it('should check if a table exists', async () => { @@ -242,8 +247,9 @@ describe('Bigtable', () => { it('should get the tables metadata', async () => { const [metadata] = await TABLE.getMetadata(); assert.strictEqual( - metadata.name, - TABLE.name.replace('{{projectId}}', bigtable.projectId)); + metadata.name, + TABLE.name.replace('{{projectId}}', bigtable.projectId) + ); }); it('should create a table with column family data', async () => { @@ -564,16 +570,15 @@ describe('Bigtable', () => { it('should get rows via stream', done => { const rows: any = []; TABLE.createReadStream() - .on('error', done) - .on('data', - row => { - assert(row instanceof Row); - rows.push(row); - }) - .on('end', () => { - assert.strictEqual(rows.length, 4); - done(); - }); + .on('error', done) + .on('data', row => { + assert(row instanceof Row); + rows.push(row); + }) + .on('end', () => { + assert.strictEqual(rows.length, 4); + done(); + }); }); it('should fetch an individual row', async () => { @@ -633,15 +638,14 @@ describe('Bigtable', () => { it('should get sample row keys via stream', done => { const keys: any = []; TABLE.sampleRowKeysStream() - .on('error', done) - .on('data', - rowKey => { - keys.push(rowKey); - }) - .on('end', () => { - assert(keys.length > 0); - done(); - }); + .on('error', done) + .on('data', rowKey => { + keys.push(rowKey); + }) + .on('end', () => { + assert(keys.length > 0); + done(); + }); }); it('should end stream early', async () => { @@ -677,16 +681,15 @@ describe('Bigtable', () => { const rows: any = []; await new Promise((resolve, reject) => { const stream = TABLE.createReadStream() - .on('error', reject) - .on('data', - row => { - rows.push(row); - stream.end(); - }) - .on('end', () => { - assert.strictEqual(rows.length, 1); - resolve(); - }); + .on('error', reject) + .on('data', row => { + rows.push(row); + stream.end(); + }) + .on('end', () => { + assert.strictEqual(rows.length, 1); + resolve(); + }); }); }); diff --git a/system-test/mutate-rows.ts b/system-test/mutate-rows.ts index d490ba77c..3a05b775e 100644 --- a/system-test/mutate-rows.ts +++ b/system-test/mutate-rows.ts @@ -45,9 +45,9 @@ function dispatch(emitter, response) { function entryResponses(statusCodes) { return { entries: statusCodes.map((code, index) => ({ - index, - status: {code}, - })), + index, + status: {code}, + })), }; } @@ -86,7 +86,8 @@ describe('Bigtable/Table', () => { bigtable.api.BigtableClient = { mutateRows: reqOpts => { mutationBatchesInvoked.push( - reqOpts.entries.map(entry => entry.rowKey.asciiSlice())); + reqOpts.entries.map(entry => entry.rowKey.asciiSlice()) + ); mutationCallTimes.push(new Date().getTime()); const emitter = through.obj(); dispatch(emitter, responses.shift()); @@ -105,7 +106,9 @@ describe('Bigtable/Table', () => { TABLE.maxRetries = test.max_retries; TABLE.mutate(test.mutations_request, error => { assert.deepStrictEqual( - mutationBatchesInvoked, test.mutation_batches_invoked); + mutationBatchesInvoked, + test.mutation_batches_invoked + ); getDeltas(mutationCallTimes).forEach((delta, index) => { if (index === 0) { const message = 'First request should happen Immediately'; @@ -116,8 +119,9 @@ describe('Bigtable/Table', () => { // Adjust for some flakiness with the fake timers. const maxBackoff = minBackoff + 1010; - const message = `Backoff for retry #${index} should be between ` + - `${minBackoff} and ${maxBackoff}, was ${delta}`; + const message = + `Backoff for retry #${index} should be between ` + + `${minBackoff} and ${maxBackoff}, was ${delta}`; assert(delta > minBackoff, message); assert(delta < maxBackoff, message); }); diff --git a/system-test/read-rows-acceptance-tests.ts b/system-test/read-rows-acceptance-tests.ts index 8f2bc423c..59e9a9101 100644 --- a/system-test/read-rows-acceptance-tests.ts +++ b/system-test/read-rows-acceptance-tests.ts @@ -15,8 +15,8 @@ */ import * as assert from 'assert'; -const testcases = - require('../../system-test/read-rows-acceptance-test.json').tests; +const testcases = require('../../system-test/read-rows-acceptance-test.json') + .tests; import {PassThrough} from 'stream'; import {Table} from '../src/table.js'; import {Row} from '../src/row.js'; @@ -28,22 +28,28 @@ function applyProtoRoot(filename, root) { filename.root = path.resolve(filename.root) + '/'; root.resolvePath = function(originPath, importPath, alreadyNormalized) { return ProtoBuf.util.path.resolve( - filename.root, importPath, alreadyNormalized); + filename.root, + importPath, + alreadyNormalized + ); }; return filename.file; } const root = new ProtoBuf.Root(); root.loadSync( - applyProtoRoot( - { - root: protosRoot, - file: 'google/bigtable/v2/bigtable.proto', - }, - root), - {keepCase: false}); + applyProtoRoot( + { + root: protosRoot, + file: 'google/bigtable/v2/bigtable.proto', + }, + root + ), + {keepCase: false} +); const ReadRowsResponse = root.lookupType('google.bigtable.v2.ReadRowsResponse'); -const CellChunk = - root.lookupType('google.bigtable.v2.ReadRowsResponse.CellChunk'); +const CellChunk = root.lookupType( + 'google.bigtable.v2.ReadRowsResponse.CellChunk' +); describe('Read Row Acceptance tests', function() { testcases.forEach(function(test) { it(test.name, done => { @@ -51,27 +57,29 @@ describe('Read Row Acceptance tests', function() { const results: any[] = []; const rawResults = test.results || []; const errorCount = rawResults.filter(result => result.error).length; - rawResults.filter(result => !result.error).forEach(result => { - const existingRow = results.find(filter => filter.key === result.rk); - const row = existingRow || {key: result.rk, data: {}}; - const data = row.data; - if (typeof existingRow === 'undefined') { - results.push(row); - } - const family = data[result.fm] || {}; - data[result.fm] = family; - const qualifier = family[result.qual] || []; - family[result.qual] = qualifier; - const resultLabels: any[] = []; - if (result.label !== '') { - resultLabels.push(result.label); - } - qualifier.push({ - value: result.value, - timestamp: '' + result.ts, - labels: resultLabels, + rawResults + .filter(result => !result.error) + .forEach(result => { + const existingRow = results.find(filter => filter.key === result.rk); + const row = existingRow || {key: result.rk, data: {}}; + const data = row.data; + if (typeof existingRow === 'undefined') { + results.push(row); + } + const family = data[result.fm] || {}; + data[result.fm] = family; + const qualifier = family[result.qual] || []; + family[result.qual] = qualifier; + const resultLabels: any[] = []; + if (result.label !== '') { + resultLabels.push(result.label); + } + qualifier.push({ + value: result.value, + timestamp: '' + result.ts, + labels: resultLabels, + }); }); - }); table.bigtable = {}; table.bigtable.request = function() { @@ -81,19 +89,18 @@ describe('Read Row Acceptance tests', function() { setImmediate(function() { test.chunks_base64 - .map(chunk => { - const cellChunk = CellChunk.decode( - Buffer.from(chunk, 'base64')); //.decode64(chunk); - let readRowsResponse: any = {chunks: [cellChunk]}; - readRowsResponse = ReadRowsResponse.create(readRowsResponse); - readRowsResponse = ReadRowsResponse.toObject(readRowsResponse, { - defaults: true, - longs: String, - oneofs: true, - }); - return readRowsResponse; - }) - .forEach(readRowsResponse => stream.push(readRowsResponse)); + .map(chunk => { + const cellChunk = CellChunk.decode(Buffer.from(chunk, 'base64')); //.decode64(chunk); + let readRowsResponse: any = {chunks: [cellChunk]}; + readRowsResponse = ReadRowsResponse.create(readRowsResponse); + readRowsResponse = ReadRowsResponse.toObject(readRowsResponse, { + defaults: true, + longs: String, + oneofs: true, + }); + return readRowsResponse; + }) + .forEach(readRowsResponse => stream.push(readRowsResponse)); stream.push(null); }); @@ -109,19 +116,18 @@ describe('Read Row Acceptance tests', function() { const errors: any[] = []; const rows: any[] = []; - table.createReadStream({}) - .on('error', - err => { - errors.push(err); - verify(); - }) - .on('data', - row => { - rows.push(row); - }) - .on('end', () => { - verify(); - }); + table + .createReadStream({}) + .on('error', err => { + errors.push(err); + verify(); + }) + .on('data', row => { + rows.push(row); + }) + .on('end', () => { + verify(); + }); function verify() { assert.strictEqual(errors.length, errorCount, ' error count mismatch'); assert.strictEqual(rows.length, results.length, 'row count mismatch'); diff --git a/system-test/read-rows.ts b/system-test/read-rows.ts index 7d0df678e..9f2813a45 100644 --- a/system-test/read-rows.ts +++ b/system-test/read-rows.ts @@ -108,13 +108,15 @@ describe('Bigtable/Table', () => { requestOptions.rowRanges = reqOpts.rows.rowRanges.map(range => { const convertedRowRange = {}; Object.keys(range).forEach( - key => (convertedRowRange[key] = range[key].asciiSlice())); + key => (convertedRowRange[key] = range[key].asciiSlice()) + ); return convertedRowRange; }); } if (reqOpts.rows && reqOpts.rows.rowKeys) { - requestOptions.rowKeys = - reqOpts.rows.rowKeys.map(rowKeys => rowKeys.asciiSlice()); + requestOptions.rowKeys = reqOpts.rows.rowKeys.map(rowKeys => + rowKeys.asciiSlice() + ); } if (reqOpts.rowsLimit) { requestOptions.rowsLimit = reqOpts.rowsLimit; @@ -137,9 +139,9 @@ describe('Bigtable/Table', () => { responses = test.responses; TABLE.maxRetries = test.max_retries; TABLE.createReadStream(test.createReadStream_options) - .on('data', row => rowKeysRead[rowKeysRead.length - 1].push(row.id)) - .on('end', () => (endCalled = true)) - .on('error', err => (error = err)); + .on('data', row => rowKeysRead[rowKeysRead.length - 1].push(row.id)) + .on('end', () => (endCalled = true)) + .on('error', err => (error = err)); clock.runAll(); if (test.error) { @@ -151,7 +153,10 @@ describe('Bigtable/Table', () => { } assert.deepStrictEqual(rowKeysRead, test.row_keys_read); assert.strictEqual( - responses.length, 0, 'not all the responses were used'); + responses.length, + 0, + 'not all the responses were used' + ); assert.deepStrictEqual(requestedOptions, test.request_options); }); }); diff --git a/test/app-profile.ts b/test/app-profile.ts index 87d7d1833..070f1cc59 100644 --- a/test/app-profile.ts +++ b/test/app-profile.ts @@ -51,9 +51,9 @@ describe('Bigtable/AppProfile', function() { before(function() { AppProfile = proxyquire('../src/app-profile.js', { - '../src/cluster.js': {Cluster: FakeCluster}, - '@google-cloud/promisify': fakePromisify, - }).AppProfile; + '../src/cluster.js': {Cluster: FakeCluster}, + '@google-cloud/promisify': fakePromisify, + }).AppProfile; }); beforeEach(function() { @@ -86,12 +86,11 @@ describe('Bigtable/AppProfile', function() { assert.strictEqual(appProfile.id, APP_PROFILE_ID); }); - it('should leave full app profile name unaltered and localize the id from the name', - function() { - const appProfile = new AppProfile(INSTANCE, APP_PROFILE_NAME); - assert.strictEqual(appProfile.name, APP_PROFILE_NAME); - assert.strictEqual(appProfile.id, APP_PROFILE_ID); - }); + it('should leave full app profile name unaltered and localize the id from the name', function() { + const appProfile = new AppProfile(INSTANCE, APP_PROFILE_NAME); + assert.strictEqual(appProfile.name, APP_PROFILE_NAME); + assert.strictEqual(appProfile.id, APP_PROFILE_ID); + }); it('should throw if cluster id in wrong format', function() { const id = `appProfiles/${APP_PROFILE_ID}`; @@ -141,14 +140,14 @@ describe('Bigtable/AppProfile', function() { }); it('should throw for an invalid routing policy', function() { - const errorReg = - /An app profile routing policy can only contain "any" or a `Cluster`\./; + const errorReg = /An app profile routing policy can only contain "any" or a `Cluster`\./; assert.throws( - AppProfile.formatAppProfile_.bind(null, { - routing: 'not-any', - }), - errorReg); + AppProfile.formatAppProfile_.bind(null, { + routing: 'not-any', + }), + errorReg + ); }); }); }); @@ -399,10 +398,13 @@ describe('Bigtable/AppProfile', function() { appProfile.bigtable.request = function(config) { assert( - config.reqOpts.updateMask.paths.indexOf('description') !== -1, - `updateMask does not should include 'description'`); + config.reqOpts.updateMask.paths.indexOf('description') !== -1, + `updateMask does not should include 'description'` + ); assert.strictEqual( - config.reqOpts.appProfile.description, options.description); + config.reqOpts.appProfile.description, + options.description + ); done(); }; @@ -429,11 +431,15 @@ describe('Bigtable/AppProfile', function() { appProfile.bigtable.request = function(config) { assert( - config.reqOpts.updateMask.paths.indexOf( - 'multi_cluster_routing_use_any') !== -1, - `updateMask does not should include 'multi_cluster_routing_use_any'`); + config.reqOpts.updateMask.paths.indexOf( + 'multi_cluster_routing_use_any' + ) !== -1, + `updateMask does not should include 'multi_cluster_routing_use_any'` + ); assert.deepStrictEqual( - config.reqOpts.appProfile.multiClusterRoutingUseAny, {}); + config.reqOpts.appProfile.multiClusterRoutingUseAny, + {} + ); done(); }; @@ -445,11 +451,15 @@ describe('Bigtable/AppProfile', function() { appProfile.bigtable.request = function(config) { assert( - config.reqOpts.updateMask.paths.indexOf( - 'single_cluster_routing') !== -1, - `updateMask does not should include 'single_cluster_routing'`); + config.reqOpts.updateMask.paths.indexOf( + 'single_cluster_routing' + ) !== -1, + `updateMask does not should include 'single_cluster_routing'` + ); assert.deepStrictEqual( - config.reqOpts.appProfile.singleClusterRouting, {clusterId}); + config.reqOpts.appProfile.singleClusterRouting, + {clusterId} + ); done(); }; diff --git a/test/chunktransformer.ts b/test/chunktransformer.ts index 3781f4aa9..fe1dab36d 100644 --- a/test/chunktransformer.ts +++ b/test/chunktransformer.ts @@ -45,8 +45,8 @@ describe('Bigtable/ChunkTransformer', function() { let rows; before(function() { ChunkTransformer = proxyquire('../src/chunktransformer.js', { - './mutation.js': {Mutation: FakeMutation}, - }).ChunkTransformer; + './mutation.js': {Mutation: FakeMutation}, + }).ChunkTransformer; }); beforeEach(function() { chunkTransformer = new ChunkTransformer(); @@ -69,16 +69,30 @@ describe('Bigtable/ChunkTransformer', function() { this.state = RowStateEnum.NEW_ROW; assert.deepStrictEqual(chunkTransformer.row, {}, 'invalid initial state'); assert.deepStrictEqual( - chunkTransformer.lastRowKey, undefined, 'invalid initial state'); + chunkTransformer.lastRowKey, + undefined, + 'invalid initial state' + ); assert.deepStrictEqual( - chunkTransformer.family, {}, 'invalid initial state'); + chunkTransformer.family, + {}, + 'invalid initial state' + ); assert.deepStrictEqual( - chunkTransformer.qualifiers, [], 'invalid initial state'); + chunkTransformer.qualifiers, + [], + 'invalid initial state' + ); assert.deepStrictEqual( - chunkTransformer.qualifier, {}, 'invalid initial state'); + chunkTransformer.qualifier, + {}, + 'invalid initial state' + ); assert.deepStrictEqual( - chunkTransformer.state, RowStateEnum.NEW_ROW, - 'invalid initial state'); + chunkTransformer.state, + RowStateEnum.NEW_ROW, + 'invalid initial state' + ); }); }); describe('processNewRow', function() { @@ -124,19 +138,18 @@ describe('Bigtable/ChunkTransformer', function() { }); processNewRowSpy.call(chunkTransformer, {resetRow: true}); }); - it('should destroy when row key is equal to previous row key ', - function(done) { - chunkTransformer.on('error', function() { - assert(destroySpy.called); - done(); - }); - chunkTransformer.lastRowKey = 'key'; + it('should destroy when row key is equal to previous row key ', function(done) { + chunkTransformer.on('error', function() { + assert(destroySpy.called); + done(); + }); + chunkTransformer.lastRowKey = 'key'; - processNewRowSpy.call(chunkTransformer, { - rowKey: 'key', - resetRow: false, - }); - }); + processNewRowSpy.call(chunkTransformer, { + rowKey: 'key', + resetRow: false, + }); + }); it('should destroy when family name is undefined ', function(done) { chunkTransformer.on('error', function() { assert(destroySpy.called); @@ -182,7 +195,10 @@ describe('Bigtable/ChunkTransformer', function() { assert(resetSpy.called, 'reset state failed'); assert(commitSpy.called, 'commit row failed'); assert.strictEqual( - chunkTransformer.lastRowKey, chunk.rowKey, 'wrong state lastRowKey'); + chunkTransformer.lastRowKey, + chunk.rowKey, + 'wrong state lastRowKey' + ); const expectedRow = { key: chunk.rowKey, data: { @@ -230,7 +246,10 @@ describe('Bigtable/ChunkTransformer', function() { }; assert.deepStrictEqual(chunkTransformer.row, partialRow); assert.strictEqual( - chunkTransformer.state, RowStateEnum.ROW_IN_PROGRESS, 'wrong state'); + chunkTransformer.state, + RowStateEnum.ROW_IN_PROGRESS, + 'wrong state' + ); }); it('partial cell ', function() { const chunk = { @@ -263,7 +282,10 @@ describe('Bigtable/ChunkTransformer', function() { }; assert.deepStrictEqual(chunkTransformer.row, partialRow); assert.strictEqual( - chunkTransformer.state, RowStateEnum.CELL_IN_PROGRESS, 'wrong state'); + chunkTransformer.state, + RowStateEnum.CELL_IN_PROGRESS, + 'wrong state' + ); }); }); describe('processRowInProgress', function() { @@ -272,8 +294,10 @@ describe('Bigtable/ChunkTransformer', function() { let commitSpy; let destroySpy; beforeEach(function() { - processRowInProgressSpy = - sinon.spy(chunkTransformer, 'processRowInProgress'); + processRowInProgressSpy = sinon.spy( + chunkTransformer, + 'processRowInProgress' + ); resetSpy = sinon.spy(chunkTransformer, 'reset'); commitSpy = sinon.spy(chunkTransformer, 'commit'); destroySpy = sinon.spy(chunkTransformer, 'destroy'); @@ -394,7 +418,10 @@ describe('Bigtable/ChunkTransformer', function() { const row = rows[0]; assert.deepStrictEqual(row, expectedRow, 'row mismatch'); assert.strictEqual( - chunkTransformer.state, RowStateEnum.NEW_ROW, 'state mismatch'); + chunkTransformer.state, + RowStateEnum.NEW_ROW, + 'state mismatch' + ); }); it('chunk with qualifier and commit should produce row ', function() { chunkTransformer.qualifiers = []; @@ -437,53 +464,58 @@ describe('Bigtable/ChunkTransformer', function() { const row = rows[0]; assert.deepStrictEqual(row, expectedRow, 'row mismatch'); assert.strictEqual( - chunkTransformer.state, RowStateEnum.NEW_ROW, 'state mismatch'); + chunkTransformer.state, + RowStateEnum.NEW_ROW, + 'state mismatch' + ); + }); + it('chunk with familyName and empty qualifier should produce row', function() { + chunkTransformer.qualifiers = []; + chunkTransformer.family = { + qualifier: chunkTransformer.qualifiers, + }; + chunkTransformer.row = { + key: 'key', + data: { + family: chunkTransformer.family, + }, + }; + const chunk = { + commitRow: true, + familyName: {value: 'family2'}, + qualifier: '', + value: 'value', + timestampMicros: 0, + labels: [], + valueSize: 0, + }; + chunkTransformer.processRowInProgress(chunk); + assert(commitSpy.called, 'did not call commit'); + assert(resetSpy.called, 'did not call reset'); + assert.strictEqual(rows.length, 1, 'wrong call to push'); + const expectedRow = { + key: 'key', + data: { + family: { + qualifier: [ + { + value: 'value', + timestamp: 0, + labels: [], + }, + ], + }, + family2: {}, + }, + }; + const row = rows[0]; + assert.deepStrictEqual(row, expectedRow, 'row mismatch'); + assert.strictEqual( + chunkTransformer.state, + RowStateEnum.NEW_ROW, + 'state mismatch' + ); }); - it('chunk with familyName and empty qualifier should produce row', - function() { - chunkTransformer.qualifiers = []; - chunkTransformer.family = { - qualifier: chunkTransformer.qualifiers, - }; - chunkTransformer.row = { - key: 'key', - data: { - family: chunkTransformer.family, - }, - }; - const chunk = { - commitRow: true, - familyName: {value: 'family2'}, - qualifier: '', - value: 'value', - timestampMicros: 0, - labels: [], - valueSize: 0, - }; - chunkTransformer.processRowInProgress(chunk); - assert(commitSpy.called, 'did not call commit'); - assert(resetSpy.called, 'did not call reset'); - assert.strictEqual(rows.length, 1, 'wrong call to push'); - const expectedRow = { - key: 'key', - data: { - family: { - qualifier: [ - { - value: 'value', - timestamp: 0, - labels: [], - }, - ], - }, - family2: {}, - }, - }; - const row = rows[0]; - assert.deepStrictEqual(row, expectedRow, 'row mismatch'); - assert.strictEqual( - chunkTransformer.state, RowStateEnum.NEW_ROW, 'state mismatch'); - }); it('chunk with new family and commitRow should produce row', function() { chunkTransformer.qualifiers = []; chunkTransformer.family = { @@ -528,7 +560,10 @@ describe('Bigtable/ChunkTransformer', function() { const row = rows[0]; assert.deepStrictEqual(row, expectedRow, 'row mismatch'); assert.strictEqual( - chunkTransformer.state, RowStateEnum.NEW_ROW, 'state mismatch'); + chunkTransformer.state, + RowStateEnum.NEW_ROW, + 'state mismatch' + ); }); it('partial cell ', function() { chunkTransformer.qualifiers = []; @@ -566,14 +601,19 @@ describe('Bigtable/ChunkTransformer', function() { }, }; assert.deepStrictEqual( - chunkTransformer.row, expectedState, 'row state mismatch'); + chunkTransformer.row, + expectedState, + 'row state mismatch' + ); assert.strictEqual( - chunkTransformer.state, RowStateEnum.CELL_IN_PROGRESS, - 'state mismatch'); + chunkTransformer.state, + RowStateEnum.CELL_IN_PROGRESS, + 'state mismatch' + ); }); it('should decode numbers', function() { - const RealChunkTransformer = - require('../src/chunktransformer.js').ChunkTransformer; + const RealChunkTransformer = require('../src/chunktransformer.js') + .ChunkTransformer; chunkTransformer = new RealChunkTransformer({decode: true}); resetSpy = sinon.spy(chunkTransformer, 'reset'); commitSpy = sinon.spy(chunkTransformer, 'commit'); @@ -614,10 +654,15 @@ describe('Bigtable/ChunkTransformer', function() { }, }; assert.deepStrictEqual( - chunkTransformer.row, expectedState, 'row mismatch'); + chunkTransformer.row, + expectedState, + 'row mismatch' + ); assert.strictEqual( - chunkTransformer.state, RowStateEnum.ROW_IN_PROGRESS, - 'state mismatch'); + chunkTransformer.state, + RowStateEnum.ROW_IN_PROGRESS, + 'state mismatch' + ); }); }); describe('processCellInProgress', function() { @@ -626,8 +671,10 @@ describe('Bigtable/ChunkTransformer', function() { let commitSpy; let destroySpy; beforeEach(function() { - processCellInProgressSpy = - sinon.spy(chunkTransformer, 'processCellInProgress'); + processCellInProgressSpy = sinon.spy( + chunkTransformer, + 'processCellInProgress' + ); resetSpy = sinon.spy(chunkTransformer, 'reset'); commitSpy = sinon.spy(chunkTransformer, 'commit'); destroySpy = sinon.spy(chunkTransformer, 'destroy'); @@ -743,60 +790,69 @@ describe('Bigtable/ChunkTransformer', function() { const row = rows[0]; assert.deepStrictEqual(row, expectedRow, 'row mismatch'); assert.strictEqual( - chunkTransformer.state, RowStateEnum.NEW_ROW, 'state mismatch'); + chunkTransformer.state, + RowStateEnum.NEW_ROW, + 'state mismatch' + ); + }); + it('without commitRow should change state to processRowInProgress', function() { + chunkTransformer.qualifier = { + value: 'value', + size: 0, + timestamp: 0, + labels: [], + }; + chunkTransformer.qualifiers = [chunkTransformer.qualifier]; + chunkTransformer.family = { + qualifier: chunkTransformer.qualifiers, + }; + chunkTransformer.row = { + key: 'key', + data: { + family: chunkTransformer.family, + }, + }; + const chunk = { + commitRow: false, + value: '2', + valueSize: 0, + }; + chunkTransformer.processCellInProgress(chunk); + assert(!resetSpy.called, 'invalid call to reset'); + assert(!commitSpy.called, 'invalid call to commit'); + assert.strictEqual(rows.length, 0, 'wrong call to push'); + const expectedState = { + key: 'key', + data: { + family: { + qualifier: [ + { + value: 'value2', + size: 0, + timestamp: 0, + labels: [], + }, + ], + }, + }, + }; + assert.deepStrictEqual( + chunkTransformer.row, + expectedState, + 'row mismatch' + ); + assert.strictEqual( + chunkTransformer.state, + RowStateEnum.ROW_IN_PROGRESS, + 'state mismatch' + ); }); - it('without commitRow should change state to processRowInProgress', - function() { - chunkTransformer.qualifier = { - value: 'value', - size: 0, - timestamp: 0, - labels: [], - }; - chunkTransformer.qualifiers = [chunkTransformer.qualifier]; - chunkTransformer.family = { - qualifier: chunkTransformer.qualifiers, - }; - chunkTransformer.row = { - key: 'key', - data: { - family: chunkTransformer.family, - }, - }; - const chunk = { - commitRow: false, - value: '2', - valueSize: 0, - }; - chunkTransformer.processCellInProgress(chunk); - assert(!resetSpy.called, 'invalid call to reset'); - assert(!commitSpy.called, 'invalid call to commit'); - assert.strictEqual(rows.length, 0, 'wrong call to push'); - const expectedState = { - key: 'key', - data: { - family: { - qualifier: [ - { - value: 'value2', - size: 0, - timestamp: 0, - labels: [], - }, - ], - }, - }, - }; - assert.deepStrictEqual( - chunkTransformer.row, expectedState, 'row mismatch'); - assert.strictEqual( - chunkTransformer.state, RowStateEnum.ROW_IN_PROGRESS, - 'state mismatch'); - }); it('should concat buffer when decode option is false', function() { chunkTransformer = new ChunkTransformer({decode: false}); - processCellInProgressSpy = - sinon.spy(chunkTransformer, 'processCellInProgress'); + processCellInProgressSpy = sinon.spy( + chunkTransformer, + 'processCellInProgress' + ); resetSpy = sinon.spy(chunkTransformer, 'reset'); commitSpy = sinon.spy(chunkTransformer, 'commit'); destroySpy = sinon.spy(chunkTransformer, 'destroy'); @@ -844,10 +900,15 @@ describe('Bigtable/ChunkTransformer', function() { }, }; assert.deepStrictEqual( - chunkTransformer.row, expectedState, 'row mismatch'); + chunkTransformer.row, + expectedState, + 'row mismatch' + ); assert.strictEqual( - chunkTransformer.state, RowStateEnum.ROW_IN_PROGRESS, - 'state mismatch'); + chunkTransformer.state, + RowStateEnum.ROW_IN_PROGRESS, + 'state mismatch' + ); }); }); describe('_flush', function() { @@ -883,10 +944,14 @@ describe('Bigtable/ChunkTransformer', function() { beforeEach(function() { callback = sinon.spy(); processNewRowSpy = sinon.spy(chunkTransformer, 'processNewRow'); - processRowInProgressSpy = - sinon.spy(chunkTransformer, 'processRowInProgress'); - processCellInProgressSpy = - sinon.spy(chunkTransformer, 'processCellInProgress'); + processRowInProgressSpy = sinon.spy( + chunkTransformer, + 'processRowInProgress' + ); + processCellInProgressSpy = sinon.spy( + chunkTransformer, + 'processCellInProgress' + ); }); it('when current state is NEW_ROW should call processNewRow', function() { const chunk = { @@ -906,42 +971,44 @@ describe('Bigtable/ChunkTransformer', function() { const err = callback.getCall(0).args[0]; assert(!err, 'did not expect error'); }); - it('when current state is ROW_IN_PROGRESS should call processRowInProgress', - function() { - chunkTransformer.row = {key: 'key'}; - chunkTransformer.state = RowStateEnum.ROW_IN_PROGRESS; - const chunks = [{key: 'key'}]; - chunkTransformer._transform({chunks}, {}, callback); - assert( - processRowInProgressSpy.called, - 'did not call processRowInProgress'); - const err = callback.getCall(0).args[0]; - assert(!err, 'did not expect error'); - }); - it('when current state is CELL_IN_PROGRESS should call processCellInProgress', - function() { - chunkTransformer.row = {key: 'key'}; - chunkTransformer.state = RowStateEnum.CELL_IN_PROGRESS; - const chunks = [{key: 'key'}]; - chunkTransformer._transform({chunks}, {}, callback); - assert( - processCellInProgressSpy.called, - 'did not call processCellInProgress'); - const err = callback.getCall(0).args[0]; - assert(!err, 'did not expect error'); - }); + it('when current state is ROW_IN_PROGRESS should call processRowInProgress', function() { + chunkTransformer.row = {key: 'key'}; + chunkTransformer.state = RowStateEnum.ROW_IN_PROGRESS; + const chunks = [{key: 'key'}]; + chunkTransformer._transform({chunks}, {}, callback); + assert( + processRowInProgressSpy.called, + 'did not call processRowInProgress' + ); + const err = callback.getCall(0).args[0]; + assert(!err, 'did not expect error'); + }); + it('when current state is CELL_IN_PROGRESS should call processCellInProgress', function() { + chunkTransformer.row = {key: 'key'}; + chunkTransformer.state = RowStateEnum.CELL_IN_PROGRESS; + const chunks = [{key: 'key'}]; + chunkTransformer._transform({chunks}, {}, callback); + assert( + processCellInProgressSpy.called, + 'did not call processCellInProgress' + ); + const err = callback.getCall(0).args[0]; + assert(!err, 'did not expect error'); + }); it('should return when stream is destroyed', function() { chunkTransformer._destroyed = true; const chunks = [{key: 'key'}]; chunkTransformer._transform({chunks}, {}, callback); assert(!callback.called, 'unexpected call to next'); }); - it('should change the `lastRowKey` value for `data.lastScannedRowKey`', - function() { - chunkTransformer._transform( - {chunks: [], lastScannedRowKey: 'foo'}, {}, callback); - assert.deepStrictEqual(chunkTransformer.lastRowKey, 'foo'); - }); + it('should change the `lastRowKey` value for `data.lastScannedRowKey`', function() { + chunkTransformer._transform( + {chunks: [], lastScannedRowKey: 'foo'}, + {}, + callback + ); + assert.deepStrictEqual(chunkTransformer.lastRowKey, 'foo'); + }); }); describe('reset', function() { it('should reset initial state', function() { @@ -966,16 +1033,30 @@ describe('Bigtable/ChunkTransformer', function() { chunkTransformer.reset(); assert.deepStrictEqual(chunkTransformer.row, {}, 'invalid initial state'); assert.deepStrictEqual( - chunkTransformer.lastRowKey, 'prevkey', 'invalid initial state'); + chunkTransformer.lastRowKey, + 'prevkey', + 'invalid initial state' + ); assert.deepStrictEqual( - chunkTransformer.family, {}, 'invalid initial state'); + chunkTransformer.family, + {}, + 'invalid initial state' + ); assert.deepStrictEqual( - chunkTransformer.qualifiers, [], 'invalid initial state'); + chunkTransformer.qualifiers, + [], + 'invalid initial state' + ); assert.deepStrictEqual( - chunkTransformer.qualifier, {}, 'invalid initial state'); + chunkTransformer.qualifier, + {}, + 'invalid initial state' + ); assert.deepStrictEqual( - chunkTransformer.state, RowStateEnum.NEW_ROW, - 'invalid initial state'); + chunkTransformer.state, + RowStateEnum.NEW_ROW, + 'invalid initial state' + ); }); }); describe('commit', function() { @@ -1006,16 +1087,30 @@ describe('Bigtable/ChunkTransformer', function() { assert(resetSpy.called, 'did not call reset'); assert.deepStrictEqual(chunkTransformer.row, {}, 'invalid initial state'); assert.deepStrictEqual( - chunkTransformer.lastRowKey, 'key', 'invalid initial state'); + chunkTransformer.lastRowKey, + 'key', + 'invalid initial state' + ); assert.deepStrictEqual( - chunkTransformer.family, {}, 'invalid initial state'); + chunkTransformer.family, + {}, + 'invalid initial state' + ); assert.deepStrictEqual( - chunkTransformer.qualifiers, [], 'invalid initial state'); + chunkTransformer.qualifiers, + [], + 'invalid initial state' + ); assert.deepStrictEqual( - chunkTransformer.qualifier, {}, 'invalid initial state'); + chunkTransformer.qualifier, + {}, + 'invalid initial state' + ); assert.deepStrictEqual( - chunkTransformer.state, RowStateEnum.NEW_ROW, - 'invalid initial state'); + chunkTransformer.state, + RowStateEnum.NEW_ROW, + 'invalid initial state' + ); }); }); describe('moveToNextState', function() { @@ -1023,78 +1118,82 @@ describe('Bigtable/ChunkTransformer', function() { beforeEach(function() { commitSpy = sinon.spy(chunkTransformer, 'commit'); }); - it('chunk with commit row should call callback with row and call commit state', - function() { - chunkTransformer.qualifier = { - value: 'value', - size: 0, - timestamp: 0, - labels: [], - }; - chunkTransformer.qualifiers = [chunkTransformer.qualifier]; - chunkTransformer.family = { - qualifier: chunkTransformer.qualifiers, - }; - chunkTransformer.row = { - key: 'key', - data: { - family: chunkTransformer.family, - }, - }; - const chunk = { - commitRow: true, - }; - chunkTransformer.moveToNextState(chunk); - assert(commitSpy.called, 'did not call commit'); - assert.strictEqual(rows.length, 1, 'did not call push'); - const expectedRow = { - key: 'key', - data: { - family: { - qualifier: [ - { - value: 'value', - size: 0, - timestamp: 0, - labels: [], - }, - ], - }, - }, - }; - const row = rows[0]; - assert.deepStrictEqual(row, expectedRow, 'row mismatch'); - assert.strictEqual( - chunkTransformer.state, RowStateEnum.NEW_ROW, 'state mismatch'); - }); - it('chunk without commitRow and value size>0 should move to CELL_IN_PROGRESS', - function() { - const chunk = { - commitRow: false, - valueSize: 10, - }; - chunkTransformer.state = RowStateEnum.NEW_ROW; - chunkTransformer.moveToNextState(chunk); - assert(!commitSpy.called, 'did not call commit'); - assert.strictEqual(rows.length, 0, 'unexpected call to push'); - assert.strictEqual( - chunkTransformer.state, RowStateEnum.CELL_IN_PROGRESS, - 'wrong state'); - }); - it('chunk without commitRow and value size==0 should move to ROW_IN_PROGRESS', - function() { - const chunk = { - commitRow: false, - valueSize: 0, - }; - chunkTransformer.state = RowStateEnum.CELL_IN_PROGRESS; - chunkTransformer.moveToNextState(chunk); - assert(!commitSpy.called, 'did not call commit'); - assert.strictEqual(rows.length, 0, 'unexpected call to push'); - assert.strictEqual( - chunkTransformer.state, RowStateEnum.ROW_IN_PROGRESS, - 'wrong state'); - }); + it('chunk with commit row should call callback with row and call commit state', function() { + chunkTransformer.qualifier = { + value: 'value', + size: 0, + timestamp: 0, + labels: [], + }; + chunkTransformer.qualifiers = [chunkTransformer.qualifier]; + chunkTransformer.family = { + qualifier: chunkTransformer.qualifiers, + }; + chunkTransformer.row = { + key: 'key', + data: { + family: chunkTransformer.family, + }, + }; + const chunk = { + commitRow: true, + }; + chunkTransformer.moveToNextState(chunk); + assert(commitSpy.called, 'did not call commit'); + assert.strictEqual(rows.length, 1, 'did not call push'); + const expectedRow = { + key: 'key', + data: { + family: { + qualifier: [ + { + value: 'value', + size: 0, + timestamp: 0, + labels: [], + }, + ], + }, + }, + }; + const row = rows[0]; + assert.deepStrictEqual(row, expectedRow, 'row mismatch'); + assert.strictEqual( + chunkTransformer.state, + RowStateEnum.NEW_ROW, + 'state mismatch' + ); + }); + it('chunk without commitRow and value size>0 should move to CELL_IN_PROGRESS', function() { + const chunk = { + commitRow: false, + valueSize: 10, + }; + chunkTransformer.state = RowStateEnum.NEW_ROW; + chunkTransformer.moveToNextState(chunk); + assert(!commitSpy.called, 'did not call commit'); + assert.strictEqual(rows.length, 0, 'unexpected call to push'); + assert.strictEqual( + chunkTransformer.state, + RowStateEnum.CELL_IN_PROGRESS, + 'wrong state' + ); + }); + it('chunk without commitRow and value size==0 should move to ROW_IN_PROGRESS', function() { + const chunk = { + commitRow: false, + valueSize: 0, + }; + chunkTransformer.state = RowStateEnum.CELL_IN_PROGRESS; + chunkTransformer.moveToNextState(chunk); + assert(!commitSpy.called, 'did not call commit'); + assert.strictEqual(rows.length, 0, 'unexpected call to push'); + assert.strictEqual( + chunkTransformer.state, + RowStateEnum.ROW_IN_PROGRESS, + 'wrong state' + ); + }); }); describe('destroy', function() { it('should emit error when destroy is called with error', function(done) { diff --git a/test/cluster.ts b/test/cluster.ts index f6a028be2..48b984941 100644 --- a/test/cluster.ts +++ b/test/cluster.ts @@ -42,8 +42,8 @@ describe('Bigtable/Cluster', function() { before(function() { Cluster = proxyquire('../src/cluster.js', { - '@google-cloud/promisify': fakePromisify, - }).Cluster; + '@google-cloud/promisify': fakePromisify, + }).Cluster; }); beforeEach(function() { @@ -76,12 +76,11 @@ describe('Bigtable/Cluster', function() { assert.strictEqual(cluster.id, CLUSTER_ID); }); - it('should leave full cluster names unaltered and localize the id from the name', - function() { - const cluster = new Cluster(INSTANCE, CLUSTER_NAME); - assert.strictEqual(cluster.name, CLUSTER_NAME); - assert.strictEqual(cluster.id, CLUSTER_ID); - }); + it('should leave full cluster names unaltered and localize the id from the name', function() { + const cluster = new Cluster(INSTANCE, CLUSTER_NAME); + assert.strictEqual(cluster.name, CLUSTER_NAME); + assert.strictEqual(cluster.id, CLUSTER_ID); + }); it('should throw if cluster id in wrong format', function() { const id = `clusters/${CLUSTER_ID}`; @@ -102,8 +101,9 @@ describe('Bigtable/Cluster', function() { it('should format the location name for project name with /', function() { const PROJECT_NAME = 'projects/grape-spaceship-123'; - const expected = - `projects/${PROJECT_NAME.split('/').pop()}/locations/${LOCATION}`; + const expected = `projects/${PROJECT_NAME.split( + '/' + ).pop()}/locations/${LOCATION}`; const formatted = Cluster.getLocation_(PROJECT_NAME, LOCATION); assert.strictEqual(formatted, expected); }); @@ -144,7 +144,7 @@ describe('Bigtable/Cluster', function() { cluster.instance.createCluster = function(id, options_, callback) { assert.strictEqual(id, cluster.id); assert.strictEqual(options_, options); - callback(); // done() + callback(); // done() }; cluster.create(options, done); @@ -153,7 +153,7 @@ describe('Bigtable/Cluster', function() { it('should not require options', function(done) { cluster.instance.createCluster = function(id, options, callback) { assert.deepStrictEqual(options, {}); - callback(); // done() + callback(); // done() }; cluster.create(done); @@ -172,7 +172,7 @@ describe('Bigtable/Cluster', function() { assert.deepStrictEqual(config.gaxOpts, {}); - callback(); // done() + callback(); // done() }; cluster.delete(done); @@ -365,7 +365,7 @@ describe('Bigtable/Cluster', function() { assert.strictEqual(config.client, 'BigtableInstanceAdminClient'); assert.strictEqual(config.method, 'updateCluster'); assert.strictEqual(config.reqOpts.name, CLUSTER_NAME); - callback(); // done() + callback(); // done() }; cluster.setMetadata({}, done); diff --git a/test/family.ts b/test/family.ts index 9d0864c7b..65350d495 100644 --- a/test/family.ts +++ b/test/family.ts @@ -76,16 +76,14 @@ describe('Bigtable/Family', function() { assert.strictEqual(family.name, FAMILY_NAME); }); - it('should leave full family names unaltered and localize the id from the name', - function() { - const family = new Family(TABLE, FAMILY_NAME); - assert.strictEqual(family.name, FAMILY_NAME); - assert.strictEqual(family.id, FAMILY_ID); - }); + it('should leave full family names unaltered and localize the id from the name', function() { + const family = new Family(TABLE, FAMILY_NAME); + assert.strictEqual(family.name, FAMILY_NAME); + assert.strictEqual(family.id, FAMILY_ID); + }); it('should throw if family id in wrong format', function() { - const id = `/project/bad-project/instances/bad-instance/columnFamiles/${ - FAMILY_ID}`; + const id = `/project/bad-project/instances/bad-instance/columnFamiles/${FAMILY_ID}`; assert.throws(function() { new Family(TABLE, id); }, Error); @@ -208,7 +206,7 @@ describe('Bigtable/Family', function() { family.table.createFamily = function(id, options_, callback) { assert.strictEqual(id, family.id); assert.strictEqual(options_, options); - callback(); // done() + callback(); // done() }; family.create(options, done); @@ -217,7 +215,7 @@ describe('Bigtable/Family', function() { it('should not require options', function(done) { family.table.createFamily = function(name, options, callback) { assert.deepStrictEqual(options, {}); - callback(); // done() + callback(); // done() }; family.create(done); @@ -242,7 +240,7 @@ describe('Bigtable/Family', function() { assert.deepStrictEqual(config.gaxOpts, {}); - callback(); // done() + callback(); // done() }; family.delete(done); @@ -610,7 +608,9 @@ describe('Bigtable/Family', function() { assert.strictEqual(err.code, 404); assert.strictEqual( - err.message, 'Column family not found: ' + FAMILY_NAME + '.'); + err.message, + 'Column family not found: ' + FAMILY_NAME + '.' + ); }); }); }); diff --git a/test/filter.ts b/test/filter.ts index 845013a35..2caae4be2 100644 --- a/test/filter.ts +++ b/test/filter.ts @@ -422,8 +422,9 @@ describe('Bigtable/Filter', function() { }; const convertedKey = 'abcd'; - const spy = - sinon.stub(Filter, 'convertToRegExpString').returns(convertedKey); + const spy = sinon + .stub(Filter, 'convertToRegExpString') + .returns(convertedKey); filter.set = function(filterName, value) { assert.strictEqual(filterName, 'rowKeyRegexFilter'); @@ -570,8 +571,9 @@ describe('Bigtable/Filter', function() { const fakeRegExValue = 'abcd'; const fakeConvertedValue = 'dcba'; - const regSpy = - sinon.stub(Filter, 'convertToRegExpString').returns(fakeRegExValue); + const regSpy = sinon + .stub(Filter, 'convertToRegExpString') + .returns(fakeRegExValue); const bytesSpy = (FakeMutation.convertToBytes = sinon.spy(function() { return fakeConvertedValue; @@ -595,8 +597,9 @@ describe('Bigtable/Filter', function() { const fakeRegExValue = 'abcd'; const fakeConvertedValue = 'dcba'; - const regSpy = - sinon.stub(Filter, 'convertToRegExpString').returns(fakeRegExValue); + const regSpy = sinon + .stub(Filter, 'convertToRegExpString') + .returns(fakeRegExValue); const bytesSpy = (FakeMutation.convertToBytes = sinon.spy(function() { return fakeConvertedValue; diff --git a/test/gapic-v2-admin.ts b/test/gapic-v2-admin.ts index 864e7f0e0..58ff0f397 100644 --- a/test/gapic-v2-admin.ts +++ b/test/gapic-v2-admin.ts @@ -49,21 +49,24 @@ describe('BigtableInstanceAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.createInstance = - mockLongRunningGrpcMethod(request, expectedResponse); - - client.createInstance(request) - .then(responses => { - const operation = responses[0]; - return operation.promise(); - }) - .then(responses => { - assert.deepStrictEqual(responses[0], expectedResponse); - done(); - }) - .catch(err => { - done(err); - }); + client._innerApiCalls.createInstance = mockLongRunningGrpcMethod( + request, + expectedResponse + ); + + client + .createInstance(request) + .then(responses => { + const operation = responses[0]; + return operation.promise(); + }) + .then(responses => { + assert.deepStrictEqual(responses[0], expectedResponse); + done(); + }) + .catch(err => { + done(err); + }); }); it('invokes createInstance with error', done => { @@ -85,22 +88,26 @@ describe('BigtableInstanceAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.createInstance = - mockLongRunningGrpcMethod(request, null, error); - - client.createInstance(request) - .then(responses => { - const operation = responses[0]; - return operation.promise(); - }) - .then(() => { - assert.fail(); - }) - .catch(err => { - assert(err instanceof Error); - assert.strictEqual(err.code, FAKE_STATUS_CODE); - done(); - }); + client._innerApiCalls.createInstance = mockLongRunningGrpcMethod( + request, + null, + error + ); + + client + .createInstance(request) + .then(responses => { + const operation = responses[0]; + return operation.promise(); + }) + .then(() => { + assert.fail(); + }) + .catch(err => { + assert(err instanceof Error); + assert.strictEqual(err.code, FAKE_STATUS_CODE); + done(); + }); }); it('has longrunning decoder functions', () => { @@ -109,13 +116,13 @@ describe('BigtableInstanceAdminClient', () => { projectId: 'bogus', }); assert( - client._descriptors.longrunning.createInstance - .responseDecoder instanceof - Function); + client._descriptors.longrunning.createInstance + .responseDecoder instanceof Function + ); assert( - client._descriptors.longrunning.createInstance - .metadataDecoder instanceof - Function); + client._descriptors.longrunning.createInstance + .metadataDecoder instanceof Function + ); }); }); @@ -141,8 +148,10 @@ describe('BigtableInstanceAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.getInstance = - mockSimpleGrpcMethod(request, expectedResponse); + client._innerApiCalls.getInstance = mockSimpleGrpcMethod( + request, + expectedResponse + ); client.getInstance(request, (err, response) => { assert.ifError(err); @@ -164,8 +173,11 @@ describe('BigtableInstanceAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.getInstance = - mockSimpleGrpcMethod(request, null, error); + client._innerApiCalls.getInstance = mockSimpleGrpcMethod( + request, + null, + error + ); client.getInstance(request, (err, response) => { assert(err instanceof Error); @@ -196,8 +208,10 @@ describe('BigtableInstanceAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.listInstances = - mockSimpleGrpcMethod(request, expectedResponse); + client._innerApiCalls.listInstances = mockSimpleGrpcMethod( + request, + expectedResponse + ); client.listInstances(request, (err, response) => { assert.ifError(err); @@ -219,8 +233,11 @@ describe('BigtableInstanceAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.listInstances = - mockSimpleGrpcMethod(request, null, error); + client._innerApiCalls.listInstances = mockSimpleGrpcMethod( + request, + null, + error + ); client.listInstances(request, (err, response) => { assert(err instanceof Error); @@ -259,8 +276,10 @@ describe('BigtableInstanceAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.updateInstance = - mockSimpleGrpcMethod(request, expectedResponse); + client._innerApiCalls.updateInstance = mockSimpleGrpcMethod( + request, + expectedResponse + ); client.updateInstance(request, (err, response) => { assert.ifError(err); @@ -288,8 +307,11 @@ describe('BigtableInstanceAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.updateInstance = - mockSimpleGrpcMethod(request, null, error); + client._innerApiCalls.updateInstance = mockSimpleGrpcMethod( + request, + null, + error + ); client.updateInstance(request, (err, response) => { assert(err instanceof Error); @@ -324,21 +346,24 @@ describe('BigtableInstanceAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.partialUpdateInstance = - mockLongRunningGrpcMethod(request, expectedResponse); - - client.partialUpdateInstance(request) - .then(responses => { - const operation = responses[0]; - return operation.promise(); - }) - .then(responses => { - assert.deepStrictEqual(responses[0], expectedResponse); - done(); - }) - .catch(err => { - done(err); - }); + client._innerApiCalls.partialUpdateInstance = mockLongRunningGrpcMethod( + request, + expectedResponse + ); + + client + .partialUpdateInstance(request) + .then(responses => { + const operation = responses[0]; + return operation.promise(); + }) + .then(responses => { + assert.deepStrictEqual(responses[0], expectedResponse); + done(); + }) + .catch(err => { + done(err); + }); }); it('invokes partialUpdateInstance with error', done => { @@ -356,22 +381,26 @@ describe('BigtableInstanceAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.partialUpdateInstance = - mockLongRunningGrpcMethod(request, null, error); - - client.partialUpdateInstance(request) - .then(responses => { - const operation = responses[0]; - return operation.promise(); - }) - .then(() => { - assert.fail(); - }) - .catch(err => { - assert(err instanceof Error); - assert.strictEqual(err.code, FAKE_STATUS_CODE); - done(); - }); + client._innerApiCalls.partialUpdateInstance = mockLongRunningGrpcMethod( + request, + null, + error + ); + + client + .partialUpdateInstance(request) + .then(responses => { + const operation = responses[0]; + return operation.promise(); + }) + .then(() => { + assert.fail(); + }) + .catch(err => { + assert(err instanceof Error); + assert.strictEqual(err.code, FAKE_STATUS_CODE); + done(); + }); }); it('has longrunning decoder functions', () => { @@ -380,13 +409,13 @@ describe('BigtableInstanceAdminClient', () => { projectId: 'bogus', }); assert( - client._descriptors.longrunning.partialUpdateInstance - .responseDecoder instanceof - Function); + client._descriptors.longrunning.partialUpdateInstance + .responseDecoder instanceof Function + ); assert( - client._descriptors.longrunning.partialUpdateInstance - .metadataDecoder instanceof - Function); + client._descriptors.longrunning.partialUpdateInstance + .metadataDecoder instanceof Function + ); }); }); @@ -425,8 +454,11 @@ describe('BigtableInstanceAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.deleteInstance = - mockSimpleGrpcMethod(request, null, error); + client._innerApiCalls.deleteInstance = mockSimpleGrpcMethod( + request, + null, + error + ); client.deleteInstance(request, err => { assert(err instanceof Error); @@ -464,21 +496,24 @@ describe('BigtableInstanceAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.createCluster = - mockLongRunningGrpcMethod(request, expectedResponse); - - client.createCluster(request) - .then(responses => { - const operation = responses[0]; - return operation.promise(); - }) - .then(responses => { - assert.deepStrictEqual(responses[0], expectedResponse); - done(); - }) - .catch(err => { - done(err); - }); + client._innerApiCalls.createCluster = mockLongRunningGrpcMethod( + request, + expectedResponse + ); + + client + .createCluster(request) + .then(responses => { + const operation = responses[0]; + return operation.promise(); + }) + .then(responses => { + assert.deepStrictEqual(responses[0], expectedResponse); + done(); + }) + .catch(err => { + done(err); + }); }); it('invokes createCluster with error', done => { @@ -498,22 +533,26 @@ describe('BigtableInstanceAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.createCluster = - mockLongRunningGrpcMethod(request, null, error); - - client.createCluster(request) - .then(responses => { - const operation = responses[0]; - return operation.promise(); - }) - .then(() => { - assert.fail(); - }) - .catch(err => { - assert(err instanceof Error); - assert.strictEqual(err.code, FAKE_STATUS_CODE); - done(); - }); + client._innerApiCalls.createCluster = mockLongRunningGrpcMethod( + request, + null, + error + ); + + client + .createCluster(request) + .then(responses => { + const operation = responses[0]; + return operation.promise(); + }) + .then(() => { + assert.fail(); + }) + .catch(err => { + assert(err instanceof Error); + assert.strictEqual(err.code, FAKE_STATUS_CODE); + done(); + }); }); it('has longrunning decoder functions', () => { @@ -522,13 +561,13 @@ describe('BigtableInstanceAdminClient', () => { projectId: 'bogus', }); assert( - client._descriptors.longrunning.createCluster - .responseDecoder instanceof - Function); + client._descriptors.longrunning.createCluster.responseDecoder instanceof + Function + ); assert( - client._descriptors.longrunning.createCluster - .metadataDecoder instanceof - Function); + client._descriptors.longrunning.createCluster.metadataDecoder instanceof + Function + ); }); }); @@ -540,8 +579,11 @@ describe('BigtableInstanceAdminClient', () => { }); // Mock request - const formattedName = - client.clusterPath('[PROJECT]', '[INSTANCE]', '[CLUSTER]'); + const formattedName = client.clusterPath( + '[PROJECT]', + '[INSTANCE]', + '[CLUSTER]' + ); const request = { name: formattedName, }; @@ -557,8 +599,10 @@ describe('BigtableInstanceAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.getCluster = - mockSimpleGrpcMethod(request, expectedResponse); + client._innerApiCalls.getCluster = mockSimpleGrpcMethod( + request, + expectedResponse + ); client.getCluster(request, (err, response) => { assert.ifError(err); @@ -574,15 +618,21 @@ describe('BigtableInstanceAdminClient', () => { }); // Mock request - const formattedName = - client.clusterPath('[PROJECT]', '[INSTANCE]', '[CLUSTER]'); + const formattedName = client.clusterPath( + '[PROJECT]', + '[INSTANCE]', + '[CLUSTER]' + ); const request = { name: formattedName, }; // Mock Grpc layer - client._innerApiCalls.getCluster = - mockSimpleGrpcMethod(request, null, error); + client._innerApiCalls.getCluster = mockSimpleGrpcMethod( + request, + null, + error + ); client.getCluster(request, (err, response) => { assert(err instanceof Error); @@ -613,8 +663,10 @@ describe('BigtableInstanceAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.listClusters = - mockSimpleGrpcMethod(request, expectedResponse); + client._innerApiCalls.listClusters = mockSimpleGrpcMethod( + request, + expectedResponse + ); client.listClusters(request, (err, response) => { assert.ifError(err); @@ -636,8 +688,11 @@ describe('BigtableInstanceAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.listClusters = - mockSimpleGrpcMethod(request, null, error); + client._innerApiCalls.listClusters = mockSimpleGrpcMethod( + request, + null, + error + ); client.listClusters(request, (err, response) => { assert(err instanceof Error); @@ -656,8 +711,11 @@ describe('BigtableInstanceAdminClient', () => { }); // Mock request - const formattedName = - client.clusterPath('[PROJECT]', '[INSTANCE]', '[CLUSTER]'); + const formattedName = client.clusterPath( + '[PROJECT]', + '[INSTANCE]', + '[CLUSTER]' + ); const location = 'location1901043637'; const serveNodes = 1288838783; const request = { @@ -677,21 +735,24 @@ describe('BigtableInstanceAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.updateCluster = - mockLongRunningGrpcMethod(request, expectedResponse); - - client.updateCluster(request) - .then(responses => { - const operation = responses[0]; - return operation.promise(); - }) - .then(responses => { - assert.deepStrictEqual(responses[0], expectedResponse); - done(); - }) - .catch(err => { - done(err); - }); + client._innerApiCalls.updateCluster = mockLongRunningGrpcMethod( + request, + expectedResponse + ); + + client + .updateCluster(request) + .then(responses => { + const operation = responses[0]; + return operation.promise(); + }) + .then(responses => { + assert.deepStrictEqual(responses[0], expectedResponse); + done(); + }) + .catch(err => { + done(err); + }); }); it('invokes updateCluster with error', done => { @@ -701,8 +762,11 @@ describe('BigtableInstanceAdminClient', () => { }); // Mock request - const formattedName = - client.clusterPath('[PROJECT]', '[INSTANCE]', '[CLUSTER]'); + const formattedName = client.clusterPath( + '[PROJECT]', + '[INSTANCE]', + '[CLUSTER]' + ); const location = 'location1901043637'; const serveNodes = 1288838783; const request = { @@ -712,22 +776,26 @@ describe('BigtableInstanceAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.updateCluster = - mockLongRunningGrpcMethod(request, null, error); - - client.updateCluster(request) - .then(responses => { - const operation = responses[0]; - return operation.promise(); - }) - .then(() => { - assert.fail(); - }) - .catch(err => { - assert(err instanceof Error); - assert.strictEqual(err.code, FAKE_STATUS_CODE); - done(); - }); + client._innerApiCalls.updateCluster = mockLongRunningGrpcMethod( + request, + null, + error + ); + + client + .updateCluster(request) + .then(responses => { + const operation = responses[0]; + return operation.promise(); + }) + .then(() => { + assert.fail(); + }) + .catch(err => { + assert(err instanceof Error); + assert.strictEqual(err.code, FAKE_STATUS_CODE); + done(); + }); }); it('has longrunning decoder functions', () => { @@ -736,13 +804,13 @@ describe('BigtableInstanceAdminClient', () => { projectId: 'bogus', }); assert( - client._descriptors.longrunning.updateCluster - .responseDecoder instanceof - Function); + client._descriptors.longrunning.updateCluster.responseDecoder instanceof + Function + ); assert( - client._descriptors.longrunning.updateCluster - .metadataDecoder instanceof - Function); + client._descriptors.longrunning.updateCluster.metadataDecoder instanceof + Function + ); }); }); @@ -754,8 +822,11 @@ describe('BigtableInstanceAdminClient', () => { }); // Mock request - const formattedName = - client.clusterPath('[PROJECT]', '[INSTANCE]', '[CLUSTER]'); + const formattedName = client.clusterPath( + '[PROJECT]', + '[INSTANCE]', + '[CLUSTER]' + ); const request = { name: formattedName, }; @@ -776,15 +847,21 @@ describe('BigtableInstanceAdminClient', () => { }); // Mock request - const formattedName = - client.clusterPath('[PROJECT]', '[INSTANCE]', '[CLUSTER]'); + const formattedName = client.clusterPath( + '[PROJECT]', + '[INSTANCE]', + '[CLUSTER]' + ); const request = { name: formattedName, }; // Mock Grpc layer - client._innerApiCalls.deleteCluster = - mockSimpleGrpcMethod(request, null, error); + client._innerApiCalls.deleteCluster = mockSimpleGrpcMethod( + request, + null, + error + ); client.deleteCluster(request, err => { assert(err instanceof Error); @@ -822,8 +899,10 @@ describe('BigtableInstanceAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.createAppProfile = - mockSimpleGrpcMethod(request, expectedResponse); + client._innerApiCalls.createAppProfile = mockSimpleGrpcMethod( + request, + expectedResponse + ); client.createAppProfile(request, (err, response) => { assert.ifError(err); @@ -849,8 +928,11 @@ describe('BigtableInstanceAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.createAppProfile = - mockSimpleGrpcMethod(request, null, error); + client._innerApiCalls.createAppProfile = mockSimpleGrpcMethod( + request, + null, + error + ); client.createAppProfile(request, (err, response) => { assert(err instanceof Error); @@ -869,8 +951,11 @@ describe('BigtableInstanceAdminClient', () => { }); // Mock request - const formattedName = - client.appProfilePath('[PROJECT]', '[INSTANCE]', '[APP_PROFILE]'); + const formattedName = client.appProfilePath( + '[PROJECT]', + '[INSTANCE]', + '[APP_PROFILE]' + ); const request = { name: formattedName, }; @@ -886,8 +971,10 @@ describe('BigtableInstanceAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.getAppProfile = - mockSimpleGrpcMethod(request, expectedResponse); + client._innerApiCalls.getAppProfile = mockSimpleGrpcMethod( + request, + expectedResponse + ); client.getAppProfile(request, (err, response) => { assert.ifError(err); @@ -903,15 +990,21 @@ describe('BigtableInstanceAdminClient', () => { }); // Mock request - const formattedName = - client.appProfilePath('[PROJECT]', '[INSTANCE]', '[APP_PROFILE]'); + const formattedName = client.appProfilePath( + '[PROJECT]', + '[INSTANCE]', + '[APP_PROFILE]' + ); const request = { name: formattedName, }; // Mock Grpc layer - client._innerApiCalls.getAppProfile = - mockSimpleGrpcMethod(request, null, error); + client._innerApiCalls.getAppProfile = mockSimpleGrpcMethod( + request, + null, + error + ); client.getAppProfile(request, (err, response) => { assert(err instanceof Error); @@ -945,11 +1038,14 @@ describe('BigtableInstanceAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.listAppProfiles = - (actualRequest, options, callback) => { - assert.deepStrictEqual(actualRequest, request); - callback(null, expectedResponse.appProfiles); - }; + client._innerApiCalls.listAppProfiles = ( + actualRequest, + options, + callback + ) => { + assert.deepStrictEqual(actualRequest, request); + callback(null, expectedResponse.appProfiles); + }; client.listAppProfiles(request, (err, response) => { assert.ifError(err); @@ -971,8 +1067,11 @@ describe('BigtableInstanceAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.listAppProfiles = - mockSimpleGrpcMethod(request, null, error); + client._innerApiCalls.listAppProfiles = mockSimpleGrpcMethod( + request, + null, + error + ); client.listAppProfiles(request, (err, response) => { assert(err instanceof Error); @@ -1009,21 +1108,24 @@ describe('BigtableInstanceAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.updateAppProfile = - mockLongRunningGrpcMethod(request, expectedResponse); - - client.updateAppProfile(request) - .then(responses => { - const operation = responses[0]; - return operation.promise(); - }) - .then(responses => { - assert.deepStrictEqual(responses[0], expectedResponse); - done(); - }) - .catch(err => { - done(err); - }); + client._innerApiCalls.updateAppProfile = mockLongRunningGrpcMethod( + request, + expectedResponse + ); + + client + .updateAppProfile(request) + .then(responses => { + const operation = responses[0]; + return operation.promise(); + }) + .then(responses => { + assert.deepStrictEqual(responses[0], expectedResponse); + done(); + }) + .catch(err => { + done(err); + }); }); it('invokes updateAppProfile with error', done => { @@ -1041,22 +1143,26 @@ describe('BigtableInstanceAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.updateAppProfile = - mockLongRunningGrpcMethod(request, null, error); - - client.updateAppProfile(request) - .then(responses => { - const operation = responses[0]; - return operation.promise(); - }) - .then(() => { - assert.fail(); - }) - .catch(err => { - assert(err instanceof Error); - assert.strictEqual(err.code, FAKE_STATUS_CODE); - done(); - }); + client._innerApiCalls.updateAppProfile = mockLongRunningGrpcMethod( + request, + null, + error + ); + + client + .updateAppProfile(request) + .then(responses => { + const operation = responses[0]; + return operation.promise(); + }) + .then(() => { + assert.fail(); + }) + .catch(err => { + assert(err instanceof Error); + assert.strictEqual(err.code, FAKE_STATUS_CODE); + done(); + }); }); it('has longrunning decoder functions', () => { @@ -1065,13 +1171,13 @@ describe('BigtableInstanceAdminClient', () => { projectId: 'bogus', }); assert( - client._descriptors.longrunning.updateAppProfile - .responseDecoder instanceof - Function); + client._descriptors.longrunning.updateAppProfile + .responseDecoder instanceof Function + ); assert( - client._descriptors.longrunning.updateAppProfile - .metadataDecoder instanceof - Function); + client._descriptors.longrunning.updateAppProfile + .metadataDecoder instanceof Function + ); }); }); @@ -1083,8 +1189,11 @@ describe('BigtableInstanceAdminClient', () => { }); // Mock request - const formattedName = - client.appProfilePath('[PROJECT]', '[INSTANCE]', '[APP_PROFILE]'); + const formattedName = client.appProfilePath( + '[PROJECT]', + '[INSTANCE]', + '[APP_PROFILE]' + ); const ignoreWarnings = true; const request = { name: formattedName, @@ -1107,8 +1216,11 @@ describe('BigtableInstanceAdminClient', () => { }); // Mock request - const formattedName = - client.appProfilePath('[PROJECT]', '[INSTANCE]', '[APP_PROFILE]'); + const formattedName = client.appProfilePath( + '[PROJECT]', + '[INSTANCE]', + '[APP_PROFILE]' + ); const ignoreWarnings = true; const request = { name: formattedName, @@ -1116,8 +1228,11 @@ describe('BigtableInstanceAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.deleteAppProfile = - mockSimpleGrpcMethod(request, null, error); + client._innerApiCalls.deleteAppProfile = mockSimpleGrpcMethod( + request, + null, + error + ); client.deleteAppProfile(request, err => { assert(err instanceof Error); @@ -1149,8 +1264,10 @@ describe('BigtableInstanceAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.getIamPolicy = - mockSimpleGrpcMethod(request, expectedResponse); + client._innerApiCalls.getIamPolicy = mockSimpleGrpcMethod( + request, + expectedResponse + ); client.getIamPolicy(request, (err, response) => { assert.ifError(err); @@ -1172,8 +1289,11 @@ describe('BigtableInstanceAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.getIamPolicy = - mockSimpleGrpcMethod(request, null, error); + client._innerApiCalls.getIamPolicy = mockSimpleGrpcMethod( + request, + null, + error + ); client.getIamPolicy(request, (err, response) => { assert(err instanceof Error); @@ -1208,8 +1328,10 @@ describe('BigtableInstanceAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.setIamPolicy = - mockSimpleGrpcMethod(request, expectedResponse); + client._innerApiCalls.setIamPolicy = mockSimpleGrpcMethod( + request, + expectedResponse + ); client.setIamPolicy(request, (err, response) => { assert.ifError(err); @@ -1233,8 +1355,11 @@ describe('BigtableInstanceAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.setIamPolicy = - mockSimpleGrpcMethod(request, null, error); + client._innerApiCalls.setIamPolicy = mockSimpleGrpcMethod( + request, + null, + error + ); client.setIamPolicy(request, (err, response) => { assert(err instanceof Error); @@ -1264,8 +1389,10 @@ describe('BigtableInstanceAdminClient', () => { const expectedResponse = {}; // Mock Grpc layer - client._innerApiCalls.testIamPermissions = - mockSimpleGrpcMethod(request, expectedResponse); + client._innerApiCalls.testIamPermissions = mockSimpleGrpcMethod( + request, + expectedResponse + ); client.testIamPermissions(request, (err, response) => { assert.ifError(err); @@ -1289,8 +1416,11 @@ describe('BigtableInstanceAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.testIamPermissions = - mockSimpleGrpcMethod(request, null, error); + client._innerApiCalls.testIamPermissions = mockSimpleGrpcMethod( + request, + null, + error + ); client.testIamPermissions(request, (err, response) => { assert(err instanceof Error); @@ -1326,8 +1456,10 @@ describe('BigtableTableAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.createTable = - mockSimpleGrpcMethod(request, expectedResponse); + client._innerApiCalls.createTable = mockSimpleGrpcMethod( + request, + expectedResponse + ); client.createTable(request, (err, response) => { assert.ifError(err); @@ -1353,8 +1485,11 @@ describe('BigtableTableAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.createTable = - mockSimpleGrpcMethod(request, null, error); + client._innerApiCalls.createTable = mockSimpleGrpcMethod( + request, + null, + error + ); client.createTable(request, (err, response) => { assert(err instanceof Error); @@ -1389,21 +1524,24 @@ describe('BigtableTableAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.createTableFromSnapshot = - mockLongRunningGrpcMethod(request, expectedResponse); - - client.createTableFromSnapshot(request) - .then(responses => { - const operation = responses[0]; - return operation.promise(); - }) - .then(responses => { - assert.deepStrictEqual(responses[0], expectedResponse); - done(); - }) - .catch(err => { - done(err); - }); + client._innerApiCalls.createTableFromSnapshot = mockLongRunningGrpcMethod( + request, + expectedResponse + ); + + client + .createTableFromSnapshot(request) + .then(responses => { + const operation = responses[0]; + return operation.promise(); + }) + .then(responses => { + assert.deepStrictEqual(responses[0], expectedResponse); + done(); + }) + .catch(err => { + done(err); + }); }); it('invokes createTableFromSnapshot with error', done => { @@ -1423,22 +1561,26 @@ describe('BigtableTableAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.createTableFromSnapshot = - mockLongRunningGrpcMethod(request, null, error); - - client.createTableFromSnapshot(request) - .then(responses => { - const operation = responses[0]; - return operation.promise(); - }) - .then(() => { - assert.fail(); - }) - .catch(err => { - assert(err instanceof Error); - assert.strictEqual(err.code, FAKE_STATUS_CODE); - done(); - }); + client._innerApiCalls.createTableFromSnapshot = mockLongRunningGrpcMethod( + request, + null, + error + ); + + client + .createTableFromSnapshot(request) + .then(responses => { + const operation = responses[0]; + return operation.promise(); + }) + .then(() => { + assert.fail(); + }) + .catch(err => { + assert(err instanceof Error); + assert.strictEqual(err.code, FAKE_STATUS_CODE); + done(); + }); }); it('has longrunning decoder functions', () => { @@ -1447,13 +1589,13 @@ describe('BigtableTableAdminClient', () => { projectId: 'bogus', }); assert( - client._descriptors.longrunning.createTableFromSnapshot - .responseDecoder instanceof - Function); + client._descriptors.longrunning.createTableFromSnapshot + .responseDecoder instanceof Function + ); assert( - client._descriptors.longrunning.createTableFromSnapshot - .metadataDecoder instanceof - Function); + client._descriptors.longrunning.createTableFromSnapshot + .metadataDecoder instanceof Function + ); }); }); @@ -1505,8 +1647,11 @@ describe('BigtableTableAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.listTables = - mockSimpleGrpcMethod(request, null, error); + client._innerApiCalls.listTables = mockSimpleGrpcMethod( + request, + null, + error + ); client.listTables(request, (err, response) => { assert(err instanceof Error); @@ -1525,8 +1670,11 @@ describe('BigtableTableAdminClient', () => { }); // Mock request - const formattedName = - client.tablePath('[PROJECT]', '[INSTANCE]', '[TABLE]'); + const formattedName = client.tablePath( + '[PROJECT]', + '[INSTANCE]', + '[TABLE]' + ); const request = { name: formattedName, }; @@ -1538,8 +1686,10 @@ describe('BigtableTableAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.getTable = - mockSimpleGrpcMethod(request, expectedResponse); + client._innerApiCalls.getTable = mockSimpleGrpcMethod( + request, + expectedResponse + ); client.getTable(request, (err, response) => { assert.ifError(err); @@ -1555,15 +1705,21 @@ describe('BigtableTableAdminClient', () => { }); // Mock request - const formattedName = - client.tablePath('[PROJECT]', '[INSTANCE]', '[TABLE]'); + const formattedName = client.tablePath( + '[PROJECT]', + '[INSTANCE]', + '[TABLE]' + ); const request = { name: formattedName, }; // Mock Grpc layer - client._innerApiCalls.getTable = - mockSimpleGrpcMethod(request, null, error); + client._innerApiCalls.getTable = mockSimpleGrpcMethod( + request, + null, + error + ); client.getTable(request, (err, response) => { assert(err instanceof Error); @@ -1582,8 +1738,11 @@ describe('BigtableTableAdminClient', () => { }); // Mock request - const formattedName = - client.tablePath('[PROJECT]', '[INSTANCE]', '[TABLE]'); + const formattedName = client.tablePath( + '[PROJECT]', + '[INSTANCE]', + '[TABLE]' + ); const request = { name: formattedName, }; @@ -1604,15 +1763,21 @@ describe('BigtableTableAdminClient', () => { }); // Mock request - const formattedName = - client.tablePath('[PROJECT]', '[INSTANCE]', '[TABLE]'); + const formattedName = client.tablePath( + '[PROJECT]', + '[INSTANCE]', + '[TABLE]' + ); const request = { name: formattedName, }; // Mock Grpc layer - client._innerApiCalls.deleteTable = - mockSimpleGrpcMethod(request, null, error); + client._innerApiCalls.deleteTable = mockSimpleGrpcMethod( + request, + null, + error + ); client.deleteTable(request, err => { assert(err instanceof Error); @@ -1630,8 +1795,11 @@ describe('BigtableTableAdminClient', () => { }); // Mock request - const formattedName = - client.tablePath('[PROJECT]', '[INSTANCE]', '[TABLE]'); + const formattedName = client.tablePath( + '[PROJECT]', + '[INSTANCE]', + '[TABLE]' + ); const modifications = []; const request = { name: formattedName, @@ -1645,8 +1813,10 @@ describe('BigtableTableAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.modifyColumnFamilies = - mockSimpleGrpcMethod(request, expectedResponse); + client._innerApiCalls.modifyColumnFamilies = mockSimpleGrpcMethod( + request, + expectedResponse + ); client.modifyColumnFamilies(request, (err, response) => { assert.ifError(err); @@ -1662,8 +1832,11 @@ describe('BigtableTableAdminClient', () => { }); // Mock request - const formattedName = - client.tablePath('[PROJECT]', '[INSTANCE]', '[TABLE]'); + const formattedName = client.tablePath( + '[PROJECT]', + '[INSTANCE]', + '[TABLE]' + ); const modifications = []; const request = { name: formattedName, @@ -1671,8 +1844,11 @@ describe('BigtableTableAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.modifyColumnFamilies = - mockSimpleGrpcMethod(request, null, error); + client._innerApiCalls.modifyColumnFamilies = mockSimpleGrpcMethod( + request, + null, + error + ); client.modifyColumnFamilies(request, (err, response) => { assert(err instanceof Error); @@ -1691,8 +1867,11 @@ describe('BigtableTableAdminClient', () => { }); // Mock request - const formattedName = - client.tablePath('[PROJECT]', '[INSTANCE]', '[TABLE]'); + const formattedName = client.tablePath( + '[PROJECT]', + '[INSTANCE]', + '[TABLE]' + ); const request = { name: formattedName, }; @@ -1713,15 +1892,21 @@ describe('BigtableTableAdminClient', () => { }); // Mock request - const formattedName = - client.tablePath('[PROJECT]', '[INSTANCE]', '[TABLE]'); + const formattedName = client.tablePath( + '[PROJECT]', + '[INSTANCE]', + '[TABLE]' + ); const request = { name: formattedName, }; // Mock Grpc layer - client._innerApiCalls.dropRowRange = - mockSimpleGrpcMethod(request, null, error); + client._innerApiCalls.dropRowRange = mockSimpleGrpcMethod( + request, + null, + error + ); client.dropRowRange(request, err => { assert(err instanceof Error); @@ -1739,8 +1924,11 @@ describe('BigtableTableAdminClient', () => { }); // Mock request - const formattedName = - client.tablePath('[PROJECT]', '[INSTANCE]', '[TABLE]'); + const formattedName = client.tablePath( + '[PROJECT]', + '[INSTANCE]', + '[TABLE]' + ); const request = { name: formattedName, }; @@ -1752,8 +1940,10 @@ describe('BigtableTableAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.generateConsistencyToken = - mockSimpleGrpcMethod(request, expectedResponse); + client._innerApiCalls.generateConsistencyToken = mockSimpleGrpcMethod( + request, + expectedResponse + ); client.generateConsistencyToken(request, (err, response) => { assert.ifError(err); @@ -1769,15 +1959,21 @@ describe('BigtableTableAdminClient', () => { }); // Mock request - const formattedName = - client.tablePath('[PROJECT]', '[INSTANCE]', '[TABLE]'); + const formattedName = client.tablePath( + '[PROJECT]', + '[INSTANCE]', + '[TABLE]' + ); const request = { name: formattedName, }; // Mock Grpc layer - client._innerApiCalls.generateConsistencyToken = - mockSimpleGrpcMethod(request, null, error); + client._innerApiCalls.generateConsistencyToken = mockSimpleGrpcMethod( + request, + null, + error + ); client.generateConsistencyToken(request, (err, response) => { assert(err instanceof Error); @@ -1796,8 +1992,11 @@ describe('BigtableTableAdminClient', () => { }); // Mock request - const formattedName = - client.tablePath('[PROJECT]', '[INSTANCE]', '[TABLE]'); + const formattedName = client.tablePath( + '[PROJECT]', + '[INSTANCE]', + '[TABLE]' + ); const consistencyToken = 'consistencyToken-1090516718'; const request = { name: formattedName, @@ -1811,8 +2010,10 @@ describe('BigtableTableAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.checkConsistency = - mockSimpleGrpcMethod(request, expectedResponse); + client._innerApiCalls.checkConsistency = mockSimpleGrpcMethod( + request, + expectedResponse + ); client.checkConsistency(request, (err, response) => { assert.ifError(err); @@ -1828,8 +2029,11 @@ describe('BigtableTableAdminClient', () => { }); // Mock request - const formattedName = - client.tablePath('[PROJECT]', '[INSTANCE]', '[TABLE]'); + const formattedName = client.tablePath( + '[PROJECT]', + '[INSTANCE]', + '[TABLE]' + ); const consistencyToken = 'consistencyToken-1090516718'; const request = { name: formattedName, @@ -1837,8 +2041,11 @@ describe('BigtableTableAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.checkConsistency = - mockSimpleGrpcMethod(request, null, error); + client._innerApiCalls.checkConsistency = mockSimpleGrpcMethod( + request, + null, + error + ); client.checkConsistency(request, (err, response) => { assert(err instanceof Error); @@ -1857,8 +2064,11 @@ describe('BigtableTableAdminClient', () => { }); // Mock request - const formattedName = - client.tablePath('[PROJECT]', '[INSTANCE]', '[TABLE]'); + const formattedName = client.tablePath( + '[PROJECT]', + '[INSTANCE]', + '[TABLE]' + ); const cluster = 'cluster872092154'; const snapshotId = 'snapshotId-168585866'; const description = 'description-1724546052'; @@ -1880,21 +2090,24 @@ describe('BigtableTableAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.snapshotTable = - mockLongRunningGrpcMethod(request, expectedResponse); - - client.snapshotTable(request) - .then(responses => { - const operation = responses[0]; - return operation.promise(); - }) - .then(responses => { - assert.deepStrictEqual(responses[0], expectedResponse); - done(); - }) - .catch(err => { - done(err); - }); + client._innerApiCalls.snapshotTable = mockLongRunningGrpcMethod( + request, + expectedResponse + ); + + client + .snapshotTable(request) + .then(responses => { + const operation = responses[0]; + return operation.promise(); + }) + .then(responses => { + assert.deepStrictEqual(responses[0], expectedResponse); + done(); + }) + .catch(err => { + done(err); + }); }); it('invokes snapshotTable with error', done => { @@ -1904,8 +2117,11 @@ describe('BigtableTableAdminClient', () => { }); // Mock request - const formattedName = - client.tablePath('[PROJECT]', '[INSTANCE]', '[TABLE]'); + const formattedName = client.tablePath( + '[PROJECT]', + '[INSTANCE]', + '[TABLE]' + ); const cluster = 'cluster872092154'; const snapshotId = 'snapshotId-168585866'; const description = 'description-1724546052'; @@ -1917,22 +2133,26 @@ describe('BigtableTableAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.snapshotTable = - mockLongRunningGrpcMethod(request, null, error); - - client.snapshotTable(request) - .then(responses => { - const operation = responses[0]; - return operation.promise(); - }) - .then(() => { - assert.fail(); - }) - .catch(err => { - assert(err instanceof Error); - assert.strictEqual(err.code, FAKE_STATUS_CODE); - done(); - }); + client._innerApiCalls.snapshotTable = mockLongRunningGrpcMethod( + request, + null, + error + ); + + client + .snapshotTable(request) + .then(responses => { + const operation = responses[0]; + return operation.promise(); + }) + .then(() => { + assert.fail(); + }) + .catch(err => { + assert(err instanceof Error); + assert.strictEqual(err.code, FAKE_STATUS_CODE); + done(); + }); }); it('has longrunning decoder functions', () => { @@ -1941,13 +2161,13 @@ describe('BigtableTableAdminClient', () => { projectId: 'bogus', }); assert( - client._descriptors.longrunning.snapshotTable - .responseDecoder instanceof - Function); + client._descriptors.longrunning.snapshotTable.responseDecoder instanceof + Function + ); assert( - client._descriptors.longrunning.snapshotTable - .metadataDecoder instanceof - Function); + client._descriptors.longrunning.snapshotTable.metadataDecoder instanceof + Function + ); }); }); @@ -1960,7 +2180,11 @@ describe('BigtableTableAdminClient', () => { // Mock request const formattedName = client.snapshotPath( - '[PROJECT]', '[INSTANCE]', '[CLUSTER]', '[SNAPSHOT]'); + '[PROJECT]', + '[INSTANCE]', + '[CLUSTER]', + '[SNAPSHOT]' + ); const request = { name: formattedName, }; @@ -1976,8 +2200,10 @@ describe('BigtableTableAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.getSnapshot = - mockSimpleGrpcMethod(request, expectedResponse); + client._innerApiCalls.getSnapshot = mockSimpleGrpcMethod( + request, + expectedResponse + ); client.getSnapshot(request, (err, response) => { assert.ifError(err); @@ -1994,14 +2220,21 @@ describe('BigtableTableAdminClient', () => { // Mock request const formattedName = client.snapshotPath( - '[PROJECT]', '[INSTANCE]', '[CLUSTER]', '[SNAPSHOT]'); + '[PROJECT]', + '[INSTANCE]', + '[CLUSTER]', + '[SNAPSHOT]' + ); const request = { name: formattedName, }; // Mock Grpc layer - client._innerApiCalls.getSnapshot = - mockSimpleGrpcMethod(request, null, error); + client._innerApiCalls.getSnapshot = mockSimpleGrpcMethod( + request, + null, + error + ); client.getSnapshot(request, (err, response) => { assert(err instanceof Error); @@ -2020,8 +2253,11 @@ describe('BigtableTableAdminClient', () => { }); // Mock request - const formattedParent = - client.clusterPath('[PROJECT]', '[INSTANCE]', '[CLUSTER]'); + const formattedParent = client.clusterPath( + '[PROJECT]', + '[INSTANCE]', + '[CLUSTER]' + ); const request = { parent: formattedParent, }; @@ -2036,11 +2272,14 @@ describe('BigtableTableAdminClient', () => { }; // Mock Grpc layer - client._innerApiCalls.listSnapshots = - (actualRequest, options, callback) => { - assert.deepStrictEqual(actualRequest, request); - callback(null, expectedResponse.snapshots); - }; + client._innerApiCalls.listSnapshots = ( + actualRequest, + options, + callback + ) => { + assert.deepStrictEqual(actualRequest, request); + callback(null, expectedResponse.snapshots); + }; client.listSnapshots(request, (err, response) => { assert.ifError(err); @@ -2056,15 +2295,21 @@ describe('BigtableTableAdminClient', () => { }); // Mock request - const formattedParent = - client.clusterPath('[PROJECT]', '[INSTANCE]', '[CLUSTER]'); + const formattedParent = client.clusterPath( + '[PROJECT]', + '[INSTANCE]', + '[CLUSTER]' + ); const request = { parent: formattedParent, }; // Mock Grpc layer - client._innerApiCalls.listSnapshots = - mockSimpleGrpcMethod(request, null, error); + client._innerApiCalls.listSnapshots = mockSimpleGrpcMethod( + request, + null, + error + ); client.listSnapshots(request, (err, response) => { assert(err instanceof Error); @@ -2084,7 +2329,11 @@ describe('BigtableTableAdminClient', () => { // Mock request const formattedName = client.snapshotPath( - '[PROJECT]', '[INSTANCE]', '[CLUSTER]', '[SNAPSHOT]'); + '[PROJECT]', + '[INSTANCE]', + '[CLUSTER]', + '[SNAPSHOT]' + ); const request = { name: formattedName, }; @@ -2106,14 +2355,21 @@ describe('BigtableTableAdminClient', () => { // Mock request const formattedName = client.snapshotPath( - '[PROJECT]', '[INSTANCE]', '[CLUSTER]', '[SNAPSHOT]'); + '[PROJECT]', + '[INSTANCE]', + '[CLUSTER]', + '[SNAPSHOT]' + ); const request = { name: formattedName, }; // Mock Grpc layer - client._innerApiCalls.deleteSnapshot = - mockSimpleGrpcMethod(request, null, error); + client._innerApiCalls.deleteSnapshot = mockSimpleGrpcMethod( + request, + null, + error + ); client.deleteSnapshot(request, err => { assert(err instanceof Error); diff --git a/test/index.ts b/test/index.ts index bfde71ff5..1c7ff65d5 100644 --- a/test/index.ts +++ b/test/index.ts @@ -65,8 +65,10 @@ function fakeGoogleAuth() { let retryRequestOverride; function fakeRetryRequest() { - return (retryRequestOverride || require('retry-request')) - .apply(null, arguments); + return (retryRequestOverride || require('retry-request')).apply( + null, + arguments + ); } function createFake(Class) { @@ -152,16 +154,18 @@ describe('Bigtable', function() { googleAuthOverride = function(options_) { assert.deepStrictEqual( - options_, - Object.assign( - { - libName: 'gccl', - libVersion: PKG.version, - scopes: EXPECTED_SCOPES, - 'grpc.max_send_message_length': -1, - 'grpc.max_receive_message_length': -1, - }, - options)); + options_, + Object.assign( + { + libName: 'gccl', + libVersion: PKG.version, + scopes: EXPECTED_SCOPES, + 'grpc.max_send_message_length': -1, + 'grpc.max_receive_message_length': -1, + }, + options + ) + ); return fakeGoogleAuthInstance; }; @@ -192,26 +196,29 @@ describe('Bigtable', function() { assert.deepStrictEqual(bigtable.options, { BigtableClient: Object.assign( - { - servicePath: 'bigtable.googleapis.com', - port: 443, - sslCreds: undefined, - }, - defaultOptions), + { + servicePath: 'bigtable.googleapis.com', + port: 443, + sslCreds: undefined, + }, + defaultOptions + ), BigtableInstanceAdminClient: Object.assign( - { - servicePath: 'bigtableadmin.googleapis.com', - port: 443, - sslCreds: undefined, - }, - defaultOptions), + { + servicePath: 'bigtableadmin.googleapis.com', + port: 443, + sslCreds: undefined, + }, + defaultOptions + ), BigtableTableAdminClient: Object.assign( - { - servicePath: 'bigtableadmin.googleapis.com', - port: 443, - sslCreds: undefined, - }, - defaultOptions), + { + servicePath: 'bigtableadmin.googleapis.com', + port: 443, + sslCreds: undefined, + }, + defaultOptions + ), }); }); @@ -231,30 +238,35 @@ describe('Bigtable', function() { const bigtable = new Bigtable(options); assert.strictEqual( - bigtable.customEndpoint, process.env.BIGTABLE_EMULATOR_HOST); + bigtable.customEndpoint, + process.env.BIGTABLE_EMULATOR_HOST + ); assert.deepStrictEqual(bigtable.options, { BigtableClient: Object.assign( - { - servicePath: 'override', - port: 8080, - sslCreds: grpc.credentials.createInsecure(), - }, - options), + { + servicePath: 'override', + port: 8080, + sslCreds: grpc.credentials.createInsecure(), + }, + options + ), BigtableInstanceAdminClient: Object.assign( - { - servicePath: 'override', - port: 8080, - sslCreds: grpc.credentials.createInsecure(), - }, - options), + { + servicePath: 'override', + port: 8080, + sslCreds: grpc.credentials.createInsecure(), + }, + options + ), BigtableTableAdminClient: Object.assign( - { - servicePath: 'override', - port: 8080, - sslCreds: grpc.credentials.createInsecure(), - }, - options), + { + servicePath: 'override', + port: 8080, + sslCreds: grpc.credentials.createInsecure(), + }, + options + ), }); }); @@ -276,26 +288,29 @@ describe('Bigtable', function() { assert.deepStrictEqual(bigtable.options, { BigtableClient: Object.assign( - { - servicePath: 'customEndpoint', - port: 9090, - sslCreds: grpc.credentials.createInsecure(), - }, - options), + { + servicePath: 'customEndpoint', + port: 9090, + sslCreds: grpc.credentials.createInsecure(), + }, + options + ), BigtableInstanceAdminClient: Object.assign( - { - servicePath: 'customEndpoint', - port: 9090, - sslCreds: grpc.credentials.createInsecure(), - }, - options), + { + servicePath: 'customEndpoint', + port: 9090, + sslCreds: grpc.credentials.createInsecure(), + }, + options + ), BigtableTableAdminClient: Object.assign( - { - servicePath: 'customEndpoint', - port: 9090, - sslCreds: grpc.credentials.createInsecure(), - }, - options), + { + servicePath: 'customEndpoint', + port: 9090, + sslCreds: grpc.credentials.createInsecure(), + }, + options + ), }); }); @@ -357,7 +372,9 @@ describe('Bigtable', function() { bigtable.request = function(config) { assert.strictEqual( - config.reqOpts.instance.displayName, options.displayName); + config.reqOpts.instance.displayName, + options.displayName + ); done(); }; @@ -690,28 +707,27 @@ describe('Bigtable', function() { bigtable.request(CONFIG, assert.ifError); }); - it('should not replace token when project ID not detected', - function(done) { - replaceProjectIdTokenOverride = function() { - throw new Error('Should not have tried to replace token.'); - }; + it('should not replace token when project ID not detected', function(done) { + replaceProjectIdTokenOverride = function() { + throw new Error('Should not have tried to replace token.'); + }; - bigtable.getProjectId_ = function(callback) { - callback(null, PROJECT_ID_TOKEN); - }; + bigtable.getProjectId_ = function(callback) { + callback(null, PROJECT_ID_TOKEN); + }; - bigtable.api[CONFIG.client][CONFIG.method] = { - bind(gaxClient, reqOpts) { - assert.deepStrictEqual(reqOpts, CONFIG.reqOpts); + bigtable.api[CONFIG.client][CONFIG.method] = { + bind(gaxClient, reqOpts) { + assert.deepStrictEqual(reqOpts, CONFIG.reqOpts); - setImmediate(done); + setImmediate(done); - return common.util.noop; - }, - }; + return common.util.noop; + }, + }; - bigtable.request(CONFIG, assert.ifError); - }); + bigtable.request(CONFIG, assert.ifError); + }); }); describe('makeRequestCallback', function() { @@ -748,7 +764,7 @@ describe('Bigtable', function() { it('should execute the request function', function() { bigtable.api[CONFIG.client][CONFIG.method] = function(done) { const callback: Function = [].slice.call(arguments).pop()!; - callback(null, done); // so it ends the test + callback(null, done); // so it ends the test }; bigtable.request(CONFIG, assert.ifError); @@ -775,8 +791,9 @@ describe('Bigtable', function() { assert.strictEqual(config.currentRetryAttempt, 0); assert.strictEqual(config.objectMode, true); assert.strictEqual( - config.shouldRetryFn, - (common.Service as any).shouldRetryRequest_); + config.shouldRetryFn, + (common.Service as any).shouldRetryRequest_ + ); done(); }; diff --git a/test/instance.ts b/test/instance.ts index 9b3d9a88c..7d1cf4234 100644 --- a/test/instance.ts +++ b/test/instance.ts @@ -89,13 +89,13 @@ describe('Bigtable/Instance', function() { before(function() { Instance = proxyquire('../src/instance.js', { - '@google-cloud/paginator': fakePaginator, - '@google-cloud/promisify': fakePromisify, - './app-profile.js': {AppProfile: FakeAppProfile}, - './cluster.js': {Cluster: FakeCluster}, - './family.js': {Family: FakeFamily}, - './table.js': {Table: FakeTable}, - }).Instance; + '@google-cloud/paginator': fakePaginator, + '@google-cloud/promisify': fakePromisify, + './app-profile.js': {AppProfile: FakeAppProfile}, + './cluster.js': {Cluster: FakeCluster}, + './family.js': {Family: FakeFamily}, + './table.js': {Table: FakeTable}, + }).Instance; }); beforeEach(function() { @@ -129,12 +129,11 @@ describe('Bigtable/Instance', function() { assert.strictEqual(instance.id, INSTANCE_ID); }); - it('should leave full instance name unaltered and localize the id from the name', - function() { - const instance = new Instance(BIGTABLE, INSTANCE_NAME); - assert.strictEqual(instance.name, INSTANCE_NAME); - assert.strictEqual(instance.id, INSTANCE_ID); - }); + it('should leave full instance name unaltered and localize the id from the name', function() { + const instance = new Instance(BIGTABLE, INSTANCE_NAME); + assert.strictEqual(instance.name, INSTANCE_NAME); + assert.strictEqual(instance.id, INSTANCE_ID); + }); it('should throw if instance id in wrong format', function() { const id = `instances/${INSTANCE_ID}`; @@ -154,7 +153,9 @@ describe('Bigtable/Instance', function() { it('should default to unspecified', function() { assert.strictEqual(Instance.getTypeType_(), types.unspecified); assert.strictEqual( - Instance.getTypeType_('not-real-type'), types.unspecified); + Instance.getTypeType_('not-real-type'), + types.unspecified + ); }); it('should lowercase a type', function() { @@ -175,7 +176,7 @@ describe('Bigtable/Instance', function() { instance.bigtable.createInstance = function(id, options_, callback) { assert.strictEqual(id, instance.id); assert.strictEqual(options_, options); - callback(); // done() + callback(); // done() }; instance.create(options, done); @@ -184,7 +185,7 @@ describe('Bigtable/Instance', function() { it('should not require options', function(done) { instance.bigtable.createInstance = function(id, options, callback) { assert.deepStrictEqual(options, {}); - callback(); // done() + callback(); // done() }; instance.create(done); @@ -206,13 +207,17 @@ describe('Bigtable/Instance', function() { }; instance.createAppProfile( - APP_PROFILE_ID, {routing: 'any'}, assert.ifError); + APP_PROFILE_ID, + {routing: 'any'}, + assert.ifError + ); }); it('should throw if the routing option is not provided', function() { - assert.throws(instance.createAppProfile.bind( - null, APP_PROFILE_ID, assert.ifError)), - /An app profile must contain a routing policy\./; + assert.throws( + instance.createAppProfile.bind(null, APP_PROFILE_ID, assert.ifError) + ), + /An app profile must contain a routing policy\./; }); it('should accept gaxOptions', function(done) { @@ -239,7 +244,9 @@ describe('Bigtable/Instance', function() { instance.bigtable.request = function(config) { assert.deepStrictEqual( - config.reqOpts.appProfile.multiClusterRoutingUseAny, {}); + config.reqOpts.appProfile.multiClusterRoutingUseAny, + {} + ); done(); }; @@ -251,8 +258,9 @@ describe('Bigtable/Instance', function() { instance.bigtable.request = function(config) { assert.deepStrictEqual( - config.reqOpts.appProfile.singleClusterRouting, - {clusterId: CLUSTER_ID}); + config.reqOpts.appProfile.singleClusterRouting, + {clusterId: CLUSTER_ID} + ); done(); }; @@ -269,9 +277,10 @@ describe('Bigtable/Instance', function() { instance.bigtable.request = function(config) { assert.deepStrictEqual( - config.reqOpts.appProfile.singleClusterRouting - .allowTransactionalWrites, - true); + config.reqOpts.appProfile.singleClusterRouting + .allowTransactionalWrites, + true + ); done(); }; @@ -286,7 +295,9 @@ describe('Bigtable/Instance', function() { instance.bigtable.request = function(config) { assert.deepStrictEqual( - config.reqOpts.appProfile.description, options.description); + config.reqOpts.appProfile.description, + options.description + ); done(); }; @@ -321,14 +332,16 @@ describe('Bigtable/Instance', function() { return fakeAppProfile; }; - instance.createAppProfile( - APP_PROFILE_ID, {routing: 'any'}, - function(err, appProfile, apiResponse) { - assert.ifError(err); - assert.strictEqual(arguments[1], fakeAppProfile); - assert.strictEqual(apiResponse, response); - done(); - }); + instance.createAppProfile(APP_PROFILE_ID, {routing: 'any'}, function( + err, + appProfile, + apiResponse + ) { + assert.ifError(err); + assert.strictEqual(arguments[1], fakeAppProfile); + assert.strictEqual(apiResponse, response); + done(); + }); }); }); @@ -410,7 +423,9 @@ describe('Bigtable/Instance', function() { instance.bigtable.request = function(config) { assert.strictEqual( - config.reqOpts.cluster.defaultStorageType, fakeStorageType); + config.reqOpts.cluster.defaultStorageType, + fakeStorageType + ); done(); }; @@ -443,7 +458,7 @@ describe('Bigtable/Instance', function() { describe('createTable', function() { const TABLE_ID = 'my-table'; const TABLE_NAME = - 'projects/my-project/instances/my-instance/tables/my-table'; + 'projects/my-project/instances/my-instance/tables/my-table'; it('should throw if an id is not provided', function() { assert.throws(function() { @@ -595,7 +610,7 @@ describe('Bigtable/Instance', function() { assert.deepStrictEqual(config.gaxOpts, {}); - callback(); // done() + callback(); // done() }; instance.delete(done); @@ -1026,7 +1041,7 @@ describe('Bigtable/Instance', function() { assert.strictEqual(config.client, 'BigtableInstanceAdminClient'); assert.strictEqual(config.method, 'partialUpdateInstance'); assert.deepStrictEqual(config.reqOpts, expectedMetadata); - callback(); // done() + callback(); // done() }; instance.setMetadata(metadata, done); diff --git a/test/mutation.ts b/test/mutation.ts index 2b82910ba..df5d993ec 100644 --- a/test/mutation.ts +++ b/test/mutation.ts @@ -47,8 +47,9 @@ describe('Bigtable/Mutation', function() { describe('isPossibleNumber', function() { it('should convert a base64 encoded number when true', function() { const num = 10; - const encoded = - Buffer.from(Long.fromNumber(num).toBytesBE()).toString('base64'); + const encoded = Buffer.from(Long.fromNumber(num).toBytesBE()).toString( + 'base64' + ); const decoded = Mutation.convertFromBytes(encoded, { isPossibleNumber: true, }); @@ -57,8 +58,9 @@ describe('Bigtable/Mutation', function() { }); it('should not convert a base64 encoded number when false', function() { const num = 10; - const encoded = - Buffer.from(Long.fromNumber(num).toBytesBE()).toString('base64'); + const encoded = Buffer.from(Long.fromNumber(num).toBytesBE()).toString( + 'base64' + ); const decoded = Mutation.convertFromBytes(encoded); assert.notStrictEqual(num, decoded); @@ -180,7 +182,7 @@ describe('Bigtable/Mutation', function() { setCell: { familyName: 'follows', columnQualifier: 'gwashington', - timestampMicros: fakeTime * 1000, // Convert ms to μs + timestampMicros: fakeTime * 1000, // Convert ms to μs value: 1, }, }, @@ -188,7 +190,7 @@ describe('Bigtable/Mutation', function() { setCell: { familyName: 'follows', columnQualifier: 'alincoln', - timestampMicros: fakeTime * 1000, // Convert ms to μs + timestampMicros: fakeTime * 1000, // Convert ms to μs value: 1, }, }, @@ -215,7 +217,7 @@ describe('Bigtable/Mutation', function() { setCell: { familyName: 'follows', columnQualifier: 'gwashington', - timestampMicros: realTimestamp * 1000, // Convert ms to μs + timestampMicros: realTimestamp * 1000, // Convert ms to μs value: 1, }, }, @@ -240,7 +242,7 @@ describe('Bigtable/Mutation', function() { setCell: { familyName: 'follows', columnQualifier: 'gwashington', - timestampMicros: fakeTime * 1000, // Convert ms to μs + timestampMicros: fakeTime * 1000, // Convert ms to μs value: val, }, }, diff --git a/test/row.ts b/test/row.ts index dc4dea289..08cac5d0d 100644 --- a/test/row.ts +++ b/test/row.ts @@ -664,7 +664,7 @@ describe('Bigtable/Row', function() { assert.strictEqual(spy.getCall(1).args[0], 'c'); assert.strictEqual(spy.getCall(2).args[0], ROW_ID); - callback(); // done() + callback(); // done() }; row.createRules(rules, done); @@ -676,7 +676,9 @@ describe('Bigtable/Row', function() { bigtableInstance.request = function(config) { assert.strictEqual( - config.reqOpts.appProfileId, bigtableInstance.appProfileId); + config.reqOpts.appProfileId, + bigtableInstance.appProfileId + ); done(); }; @@ -701,7 +703,7 @@ describe('Bigtable/Row', function() { assert.strictEqual(mutation.key, ROW_ID); assert.strictEqual(mutation.method, FakeMutation.methods.DELETE); assert.deepStrictEqual(gaxOptions, {}); - callback(); // done() + callback(); // done() }; row.delete(done); @@ -739,7 +741,7 @@ describe('Bigtable/Row', function() { assert.strictEqual(mutation.data, columns); assert.strictEqual(mutation.method, FakeMutation.methods.DELETE); assert.deepStrictEqual(gaxOptions, {}); - callback(); // done() + callback(); // done() }; row.deleteCells(columns, done); @@ -758,7 +760,7 @@ describe('Bigtable/Row', function() { it('should remove existing data', function(done) { row.table.mutate = function(mutation, gaxOptions, callback) { - callback(); // done() + callback(); // done() }; row.deleteCells(columns, done); @@ -873,11 +875,17 @@ describe('Bigtable/Row', function() { assert.strictEqual(config.reqOpts.tableName, TABLE.name); assert.strictEqual(config.reqOpts.rowKey, CONVERTED_ROW_ID); assert.deepStrictEqual( - config.reqOpts.predicateFilter, fakeParsedFilter); + config.reqOpts.predicateFilter, + fakeParsedFilter + ); assert.deepStrictEqual( - config.reqOpts.trueMutations, fakeMutations.mutations); + config.reqOpts.trueMutations, + fakeMutations.mutations + ); assert.deepStrictEqual( - config.reqOpts.falseMutations, fakeMutations.mutations); + config.reqOpts.falseMutations, + fakeMutations.mutations + ); assert.strictEqual(config.gaxOpts, undefined); @@ -892,11 +900,13 @@ describe('Bigtable/Row', function() { }; row.filter( - filter, { - onMatch: mutations, - onNoMatch: mutations, - }, - assert.ifError); + filter, + { + onMatch: mutations, + onNoMatch: mutations, + }, + assert.ifError + ); }); it('should accept gaxOptions', function(done) { @@ -923,7 +933,9 @@ describe('Bigtable/Row', function() { bigtableInstance.request = function(config) { assert.strictEqual( - config.reqOpts.appProfileId, bigtableInstance.appProfileId); + config.reqOpts.appProfileId, + bigtableInstance.appProfileId + ); done(); }; @@ -1096,58 +1108,57 @@ describe('Bigtable/Row', function() { row.get(keys, options, assert.ifError); }); - it('should respect the options object with filter for multiple columns', - function(done) { - const keys = ['a:b', 'c:d']; - - const options: any = { - filter: [ - { - column: { - cellLimit: 1, - }, - }, - ], - }; - - const expectedFilter = [ - { - interleave: [ - [ - { - family: 'a', - }, - { - column: 'b', - }, - ], - [ - { - family: 'c', - }, - { - column: 'd', - }, - ], - ], - }, - { - column: { - cellLimit: 1, - }, - }, - ]; - - row.table.getRows = function(reqOpts) { - assert.deepStrictEqual(reqOpts.filter, expectedFilter); - assert.strictEqual(FakeMutation.parseColumnName.callCount, 2); - assert(FakeMutation.parseColumnName.calledWith(keys[0])); - assert.strictEqual(reqOpts.decode, options.decode); - done(); - }; - - row.get(keys, options, assert.ifError); - }); + it('should respect the options object with filter for multiple columns', function(done) { + const keys = ['a:b', 'c:d']; + + const options: any = { + filter: [ + { + column: { + cellLimit: 1, + }, + }, + ], + }; + + const expectedFilter = [ + { + interleave: [ + [ + { + family: 'a', + }, + { + column: 'b', + }, + ], + [ + { + family: 'c', + }, + { + column: 'd', + }, + ], + ], + }, + { + column: { + cellLimit: 1, + }, + }, + ]; + + row.table.getRows = function(reqOpts) { + assert.deepStrictEqual(reqOpts.filter, expectedFilter); + assert.strictEqual(FakeMutation.parseColumnName.callCount, 2); + assert(FakeMutation.parseColumnName.calledWith(keys[0])); + assert.strictEqual(reqOpts.decode, options.decode); + done(); + }; + + row.get(keys, options, assert.ifError); + }); it('should respect filter in options object', function(done) { const keys = []; @@ -1439,7 +1450,7 @@ describe('Bigtable/Row', function() { it('should insert an object', function(done) { row.table.mutate = function(entry, gaxOptions, callback) { assert.strictEqual(entry.data, data); - callback(); // done() + callback(); // done() }; row.save(data, done); diff --git a/test/table.ts b/test/table.ts index d7e398cc6..34b1d0e08 100644 --- a/test/table.ts +++ b/test/table.ts @@ -14,7 +14,6 @@ * limitations under the License. */ - import * as common from '@google-cloud/common-grpc'; import * as promisify from '@google-cloud/promisify'; import * as assert from 'assert'; @@ -87,7 +86,7 @@ const FakeFilter = { }), createRange: (...args) => { return {}; - } + }, }; describe('Bigtable/Table', function() { @@ -100,17 +99,17 @@ describe('Bigtable/Table', function() { before(function() { Table = proxyquire('../src/table.js', { - '@google-cloud/common-grpc': { - Service: FakeGrpcService, - }, - '@google-cloud/promisify': fakePromisify, - './family.js': {Family: FakeFamily}, - './mutation.js': {Mutation: FakeMutation}, - './filter.js': {Filter: FakeFilter}, - pumpify, - './row.js': {Row: FakeRow}, - './chunktransformer.js': {ChunkTransformer: FakeChunkTransformer}, - }).Table; + '@google-cloud/common-grpc': { + Service: FakeGrpcService, + }, + '@google-cloud/promisify': fakePromisify, + './family.js': {Family: FakeFamily}, + './mutation.js': {Mutation: FakeMutation}, + './filter.js': {Filter: FakeFilter}, + pumpify, + './row.js': {Row: FakeRow}, + './chunktransformer.js': {ChunkTransformer: FakeChunkTransformer}, + }).Table; }); beforeEach(function() { @@ -153,12 +152,11 @@ describe('Bigtable/Table', function() { it('should localize table name', function() { assert.strictEqual(table.name, TABLE_NAME); }); - it('should leave full table name unaltered and localize the id from the name', - function() { - const table = new Table(INSTANCE, TABLE_NAME); - assert.strictEqual(table.name, TABLE_NAME); - assert.strictEqual(table.id, TABLE_ID); - }); + it('should leave full table name unaltered and localize the id from the name', function() { + const table = new Table(INSTANCE, TABLE_NAME); + assert.strictEqual(table.name, TABLE_NAME); + assert.strictEqual(table.id, TABLE_ID); + }); it('should throw if table id in wrong format', function() { const id = `tables/${TABLE_ID}`; @@ -201,7 +199,7 @@ describe('Bigtable/Table', function() { table.instance.createTable = function(id, options_, callback) { assert.strictEqual(id, table.id); assert.strictEqual(options_, options); - callback(); // done() + callback(); // done() }; table.create(options, done); @@ -210,7 +208,7 @@ describe('Bigtable/Table', function() { it('should not require options', function(done) { table.instance.createTable = function(id, options, callback) { assert.deepStrictEqual(options, {}); - callback(); // done() + callback(); // done() }; table.create(done); @@ -408,7 +406,9 @@ describe('Bigtable/Table', function() { bigtableInstance.request = function(config) { assert.strictEqual( - config.reqOpts.appProfileId, bigtableInstance.appProfileId); + config.reqOpts.appProfileId, + bigtableInstance.appProfileId + ); done(); }; @@ -462,11 +462,12 @@ describe('Bigtable/Table', function() { }; const convertedKeys = ['a', 'b']; - const convertSpy = - (FakeMutation.convertToBytes = sinon.spy(function(key) { - const keyIndex = options.keys.indexOf(key); - return convertedKeys[keyIndex]; - })); + const convertSpy = (FakeMutation.convertToBytes = sinon.spy(function( + key + ) { + const keyIndex = options.keys.indexOf(key); + return convertedKeys[keyIndex]; + })); table.bigtable.request = function(config) { assert.deepStrictEqual(config.reqOpts.rows.rowKeys, convertedKeys); @@ -542,7 +543,9 @@ describe('Bigtable/Table', function() { assert.strictEqual(config.reqOpts.filter, fakeFilter); assert.strictEqual(parseSpy.callCount, 1); assert.strictEqual( - (parseSpy as any).getCall(0).args[0], options.filter); + (parseSpy as any).getCall(0).args[0], + options.filter + ); done(); }; @@ -659,18 +662,20 @@ describe('Bigtable/Table', function() { it('should transform the prefix into a range', function(done) { const fakeRange = {}; - const fakePrefixRange = { + const fakePrefixRange = ({ start: 'a', end: 'b', - } as {} as tblTypes.PrefixRange; + } as {}) as tblTypes.PrefixRange; const fakePrefix = 'abc'; - const prefixSpy = - sandbox.stub(Table, 'createPrefixRange').returns(fakePrefixRange); + const prefixSpy = sandbox + .stub(Table, 'createPrefixRange') + .returns(fakePrefixRange); - const rangeSpy = - sandbox.stub(FakeFilter, 'createRange').returns(fakeRange); + const rangeSpy = sandbox + .stub(FakeFilter, 'createRange') + .returns(fakeRange); table.bigtable.request = function(config) { assert.strictEqual(prefixSpy.getCall(0).args[0], fakePrefix); @@ -690,22 +695,24 @@ describe('Bigtable/Table', function() { it('should accept multiple prefixes', function(done) { const prefixes = ['abc', 'def']; - const prefixRanges = [ + const prefixRanges = ([ {start: 'abc', end: 'abd'}, {start: 'def', end: 'deg'}, - ] as {} as tblTypes.PrefixRange[]; - const prefixSpy = - sandbox.stub(Table, 'createPrefixRange').callsFake(function() { - const callIndex = prefixSpy.callCount - 1; - return prefixRanges[callIndex]; - }); + ] as {}) as tblTypes.PrefixRange[]; + const prefixSpy = sandbox + .stub(Table, 'createPrefixRange') + .callsFake(function() { + const callIndex = prefixSpy.callCount - 1; + return prefixRanges[callIndex]; + }); const ranges = [{}, {}]; - const rangeSpy = - sandbox.stub(FakeFilter, 'createRange').callsFake(() => { - const callIndex = rangeSpy.callCount - 1; - return ranges[callIndex]; - }); + const rangeSpy = sandbox + .stub(FakeFilter, 'createRange') + .callsFake(() => { + const callIndex = rangeSpy.callCount - 1; + return ranges[callIndex]; + }); table.bigtable.request = function(config) { assert.strictEqual(prefixSpy.callCount, 2); @@ -755,7 +762,10 @@ describe('Bigtable/Table', function() { return {} as Row; }); FakeChunkTransformer.prototype._transform = function( - chunks, enc, next) { + chunks, + enc, + next + ) { formattedRows.forEach(row => this.push(row)); next(); }; @@ -780,44 +790,42 @@ describe('Bigtable/Table', function() { it('should stream Row objects', function(done) { const rows: any[] = []; - table.createReadStream() - .on('error', done) - .on('data', - function(row) { - rows.push(row); - }) - .on('end', function() { - const rowSpy: any = table.row; + table + .createReadStream() + .on('error', done) + .on('data', function(row) { + rows.push(row); + }) + .on('end', function() { + const rowSpy: any = table.row; - assert.strictEqual(rows.length, formattedRows.length); - assert.strictEqual(rowSpy.callCount, formattedRows.length); + assert.strictEqual(rows.length, formattedRows.length); + assert.strictEqual(rowSpy.callCount, formattedRows.length); - assert.strictEqual( - rowSpy.getCall(0).args[0], formattedRows[0].key); - assert.strictEqual(rows[0].data, formattedRows[0].data); + assert.strictEqual(rowSpy.getCall(0).args[0], formattedRows[0].key); + assert.strictEqual(rows[0].data, formattedRows[0].data); - assert.strictEqual( - rowSpy.getCall(1).args[0], formattedRows[1].key); - assert.strictEqual(rows[1].data, formattedRows[1].data); + assert.strictEqual(rowSpy.getCall(1).args[0], formattedRows[1].key); + assert.strictEqual(rows[1].data, formattedRows[1].data); - done(); - }); + done(); + }); }); it('should allow a stream to end early', function(done) { const rows: any[] = []; - const stream = table.createReadStream() - .on('error', done) - .on('data', - function(row) { - rows.push(row); - stream.end(); - }) - .on('end', function() { - assert.strictEqual(rows.length, 1); - done(); - }); + const stream = table + .createReadStream() + .on('error', done) + .on('data', function(row) { + rows.push(row); + stream.end(); + }) + .on('end', function() { + assert.strictEqual(rows.length, 1); + done(); + }); }); }); @@ -866,70 +874,71 @@ describe('Bigtable/Table', function() { return stream; }; - table.createReadStream() - .on('error', - function(err) { - assert.strictEqual(error, err); - done(); - }) - .on('data', done); - }); - it('should emit an error event when chunk format returns error', - function(done) { - table.bigtable.request = function() { - const stream = new PassThrough({ - objectMode: true, - }); - - setImmediate(function() { - stream.push(fakeChunks); - stream.push(null); - }); - - return stream; - }; - FakeChunkTransformer.prototype._transform = function( - chunks, enc, next) { - next(error); - }; - table.createReadStream() - .on('error', - function(err) { - assert.strictEqual(error, err); - done(); - }) - .on('data', done); - }); - it('should emit an error event when chunktransformer returns error on flush end', - function(done) { - table.bigtable.request = function() { - const stream = new PassThrough({ - objectMode: true, - }); - - setImmediate(function() { - stream.push(null); - }); - - return stream; - }; - FakeChunkTransformer.prototype._flush = function(next) { - next(error); - }; - table.createReadStream() - .on('error', - function(err) { - assert.strictEqual(error, err); - done(); - }) - .on('data', done); - }); + table + .createReadStream() + .on('error', function(err) { + assert.strictEqual(error, err); + done(); + }) + .on('data', done); + }); + it('should emit an error event when chunk format returns error', function(done) { + table.bigtable.request = function() { + const stream = new PassThrough({ + objectMode: true, + }); + + setImmediate(function() { + stream.push(fakeChunks); + stream.push(null); + }); + + return stream; + }; + FakeChunkTransformer.prototype._transform = function( + chunks, + enc, + next + ) { + next(error); + }; + table + .createReadStream() + .on('error', function(err) { + assert.strictEqual(error, err); + done(); + }) + .on('data', done); + }); + it('should emit an error event when chunktransformer returns error on flush end', function(done) { + table.bigtable.request = function() { + const stream = new PassThrough({ + objectMode: true, + }); + + setImmediate(function() { + stream.push(null); + }); + + return stream; + }; + FakeChunkTransformer.prototype._flush = function(next) { + next(error); + }; + table + .createReadStream() + .on('error', function(err) { + assert.strictEqual(error, err); + done(); + }) + .on('data', done); + }); }); describe('retries', function() { let callCreateReadStream; - let emitters; // = [function(stream) { stream.push([{ key: 'a' }]); - // stream.end(); }, ...]; + let emitters; // = [function(stream) { stream.push([{ key: 'a' }]); + // stream.end(); }, ...]; let makeRetryableError; let reqOptsCalls; let setTimeoutSpy; @@ -946,13 +955,14 @@ describe('Bigtable/Table', function() { }; callCreateReadStream = (options, verify) => { - table.createReadStream(options) - .on('end', verify) - .resume(); // The stream starts paused unless it has a `.data()` - // callback. + table + .createReadStream(options) + .on('end', verify) + .resume(); // The stream starts paused unless it has a `.data()` + // callback. }; - emitters = null; // This needs to be assigned in each test case. + emitters = null; // This needs to be assigned in each test case. makeRetryableError = () => { const error: any = new Error('retry me!'); @@ -965,7 +975,7 @@ describe('Bigtable/Table', function() { if (start) { range.start = start.value || start; range.startInclusive = - typeof start === 'object' ? start.inclusive : true; + typeof start === 'object' ? start.inclusive : true; } if (end) { range.end = end.value || end; @@ -979,8 +989,9 @@ describe('Bigtable/Table', function() { reqOptsCalls = []; - setTimeoutSpy = sandbox.stub(global, 'setTimeout') - .callsFake(fn => (fn as Function)()); + setTimeoutSpy = sandbox + .stub(global, 'setTimeout') + .callsFake(fn => (fn as Function)()); table.bigtable.request = function(config) { reqOptsCalls.push(config.reqOpts); @@ -1020,49 +1031,47 @@ describe('Bigtable/Table', function() { }); }); - it('should have a range which starts after the last read key', - function(done) { - emitters = [ - function(stream) { - stream.push([{key: 'a'}]); - stream.emit('error', makeRetryableError()); - }, - function(stream) { - stream.end(); - }, - ]; - - callCreateReadStream(null, () => { - assert.strictEqual(reqOptsCalls[0].rows, undefined); - assert.deepStrictEqual(reqOptsCalls[1].rows, { - rowRanges: [{start: 'a', startInclusive: false}], - }); - done(); - }); - }); - - it('should move the active range start to after the last read key', - function(done) { - emitters = [ - function(stream) { - stream.push([{key: 'a'}]); - stream.emit('error', makeRetryableError()); - }, - function(stream) { - stream.end(); - }, - ]; - - callCreateReadStream({ranges: [{start: 'a'}]}, () => { - assert.deepStrictEqual(reqOptsCalls[0].rows, { - rowRanges: [{start: 'a', startInclusive: true}], - }); - assert.deepStrictEqual(reqOptsCalls[1].rows, { - rowRanges: [{start: 'a', startInclusive: false}], - }); - done(); - }); - }); + it('should have a range which starts after the last read key', function(done) { + emitters = [ + function(stream) { + stream.push([{key: 'a'}]); + stream.emit('error', makeRetryableError()); + }, + function(stream) { + stream.end(); + }, + ]; + + callCreateReadStream(null, () => { + assert.strictEqual(reqOptsCalls[0].rows, undefined); + assert.deepStrictEqual(reqOptsCalls[1].rows, { + rowRanges: [{start: 'a', startInclusive: false}], + }); + done(); + }); + }); + + it('should move the active range start to after the last read key', function(done) { + emitters = [ + function(stream) { + stream.push([{key: 'a'}]); + stream.emit('error', makeRetryableError()); + }, + function(stream) { + stream.end(); + }, + ]; + + callCreateReadStream({ranges: [{start: 'a'}]}, () => { + assert.deepStrictEqual(reqOptsCalls[0].rows, { + rowRanges: [{start: 'a', startInclusive: true}], + }); + assert.deepStrictEqual(reqOptsCalls[1].rows, { + rowRanges: [{start: 'a', startInclusive: false}], + }); + done(); + }); + }); it('should remove ranges which were already read', function(done) { emitters = [ @@ -1147,7 +1156,7 @@ describe('Bigtable/Table', function() { assert.deepStrictEqual(config.gaxOpts, {}); - callback(); // done() + callback(); // done() }; table.delete(done); @@ -1194,8 +1203,9 @@ describe('Bigtable/Table', function() { it('should respect the row key prefix option', function(done) { const fakePrefix = 'b'; - const spy = - ((FakeMutation as any).convertToBytes = sinon.spy(() => fakePrefix)); + const spy = ((FakeMutation as any).convertToBytes = sinon.spy( + () => fakePrefix + )); table.bigtable.request = function(config) { assert.strictEqual(config.reqOpts.rowKeyPrefix, fakePrefix); @@ -1333,7 +1343,7 @@ describe('Bigtable/Table', function() { table.create = function(options_, callback) { assert.strictEqual(options_.gaxOptions, options.gaxOptions); - callback(); // done() + callback(); // done() }; table.get(options, done); @@ -1577,7 +1587,7 @@ describe('Bigtable/Table', function() { it('should return true if token is consistent', done => { responses = [ (config, callback) => - callback(null, {consistencyToken: 'sample-token12345'}), + callback(null, {consistencyToken: 'sample-token12345'}), (config, callback) => callback(null, {consistent: true}), ]; @@ -1602,7 +1612,7 @@ describe('Bigtable/Table', function() { it('should retry checkConsistency', done => { responses = [ (config, callback) => - callback(null, {consistencyToken: 'sample-token12345'}), + callback(null, {consistencyToken: 'sample-token12345'}), (config, callback) => callback(null, {consistent: false}), (config, callback) => callback(null, {consistent: true}), ]; @@ -1655,7 +1665,7 @@ describe('Bigtable/Table', function() { responses = [ (config, callback) => - callback(null, {consistencyToken: 'sample-token12345'}), + callback(null, {consistencyToken: 'sample-token12345'}), (config, callback) => callback(error), ]; @@ -2016,7 +2026,9 @@ describe('Bigtable/Table', function() { bigtableInstance.request = function(config) { assert.strictEqual( - config.reqOpts.appProfileId, bigtableInstance.appProfileId); + config.reqOpts.appProfileId, + bigtableInstance.appProfileId + ); done(); }; @@ -2138,16 +2150,18 @@ describe('Bigtable/Table', function() { assert.deepStrictEqual(err.errors, [ Object.assign( - { - entry: entries[0], - }, - parsedStatuses[0]), + { + entry: entries[0], + }, + parsedStatuses[0] + ), Object.assign( - { - entry: entries[1], - }, - parsedStatuses[1]), + { + entry: entries[1], + }, + parsedStatuses[1] + ), ]); done(); @@ -2374,7 +2388,9 @@ describe('Bigtable/Table', function() { bigtableInstance.request = function(config) { assert.strictEqual( - config.reqOpts.appProfileId, bigtableInstance.appProfileId); + config.reqOpts.appProfileId, + bigtableInstance.appProfileId + ); done(); }; @@ -2430,19 +2446,19 @@ describe('Bigtable/Table', function() { it('should stream key objects', function(done) { const keys: any[] = []; - table.sampleRowKeysStream() - .on('error', done) - .on('data', - function(key) { - keys.push(key); - }) - .on('end', function() { - assert.strictEqual(keys[0].key, fakeKeys[0].rowKey); - assert.strictEqual(keys[0].offset, fakeKeys[0].offsetBytes); - assert.strictEqual(keys[1].key, fakeKeys[1].rowKey); - assert.strictEqual(keys[1].offset, fakeKeys[1].offsetBytes); - done(); - }); + table + .sampleRowKeysStream() + .on('error', done) + .on('data', function(key) { + keys.push(key); + }) + .on('end', function() { + assert.strictEqual(keys[0].key, fakeKeys[0].rowKey); + assert.strictEqual(keys[0].offset, fakeKeys[0].offsetBytes); + assert.strictEqual(keys[1].key, fakeKeys[1].rowKey); + assert.strictEqual(keys[1].offset, fakeKeys[1].offsetBytes); + done(); + }); }); }); @@ -2464,13 +2480,13 @@ describe('Bigtable/Table', function() { }); it('should emit an error event', function(done) { - table.sampleRowKeysStream() - .on('error', - function(err) { - assert.strictEqual(err, error); - done(); - }) - .on('data', done); + table + .sampleRowKeysStream() + .on('error', function(err) { + assert.strictEqual(err, error); + done(); + }) + .on('data', done); }); }); });