···11+# Changesets
22+33+Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
44+with multi-package repos, or single-package repos to help you version and publish your code. You can
55+find the full documentation for it [in our repository](https://github.com/changesets/changesets)
66+77+We have a quick list of common questions to get you started engaging with this project in
88+[our documentation](https://github.com/changesets/changesets/blob/master/docs/common-questions.md)
···11+MIT License
22+33+Copyright (c) Phil Pluckthun,
44+Copyright (c) 650 Industries, Inc. (aka Expo)
55+66+Permission is hereby granted, free of charge, to any person obtaining a copy
77+of this software and associated documentation files (the "Software"), to deal
88+in the Software without restriction, including without limitation the rights
99+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1010+copies of the Software, and to permit persons to whom the Software is
1111+furnished to do so, subject to the following conditions:
1212+1313+The above copyright notice and this permission notice shall be included in all
1414+copies or substantial portions of the Software.
1515+1616+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1717+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1818+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1919+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2020+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2121+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222+SOFTWARE.
+3
README.md
···11+# fiber-dev
22+33+Node.js isolation primitive to run asynchronous worker-like operations without leaking async IO
···11+import type {
22+ AsyncResourceFiber,
33+ AsyncResourceNode,
44+} from './asyncResourceGraph';
55+66+export type FiberErrorCode =
77+ | 'FOREIGN_ASYNC_TRIGGER'
88+ | 'PARENT_ASYNC_TRIGGER'
99+ | 'FOREIGN_ASYNC_ABORTED'
1010+ | 'FIBER_ABORTED'
1111+ | 'FIBER_STALL';
1212+1313+const codeToMessage = (
1414+ code: FiberErrorCode,
1515+ fiber: AsyncResourceFiber,
1616+ node: AsyncResourceNode
1717+): string => {
1818+ switch (code) {
1919+ case 'FOREIGN_ASYNC_TRIGGER':
2020+ return (
2121+ `${fiber} tried to create ${node} which will be triggered by async IO from a different fiber.\n` +
2222+ 'Fibers are isolated and may only create and reference async resources they have created themselves.'
2323+ );
2424+ case 'PARENT_ASYNC_TRIGGER':
2525+ return (
2626+ `${fiber} tried to create ${node} which will be triggered by async IO of this fiber's parent context.\n` +
2727+ 'Fibers are isolated and may only create and reference async resources they have created themselves.'
2828+ );
2929+ case 'FOREIGN_ASYNC_ABORTED':
3030+ return (
3131+ `${fiber} used ${node} from another fiber which was aborted and can never resolve.\n` +
3232+ 'Fibers may not share async resources, and may accidentally prevent each other from resolving if they do.'
3333+ );
3434+ case 'FIBER_ABORTED':
3535+ return (
3636+ `${fiber}'s ${node} was aborted and will never resolve.\n` +
3737+ "If you see this message, you're observing an internal forceful cancellation of a fiber and this error is expected."
3838+ );
3939+ case 'FIBER_STALL':
4040+ return (
4141+ `${fiber} has finished all of its work but won't resolve and pass control back to the parent fiber.\n` +
4242+ 'This usally happens if a Promise is unresolved or if its async IO has been cancelled without a callback being handled.\n' +
4343+ `${node} is the last async resource the fiber got stuck on.`
4444+ );
4545+ }
4646+};
4747+4848+const traceNode = (
4949+ node: AsyncResourceNode,
5050+ fiber: AsyncResourceFiber,
5151+ depth = 1
5252+): string => {
5353+ let trace = `${node}`;
5454+ let origin: AsyncResourceNode | null = node;
5555+ for (let idx = 1; origin && origin !== fiber.root && idx <= depth; idx++) {
5656+ if (origin.frame) trace += `\n at ${origin.frame}`;
5757+ origin = origin.executionOrigin;
5858+ }
5959+ return trace;
6060+};
6161+6262+export class FiberError extends Error {
6363+ static stackTraceLimit = 10;
6464+6565+ readonly fiber: AsyncResourceFiber;
6666+ readonly node: AsyncResourceNode;
6767+ readonly code: FiberErrorCode;
6868+6969+ constructor(
7070+ code: FiberErrorCode,
7171+ fiber: AsyncResourceFiber,
7272+ node: AsyncResourceNode
7373+ ) {
7474+ super(codeToMessage(code, fiber, node));
7575+ this.fiber = fiber;
7676+ this.node = node;
7777+ this.code = code;
7878+ }
7979+8080+ get trace(): string {
8181+ let trace = traceNode(this.node, this.fiber);
8282+ if (this.node.triggerOrigin)
8383+ trace += `\ntriggered by ${traceNode(this.node.triggerOrigin, this.fiber, FiberError.stackTraceLimit)}`;
8484+ if (this.node.executionOrigin)
8585+ trace += `\nexecuted in ${traceNode(this.node.executionOrigin, this.fiber, FiberError.stackTraceLimit)}`;
8686+ return trace;
8787+ }
8888+8989+ toString() {
9090+ return `${this.message.trim()}\n\n${this.trace}`;
9191+ }
9292+}
+12
src/index.ts
···11+export type { StackFrame } from './utils';
22+export type * from './constants';
33+export type * from './errors';
44+export type * from './asyncResourceGraph';
55+66+export {
77+ getFiberNode,
88+ getFiber,
99+ fiber,
1010+ enable,
1111+ disable,
1212+} from './asyncResourceGraph';