taro-bluetooth-print 2.2.0 → 2.3.0

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.
Files changed (49) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/README.md +128 -22
  3. package/dist/index.cjs.js +1 -1
  4. package/dist/index.es.js +1 -6995
  5. package/dist/index.umd.js +1 -1
  6. package/dist/types/adapters/AdapterFactory.d.ts +0 -1
  7. package/dist/types/adapters/AlipayAdapter.d.ts +6 -34
  8. package/dist/types/adapters/BaiduAdapter.d.ts +6 -34
  9. package/dist/types/adapters/BaseAdapter.d.ts +112 -1
  10. package/dist/types/adapters/ByteDanceAdapter.d.ts +6 -34
  11. package/dist/types/adapters/TaroAdapter.d.ts +6 -34
  12. package/dist/types/adapters/WebBluetoothAdapter.d.ts +0 -1
  13. package/dist/types/config/PrinterConfig.d.ts +0 -1
  14. package/dist/types/core/BluetoothPrinter.d.ts +0 -1
  15. package/dist/types/drivers/EscPos.d.ts +0 -1
  16. package/dist/types/drivers/TsplDriver.d.ts +251 -0
  17. package/dist/types/encoding/gbk-data.d.ts +12 -0
  18. package/dist/types/encoding/gbk-table.d.ts +5 -1
  19. package/dist/types/index.d.ts +5 -0
  20. package/dist/types/plugins/PluginManager.d.ts +87 -0
  21. package/dist/types/plugins/builtin/LoggingPlugin.d.ts +14 -0
  22. package/dist/types/plugins/builtin/RetryPlugin.d.ts +18 -0
  23. package/dist/types/plugins/index.d.ts +7 -0
  24. package/dist/types/plugins/types.d.ts +97 -0
  25. package/dist/types/services/CommandBuilder.d.ts +6 -1
  26. package/dist/types/services/ConnectionManager.d.ts +0 -1
  27. package/dist/types/services/PrintJobManager.d.ts +6 -2
  28. package/dist/types/services/interfaces/index.d.ts +0 -1
  29. package/dist/types/template/TemplateEngine.d.ts +0 -1
  30. package/package.json +16 -18
  31. package/src/adapters/AlipayAdapter.ts +8 -314
  32. package/src/adapters/BaiduAdapter.ts +8 -312
  33. package/src/adapters/BaseAdapter.ts +366 -0
  34. package/src/adapters/ByteDanceAdapter.ts +8 -316
  35. package/src/adapters/TaroAdapter.ts +8 -367
  36. package/src/core/EventEmitter.ts +9 -6
  37. package/src/drivers/TsplDriver.ts +417 -0
  38. package/src/encoding/gbk-data.ts +1911 -0
  39. package/src/encoding/gbk-table.ts +22 -498
  40. package/src/index.ts +14 -0
  41. package/src/plugins/PluginManager.ts +193 -0
  42. package/src/plugins/builtin/LoggingPlugin.ts +97 -0
  43. package/src/plugins/builtin/RetryPlugin.ts +109 -0
  44. package/src/plugins/index.ts +10 -0
  45. package/src/plugins/types.ts +119 -0
  46. package/src/preview/PreviewRenderer.ts +7 -1
  47. package/src/queue/PrintQueue.ts +10 -6
  48. package/src/services/CommandBuilder.ts +30 -0
  49. package/src/services/PrintJobManager.ts +51 -35
@@ -0,0 +1,87 @@
1
+ import { Plugin, PluginHooks, PluginOptions } from './types';
2
+ import { BluetoothPrintError } from '../errors/BluetoothError';
3
+ import { PrinterState } from '../types';
4
+ /**
5
+ * Manages plugins for BluetoothPrinter
6
+ */
7
+ export declare class PluginManager {
8
+ private plugins;
9
+ private readonly logger;
10
+ /**
11
+ * Register a plugin
12
+ * @param plugin - Plugin to register
13
+ * @param options - Plugin options
14
+ * @throws {BluetoothPrintError} If plugin with same name already exists
15
+ */
16
+ register(plugin: Plugin, options?: PluginOptions): Promise<void>;
17
+ /**
18
+ * Unregister a plugin
19
+ * @param name - Plugin name to unregister
20
+ */
21
+ unregister(name: string): Promise<void>;
22
+ /**
23
+ * Get a registered plugin
24
+ * @param name - Plugin name
25
+ * @returns Plugin instance or undefined
26
+ */
27
+ get(name: string): Plugin | undefined;
28
+ /**
29
+ * Get all registered plugin names
30
+ * @returns Array of plugin names
31
+ */
32
+ getNames(): string[];
33
+ /**
34
+ * Check if a plugin is registered
35
+ * @param name - Plugin name
36
+ * @returns True if registered
37
+ */
38
+ has(name: string): boolean;
39
+ /**
40
+ * Execute a hook across all plugins
41
+ * @param hookName - Name of the hook to execute
42
+ * @param args - Arguments to pass to the hook
43
+ * @returns Result from hooks (last non-void result)
44
+ */
45
+ executeHook<K extends keyof PluginHooks>(hookName: K, ...args: Parameters<NonNullable<PluginHooks[K]>>): Promise<unknown>;
46
+ /**
47
+ * Execute beforeConnect hooks
48
+ */
49
+ beforeConnect(deviceId: string): Promise<string>;
50
+ /**
51
+ * Execute afterConnect hooks
52
+ */
53
+ afterConnect(deviceId: string): Promise<void>;
54
+ /**
55
+ * Execute beforeDisconnect hooks
56
+ */
57
+ beforeDisconnect(deviceId: string): Promise<void>;
58
+ /**
59
+ * Execute afterDisconnect hooks
60
+ */
61
+ afterDisconnect(deviceId: string): Promise<void>;
62
+ /**
63
+ * Execute beforePrint hooks
64
+ */
65
+ beforePrint(buffer: Uint8Array): Promise<Uint8Array>;
66
+ /**
67
+ * Execute afterPrint hooks
68
+ */
69
+ afterPrint(bytesSent: number): Promise<void>;
70
+ /**
71
+ * Execute onError hooks
72
+ * @returns True if error should be suppressed
73
+ */
74
+ onError(error: BluetoothPrintError): Promise<boolean>;
75
+ /**
76
+ * Execute onStateChange hooks
77
+ */
78
+ onStateChange(state: PrinterState, previousState: PrinterState): Promise<void>;
79
+ /**
80
+ * Execute onProgress hooks
81
+ */
82
+ onProgress(sent: number, total: number): Promise<void>;
83
+ /**
84
+ * Clear all plugins
85
+ */
86
+ clear(): Promise<void>;
87
+ }
@@ -0,0 +1,14 @@
1
+ import { PluginFactory, PluginOptions } from '../types';
2
+ import { LogLevel } from '../../utils/logger';
3
+ export interface LoggingPluginOptions extends PluginOptions {
4
+ /** Log level (default: DEBUG) */
5
+ level?: LogLevel;
6
+ /** Include timestamps (default: true) */
7
+ timestamps?: boolean;
8
+ /** Log progress updates (default: false, can be noisy) */
9
+ logProgress?: boolean;
10
+ }
11
+ /**
12
+ * Creates a logging plugin instance
13
+ */
14
+ export declare const createLoggingPlugin: PluginFactory;
@@ -0,0 +1,18 @@
1
+ import { PluginFactory, PluginOptions } from '../types';
2
+ import { ErrorCode } from '../../errors/BluetoothError';
3
+ export interface RetryPluginOptions extends PluginOptions {
4
+ /** Maximum retry attempts (default: 3) */
5
+ maxRetries?: number;
6
+ /** Initial delay in ms (default: 1000) */
7
+ initialDelay?: number;
8
+ /** Maximum delay in ms (default: 10000) */
9
+ maxDelay?: number;
10
+ /** Backoff multiplier (default: 2) */
11
+ backoffMultiplier?: number;
12
+ /** Error codes that should trigger retry */
13
+ retryableErrors?: ErrorCode[];
14
+ }
15
+ /**
16
+ * Creates a retry plugin instance
17
+ */
18
+ export declare const createRetryPlugin: PluginFactory;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Plugin System Exports
3
+ */
4
+ export { PluginManager } from './PluginManager';
5
+ export type { Plugin, PluginHooks, PluginOptions, PluginFactory } from './types';
6
+ export { createLoggingPlugin } from './builtin/LoggingPlugin';
7
+ export { createRetryPlugin } from './builtin/RetryPlugin';
@@ -0,0 +1,97 @@
1
+ import { BluetoothPrintError } from '../errors/BluetoothError';
2
+ import { PrinterState } from '../types';
3
+ /**
4
+ * Plugin lifecycle hooks
5
+ */
6
+ export interface PluginHooks {
7
+ /**
8
+ * Called before connecting to a device
9
+ * @param deviceId - Target device ID
10
+ * @returns Modified device ID or void
11
+ */
12
+ beforeConnect?: (deviceId: string) => string | void | Promise<string | void>;
13
+ /**
14
+ * Called after successful connection
15
+ * @param deviceId - Connected device ID
16
+ */
17
+ afterConnect?: (deviceId: string) => void | Promise<void>;
18
+ /**
19
+ * Called before disconnecting
20
+ * @param deviceId - Device ID to disconnect
21
+ */
22
+ beforeDisconnect?: (deviceId: string) => void | Promise<void>;
23
+ /**
24
+ * Called after disconnection
25
+ * @param deviceId - Disconnected device ID
26
+ */
27
+ afterDisconnect?: (deviceId: string) => void | Promise<void>;
28
+ /**
29
+ * Called before sending print data
30
+ * @param buffer - Data buffer to send
31
+ * @returns Modified buffer or void
32
+ */
33
+ beforePrint?: (buffer: Uint8Array) => Uint8Array | void | Promise<Uint8Array | void>;
34
+ /**
35
+ * Called after print job completes
36
+ * @param bytesSent - Total bytes sent
37
+ */
38
+ afterPrint?: (bytesSent: number) => void | Promise<void>;
39
+ /**
40
+ * Called when an error occurs
41
+ * @param error - The error that occurred
42
+ * @returns Whether to suppress the error (true = suppress)
43
+ */
44
+ onError?: (error: BluetoothPrintError) => boolean | void | Promise<boolean | void>;
45
+ /**
46
+ * Called when printer state changes
47
+ * @param state - New printer state
48
+ * @param previousState - Previous state
49
+ */
50
+ onStateChange?: (state: PrinterState, previousState: PrinterState) => void | Promise<void>;
51
+ /**
52
+ * Called during print progress
53
+ * @param sent - Bytes sent
54
+ * @param total - Total bytes
55
+ */
56
+ onProgress?: (sent: number, total: number) => void | Promise<void>;
57
+ }
58
+ /**
59
+ * Plugin configuration options
60
+ */
61
+ export interface PluginOptions {
62
+ [key: string]: unknown;
63
+ }
64
+ /**
65
+ * Plugin interface
66
+ */
67
+ export interface Plugin {
68
+ /**
69
+ * Unique plugin name
70
+ */
71
+ name: string;
72
+ /**
73
+ * Plugin version
74
+ */
75
+ version?: string;
76
+ /**
77
+ * Plugin description
78
+ */
79
+ description?: string;
80
+ /**
81
+ * Plugin hooks
82
+ */
83
+ hooks: PluginHooks;
84
+ /**
85
+ * Plugin initialization
86
+ * @param options - Plugin options
87
+ */
88
+ init?: (options?: PluginOptions) => void | Promise<void>;
89
+ /**
90
+ * Plugin cleanup
91
+ */
92
+ destroy?: () => void | Promise<void>;
93
+ }
94
+ /**
95
+ * Plugin factory function type
96
+ */
97
+ export type PluginFactory = (options?: PluginOptions) => Plugin;
@@ -2,13 +2,13 @@ import { IPrinterDriver, IQrOptions } from '../types';
2
2
  import { ICommandBuilder } from './interfaces';
3
3
  import { TextAlign, TextStyle } from '../formatter';
4
4
  import { BarcodeOptions } from '../barcode';
5
-
6
5
  /**
7
6
  * Command Builder implementation
8
7
  */
9
8
  export declare class CommandBuilder implements ICommandBuilder {
10
9
  private driver;
11
10
  private buffer;
11
+ private _cachedBuffer;
12
12
  private readonly logger;
13
13
  private readonly formatter;
14
14
  private readonly barcodeGenerator;
@@ -18,6 +18,11 @@ export declare class CommandBuilder implements ICommandBuilder {
18
18
  * @param driver - Printer driver instance
19
19
  */
20
20
  constructor(driver?: IPrinterDriver);
21
+ /**
22
+ * Invalidates the cached combined buffer.
23
+ * Must be called whenever the internal buffer array is modified.
24
+ */
25
+ private invalidateCache;
21
26
  /**
22
27
  * Adds text to the print queue
23
28
  *
@@ -2,7 +2,6 @@ import { IPrinterAdapter, PrinterState } from '../types';
2
2
  import { IConnectionManager } from './interfaces';
3
3
  import { BluetoothPrintError } from '../errors/BluetoothError';
4
4
  import { EventEmitter } from '../core/EventEmitter';
5
-
6
5
  /**
7
6
  * Connection manager configuration
8
7
  */
@@ -1,10 +1,11 @@
1
1
  import { IAdapterOptions } from '../types';
2
2
  import { IPrintJobManager, IConnectionManager } from './interfaces';
3
-
4
3
  /**
5
4
  * Print Job Manager implementation
6
5
  */
7
6
  export declare class PrintJobManager implements IPrintJobManager {
7
+ /** 内存中的任务状态存储(可被子类或外部替换为持久化方案) */
8
+ private static jobStateStore;
8
9
  private adapter;
9
10
  private connectionManager;
10
11
  private jobBuffer;
@@ -62,7 +63,10 @@ export declare class PrintJobManager implements IPrintJobManager {
62
63
  */
63
64
  private generateJobId;
64
65
  /**
65
- * Saves the current job state for resume later
66
+ * Saves the current job state for resume later.
67
+ *
68
+ * 默认实现使用内存存储。如需持久化(如 localStorage),
69
+ * 可通过 setSaveHandler/setLoadHandler 自定义。
66
70
  */
67
71
  private saveJobState;
68
72
  /**
@@ -1,7 +1,6 @@
1
1
  import { IAdapterOptions, IQrOptions, PrinterState, IPrinterAdapter } from '../../types';
2
2
  import { TextAlign, TextStyle } from '../../formatter';
3
3
  import { BarcodeOptions } from '../../barcode';
4
-
5
4
  /**
6
5
  * Connection Manager Interface
7
6
  *
@@ -1,6 +1,5 @@
1
1
  import { TextAlign } from '../formatter';
2
2
  import { BarcodeFormat } from '../barcode';
3
-
4
3
  /**
5
4
  * Template type
6
5
  */
package/package.json CHANGED
@@ -1,16 +1,16 @@
1
1
  {
2
2
  "name": "taro-bluetooth-print",
3
- "version": "2.2.0",
4
- "description": "Taro 蓝牙打印库 v2.2 - 轻量级、高性能、跨平台支持微信、支付宝、百度、字节跳动小程序及H5 Web Bluetooth",
3
+ "version": "2.3.0",
4
+ "description": "Taro 蓝牙打印库 v2.3 - 轻量级、高性能、跨平台支持微信、支付宝、百度、字节跳动小程序及H5 Web Bluetooth",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs.js",
7
7
  "module": "dist/index.es.js",
8
8
  "types": "dist/types/index.d.ts",
9
9
  "exports": {
10
10
  ".": {
11
+ "types": "./dist/types/index.d.ts",
11
12
  "import": "./dist/index.es.js",
12
13
  "require": "./dist/index.cjs.js",
13
- "types": "./dist/types/index.d.ts",
14
14
  "default": "./dist/index.umd.js"
15
15
  },
16
16
  "./package.json": "./package.json"
@@ -34,9 +34,9 @@
34
34
  "format:check": "prettier --check \"src/**/*.{ts,tsx,json,md}\"",
35
35
  "type-check": "tsc --noEmit",
36
36
  "prepublishOnly": "npm run build",
37
- "test": "jest",
38
- "test:watch": "jest --watch",
39
- "test:coverage": "jest --coverage"
37
+ "test": "vitest run",
38
+ "test:watch": "vitest",
39
+ "test:coverage": "vitest run --coverage"
40
40
  },
41
41
  "keywords": [
42
42
  "taro",
@@ -50,27 +50,25 @@
50
50
  "author": "Agions",
51
51
  "license": "MIT",
52
52
  "dependencies": {
53
- "@tarojs/taro": "^3.6.0"
53
+ "@tarojs/taro": "^4.1.11"
54
54
  },
55
55
  "devDependencies": {
56
- "@types/jest": "^29.5.11",
57
56
  "@types/node": "^20.14.8",
58
57
  "@typescript-eslint/eslint-plugin": "^6.19.0",
59
58
  "@typescript-eslint/parser": "^6.19.0",
59
+ "@vitest/coverage-v8": "^4.0.18",
60
60
  "eslint": "^8.56.0",
61
61
  "eslint-config-prettier": "^9.1.0",
62
62
  "eslint-plugin-prettier": "^5.1.3",
63
63
  "fast-check": "^4.5.3",
64
- "jest": "^29.7.0",
65
- "jest-environment-jsdom": "^29.7.0",
66
- "prettier": "^3.2.4",
67
- "rimraf": "^5.0.0",
68
- "terser": "^5.24.0",
69
- "ts-jest": "^29.1.1",
64
+ "prettier": "^3.8.1",
65
+ "rimraf": "^6.1.2",
66
+ "terser": "^5.46.0",
70
67
  "typescript": "^5.9.3",
71
- "vite": "^5.0.0",
72
- "vite-plugin-dts": "^3.9.1",
73
- "vitepress": "^1.0.0"
68
+ "vite": "^7.3.1",
69
+ "vite-plugin-dts": "^4.5.4",
70
+ "vitepress": "^1.0.0",
71
+ "vitest": "^4.0.18"
74
72
  },
75
73
  "sideEffects": false
76
- }
74
+ }