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.
iterable-ndjson
Advanced tools
Takes an (async) iterable that yields ndjson and returns an async iterable that yields JS objects
npm install iterable-ndjson
const ndjson = require('iterable-ndjson')
const it = ndjson.parse(source) // where `source` is any iterable that yields ndjson
for await (const obj of it)
console.log(obj)
Node.js streams are async iterable:
const ndjson = require('iterable-ndjson')
const fs = require('fs')
const source = fs.createReadStream('/path/to/file.ndjson')
for await (const obj of ndjson.parse(source))
console.log(obj)
Async iterable:
const ndjson = require('iterable-ndjson')
// An ndjson async iterator
const source = (() => {
const array = ['{"id": 1}\n', '{"id"', ': 2}', '\n{"id": 3}\n']
return {
[Symbol.asyncIterator] () {
return this
},
async next () {
await new Promise(resolve => setTimeout(resolve))
return array.length
? { done: false, value: array.shift() }
: { done: true }
}
}
})()
async function main () {
for await (const obj of ndjson.parse(source))
console.log(obj)
// Logs out:
// { id: 1 }
// { id: 2 }
// { id: 3 }
}
main()
Async iterable generator:
const ndjson = require('iterable-ndjson')
// An ndjson async iterator
const source = (async function * () {
const array = ['{"id": 1}\n', '{"id"', ': 2}', '\n{"id": 3}\n']
for (let i = 0; i < array.length; i++) {
yield new Promise(resolve => setTimeout(() => resolve(array[i])))
}
})()
async function main () {
for await (const obj of ndjson.parse(source))
console.log(obj)
// Logs out:
// { id: 1 }
// { id: 2 }
// { id: 3 }
}
main()
Regular iterable (like an array):
const ndjson = require('iterable-ndjson')
const source = ['{"id": 1}\n', '{"id"', ': 2}', '\n{"id": 3}\n']
async function main () {
for await (const obj of ndjson.parse(source))
console.log(obj)
// Logs out:
// { id: 1 }
// { id: 2 }
// { id: 3 }
}
main()
Stringify JS objects to NDJSON:
const ndjson = require('iterable-ndjson')
const source = [{ id: 1 }, { id: 2 }, { id: 3 }]
async function main () {
for await (const obj of ndjson.stringify(source))
console.log(obj)
// Logs out:
// '{"id":1}\n'
// '{"id":2}\n'
// '{"id":3}\n'
}
main()
Feel free to dive in! Open an issue or submit PRs.
MIT © Alan Shaw
FAQs
ndjson to async iterator
We found that iterable-ndjson 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.