
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
A browser and Node.js compatible queue library with advanced features like priority, delay, retry, and persistence
一个同时支持浏览器和Node.js环境的队列库,使用TypeScript开发。
npm install web-queue
# 或
yarn add web-queue
import { Queue } from 'web-queue';
// 创建一个队列
const queue = new Queue<number>();
// 添加元素
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
// 获取队列大小
console.log(queue.size()); // 3
// 查看队首元素但不移除
console.log(queue.peek()); // 1
// 移除并返回队首元素
console.log(queue.dequeue()); // 1
// 检查队列是否为空
console.log(queue.isEmpty()); // false
// 清空队列
queue.clear();
console.log(queue.isEmpty()); // true
const queue = new Queue<string>();
queue.enqueue('a');
queue.enqueue('b');
queue.enqueue('c');
const array = queue.toArray();
console.log(array); // ['a', 'b', 'c']
TopicQueue 使用 BroadcastChannel API 实现基于主题的消息传递,适用于浏览器环境。
import { TopicQueue } from 'web-queue';
// 创建一个基于主题的队列
const topic = new TopicQueue<string>('myChannel');
// 订阅消息
const unsubscribe = topic.subscribe(message => {
console.log('收到消息:', message);
});
// 发送消息(同时添加到队列并广播)
topic.enqueue('Hello World');
// 取消订阅
unsubscribe();
// 关闭通道
topic.close();
// 在标签页 A 中
const topicA = new TopicQueue<string>('sharedChannel');
topicA.enqueue('来自标签页 A 的消息');
// 在标签页 B 中
const topicB = new TopicQueue<string>('sharedChannel');
topicB.subscribe(message => {
console.log('标签页 B 收到:', message); // 将显示 "来自标签页 A 的消息"
});
AdvancedQueue 提供了更多高级功能,如消息幂等性、优先级、延迟投递、重试机制和持久化。
import { AdvancedQueue, MessageStatus } from 'web-queue';
// 创建高级队列,配置最大重试次数和持久化
const queue = new AdvancedQueue<{ taskId: string, data: any }>({
maxRetries: 3,
retryDelay: 1000,
persistenceEnabled: true,
persistenceDriver: 'localstorage',
persistenceInterval: 5000
});
// 添加消息,支持自定义ID(幂等性)、优先级和延迟
const message = queue.enqueue(
{ taskId: 'task-123', data: { value: 42 } },
{
id: 'unique-message-id', // 自定义ID,确保幂等性
priority: 10, // 优先级,数字越大优先级越高
delay: 5000 // 延迟5秒后才可处理
}
);
// 获取下一个待处理的消息
const nextMessage = queue.dequeue();
if (nextMessage) {
try {
// 处理消息...
processMessage(nextMessage.data);
// 标记消息处理成功
queue.complete(nextMessage.id);
} catch (error) {
// 标记消息处理失败,会自动重试
queue.fail(nextMessage.id, error.message);
}
}
// 获取延迟队列中的消息
const delayedMessages = queue.getDelayedMessages();
// 取消延迟消息
queue.cancelDelayed('message-id');
// 获取死信队列中的消息
const deadLetterMessages = queue.getDeadLetterMessages();
// 重试死信队列中的消息
queue.retryDeadLetter('message-id');
// 清理资源
queue.dispose();
AdvancedTopicQueue 结合了 AdvancedQueue 的高级功能和 TopicQueue 的广播能力。
import { AdvancedTopicQueue } from 'web-queue';
// 创建高级主题队列
const topicQueue = new AdvancedTopicQueue<{ event: string, payload: any }>(
'notifications',
{ maxRetries: 2, persistenceEnabled: true }
);
// 订阅消息
const unsubscribe = topicQueue.subscribe(message => {
console.log(`收到消息 ${message.id}:`, message.data);
});
// 发送高优先级消息
topicQueue.enqueue(
{ event: 'user.login', payload: { userId: 123 } },
{ priority: 10 }
);
// 发送延迟消息(不会立即广播)
topicQueue.enqueue(
{ event: 'maintenance.reminder', payload: { time: '2小时后' } },
{ delay: 7200000 } // 2小时后
);
// 取消订阅并关闭
unsubscribe();
topicQueue.close();
web-queue 支持多种存储驱动来持久化队列数据:
import {
AdvancedQueue,
MemoryStorageDriver,
LocalStorageDriver,
IndexedDBStorageDriver
} from 'web-queue';
// 使用内存存储(默认)
const memoryQueue = new AdvancedQueue({
persistenceEnabled: true,
persistenceDriver: 'memory'
});
// 使用 localStorage 存储
const localStorageQueue = new AdvancedQueue({
persistenceEnabled: true,
persistenceDriver: 'localstorage'
});
// 使用 IndexedDB 存储
const indexedDBQueue = new AdvancedQueue({
persistenceEnabled: true,
persistenceDriver: 'indexeddb'
});
MemoryStorageDriver:
LocalStorageDriver:
localStorage API,适合存储小量数据(通常限制为 5MB)。IndexedDBStorageDriver:
IndexedDB API,适合存储大量结构化数据。项目包含多个交互式示例,展示了各种功能的使用方法:
examples/advanced-queue-demo.html - 高级队列基本功能演示examples/advanced-topic-queue-demo.html - 高级主题队列和多标签页通信examples/storage-drivers-demo.html - 不同存储驱动的使用examples/idempotent-messages-demo.html - 消息幂等性功能examples/retry-dead-letter-demo.html - 重试机制和死信队列examples/delayed-queue-demo.html - 延迟队列和定时消息要运行这些示例,只需在浏览器中打开对应的HTML文件。
enqueue(item: T): void - 将元素添加到队列末尾dequeue(): T | undefined - 移除并返回队首元素peek(): T | undefined - 返回队首元素但不移除size(): number - 返回队列中的元素数量isEmpty(): boolean - 检查队列是否为空clear(): void - 清空队列toArray(): T[] - 将队列转换为数组TopicQueue 继承自 Queue,并添加了以下方法:
constructor(topicName?: string) - 创建一个基于主题的队列subscribe(callback: (item: T) => void): () => void - 订阅主题消息,返回取消订阅的函数close(): void - 关闭广播通道constructor(options?: Partial<QueueOptions>) - 创建高级队列,可配置重试、持久化等选项enqueue(data: T, options?: { priority?: number; delay?: number; id?: string }): Message<T> - 添加消息,支持优先级、延迟和自定义IDdequeue(): Message<T> | undefined - 获取并标记为处理中的下一个消息peek(): Message<T> | undefined - 查看下一个待处理消息但不移除complete(messageId: string): boolean - 标记消息处理成功fail(messageId: string, reason?: string): boolean - 标记消息处理失败findMessageById(id: string): Message<T> | undefined - 通过ID查找消息cancelDelayed(messageId: string): boolean - 取消延迟消息retryDeadLetter(messageId: string): boolean - 重试死信队列中的消息getDelayedMessages(): Message<T>[] - 获取所有延迟消息getDeadLetterMessages(): Message<T>[] - 获取所有死信消息getAllMessages(): Message<T>[] - 获取所有消息size(): number - 获取待处理消息数量totalSize(): number - 获取所有消息数量isEmpty(): boolean - 检查是否没有待处理消息clear(): void - 清空所有队列toArray(): Message<T>[] - 将待处理消息转换为数组dispose(): void - 清理资源AdvancedTopicQueue 继承自 AdvancedQueue,并添加了以下方法:
constructor(topicName?: string, options?: Partial<QueueOptions>) - 创建高级主题队列subscribe(callback: (message: Message<T>) => void): () => void - 订阅消息,返回取消订阅的函数close(): void - 关闭广播通道并清理资源MemoryStorageDriver - 内存存储驱动
save<T>(key: string, data: T): Promise<void> - 保存数据到内存load<T>(key: string): Promise<T | null> - 从内存加载数据delete(key: string): Promise<void> - 从内存删除数据clear(): Promise<void> - 清空内存存储LocalStorageDriver - localStorage 存储驱动
save<T>(key: string, data: T): Promise<void> - 保存数据到 localStorageload<T>(key: string): Promise<T | null> - 从 localStorage 加载数据delete(key: string): Promise<void> - 从 localStorage 删除数据clear(): Promise<void> - 清空 localStorage 存储IndexedDBStorageDriver - IndexedDB 存储驱动
save<T>(key: string, data: T): Promise<void> - 保存数据到 IndexedDBload<T>(key: string): Promise<T | null> - 从 IndexedDB 加载数据delete(key: string): Promise<void> - 从 IndexedDB 删除数据clear(): Promise<void> - 清空 IndexedDB 存储# 安装依赖
npm install
# 开发模式
npm run dev
# 构建
npm run build
# 运行测试
npm test
Apache License Version 2.0
FAQs
A browser and Node.js compatible queue library with advanced features like priority, delay, retry, and persistence
We found that web-queue demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.