Comparing version 2.0.0 to 2.0.1
@@ -12,3 +12,4 @@ if (process.fiberLib) { | ||
try { | ||
fs.statSync(modPath+ '.node'); | ||
// Pull in fibers implementation | ||
process.fiberLib = module.exports = require(modPath).Fiber; | ||
} catch (ex) { | ||
@@ -21,7 +22,73 @@ // No binary! | ||
); | ||
console.error(ex.stack || ex.message || ex); | ||
throw new Error('Missing binary. See message above.'); | ||
} | ||
// Pull in fibers implementation | ||
process.fiberLib = module.exports = require(modPath).Fiber; | ||
setupAsyncHacks(module.exports); | ||
} | ||
function setupAsyncHacks(Fiber) { | ||
// Older (or newer?) versions of node may not support this API | ||
try { | ||
var aw = process.binding('async_wrap'); | ||
var getAsyncIdStackSize; | ||
if (aw.asyncIdStackSize instanceof Function) { | ||
getAsyncIdStackSize = aw.asyncIdStackSize; | ||
} else if (aw.constants.kStackLength !== undefined) { | ||
getAsyncIdStackSize = function(kStackLength) { | ||
return function() { | ||
return aw.async_hook_fields[kStackLength]; | ||
}; | ||
}(aw.constants.kStackLength); | ||
} else { | ||
throw new Error('Couldn\'t figure out how to get async stack size'); | ||
} | ||
if (!aw.popAsyncIds || !aw.pushAsyncIds) { | ||
throw new Error('Push/pop do not exist'); | ||
} | ||
var kExecutionAsyncId = aw.constants.kExecutionAsyncId; | ||
var kTriggerAsyncId = aw.constants.kTriggerAsyncId; | ||
function getAndClearStack() { | ||
var ii = getAsyncIdStackSize(); | ||
var stack = new Array(ii); | ||
for (; ii > 0; --ii) { | ||
var asyncId = aw.async_id_fields[kExecutionAsyncId]; | ||
stack[ii - 1] = { | ||
asyncId: asyncId, | ||
triggerId: aw.async_id_fields[kTriggerAsyncId], | ||
}; | ||
aw.popAsyncIds(asyncId); | ||
} | ||
return stack; | ||
} | ||
function restoreStack(stack) { | ||
for (var ii = 0; ii < stack.length; ++ii) { | ||
aw.pushAsyncIds(stack[ii].asyncId, stack[ii].triggerId); | ||
} | ||
} | ||
function wrapFunction(fn) { | ||
return function() { | ||
var stack = getAndClearStack(); | ||
try { | ||
return fn.apply(this, arguments); | ||
} finally { | ||
restoreStack(stack); | ||
} | ||
} | ||
} | ||
// Monkey patch methods which may long jump | ||
Fiber.yield = wrapFunction(Fiber.yield); | ||
Fiber.prototype.run = wrapFunction(Fiber.prototype.run); | ||
Fiber.prototype.throwInto = wrapFunction(Fiber.prototype.throwInto); | ||
} catch (err) { | ||
return; | ||
} | ||
} |
{ | ||
"name": "fibers", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "Cooperative multi-tasking for Javascript", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
fibers(1) -- Fiber support for v8 and Node | ||
========================================== | ||
[![npm version](https://img.shields.io/npm/v/fibers.svg)](https://www.npmjs.com/package/fibers) [![mit license](https://img.shields.io/npm/l/fibers.svg)](https://github.com/laverdet/node-fibers/blob/master/LICENSE) [![travis build](https://img.shields.io/travis/laverdet/node-fibers/master.svg)](https://travis-ci.org/laverdet/node-fibers) [![npm downloads](https://img.shields.io/npm/dw/fibers.svg)](https://www.npmjs.com/package/fibers) | ||
Fibers, sometimes called [coroutines](https://en.wikipedia.org/wiki/Coroutine), are a powerful tool which expose an API to jump between multiple call stacks from within a single thread. This can be useful to make code written for a synchronous library play nicely in an asynchronous environment. | ||
INSTALLING | ||
---------- | ||
[![NPM](https://nodei.co/npm/fibers.png)](https://www.npmjs.com/package/fibers) | ||
@@ -7,0 +11,0 @@ ### via npm |
11
test.js
@@ -13,7 +13,8 @@ #!/usr/bin/env node | ||
env.NODE_PATH = __dirname; | ||
var proc = spawn( | ||
process.execPath, | ||
[path.join('test', test)], | ||
{env: env} | ||
); | ||
var args = []; | ||
if (process.versions.modules >= 57) { | ||
args.push('--force-async-hooks-checks'); | ||
} | ||
args.push(path.join('test', test)); | ||
var proc = spawn(process.execPath, args, { env: env }); | ||
proc.stdout.setEncoding('utf8'); | ||
@@ -20,0 +21,0 @@ proc.stderr.setEncoding('utf8'); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1387902
1047
597
8