Skip to content

Commit

Permalink
refactor(ts): convert to typescript (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored Nov 2, 2018
1 parent 1984bce commit f8bd0ae
Show file tree
Hide file tree
Showing 31 changed files with 339 additions and 225 deletions.
3 changes: 3 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Language: JavaScript
BasedOnStyle: Google
ColumnLimit: 80
2 changes: 1 addition & 1 deletion .jsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = {
source: {
excludePattern: '(^|\\/|\\\\)[._]',
include: [
'src'
'build/src'
],
includePattern: '\\.js$'
},
Expand Down
41 changes: 31 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
"node": ">=6.0.0"
},
"repository": "googleapis/nodejs-pubsub",
"main": "./src/index.js",
"main": "./build/src/index.js",
"files": [
"protos",
"src",
"build/src",
"AUTHORS",
"CONTRIBUTORS",
"LICENSE"
Expand Down Expand Up @@ -62,16 +62,20 @@
"renovate[bot] <renovate[bot]@users.noreply.github.com>"
],
"scripts": {
"system-test": "mocha system-test/ --timeout 600000",
"cover": "nyc --reporter=lcov mocha test/ && nyc report",
"presystem-test": "npm run compile",
"system-test": "mocha build/system-test --timeout 600000",
"cover": "nyc --reporter=lcov mocha build/test && nyc report",
"samples-test": "cd samples/ && npm link ../ && npm test && cd ../",
"test-no-cover": "mocha test/*.js",
"test-no-cover": "mocha build/test",
"test": "npm run cover",
"lint": "eslint '**/*.js'",
"prettier": "prettier --write src/*.js src/*/*.js samples/*.js samples/*/*.js test/*.js test/*/*.js system-test/*.js system-test/*/*.js smoke-test/*.js",
"lint": "eslint 'samples/*.js' 'samples/**/*.js'",
"docs": "jsdoc -c .jsdoc.js",
"fix": "eslint --fix '**/*.js' && npm run prettier",
"generate-scaffolding": "repo-tools generate all && repo-tools generate lib_samples_readme -l samples/ --config ../.cloud-repo-tools.json"
"fix": "eslint --fix 'samples/*.js' 'samples/**/*.js'",
"generate-scaffolding": "repo-tools generate all && repo-tools generate lib_samples_readme -l samples/ --config ../.cloud-repo-tools.json",
"clean": "gts clean",
"compile": "tsc -p . && cp -r src/v1/ build/src/v1/ && cp -r protos build/",
"prepare": "npm run compile",
"pretest": "npm run compile"
},
"dependencies": {
"@google-cloud/paginator": "^0.1.0",
Expand All @@ -95,19 +99,36 @@
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "^2.3.3",
"@types/arrify": "^1.0.4",
"@types/extend": "^3.0.0",
"@types/is": "0.0.20",
"@types/mocha": "^5.2.5",
"@types/proxyquire": "^1.3.28",
"@types/sinon": "^5.0.5",
"@types/through2": "^2.0.34",
"@types/uuid": "^3.4.4",
"async": "^2.6.0",
"codecov": "^3.0.0",
"eslint": "^5.0.0",
"eslint-config-prettier": "^3.0.0",
"eslint-plugin-node": "^8.0.0",
"eslint-plugin-prettier": "^3.0.0",
"gts": "^0.8.0",
"ink-docstrap": "^1.3.2",
"intelli-espower-loader": "^1.0.1",
"jsdoc": "^3.5.5",
"mocha": "^5.0.0",
"nyc": "^13.0.0",
"power-assert": "^1.4.4",
"prettier": "^1.9.1",
"proxyquire": "^2.0.0"
"proxyquire": "^2.0.0",
"sinon": "^7.1.1",
"source-map-support": "^0.5.9",
"typescript": "~3.1.5"
},
"nyc": {
"exclude": [
"build/test"
]
}
}
File renamed without changes.
42 changes: 26 additions & 16 deletions src/connection-pool.js → src/connection-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@
* limitations under the License.
*/

'use strict';

const {replaceProjectIdToken} = require('@google-cloud/projectify');
import {replaceProjectIdToken} from '@google-cloud/projectify';
const duplexify = require('duplexify');
const each = require('async-each');
const {EventEmitter} = require('events');
const is = require('is');
const through = require('through2');
const uuid = require('uuid');
const util = require('./util');
import {EventEmitter} from 'events';
import * as is from 'is';
import * as through from 'through2';
import * as uuid from 'uuid';
import * as util from './util';

const CHANNEL_READY_EVENT = 'channel.ready';
const CHANNEL_ERROR_EVENT = 'channel.error';
Expand Down Expand Up @@ -64,6 +62,18 @@ const RETRY_CODES = [
* creating a connection.
*/
class ConnectionPool extends EventEmitter {
subscription;
pubsub;
connections;
isPaused;
isOpen;
isGettingChannelState;
failedConnectionAttempts;
noConnectionsTime;
settings;
queue;
keepAliveHandle;
client;
constructor(subscription) {
super();
this.subscription = subscription;
Expand Down Expand Up @@ -223,7 +233,7 @@ class ConnectionPool extends EventEmitter {
this.queueConnection();
} else if (this.isOpen && !this.connections.size) {
const error = new Error(status.details);
error.code = status.code;
(error as any).code = status.code;
this.emit('error', error);
}
};
Expand Down Expand Up @@ -272,13 +282,13 @@ class ConnectionPool extends EventEmitter {
get length() {
return originalDataLength;
},
};
message.ack = () => {
this.subscription.ack_(message);
};
message.nack = () => {
this.subscription.nack_(message);
};
ack: () => {
this.subscription.ack_(message);
},
nack: () => {
this.subscription.nack_(message);
}
}
return message;
}
/*!
Expand Down
7 changes: 4 additions & 3 deletions src/histogram.js → src/histogram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
* limitations under the License.
*/

'use strict';

const extend = require('extend');
import * as extend from 'extend';

/*!
* The Histogram class is used to capture the lifespan of messages within the
Expand All @@ -27,6 +25,9 @@ const extend = require('extend');
* @class
*/
class Histogram {
options;
data;
length;
constructor(options) {
this.options = extend(
{
Expand Down
10 changes: 7 additions & 3 deletions src/iam.js → src/iam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

'use strict';

const arrify = require('arrify');
const {promisifyAll} = require('@google-cloud/promisify');
const is = require('is');
import * as arrify from 'arrify';
import {promisifyAll} from '@google-cloud/promisify';
import * as is from 'is';

/**
* [IAM (Identity and Access Management)](https://cloud.google.com/pubsub/access_control)
Expand Down Expand Up @@ -63,6 +63,10 @@ const is = require('is');
* // subscription.iam
*/
class IAM {
Promise;
pubsub;
request;
id;
constructor(pubsub, id) {
if (pubsub.Promise) {
this.Promise = pubsub.Promise;
Expand Down
30 changes: 18 additions & 12 deletions src/index.js → src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@

'use strict';

const {replaceProjectIdToken} = require('@google-cloud/projectify');
const {paginator} = require('@google-cloud/paginator');
const {promisifyAll} = require('@google-cloud/promisify');
const extend = require('extend');
const {GoogleAuth} = require('google-auth-library');
import {replaceProjectIdToken} from '@google-cloud/projectify';
import {paginator} from '@google-cloud/paginator';
import {promisifyAll} from '@google-cloud/promisify';
import * as extend from 'extend';
import {GoogleAuth} from 'google-auth-library';
const gax = require('google-gax');
const {grpc} = new gax.GrpcClient();
const is = require('is');
import * as is from 'is';

const PKG = require('../package.json');
const PKG = require('../../package.json');
const v1 = require('./v1');

const Snapshot = require('./snapshot');
Expand Down Expand Up @@ -94,6 +94,12 @@ const PROJECT_ID_PLACEHOLDER = '{{projectId}}';
* Full quickstart example:
*/
class PubSub {
options;
isEmulator;
api;
auth;
projectId;
Promise;
constructor(options) {
options = options || {};
// Determine what scopes are needed.
Expand Down Expand Up @@ -728,7 +734,7 @@ class PubSub {
* // message.publishTime = Timestamp when Pub/Sub received the message.
* });
*/
subscription(name, options) {
subscription(name, options?) {
if (!name) {
throw new Error('A name must be specified for a subscription.');
}
Expand All @@ -748,7 +754,7 @@ class PubSub {
*
* const topic = pubsub.topic('my-topic');
*/
topic(name, options) {
topic(name, options?) {
if (!name) {
throw new Error('A name must be specified for a topic.');
}
Expand Down Expand Up @@ -786,7 +792,7 @@ class PubSub {
* this.end();
* });
*/
PubSub.prototype.getSnapshotsStream = paginator.streamify('getSnapshots');
(PubSub.prototype as any).getSnapshotsStream = paginator.streamify('getSnapshots');

/**
* Get a list of the {@link Subscription} objects registered to all of
Expand Down Expand Up @@ -819,7 +825,7 @@ PubSub.prototype.getSnapshotsStream = paginator.streamify('getSnapshots');
* this.end();
* });
*/
PubSub.prototype.getSubscriptionsStream = paginator.streamify(
(PubSub.prototype as any).getSubscriptionsStream = paginator.streamify(
'getSubscriptions'
);

Expand Down Expand Up @@ -854,7 +860,7 @@ PubSub.prototype.getSubscriptionsStream = paginator.streamify(
* this.end();
* });
*/
PubSub.prototype.getTopicsStream = paginator.streamify('getTopics');
(PubSub.prototype as any).getTopicsStream = paginator.streamify('getTopics');

/*! Developer Documentation
*
Expand Down
13 changes: 9 additions & 4 deletions src/publisher.js → src/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

'use strict';

const arrify = require('arrify');
const {promisifyAll} = require('@google-cloud/promisify');
import * as arrify from 'arrify';
import {promisifyAll} from '@google-cloud/promisify';
const each = require('async-each');
const extend = require('extend');
const is = require('is');
import * as extend from 'extend';
import * as is from 'is';

/**
* A Publisher object allows you to publish messages to a specific topic.
Expand Down Expand Up @@ -49,6 +49,11 @@ const is = require('is');
* const publisher = topic.publisher();
*/
class Publisher {
Promise;
topic;
inventory_;
settings;
timeoutHandle_;
constructor(topic, options) {
if (topic.Promise) {
this.Promise = topic.Promise;
Expand Down
11 changes: 8 additions & 3 deletions src/snapshot.js → src/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

'use strict';

const util = require('./util');
const {promisifyAll} = require('@google-cloud/promisify');
const is = require('is');
import * as util from './util';
import {promisifyAll} from '@google-cloud/promisify';
import * as is from 'is';

/**
* A Snapshot object will give you access to your Cloud Pub/Sub snapshot.
Expand Down Expand Up @@ -85,6 +85,11 @@ const is = require('is');
* });
*/
class Snapshot {
parent;
name;
Promise;
create;
seek;
constructor(parent, name) {
if (parent.Promise) {
this.Promise = parent.Promise;
Expand Down
Loading

0 comments on commit f8bd0ae

Please sign in to comment.