Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "@aptoma/eslint-config",
"parserOptions": {
"ecmaVersion": "2017"
"ecmaVersion": 2023
},
"env": {
"node": true,
Expand Down
22 changes: 22 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Node.js CI

on: [push]

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20]

steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
registry-url: 'https://registry.npmjs.org'
cache: 'npm'
cache-dependency-path: ./package.json
- run: npm install
- run: npm test
4 changes: 0 additions & 4 deletions .travis.yml

This file was deleted.

5 changes: 1 addition & 4 deletions handlers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';

const Promise = require('bluebird');
const Hoek = require('@hapi/hoek');
const request = require('request-prom');

exports.hook = (sns, {handlers, skipPayloadValidation, topic}) => {
handlers = Hoek.applyToDefaults({
Expand Down Expand Up @@ -41,9 +39,8 @@ async function confirmSubscription(sns, topicOpts, req, h, payload) {
}

try {
await request.get(payload.SubscribeURL);
await fetch(payload.SubscribeURL);
req.log(['hookido', 'info'], `SNS subscription confirmed for ${payload.TopicArn}`);

} catch (err) {
req.log(['hookido', 'error'], `Unable to confirm SNS subscription for ${payload.TopicArn}, err: ${err.message}`);
throw err;
Expand Down
29 changes: 14 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,22 @@ function register(server, opts) {
snsInstances.push(sns);
const subscribe = Hoek.reach(config, 'topic.subscribe');

function requestSubscription() {
return sns
.subscribe(config.topic.arn, subscribe.protocol, subscribe.endpoint)
.then(() => server.log(['hookido', 'subscribe'], `Subscription request sent for ${config.topic.arn}`));
}

if (subscribe) {

server.ext('onPostStart', () => {
return sns
.findSubscriptionArn(config.topic.arn, subscribe.protocol, subscribe.endpoint)
.then(() => server.log(['hookido', 'subscribe'], `Subscription already exists for ${config.topic.arn}`))
.catch({code: 'NOT_FOUND'}, requestSubscription)
.catch({code: 'PENDING'}, requestSubscription)
.catch((err) => server.log(['hookido', 'subscribe', 'error'], err));
server.ext('onPostStart', async () => {
try {
await sns.findSubscriptionArn(config.topic.arn, subscribe.protocol, subscribe.endpoint);
server.log(['hookido', 'subscribe'], `Subscription already exists for ${config.topic.arn}`);
} catch (err) {
if (err.code === 'NOT_FOUND' || err.code === 'PENDING') {
await sns
.subscribe(config.topic.arn, subscribe.protocol, subscribe.endpoint)
.then(() => server.log(['hookido', 'subscribe'], `Subscription request sent for ${config.topic.arn}`))
.catch((err) => server.log(['hookido', 'subscribe', 'error'], err));
} else {
server.log(['hookido', 'subscribe', 'error'], err);
}
}
});

}

const topicAttributes = Hoek.reach(config, 'topic.attributes');
Expand Down
49 changes: 26 additions & 23 deletions lib/sns.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,52 @@
'use strict';

const Promise = require('bluebird');
const AWS = require('aws-sdk');
const {
SNSClient,
SubscribeCommand,
SetTopicAttributesCommand,
ListSubscriptionsByTopicCommand,
SetSubscriptionAttributesCommand
} = require('@aws-sdk/client-sns');
const MessageValidator = require('sns-validator');
const validator = new MessageValidator();

class SNS {

constructor(awsConfig) {
const sns = new AWS.SNS(awsConfig || {});
this.sns = {
subscribe: Promise.promisify(sns.subscribe, {context: sns}),
setTopicAttributes: Promise.promisify(sns.setTopicAttributes, {context: sns}),
listSubscriptionsByTopic: Promise.promisify(sns.listSubscriptionsByTopic, {context: sns}),
setSubscriptionAttributes: Promise.promisify(sns.setSubscriptionAttributes, {context: sns})
};
this.snsClient = new SNSClient(awsConfig || {});
}

setTopicAttributes(TopicArn, attributes) {
// this needs to be done sequentially otherwise only one attribute will be set, have no idea why.
return Promise.mapSeries(Object.keys(attributes), (key) => {
return this.sns.setTopicAttributes({
async setTopicAttributes(TopicArn, attributes) {
// Process attributes sequentially
for (const key of Object.keys(attributes)) {
const command = new SetTopicAttributesCommand({
TopicArn,
AttributeName: key,
AttributeValue: attributes[key]
});
});
await this.snsClient.send(command);
}
}

subscribe(TopicArn, Protocol, Endpoint) {
const params = {
const command = new SubscribeCommand({
TopicArn,
Protocol,
Endpoint
};
});

return this.sns.subscribe(params);
return this.snsClient.send(command);
}

setSubscriptionAttributes(SubscriptionArn, attributes) {
return Promise.mapSeries(Object.keys(attributes), (key) => {
return this.sns.setSubscriptionAttributes({
async setSubscriptionAttributes(SubscriptionArn, attributes) {
for (const key of Object.keys(attributes)) {
const command = new SetSubscriptionAttributesCommand({
SubscriptionArn,
AttributeName: key,
AttributeValue: attributes[key]
});
});
await this.snsClient.send(command);
}
}

/**
Expand All @@ -63,8 +64,10 @@ class SNS {
params.NextToken = NextToken;
}

return this.sns
.listSubscriptionsByTopic(params)
const command = new ListSubscriptionsByTopicCommand(params);

return this.snsClient
.send(command)
.then((data) => {
const arn = data.Subscriptions.find((sub) => {
return sub.Protocol.toLowerCase() === Protocol.toLowerCase() &&
Expand Down
25 changes: 12 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"release:major": "npm run test && release-it -n -i major"
},
"engines": {
"node": ">=10.x.x"
"node": ">=20.x.x"
},
"repository": {
"type": "git",
Expand All @@ -31,21 +31,20 @@
},
"homepage": "https://github.com/martinj/hookido",
"dependencies": {
"@hapi/hoek": "^9.2.1",
"aws-sdk": "^2.1094.0",
"bluebird": "^3.7.2",
"joi": "^17.6.0",
"request-prom": "^4.0.1",
"sns-validator": "^0.3.4"
"@aws-sdk/client-sns": "^3.731.1",
"@hapi/hoek": "^11.0.7",
"joi": "^17.13.3",
"sns-validator": "^0.3.5"
},
"devDependencies": {
"@aptoma/eslint-config": "^7.0.1",
"@hapi/hapi": "^20.2.1",
"chai": "^4.3.6",
"eslint": "^8.11.0",
"mocha": "^9.2.2",
"nock": "^13.2.4",
"@hapi/hapi": "^21.3.12",
"aws-sdk-client-mock": "^4.1.0",
"chai": "^4.5.0",
"eslint": "^8.57.1",
"mocha": "^11.0.1",
"nyc": "^15.1.0",
"release-it": "^2.7.3"
"release-it": "^2.7.3",
"sinon": "^19.0.2"
}
}
Loading
Loading