···1+# Changesets
2+3+Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
4+with multi-package repos, or single-package repos to help you version and publish your code. You can
5+find the full documentation for it [in our repository](https://github.com/changesets/changesets)
6+7+We have a quick list of common questions to get you started engaging with this project in
8+[our documentation](https://github.com/changesets/changesets/blob/master/docs/common-questions.md)
···1+MIT License
2+3+Copyright (c) Phil Pluckthun,
4+Copyright (c) 650 Industries, Inc. (aka Expo)
5+6+Permission is hereby granted, free of charge, to any person obtaining a copy
7+of this software and associated documentation files (the "Software"), to deal
8+in the Software without restriction, including without limitation the rights
9+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+copies of the Software, and to permit persons to whom the Software is
11+furnished to do so, subject to the following conditions:
12+13+The above copyright notice and this permission notice shall be included in all
14+copies or substantial portions of the Software.
15+16+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+SOFTWARE.
+3
README.md
···000
···1+# fiber-dev
2+3+Node.js isolation primitive to run asynchronous worker-like operations without leaking async IO
···1+import type {
2+ AsyncResourceFiber,
3+ AsyncResourceNode,
4+} from './asyncResourceGraph';
5+6+export type FiberErrorCode =
7+ | 'FOREIGN_ASYNC_TRIGGER'
8+ | 'PARENT_ASYNC_TRIGGER'
9+ | 'FOREIGN_ASYNC_ABORTED'
10+ | 'FIBER_ABORTED'
11+ | 'FIBER_STALL';
12+13+const codeToMessage = (
14+ code: FiberErrorCode,
15+ fiber: AsyncResourceFiber,
16+ node: AsyncResourceNode
17+): string => {
18+ switch (code) {
19+ case 'FOREIGN_ASYNC_TRIGGER':
20+ return (
21+ `${fiber} tried to create ${node} which will be triggered by async IO from a different fiber.\n` +
22+ 'Fibers are isolated and may only create and reference async resources they have created themselves.'
23+ );
24+ case 'PARENT_ASYNC_TRIGGER':
25+ return (
26+ `${fiber} tried to create ${node} which will be triggered by async IO of this fiber's parent context.\n` +
27+ 'Fibers are isolated and may only create and reference async resources they have created themselves.'
28+ );
29+ case 'FOREIGN_ASYNC_ABORTED':
30+ return (
31+ `${fiber} used ${node} from another fiber which was aborted and can never resolve.\n` +
32+ 'Fibers may not share async resources, and may accidentally prevent each other from resolving if they do.'
33+ );
34+ case 'FIBER_ABORTED':
35+ return (
36+ `${fiber}'s ${node} was aborted and will never resolve.\n` +
37+ "If you see this message, you're observing an internal forceful cancellation of a fiber and this error is expected."
38+ );
39+ case 'FIBER_STALL':
40+ return (
41+ `${fiber} has finished all of its work but won't resolve and pass control back to the parent fiber.\n` +
42+ 'This usally happens if a Promise is unresolved or if its async IO has been cancelled without a callback being handled.\n' +
43+ `${node} is the last async resource the fiber got stuck on.`
44+ );
45+ }
46+};
47+48+const traceNode = (
49+ node: AsyncResourceNode,
50+ fiber: AsyncResourceFiber,
51+ depth = 1
52+): string => {
53+ let trace = `${node}`;
54+ let origin: AsyncResourceNode | null = node;
55+ for (let idx = 1; origin && origin !== fiber.root && idx <= depth; idx++) {
56+ if (origin.frame) trace += `\n at ${origin.frame}`;
57+ origin = origin.executionOrigin;
58+ }
59+ return trace;
60+};
61+62+export class FiberError extends Error {
63+ static stackTraceLimit = 10;
64+65+ readonly fiber: AsyncResourceFiber;
66+ readonly node: AsyncResourceNode;
67+ readonly code: FiberErrorCode;
68+69+ constructor(
70+ code: FiberErrorCode,
71+ fiber: AsyncResourceFiber,
72+ node: AsyncResourceNode
73+ ) {
74+ super(codeToMessage(code, fiber, node));
75+ this.fiber = fiber;
76+ this.node = node;
77+ this.code = code;
78+ }
79+80+ get trace(): string {
81+ let trace = traceNode(this.node, this.fiber);
82+ if (this.node.triggerOrigin)
83+ trace += `\ntriggered by ${traceNode(this.node.triggerOrigin, this.fiber, FiberError.stackTraceLimit)}`;
84+ if (this.node.executionOrigin)
85+ trace += `\nexecuted in ${traceNode(this.node.executionOrigin, this.fiber, FiberError.stackTraceLimit)}`;
86+ return trace;
87+ }
88+89+ toString() {
90+ return `${this.message.trim()}\n\n${this.trace}`;
91+ }
92+}
+12
src/index.ts
···000000000000
···1+export type { StackFrame } from './utils';
2+export type * from './constants';
3+export type * from './errors';
4+export type * from './asyncResourceGraph';
5+6+export {
7+ getFiberNode,
8+ getFiber,
9+ fiber,
10+ enable,
11+ disable,
12+} from './asyncResourceGraph';