New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

continuation-local-storage

Package Overview
Dependencies
Maintainers
1
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

continuation-local-storage - npm Package Compare versions

Comparing version 2.6.2 to 3.0.0

CHANGELOG.md

24

context.js

@@ -21,4 +21,4 @@ 'use strict';

this.name = name;
// every namespace has a default / "global" context
this.active = Object.create(null);
// changed in 2.7: no default context
this.active = null;
this._set = [];

@@ -29,2 +29,6 @@ this.id = null;

Namespace.prototype.set = function (key, value) {
if (!this.active) {
throw new Error("No context available. ns.run() or ns.bind() must be called first.");
}
this.active[key] = value;

@@ -35,2 +39,4 @@ return value;

Namespace.prototype.get = function (key) {
if (!this.active) return undefined;
return this.active[key];

@@ -60,3 +66,11 @@ };

Namespace.prototype.bind = function (fn, context) {
if (!context) context = this.active;
if (!context) {
if (!this.active) {
context = Object.create(this.active);
}
else {
context = this.active;
}
}
var self = this;

@@ -158,4 +172,4 @@ return function () {

{
before : function (context, domain) { namespace.enter(domain); },
after : function (context, domain) { namespace.exit(domain); },
before : function (context, domain) { if (domain) namespace.enter(domain); },
after : function (context, domain) { if (domain) namespace.exit(domain); },
error : function (domain) { if (domain) namespace.exit(domain); }

@@ -162,0 +176,0 @@ }

{
"name": "continuation-local-storage",
"version": "2.6.2",
"version": "3.0.0",
"description": "userland implementation of https://github.com/joyent/node/issues/5243",

@@ -5,0 +5,0 @@ "main": "context.js",

@@ -80,4 +80,8 @@ [![NPM](https://nodei.co/npm/continuation-local-storage.png?downloads=true&stars=true)](https://nodei.co/npm/continuation-local-storage/)

var writer = createNamespace('writer');
writer.set('value', 0);
writer.run(function () {
writer.set('value', 0);
requestHandler();
});
function requestHandler() {

@@ -164,3 +168,4 @@ writer.run(function(outer) {

Set a value on the current continuation context.
Set a value on the current continuation context. Must be set within an active
continuation chain started with `namespace.run()` or `namespace.bind()`.

@@ -173,3 +178,4 @@ ### namespace.get(key)

the innermost to outermost nested continuation context for a value associated
with a given key.
with a given key. Must be set within an active continuation chain started with
`namespace.run()` or `namespace.bind()`.

@@ -192,3 +198,4 @@ ### namespace.run(callback)

`Function.bind()` or `domain.bind()`. If context is omitted, it will default to
the currently active context in the namespace.
the currently active context in the namespace, or create a new context if none
is currently defined.

@@ -195,0 +202,0 @@ ### namespace.bindEmitter(emitter)

@@ -14,4 +14,6 @@ 'use strict';

namespace.set('test', 1337);
t.equal(namespace.get('test'), 1337, "namespace is working");
namespace.run(function () {
namespace.set('test', 1337);
t.equal(namespace.get('test'), 1337, "namespace is working");
});
});

@@ -314,3 +314,3 @@ 'use strict';

t.test("emitter bound to multiple namespaces handles them correctly", function (t) {
t.plan(6);
t.plan(8);

@@ -327,5 +327,2 @@ var ee = new EventEmitter()

ns1.set('name', 'tom1');
ns2.set('name', 'tom2');
t.doesNotThrow(function () { ns1.bindEmitter(ee); });

@@ -335,12 +332,22 @@ t.doesNotThrow(function () { ns2.bindEmitter(ee); });

ns1.run(function () {
process.nextTick(function () {
t.equal(ns1.get('name'), 'tom1', "ns1 value correct");
t.equal(ns2.get('name'), 'tom2', "ns2 value correct");
ns2.run(function () {
ns1.set('name', 'tom1');
ns2.set('name', 'tom2');
ns1.set('name', 'bob');
ns2.set('name', 'alice');
t.doesNotThrow(function () { ns1.bindEmitter(ee); });
t.doesNotThrow(function () { ns2.bindEmitter(ee); });
ee.on('data', function () {
t.equal(ns1.get('name'), 'bob', "ns1 value bound onto emitter");
t.equal(ns2.get('name'), 'alice', "ns2 value bound onto emitter");
ns1.run(function () {
process.nextTick(function () {
t.equal(ns1.get('name'), 'tom1', "ns1 value correct");
t.equal(ns2.get('name'), 'tom2', "ns2 value correct");
ns1.set('name', 'bob');
ns2.set('name', 'alice');
ee.on('data', function () {
t.equal(ns1.get('name'), 'bob', "ns1 value bound onto emitter");
t.equal(ns2.get('name'), 'alice', "ns2 value bound onto emitter");
});
});
});

@@ -347,0 +354,0 @@ });

@@ -11,4 +11,4 @@ 'use strict';

catch (err) {}
if (crypto) {
test("continuation-local state with crypto.randomBytes", function (t) {

@@ -18,11 +18,13 @@ t.plan(1);

var namespace = createNamespace('namespace');
namespace.set('test', 0xabad1dea);
namespace.run(function () {
namespace.set('test', 0xabad1dea);
t.test("deflate", function (t) {
namespace.run(function () {
namespace.set('test', 42);
crypto.randomBytes(100, function (err, bytes) {
if (err) throw err;
t.equal(namespace.get('test'), 42, "mutated state was preserved");
t.end();
t.test("deflate", function (t) {
namespace.run(function () {
namespace.set('test', 42);
crypto.randomBytes(100, function (err) {
if (err) throw err;
t.equal(namespace.get('test'), 42, "mutated state was preserved");
t.end();
});
});

@@ -37,11 +39,13 @@ });

var namespace = createNamespace('namespace');
namespace.set('test', 0xabad1dea);
namespace.run(function () {
namespace.set('test', 0xabad1dea);
t.test("deflate", function (t) {
namespace.run(function () {
namespace.set('test', 42);
crypto.pseudoRandomBytes(100, function (err, bytes) {
if (err) throw err;
t.equal(namespace.get('test'), 42, "mutated state was preserved");
t.end();
t.test("deflate", function (t) {
namespace.run(function () {
namespace.set('test', 42);
crypto.pseudoRandomBytes(100, function (err) {
if (err) throw err;
t.equal(namespace.get('test'), 42, "mutated state was preserved");
t.end();
});
});

@@ -56,11 +60,13 @@ });

var namespace = createNamespace('namespace');
namespace.set('test', 0xabad1dea);
namespace.run(function () {
namespace.set('test', 0xabad1dea);
t.test("deflate", function (t) {
namespace.run(function () {
namespace.set('test', 42);
crypto.pbkdf2("s3cr3tz", "451243", 10, 40, function (err, key) {
if (err) throw err;
t.equal(namespace.get('test'), 42, "mutated state was preserved");
t.end();
t.test("deflate", function (t) {
namespace.run(function () {
namespace.set('test', 42);
crypto.pbkdf2("s3cr3tz", "451243", 10, 40, function (err) {
if (err) throw err;
t.equal(namespace.get('test'), 42, "mutated state was preserved");
t.end();
});
});

@@ -70,4 +76,2 @@ });

});
}

@@ -13,190 +13,192 @@ 'use strict';

var namespace = createNamespace('dns');
namespace.set('test', 0xabad1dea);
namespace.run(function () {
namespace.set('test', 0xabad1dea);
t.test("dns.lookup", function (t) {
namespace.run(function () {
namespace.set('test', 808);
t.equal(namespace.get('test'), 808, "state has been mutated");
t.test("dns.lookup", function (t) {
namespace.run(function () {
namespace.set('test', 808);
t.equal(namespace.get('test'), 808, "state has been mutated");
dns.lookup('www.newrelic.com', 4, function (err, addresses) {
t.notOk(err, "lookup succeeded");
t.ok(addresses.length > 0, "some results were found");
dns.lookup('www.newrelic.com', 4, function (err, addresses) {
t.notOk(err, "lookup succeeded");
t.ok(addresses.length > 0, "some results were found");
t.equal(namespace.get('test'), 808,
"mutated state has persisted to dns.lookup's callback");
t.equal(namespace.get('test'), 808,
"mutated state has persisted to dns.lookup's callback");
t.end();
t.end();
});
});
});
});
t.test("dns.resolve", function (t) {
namespace.run(function () {
namespace.set('test', 909);
t.equal(namespace.get('test'), 909, "state has been mutated");
t.test("dns.resolve", function (t) {
namespace.run(function () {
namespace.set('test', 909);
t.equal(namespace.get('test'), 909, "state has been mutated");
dns.resolve('newrelic.com', 'NS', function (err, addresses) {
t.notOk(err, "lookup succeeded");
t.ok(addresses.length > 0, "some results were found");
dns.resolve('newrelic.com', 'NS', function (err, addresses) {
t.notOk(err, "lookup succeeded");
t.ok(addresses.length > 0, "some results were found");
t.equal(namespace.get('test'), 909,
"mutated state has persisted to dns.resolve's callback");
t.equal(namespace.get('test'), 909,
"mutated state has persisted to dns.resolve's callback");
t.end();
t.end();
});
});
});
});
t.test("dns.resolve4", function (t) {
namespace.run(function () {
namespace.set('test', 303);
t.equal(namespace.get('test'), 303, "state has been mutated");
t.test("dns.resolve4", function (t) {
namespace.run(function () {
namespace.set('test', 303);
t.equal(namespace.get('test'), 303, "state has been mutated");
dns.resolve4('www.newrelic.com', function (err, addresses) {
t.notOk(err, "lookup succeeded");
t.ok(addresses.length > 0, "some results were found");
dns.resolve4('www.newrelic.com', function (err, addresses) {
t.notOk(err, "lookup succeeded");
t.ok(addresses.length > 0, "some results were found");
t.equal(namespace.get('test'), 303,
"mutated state has persisted to dns.resolve4's callback");
t.equal(namespace.get('test'), 303,
"mutated state has persisted to dns.resolve4's callback");
t.end();
t.end();
});
});
});
});
t.test("dns.resolve6", function (t) {
namespace.run(function () {
namespace.set('test', 101);
t.equal(namespace.get('test'), 101, "state has been mutated");
t.test("dns.resolve6", function (t) {
namespace.run(function () {
namespace.set('test', 101);
t.equal(namespace.get('test'), 101, "state has been mutated");
dns.resolve6('google.com', function (err, addresses) {
t.notOk(err, "lookup succeeded");
t.ok(addresses.length > 0, "some results were found");
dns.resolve6('google.com', function (err, addresses) {
t.notOk(err, "lookup succeeded");
t.ok(addresses.length > 0, "some results were found");
t.equal(namespace.get('test'), 101,
"mutated state has persisted to dns.resolve6's callback");
t.equal(namespace.get('test'), 101,
"mutated state has persisted to dns.resolve6's callback");
t.end();
t.end();
});
});
});
});
t.test("dns.resolveCname", function (t) {
namespace.run(function () {
namespace.set('test', 212);
t.equal(namespace.get('test'), 212, "state has been mutated");
t.test("dns.resolveCname", function (t) {
namespace.run(function () {
namespace.set('test', 212);
t.equal(namespace.get('test'), 212, "state has been mutated");
dns.resolveCname('mail.newrelic.com', function (err, addresses) {
t.notOk(err, "lookup succeeded");
t.ok(addresses.length > 0, "some results were found");
dns.resolveCname('mail.newrelic.com', function (err, addresses) {
t.notOk(err, "lookup succeeded");
t.ok(addresses.length > 0, "some results were found");
t.equal(namespace.get('test'), 212,
"mutated state has persisted to dns.resolveCname's callback");
t.equal(namespace.get('test'), 212,
"mutated state has persisted to dns.resolveCname's callback");
t.end();
t.end();
});
});
});
});
t.test("dns.resolveMx", function (t) {
namespace.run(function () {
namespace.set('test', 707);
t.equal(namespace.get('test'), 707, "state has been mutated");
t.test("dns.resolveMx", function (t) {
namespace.run(function () {
namespace.set('test', 707);
t.equal(namespace.get('test'), 707, "state has been mutated");
dns.resolveMx('newrelic.com', function (err, addresses) {
t.notOk(err, "lookup succeeded");
t.ok(addresses.length > 0, "some results were found");
dns.resolveMx('newrelic.com', function (err, addresses) {
t.notOk(err, "lookup succeeded");
t.ok(addresses.length > 0, "some results were found");
t.equal(namespace.get('test'), 707,
"mutated state has persisted to dns.resolveMx's callback");
t.equal(namespace.get('test'), 707,
"mutated state has persisted to dns.resolveMx's callback");
t.end();
t.end();
});
});
});
});
t.test("dns.resolveNs", function (t) {
namespace.run(function () {
namespace.set('test', 717);
t.equal(namespace.get('test'), 717, "state has been mutated");
t.test("dns.resolveNs", function (t) {
namespace.run(function () {
namespace.set('test', 717);
t.equal(namespace.get('test'), 717, "state has been mutated");
dns.resolveNs('newrelic.com', function (err, addresses) {
t.notOk(err, "lookup succeeded");
t.ok(addresses.length > 0, "some results were found");
dns.resolveNs('newrelic.com', function (err, addresses) {
t.notOk(err, "lookup succeeded");
t.ok(addresses.length > 0, "some results were found");
t.equal(namespace.get('test'), 717,
"mutated state has persisted to dns.resolveNs's callback");
t.equal(namespace.get('test'), 717,
"mutated state has persisted to dns.resolveNs's callback");
t.end();
t.end();
});
});
});
});
t.test("dns.resolveTxt", function (t) {
namespace.run(function () {
namespace.set('test', 2020);
t.equal(namespace.get('test'), 2020, "state has been mutated");
t.test("dns.resolveTxt", function (t) {
namespace.run(function () {
namespace.set('test', 2020);
t.equal(namespace.get('test'), 2020, "state has been mutated");
dns.resolveTxt('newrelic.com', function (err, addresses) {
t.notOk(err, "lookup succeeded");
t.ok(addresses.length > 0, "some results were found");
dns.resolveTxt('newrelic.com', function (err, addresses) {
t.notOk(err, "lookup succeeded");
t.ok(addresses.length > 0, "some results were found");
t.equal(namespace.get('test'), 2020,
"mutated state has persisted to dns.resolveTxt's callback");
t.equal(namespace.get('test'), 2020,
"mutated state has persisted to dns.resolveTxt's callback");
t.end();
t.end();
});
});
});
});
t.test("dns.resolveSrv", function (t) {
namespace.run(function () {
namespace.set('test', 9000);
t.equal(namespace.get('test'), 9000, "state has been mutated");
t.test("dns.resolveSrv", function (t) {
namespace.run(function () {
namespace.set('test', 9000);
t.equal(namespace.get('test'), 9000, "state has been mutated");
dns.resolveSrv('_xmpp-server._tcp.google.com', function (err, addresses) {
t.notOk(err, "lookup succeeded");
t.ok(addresses.length > 0, "some results were found");
dns.resolveSrv('_xmpp-server._tcp.google.com', function (err, addresses) {
t.notOk(err, "lookup succeeded");
t.ok(addresses.length > 0, "some results were found");
t.equal(namespace.get('test'), 9000,
"mutated state has persisted to dns.resolveSrv's callback");
t.equal(namespace.get('test'), 9000,
"mutated state has persisted to dns.resolveSrv's callback");
t.end();
t.end();
});
});
});
});
t.test("dns.resolveNaptr", function (t) {
// dns.resolveNaptr only in Node > 0.9.x
if (!dns.resolveNaptr) return t.end();
t.test("dns.resolveNaptr", function (t) {
// dns.resolveNaptr only in Node > 0.9.x
if (!dns.resolveNaptr) return t.end();
namespace.run(function () {
namespace.set('test', 'Polysix');
t.equal(namespace.get('test'), 'Polysix', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'Polysix');
t.equal(namespace.get('test'), 'Polysix', "state has been mutated");
dns.resolveNaptr('columbia.edu', function (err, addresses) {
t.notOk(err, "lookup succeeded");
t.ok(addresses.length > 0, "some results were found");
dns.resolveNaptr('columbia.edu', function (err, addresses) {
t.notOk(err, "lookup succeeded");
t.ok(addresses.length > 0, "some results were found");
t.equal(namespace.get('test'), 'Polysix',
"mutated state has persisted to dns.resolveNaptr's callback");
t.equal(namespace.get('test'), 'Polysix',
"mutated state has persisted to dns.resolveNaptr's callback");
t.end();
t.end();
});
});
});
});
t.test("dns.reverse", function (t) {
namespace.run(function () {
namespace.set('test', 1000);
t.equal(namespace.get('test'), 1000, "state has been mutated");
t.test("dns.reverse", function (t) {
namespace.run(function () {
namespace.set('test', 1000);
t.equal(namespace.get('test'), 1000, "state has been mutated");
dns.reverse('204.93.223.144', function (err, addresses) {
t.notOk(err, "lookup succeeded");
t.ok(addresses.length > 0, "some results were found");
dns.reverse('204.93.223.144', function (err, addresses) {
t.notOk(err, "lookup succeeded");
t.ok(addresses.length > 0, "some results were found");
t.equal(namespace.get('test'), 1000,
"mutated state has persisted to dns.reverse's callback");
t.equal(namespace.get('test'), 1000,
"mutated state has persisted to dns.reverse's callback");
t.end();
t.end();
});
});

@@ -203,0 +205,0 @@ });

@@ -42,16 +42,19 @@ 'use strict';

var namespace = cls.createNamespace('cls@synchronous');
namespace.set('value', 'transaction clear');
try {
namespace.run(function () {
namespace.set('value', 'transaction set');
throw new Error('cls@synchronous explosion');
});
}
catch (e) {
t.ok(namespace.fromException(e), "context was attached to error");
t.equal(namespace.fromException(e)['value'], 'transaction set',
"found the inner value");
}
namespace.run(function () {
namespace.set('value', 'transaction clear');
try {
namespace.run(function () {
namespace.set('value', 'transaction set');
throw new Error('cls@synchronous explosion');
});
}
catch (e) {
t.ok(namespace.fromException(e), "context was attached to error");
t.equal(namespace.fromException(e)['value'], 'transaction set',
"found the inner value");
}
t.equal(namespace.get('value'), 'transaction clear', "everything was reset");
t.equal(namespace.get('value'), 'transaction clear', "everything was reset");
});
cls.destroyNamespace('cls@synchronous');

@@ -64,5 +67,4 @@ });

var namespace = cls.createNamespace('cls@nexttick');
var d = domain.create();
namespace.set('value', 'transaction clear');
d.on('error', function (e) {

@@ -76,11 +78,15 @@ t.ok(namespace.fromException(e), "context was attached to error");

// tap is only trying to help
process.nextTick(d.bind(function () {
namespace.run(function () {
namespace.set('value', 'transaction set');
throw new Error("cls@nexttick explosion");
});
}));
namespace.run(function () {
namespace.set('value', 'transaction clear');
t.equal(namespace.get('value'), 'transaction clear', "everything was reset");
// tap is only trying to help
process.nextTick(d.bind(function () {
namespace.run(function () {
namespace.set('value', 'transaction set');
throw new Error("cls@nexttick explosion");
});
}));
t.equal(namespace.get('value'), 'transaction clear', "everything was reset");
});
});

@@ -93,3 +99,2 @@

var d = domain.create();
namespace.set('value', 'transaction clear');

@@ -104,11 +109,15 @@ d.on('error', function (e) {

// tap is only trying to help
setTimeout(d.bind(function () {
namespace.run(function () {
namespace.set('value', 'transaction set');
throw new Error("cls@nexttick explosion");
});
}));
namespace.run(function () {
namespace.set('value', 'transaction clear');
t.equal(namespace.get('value'), 'transaction clear', "everything was reset");
// tap is only trying to help
setTimeout(d.bind(function () {
namespace.run(function () {
namespace.set('value', 'transaction set');
throw new Error("cls@nexttick explosion");
});
}));
t.equal(namespace.get('value'), 'transaction clear', "everything was reset");
});
});

@@ -80,801 +80,804 @@ 'use strict';

var namespace = createNamespace('fs');
namespace.set('test', 0xabad1dea);
namespace.run(function () {
namespace.set('test', 0xabad1dea);
t.test("fs.rename", function (t) {
createFile(t);
t.test("fs.rename", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'rename');
t.equal(namespace.get('test'), 'rename', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'rename');
t.equal(namespace.get('test'), 'rename', "state has been mutated");
fs.rename(FILENAME, '__renamed', function (error) {
t.notOk(error, "renaming shouldn't error");
t.equal(namespace.get('test'), 'rename',
"mutated state has persisted to fs.rename's callback");
fs.rename(FILENAME, '__renamed', function (error) {
t.notOk(error, "renaming shouldn't error");
t.equal(namespace.get('test'), 'rename',
"mutated state has persisted to fs.rename's callback");
fs.unlinkSync('__renamed');
t.end();
fs.unlinkSync('__renamed');
t.end();
});
});
});
});
t.test("fs.truncate", function (t) {
// truncate -> ftruncate in Node > 0.8.x
if (!fs.ftruncate) return t.end();
t.test("fs.truncate", function (t) {
// truncate -> ftruncate in Node > 0.8.x
if (!fs.ftruncate) return t.end();
createFile(t);
createFile(t);
namespace.run(function () {
namespace.set('test', 'truncate');
t.equal(namespace.get('test'), 'truncate', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'truncate');
t.equal(namespace.get('test'), 'truncate', "state has been mutated");
fs.truncate(FILENAME, 0, function (error) {
t.notOk(error, "truncation shouldn't error");
fs.truncate(FILENAME, 0, function (error) {
t.notOk(error, "truncation shouldn't error");
var stats = fs.statSync(FILENAME);
t.equal(stats.size, 0, "file has been truncated");
var stats = fs.statSync(FILENAME);
t.equal(stats.size, 0, "file has been truncated");
t.equal(namespace.get('test'), 'truncate',
"mutated state has persisted to fs.truncate's callback");
t.equal(namespace.get('test'), 'truncate',
"mutated state has persisted to fs.truncate's callback");
deleteFile();
t.end();
deleteFile();
t.end();
});
});
});
});
t.test("fs.ftruncate", function (t) {
createFile(t);
t.test("fs.ftruncate", function (t) {
createFile(t);
// truncate -> ftruncate in Node > 0.8.x
var truncate = fs.ftruncate ? fs.ftruncate : fs.truncate;
// truncate -> ftruncate in Node > 0.8.x
var truncate = fs.ftruncate ? fs.ftruncate : fs.truncate;
namespace.run(function () {
namespace.set('test', 'ftruncate');
t.equal(namespace.get('test'), 'ftruncate', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'ftruncate');
t.equal(namespace.get('test'), 'ftruncate', "state has been mutated");
var file = fs.openSync(FILENAME, 'w');
truncate(file, 0, function (error) {
t.notOk(error, "truncation shouldn't error");
var file = fs.openSync(FILENAME, 'w');
truncate(file, 0, function (error) {
t.notOk(error, "truncation shouldn't error");
fs.closeSync(file);
var stats = fs.statSync(FILENAME);
t.equal(stats.size, 0, "file has been truncated");
fs.closeSync(file);
var stats = fs.statSync(FILENAME);
t.equal(stats.size, 0, "file has been truncated");
t.equal(namespace.get('test'), 'ftruncate',
"mutated state has persisted to fs.ftruncate's callback");
t.equal(namespace.get('test'), 'ftruncate',
"mutated state has persisted to fs.ftruncate's callback");
deleteFile();
t.end();
deleteFile();
t.end();
});
});
});
});
t.test("fs.chown", function (t) {
createFile(t);
t.test("fs.chown", function (t) {
createFile(t);
mapIds('daemon', 'daemon', function (error, uid, gid) {
t.notOk(error, "looking up uid & gid shouldn't error");
t.ok(uid, "uid for daemon was found");
t.ok(gid, "gid for daemon was found");
mapIds('daemon', 'daemon', function (error, uid, gid) {
t.notOk(error, "looking up uid & gid shouldn't error");
t.ok(uid, "uid for daemon was found");
t.ok(gid, "gid for daemon was found");
namespace.run(function () {
namespace.set('test', 'chown');
t.equal(namespace.get('test'), 'chown', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'chown');
t.equal(namespace.get('test'), 'chown', "state has been mutated");
fs.chown(FILENAME, uid, gid, function (error) {
t.ok(error, "changing ownership will error for non-root users");
fs.chown(FILENAME, uid, gid, function (error) {
t.ok(error, "changing ownership will error for non-root users");
t.equal(namespace.get('test'), 'chown',
"mutated state has persisted to fs.chown's callback");
t.equal(namespace.get('test'), 'chown',
"mutated state has persisted to fs.chown's callback");
deleteFile();
t.end();
deleteFile();
t.end();
});
});
});
});
});
t.test("fs.fchown", function (t) {
createFile(t);
t.test("fs.fchown", function (t) {
createFile(t);
mapIds('daemon', 'daemon', function (error, uid, gid) {
t.notOk(error, "looking up uid & gid shouldn't error");
t.ok(uid, "uid for daemon was found");
t.ok(gid, "gid for daemon was found");
mapIds('daemon', 'daemon', function (error, uid, gid) {
t.notOk(error, "looking up uid & gid shouldn't error");
t.ok(uid, "uid for daemon was found");
t.ok(gid, "gid for daemon was found");
namespace.run(function () {
namespace.set('test', 'fchown');
t.equal(namespace.get('test'), 'fchown', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'fchown');
t.equal(namespace.get('test'), 'fchown', "state has been mutated");
var file = fs.openSync(FILENAME, 'w');
fs.fchown(file, uid, gid, function (error) {
t.ok(error, "changing ownership will error for non-root users");
var file = fs.openSync(FILENAME, 'w');
fs.fchown(file, uid, gid, function (error) {
t.ok(error, "changing ownership will error for non-root users");
t.equal(namespace.get('test'), 'fchown',
"mutated state has persisted to fs.fchown's callback");
t.equal(namespace.get('test'), 'fchown',
"mutated state has persisted to fs.fchown's callback");
fs.closeSync(file);
deleteFile();
t.end();
fs.closeSync(file);
deleteFile();
t.end();
});
});
});
});
});
t.test("fs.lchown", function (t) {
if (!fs.lchown) return t.end();
createLink(t);
t.test("fs.lchown", function (t) {
if (!fs.lchown) return t.end();
createLink(t);
mapIds('daemon', 'daemon', function (error, uid, gid) {
t.notOk(error, "looking up uid & gid shouldn't error");
t.ok(uid, "uid for daemon was found");
t.ok(gid, "gid for daemon was found");
mapIds('daemon', 'daemon', function (error, uid, gid) {
t.notOk(error, "looking up uid & gid shouldn't error");
t.ok(uid, "uid for daemon was found");
t.ok(gid, "gid for daemon was found");
namespace.run(function () {
namespace.set('test', 'lchown');
t.equal(namespace.get('test'), 'lchown', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'lchown');
t.equal(namespace.get('test'), 'lchown', "state has been mutated");
fs.lchown(LINKNAME, uid, gid, function (error) {
t.ok(error, "changing ownership will error for non-root users");
fs.lchown(LINKNAME, uid, gid, function (error) {
t.ok(error, "changing ownership will error for non-root users");
t.equal(namespace.get('test'), 'lchown',
"mutated state has persisted to fs.lchown's callback");
t.equal(namespace.get('test'), 'lchown',
"mutated state has persisted to fs.lchown's callback");
deleteLink();
t.end();
deleteLink();
t.end();
});
});
});
});
});
t.test("fs.chmod", function (t) {
createFile(t);
t.test("fs.chmod", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'chmod');
t.equal(namespace.get('test'), 'chmod', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'chmod');
t.equal(namespace.get('test'), 'chmod', "state has been mutated");
fs.chmod(FILENAME, '0700', function (error) {
t.notOk(error, "changing mode shouldn't error");
fs.chmod(FILENAME, '0700', function (error) {
t.notOk(error, "changing mode shouldn't error");
t.equal(namespace.get('test'), 'chmod',
"mutated state has persisted to fs.chmod's callback");
t.equal(namespace.get('test'), 'chmod',
"mutated state has persisted to fs.chmod's callback");
var stats = fs.statSync(FILENAME);
t.equal(stats.mode.toString(8), '100700', "extra access bits are stripped");
var stats = fs.statSync(FILENAME);
t.equal(stats.mode.toString(8), '100700', "extra access bits are stripped");
deleteFile();
t.end();
deleteFile();
t.end();
});
});
});
});
t.test("fs.fchmod", function (t) {
createFile(t);
t.test("fs.fchmod", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'fchmod');
t.equal(namespace.get('test'), 'fchmod', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'fchmod');
t.equal(namespace.get('test'), 'fchmod', "state has been mutated");
var file = fs.openSync(FILENAME, 'w+');
fs.fchmod(file, '0700', function (error) {
t.notOk(error, "changing mode shouldn't error");
var file = fs.openSync(FILENAME, 'w+');
fs.fchmod(file, '0700', function (error) {
t.notOk(error, "changing mode shouldn't error");
t.equal(namespace.get('test'), 'fchmod',
"mutated state has persisted to fs.fchmod's callback");
t.equal(namespace.get('test'), 'fchmod',
"mutated state has persisted to fs.fchmod's callback");
fs.closeSync(file);
var stats = fs.statSync(FILENAME);
t.equal(stats.mode.toString(8), '100700', "extra access bits are stripped");
fs.closeSync(file);
var stats = fs.statSync(FILENAME);
t.equal(stats.mode.toString(8), '100700', "extra access bits are stripped");
deleteFile();
t.end();
deleteFile();
t.end();
});
});
});
});
t.test("fs.lchmod", function (t) {
if (!fs.lchmod) return t.end();
createLink(t);
t.test("fs.lchmod", function (t) {
if (!fs.lchmod) return t.end();
createLink(t);
namespace.run(function () {
namespace.set('test', 'lchmod');
t.equal(namespace.get('test'), 'lchmod', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'lchmod');
t.equal(namespace.get('test'), 'lchmod', "state has been mutated");
fs.lchmod(LINKNAME, '0700', function (error) {
t.notOk(error, "changing mode shouldn't error");
fs.lchmod(LINKNAME, '0700', function (error) {
t.notOk(error, "changing mode shouldn't error");
t.equal(namespace.get('test'), 'lchmod',
"mutated state has persisted to fs.lchmod's callback");
t.equal(namespace.get('test'), 'lchmod',
"mutated state has persisted to fs.lchmod's callback");
var stats = fs.lstatSync(LINKNAME);
t.equal(stats.mode.toString(8), '120700', "extra access bits are stripped");
var stats = fs.lstatSync(LINKNAME);
t.equal(stats.mode.toString(8), '120700', "extra access bits are stripped");
deleteLink();
t.end();
deleteLink();
t.end();
});
});
});
});
t.test("fs.stat", function (t) {
createFile(t);
t.test("fs.stat", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'stat');
t.equal(namespace.get('test'), 'stat', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'stat');
t.equal(namespace.get('test'), 'stat', "state has been mutated");
fs.stat(FILENAME, function (error, stats) {
t.notOk(error, "reading stats shouldn't error");
fs.stat(FILENAME, function (error, stats) {
t.notOk(error, "reading stats shouldn't error");
t.equal(namespace.get('test'), 'stat',
"mutated state has persisted to fs.stat's callback");
t.equal(namespace.get('test'), 'stat',
"mutated state has persisted to fs.stat's callback");
t.equal(stats.mode.toString(8), '100666', "permissions should be as created");
t.equal(stats.mode.toString(8), '100666', "permissions should be as created");
deleteFile();
t.end();
deleteFile();
t.end();
});
});
});
});
t.test("fs.fstat", function (t) {
createFile(t);
t.test("fs.fstat", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'fstat');
t.equal(namespace.get('test'), 'fstat', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'fstat');
t.equal(namespace.get('test'), 'fstat', "state has been mutated");
var file = fs.openSync(FILENAME, 'r');
fs.fstat(file, function (error, stats) {
t.notOk(error, "reading stats shouldn't error");
var file = fs.openSync(FILENAME, 'r');
fs.fstat(file, function (error, stats) {
t.notOk(error, "reading stats shouldn't error");
t.equal(namespace.get('test'), 'fstat',
"mutated state has persisted to fs.fstat's callback");
t.equal(namespace.get('test'), 'fstat',
"mutated state has persisted to fs.fstat's callback");
t.equal(stats.mode.toString(8), '100666', "permissions should be as created");
t.equal(stats.mode.toString(8), '100666', "permissions should be as created");
fs.closeSync(file);
deleteFile();
t.end();
fs.closeSync(file);
deleteFile();
t.end();
});
});
});
});
t.test("fs.lstat", function (t) {
createLink(t);
t.test("fs.lstat", function (t) {
createLink(t);
namespace.run(function () {
namespace.set('test', 'lstat');
t.equal(namespace.get('test'), 'lstat', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'lstat');
t.equal(namespace.get('test'), 'lstat', "state has been mutated");
fs.lstat(LINKNAME, function (error, stats) {
t.notOk(error, "reading stats shouldn't error");
fs.lstat(LINKNAME, function (error, stats) {
t.notOk(error, "reading stats shouldn't error");
t.equal(namespace.get('test'), 'lstat',
"mutated state has persisted to fs.lstat's callback");
t.equal(namespace.get('test'), 'lstat',
"mutated state has persisted to fs.lstat's callback");
t.equal(stats.mode.toString(8), '120777', "permissions should be as created");
t.equal(stats.mode.toString(8), '120777', "permissions should be as created");
deleteLink();
t.end();
deleteLink();
t.end();
});
});
});
});
t.test("fs.link", function (t) {
createFile(t);
t.test("fs.link", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'link');
t.equal(namespace.get('test'), 'link', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'link');
t.equal(namespace.get('test'), 'link', "state has been mutated");
fs.link(FILENAME, HARDLINKNAME, function (error) {
t.notOk(error, "creating a link shouldn't error");
fs.link(FILENAME, HARDLINKNAME, function (error) {
t.notOk(error, "creating a link shouldn't error");
t.equal(namespace.get('test'), 'link',
"mutated state has persisted to fs.link's callback");
t.equal(namespace.get('test'), 'link',
"mutated state has persisted to fs.link's callback");
var orig = fs.statSync(FILENAME)
, linked = fs.statSync(HARDLINKNAME)
;
t.equal(orig.ino, linked.ino, "entries should point to same file");
var orig = fs.statSync(FILENAME)
, linked = fs.statSync(HARDLINKNAME)
;
t.equal(orig.ino, linked.ino, "entries should point to same file");
t.notOk(fs.unlinkSync(HARDLINKNAME), "link has been removed");
deleteFile();
t.end();
t.notOk(fs.unlinkSync(HARDLINKNAME), "link has been removed");
deleteFile();
t.end();
});
});
});
});
t.test("fs.symlink", function (t) {
createFile(t);
t.test("fs.symlink", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'symlink');
t.equal(namespace.get('test'), 'symlink', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'symlink');
t.equal(namespace.get('test'), 'symlink', "state has been mutated");
fs.symlink(FILENAME, LINKNAME, function (error) {
t.notOk(error, "creating a symlink shouldn't error");
fs.symlink(FILENAME, LINKNAME, function (error) {
t.notOk(error, "creating a symlink shouldn't error");
t.equal(namespace.get('test'), 'symlink',
"mutated state has persisted to fs.symlink's callback");
t.equal(namespace.get('test'), 'symlink',
"mutated state has persisted to fs.symlink's callback");
var pointed = fs.readlinkSync(LINKNAME);
t.equal(pointed, FILENAME, "symlink points back to original file");
var pointed = fs.readlinkSync(LINKNAME);
t.equal(pointed, FILENAME, "symlink points back to original file");
t.notOk(fs.unlinkSync(LINKNAME), "symlink has been removed");
deleteFile();
t.end();
t.notOk(fs.unlinkSync(LINKNAME), "symlink has been removed");
deleteFile();
t.end();
});
});
});
});
t.test("fs.readlink", function (t) {
createLink(t);
t.test("fs.readlink", function (t) {
createLink(t);
namespace.run(function () {
namespace.set('test', 'readlink');
t.equal(namespace.get('test'), 'readlink', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'readlink');
t.equal(namespace.get('test'), 'readlink', "state has been mutated");
fs.readlink(LINKNAME, function (error, pointed) {
t.notOk(error, "reading symlink shouldn't error");
fs.readlink(LINKNAME, function (error, pointed) {
t.notOk(error, "reading symlink shouldn't error");
t.equal(namespace.get('test'), 'readlink',
"mutated state has persisted to fs.readlink's callback");
t.equal(namespace.get('test'), 'readlink',
"mutated state has persisted to fs.readlink's callback");
t.equal(pointed, FILENAME, "symlink points back to original file");
t.equal(pointed, FILENAME, "symlink points back to original file");
deleteLink();
t.end();
deleteLink();
t.end();
});
});
});
});
t.test("fs.unlink", function (t) {
createFile(t);
t.test("fs.unlink", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'unlink');
t.equal(namespace.get('test'), 'unlink', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'unlink');
t.equal(namespace.get('test'), 'unlink', "state has been mutated");
fs.unlink(FILENAME, function (error) {
t.notOk(error, "deleting file shouldn't error");
fs.unlink(FILENAME, function (error) {
t.notOk(error, "deleting file shouldn't error");
t.equal(namespace.get('test'), 'unlink',
"mutated state has persisted to fs.unlink's callback");
t.equal(namespace.get('test'), 'unlink',
"mutated state has persisted to fs.unlink's callback");
t.notOk(fs.exists(FILENAME), "file should be gone");
t.end();
t.notOk(fs.exists(FILENAME), "file should be gone");
t.end();
});
});
});
});
t.test("fs.realpath", function (t) {
createFile(t);
t.test("fs.realpath", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'realpath');
t.equal(namespace.get('test'), 'realpath', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'realpath');
t.equal(namespace.get('test'), 'realpath', "state has been mutated");
fs.realpath(FILENAME, function (error, real) {
t.notOk(error, "deleting file shouldn't error");
fs.realpath(FILENAME, function (error, real) {
t.notOk(error, "deleting file shouldn't error");
t.equal(namespace.get('test'), 'realpath',
"mutated state has persisted to fs.realpath's callback");
t.equal(namespace.get('test'), 'realpath',
"mutated state has persisted to fs.realpath's callback");
t.equal(real, path.resolve(FILENAME), "no funny business with the real path");
t.equal(real, path.resolve(FILENAME), "no funny business with the real path");
deleteFile();
t.end();
deleteFile();
t.end();
});
});
});
});
t.test("fs.mkdir", function (t) {
namespace.run(function () {
namespace.set('test', 'mkdir');
t.equal(namespace.get('test'), 'mkdir', "state has been mutated");
t.test("fs.mkdir", function (t) {
namespace.run(function () {
namespace.set('test', 'mkdir');
t.equal(namespace.get('test'), 'mkdir', "state has been mutated");
fs.mkdir(DIRNAME, function (error) {
t.notOk(error, "creating directory shouldn't error");
fs.mkdir(DIRNAME, function (error) {
t.notOk(error, "creating directory shouldn't error");
t.equal(namespace.get('test'), 'mkdir',
"mutated state has persisted to fs.mkdir's callback");
t.equal(namespace.get('test'), 'mkdir',
"mutated state has persisted to fs.mkdir's callback");
t.ok(fs.existsSync(DIRNAME), "directory was created");
t.ok(fs.existsSync(DIRNAME), "directory was created");
fs.rmdirSync(DIRNAME);
t.end();
fs.rmdirSync(DIRNAME);
t.end();
});
});
});
});
t.test("fs.rmdir", function (t) {
createDirectory(t);
t.test("fs.rmdir", function (t) {
createDirectory(t);
namespace.run(function () {
namespace.set('test', 'rmdir');
t.equal(namespace.get('test'), 'rmdir', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'rmdir');
t.equal(namespace.get('test'), 'rmdir', "state has been mutated");
fs.rmdir(DIRNAME, function (error) {
t.notOk(error, "deleting directory shouldn't error");
fs.rmdir(DIRNAME, function (error) {
t.notOk(error, "deleting directory shouldn't error");
t.equal(namespace.get('test'), 'rmdir',
"mutated state has persisted to fs.rmdir's callback");
t.equal(namespace.get('test'), 'rmdir',
"mutated state has persisted to fs.rmdir's callback");
t.notOk(fs.existsSync(DIRNAME), "directory was removed");
t.notOk(fs.existsSync(DIRNAME), "directory was removed");
t.end();
t.end();
});
});
});
});
t.test("fs.readdir", function (t) {
createDirectory(t);
t.test("fs.readdir", function (t) {
createDirectory(t);
var file1 = fs.openSync(path.join(DIRNAME, 'file1'), 'w');
fs.writeSync(file1, 'one');
fs.closeSync(file1);
var file1 = fs.openSync(path.join(DIRNAME, 'file1'), 'w');
fs.writeSync(file1, 'one');
fs.closeSync(file1);
var file2 = fs.openSync(path.join(DIRNAME, 'file2'), 'w');
fs.writeSync(file2, 'two');
fs.closeSync(file2);
var file2 = fs.openSync(path.join(DIRNAME, 'file2'), 'w');
fs.writeSync(file2, 'two');
fs.closeSync(file2);
var file3 = fs.openSync(path.join(DIRNAME, 'file3'), 'w');
fs.writeSync(file3, 'three');
fs.closeSync(file3);
var file3 = fs.openSync(path.join(DIRNAME, 'file3'), 'w');
fs.writeSync(file3, 'three');
fs.closeSync(file3);
namespace.run(function () {
namespace.set('test', 'readdir');
t.equal(namespace.get('test'), 'readdir', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'readdir');
t.equal(namespace.get('test'), 'readdir', "state has been mutated");
fs.readdir(DIRNAME, function (error, contents) {
t.notOk(error, "reading directory shouldn't error");
fs.readdir(DIRNAME, function (error, contents) {
t.notOk(error, "reading directory shouldn't error");
t.equal(namespace.get('test'), 'readdir',
"mutated state has persisted to fs.readdir's callback");
t.equal(namespace.get('test'), 'readdir',
"mutated state has persisted to fs.readdir's callback");
t.equal(contents.length, 3, "3 files were found");
t.equal(contents.length, 3, "3 files were found");
fs.unlinkSync(path.join(DIRNAME, 'file1'));
fs.unlinkSync(path.join(DIRNAME, 'file2'));
fs.unlinkSync(path.join(DIRNAME, 'file3'));
deleteDirectory();
t.end();
fs.unlinkSync(path.join(DIRNAME, 'file1'));
fs.unlinkSync(path.join(DIRNAME, 'file2'));
fs.unlinkSync(path.join(DIRNAME, 'file3'));
deleteDirectory();
t.end();
});
});
});
});
t.test("fs.watch", function (t) {
createFile(t);
t.test("fs.watch", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'watch');
t.equal(namespace.get('test'), 'watch', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'watch');
t.equal(namespace.get('test'), 'watch', "state has been mutated");
var watcher = fs.watch(FILENAME,
{persistent : false, interval : 200},
function (event) {
t.equal(namespace.get('test'), 'watch',
"mutated state has persisted to fs.watch's callback");
var watcher = fs.watch(FILENAME,
{persistent : false, interval : 200},
function (event) {
t.equal(namespace.get('test'), 'watch',
"mutated state has persisted to fs.watch's callback");
t.equal(event, 'change', "file was changed");
t.equal(event, 'change', "file was changed");
watcher.close();
process.nextTick(function cleanup() {
deleteFile();
t.end();
watcher.close();
process.nextTick(function cleanup() {
deleteFile();
t.end();
});
});
});
process.nextTick(function poke() {
fs.writeFileSync(FILENAME, 'still a test');
process.nextTick(function poke() {
fs.writeFileSync(FILENAME, 'still a test');
});
});
});
});
t.test("fs.watchFile", function (t) {
createFile(t);
t.test("fs.watchFile", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'watchFile');
t.equal(namespace.get('test'), 'watchFile', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'watchFile');
t.equal(namespace.get('test'), 'watchFile', "state has been mutated");
fs.watchFile(FILENAME,
{persistent : false, interval : 200},
function (before, after) {
t.equal(namespace.get('test'), 'watchFile',
"mutated state has persisted to fs.watchFile's callback");
fs.watchFile(FILENAME,
{persistent : false, interval : 200},
function (before, after) {
t.equal(namespace.get('test'), 'watchFile',
"mutated state has persisted to fs.watchFile's callback");
t.ok(before.ino, "file has an entry");
t.equal(before.ino, after.ino, "file is at the same location");
t.ok(before.ino, "file has an entry");
t.equal(before.ino, after.ino, "file is at the same location");
fs.unwatchFile(FILENAME);
process.nextTick(function () {
deleteFile();
t.end();
fs.unwatchFile(FILENAME);
process.nextTick(function () {
deleteFile();
t.end();
});
});
});
process.nextTick(function poke() {
fs.writeFileSync(FILENAME, 'still a test');
process.nextTick(function poke() {
fs.writeFileSync(FILENAME, 'still a test');
});
});
});
});
t.test("fs.utimes", function (t) {
createFile(t);
t.test("fs.utimes", function (t) {
createFile(t);
/* utimes(2) takes seconds since the epoch, and Date() deals with
* milliseconds. I just want a date some time in the past.
*/
var PASTIME = new Date(Math.floor((Date.now() - 31337) / 1000) * 1000);
/* utimes(2) takes seconds since the epoch, and Date() deals with
* milliseconds. I just want a date some time in the past.
*/
var PASTIME = new Date(Math.floor((Date.now() - 31337) / 1000) * 1000);
namespace.run(function () {
namespace.set('test', 'utimes');
t.equal(namespace.get('test'), 'utimes', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'utimes');
t.equal(namespace.get('test'), 'utimes', "state has been mutated");
var before = fs.statSync(FILENAME);
t.ok(before.atime, "access time of newly-created file set");
t.ok(before.mtime, "modification time of newly-created file set");
var before = fs.statSync(FILENAME);
t.ok(before.atime, "access time of newly-created file set");
t.ok(before.mtime, "modification time of newly-created file set");
fs.utimes(FILENAME, PASTIME, PASTIME, function (error) {
t.notOk(error, "setting utimes shouldn't error");
fs.utimes(FILENAME, PASTIME, PASTIME, function (error) {
t.notOk(error, "setting utimes shouldn't error");
t.equal(namespace.get('test'), 'utimes',
"mutated state has persisted to fs.utimes's callback");
t.equal(namespace.get('test'), 'utimes',
"mutated state has persisted to fs.utimes's callback");
var after = fs.statSync(FILENAME);
t.deepEqual(after.atime, PASTIME, "access time of newly-created file is reset");
t.deepEqual(after.mtime, PASTIME, "mod time of newly-created file is reset");
t.notDeepEqual(before.atime, after.atime, "access time changed");
t.notDeepEqual(before.mtime, after.mtime, "mod time changed");
var after = fs.statSync(FILENAME);
t.deepEqual(after.atime, PASTIME, "access time of newly-created file is reset");
t.deepEqual(after.mtime, PASTIME, "mod time of newly-created file is reset");
t.notDeepEqual(before.atime, after.atime, "access time changed");
t.notDeepEqual(before.mtime, after.mtime, "mod time changed");
deleteFile();
t.end();
deleteFile();
t.end();
});
});
});
});
t.test("fs.futimes", function (t) {
createFile(t);
t.test("fs.futimes", function (t) {
createFile(t);
/* futimes(2) takes seconds since the epoch, and Date() deals with
* milliseconds. I just want a date some time in the past.
*/
var PASTIME = new Date(Math.floor((Date.now() - 0xb33fd) / 1000) * 1000);
/* futimes(2) takes seconds since the epoch, and Date() deals with
* milliseconds. I just want a date some time in the past.
*/
var PASTIME = new Date(Math.floor((Date.now() - 0xb33fd) / 1000) * 1000);
namespace.run(function () {
namespace.set('test', 'futimes');
t.equal(namespace.get('test'), 'futimes', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'futimes');
t.equal(namespace.get('test'), 'futimes', "state has been mutated");
var before = fs.statSync(FILENAME);
t.ok(before.atime, "access time of newly-created file set");
t.ok(before.mtime, "modification time of newly-created file set");
var before = fs.statSync(FILENAME);
t.ok(before.atime, "access time of newly-created file set");
t.ok(before.mtime, "modification time of newly-created file set");
var file = fs.openSync(FILENAME, "w+");
fs.futimes(file, PASTIME, PASTIME, function (error) {
t.notOk(error, "setting futimes shouldn't error");
fs.closeSync(file);
var file = fs.openSync(FILENAME, "w+");
fs.futimes(file, PASTIME, PASTIME, function (error) {
t.notOk(error, "setting futimes shouldn't error");
fs.closeSync(file);
t.equal(namespace.get('test'), 'futimes',
"mutated state has persisted to fs.futimes's callback");
t.equal(namespace.get('test'), 'futimes',
"mutated state has persisted to fs.futimes's callback");
var after = fs.statSync(FILENAME);
t.deepEqual(after.atime, PASTIME, "access time of newly-created file is reset");
t.deepEqual(after.mtime, PASTIME, "mod time of newly-created file is reset");
t.notDeepEqual(before.atime, after.atime, "access time changed");
t.notDeepEqual(before.mtime, after.mtime, "mod time changed");
var after = fs.statSync(FILENAME);
t.deepEqual(after.atime, PASTIME, "access time of newly-created file is reset");
t.deepEqual(after.mtime, PASTIME, "mod time of newly-created file is reset");
t.notDeepEqual(before.atime, after.atime, "access time changed");
t.notDeepEqual(before.mtime, after.mtime, "mod time changed");
deleteFile();
t.end();
deleteFile();
t.end();
});
});
});
});
t.test("fs.fsync", function (t) {
createFile(t);
t.test("fs.fsync", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'fsync');
t.equal(namespace.get('test'), 'fsync', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'fsync');
t.equal(namespace.get('test'), 'fsync', "state has been mutated");
var file = fs.openSync(FILENAME, 'w+');
fs.fsync(file, function (error) {
t.notOk(error, "syncing the file shouldn't error");
var file = fs.openSync(FILENAME, 'w+');
fs.fsync(file, function (error) {
t.notOk(error, "syncing the file shouldn't error");
t.equal(namespace.get('test'), 'fsync',
"mutated state has persisted to fs.fsync's callback");
t.equal(namespace.get('test'), 'fsync',
"mutated state has persisted to fs.fsync's callback");
fs.closeSync(file);
deleteFile();
t.end();
fs.closeSync(file);
deleteFile();
t.end();
});
});
});
});
t.test("fs.open", function (t) {
createFile(t);
t.test("fs.open", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'open');
t.equal(namespace.get('test'), 'open', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'open');
t.equal(namespace.get('test'), 'open', "state has been mutated");
fs.open(FILENAME, 'r', function (error, file) {
t.notOk(error, "opening the file shouldn't error");
fs.open(FILENAME, 'r', function (error, file) {
t.notOk(error, "opening the file shouldn't error");
t.equal(namespace.get('test'), 'open',
"mutated state has persisted to fs.open's callback");
t.equal(namespace.get('test'), 'open',
"mutated state has persisted to fs.open's callback");
var contents = new Buffer(4);
fs.readSync(file, contents, 0, 4, 0);
t.equal(contents.toString(), 'UHOH', "contents are still available");
var contents = new Buffer(4);
fs.readSync(file, contents, 0, 4, 0);
t.equal(contents.toString(), 'UHOH', "contents are still available");
fs.closeSync(file);
deleteFile();
t.end();
fs.closeSync(file);
deleteFile();
t.end();
});
});
});
});
t.test("fs.close", function (t) {
createFile(t);
t.test("fs.close", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'close');
t.equal(namespace.get('test'), 'close', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'close');
t.equal(namespace.get('test'), 'close', "state has been mutated");
var file = fs.openSync(FILENAME, 'r');
fs.close(file, function (error) {
t.notOk(error, "closing the file shouldn't error");
var file = fs.openSync(FILENAME, 'r');
fs.close(file, function (error) {
t.notOk(error, "closing the file shouldn't error");
t.equal(namespace.get('test'), 'close',
"mutated state has persisted to fs.close's callback");
t.equal(namespace.get('test'), 'close',
"mutated state has persisted to fs.close's callback");
deleteFile();
t.end();
deleteFile();
t.end();
});
});
});
});
t.test("fs.read", function (t) {
createFile(t);
t.test("fs.read", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'read');
t.equal(namespace.get('test'), 'read', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'read');
t.equal(namespace.get('test'), 'read', "state has been mutated");
var file = fs.openSync(FILENAME, 'r')
, contents = new Buffer(4)
;
fs.read(file, contents, 0, 4, 0, function (error) {
t.notOk(error, "reading from the file shouldn't error");
var file = fs.openSync(FILENAME, 'r')
, contents = new Buffer(4)
;
fs.read(file, contents, 0, 4, 0, function (error) {
t.notOk(error, "reading from the file shouldn't error");
t.equal(namespace.get('test'), 'read',
"mutated state has persisted to fs.read's callback");
t.equal(namespace.get('test'), 'read',
"mutated state has persisted to fs.read's callback");
t.equal(contents.toString(), 'UHOH', "contents are still available");
t.equal(contents.toString(), 'UHOH', "contents are still available");
fs.closeSync(file);
deleteFile();
t.end();
fs.closeSync(file);
deleteFile();
t.end();
});
});
});
});
t.test("fs.write", function (t) {
createFile(t);
t.test("fs.write", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'write');
t.equal(namespace.get('test'), 'write', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'write');
t.equal(namespace.get('test'), 'write', "state has been mutated");
var file = fs.openSync(FILENAME, 'w')
, contents = new Buffer('yeap')
;
fs.write(file, contents, 0, 4, 0, function (error) {
t.notOk(error, "writing to the file shouldn't error");
var file = fs.openSync(FILENAME, 'w')
, contents = new Buffer('yeap')
;
fs.write(file, contents, 0, 4, 0, function (error) {
t.notOk(error, "writing to the file shouldn't error");
t.equal(namespace.get('test'), 'write',
"mutated state has persisted to fs.write's callback");
t.equal(namespace.get('test'), 'write',
"mutated state has persisted to fs.write's callback");
fs.closeSync(file);
fs.closeSync(file);
var readback = fs.readFileSync(FILENAME);
t.equal(readback.toString(), 'yeap', "contents are still available");
var readback = fs.readFileSync(FILENAME);
t.equal(readback.toString(), 'yeap', "contents are still available");
deleteFile();
t.end();
deleteFile();
t.end();
});
});
});
});
t.test("fs.readFile", function (t) {
createFile(t);
t.test("fs.readFile", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'readFile');
t.equal(namespace.get('test'), 'readFile', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'readFile');
t.equal(namespace.get('test'), 'readFile', "state has been mutated");
fs.readFile(FILENAME, function (error, contents) {
t.notOk(error, "reading from the file shouldn't error");
fs.readFile(FILENAME, function (error, contents) {
t.notOk(error, "reading from the file shouldn't error");
t.equal(namespace.get('test'), 'readFile',
"mutated state has persisted to fs.readFile's callback");
t.equal(namespace.get('test'), 'readFile',
"mutated state has persisted to fs.readFile's callback");
t.equal(contents.toString(), 'UHOH', "contents are still available");
t.equal(contents.toString(), 'UHOH', "contents are still available");
deleteFile();
t.end();
deleteFile();
t.end();
});
});
});
});
t.test("fs.writeFile", function (t) {
createFile(t);
t.test("fs.writeFile", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'writeFile');
t.equal(namespace.get('test'), 'writeFile', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'writeFile');
t.equal(namespace.get('test'), 'writeFile', "state has been mutated");
fs.writeFile(FILENAME, 'woopwoop', function (error) {
t.notOk(error, "rewriting the file shouldn't error");
fs.writeFile(FILENAME, 'woopwoop', function (error) {
t.notOk(error, "rewriting the file shouldn't error");
t.equal(namespace.get('test'), 'writeFile',
"mutated state has persisted to fs.writeFile's callback");
t.equal(namespace.get('test'), 'writeFile',
"mutated state has persisted to fs.writeFile's callback");
var readback = fs.readFileSync(FILENAME);
t.equal(readback.toString(), 'woopwoop', "rewritten contents are available");
var readback = fs.readFileSync(FILENAME);
t.equal(readback.toString(), 'woopwoop', "rewritten contents are available");
deleteFile();
t.end();
deleteFile();
t.end();
});
});
});
});
t.test("fs.appendFile", function (t) {
createFile(t);
t.test("fs.appendFile", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'appendFile');
t.equal(namespace.get('test'), 'appendFile', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'appendFile');
t.equal(namespace.get('test'), 'appendFile', "state has been mutated");
fs.appendFile(FILENAME, '/jk', function (error) {
t.notOk(error, "appending to the file shouldn't error");
fs.appendFile(FILENAME, '/jk', function (error) {
t.notOk(error, "appending to the file shouldn't error");
t.equal(namespace.get('test'), 'appendFile',
"mutated state has persisted to fs.appendFile's callback");
t.equal(namespace.get('test'), 'appendFile',
"mutated state has persisted to fs.appendFile's callback");
var readback = fs.readFileSync(FILENAME);
t.equal(readback.toString(), 'UHOH/jk', "appended contents are still available");
var readback = fs.readFileSync(FILENAME);
t.equal(readback.toString(), 'UHOH/jk',
"appended contents are still available");
deleteFile();
t.end();
deleteFile();
t.end();
});
});
});
});
t.test("fs.exists", function (t) {
createFile(t);
t.test("fs.exists", function (t) {
createFile(t);
namespace.run(function () {
namespace.set('test', 'exists');
t.equal(namespace.get('test'), 'exists', "state has been mutated");
namespace.run(function () {
namespace.set('test', 'exists');
t.equal(namespace.get('test'), 'exists', "state has been mutated");
fs.exists(FILENAME, function (yep) {
t.equal(namespace.get('test'), 'exists',
"mutated state has persisted to fs.exists's callback");
fs.exists(FILENAME, function (yep) {
t.equal(namespace.get('test'), 'exists',
"mutated state has persisted to fs.exists's callback");
t.ok(yep, "precreated file does indeed exist.");
t.ok(yep, "precreated file does indeed exist.");
fs.exists('NOPENOWAY', function (nope) {
t.equal(namespace.get('test'), 'exists',
"mutated state continues to persist to fs.exists's second callback");
fs.exists('NOPENOWAY', function (nope) {
t.equal(namespace.get('test'), 'exists',
"mutated state continues to persist to fs.exists's second callback");
t.notOk(nope, "nonexistent file doesn't exist.");
t.notOk(nope, "nonexistent file doesn't exist.");
deleteFile();
t.end();
deleteFile();
t.end();
});
});

@@ -881,0 +884,0 @@ });

@@ -39,34 +39,36 @@ 'use strict';

var writer = cls.createNamespace('writer');
writer.set('value', 0);
writer.run(function () {
writer.set('value', 0);
t.equal(writer.get('value'), 0, "outer hasn't been entered yet");
function requestHandler() {
writer.run(function (outer) {
t.equal(writer.active, outer, "writer.active == outer");
t.equal(writer.get('value'), 0, "outer hasn't been entered yet");
function requestHandler() {
writer.run(function (outer) {
t.equal(writer.active, outer, "writer.active == outer");
writer.set('value', 1);
t.equal(writer.get('value'), 1, "writer.active == outer");
t.equal(outer.value, 1, "outer is active");
writer.set('value', 1);
t.equal(writer.get('value'), 1, "writer.active == outer");
t.equal(outer.value, 1, "outer is active");
process.nextTick(function () {
t.equal(writer.active, outer, "writer.active == outer");
t.equal(writer.get('value'), 1, "inner has been entered");
writer.run(function (inner) {
t.equal(writer.active, inner, "writer.active == inner");
process.nextTick(function () {
t.equal(writer.active, outer, "writer.active == outer");
t.equal(writer.get('value'), 1, "inner has been entered");
writer.run(function (inner) {
t.equal(writer.active, inner, "writer.active == inner");
writer.set('value', 2);
t.equal(outer.value, 1, "outer is unchanged");
t.equal(inner.value, 2, "inner is active");
t.equal(writer.get('value'), 2, "writer.active == inner");
writer.set('value', 2);
t.equal(outer.value, 1, "outer is unchanged");
t.equal(inner.value, 2, "inner is active");
t.equal(writer.get('value'), 2, "writer.active == inner");
});
});
});
});
setTimeout(function () {
t.equal(writer.get('value'), 0, "writer.active == global");
t.end();
}, 100);
}
setTimeout(function () {
t.equal(writer.get('value'), 0, "writer.active == global");
t.end();
}, 100);
}
requestHandler();
requestHandler();
});
});

@@ -13,26 +13,29 @@ 'use strict';

var namespace = createNamespace('net');
namespace.set('test', 0xabad1dea);
var server;
namespace.run(function () {
namespace.set('test', 0x1337);
namespace.set('test', 0xabad1dea);
server = net.createServer(function (socket) {
t.equal(namespace.get('test'), 0x1337, "state has been mutated");
socket.on("data", function (chunk) {
t.equal(namespace.get('test'), 0x1337, "state is still preserved");
server.close();
socket.end("GoodBye");
var server;
namespace.run(function () {
namespace.set('test', 0x1337);
server = net.createServer(function (socket) {
t.equal(namespace.get('test'), 0x1337, "state has been mutated");
socket.on("data", function () {
t.equal(namespace.get('test'), 0x1337, "state is still preserved");
server.close();
socket.end("GoodBye");
});
});
});
server.listen(function () {
var address = server.address();
namespace.run(function () {
namespace.set("test", "MONKEY");
var client = net.connect(address.port, function () {
t.equal(namespace.get("test"), "MONKEY", "state preserved for client connection");
client.write("Hello");
client.on("data", function (chunk) {
t.equal(namespace.get("test"), "MONKEY", "state preserved for client data");
t.end();
server.listen(function () {
var address = server.address();
namespace.run(function () {
namespace.set("test", "MONKEY");
var client = net.connect(address.port, function () {
t.equal(namespace.get("test"), "MONKEY",
"state preserved for client connection");
client.write("Hello");
client.on("data", function () {
t.equal(namespace.get("test"), "MONKEY", "state preserved for client data");
t.end();
});
});

@@ -39,0 +42,0 @@ });

@@ -12,63 +12,66 @@ 'use strict';

var namespace = createNamespace('namespace');
namespace.set('test', 0xabad1dea);
namespace.run(function () {
namespace.set('test', 0xabad1dea);
t.test("process.nextTick", function (t) {
namespace.run(function () {
namespace.set('test', 31337);
t.equal(namespace.get('test'), 31337, "state has been mutated");
t.test("process.nextTick", function (t) {
namespace.run(function () {
namespace.set('test', 31337);
t.equal(namespace.get('test'), 31337, "state has been mutated");
process.nextTick(function () {
t.equal(namespace.get('test'), 31337,
"mutated state has persisted to process.nextTick's callback");
process.nextTick(function () {
t.equal(namespace.get('test'), 31337,
"mutated state has persisted to process.nextTick's callback");
t.end();
t.end();
});
});
});
});
t.test("setImmediate", function (t) {
// setImmediate only in Node > 0.9.x
if (!global.setImmediate) return t.end();
t.test("setImmediate", function (t) {
// setImmediate only in Node > 0.9.x
if (!global.setImmediate) return t.end();
namespace.run(function () {
namespace.set('test', 999);
t.equal(namespace.get('test'), 999, "state has been mutated");
namespace.run(function () {
namespace.set('test', 999);
t.equal(namespace.get('test'), 999, "state has been mutated");
setImmediate(function () {
t.equal(namespace.get('test'), 999,
"mutated state has persisted to setImmediate's callback");
setImmediate(function () {
t.equal(namespace.get('test'), 999,
"mutated state has persisted to setImmediate's callback");
t.end();
t.end();
});
});
});
});
t.test("setTimeout", function (t) {
namespace.run(function () {
namespace.set('test', 54321);
t.equal(namespace.get('test'), 54321, "state has been mutated");
t.test("setTimeout", function (t) {
namespace.run(function () {
namespace.set('test', 54321);
t.equal(namespace.get('test'), 54321, "state has been mutated");
setTimeout(function () {
t.equal(namespace.get('test'), 54321,
"mutated state has persisted to setTimeout's callback");
setTimeout(function () {
t.equal(namespace.get('test'), 54321,
"mutated state has persisted to setTimeout's callback");
t.end();
t.end();
});
});
});
});
t.test("setInterval", function (t) {
namespace.run(function () {
namespace.set('test', 10101);
t.equal(namespace.get('test'), 10101, "continuation-local state has been mutated");
var ref = setInterval(function () {
t.test("setInterval", function (t) {
namespace.run(function () {
namespace.set('test', 10101);
t.equal(namespace.get('test'), 10101,
"mutated state has persisted to setInterval's callback");
"continuation-local state has been mutated");
clearInterval(ref);
t.end();
}, 20);
var ref = setInterval(function () {
t.equal(namespace.get('test'), 10101,
"mutated state has persisted to setInterval's callback");
clearInterval(ref);
t.end();
}, 20);
});
});
});
});

@@ -14,11 +14,13 @@ 'use strict';

var namespace = createNamespace('namespace');
namespace.set('test', 0xabad1dea);
namespace.run(function () {
namespace.set('test', 0xabad1dea);
t.test("deflate", function (t) {
namespace.run(function () {
namespace.set('test', 42);
zlib.deflate(new Buffer("Goodbye World"), function (err) {
if (err) throw err;
t.equal(namespace.get('test'), 42, "mutated state was preserved");
t.end();
t.test("deflate", function (t) {
namespace.run(function () {
namespace.set('test', 42);
zlib.deflate(new Buffer("Goodbye World"), function (err) {
if (err) throw err;
t.equal(namespace.get('test'), 42, "mutated state was preserved");
t.end();
});
});

@@ -25,0 +27,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