UNPKG

1.58 kBPlain TextView Raw
1import Fiber = require('fibers');
2export = result;
3
4
5// HOTFIX for node-fibers problem.
6// I have not prepared a repro unit test yet. In the meanwhile,
7// here is the gist of the problem:
8
9// A problem can occur if a node.js process has a node_modules tree that
10// contains multiple copies of node-fibers, whether the same version or not.
11// For instance, a project depends on two top-level modules which each
12// depend on node-fibers, such that 'npm install' installs two copies of
13// node-fibers. Furthermore, both of these copies are required() during execution.
14
15// In this scenario, the expected control flow of the process can be corrupted.
16// In one observed case, resuming a suspended fiber actually transfers control
17// to the code encapsulated in a completely different fiber.
18
19// The problem vanishes if we ensure that only one instance of node-fibers gets
20// used throughout the process. The following lines do this by caching a
21// node-fibers instance globally on first require(), and reusing that instance
22// for all subsequent require()s.
23
24// NB: This is a workaround, not a complete fix! If modules other than asyncawait
25// use node-fibers, then the process may still end up using multiple instances of
26// node-fibers during execution. This needs investigating in node-fibers itself.
27// I intend to create a cut-down repro and raise an issue in the node-fibers project.
28
29if (!global.asyncawait) global.asyncawait = {};
30if (!global.asyncawait.Fiber) global.asyncawait.Fiber = Fiber;
31var result: typeof Fiber = global.asyncawait.Fiber;