Research
Security News
Kill Switch Hidden in npm Packages Typosquatting Chalk and Chokidar
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
circular-buffer
Advanced tools
This is a simple circular buffer implementation for NodeJS.
The implementation can function both as a queue (which it's most suited for), and as a (forgetful) stack. Queue functionality uses enq()
and deq()
; stack functionality uses push()
and pop()
. Values are enqueued at the front of the buffer and dequeued at the back of the buffer; pushing and popping is at the back of the buffer. Indexing is front-to-back: the last-enqueued item has lowest index, which is also the first-pushed item.
Below is a sample session with a circular buffer with this package. It should answer most questions.
var CircularBuffer = require("circular-buffer");
var buf = new CircularBuffer(3);
console.log(buf.capacity()); // -> 3
buf.enq(1);
buf.enq(2);
console.log(buf.size()); // -> 2
buf.toarray(); // -> [2,1]
buf.push(3);
buf.toarray(); // -> [2,1,3]
buf.enq(4);
console.log(buf.size()); // -> 3 (despite having added a fourth item!)
buf.toarray(); // -> [4,2,1]
buf.get(0); // -> 4 (last enqueued item is at start of buffer)
buf.get(0,2); // -> [4,2,1] (2-parameter get takes start and end)
buf.toarray(); // -> [4,2,1] (equivalent to buf.get(0,buf.size() - 1) )
console.log(buf.deq()); // -> 1
buf.toarray(); // -> [4,2]
buf.pop(); // -> 2 (deq and pop are functionally the same)
buf.deq(); // -> 4
buf.toarray(); // -> []
buf.deq(); // -> throws RangeError("CircularBuffer dequeue on empty buffer")
size()
-> integer
capacity()
-> integer
enq(value)
value
at the front of the bufferdeq()
-> value
RangeError
if the buffer is empty on invocation.get(idx)
-> value
idx
. 0
is the front of the buffer (last-enqueued item, or first-pushed item), buf.size()-1
is the back of the buffer.get(start,end)
-> [value]
start
up to and including index end
; returns an array, in front-to-back order. Equivalent to [buf.get(start),buf.get(start+1),/*etc*/,buf.get(end)]
.toarray()
-> [value]
buf.get(0,buf.size() - 1)
: exports all items in the buffer in front-to-back order.To test the package simply run npm update && npm test
in the package's root folder.
FAQs
A NodeJS simple circular buffer implementation supporting indexing
The npm package circular-buffer receives a total of 4,243 weekly downloads. As such, circular-buffer popularity was classified as popular.
We found that circular-buffer 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 found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.