
Security News
Scaling Socket from Zero to 10,000+ Organizations
Socket CEO Feross Aboukhadijeh shares lessons from scaling a developer security startup to 10,000+ organizations in this founder interview.
dynamodb-stream
Advanced tools
A wrapper around low level aws sdk that makes it easy to consume a dynamodb-stream, even in a browser.
A wrapper around low level aws sdk that makes it easy to consume a dynamodb-stream, even in a browser.
update: serious overhaul with this commit and a few smaller ones after. Major version is bumped to 1.x.x
fetchStreamState() should be invoked whenever the consumer wishes to get the updates.
When a consumer needs to maintain a replica of the table data, fetchStreamState() is invoked on regular intervals.
The current best practice for replication is to manage the state of the stream as it relates to the consumer in a separate dynamodb table (shard iterators/sequence numbers etc), so if a failure occurs, that consumer can get back to the point he was in the stream. However for small or even medium tables this is not necessary. One can simply reread the entire table on startup.
This different approach make things more "stateless" and slightly simpler (in my view):
ShardIteratorType: LATEST to get shard iterators for all the current shards of the stream. These iterators act as a "bookmark" in the stream.Wrapping the initial data scan with fetchStreamState() calls insures that no changes will be missed. At worst, the second call might yield some duplicates.
const DynamoDBStream = require('dynamodb-stream')
const { DynamoDB } = require('@aws-sdk/client-dynamodb')
const { DynamoDBStreams } = require('@aws-sdk/client-dynamodb-streams')
const { unmarshall } = require('@aws-sdk/util-dynamodb')
const STREAM_ARN = 'your stream ARN'
const TABLE_NAME = 'testDynamoDBStream'
async function main() {
// table primary key is "pk"
const ddb = new DynamoDB()
const ddbStream = new DynamoDBStream(
new DynamoDBStreams(),
STREAM_ARN,
unmarshall
)
const localState = new Map()
await ddbStream.fetchStreamState()
const { Items } = await ddb.scan({ TableName: TABLE_NAME })
Items.map(unmarshall).forEach(item => localState.set(item.pk, item))
// parse results and store in local state
const watchStream = () => {
console.log(localState)
setTimeout(() => ddbStream.fetchStreamState().then(watchStream), 10 * 1000)
}
watchStream()
ddbStream.on('insert record', (data, keys) => {
localState.set(data.pk, data)
})
ddbStream.on('remove record', (data, keys) => {
localState.remove(data.pk)
})
ddbStream.on('modify record', (newData, oldData, keys) => {
localState.set(newData.pk, newData)
})
ddbStream.on('new shards', (shardIds) => {})
ddbStream.on('remove shards', (shardIds) => {})
}
main()
If your program crash and you want to pick up where you left off then setShardsState() and getShardState() are here for the rescue (though, I haven't tested them yet but they should work... :) )
const DynamoDBStream = require('dynamodb-stream')
const { DynamoDBStreams } = require('@aws-sdk/client-dynamodb-streams')
const { unmarshall } = require('@aws-sdk/util-dynamodb')
const fs = require('fs').promises
const STREAM_ARN = 'your stream ARN'
const FILE = 'shardState.json'
async function main() {
const ddbStream = new DynamoDBStream(
new DynamoDBStreams(),
STREAM_ARN,
unmarshall
)
// update the state so it will pick up from where it left last time
// remember this has a limit of 24 hours or something along these lines
// https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Streams.html
ddbStream.setShardState(await loadShardState())
const fetchStreamState = () => {
setTimeout(async () => {
await ddbStream.fetchStreamState()
const shardState = ddbStream.getShardState()
await fs.writeFile(FILE, JSON.stringify(shardState))
fetchStreamState()
}, 1000 * 20)
}
fetchStreamState()
}
async function loadShardState() {
try {
return JSON.parse(await fs.readFile(FILE, 'utf8'))
} catch (e) {
if (e.code === 'ENOENT') return {}
throw e
}
}
main()
MIT © ironSource ltd.
FAQs
A wrapper around low level aws sdk that makes it easy to consume a dynamodb-stream, even in a browser.
The npm package dynamodb-stream receives a total of 32 weekly downloads. As such, dynamodb-stream popularity was classified as not popular.
We found that dynamodb-stream demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CEO Feross Aboukhadijeh shares lessons from scaling a developer security startup to 10,000+ organizations in this founder interview.

Research
Socket Threat Research maps a rare inside look at OtterCookie’s npm-Vercel-GitHub chain, adding 197 malicious packages and evidence of North Korean operators.

Research
Socket researchers identified a malicious Chrome extension that manipulates Raydium swaps to inject an undisclosed SOL transfer, quietly routing fees to an attacker wallet.