Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
split-array-stream
Advanced tools
Safely push each item of an array to a stream.
$ npm install --save split-array-stream
const split = require('split-array-stream');
const through = require('through2');
const array = [
{ id: 1, user: 'Dave' },
{ id: 2, user: 'Stephen' }
];
const stream = through.obj();
stream.on('data', (item) => {
// { id: 1, user: 'Dave' }
// ...later...
// { id: 2, user: 'Stephen' }
});
split(array, stream).then((streamEnded) => {
if (!streamEnded) {
stream.push(null);
stream.end();
}
}).catch(console.error);
Before pushing an item to the stream, split-array-stream
checks that the stream hasn't been ended. This avoids those "push() after EOF" errors.
Say you're getting many items from an upstream API. Multiple requests might be required to page through all of the results. You want to push the results to the stream as they come in, and only get more results if the user hasn't ended the stream.
function getAllUsers() {
var stream = through.obj();
var requestOptions = {
method: 'get',
url: 'http://api/users',
};
request(requestOptions, onResponse);
function onResponse(err, response) {
split(response.users, stream).then((streamEnded) => {
if (streamEnded) {
return;
}
if (response.nextPageToken) {
requestOptions.pageToken = response.nextPageToken;
request(requestOptions, onResponse);
return;
}
stream.push(null);
stream.end();
});
});
return stream;
}
getAllUsers()
.on('data', function (user) {
// An item from the `response.users` API response
})
.on('end', function () {
// All users received
});
Array
The source array. Each item will be pushed to the provided stream.
Stream
The destination stream to receive the items of the array.
Function
Callback function executed after all items of the array have been iterated.
Boolean
Lets you know if the stream has been ended while items were being pushed.
FAQs
Safely push each item of an array to a stream
The npm package split-array-stream receives a total of 183,977 weekly downloads. As such, split-array-stream popularity was classified as popular.
We found that split-array-stream demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.