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

ttl-mem-cache

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ttl-mem-cache - npm Package Compare versions

Comparing version 3.0.0 to 3.0.1

.nyc_output/5228edb6b8787bacfb646c7d758d1f01.json

42

experiment/server.js

@@ -28,2 +28,3 @@ 'use strict';

transform(chunk, enc, next) {
/*
const c = Buffer.from(JSON.stringify(chunk));

@@ -33,2 +34,5 @@ const b = Buffer.from('\n\n');

this.push(b);
*/
const b = Buffer.from(chunk);
this.push(b);
next();

@@ -42,10 +46,18 @@ }

transform(chunk, enc, next) {
const c = chunk.toString();
console.log('c', c);
if (c !== undefined) {
const obj = JSON.parse(c);
this.push(obj);
}
/*
chunk.toString().split('\n\n').sort().filter((item, index, arr) => {
return item && (!index || item != arr[index - 1]);
})
.forEach((entry, x) => {
}).forEach((entry, x) => {
const obj = JSON.parse(entry);
console.log(x, obj);
this.push(obj);
});
});
*/
next();

@@ -64,4 +76,6 @@ }

a.on('connection', (connection, info) => {
cacheA.pipe(toJson).pipe(connection);
console.log('A connection', info);
if (info.initiator) {
cacheA.pipe(toJson).pipe(connection);
console.log('A connection', info);
}
// connection.pipe(fromJson).pipe(cacheA).pipe(toJson).pipe(connection);

@@ -92,4 +106,6 @@ });

b.on('connection', (connection, info) => {
connection.pipe(fromJson).pipe(cacheB);
console.log('B connection', info);
if (info.initiator) {
connection.pipe(fromJson).pipe(cacheB);
console.log('B connection', info);
}
// connection.pipe(fromJson).pipe(cacheB).pipe(toJson).pipe(connection);

@@ -117,4 +133,6 @@ });

c.on('connection', (connection, info) => {
connection.pipe(fromJson).pipe(cacheC);
console.log('C connection', info);
if (info.initiator) {
connection.pipe(fromJson).pipe(cacheC);
console.log('C connection', info);
}
// connection.pipe(fromJson).pipe(cacheC).pipe(toJson).pipe(connection);

@@ -141,4 +159,6 @@ });

d.on('connection', (connection, info) => {
connection.pipe(fromJson).pipe(cacheD);
console.log('D connection', info);
if (info.initiator) {
connection.pipe(fromJson).pipe(cacheD);
console.log('D connection', info);
}
// connection.pipe(fromJson).pipe(cacheC).pipe(toJson).pipe(connection);

@@ -145,0 +165,0 @@ });

'use strict';
const utils = require('./utils');
const os = require('os');

@@ -93,2 +94,7 @@ const _key = Symbol('_key');

[Symbol.toPrimitive](hint) {
if (hint === 'string') return `${JSON.stringify(this)}${os.EOL}`;
return Object.prototype.toString.call(this);
}
static assertLoose(obj = {}) {

@@ -95,0 +101,0 @@ if (utils.isNotEmpty(obj.key) && utils.isNotEmpty(obj.value)) {

{
"name": "ttl-mem-cache",
"version": "3.0.0",
"version": "3.0.1",
"description": "A in memory time to live cache with streaming support.",

@@ -5,0 +5,0 @@ "main": "lib/cache.js",

@@ -329,6 +329,7 @@ # ttl-mem-cache

When writing to the cache, one can control what goes into the cache etc by a dedicated Object type.
When reading from the cache, the stream will output the same Object type.
When using the Stream API to write to and read from the cache an [Entry Object](https://github.com/trygve-lie/ttl-mem-cache/blob/master/lib/entry.js)
or an Object of simmilar character is used.
The Object type looks like this:
The [Entry Object](https://github.com/trygve-lie/ttl-mem-cache/blob/master/lib/entry.js)
looks like this:

@@ -341,7 +342,19 @@ ```js

ttl: 'time to live',
expires: 'time stamp',
type: 'TtlMemCacheEntry'
expires: 'time stamp'
}
```
The Stream API will always emit a full Entry Object. When writing to the Stream API one can provide
a full Entry Object, but a simmilar Object will also be accepted. Only `key` and `value` are required
properties when writing to the Stream API.
Iow; the following Object will be accepted by the Stream API:
```js
{
key: 'foo',
value: 'bar'
}
```
`key` defines what key the value of `value` should be stored on in the cache. `key` is required

@@ -354,5 +367,12 @@ and if not provided the stream will emit an error.

When the stream emits objects each object will also have a `origin` key. The value is the unique
ID of the instance the object first was emitted on the stream.
When the stream emits objects each object will have a `origin` key. The value is the unique ID of
the instance the object first was emitted on the stream. If the construcor was given a value for
the `id` argument, this will be used as `origin` value.
`ttl` is how long the item is set to live in the cache.
`expires` is the calculated timestamp for when the item should expire from the cache. This is
calculated when an item is set in the cache. If a value for `expires` is provided when writing
to the Stream API this value will be set in the cache.
If the items you want to store in the cache does not fit your data type, its recommended to use

@@ -382,4 +402,27 @@ a [Transform Stream](https://nodejs.org/api/stream.html#stream_implementing_a_transform_stream)

The [Entry Object](https://github.com/trygve-lie/ttl-mem-cache/blob/master/lib/entry.js) does
implement `.toPrimitive` where a stringified JSON representation of the Object will be returned
when call to the Entry Object with a String hint is done.
This can be used when one want to convert the Entry Object into a [Buffer](https://nodejs.org/api/buffer.html).
```js
const cache = new Cache();
const dest = new SomeWritableStream(); // Some destination supporting Buffers
const convert = new stream.Transform({
writableObjectMode: true,
readableObjectMode: false,
transform(obj, encoding, callback) {
const buff = Buffer.from(obj); // Convert Entry Object to a Buffer
this.push(buff);
callback();
}
});
cache.pipe(convert).pipe(dest);
```
## Changelog

@@ -386,0 +429,0 @@

@@ -6,2 +6,3 @@ 'use strict';

const tap = require('tap');
const os = require('os');

@@ -394,1 +395,51 @@ /**

});
/**
* .assertStrict()
*/
tap.test('.toPrimitive() - Call entry as String - should return JSON representation of the entry object', (t) => {
const clock = lolex.install();
clock.tick(1000);
const entry = new Entry({
key: 'a',
value: 'foo',
ttl: 2000,
origin: 'source',
});
t.equal(`${entry}`, `{"key":"a","value":"foo","ttl":2000,"origin":"source","expires":3000,"type":"TtlMemCacheEntry"}${os.EOL}`);
clock.uninstall();
t.end();
});
tap.test('.toPrimitive() - Call entry as String - should have EOL at the end of String', (t) => {
const entry = new Entry({
key: 'a',
value: 'foo',
ttl: 2000,
origin: 'source',
});
const result = `${entry}`;
t.equal(result.split('}')[1], os.EOL);
t.end();
});
tap.test('.toPrimitive() - Call entry as a non String - should return default representation', (t) => {
const entry = new Entry({
key: 'a',
value: 'foo',
ttl: 2000,
origin: 'source',
});
const result = +entry; // Call it as a number
t.type(result, 'number'); // Return value is NaN which is a number
t.equal(result.toString(), 'NaN');
t.end();
});
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