taro-bluetooth-print 2.4.0 → 2.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -132,7 +132,7 @@ export declare class MultiPrinterManager {
132
132
  /**
133
133
  * Print to a specific printer
134
134
  */
135
- print(printerId: string, data: Uint8Array): Promise<void>;
135
+ print(printerId: string, data: Uint8Array): void;
136
136
  /**
137
137
  * Broadcast data to all connected printers
138
138
  */
@@ -43,7 +43,7 @@ export { BluetoothPrintError, ErrorCode } from './errors/BluetoothError';
43
43
  export { DEFAULT_CONFIG, mergeConfig } from './config/PrinterConfig';
44
44
  export type { PrinterConfig, AdapterConfig, DriverConfig, LoggingConfig, } from './config/PrinterConfig';
45
45
  export { PrinterConfigManager, printerConfigManager } from './config/PrinterConfigManager';
46
- export type { SavedPrinter, GlobalConfig, IConfigStorage, } from './config/PrinterConfigManager';
46
+ export type { SavedPrinter, GlobalConfig, IConfigStorage } from './config/PrinterConfigManager';
47
47
  export { PluginManager } from './plugins/PluginManager';
48
48
  export { createLoggingPlugin, createRetryPlugin } from './plugins';
49
49
  export type { Plugin, PluginHooks, PluginOptions, PluginFactory } from './plugins/types';
@@ -5,7 +5,7 @@
5
5
  export { ConnectionManager, type ConnectionManagerConfig, type ConnectionManagerEvents, } from './ConnectionManager';
6
6
  export { CommandBuilder } from './CommandBuilder';
7
7
  export { PrintJobManager } from './PrintJobManager';
8
- export { PrintHistory, printHistory, type PrintHistoryEntry, type PrintHistoryStats, type HistoryQueryOptions } from './PrintHistory';
9
- export { PrinterStatus, printerStatus, type PrinterStatusInfo, type StatusQueryOptions, type PaperStatus } from './PrinterStatus';
10
- export { BatchPrintManager, batchPrintManager, type BatchJob, type BatchConfig, type BatchStats, type BatchEvents } from './BatchPrintManager';
8
+ export { PrintHistory, printHistory, type PrintHistoryEntry, type PrintHistoryStats, type HistoryQueryOptions, } from './PrintHistory';
9
+ export { PrinterStatus, printerStatus, type PrinterStatusInfo, type StatusQueryOptions, type PaperStatus, } from './PrinterStatus';
10
+ export { BatchPrintManager, batchPrintManager, type BatchJob, type BatchConfig, type BatchStats, type BatchEvents, } from './BatchPrintManager';
11
11
  export * from './interfaces';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "taro-bluetooth-print",
3
- "version": "2.4.0",
3
+ "version": "2.4.1",
4
4
  "description": "Taro 蓝牙打印库 v2.4 - 轻量级、高性能、跨平台支持微信、支付宝、百度、字节跳动小程序及H5 Web Bluetooth",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs.js",
@@ -425,12 +425,16 @@ export class PrinterConfigManager {
425
425
  * Export all configuration as JSON
426
426
  */
427
427
  export(): string {
428
- return JSON.stringify({
429
- printers: Array.from(this.printers.values()),
430
- globalConfig: this.globalConfig,
431
- lastUsedPrinterId: this.lastUsedPrinterId,
432
- exportedAt: Date.now(),
433
- }, null, 2);
428
+ return JSON.stringify(
429
+ {
430
+ printers: Array.from(this.printers.values()),
431
+ globalConfig: this.globalConfig,
432
+ lastUsedPrinterId: this.lastUsedPrinterId,
433
+ exportedAt: Date.now(),
434
+ },
435
+ null,
436
+ 2
437
+ );
434
438
  }
435
439
 
436
440
  /**
@@ -26,6 +26,7 @@
26
26
  import { Logger } from '@/utils/logger';
27
27
  import { BluetoothPrintError, ErrorCode } from '@/errors/BluetoothError';
28
28
  import { BluetoothPrinter } from '@/core/BluetoothPrinter';
29
+ import { PrinterState } from '@/types';
29
30
 
30
31
  /**
31
32
  * Printer connection info
@@ -137,11 +138,12 @@ export class MultiPrinterManager {
137
138
  * Emit an event
138
139
  */
139
140
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
141
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call
140
142
  private emit<K extends keyof MultiPrinterManagerEvents>(event: K, data: any): void {
141
143
  this.listeners[event].forEach(handler => {
142
144
  try {
143
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
144
- (handler as any)(data);
145
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call
146
+ (handler as (data: any) => void)(data);
145
147
  } catch (error) {
146
148
  this.logger.error(`Error in event handler for "${event}":`, error);
147
149
  }
@@ -200,7 +202,7 @@ export class MultiPrinterManager {
200
202
  const printer = new BluetoothPrinter();
201
203
 
202
204
  // Set up error handler
203
- printer.on('error', (error) => {
205
+ printer.on('error', error => {
204
206
  this.emit('printer-error', { printerId, error });
205
207
  });
206
208
 
@@ -323,13 +325,10 @@ export class MultiPrinterManager {
323
325
  /**
324
326
  * Print to a specific printer
325
327
  */
326
- async print(printerId: string, data: Uint8Array): Promise<void> {
328
+ print(printerId: string, data: Uint8Array): void {
327
329
  const connection = this.printers.get(printerId);
328
330
  if (!connection) {
329
- throw new BluetoothPrintError(
330
- ErrorCode.DEVICE_NOT_FOUND,
331
- `Printer not found: ${printerId}`
332
- );
331
+ throw new BluetoothPrintError(ErrorCode.DEVICE_NOT_FOUND, `Printer not found: ${printerId}`);
333
332
  }
334
333
 
335
334
  connection.lastActivity = Date.now();
@@ -358,6 +357,7 @@ export class MultiPrinterManager {
358
357
  }
359
358
 
360
359
  const printPromises = Array.from(this.printers.entries()).map(
360
+ // eslint-disable-next-line @typescript-eslint/require-await
361
361
  async ([printerId, connection]) => {
362
362
  try {
363
363
  // Update activity
@@ -401,7 +401,7 @@ export class MultiPrinterManager {
401
401
  */
402
402
  getIdlePrinters(): PrinterConnection[] {
403
403
  return Array.from(this.printers.values())
404
- .filter(c => c.printer.state === 'connected')
404
+ .filter(c => c.printer.state === PrinterState.CONNECTED)
405
405
  .sort((a, b) => (a.lastActivity ?? 0) - (b.lastActivity ?? 0));
406
406
  }
407
407
 
@@ -420,7 +420,7 @@ export class MultiPrinterManager {
420
420
  };
421
421
 
422
422
  for (const connection of this.printers.values()) {
423
- if (connection.printer.state === 'connected') {
423
+ if (connection.printer.state === PrinterState.CONNECTED) {
424
424
  stats.connected++;
425
425
  }
426
426
 
package/src/index.ts CHANGED
@@ -98,11 +98,7 @@ export type {
98
98
  } from './config/PrinterConfig';
99
99
 
100
100
  export { PrinterConfigManager, printerConfigManager } from './config/PrinterConfigManager';
101
- export type {
102
- SavedPrinter,
103
- GlobalConfig,
104
- IConfigStorage,
105
- } from './config/PrinterConfigManager';
101
+ export type { SavedPrinter, GlobalConfig, IConfigStorage } from './config/PrinterConfigManager';
106
102
 
107
103
  // Plugin System - 插件系统
108
104
  export { PluginManager } from './plugins/PluginManager';
@@ -92,11 +92,11 @@ type BatchEventHandlerMap = {
92
92
  * Default batch configuration
93
93
  */
94
94
  const DEFAULT_CONFIG: BatchConfig = {
95
- maxBatchSize: 1024 * 50, // 50KB max per batch
96
- maxWaitTime: 1000, // 1 second max wait
97
- minBatchSize: 1, // Process even single jobs
98
- enableMerging: true, // Enable content merging
99
- autoProcessInterval: 500, // Check every 500ms
95
+ maxBatchSize: 1024 * 50, // 50KB max per batch
96
+ maxWaitTime: 1000, // 1 second max wait
97
+ minBatchSize: 1, // Process even single jobs
98
+ enableMerging: true, // Enable content merging
99
+ autoProcessInterval: 500, // Check every 500ms
100
100
  };
101
101
 
102
102
  /**
@@ -136,20 +136,14 @@ export class BatchPrintManager {
136
136
  /**
137
137
  * Register event listener
138
138
  */
139
- on<K extends keyof BatchEvents>(
140
- event: K,
141
- callback: BatchEvents[K]
142
- ): void {
139
+ on<K extends keyof BatchEvents>(event: K, callback: BatchEvents[K]): void {
143
140
  this.listeners[event].add(callback);
144
141
  }
145
142
 
146
143
  /**
147
144
  * Remove event listener
148
145
  */
149
- off<K extends keyof BatchEvents>(
150
- event: K,
151
- callback: BatchEvents[K]
152
- ): void {
146
+ off<K extends keyof BatchEvents>(event: K, callback: BatchEvents[K]): void {
153
147
  this.listeners[event].delete(callback);
154
148
  }
155
149
 
@@ -157,11 +151,11 @@ export class BatchPrintManager {
157
151
  * Emit an event
158
152
  */
159
153
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
160
- private emit<K extends keyof BatchEvents>(event: K, data: any): void {
154
+ private emit<K extends keyof BatchEvents>(event: K, data: Parameters<BatchEvents[K]>[0]): void {
161
155
  this.listeners[event].forEach(handler => {
162
156
  try {
163
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
164
- (handler as any)(data);
157
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call
158
+ (handler as (data: Parameters<BatchEvents[K]>[0]) => void)(data);
165
159
  } catch (error) {
166
160
  this.logger.error(`Error in event handler for "${event}":`, error);
167
161
  }
@@ -176,11 +170,7 @@ export class BatchPrintManager {
176
170
  * @param metadata - Optional metadata
177
171
  * @returns Job ID
178
172
  */
179
- addJob(
180
- data: Uint8Array,
181
- priority = 1,
182
- metadata?: Record<string, unknown>
183
- ): string {
173
+ addJob(data: Uint8Array, priority = 1, metadata?: Record<string, unknown>): string {
184
174
  const id = this.generateId();
185
175
  const job: BatchJob = {
186
176
  id,
@@ -216,7 +206,9 @@ export class BatchPrintManager {
216
206
  /**
217
207
  * Add multiple jobs at once
218
208
  */
219
- addJobs(jobs: Array<{ data: Uint8Array; priority?: number; metadata?: Record<string, unknown> }>): string[] {
209
+ addJobs(
210
+ jobs: Array<{ data: Uint8Array; priority?: number; metadata?: Record<string, unknown> }>
211
+ ): string[] {
220
212
  return jobs.map(job => this.addJob(job.data, job.priority, job.metadata));
221
213
  }
222
214
 
@@ -279,9 +271,7 @@ export class BatchPrintManager {
279
271
  * @param processor - Function to send batch data to printer
280
272
  * @returns Number of jobs processed
281
273
  */
282
- async processBatch(
283
- processor: (data: Uint8Array) => Promise<void>
284
- ): Promise<number> {
274
+ async processBatch(processor: (data: Uint8Array) => Promise<void>): Promise<number> {
285
275
  if (this.isProcessing) {
286
276
  throw new BluetoothPrintError(
287
277
  ErrorCode.PRINT_JOB_IN_PROGRESS,
@@ -390,7 +380,11 @@ export class BatchPrintManager {
390
380
 
391
381
  // Large single job
392
382
  const firstJob = this.jobs[0];
393
- if (this.jobs.length === 1 && firstJob && firstJob.data.length >= this.config.maxBatchSize * 0.8) {
383
+ if (
384
+ this.jobs.length === 1 &&
385
+ firstJob &&
386
+ firstJob.data.length >= this.config.maxBatchSize * 0.8
387
+ ) {
394
388
  return true;
395
389
  }
396
390
 
@@ -139,12 +139,15 @@ export class PrintHistory {
139
139
  /**
140
140
  * Update job status
141
141
  */
142
- updateJob(id: string, updates: Partial<{
143
- status: PrintJobStatus | 'unknown';
144
- startedAt: number;
145
- completedAt: number;
146
- error: string;
147
- }>): void {
142
+ updateJob(
143
+ id: string,
144
+ updates: Partial<{
145
+ status: PrintJobStatus | 'unknown';
146
+ startedAt: number;
147
+ completedAt: number;
148
+ error: string;
149
+ }>
150
+ ): void {
148
151
  const entry = this.entries.get(id);
149
152
  if (!entry) {
150
153
  this.logger.warn(`History entry not found: ${id}`);
@@ -242,9 +245,7 @@ export class PrintHistory {
242
245
  }
243
246
 
244
247
  const completed = entries.filter(e => e.status === PrintJobStatus.COMPLETED);
245
- const failed = entries.filter(e =>
246
- e.status === PrintJobStatus.FAILED || e.error
247
- );
248
+ const failed = entries.filter(e => e.status === PrintJobStatus.FAILED || e.error);
248
249
  const cancelled = entries.filter(e => e.status === PrintJobStatus.CANCELLED);
249
250
 
250
251
  const totalDuration = completed
@@ -320,8 +321,9 @@ export class PrintHistory {
320
321
  }
321
322
 
322
323
  // Remove oldest entries
323
- const sorted = Array.from(this.entries.entries())
324
- .sort((a, b) => a[1].createdAt - b[1].createdAt);
324
+ const sorted = Array.from(this.entries.entries()).sort(
325
+ (a, b) => a[1].createdAt - b[1].createdAt
326
+ );
325
327
 
326
328
  const toRemove = sorted.slice(0, this.entries.size - this.maxEntries);
327
329
  for (const [id] of toRemove) {
@@ -40,10 +40,10 @@ interface SavedJobState {
40
40
  export class PrintJobManager implements IPrintJobManager {
41
41
  /** Instance-level job state storage (per-printer support) */
42
42
  private instanceJobStateStore: Map<string, SavedJobState> = new Map();
43
-
43
+
44
44
  /** Static job state store for backward compatibility */
45
45
  private static _jobStateStore: Map<string, SavedJobState> = new Map();
46
-
46
+
47
47
  /**
48
48
  * Get the static job state store (for backward compatibility)
49
49
  * @deprecated Use instance-level store instead for multi-printer support
@@ -332,7 +332,7 @@ export class PrintJobManager implements IPrintJobManager {
332
332
  /**
333
333
  * Clean up expired job states from static store.
334
334
  * Call this periodically to prevent memory leaks.
335
- *
335
+ *
336
336
  * @param maxAge - Maximum age in ms (default: 1 hour)
337
337
  */
338
338
  static cleanupExpiredJobs(maxAge = 3600000): number {
@@ -70,8 +70,8 @@ const ESCPOS_STATUS_NUL = 0x04; // ENQ
70
70
  /**
71
71
  * Status response bit masks for common printers
72
72
  */
73
- const STATUS_BIT_PAPER_OUT = 0x20; // Bit 5: Paper out
74
- const STATUS_BIT_PAPER_LOW = 0x40; // Bit 6: Paper low
73
+ const STATUS_BIT_PAPER_OUT = 0x20; // Bit 5: Paper out
74
+ const STATUS_BIT_PAPER_LOW = 0x40; // Bit 6: Paper low
75
75
 
76
76
  /**
77
77
  * Printer Status Service
@@ -109,18 +109,12 @@ export class PrinterStatus {
109
109
  // Set up timeout promise
110
110
  const timeoutPromise = new Promise<ArrayBuffer>((_, reject) => {
111
111
  setTimeout(() => {
112
- reject(new BluetoothPrintError(
113
- ErrorCode.CONNECTION_TIMEOUT,
114
- 'Status query timed out'
115
- ));
112
+ reject(new BluetoothPrintError(ErrorCode.CONNECTION_TIMEOUT, 'Status query timed out'));
116
113
  }, timeout);
117
114
  });
118
115
 
119
116
  // Read response with timeout
120
- const response = await Promise.race([
121
- readFunc(),
122
- timeoutPromise
123
- ]);
117
+ const response = await Promise.race([readFunc(), timeoutPromise]);
124
118
 
125
119
  return this.parseStatus(new Uint8Array(response), includeRaw);
126
120
  } catch (error) {
@@ -13,10 +13,29 @@ export { CommandBuilder } from './CommandBuilder';
13
13
 
14
14
  export { PrintJobManager } from './PrintJobManager';
15
15
 
16
- export { PrintHistory, printHistory, type PrintHistoryEntry, type PrintHistoryStats, type HistoryQueryOptions } from './PrintHistory';
16
+ export {
17
+ PrintHistory,
18
+ printHistory,
19
+ type PrintHistoryEntry,
20
+ type PrintHistoryStats,
21
+ type HistoryQueryOptions,
22
+ } from './PrintHistory';
17
23
 
18
- export { PrinterStatus, printerStatus, type PrinterStatusInfo, type StatusQueryOptions, type PaperStatus } from './PrinterStatus';
24
+ export {
25
+ PrinterStatus,
26
+ printerStatus,
27
+ type PrinterStatusInfo,
28
+ type StatusQueryOptions,
29
+ type PaperStatus,
30
+ } from './PrinterStatus';
19
31
 
20
- export { BatchPrintManager, batchPrintManager, type BatchJob, type BatchConfig, type BatchStats, type BatchEvents } from './BatchPrintManager';
32
+ export {
33
+ BatchPrintManager,
34
+ batchPrintManager,
35
+ type BatchJob,
36
+ type BatchConfig,
37
+ type BatchStats,
38
+ type BatchEvents,
39
+ } from './BatchPrintManager';
21
40
 
22
41
  export * from './interfaces';