isahaq-barcode 1.2.0 → 1.2.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.
- package/README.md +55 -1
- package/package.json +6 -1
- package/src/builders/QrCodeBuilder.js +14 -1
package/README.md
CHANGED
|
@@ -42,7 +42,61 @@
|
|
|
42
42
|
npm install isahaq-barcode
|
|
43
43
|
```
|
|
44
44
|
|
|
45
|
-
**Requirements:** Node.js
|
|
45
|
+
**Requirements:** Node.js 18.0 or higher
|
|
46
|
+
|
|
47
|
+
## 🌐 Browser Compatibility
|
|
48
|
+
|
|
49
|
+
> **Important:** This package is designed for **Node.js server-side environments** and is not compatible with browser environments.
|
|
50
|
+
|
|
51
|
+
### Why Not Browser Compatible?
|
|
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
|
|
56
|
+
|
|
57
|
+
### Recommended Usage Patterns
|
|
58
|
+
|
|
59
|
+
**✅ Server-Side (Recommended):**
|
|
60
|
+
|
|
61
|
+
```javascript
|
|
62
|
+
// Node.js/Express.js server
|
|
63
|
+
const BarcodeGenerator = require('isahaq-barcode');
|
|
64
|
+
|
|
65
|
+
// Generate barcode on server
|
|
66
|
+
app.get('/barcode/:data', (req, res) => {
|
|
67
|
+
const barcode = BarcodeGenerator.png(req.params.data, 'code128');
|
|
68
|
+
res.set('Content-Type', 'image/png');
|
|
69
|
+
res.send(barcode);
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
**✅ Next.js API Routes:**
|
|
74
|
+
|
|
75
|
+
```javascript
|
|
76
|
+
// pages/api/barcode.js or app/api/barcode/route.js
|
|
77
|
+
import BarcodeGenerator from 'isahaq-barcode';
|
|
78
|
+
|
|
79
|
+
export default function handler(req, res) {
|
|
80
|
+
const barcode = BarcodeGenerator.png(req.query.data, 'code128');
|
|
81
|
+
res.setHeader('Content-Type', 'image/png');
|
|
82
|
+
res.send(barcode);
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
**❌ Client-Side (Not Supported):**
|
|
87
|
+
|
|
88
|
+
```javascript
|
|
89
|
+
// This will NOT work in browsers
|
|
90
|
+
import BarcodeGenerator from 'isahaq-barcode'; // ❌
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Alternative for Client-Side
|
|
94
|
+
|
|
95
|
+
For browser-based barcode generation, consider:
|
|
96
|
+
|
|
97
|
+
- **jsbarcode** - Client-side barcode generation
|
|
98
|
+
- **qrcode** - Client-side QR code generation
|
|
99
|
+
- **Server-side API** - Use this package on your backend
|
|
46
100
|
|
|
47
101
|
**Global CLI Installation:**
|
|
48
102
|
|
package/package.json
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "isahaq-barcode",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.1",
|
|
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
|
+
"browser": {
|
|
7
|
+
"fs": false,
|
|
8
|
+
"path": false,
|
|
9
|
+
"os": false
|
|
10
|
+
},
|
|
6
11
|
"bin": {
|
|
7
12
|
"barcode-generate": "./bin/generate.js"
|
|
8
13
|
},
|
|
@@ -4,7 +4,15 @@
|
|
|
4
4
|
|
|
5
5
|
const QRCode = require('qrcode');
|
|
6
6
|
const { createCanvas, loadImage } = require('canvas');
|
|
7
|
-
|
|
7
|
+
|
|
8
|
+
// Conditional fs import for Node.js environments only
|
|
9
|
+
let fs;
|
|
10
|
+
try {
|
|
11
|
+
fs = require('fs').promises;
|
|
12
|
+
} catch {
|
|
13
|
+
// fs not available in browser environments
|
|
14
|
+
fs = null;
|
|
15
|
+
}
|
|
8
16
|
|
|
9
17
|
class QrCodeBuilder {
|
|
10
18
|
constructor(options = {}) {
|
|
@@ -375,6 +383,11 @@ class QrCodeResult {
|
|
|
375
383
|
* @returns {Promise<void>}
|
|
376
384
|
*/
|
|
377
385
|
async saveToFile(filePath) {
|
|
386
|
+
if (!fs) {
|
|
387
|
+
throw new Error(
|
|
388
|
+
'File system operations are not available in browser environments. Use generate() or getDataUri() methods instead.'
|
|
389
|
+
);
|
|
390
|
+
}
|
|
378
391
|
const buffer = await this.generate();
|
|
379
392
|
await fs.writeFile(filePath, buffer);
|
|
380
393
|
}
|