slack-client
Advanced tools
Comparing version 2.0.0-beta.1 to 2.0.0-beta.2
module.exports = { | ||
WebClient: require('./lib/clients/web/client'), | ||
RtmClient: require('./lib/clients/rtm/client'), | ||
WebClient: require('./lib/clients/web/client'), | ||
RtmClient: require('./lib/clients/rtm/client'), | ||
EVENTS: require('./lib/clients/events'), | ||
}; |
@@ -17,3 +17,3 @@ /** | ||
var helpers = require('./helpers'); | ||
var requestsTransport = require('./transports/request'); | ||
var requestsTransport = require('./transports/request').requestTransport; | ||
@@ -20,0 +20,0 @@ |
@@ -7,12 +7,14 @@ /** | ||
var MemoryDataStore = require('../data-store/memory-data-store'); | ||
var RtmClient = require('../rtm/client'); | ||
var LegacyRTMClient = function (slackToken, autoReconnect, autoMark) { | ||
function LegacyRTMClient(slackToken, autoReconnect) { | ||
var opts = { | ||
autoReconnect: autoReconnect, | ||
logLevel: 'debug', | ||
dataStore: new MemoryDataStore(), | ||
}; | ||
RtmClient.call(this, slackToken, opts); | ||
}; | ||
} | ||
@@ -19,0 +21,0 @@ inherits(LegacyRTMClient, RtmClient); |
@@ -13,2 +13,3 @@ /** | ||
var bind = require('lodash').bind; | ||
var cloneDeep = require('lodash').cloneDeep; | ||
var contains = require('lodash').contains; | ||
@@ -313,2 +314,4 @@ var inherits = require('inherits'); | ||
RTMClient.prototype._pingServer = function _pingServer() { | ||
var pongInterval; | ||
if (this.connected) { | ||
@@ -318,3 +321,3 @@ this.send({ type: 'ping' }); | ||
// If the last pong was more than MAX_PONG_INTERVAL, force a reconnect | ||
var pongInterval = Date.now() - this._lastPong; | ||
pongInterval = Date.now() - this._lastPong; | ||
if (pongInterval > this.MAX_PONG_INTERVAL) { | ||
@@ -345,10 +348,11 @@ this.reconnect(); | ||
* Handler to deal with the WebSocket message event. | ||
* @param {object} message | ||
* @param {object} wsMsg | ||
*/ | ||
RTMClient.prototype.handleWsMessage = function handleWsMessage(message) { | ||
this.logger.log('debug', message); | ||
this.emit(CLIENT_EVENTS.RAW_MESSAGE, message); | ||
RTMClient.prototype.handleWsMessage = function handleWsMessage(wsMsg) { | ||
var message; | ||
this.logger.log('debug', wsMsg); | ||
this.emit(CLIENT_EVENTS.RAW_MESSAGE, wsMsg); | ||
try { | ||
message = parseAPIResponse(message); | ||
message = parseAPIResponse(wsMsg); | ||
} catch (err) { | ||
@@ -395,4 +399,6 @@ // TODO(leah): Emit an event here? | ||
messageType, message) { | ||
var replyTo; | ||
if (messageType === RTM_API_EVENTS.MESSAGE) { | ||
var replyTo = message.replyTo; | ||
replyTo = message.replyTo; | ||
if (replyTo) { | ||
@@ -479,17 +485,23 @@ // TODO(leah): figure out how message pushes via the RTM API should work and how any errors | ||
RTMClient.prototype.send = function send(message, optCb) { | ||
var wsMsg = cloneDeep(message); | ||
var jsonMessage; | ||
var err; | ||
var _this = this; | ||
if (this.connected) { | ||
message.id = this.nextMessageId(); | ||
message = JSON.stringify(message); | ||
this._pendingMessages[message.id] = message; | ||
this.ws.send(message, undefined, function handleWsMsgResponse(err) { | ||
wsMsg.id = this.nextMessageId(); | ||
jsonMessage = JSON.stringify(wsMsg); | ||
this._pendingMessages[wsMsg.id] = wsMsg; | ||
this.ws.send(jsonMessage, undefined, function handleWsMsgResponse(wsRespErr) { | ||
if (!isUndefined(err)) { | ||
this.logger.debug('Unable to send message: ' + err); | ||
_this.logger.debug('Unable to send message: ' + wsRespErr); | ||
} | ||
if (!isUndefined(optCb)) { | ||
optCb(err); | ||
optCb(wsRespErr); | ||
} | ||
}); | ||
} else { | ||
var err = 'ws not connected, unable to send message'; | ||
err = 'ws not connected, unable to send message'; | ||
this.logger.debug(err); | ||
@@ -496,0 +508,0 @@ if (!isUndefined(optCb)) { |
@@ -5,7 +5,23 @@ /** | ||
var HttpsProxyAgent = require('https-proxy-agent'); | ||
var partial = require('lodash').partial; | ||
var request = require('request'); | ||
var requestTransport = function requestTransport(args, cb) { | ||
var requestArgs = { | ||
var handleRequestTranportRes = function handleRequestTranportRes(cb, err, response, body) { | ||
var headers; | ||
var statusCode; | ||
if (err) { | ||
headers = response ? response.headers || {} : {}; | ||
statusCode = response ? response.statusCode || null : null; | ||
cb(err, headers, statusCode, body); | ||
} else { | ||
cb(err, response.headers, response.statusCode, body); | ||
} | ||
}; | ||
var getRequestTransportArgs = function getReqestTransportArgs(args) { | ||
return { | ||
url: args.url, | ||
@@ -15,18 +31,21 @@ form: args.data, | ||
}; | ||
}; | ||
request.post(requestArgs, function handleRequestTranportResponse(err, response, body) { | ||
var headers; | ||
var statusCode; | ||
if (err) { | ||
headers = response ? response.headers || {} : {}; | ||
statusCode = response ? response.statusCode || null : null; | ||
cb(err, headers, statusCode, body); | ||
} else { | ||
cb(err, response.headers, response.statusCode, body); | ||
} | ||
}); | ||
var proxiedRequestTransport = function proxiedRequestTransport(proxyURL) { | ||
return function _proxiedRequestTransport(args, cb) { | ||
var requestArgs = getRequestTransportArgs(args); | ||
requestArgs.agent = new HttpsProxyAgent(proxyURL); | ||
request.post(requestArgs, partial(handleRequestTranportRes, cb)); | ||
}; | ||
}; | ||
module.exports = requestTransport; | ||
var requestTransport = function requestTransport(args, cb) { | ||
var requestArgs = getRequestTransportArgs(args); | ||
request.post(requestArgs, partial(handleRequestTranportRes, cb)); | ||
}; | ||
module.exports.proxiedRequestTransport = proxiedRequestTransport; | ||
module.exports.requestTransport = requestTransport; |
@@ -5,9 +5,24 @@ /** | ||
var HttpsProxyAgent = require('https-proxy-agent'); | ||
var WebSocket = require('ws'); | ||
var wsTransport = function wsTransport(socketUrl) { | ||
return new WebSocket(socketUrl); | ||
/** | ||
* | ||
* @param {String} socketUrl | ||
* @param {Object=} opts | ||
* @param {String} opts.proxyURL | ||
* @returns {*} | ||
*/ | ||
var wsTransport = function wsTransport(socketUrl, opts) { | ||
var wsTransportOpts = opts || {}; | ||
var wsOpts = {}; | ||
if (wsTransportOpts.proxyURL) { | ||
wsOpts.agent = new HttpsProxyAgent(wsTransportOpts.proxyUrl); | ||
} | ||
return new WebSocket(socketUrl, wsOpts); | ||
}; | ||
module.exports = wsTransport; |
@@ -39,7 +39,8 @@ /** | ||
WebAPIClient.prototype._createFacets = function _createFacets() { | ||
var newFacet; | ||
var makeAPICall; | ||
WebAPIClient.super_.prototype._createFacets.call(this); | ||
var newFacet; | ||
var makeAPICall = bind(this.makeAPICall, this); | ||
makeAPICall = bind(this.makeAPICall, this); | ||
forEach(facets, function registerWebClientFacet(Facet) { | ||
@@ -46,0 +47,0 @@ newFacet = new Facet(makeAPICall); |
{ | ||
"name": "slack-client", | ||
"version": "2.0.0-beta.1", | ||
"version": "2.0.0-beta.2", | ||
"description": "A library for creating a full Slack client", | ||
"main": "./index", | ||
"scripts": { | ||
"test": "mocha --recursive --reporter spec test", | ||
"lint": "eslint lib/**/*.js test/**/*.js" | ||
"lint": "eslint . --ignore-path .gitignore", | ||
"test": "mocha --recursive --reporter spec test" | ||
}, | ||
@@ -39,5 +39,2 @@ "keywords": [ | ||
"eslint-config-airbnb": "^3.0.1", | ||
"grunt": "^0.4.5", | ||
"grunt-cli": "^0.1.13", | ||
"grunt-release": "~0.6.0", | ||
"mocha": "~2.3.3", | ||
@@ -48,5 +45,5 @@ "nock": "^2.15.0", | ||
"engines": { | ||
"node": ">= 0.10.x", | ||
"node": ">= 0.12.x", | ||
"npm": ">= 1.1.x" | ||
} | ||
} |
@@ -32,3 +32,3 @@ # Node.js Slack Client Library | ||
var token = '' || process.env.SLACK_API_TOKEN; | ||
var token = process.env.SLACK_API_TOKEN || ''; | ||
@@ -44,3 +44,3 @@ var rtm = new RtmClient(token, {logLevel: 'debug'}); | ||
A full example of how to use this module from Node.js can be found in the [/examples directory](https://github.com/slackhq/node-slack-client/tree/master/examples). | ||
A full example of how to use this module from Node.js can be found in the [/examples directory](examples). | ||
@@ -47,0 +47,0 @@ ## Contribute |
@@ -16,2 +16,1 @@ var expect = require('chai').expect; | ||
}); | ||
@@ -25,3 +25,3 @@ var expect = require('chai').expect; | ||
return new MockWSServer({port: wssPort}); | ||
return new MockWSServer({ port: wssPort }); | ||
}; | ||
@@ -64,3 +64,3 @@ | ||
maxPongInterval: 2, | ||
reconnectionBackoff: 1 | ||
reconnectionBackoff: 1, | ||
}; | ||
@@ -80,7 +80,8 @@ | ||
testReconnectionLogic(onFirstConn, onSecondConnFn, {reconnectionBackoff: 1}, 5222, done); | ||
testReconnectionLogic(onFirstConn, onSecondConnFn, { reconnectionBackoff: 1 }, 5222, done); | ||
}); | ||
// This is overly complex for what it's trying to test (that a state var is getting toggled), but anyway | ||
// This is overly complex for what it's trying to test (that a state var is getting toggled), | ||
// but /shrug | ||
it('should not attempt to reconnect while a connection is in progress', function (done) { | ||
@@ -100,3 +101,3 @@ var attemptingReconnectSpy = sinon.spy(); | ||
testReconnectionLogic(onFirstConn, onSecondConnFn, {reconnectionBackoff: 1}, 5223, done); | ||
testReconnectionLogic(onFirstConn, onSecondConnFn, { reconnectionBackoff: 1 }, 5223, done); | ||
}); | ||
@@ -107,3 +108,3 @@ | ||
var onFirstConn = function (wss) { | ||
wss.sendMessageToClientConn({type: 'team_migration_started'}); | ||
wss.sendMessageToClientConn({ type: 'team_migration_started' }); | ||
}; | ||
@@ -115,3 +116,3 @@ | ||
testReconnectionLogic(onFirstConn, onSecondConnFn, {reconnectionBackoff: 1}, 5224, done); | ||
testReconnectionLogic(onFirstConn, onSecondConnFn, { reconnectionBackoff: 1 }, 5224, done); | ||
}); | ||
@@ -118,0 +119,0 @@ |
@@ -20,3 +20,3 @@ var expect = require('chai').expect; | ||
userAgent: 'test', | ||
transport: lodash.noop | ||
transport: lodash.noop, | ||
}; | ||
@@ -30,4 +30,4 @@ var client = new WebAPIClient('test-token', opts); | ||
it('should register facets during construction', function () { | ||
var client = new WebAPIClient('test-token', {transport: lodash.noop}); | ||
expect(client.auth).to.not.be.undefined; | ||
var client = new WebAPIClient('test-token', { transport: lodash.noop }); | ||
expect(client.auth).to.not.equal(undefined); | ||
}); | ||
@@ -39,9 +39,9 @@ | ||
statusCode: 200, | ||
body: '{"test": 10}' | ||
body: '{"test": 10}', | ||
}; | ||
var client = new WebAPIClient('test-token', {transport: mockTransport}); | ||
var client = new WebAPIClient('test-token', { transport: mockTransport }); | ||
client.makeAPICall('test', args, function (err, res) { | ||
expect(res).to.deep.equal({'test': 10}); | ||
expect(res).to.deep.equal({ test: 10 }); | ||
done(); | ||
@@ -52,5 +52,5 @@ }); | ||
it('should not crash when no callback is supplied to an API request', function () { | ||
var client = new WebAPIClient('test-token', {transport: mockTransport}); | ||
var client = new WebAPIClient('test-token', { transport: mockTransport }); | ||
client.makeAPICall('test', {test: 'test'}); | ||
client.makeAPICall('test', { test: 'test' }); | ||
}); | ||
@@ -61,2 +61,4 @@ | ||
var attemptAPICall = function (done) { | ||
var client; | ||
nock('https://slack.com/api') | ||
@@ -66,7 +68,7 @@ .post('/test') | ||
var client = new WebAPIClient('test-token', { | ||
client = new WebAPIClient('test-token', { | ||
retryConfig: { | ||
minTimeout: 0, | ||
maxTimeout: 1 | ||
} | ||
maxTimeout: 1, | ||
}, | ||
}); | ||
@@ -84,3 +86,3 @@ sinon.spy(client, 'transport'); | ||
.post('/test') | ||
.reply(429, '{}', {'X-Retry-After': 0}); | ||
.reply(429, '{}', { 'X-Retry-After': 0 }); | ||
@@ -87,0 +89,0 @@ attemptAPICall(done); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
191433
6
81
5267
2