-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
197 lines (179 loc) · 5.43 KB
/
app.js
File metadata and controls
197 lines (179 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import cors from "cors";
import * as dotenv from "dotenv";
import express from "express";
import fetch from "node-fetch";
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
const app = express();
dotenv.config();
app.use(cors());
app.use(express.json({ type: "*/*" }));
const fakeDefaultPrinter = {
deviceType: "printer",
uid: 'default-fake-printer',
provider: "com.zebra.ds.webdriver.desktop.provider.DefaultDeviceProvider",
name: "fake printer",
connection: "network",
version: 4,
manufacturer: "Zebra Technologies",
};
let lastCommand = "";
function virtualPrint(zpl) {
const port = process.env.CHROME_EXTENSION_PORT ?? "9102";
fetch(`http://localhost:${port}`, {
method: "POST",
body: '{"mode":"print","epl":"' + Buffer.from(zpl) + '"}',
}).catch((e) => {
console.error("Error while sending print request to extension", e);
});
}
app.get("/default", (_, res) => {
console.log("GET /default");
sleep(250).then(() => res.json(fakeDefaultPrinter));
});
app.get("/available", (_, res) => {
console.log("GET /available");
let data = {
printer: [fakeDefaultPrinter, Object.assign({}, fakeDefaultPrinter),Object.assign({}, fakeDefaultPrinter)].map((printer, idx) => {
if (idx === 0) {
return printer;
}
printer.name += `_${idx}`;
printer.uid = 'printer-xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
})
return printer;
})
};
sleep(250).then(() => res.json(data));
});
app.get("/config", (_, res) => {
console.log("GET /config");
let data = {
application: {
supportedConversions: {
jpg: ["cpcl", "zpl", "kpl"],
tif: ["cpcl", "zpl", "kpl"],
pdf: ["cpcl", "zpl", "kpl"],
bmp: ["cpcl", "zpl", "kpl"],
pcx: ["cpcl", "zpl", "kpl"],
gif: ["cpcl", "zpl", "kpl"],
png: ["cpcl", "zpl", "kpl"],
jpeg: ["cpcl", "zpl", "kpl"],
},
version: "1.3.1.421",
apiLevel: 4,
buildNumber: 421,
platform: "macos",
},
};
sleep(250).then(() => res.json(data));
});
app.post("/read", (_, res) => {
console.log("POST /read");
console.log(
"Last command:",
lastCommand
.replace(/\\/g, "\\\\")
);
let responseStr = "";
if (lastCommand === "^XA^HH^XZ") {
responseStr = `\x02
15.0 DARKNESS
5 IPS PRINT SPEED
+000 TEAR OFF
TEAR OFF PRINT MODE
GAP/NOTCH MEDIA TYPE
WEB SENSOR TYPE
MANUAL SENSOR SELECT
609 PRINT WIDTH
1558 LABEL LENGTH
39.0IN 989MM MAXIMUM LENGTH
CONNECTED USB COMM.
NONE PROTOCOL
<~> 7EH CONTROL CHAR
<^> 5EH COMMAND CHAR
<,> 2CH DELIM. CHAR
ZPL II ZPL MODE
NO MOTION MEDIA POWER UP
FEED HEAD CLOSE
DEFAULT BACKFEED
+000 LABEL TOP
+0000 LEFT POSITION
NO HEXDUMP
045 WEB S.
096 MEDIA S.
007 WEB GAIN
050 MARK S.
020 MARK GAIN
095 MARK MED S.
024 MARK MEDIA GAIN
095 CONT MEDIA S.
007 CONT MEDIA GAIN
066 TAKE LABEL
CWF MODES ENABLED
... MODES DISABLED
832 8/MM FULL RESOLUTION
V61.17.17Z <- FIRMWARE
1.3 XML SCHEMA
V30.00.00 HARDWARE ID
CUSTOMIZED CONFIGURATION
2104k............R: RAM
1536k............E: ONBOARD FLASH
NONE FORMAT CONVERT
DISABLED ZBI
2.1 ZBI VERSION
3,454 IN LAST CLEANED
3,454 IN HEAD USAGE
3,454 IN TOTAL USAGE
3,454 IN RESET CNTR1
3,454 IN RESET CNTR2
28J171602337 SERIAL NUMBER
MAINT. OFF EARLY WARNING
\x03`;
}
if (lastCommand === `~hs\r\n`) {
responseStr = `\x02030,0,0,1558,000,0,0,0,000,0,0,0\x03
\x02000,0,0,0,0,2,4,0,00000000,1,000\x03
\x021234,0\x03
`;
}
lastCommand = "";
sleep(250).then(() => res.send(responseStr));
});
app.post("/write", (req, res) => {
console.log("POST request for /write recieved");
lastCommand = req?.body?.data || null;
if (lastCommand) {
console.log(
"Last command:",
lastCommand
.replace(/\n/g, "\\n")
.replace(/\r/g, "\\r")
.replace(/\t/g, "\\t")
);
} else {
console.log("No data found in request body");
}
if (lastCommand.startsWith('^xa') && process.env.CHROME_EXTENSION_ENABLED === String(true)) {
virtualPrint(lastCommand);
}
sleep(250).then(() => res.send({}));
});
const SERVER_PORT = 9100;
const EXTENSION_PORT = process.env.CHROME_EXTENSION_PORT ?? "9102";
if (process.env.CHROME_EXTENSION_ENABLED === String(true)) {
console.info(
"Preview ZPL with https://chrome.google.com/webstore/detail/zpl-printer/phoidlklenidapnijkabnfdgmadlcmjo\n" +
"Configure the extension to use port " + EXTENSION_PORT
);
} else {
console.info("Chrome extension support not enabled.");
}
app.listen(SERVER_PORT, "127.0.0.1", () => {
console.log(
`Browser Print Fake Server running on http://localhost:${SERVER_PORT}`
);
});