-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmessage.js
186 lines (149 loc) · 4.93 KB
/
message.js
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
const fs = require('fs')
const net = require('net');
const path = require('path')
const { renderMessage, pad, debug } = require('./util');
const h = require('./headers')
class Packet {
static from(header, data='') {
const head = Buffer.from(header);
const chunk = Buffer.from(data);
let sizeHex = chunk.length.toString(16);
sizeHex = pad(sizeHex, 4);
const size = Buffer.from(sizeHex);
const delimiter = Buffer.from('@');
const packet = Buffer.concat([head, size, chunk, delimiter]);
return packet;
}
}
class Messanger {
/**
*
* @param {net.Socket} socket
*/
constructor(socket) {
this.socket = socket;
this.messageCallbacks = []
this.fileCallbacks = [];
this.buffer = Buffer.alloc(0);
/** @type {Array<Buffer>} */
this.fileBuffers = [];
/** @type {File} */
this.file = {}
this.i = 1;
this.socket.on('data', data => {
this.processBuffer(data);
if(this.i%100==0) {
debug('data came '+data.length + ` ${this.i} BufferLen : ${this.buffer.length} file: ${this.fileBuffers.length}`, renderMessage)
}
this.i++;
})
}
sendMessage(text) {
const packet = Packet.from(h.TEXT, text);
this.socket.write(packet);
return this;
}
sendFile(filePath) {
return new Promise((resolve, reject) => {
const metadata = {
name: path.basename(filePath),
ext: path.extname(filePath),
size: fs.statSync(filePath).size
}
debug(metadata, renderMessage, false);
const metadataPacket = Packet.from(h.FILE_START, JSON.stringify(metadata));
this.socket.write(metadataPacket);
const CHUNK_SIZE = 16 * 1024;
const readStream = fs.createReadStream(filePath, { highWaterMark: CHUNK_SIZE });
readStream.on('data', chunk => {
const packet = Packet.from(h.FILE, chunk);
this.socket.write(packet);
})
readStream.on('end', () => {
const packet = Packet.from(h.FILE_END);
this.socket.write(packet);
resolve()
})
})
}
/**
* @typedef {Object} Metadata
* @property {string} name
* @property {string} ext
* @property {number} size
* @typedef {Object} File
* @property {Metadata} metadata
* @property {Buffer} data
*/
/**
* @callback dataCallback
* @param {string|File} data
*/
/**
* Event listeners for incoming messages or files
* @param {'message'|'file'} type
* @param {dataCallback} cb
*/
on(type, cb) {
if (type == 'message') {
this.messageCallbacks.push(cb);
}
else if (type == 'file') {
this.fileCallbacks.push(cb);
}
return this;
}
emit(event, args) {
switch (event) {
case 'message': {
this.messageCallbacks.forEach(cb => {
cb(args)
})
break;
}
case 'file': {
this.fileCallbacks.forEach(cb => {
cb(args)
})
}
}
}
processBuffer(data) {
this.buffer = Buffer.concat([this.buffer, data]);
while (this.buffer.length > 8) { // head+size+delimiter
const head = this.buffer.slice(0, 4).toString();
const sizeHex = this.buffer.slice(4, 8);
const size = parseInt(sizeHex, 16);
if (this.buffer.length < size + 9) {
break;
}
if (head == h.TEXT) {
const content = this.buffer.slice(8, 8 + size);
this.emit('message', content.toString());
}
else if(head == h.FILE_START) {
const content = this.buffer.slice(8, 8+size).toString();
debug(content, renderMessage);
this.file.metadata = JSON.parse(content);
}
else if (head == h.FILE) {
const content = this.buffer.slice(8, 8 + size);
const delimiter = this.buffer.slice(8 + size, 9 + size).toString();
this.fileBuffers.push(content)
// renderMessage("delm "+delimiter);
}
else if(head == h.FILE_END) {
this.file.data = Buffer.concat(this.fileBuffers);
this.fileBuffers = [];
this.emit('file', this.file);
this.file = {};
}
else {
renderMessage('something wrong with header ' + head)
process.exit(1)
}
this.buffer = this.buffer.slice(size + 9);
}
}
}
module.exports = Messanger;