Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

socket-mode: prep for major release #1732

Merged
merged 5 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/socket-mode/.nycrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"include": [
"src/**/*.ts"
],
"exclude": [
"**/*.spec.js"
],
"reporter": ["lcov"],
"extension": [
".ts"
],
"all": false,
"cache": true
}
2 changes: 1 addition & 1 deletion packages/socket-mode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ const socketModeClient = new SocketModeClient({

## Requirements

This package supports Node v14 and higher. It's highly recommended to use [the latest LTS version of
This package supports Node v18 and higher. It's highly recommended to use [the latest LTS version of
node](https://github.com/nodejs/Release#release-schedule), and the documentation is written using syntax and features from that version.

## Getting Help
Expand Down
56 changes: 28 additions & 28 deletions packages/socket-mode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"dist/**/*"
],
"engines": {
"node": ">=12.13.0",
"npm": ">=6.12.0"
"node": ">= 18",
"npm": ">= 8.6.0"
},
"repository": "slackapi/node-slack-sdk",
"homepage": "https://slack.dev/node-slack-sdk/socket-mode",
Expand All @@ -40,40 +40,40 @@
"build": "npm run build:clean && tsc",
"build:clean": "shx rm -rf ./dist ./coverage ./.nyc_output",
"lint": "eslint --ext .ts src",
"test": "npm run lint && npm run build && nyc mocha --config .mocharc.json src/*.spec.js",
"mocha": "mocha --config .mocharc.json src/*.spec.js",
"test:integration": "mocha --config .mocharc.json --timeout 10000 test/integration.spec.js",
"test": "npm run lint && npm run build && nyc --reporter=text-summary npm run mocha && npm run test:integration",
"watch": "npx nodemon --watch 'src' --ext 'ts' --exec npm run build"
},
"dependencies": {
"@slack/logger": "^3.0.0",
"@slack/web-api": "^6.11.2",
"@types/node": ">=12.0.0",
"@types/p-queue": "^2.3.2",
"@types/ws": "^7.4.7",
"eventemitter3": "^3.1.0",
"@slack/logger": "^4",
"@slack/web-api": "^7.0.1",
"@types/node": ">=18",
"@types/ws": "^8",
"eventemitter3": "^5",
"finity": "^0.5.4",
"p-cancelable": "^1.1.0",
"p-queue": "^2.4.2",
filmaj marked this conversation as resolved.
Show resolved Hide resolved
"ws": "^7.5.3"
"ws": "^8"
},
"devDependencies": {
"@types/chai": "^4.3.5",
"@types/mocha": "^10.0.1",
"@typescript-eslint/eslint-plugin": "^6.4.1",
"@typescript-eslint/parser": "^6.4.0",
"chai": "^4.3.8",
"eslint": "^8.47.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-airbnb-typescript": "^17.1.0",
"eslint-plugin-import": "^2.28.1",
"@types/chai": "^4",
"@types/mocha": "^10",
"@types/sinon": "^17",
"@typescript-eslint/eslint-plugin": "^6",
"@typescript-eslint/parser": "^6",
"chai": "^4",
"eslint": "^8",
"eslint-config-airbnb-base": "^15",
"eslint-config-airbnb-typescript": "^17",
"eslint-plugin-import": "^2",
"eslint-plugin-import-newlines": "^1.3.4",
"eslint-plugin-jsdoc": "^46.5.0",
"eslint-plugin-node": "^11.1.0",
"mocha": "^10.2.0",
"nyc": "^15.1.0",
"eslint-plugin-jsdoc": "^48",
"eslint-plugin-node": "^11",
"mocha": "^10",
"nyc": "^15",
"shx": "^0.3.2",
"sinon": "^15.2.0",
"sinon": "^17",
"source-map-support": "^0.5.21",
"ts-node": "^10.8.1",
"typescript": "^4.1.0"
"ts-node": "^10",
"typescript": "5.3.3"
}
}
30 changes: 15 additions & 15 deletions packages/socket-mode/src/SocketModeClient.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('SocketModeClient', () => {
});

describe('slash_commands messages', () => {
const message = {
const message = Buffer.from(JSON.stringify({
"envelope_id": "1d3c79ab-0ffb-41f3-a080-d19e85f53649",
"payload": {
"token": "verification-token",
Expand All @@ -37,7 +37,7 @@ describe('SocketModeClient', () => {
},
"type": "slash_commands",
"accepts_response_payload": true
};
}));

it('should be sent to two listeners', async () => {
const client = new SocketModeClient({ appToken: 'xapp-' });
Expand All @@ -52,7 +52,7 @@ describe('SocketModeClient', () => {
&& args.retry_num === undefined
&& args.retry_reason === undefined;
});
await client.onWebSocketMessage({ data: JSON.stringify(message) });
await client.onWebSocketMessage(message);
await sleep(30);
assert.isTrue(commandListenerCalled);
assert.isTrue(slackEventListenerCalled);
Expand All @@ -64,7 +64,7 @@ describe('SocketModeClient', () => {
client.on("slash_commands", async ({ envelope_id }) => {
passedEnvelopeId = envelope_id;
});
await client.onWebSocketMessage({ data: JSON.stringify(message) });
await client.onWebSocketMessage(message);
await sleep(30);
assert.equal(passedEnvelopeId, '1d3c79ab-0ffb-41f3-a080-d19e85f53649');
});
Expand All @@ -74,14 +74,14 @@ describe('SocketModeClient', () => {
client.on("slack_event", async ({ envelope_id }) => {
passedEnvelopeId = envelope_id;
});
await client.onWebSocketMessage({ data: JSON.stringify(message) });
await client.onWebSocketMessage(message);
await sleep(30);
assert.equal(passedEnvelopeId, '1d3c79ab-0ffb-41f3-a080-d19e85f53649');
});
});

describe('events_api messages', () => {
const message = {
const message = Buffer.from(JSON.stringify({
"envelope_id": "cda4159a-72a5-4744-aba3-4d66eb52682b",
"payload": {
"token": "verification-token",
Expand Down Expand Up @@ -133,7 +133,7 @@ describe('SocketModeClient', () => {
"accepts_response_payload": false,
"retry_attempt": 2,
"retry_reason": "timeout"
};
}));

it('should be sent to two listeners', async () => {
const client = new SocketModeClient({ appToken: 'xapp-' });
Expand All @@ -154,7 +154,7 @@ describe('SocketModeClient', () => {
&& args.retry_num === 2
&& args.retry_reason === 'timeout';
});
await client.onWebSocketMessage({ data: JSON.stringify(message) });
await client.onWebSocketMessage(message);
await sleep(30);
assert.isFalse(otherListenerCalled);
assert.isTrue(eventsApiListenerCalled);
Expand All @@ -167,7 +167,7 @@ describe('SocketModeClient', () => {
client.on("app_mention", async ({ envelope_id }) => {
passedEnvelopeId = envelope_id;
});
await client.onWebSocketMessage({ data: JSON.stringify(message) });
await client.onWebSocketMessage(message);
await sleep(30);
assert.equal(passedEnvelopeId, 'cda4159a-72a5-4744-aba3-4d66eb52682b');
});
Expand All @@ -177,14 +177,14 @@ describe('SocketModeClient', () => {
client.on("slack_event", async ({ envelope_id }) => {
passedEnvelopeId = envelope_id;
});
await client.onWebSocketMessage({ data: JSON.stringify(message) });
await client.onWebSocketMessage(message);
await sleep(30);
assert.equal(passedEnvelopeId, 'cda4159a-72a5-4744-aba3-4d66eb52682b');
});
});

describe('interactivity messages', () => {
const message = {
const message = Buffer.from(JSON.stringify({
"envelope_id": "57d6a792-4d35-4d0b-b6aa-3361493e1caf",
"payload": {
"type": "shortcut",
Expand All @@ -206,7 +206,7 @@ describe('SocketModeClient', () => {
},
"type": "interactive",
"accepts_response_payload": false
};
}));

it('should be sent to two listeners', async () => {
const client = new SocketModeClient({ appToken: 'xapp-' });
Expand All @@ -222,7 +222,7 @@ describe('SocketModeClient', () => {
client.on("slack_event", async (args) => {
slackEventListenerCalled = args.ack !== undefined && args.body !== undefined;
});
await client.onWebSocketMessage({ data: JSON.stringify(message) });
await client.onWebSocketMessage(message);
await sleep(30);
assert.isFalse(otherListenerCalled);
assert.isTrue(interactiveListenerCalled);
Expand All @@ -235,7 +235,7 @@ describe('SocketModeClient', () => {
client.on("interactive", async ({ envelope_id }) => {
passedEnvelopeId = envelope_id;
});
await client.onWebSocketMessage({ data: JSON.stringify(message) });
await client.onWebSocketMessage(message);
await sleep(30);
assert.equal(passedEnvelopeId, '57d6a792-4d35-4d0b-b6aa-3361493e1caf');
});
Expand All @@ -245,7 +245,7 @@ describe('SocketModeClient', () => {
client.on("slack_event", async ({ envelope_id }) => {
passedEnvelopeId = envelope_id;
});
await client.onWebSocketMessage({ data: JSON.stringify(message) });
await client.onWebSocketMessage(message);
await sleep(30);
assert.equal(passedEnvelopeId, '57d6a792-4d35-4d0b-b6aa-3361493e1caf');
});
Expand Down
26 changes: 15 additions & 11 deletions packages/socket-mode/src/SocketModeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ export class SocketModeClient extends EventEmitter {
private async retrieveWSSURL(): Promise<AppsConnectionsOpenResponse> {
try {
this.logger.debug('Going to retrieve a new WSS URL ...');
return await this.webClient.apps.connections.open();
return await this.webClient.apps.connections.open({});
} catch (error) {
this.logger.error(`Failed to retrieve a new WSS URL for reconnection (error: ${error})`);
throw error;
Expand Down Expand Up @@ -525,17 +525,17 @@ export class SocketModeClient extends EventEmitter {
websocket.addEventListener('open', (event) => {
this.stateMachine.handle(Event.WebSocketOpen, event);
});
websocket.addEventListener('close', (event) => {
this.stateMachine.handle(Event.WebSocketClose, event);
});
websocket.addEventListener('error', (event) => {
this.logger.error(`A WebSocket error occurred: ${event.message}`);
this.emit('error', websocketErrorWithOriginal(event.error));
});
websocket.addEventListener('message', this.onWebSocketMessage.bind(this));
websocket.on('message', this.onWebSocketMessage.bind(this));
websocket.on('close', (code: number, _data: Buffer) => {
this.stateMachine.handle(Event.WebSocketClose, code);
});

// Confirm WebSocket connection is still active
websocket.addEventListener('ping', ((data: Buffer) => {
websocket.on('ping', ((data: Buffer) => {
if (this.pingPongLoggingEnabled) {
this.logger.debug(`Received ping from Slack server (data: ${data})`);
}
Expand All @@ -544,7 +544,7 @@ export class SocketModeClient extends EventEmitter {
// we cast this function to any as a workaround
}) as any); // eslint-disable-line @typescript-eslint/no-explicit-any

websocket.addEventListener('pong', ((data: Buffer) => {
websocket.on('pong', ((data: Buffer) => {
if (this.pingPongLoggingEnabled) {
this.logger.debug(`Received pong from Slack server (data: ${data})`);
}
Expand Down Expand Up @@ -694,8 +694,12 @@ export class SocketModeClient extends EventEmitter {
* `onmessage` handler for the client's WebSocket.
* This will parse the payload and dispatch the relevant events for each incoming message.
*/
protected async onWebSocketMessage({ data }: { data: string }): Promise<void> {
this.logger.debug(`Received a message on the WebSocket: ${data}`);
protected async onWebSocketMessage(data: WebSocket.RawData, isBinary: boolean): Promise<void> {
filmaj marked this conversation as resolved.
Show resolved Hide resolved
if (isBinary) {
this.logger.error('Unexpected binary message received!');
return;
}
this.logger.debug(`Received a message on the WebSocket: ${data.toString()}`);

// Parse message into slack event
let event: {
Expand All @@ -710,12 +714,12 @@ export class SocketModeClient extends EventEmitter {
};

try {
event = JSON.parse(data);
event = JSON.parse(data.toString());
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (parseError: any) {
// Prevent application from crashing on a bad message, but log an error to bring attention
this.logger.error(
`Unable to parse an incoming WebSocket message: ${parseError.message}`,
`Unable to parse an incoming WebSocket message: ${parseError.message}, ${data.toString()}`,
);
return;
}
Expand Down
Loading