|
| 1 | +const {Writable} = require('stream'); |
| 2 | +const DynamodbClient = require('aws-sdk/clients/dynamodb'); |
| 3 | +const DynamodbStreamsClient = require('aws-sdk/clients/dynamodbstreams'); |
| 4 | +const DynamodbStreamsReadable = require('dynamodb-streams-readable'); |
| 5 | +const {assign} = require('lodash/fp'); |
| 6 | +const DynamodbStreamsEventDefinition = require('./dynamodb-streams-event-definition'); |
| 7 | +const DynamodbStreamsEvent = require('./dynamodb-streams-event'); |
| 8 | + |
| 9 | +const delay = timeout => new Promise(resolve => setTimeout(resolve, timeout)); |
| 10 | + |
| 11 | +class DynamodbStreams { |
| 12 | + constructor(lambda, options) { |
| 13 | + this.lambda = null; |
| 14 | + this.options = null; |
| 15 | + |
| 16 | + this.lambda = lambda; |
| 17 | + this.options = options; |
| 18 | + |
| 19 | + this.client = new DynamodbClient(this.options); |
| 20 | + this.streamsClient = new DynamodbStreamsClient(this.options); |
| 21 | + |
| 22 | + this.readables = []; |
| 23 | + } |
| 24 | + |
| 25 | + create(events) { |
| 26 | + return Promise.all( |
| 27 | + events.map(({functionKey, dynamodbStreams}) => this._create(functionKey, dynamodbStreams)) |
| 28 | + ); |
| 29 | + } |
| 30 | + |
| 31 | + start() { |
| 32 | + this.readables.forEach(readable => readable.resume()); |
| 33 | + } |
| 34 | + |
| 35 | + stop(timeout) { |
| 36 | + this.readables.forEach(readable => readable.pause()); |
| 37 | + } |
| 38 | + |
| 39 | + _create(functionKey, rawDynamodbStreamsEventDefinition) { |
| 40 | + const dynamodbStreamsEvent = new DynamodbStreamsEventDefinition( |
| 41 | + rawDynamodbStreamsEventDefinition, |
| 42 | + this.options.region, |
| 43 | + this.options.accountId |
| 44 | + ); |
| 45 | + |
| 46 | + return this._dynamodbStreamsEvent(functionKey, dynamodbStreamsEvent); |
| 47 | + } |
| 48 | + |
| 49 | + async _dynamodbStreamsEvent(functionKey, dynamodbStreamsEvent) { |
| 50 | + const { |
| 51 | + enabled, |
| 52 | + tableName, |
| 53 | + arn, |
| 54 | + batchSize, |
| 55 | + startingPosition, |
| 56 | + maximumRetryAttempts |
| 57 | + } = dynamodbStreamsEvent; |
| 58 | + |
| 59 | + if (!enabled) return; |
| 60 | + |
| 61 | + const { |
| 62 | + Table: {LatestStreamArn} |
| 63 | + } = await this.client |
| 64 | + .describeTable({ |
| 65 | + TableName: tableName |
| 66 | + }) |
| 67 | + .promise(); |
| 68 | + |
| 69 | + const { |
| 70 | + StreamDescription: {Shards: shards} |
| 71 | + } = await this.streamsClient |
| 72 | + .describeStream({ |
| 73 | + StreamArn: LatestStreamArn |
| 74 | + }) |
| 75 | + .promise(); |
| 76 | + |
| 77 | + shards.forEach(({ShardId: shardId}) => { |
| 78 | + const readable = DynamodbStreamsReadable( |
| 79 | + this.streamsClient, |
| 80 | + LatestStreamArn, |
| 81 | + assign(dynamodbStreamsEvent, { |
| 82 | + shardId, |
| 83 | + limit: batchSize, |
| 84 | + iterator: startingPosition |
| 85 | + }) |
| 86 | + ); |
| 87 | + |
| 88 | + const writable = new Writable({ |
| 89 | + objectMode: true, |
| 90 | + write: (chunk, _, cb) => { |
| 91 | + const task = async remainingAttempts => { |
| 92 | + try { |
| 93 | + const lambdaFunction = this.lambda.get(functionKey); |
| 94 | + |
| 95 | + const event = new DynamodbStreamsEvent(chunk, this.region, arn); |
| 96 | + lambdaFunction.setEvent(event); |
| 97 | + |
| 98 | + await lambdaFunction.runHandler(); |
| 99 | + } catch (err) { |
| 100 | + if (remainingAttempts > 0) { |
| 101 | + await delay(500); |
| 102 | + return task(remainingAttempts - 1); |
| 103 | + } |
| 104 | + } |
| 105 | + }; |
| 106 | + |
| 107 | + task(maximumRetryAttempts - 1) |
| 108 | + .then(() => cb()) |
| 109 | + .catch(cb); |
| 110 | + } |
| 111 | + }); |
| 112 | + |
| 113 | + readable.pipe(writable); |
| 114 | + readable.pause(); |
| 115 | + |
| 116 | + this.readables.push(readable); |
| 117 | + }); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +module.exports = DynamodbStreams; |
0 commit comments