@alizadefaig/pos-server 1.2.18 → 1.2.20
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/app/socket/server.js +17 -0
- package/app/tcp.js +4 -28
- package/package.json +2 -2
- package/routes/desktop/api/printers.js +14 -1
package/app/socket/server.js
CHANGED
|
@@ -37,6 +37,22 @@ module.exports = (server = null) => {
|
|
|
37
37
|
logger.error(error);
|
|
38
38
|
});
|
|
39
39
|
|
|
40
|
+
// API-over-socket: frontend sends API calls over the existing WS connection
|
|
41
|
+
socket.on('api:request', async ({ request_id, event, data, headers }) => {
|
|
42
|
+
if (!request_id || !event) return;
|
|
43
|
+
const responseEvent = `api:response:${request_id}`;
|
|
44
|
+
try {
|
|
45
|
+
const response = await axios.post(
|
|
46
|
+
`${PROXY_TARGET}/api/${event}`,
|
|
47
|
+
data ?? {},
|
|
48
|
+
{ headers: { 'Content-Type': 'application/json', ...headers }, timeout: 15000 }
|
|
49
|
+
);
|
|
50
|
+
socket.emit(responseEvent, { success: true, data: response.data });
|
|
51
|
+
} catch (err) {
|
|
52
|
+
socket.emit(responseEvent, { success: false, error: err?.response?.data ?? { message: err.message } });
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
|
|
40
56
|
socket.on('setUserId', (id) => {
|
|
41
57
|
userId = id;
|
|
42
58
|
|
|
@@ -66,6 +82,7 @@ module.exports = (server = null) => {
|
|
|
66
82
|
});
|
|
67
83
|
|
|
68
84
|
socket.onAny((eventName, ...data) => {
|
|
85
|
+
if (eventName.startsWith('api:')) return; // handled by api:request handler
|
|
69
86
|
axios.post(`${PROXY_TARGET}/api/socket/${eventName}`, {
|
|
70
87
|
companyId,
|
|
71
88
|
terminalId,
|
package/app/tcp.js
CHANGED
|
@@ -37,49 +37,25 @@ module.exports = function (io = null) {
|
|
|
37
37
|
// Обработка полного сообщения
|
|
38
38
|
try {
|
|
39
39
|
const rawData = messageBuffer.toString('utf8');
|
|
40
|
-
|
|
41
|
-
let profiling = {};
|
|
42
|
-
if (payload.hasOwnProperty('profiling')) {
|
|
43
|
-
profiling = payload.profiling;
|
|
44
|
-
profiling['12_connection_received'] = connectedAt;
|
|
45
|
-
payload = payload.data;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
// payload содержит: event, company_id, to_id, data и т.д.
|
|
50
|
-
|
|
51
|
-
// Логика отправки в Socket.IO
|
|
52
|
-
// Пример: отправляем в конкретную комнату или всем
|
|
40
|
+
const payload = JSON.parse(rawData);
|
|
53
41
|
|
|
54
42
|
const companyId = payload.company_id;
|
|
55
43
|
const eventName = payload.event;
|
|
56
44
|
const data = payload.data;
|
|
57
45
|
|
|
58
46
|
if (!companyId || !data || !eventName) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
message: 'Required fields is not set',
|
|
62
|
-
});
|
|
47
|
+
console.error('Required fields is not set', payload);
|
|
48
|
+
return;
|
|
63
49
|
}
|
|
64
50
|
|
|
65
51
|
const to = payload.to;
|
|
66
52
|
const excepts = payload.excepts;
|
|
67
53
|
const toId = payload.toId;
|
|
68
|
-
const now = Date.now();
|
|
69
|
-
// const nowUtc = moment.utc();
|
|
70
54
|
|
|
71
|
-
const logger = getLogger();
|
|
72
|
-
// logger.info(`Start: ${now} ${companyId} - ${JSON.stringify(payload)}`)
|
|
73
|
-
console.log(`Start: ${now} ${companyId} - ${JSON.stringify(payload)}`);
|
|
74
|
-
profiling['13_sent_to_websocket'] = Date.now();
|
|
75
55
|
socketSendByFilter({
|
|
76
56
|
companyId,
|
|
77
57
|
eventName,
|
|
78
|
-
data
|
|
79
|
-
...data,
|
|
80
|
-
profiling: profiling,
|
|
81
|
-
// _server_time: nowUtc?.valueOf()
|
|
82
|
-
},
|
|
58
|
+
data,
|
|
83
59
|
to,
|
|
84
60
|
toId,
|
|
85
61
|
excepts
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alizadefaig/pos-server",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.20",
|
|
4
4
|
"description": "Server in express js with Proxy to Main server if need and will work with Electron JS",
|
|
5
5
|
"homepage": "https://gitlab.com/faigalizade/pos-server#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -39,4 +39,4 @@
|
|
|
39
39
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
40
40
|
"start": "node server.js"
|
|
41
41
|
}
|
|
42
|
-
}
|
|
42
|
+
}
|
|
@@ -95,12 +95,20 @@ router.get('/printers.get', async (req, res) => {
|
|
|
95
95
|
let usbPrinters = await getUsbPrinters();
|
|
96
96
|
let networkPrinters = await getNetworkDevices();
|
|
97
97
|
const printers = [...usbPrinters, ...networkPrinters]
|
|
98
|
+
const headers = { ...req.headers };
|
|
99
|
+
delete headers['accept-encoding'];
|
|
98
100
|
axios.post(PROXY_TARGET + '/api/printers.get', {
|
|
99
101
|
printers,
|
|
100
102
|
body: req.body,
|
|
101
103
|
params: req.query
|
|
102
104
|
}, {
|
|
103
|
-
headers: {
|
|
105
|
+
headers: {
|
|
106
|
+
...headers,
|
|
107
|
+
host: new URL(PROXY_TARGET).host,
|
|
108
|
+
Accept: 'application/json',
|
|
109
|
+
// Можно явно указать безопасные методы
|
|
110
|
+
'Accept-Encoding': 'gzip, deflate, br'
|
|
111
|
+
}
|
|
104
112
|
}).then(({data}) => {
|
|
105
113
|
res.json(data);
|
|
106
114
|
}).catch(e => {
|
|
@@ -114,6 +122,11 @@ router.get('/printers.getUsbPrinters', async (req, res) => {
|
|
|
114
122
|
})
|
|
115
123
|
|
|
116
124
|
router.post('/printers.print', async (req, res) => {
|
|
125
|
+
if (process.env.APP_DEBUG === 'true') {
|
|
126
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
127
|
+
return res.json({ success: true, mock: true });
|
|
128
|
+
}
|
|
129
|
+
|
|
117
130
|
const device = getPrinterDevice(req.body.printer);
|
|
118
131
|
const printMethod = req.body.print_method || 'raster';
|
|
119
132
|
const printMode = req.body.mode;
|