Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
event-target-to-async-iter
Advanced tools
Takes an event target and an event name and turns it into an async iterable
Takes an EventTarget
and an event name and turns it into an async iterable.
Note that the async iterable will not complete until either
return
function is called, orreturnEvent
is provided and gets dispatched on the event target.Waiting for web socket messages:
const socket = new WebSocket('wss://example.com/socketserver');
const iter = eventTargetToAsyncIter(socket, 'message', {
returnEvent: 'close'
});
for await (const message of iter) {
console.log(message)
}
Say you have a heavily callback-based API such as HTMLRewriter and would prefer to process it as an async iterable. You can use a custom event target and eventTargetToAsyncIter
:
const target = new EventTarget();
const iter = eventTargetToAsyncIter(target, 'data');
// Helper function that consumes a readable stream
async function swallow(stream) {
const reader = stream.getReader();
while (await reader.read().then(x => !x.done)) {}
}
const response = new HTMLRewriter()
.on('.athing[id]', {
element(el) {
target.dispatchEvent(new CustomEvent('data', {
detail: el.getAttribute('id'),
}));
}
})
.transform(await fetch('https://news.ycombinator.com'));
// No await here
swallow(response.body)
.then(() => iter.return()) // Don't create an endless loop
.catch(e => iter.throw(e)) // Don't swallow errors
for await (const event of iter) {
const id = Number(event.detail);
console.log(id);
}
FAQs
Takes an event target and an event name and turns it into an async iterable
We found that event-target-to-async-iter 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.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.