buffertools
Advanced tools
Comparing version 1.1.1 to 2.0.0
@@ -24,15 +24,25 @@ /* Copyright (c) 2010, Ben Noordhuis <info@bnoordhuis.nl> | ||
// extend object prototypes | ||
for (var key in buffertools) { | ||
var val = buffertools[key]; | ||
SlowBuffer.prototype[key] = val; | ||
Buffer.prototype[key] = val; | ||
exports[key] = val; | ||
} | ||
// bug fix, see https://github.com/bnoordhuis/node-buffertools/issues/#issue/6 | ||
Buffer.prototype.concat = SlowBuffer.prototype.concat = function() { | ||
var args = [this].concat(Array.prototype.slice.call(arguments)); | ||
return buffertools.concat.apply(buffertools, args); | ||
exports.extend = function() { | ||
var receivers; | ||
if (arguments.length > 0) { | ||
receivers = Array.prototype.slice.call(arguments); | ||
} else if (typeof SlowBuffer === 'function') { | ||
receivers = [Buffer.prototype, SlowBuffer.prototype]; | ||
} else { | ||
receivers = [Buffer.prototype]; | ||
} | ||
for (var i = 0, n = receivers.length; i < n; i += 1) { | ||
var receiver = receivers[i]; | ||
for (var key in buffertools) { | ||
receiver[key] = buffertools[key]; | ||
} | ||
if (receiver !== exports) { | ||
receiver.concat = function() { | ||
var args = [this].concat(Array.prototype.slice.call(arguments)); | ||
return buffertools.concat.apply(buffertools, args); | ||
}; | ||
} | ||
} | ||
}; | ||
exports.extend(exports); | ||
@@ -39,0 +49,0 @@ // |
{ | ||
"name": "buffertools", | ||
"main": "buffertools", | ||
"version": "1.1.1", | ||
"version": "2.0.0", | ||
"keywords": ["buffer", "buffers"], | ||
@@ -6,0 +6,0 @@ "description": "Working with node.js buffers made easy.", |
@@ -18,5 +18,12 @@ # node-buffertools | ||
require('buffertools'); | ||
new Buffer(42).clear(); | ||
require('buffertools').extend(); // extend Buffer.prototype | ||
var buf = new Buffer(42); // create a 42 byte buffer | ||
buf.clear(); // clear it! | ||
If you don't want to extend the Buffer class's prototype (recommended): | ||
var buffertools = require('buffertools'); | ||
var buf = new Buffer(42); | ||
buffertools.clear(buf); | ||
## Methods | ||
@@ -26,8 +33,23 @@ | ||
### Buffer.clear() | ||
### buffertools.extend([object], [object...]) | ||
Clear the buffer. This is equivalent to `Buffer.fill(0)`. | ||
Extend the arguments with the buffertools methods. If called without arguments, | ||
defaults to `[Buffer.prototype, SlowBuffer.prototype]`. Extending prototypes | ||
only makes sense for classes that derive from `Buffer`. | ||
buffertools v1.x extended the `Buffer` prototype by default. In v2.x, it is | ||
opt-in. The reason for that is that buffertools was originally developed for | ||
node.js v0.3 (or maybe v0.2, I don't remember exactly when buffers were added) | ||
where the `Buffer` class was devoid of any useful methods. Over the years, it | ||
has grown a number of utility methods, some of which conflict with the | ||
buffertools methods of the same name, like `Buffer#fill()`. | ||
### Buffer#clear() | ||
### buffertools.clear(buffer) | ||
Clear the buffer. This is equivalent to `Buffer#fill(0)`. | ||
Returns the buffer object so you can chain method calls. | ||
### Buffer.compare(buffer|string) | ||
### Buffer#compare(buffer|string) | ||
### buffertools.compare(buffer, buffer|string) | ||
@@ -43,3 +65,3 @@ Lexicographically compare two buffers. Returns a number less than zero | ||
### Buffer.concat(a, b, c, ...) | ||
### Buffer#concat(a, b, c, ...) | ||
### buffertools.concat(a, b, c, ...) | ||
@@ -58,3 +80,4 @@ | ||
### Buffer.equals(buffer|string) | ||
### Buffer#equals(buffer|string) | ||
### buffertools.equals(buffer, buffer|string) | ||
@@ -69,3 +92,4 @@ Returns true if this buffer equals the argument, false otherwise. | ||
### Buffer.fill(integer|string|buffer) | ||
### Buffer#fill(integer|string|buffer) | ||
### buffertools.fill(buffer, integer|string|buffer) | ||
@@ -75,3 +99,4 @@ Fill the buffer (repeatedly if necessary) with the argument. | ||
### Buffer.fromHex() | ||
### Buffer#fromHex() | ||
### buffertools.fromHex(buffer) | ||
@@ -82,3 +107,4 @@ Assumes this buffer contains hexadecimal data (packed, no whitespace) | ||
### Buffer.indexOf(buffer|string, [start=0]) | ||
### Buffer#indexOf(buffer|string, [start=0]) | ||
### buffertools.indexOf(buffer, buffer|string, [start=0]) | ||
@@ -88,3 +114,4 @@ Search this buffer for the first occurrence of the argument, starting at | ||
### Buffer.reverse() | ||
### Buffer#reverse() | ||
### buffertools.reverse(buffer) | ||
@@ -97,3 +124,4 @@ Reverse the content of the buffer in place. Example: | ||
### Buffer.toHex() | ||
### Buffer#toHex() | ||
### buffertools.toHex(buffer) | ||
@@ -100,0 +128,0 @@ Returns the contents of this buffer encoded as a hexadecimal string. |
23
test.js
@@ -16,8 +16,11 @@ /* Copyright (c) 2010, Ben Noordhuis <info@bnoordhuis.nl> | ||
buffertools = require('./buffertools'); | ||
Buffer = require('buffer').Buffer; | ||
assert = require('assert'); | ||
var buffertools = require('./buffertools'); | ||
var Buffer = require('buffer').Buffer; | ||
var assert = require('assert'); | ||
WritableBufferStream = buffertools.WritableBufferStream; | ||
var WritableBufferStream = buffertools.WritableBufferStream; | ||
// Extend Buffer.prototype and SlowBuffer.prototype. | ||
buffertools.extend(); | ||
// these trigger the code paths for UnaryAction and BinaryAction | ||
@@ -27,3 +30,3 @@ assert.throws(function() { buffertools.clear({}); }); | ||
a = new Buffer('abcd'), b = new Buffer('abcd'), c = new Buffer('efgh'); | ||
var a = new Buffer('abcd'), b = new Buffer('abcd'), c = new Buffer('efgh'); | ||
assert.ok(a.equals(b)); | ||
@@ -109,3 +112,3 @@ assert.ok(!a.equals(c)); | ||
// bug fix, see http://github.com/bnoordhuis/node-buffertools/issues#issue/5 | ||
endOfHeader = new Buffer('\r\n\r\n'); | ||
var endOfHeader = new Buffer('\r\n\r\n'); | ||
assert.equal(0, endOfHeader.indexOf(endOfHeader)); | ||
@@ -115,4 +118,4 @@ assert.equal(0, endOfHeader.indexOf('\r\n\r\n')); | ||
// feature request, see https://github.com/bnoordhuis/node-buffertools/issues#issue/8 | ||
closed = false; | ||
stream = new WritableBufferStream(); | ||
var closed = false; | ||
var stream = new WritableBufferStream(); | ||
@@ -134,5 +137,5 @@ stream.on('close', function() { closed = true; }); | ||
// GH-10 indexOf sometimes incorrectly returns -1 | ||
for (i = 0; i < 100; i++) { | ||
buffer = new Buffer('9A8B3F4491734D18DEFC6D2FA96A2D3BC1020EECB811F037F977D039B4713B1984FBAB40FCB4D4833D4A31C538B76EB50F40FA672866D8F50D0A1063666721B8D8322EDEEC74B62E5F5B959393CD3FCE831CC3D1FA69D79C758853AFA3DC54D411043263596BAD1C9652970B80869DD411E82301DF93D47DCD32421A950EF3E555152E051C6943CC3CA71ED0461B37EC97C5A00EBACADAA55B9A7835F148DEF8906914617C6BD3A38E08C14735FC2EFE075CC61DFE5F2F9686AB0D0A3926604E320160FDC1A4488A323CB4308CDCA4FD9701D87CE689AF999C5C409854B268D00B063A89C2EEF6673C80A4F4D8D0A00163082EDD20A2F1861512F6FE9BB479A22A3D4ACDD2AA848254BA74613190957C7FCD106BF7441946D0E1A562DA68BC37752B1551B8855C8DA08DFE588902D44B2CAB163F3D7D7706B9CC78900D0AFD5DAE5492535A17DB17E24389F3BAA6F5A95B9F6FE955193D40932B5988BC53E49CAC81955A28B81F7B36A1EDA3B4063CBC187B0488FCD51FAE71E4FBAEE56059D847591B960921247A6B7C5C2A7A757EC62A2A2A2A2A2A2A25552591C03EF48994BD9F594A5E14672F55359EF1B38BF2976D1216C86A59847A6B7C4A5C585A0D0A2A6D9C8F8B9E999C2A836F786D577A79816F7C577A797D7E576B506B57A05B5B8C4A8D99989E8B8D9E644A6B9D9D8F9C9E4A504A6B968B93984A93984A988FA19D919C999F9A4A8B969E588C93988B9C938F9D588D8B9C9E9999989D58909C8F988D92588E0D0A3D79656E642073697A653D373035393620706172743D31207063726333323D33616230646235300D0A2E0D0A').fromHex(); | ||
for (var i = 0; i < 100; i++) { | ||
var buffer = new Buffer('9A8B3F4491734D18DEFC6D2FA96A2D3BC1020EECB811F037F977D039B4713B1984FBAB40FCB4D4833D4A31C538B76EB50F40FA672866D8F50D0A1063666721B8D8322EDEEC74B62E5F5B959393CD3FCE831CC3D1FA69D79C758853AFA3DC54D411043263596BAD1C9652970B80869DD411E82301DF93D47DCD32421A950EF3E555152E051C6943CC3CA71ED0461B37EC97C5A00EBACADAA55B9A7835F148DEF8906914617C6BD3A38E08C14735FC2EFE075CC61DFE5F2F9686AB0D0A3926604E320160FDC1A4488A323CB4308CDCA4FD9701D87CE689AF999C5C409854B268D00B063A89C2EEF6673C80A4F4D8D0A00163082EDD20A2F1861512F6FE9BB479A22A3D4ACDD2AA848254BA74613190957C7FCD106BF7441946D0E1A562DA68BC37752B1551B8855C8DA08DFE588902D44B2CAB163F3D7D7706B9CC78900D0AFD5DAE5492535A17DB17E24389F3BAA6F5A95B9F6FE955193D40932B5988BC53E49CAC81955A28B81F7B36A1EDA3B4063CBC187B0488FCD51FAE71E4FBAEE56059D847591B960921247A6B7C5C2A7A757EC62A2A2A2A2A2A2A25552591C03EF48994BD9F594A5E14672F55359EF1B38BF2976D1216C86A59847A6B7C4A5C585A0D0A2A6D9C8F8B9E999C2A836F786D577A79816F7C577A797D7E576B506B57A05B5B8C4A8D99989E8B8D9E644A6B9D9D8F9C9E4A504A6B968B93984A93984A988FA19D919C999F9A4A8B969E588C93988B9C938F9D588D8B9C9E9999989D58909C8F988D92588E0D0A3D79656E642073697A653D373035393620706172743D31207063726333323D33616230646235300D0A2E0D0A').fromHex(); | ||
assert.equal(551, buffer.indexOf('=yend')); | ||
} |
Sorry, the diff of this file is not supported yet
32859
216
164