@ariestools/threads

中文文档
9.0.3发布于 2026/11/06MITTypeScriptES Module近 30 天下载 1.1万

Web workers & worker threads as simple as a function call

原始使用文档
文档目录110 节
  1. Install
  2. License
  3. Reference
  4. packages
  5. threads
  6. Call Signature
  7. Parameters
  8. transferable
  9. Returns
  10. See
  11. Call Signature
  12. Type Parameters
  13. T
  14. Parameters
  15. payload
  16. transferables
  17. Returns
  18. See
  19. Returns
  20. Parameters
  21. serializer
  22. Returns
  23. Type Parameters
  24. Exposed
  25. Parameters
  26. worker
  27. options?
  28. timeout?
  29. Returns
  30. Type Parameters
  31. ThreadType
  32. Methods
  33. completed()
  34. Parameters
  35. Returns
  36. settled()
  37. Parameters
  38. Returns
  39. events()
  40. Returns
  41. queue()
  42. Type Parameters
  43. Parameters
  44. Returns
  45. terminate()
  46. Parameters
  47. Returns
  48. Type Parameters
  49. ThreadType
  50. Return
  51. Properties
  52. then
  53. Type Parameters
  54. Parameters
  55. Returns
  56. Methods
  57. cancel()
  58. Returns
  59. Type Parameters
  60. Msg
  61. Input
  62. Methods
  63. deserialize()
  64. Parameters
  65. Returns
  66. serialize()
  67. Parameters
  68. Returns
  69. Type Parameters
  70. Msg
  71. Input
  72. Methods
  73. deserialize()
  74. Parameters
  75. Returns
  76. serialize()
  77. Parameters
  78. Returns
  79. Type Parameters
  80. T
  81. Properties
  82. [$transferable]
  83. send
  84. transferables
  85. Type Parameters
  86. ThreadType
  87. Type Parameters
  88. Exposed
  89. Type Parameters
  90. Args
  91. ReturnType
  92. Type Parameters
  93. Methods
  94. Type Declaration
  95. EventType
  96. Type Declaration
  97. errors()
  98. Type Parameters
  99. Parameters
  100. Returns
  101. events()
  102. Type Parameters
  103. Parameters
  104. Returns
  105. terminate()
  106. Type Parameters
  107. Parameters
  108. Returns
  109. Returns
  110. Credits

logo

@ariestools/threads

npm license

Web workers & worker threads as simple as a function call

Internal status: public-only package with zero consumers inside this monorepo. The dead.package.internal / dead.repo.internal ignores and coverage excludes for this package are intentional.

Install

Using npm:

npm install {{name}}

Using yarn:

yarn add {{name}}

Using pnpm:

pnpm add {{name}}

Using bun:

bun add {{name}}

License

See the LICENSE file for license rights and limitations (LGPL-3.0-only).

Reference

packages

threads

### .temp-typedoc

  ### index-browser

    ### functions

      ### <a id="Transfer"></a>Transfer

@ariestools/threads


Call Signature

function Transfer(transferable): TransferDescriptor;

Mark a transferable object as such, so it will no be serialized and deserialized on messaging with the main thread, but to transfer ownership of it to the receiving thread.

Only works with array buffers, message ports and few more special types of objects, but it's much faster than serializing and deserializing them.

Note: The transferable object cannot be accessed by this thread again unless the receiving thread transfers it back again!

Parameters

transferable

Transferable

Array buffer, message port or similar.

Returns

TransferDescriptor

See

https://developers.google.com/web/updates/2011/12/Transferable-Objects-Lightning-Fast

Call Signature

function Transfer<T>(payload, transferables): TransferDescriptor;

Mark transferable objects within an arbitrary object or array as being a transferable object. They will then not be serialized and deserialized on messaging with the main thread, but ownership of them will be tranferred to the receiving thread.

Only array buffers, message ports and few more special types of objects can be transferred, but it's much faster than serializing and deserializing them.

Note: The transferable object cannot be accessed by this thread again unless the receiving thread transfers it back again!

Type Parameters

T

T

Parameters

payload

T

transferables

Transferable[]

Returns

TransferDescriptor

See

https://developers.google.com/web/updates/2011/12/Transferable-Objects-Lightning-Fast

      ### <a id="isWorkerRuntime"></a>isWorkerRuntime

@ariestools/threads


function isWorkerRuntime(): boolean;

Check whether the current code is running inside a web worker context.

Returns

boolean

True if running in a worker, false otherwise.

      ### <a id="registerSerializer"></a>registerSerializer

@ariestools/threads


function registerSerializer(serializer): void;

Register a custom serializer to extend the default serialization behavior for worker messages.

Parameters

serializer

SerializerImplementation<JsonSerializable>

The serializer implementation to register.

Returns

void

      ### <a id="spawn"></a>spawn

@ariestools/threads


function spawn<Exposed>(worker, options?): Promise<ExposedAs<Exposed>>;

Spawn a new thread. Takes a fresh worker instance, wraps it in a thin abstraction layer to provide the transparent API and verifies that the worker has initialized successfully.

Type Parameters

Exposed

Exposed extends WorkerFunction | WorkerModule<any> = ArbitraryWorkerInterface

Parameters

worker

Worker

Instance of Worker. Either a web worker or worker_threads worker.

options?

timeout?

number

Init message timeout. Default: 10000 or set by environment variable.

Returns

Promise<ExposedAs<Exposed>>

    ### interfaces

      ### <a id="Pool"></a>Pool

@ariestools/threads


Thread pool managing a set of worker threads. Use it to queue tasks that are run on those threads with limited concurrency.

Type Parameters

ThreadType

ThreadType extends Thread

Methods

completed()

completed(allowResolvingImmediately?): Promise<any>;

Returns a promise that resolves once the task queue is emptied. Promise will be rejected if any task fails.

Parameters

allowResolvingImmediately?

boolean

Set to true to resolve immediately if task queue is currently empty.

Returns

Promise<any>


settled()

settled(allowResolvingImmediately?): Promise<Error[]>;

Returns a promise that resolves once the task queue is emptied. Failing tasks will not cause the promise to be rejected.

Parameters

allowResolvingImmediately?

boolean

Set to true to resolve immediately if task queue is currently empty.

Returns

Promise<Error[]>


events()

events(): Observable<PoolEvent<ThreadType>>;

Returns an observable that yields pool events.

Returns

Observable<PoolEvent<ThreadType>>


queue()

queue<Return>(task): QueuedTask<ThreadType, Return>;

Queue a task and return a promise that resolves once the task has been dequeued, started and finished.

Type Parameters

Return

Return

Parameters

task

TaskRunFunction<ThreadType, Return>

An async function that takes a thread instance and invokes it.

Returns

QueuedTask<ThreadType, Return>


terminate()

terminate(force?): Promise<void>;

Terminate all pool threads.

Parameters

force?

boolean

Set to true to kill the thread even if it cannot be stopped gracefully.

Returns

Promise<void>

      ### <a id="QueuedTask"></a>QueuedTask

@ariestools/threads


Task that has been pool.queued()-ed.

Type Parameters

ThreadType

ThreadType extends Thread

Return

Return

Properties

then

then: <TResult1, TResult2>(onfulfilled?, onrejected?) => Promise<TResult1 | TResult2>;

QueuedTask is thenable, so you can await it. Resolves when the task has successfully been executed. Rejects if the task fails.

Attaches callbacks for the resolution and/or rejection of the Promise.

Type Parameters

TResult1

TResult1 = Return

TResult2

TResult2 = never

Parameters

onfulfilled?

((value) => TResult1 | PromiseLike<TResult1>) | null

The callback to execute when the Promise is resolved.

onrejected?

((reason) => TResult2 | PromiseLike<TResult2>) | null

The callback to execute when the Promise is rejected.

Returns

Promise<TResult1 | TResult2>

A Promise for the completion of which ever callback is executed.

Methods

cancel()

cancel(): void;

Queued tasks can be cancelled until the pool starts running them on a worker thread.

Returns

void

      ### <a id="Serializer"></a>Serializer

@ariestools/threads


A serializer that can convert between a message format and an input type.

Type Parameters

Msg

Msg = JsonSerializable

Input

Input = any

Methods

deserialize()

deserialize(message): Input;

Parameters

message

Msg

Returns

Input


serialize()

serialize(input): Msg;

Parameters

input

Input

Returns

Msg

      ### <a id="SerializerImplementation"></a>SerializerImplementation

@ariestools/threads


A serializer implementation that receives a fallback (default) serializer for chaining.

Type Parameters

Msg

Msg = JsonSerializable

Input

Input = any

Methods

deserialize()

deserialize(message, defaultDeserialize): Input;

Parameters

message

Msg

defaultDeserialize

(msg) => Input

Returns

Input


serialize()

serialize(input, defaultSerialize): Msg;

Parameters

input

Input

defaultSerialize

(inp) => Msg

Returns

Msg

      ### <a id="TransferDescriptor"></a>TransferDescriptor

@ariestools/threads


Descriptor wrapping a value with its associated transferable objects for zero-copy messaging.

Type Parameters

T

T = any

Properties

[$transferable]

[$transferable]: true;

send

send: T;

transferables

transferables: Transferable[];
    ### namespaces

      ### Pool

        ### type-aliases

          ### <a id="Event"></a>Event

@ariestools/threads


type Event<ThreadType> = PoolEvent<ThreadType>;

Type Parameters

ThreadType

ThreadType extends Thread = any

          ### <a id="EventType"></a>EventType

@ariestools/threads


type EventType = PoolEventType;
    ### type-aliases

      ### <a id="BlobWorker"></a>BlobWorker

@ariestools/threads


type BlobWorker = typeof BlobWorkerClass;

Separate class to spawn workers from source code blobs or strings.

      ### <a id="ExposedAs"></a>ExposedAs

@ariestools/threads


type ExposedAs<Exposed> = Exposed extends ArbitraryWorkerInterface ? ArbitraryThreadType : Exposed extends WorkerFunction ? FunctionThread<Parameters<Exposed>, StripAsync<ReturnType<Exposed>>> : Exposed extends WorkerModule<any> ? ModuleThread<Exposed> : never;

Maps a worker's exposed API type to its corresponding thread type (FunctionThread or ModuleThread).

Type Parameters

Exposed

Exposed extends WorkerFunction | WorkerModule<any>

      ### <a id="FunctionThread"></a>FunctionThread

@ariestools/threads


type FunctionThread<Args, ReturnType> = ProxyableFunction<Args, ReturnType> & PrivateThreadProps;

A thread wrapping a single exposed function.

Type Parameters

Args

Args extends any[] = any[]

ReturnType

ReturnType = any

      ### <a id="JsonSerializable"></a>JsonSerializable

@ariestools/threads


type JsonSerializable = 
  | JsonSerializablePrimitive
  | JsonSerializablePrimitive[]
  | JsonSerializableObject
  | JsonSerializableObject[];

A JSON-compatible value that can be serialized for worker message passing.

      ### <a id="ModuleThread"></a>ModuleThread

@ariestools/threads


type ModuleThread<Methods> = ModuleProxy<Methods> & PrivateThreadProps;

A thread wrapping an exposed module of functions.

Type Parameters

Methods

Methods extends ModuleMethods = any

      ### <a id="Thread"></a>Thread

@ariestools/threads


type Thread = ThreadType;

Re-exported Thread type from the master types module.

      ### <a id="Worker"></a>Worker

@ariestools/threads


type Worker = WorkerType;

Worker implementation. Either web worker or a node.js Worker class.

    ### variables

      ### <a id="BlobWorker"></a>BlobWorker

@ariestools/threads


BlobWorker: typeof BlobWorker;

Separate class to spawn workers from source code blobs or strings.

      ### <a id="DefaultSerializer"></a>DefaultSerializer

@ariestools/threads


const DefaultSerializer: Serializer<JsonSerializable>;

Default serializer that handles Error instances and passes other values through.

      ### <a id="Pool"></a>Pool

@ariestools/threads


Pool: <ThreadType>(spawnWorker, optionsOrSize?) => WorkerPool<ThreadType> & object;

Thread pool constructor. Creates a new pool and spawns its worker threads.

Type Declaration

EventType

EventType: typeof PoolEventType;
      ### <a id="Thread"></a>Thread

@ariestools/threads


Thread: object;

Thread utility functions. Use them to manage or inspect a spawn()-ed thread.

Type Declaration

errors()

errors<ThreadT>(thread): Observable<Error>;

Return an observable that can be used to subscribe to all errors happening in the thread.

Type Parameters

ThreadT

ThreadT extends Thread

Parameters

thread

ThreadT

Returns

Observable<Error>

events()

events<ThreadT>(thread): Observable<WorkerEvent>;

Return an observable that can be used to subscribe to internal events happening in the thread. Useful for debugging.

Type Parameters

ThreadT

ThreadT extends Thread

Parameters

thread

ThreadT

Returns

Observable<WorkerEvent>

terminate()

terminate<ThreadT>(thread): Promise<void>;

Terminate a thread. Remember to terminate every thread when you are done using it.

Type Parameters

ThreadT

ThreadT extends Thread

Parameters

thread

ThreadT

Returns

Promise<void>

      ### <a id="Worker"></a>Worker

@ariestools/threads


Worker: typeof WorkerImplementation;

Worker implementation. Either web worker or a node.js Worker class.

  ### index-node

    ### functions

      ### <a id="isWorkerRuntime"></a>isWorkerRuntime

@ariestools/threads


function isWorkerRuntime(): boolean;

Check whether the current code is running inside a Node.js worker thread.

Returns

boolean

True if running in a worker thread (not the main thread), false otherwise.

    ### type-aliases

      ### <a id="BlobWorker"></a>BlobWorker

@ariestools/threads


type BlobWorker = typeof BlobWorkerClass;

Separate class to spawn workers from source code blobs or strings.

      ### <a id="Worker"></a>Worker

@ariestools/threads


type Worker = WorkerType;

Worker implementation. Either web worker or a node.js Worker class.

    ### variables

      ### <a id="BlobWorker"></a>BlobWorker

@ariestools/threads


BlobWorker: typeof BlobWorker;

Separate class to spawn workers from source code blobs or strings.

      ### <a id="Worker"></a>Worker

@ariestools/threads


Worker: typeof WorkerImplementation;

Worker implementation. Either web worker or a node.js Worker class.

Credits

Made with 🔥 and ❄️ by XY Labs

版本与发布记录

@ariestools/threads 共发布 24 个版本,首次发布于 2026/07/07;过去 12 个月发布了 24 个版本。

版本发布日期距今
9.0.3latest2026/11/062 天前
9.0.22026/10/2711 天前
9.0.12026/09/251 个月前
9.0.02026/09/171 个月前
8.3.02026/09/062 个月前
8.2.02026/08/282 个月前

关于 @ariestools/threads 的常见问题

@ariestools/threads 是什么?

Web workers & worker threads as simple as a function call

如何安装 @ariestools/threads?

在项目目录运行 npm install @ariestools/threads,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:observable-fns(^0.6)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@ariestools/threads 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ariestools/threads 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@ariestools/threads 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=26。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@ariestools/threads 可以商用吗?

@ariestools/threads 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@ariestools/threads 的下载量有多少?

根据 npm 官方下载统计,@ariestools/threads 在 2026/10/07 – 2026/11/05 这 30 天内被下载 11,219 次,日均约 374 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ariestools/threads 还在维护吗?

最新版本 9.0.3 发布于 2026/11/06(2 天前);至今共发布 24 个版本,其中过去 12 个月发布了 24 个;首次发布于 2026/07/07。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。