
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
next-buffer
Advanced tools
Node.js Buffer module, fully optimized for Next.js. C++ or Pure-JS automatic fallback, depending on the runtime environments. Tree-shaking ready.
Next Buffer is a version of Node.js Buffer module, optimized for client-side Next.js, with a ridiculously reduced bundle size.
This NPM module is usable server-side, client-side and in Edge Runtime (for middleware). Since Node.js Buffer is C++ implemented, it is already very fast and nothing more has been done on this side. Tis module is providing a pure-JS implementation of the Buffer API (at least for the main usecases), which is tree-shaking ready and optimized for client-side browser usage.
Please note that Next Buffer provides an API that is not 100% identical to Node's Buffer API, but it is compatible with the most common use cases. See the detailed list of implementations for more information. These limitations only concern the pure-JS implementation, when used on client-side (browser).
Choice of using Buffer or pure-JS is transparently and automatically handle by webpack. Fallback to Buffer (with C++ library) in 'node' and 'worker' environments, for faster execution (especially when encoding long string) in Next.js and Edge Runtime middlewares. On client-side, you will automatically use the pure-JS implementation, which is lighter and faster than the browserify Buffer implementation.
Pure-JS, tree-shaking ready, with optimized performances:
20kB seems nothing, but it's worth it if you're only using Buffer as a string encoder/decoder... From acorns grow oak trees.
Install with npm:
npm install next-buffer
The main difference with original Buffer API is that you have to separately import the specific encoder/decoders you need. This is to allow tree-shaking to be effective, and to avoid webpacking the whole Buffer API when you only need a small part of it.
The other difference is that you will instanciate Buffer with Buffer.from(string, FROM_UTF8) where FROM_UTF8 is an encoder (object providing an encode(string) method). In the original API, you use Buffer.from(string, 'utf-8'), where 'utf-8' is a string representing an encoding scheme.
Import only the class you need, in order tree-shaking to be effective.
import { Buffer, TO_BASE64URL } from 'next-buffer';
const phrase = 'Hello Mr Warniiiz 👋';
const base64Phrase = Buffer.from(phrase).toString(TO_BASE64URL);
console.log(base64Phrase);
// Expected: SGVsbG8gTXIgV2FybmlpaXog8J-Riw
FROM_UTF8 is the default Encoder, so you can omit the second parameter of Buffer.from() static method: In the above code snippet, Buffer.from(phrase) is equivalent to Buffer.from(phrase, FROM_UTF8).
import { Buffer, FROM_BASE64URL, TO_LATIN1 } from 'next-buffer';
const base64Phrase = "SGVsbG8gTXIgV2FybmlpaXog8J-Riw";
const phrase = Buffer.from(base64Phrase, FROM_BASE64URL).toString(TO_LATIN1);
console.log(phrase);
// Expected: Hello Mr Warniiiz ð
// (Latin-1 does not support Unicode characters)
import { Buffer, FROM_HEX } from 'next-buffer';
const hexPhrase = "48656c6c6f204d72205761726e6969697a20f09f918b";
const phrase = Buffer.from(hexPhrase, FROM_HEX).toString();
console.log(phrase);
// Expected: Hello Mr Warniiiz 👋
TO_UTF8 is the default Decoder, so you can omit the only parameter of toString() method: In the above code snippet, buffer.toString() is equivalent to buffer.toString(TO_UTF8).
Named straightforward, after their equivalent from Node's Buffer:
FROM_UTF8FROM_ASCIIFROM_LATIN1FROM_BINARYFROM_UTF16LEFROM_UCS2FROM_BASE64FROM_BASE64URLFROM_HEXTO_UTF8TO_ASCIITO_LATIN1TO_BINARYTO_UTF16LETO_UCS2TO_BASE64TO_BASE64URLTO_HEXFeel free to open an issue on GitHub if you need a specific encoder/decoder. You can also submit a pull request, developping your own Encoder/Decoder class: the only requirement is a respective encode(string) => typedArray or decode(typedArray) => string method.
Although the fallback on Node's Buffer when using Next Buffer in Node or Edge runtime shall never be a problem, you may encounter some limitations when using Next Buffer in client-side Next.js.
The Pure-JS API is not 100% identical as the one of Node.js. Some of the initial methods have not yet been implemented. If you need some, feel free to ask or to do a pull-request.
Implementation of Buffer basically extends Uint8Array, which natively implements some of the methods of Node.js Buffer.
Implemented methods (in Pure-JS Buffer):
Static method: Buffer.compare(buf1, buf2)Static method: Buffer.concat(list[, totalLength]) /!\ PARTIAL IMPLEMENTATIONStatic method: Buffer.from(array)Static method: Buffer.from(arrayBuffer[, byteOffset[, length]])Static method: Buffer.from(buffer)Static method: Buffer.from(object[, offsetOrEncoding[, length]]) /!\ MODIFIED IMPLEMENTATIONStatic method: Buffer.from(string[, encoding]) /!\ MODIFIED IMPLEMENTATIONStatic method: Buffer.isBuffer(obj)buf.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]]) /!\ PARTIAL IMPLEMENTATIONbuf.equals(otherBuffer)buf.toString([encoding[, start[, end]]]) /!\ PARTIAL & MODIFIED IMPLEMENTATIONInherited methods (from UInt8Array):
buf[index]buf.bufferbuf.byteOffsetbuf.entries()buf.fill(value[, offset[, end]][, encoding]) /!\ PARTIAL IMPLEMENTATIONbuf.includes(value[, byteOffset][, encoding]) /!\ PARTIAL IMPLEMENTATIONbuf.indexOf(value[, byteOffset][, encoding]) /!\ PARTIAL IMPLEMENTATIONbuf.keys()buf.lengthbuf.lastIndexOf(value[, byteOffset][, encoding]) /!\ PARTIAL IMPLEMENTATIONbuf.slice([start[, end]])buf.subarray([start[, end]])buf.values()Not implemented :
Static method: Buffer.alloc(size[, fill[, encoding]])Static method: Buffer.allocUnsafe(size)Static method: Buffer.allocUnsafeSlow(size)Static method: Buffer.byteLength(string[, encoding])Static method: Buffer.copyBytesFrom(view[, offset[, length]])Static method: Buffer.isEncoding(encoding)Class property: Buffer.poolSizebuf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])buf.readInt16BE|readBigInt64LE|readBigUInt64LE([offset]) ### x22 methodsbuf.swap16()buf.swap32()buf.swap64()buf.toJSON()buf.write(string[, offset[, length]][, encoding])buf.writeBigInt64BE|writeBigInt64LE|writeInt8(value[, offset]) ### x22 methodsPlease refer to:
To launch the tests using jest:
npm run test
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
Next Buffer module is ISC licensed.
FAQs
Node.js Buffer module, fully optimized for Next.js. C++ or Pure-JS automatic fallback, depending on the runtime environments. Tree-shaking ready.
We found that next-buffer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 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
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.