isahaq-barcode 1.3.0 → 1.7.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.
package/README.md CHANGED
@@ -44,15 +44,23 @@ npm install isahaq-barcode
44
44
 
45
45
  **Requirements:** Node.js 18.0 or higher
46
46
 
47
- ## 🌐 Browser Compatibility
47
+ ## 🌐 Universal Compatibility
48
48
 
49
- > **Important:** This package is designed for **Node.js server-side environments** and is not compatible with browser environments.
49
+ > **Great News:** This package now works in **both Node.js and browser environments**!
50
50
 
51
- ### Why Not Browser Compatible?
51
+ ### Server-Side (Node.js)
52
52
 
53
- - **Canvas Dependency**: Uses Node.js `canvas` library for image generation
54
- - **File System Access**: Requires Node.js `fs` module for file operations
55
- - **Server-Side Rendering**: Designed for server-side barcode generation
53
+ - **Full Features**: All barcode types, formats, and advanced features
54
+ - **Canvas Support**: High-quality image generation with Node.js canvas
55
+ - **File Operations**: Save files directly to filesystem
56
+ - **CLI Tools**: Command-line interface available
57
+
58
+ ### ✅ Client-Side (Browser)
59
+
60
+ - **Core Features**: PNG, SVG, HTML generation
61
+ - **QR Codes**: Full QR code generation with customization
62
+ - **Barcode Types**: Code128, Code39, EAN13, UPC, and more
63
+ - **No Dependencies**: Works with just jsbarcode and qrcode
56
64
 
57
65
  ### Recommended Usage Patterns
58
66
 
@@ -83,11 +91,17 @@ export default function handler(req, res) {
83
91
  }
84
92
  ```
85
93
 
86
- **❌ Client-Side (Not Supported):**
94
+ **✅ Client-Side (Browser/React/Vue):**
87
95
 
88
96
  ```javascript
89
- // This will NOT work in browsers
90
- import BarcodeGenerator from 'isahaq-barcode'; // ❌
97
+ // Browser/React/Vue components
98
+ import BarcodeGenerator from 'isahaq-barcode';
99
+
100
+ // Generate barcode in browser
101
+ const generateBarcode = async () => {
102
+ const barcode = await BarcodeGenerator.png('123456789', 'code128');
103
+ // Use the barcode buffer
104
+ };
91
105
  ```
92
106
 
93
107
  ### Alternative for Client-Side
package/browser.js ADDED
@@ -0,0 +1,228 @@
1
+ /**
2
+ * Browser-compatible version of Isahaq Barcode Generator
3
+ * This version uses client-side libraries instead of Node.js modules
4
+ */
5
+
6
+ // Browser-compatible QR code generation
7
+ const QRCode = require('qrcode');
8
+
9
+ // Browser-compatible barcode generation using jsbarcode
10
+ const JsBarcode = require('jsbarcode');
11
+
12
+ class BrowserBarcodeGenerator {
13
+ /**
14
+ * Generate PNG barcode (browser-compatible)
15
+ * @param {string} data - Data to encode
16
+ * @param {string} type - Barcode type
17
+ * @param {Object} options - Options
18
+ * @returns {Promise<Buffer>} PNG buffer
19
+ */
20
+ static async png(data, type = 'code128', options = {}) {
21
+ try {
22
+ // Create a canvas element
23
+ const canvas = document.createElement('canvas');
24
+
25
+ // Generate barcode using jsbarcode
26
+ JsBarcode(canvas, data, {
27
+ format: type.toUpperCase(),
28
+ width: options.width || 2,
29
+ height: options.height || 100,
30
+ displayValue: options.displayValue !== false,
31
+ fontSize: options.fontSize || 20,
32
+ textAlign: options.textAlign || 'center',
33
+ textPosition: options.textPosition || 'bottom',
34
+ textMargin: options.textMargin || 2,
35
+ background: options.background || '#ffffff',
36
+ lineColor: options.lineColor || '#000000',
37
+ margin: options.margin || 10,
38
+ ...options,
39
+ });
40
+
41
+ // Convert canvas to blob
42
+ return new Promise(resolve => {
43
+ canvas.toBlob(blob => {
44
+ const reader = new FileReader();
45
+ reader.onload = () => resolve(Buffer.from(reader.result));
46
+ reader.readAsArrayBuffer(blob);
47
+ }, 'image/png');
48
+ });
49
+ } catch (error) {
50
+ throw new Error(`Failed to generate PNG barcode: ${error.message}`);
51
+ }
52
+ }
53
+
54
+ /**
55
+ * Generate SVG barcode (browser-compatible)
56
+ * @param {string} data - Data to encode
57
+ * @param {string} type - Barcode type
58
+ * @param {Object} options - Options
59
+ * @returns {string} SVG string
60
+ */
61
+ static svg(data, type = 'code128', options = {}) {
62
+ try {
63
+ // Create a temporary div to hold the SVG
64
+ const tempDiv = document.createElement('div');
65
+
66
+ // Generate barcode using jsbarcode
67
+ JsBarcode(tempDiv, data, {
68
+ format: type.toUpperCase(),
69
+ width: options.width || 2,
70
+ height: options.height || 100,
71
+ displayValue: options.displayValue !== false,
72
+ fontSize: options.fontSize || 20,
73
+ textAlign: options.textAlign || 'center',
74
+ textPosition: options.textPosition || 'bottom',
75
+ textMargin: options.textMargin || 2,
76
+ background: options.background || '#ffffff',
77
+ lineColor: options.lineColor || '#000000',
78
+ margin: options.margin || 10,
79
+ xmlDocument: document,
80
+ ...options,
81
+ });
82
+
83
+ return tempDiv.innerHTML;
84
+ } catch (error) {
85
+ throw new Error(`Failed to generate SVG barcode: ${error.message}`);
86
+ }
87
+ }
88
+
89
+ /**
90
+ * Generate HTML barcode (browser-compatible)
91
+ * @param {string} data - Data to encode
92
+ * @param {string} type - Barcode type
93
+ * @param {Object} options - Options
94
+ * @returns {string} HTML string
95
+ */
96
+ static html(data, type = 'code128', options = {}) {
97
+ const svg = this.svg(data, type, options);
98
+ return `<div class="barcode-container">${svg}</div>`;
99
+ }
100
+
101
+ /**
102
+ * Generate QR code (browser-compatible)
103
+ * @param {string} data - Data to encode
104
+ * @param {Object} options - Options
105
+ * @returns {Promise<Buffer>} PNG buffer
106
+ */
107
+ static async qrCode(data, options = {}) {
108
+ try {
109
+ const canvas = document.createElement('canvas');
110
+ await QRCode.toCanvas(canvas, data, {
111
+ width: options.size || 300,
112
+ margin: options.margin || 10,
113
+ color: {
114
+ dark: options.foregroundColor || '#000000',
115
+ light: options.backgroundColor || '#FFFFFF',
116
+ },
117
+ errorCorrectionLevel: options.errorCorrectionLevel || 'M',
118
+ ...options,
119
+ });
120
+
121
+ return new Promise(resolve => {
122
+ canvas.toBlob(blob => {
123
+ const reader = new FileReader();
124
+ reader.onload = () => resolve(Buffer.from(reader.result));
125
+ reader.readAsArrayBuffer(blob);
126
+ }, 'image/png');
127
+ });
128
+ } catch (error) {
129
+ throw new Error(`Failed to generate QR code: ${error.message}`);
130
+ }
131
+ }
132
+
133
+ /**
134
+ * Validate barcode data (browser-compatible)
135
+ * @param {string} data - Data to validate
136
+ * @param {string} type - Barcode type
137
+ * @returns {Object} Validation result
138
+ */
139
+ static validate(data, type) {
140
+ if (!data || typeof data !== 'string') {
141
+ return {
142
+ valid: false,
143
+ error: 'Data must be a non-empty string',
144
+ };
145
+ }
146
+
147
+ // Basic validation for common barcode types
148
+ switch (type.toLowerCase()) {
149
+ case 'code128':
150
+ if (!/^[\x00-\x7F]+$/.test(data)) {
151
+ return {
152
+ valid: false,
153
+ error: 'Code 128 supports ASCII characters only',
154
+ };
155
+ }
156
+ break;
157
+ case 'code39':
158
+ if (!/^[A-Z0-9\s\-\.\$\/\+\%]+$/.test(data)) {
159
+ return {
160
+ valid: false,
161
+ error: 'Code 39 supports characters: A-Z, 0-9, space, -.$/+%',
162
+ };
163
+ }
164
+ break;
165
+ case 'ean13':
166
+ if (!/^\d{12,13}$/.test(data)) {
167
+ return {
168
+ valid: false,
169
+ error: 'EAN-13 must be 12 or 13 digits',
170
+ };
171
+ }
172
+ break;
173
+ case 'qrcode':
174
+ if (data.length === 0) {
175
+ return {
176
+ valid: false,
177
+ error: 'QR Code data cannot be empty',
178
+ };
179
+ }
180
+ break;
181
+ }
182
+
183
+ return { valid: true };
184
+ }
185
+
186
+ /**
187
+ * Get supported barcode types
188
+ * @returns {Array} Array of supported types
189
+ */
190
+ static getBarcodeTypes() {
191
+ return [
192
+ 'code128',
193
+ 'code128a',
194
+ 'code128b',
195
+ 'code128c',
196
+ 'code128auto',
197
+ 'code39',
198
+ 'code39extended',
199
+ 'code39checksum',
200
+ 'code39auto',
201
+ 'code93',
202
+ 'ean13',
203
+ 'ean8',
204
+ 'upca',
205
+ 'upce',
206
+ 'codabar',
207
+ 'code11',
208
+ 'msi',
209
+ 'pharmazentral',
210
+ 'interleaved25',
211
+ 'standard25',
212
+ 'qrcode',
213
+ 'datamatrix',
214
+ 'pdf417',
215
+ 'aztec',
216
+ ];
217
+ }
218
+
219
+ /**
220
+ * Get supported render formats
221
+ * @returns {Array} Array of supported formats
222
+ */
223
+ static getRenderFormats() {
224
+ return ['png', 'svg', 'html'];
225
+ }
226
+ }
227
+
228
+ module.exports = BrowserBarcodeGenerator;
package/eslint.config.js CHANGED
@@ -15,6 +15,7 @@ module.exports = [
15
15
  require: 'readonly',
16
16
  exports: 'readonly',
17
17
  document: 'readonly',
18
+ FileReader: 'readonly',
18
19
  },
19
20
  },
20
21
  plugins: {
package/index.js CHANGED
@@ -3,132 +3,152 @@
3
3
  * A universal barcode generator package supporting multiple barcode types and output formats
4
4
  */
5
5
 
6
- const BarcodeService = require('./src/services/BarcodeService');
7
- const QrCodeBuilder = require('./src/builders/QrCodeBuilder');
8
- const { BarcodeTypes } = require('./src/types/BarcodeTypes');
9
- const { RenderFormats } = require('./src/renderers/RenderFormats');
6
+ // Detect environment
7
+ const isBrowser =
8
+ typeof window !== 'undefined' && typeof document !== 'undefined';
10
9
 
11
- // Main service instance
12
- const barcodeService = new BarcodeService();
10
+ if (isBrowser) {
11
+ // Browser environment - use browser-compatible version
12
+ module.exports = require('./browser');
13
+ } else {
14
+ // Node.js environment - use full-featured version
15
+ const BarcodeService = require('./src/services/BarcodeService');
16
+ const QrCodeBuilder = require('./src/builders/QrCodeBuilder');
17
+ const { BarcodeTypes } = require('./src/types/BarcodeTypes');
18
+ const { RenderFormats } = require('./src/renderers/RenderFormats');
13
19
 
14
- /**
15
- * Main Barcode Generator Class
16
- */
17
- class BarcodeGenerator {
18
- /**
19
- * Generate a barcode in PNG format
20
- * @param {string} data - The data to encode
21
- * @param {string} type - The barcode type
22
- * @param {Object} options - Additional options
23
- * @returns {Buffer} PNG buffer
24
- */
25
- static png(data, type = 'code128', options = {}) {
26
- return barcodeService.generate(data, type, 'png', options);
27
- }
20
+ // Main service instance
21
+ const barcodeService = new BarcodeService();
28
22
 
29
23
  /**
30
- * Generate a barcode in SVG format
31
- * @param {string} data - The data to encode
32
- * @param {string} type - The barcode type
33
- * @param {Object} options - Additional options
34
- * @returns {string} SVG string
24
+ * Main Barcode Generator Class
35
25
  */
36
- static svg(data, type = 'code128', options = {}) {
37
- return barcodeService.generate(data, type, 'svg', options);
38
- }
26
+ class BarcodeGenerator {
27
+ /**
28
+ * Generate a barcode in PNG format
29
+ * @param {string} data - The data to encode
30
+ * @param {string} type - The barcode type
31
+ * @param {Object} options - Generation options
32
+ * @returns {Buffer} PNG buffer
33
+ */
34
+ static png(data, type = 'code128', options = {}) {
35
+ return barcodeService.generate(data, type, 'png', options);
36
+ }
39
37
 
40
- /**
41
- * Generate a barcode in HTML format
42
- * @param {string} data - The data to encode
43
- * @param {string} type - The barcode type
44
- * @param {Object} options - Additional options
45
- * @returns {string} HTML string
46
- */
47
- static html(data, type = 'code128', options = {}) {
48
- return barcodeService.generate(data, type, 'html', options);
49
- }
38
+ /**
39
+ * Generate a barcode in SVG format
40
+ * @param {string} data - The data to encode
41
+ * @param {string} type - The barcode type
42
+ * @param {Object} options - Generation options
43
+ * @returns {string} SVG string
44
+ */
45
+ static svg(data, type = 'code128', options = {}) {
46
+ return barcodeService.generate(data, type, 'svg', options);
47
+ }
50
48
 
51
- /**
52
- * Generate a barcode in PDF format
53
- * @param {string} data - The data to encode
54
- * @param {string} type - The barcode type
55
- * @param {Object} options - Additional options
56
- * @returns {Buffer} PDF buffer
57
- */
58
- static pdf(data, type = 'code128', options = {}) {
59
- return barcodeService.generate(data, type, 'pdf', options);
60
- }
49
+ /**
50
+ * Generate a barcode in HTML format
51
+ * @param {string} data - The data to encode
52
+ * @param {string} type - The barcode type
53
+ * @param {Object} options - Generation options
54
+ * @returns {string} HTML string
55
+ */
56
+ static html(data, type = 'code128', options = {}) {
57
+ return barcodeService.generate(data, type, 'html', options);
58
+ }
61
59
 
62
- /**
63
- * Generate a QR code with advanced features
64
- * @param {Object} options - QR code options
65
- * @returns {Object} QR code result
66
- */
67
- static modernQr(options = {}) {
68
- return QrCodeBuilder.create(options);
69
- }
60
+ /**
61
+ * Generate a barcode in PDF format
62
+ * @param {string} data - The data to encode
63
+ * @param {string} type - The barcode type
64
+ * @param {Object} options - Generation options
65
+ * @returns {Promise<Buffer>} PDF buffer
66
+ */
67
+ static async pdf(data, type = 'code128', options = {}) {
68
+ return barcodeService.generate(data, type, 'pdf', options);
69
+ }
70
70
 
71
- /**
72
- * Get available barcode types
73
- * @returns {Array} Array of barcode types
74
- */
75
- static getBarcodeTypes() {
76
- return BarcodeTypes.getAll();
77
- }
71
+ /**
72
+ * Generate a QR code
73
+ * @param {string} data - The data to encode
74
+ * @param {Object} options - Generation options
75
+ * @returns {QrCodeBuilder} QR code builder instance
76
+ */
77
+ static qrCode(data, options = {}) {
78
+ return new QrCodeBuilder({ data, ...options });
79
+ }
78
80
 
79
- /**
80
- * Get available render formats
81
- * @returns {Array} Array of render formats
82
- */
83
- static getRenderFormats() {
84
- return RenderFormats.getAll();
85
- }
81
+ /**
82
+ * Validate barcode data
83
+ * @param {string} data - The data to validate
84
+ * @param {string} type - The barcode type
85
+ * @returns {Object} Validation result
86
+ */
87
+ static validate(data, type) {
88
+ return barcodeService.validate(data, type);
89
+ }
86
90
 
87
- /**
88
- * Validate data for a specific barcode type
89
- * @param {string} data - The data to validate
90
- * @param {string} type - The barcode type
91
- * @returns {Object} Validation result
92
- */
93
- static validate(data, type) {
94
- return barcodeService.validate(data, type);
95
- }
91
+ /**
92
+ * Generate multiple barcodes in batch
93
+ * @param {Array} items - Array of barcode configurations
94
+ * @returns {Promise<Array>} Array of generated barcodes
95
+ */
96
+ static async batch(items) {
97
+ return barcodeService.batch(items);
98
+ }
96
99
 
97
- /**
98
- * Generate multiple barcodes in batch
99
- * @param {Array} items - Array of barcode generation items
100
- * @returns {Array} Array of generated barcodes
101
- */
102
- static batch(items) {
103
- return barcodeService.batch(items);
104
- }
100
+ /**
101
+ * Get supported barcode types
102
+ * @returns {Array} Array of supported types
103
+ */
104
+ static getBarcodeTypes() {
105
+ return BarcodeTypes.getTypes();
106
+ }
105
107
 
106
- /**
107
- * Get watermark positions for QR codes
108
- * @returns {Array} Array of watermark positions
109
- */
110
- static getWatermarkPositions() {
111
- return [
112
- 'top-left',
113
- 'top-right',
114
- 'bottom-left',
115
- 'bottom-right',
116
- 'center',
117
- 'top-center',
118
- 'bottom-center',
119
- 'left-center',
120
- 'right-center',
121
- ];
122
- }
123
- }
108
+ /**
109
+ * Get supported render formats
110
+ * @returns {Array} Array of supported formats
111
+ */
112
+ static getRenderFormats() {
113
+ return RenderFormats.getFormats();
114
+ }
124
115
 
125
- // Export main class and utilities
126
- module.exports = BarcodeGenerator;
127
- module.exports.BarcodeGenerator = BarcodeGenerator;
128
- module.exports.BarcodeService = BarcodeService;
129
- module.exports.QrCodeBuilder = QrCodeBuilder;
130
- module.exports.BarcodeTypes = BarcodeTypes;
131
- module.exports.RenderFormats = RenderFormats;
116
+ /**
117
+ * Get watermark positions
118
+ * @returns {Array} Array of watermark positions
119
+ */
120
+ static getWatermarkPositions() {
121
+ return [
122
+ 'top-left',
123
+ 'top-center',
124
+ 'top-right',
125
+ 'left-center',
126
+ 'center',
127
+ 'right-center',
128
+ 'bottom-left',
129
+ 'bottom-center',
130
+ 'bottom-right',
131
+ ];
132
+ }
132
133
 
133
- // Default export
134
- module.exports.default = BarcodeGenerator;
134
+ /**
135
+ * Get barcode type information
136
+ * @param {string} type - The barcode type
137
+ * @returns {Object} Type information
138
+ */
139
+ static getBarcodeTypeInfo(type) {
140
+ return BarcodeTypes.getTypeInfo(type);
141
+ }
142
+
143
+ /**
144
+ * Get render format information
145
+ * @param {string} format - The render format
146
+ * @returns {Object} Format information
147
+ */
148
+ static getRenderFormatInfo(format) {
149
+ return RenderFormats.getFormatInfo(format);
150
+ }
151
+ }
152
+
153
+ module.exports = BarcodeGenerator;
154
+ }
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "isahaq-barcode",
3
- "version": "1.3.0",
3
+ "version": "1.7.0",
4
4
  "description": "A universal barcode generator package supporting multiple barcode types and output formats, with extra features like batch generation, watermarking, validation, CLI, and Express.js integration",
5
5
  "main": "index.js",
6
6
  "browser": {
7
7
  "fs": false,
8
8
  "path": false,
9
- "os": false
9
+ "os": false,
10
+ "canvas": false
10
11
  },
11
12
  "bin": {
12
13
  "barcode-generate": "./bin/generate.js"
@@ -54,6 +55,10 @@
54
55
  "pdfkit": "^0.17.2",
55
56
  "qrcode": "^1.5.4"
56
57
  },
58
+ "peerDependencies": {
59
+ "jsbarcode": "^3.12.1",
60
+ "qrcode": "^1.5.4"
61
+ },
57
62
  "devDependencies": {
58
63
  "@types/node": "^22.10.0",
59
64
  "eslint": "^9.38.0",
@@ -10,7 +10,7 @@ let fs;
10
10
  try {
11
11
  fs = require('fs').promises;
12
12
  } catch {
13
- // fs not available in browser environments
13
+ // fs not available in browser environment
14
14
  fs = null;
15
15
  }
16
16