-
-
Notifications
You must be signed in to change notification settings - Fork 588
/
Copy pathstream-java.js
129 lines (107 loc) · 2.85 KB
/
stream-java.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
"use strict";
const ServiceBroker = require("../src/service-broker");
const fs = require("fs");
const path = require("path");
const kleur = require("kleur");
const crypto = require("crypto");
const broker = new ServiceBroker({
//namespace: "streaming",
nodeID: "node-client-" + process.pid,
//transporter: "nats://192.168.51.100:4222",
transporter: "redis://192.168.51.100:6379",
serializer: "MsgPack",
logger: console,
logLevel: "debug"
});
let startTime, origHash;
/*
const file = "grpc-netty-shaded-1.10.1.jar";
broker.start()
.delay(2000)
.then(() => {
startTime = Date.now();
return broker.call("stream.getFile", { file });
})
.then(stream => {
const s = fs.createWriteStream(path.join("d:", file));
stream.pipe(s);
s.on("close", () => {
broker.logger.info("Time:", Date.now() - startTime + "ms");
broker.logger.info(kleur.green().bold("File received OK!"));
broker.stop();
});
});
*/
/*
broker.start()
.delay(2000)
.then(() => {
const fileName = "d://1.pdf";
return getSHA(fileName).then(hash1 => {
origHash = hash1;
broker.logger.info("Original SHA:", hash1);
const startTime = Date.now();
const stream = fs.createReadStream(fileName);
broker.call("stream.sha", stream)
.then(({ digest }) => {
broker.logger.info("Time:", Date.now() - startTime + "ms");
broker.logger.info("Received SHA:", digest);
if (digest != origHash) {
broker.logger.error(kleur.red().bold("Hash mismatch!"));
} else {
broker.logger.info(kleur.green().bold("Hash OK!"));
}
broker.stop();
});
});
});
*/
broker
.start()
.delay(2000)
.then(() => {
const fileName = "d://1.pdf";
const fileName2 = "d://2.pdf";
return getSHA(fileName).then(hash1 => {
origHash = hash1;
broker.logger.info("Original SHA:", hash1);
const startTime = Date.now();
const stream = fs.createReadStream(fileName);
broker.call("echo.reply", stream).then(stream => {
const s = fs.createWriteStream(fileName2);
stream.pipe(s);
s.on("close", () => {
broker.logger.info("Time:", Date.now() - startTime + "ms");
getSHA(fileName2).then(hash => {
broker.logger.info("Received SHA:", hash);
if (hash != origHash) {
broker.logger.error(kleur.red().bold("Hash mismatch!"));
} else {
broker.logger.info(kleur.green().bold("Hash OK!"));
}
});
broker.stop();
});
});
});
});
/*
broker.start()
.delay(2000)
.then(() => {
return broker.call("stream.listFiles");
})
.then(res => {
broker.logger.info(res);
broker.stop();
});
*/
function getSHA(fileName) {
return new Promise((resolve, reject) => {
let hash = crypto.createHash("sha256");
let stream = fs.createReadStream(fileName);
stream.on("error", err => reject(err));
stream.on("data", chunk => hash.update(chunk));
stream.on("end", () => resolve(hash.digest("base64")));
});
}