Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

catbox-memory

Package Overview
Dependencies
Maintainers
3
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

catbox-memory - npm Package Compare versions

Comparing version 1.0.5 to 1.1.0

54

lib/index.js

@@ -12,3 +12,4 @@ // Load modules

internals.defaults = {
maxByteSize: 100 * 1024 * 1024 // 100MB
maxByteSize: 100 * 1024 * 1024, // 100MB
allowMixedContent: false
};

@@ -20,8 +21,18 @@

internals.MemoryCacheEntry = function MemoryCacheEntry (key, value, ttl) {
internals.MemoryCacheEntry = function MemoryCacheEntry (key, value, ttl, allowMixedContent) {
// stringify() to prevent value from changing while in the cache
var stringifiedValue = JSON.stringify(value);
var valueByteSize = 0;
this.item = stringifiedValue;
if (allowMixedContent && Buffer.isBuffer(value)) {
this.item = new Buffer(value.length);
// copy buffer to prevent value from changing while in the cache
value.copy(this.item);
valueByteSize = this.item.length;
}
else {
// stringify() to prevent value from changing while in the cache
this.item = JSON.stringify(value);
valueByteSize = Buffer.byteLength(this.item);
}
this.stored = Date.now();

@@ -31,3 +42,3 @@ this.ttl = ttl;

// Approximate cache entry size without value: 144 bytes
this.byteSize = 144 + Buffer.byteLength(stringifiedValue) + Buffer.byteLength(key.segment) + Buffer.byteLength(key.id);
this.byteSize = 144 + valueByteSize + Buffer.byteLength(key.segment) + Buffer.byteLength(key.id);

@@ -42,2 +53,3 @@ this.timeoutId = null;

Hoek.assert(!options || options.maxByteSize === undefined || options.maxByteSize >= 0, 'Invalid cache maxByteSize value');
Hoek.assert(!options || options.allowMixedContent === undefined || typeof options.allowMixedContent === 'boolean', 'Invalid allowMixedContent value');

@@ -109,7 +121,12 @@ this.settings = Hoek.applyToDefaults(internals.defaults, options || {});

var value = null;
try {
value = JSON.parse(envelope.item);
if (Buffer.isBuffer(envelope.item)) {
value = envelope.item;
}
catch (err) {
return callback(new Error('Bad value content'));
else {
value = internals.parseJSON(envelope.item);
if (value instanceof Error) {
return callback(new Error('Bad value content'));
}
}

@@ -143,3 +160,3 @@

try {
envelope = new internals.MemoryCacheEntry(key, value, ttl);
envelope = new internals.MemoryCacheEntry(key, value, ttl, this.settings.allowMixedContent);
} catch (err) {

@@ -201,1 +218,16 @@ return callback(err);

};
internals.parseJSON = function (json) {
var obj = null;
try {
obj = JSON.parse(json);
}
catch (err) {
obj = err;
}
return obj;
};
{
"name": "catbox-memory",
"description": "Memory adapter for catbox",
"version": "1.0.5",
"version": "1.1.0",
"author": "Eran Hammer <eran@hammer.io> (http://hueniverse.com)",

@@ -6,0 +6,0 @@ "contributors": [

@@ -6,10 +6,16 @@ catbox-memory

Lead Maintainer - [Eran Hammer](https://github.com/hueniverse)
Lead Maintainer - [Colin Ihrig](https://github.com/cjihrig)
Current version: **1.0.x** [![Build Status](https://api.travis-ci.org/hapijs/catbox-memory.svg)](https://travis-ci.org/hapijs/catbox-memory)
### Options
- `maxByteSize` - sets an upper limit on the number of bytes that can be stored in the
cached. Once this limit is reached no additional items will be added to the cache
cache. Once this limit is reached no additional items will be added to the cache
until some expire. The utilized memory calculation is a rough approximation and must
not be relied on. Defaults to `104857600` (100MB).
- `allowMixedContent` - by default, all data is cached as JSON strings, and converted
to an object using `JSON.parse()` on retrieval. By setting this option to `true`,
`Buffer` data can be stored alongside the stringified data. `Buffer`s are not
stringified, and are copied before storage to prevent the value from changing while
in the cache. Defaults to `false`.

@@ -77,2 +77,70 @@ // Load modules

it('buffers can be set and retrieved when allowMixedContent is true', function (done) {
var buffer = new Buffer('string value');
var client = new Catbox.Client(new Memory({ allowMixedContent: true }));
client.start(function (err) {
var key = { id: 'x', segment: 'test' };
client.set(key, buffer, 500, function (err) {
expect(err).to.not.exist;
client.get(key, function (err, result) {
expect(err).to.not.exist;
expect(result.item instanceof Buffer).to.equal(true);
expect(result.item).to.deep.equal(buffer);
done();
});
});
});
});
it('buffers are copied before storing when allowMixedContent is true', function (done) {
var buffer = new Buffer('string value');
var client = new Catbox.Client(new Memory({ allowMixedContent: true }));
client.start(function (err) {
var key = { id: 'x', segment: 'test' };
client.set(key, buffer, 500, function (err) {
expect(err).to.not.exist;
client.get(key, function (err, result) {
expect(err).to.not.exist;
expect(result.item).to.not.equal(buffer);
done();
});
});
});
});
it('buffers are stringified when allowMixedContent is not true', function (done) {
var buffer = new Buffer('string value');
var client = new Catbox.Client(new Memory());
client.start(function (err) {
var key = { id: 'x', segment: 'test' };
client.set(key, buffer, 500, function (err) {
expect(err).to.not.exist;
client.get(key, function (err, result) {
expect(err).to.not.exist;
expect(result.item instanceof Buffer).to.equal(false);
expect(result.item).to.deep.equal(JSON.parse(JSON.stringify(buffer)));
done();
});
});
});
});
it('gets an item after setting it (no memory limit)', function (done) {

@@ -79,0 +147,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc