-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjs_integration.rs
More file actions
284 lines (244 loc) · 7.86 KB
/
js_integration.rs
File metadata and controls
284 lines (244 loc) · 7.86 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
mod common;
use std::time::Duration;
use hypercore::{
replication::{CoreInfo, CoreMethods},
PartialKeypair, VerifyingKey,
};
use macros::start_func_with;
use tokio::{net::TcpListener, spawn};
use tokio_util::compat::TokioAsyncReadCompatExt;
use utils::ram_core;
use rusty_nodejs_repl::{Config, Repl};
use common::{
js::{path_to_node_modules, require_js_data},
serialize_public_key, Result, LOOPBACK,
};
use replicator::{utils::make_reader_and_writer_keys, ReplicatingCore};
async fn rust_writer_js_reader<A: AsRef<[u8]>, B: AsRef<[A]>>(
batch: B,
) -> Result<(ReplicatingCore, Repl)> {
let (rkey, wkey) = make_reader_and_writer_keys();
let core: ReplicatingCore = ram_core(Some(&wkey)).await.into();
let listener = TcpListener::bind(format!("{}:0", LOOPBACK)).await?;
let port = format!("{}", listener.local_addr()?.port());
let hostname = LOOPBACK;
let replicating_core = core.clone();
spawn(async move {
let stream = listener.accept().await?.0;
replicating_core.add_stream(stream.compat(), false).await;
Ok::<(), std::io::Error>(())
});
for b in batch.as_ref().iter() {
core.append(b.as_ref()).await?;
}
let mut conf = Config::build()?;
conf.imports.push(
"
RAM = require('random-access-memory');
Hypercore = require('hypercore');
net = require('net');
"
.into(),
);
conf.before.push(format!(
"
key = '{}';
core = new Hypercore(RAM, key);
await core.ready();
",
serialize_public_key(&rkey)
));
conf.after.push("await core.close()".into());
conf.before.push(format!(
"
socket = net.connect('{port}', '{hostname}');
socket.pipe(core.replicate(true)).pipe(socket);
await core.update({{wait: true}});
"
));
conf.after.push("socket.destroy();".into());
conf.path_to_node_modules = Some(path_to_node_modules()?.display().to_string());
Ok((core, conf.start()?))
}
async fn setup_js_writer_rust_reader() -> Result<(ReplicatingCore, Repl)> {
let listener = TcpListener::bind(format!("{}:0", LOOPBACK)).await?;
let mut conf = Config::build()?;
conf.imports.push(
"
RAM = require('random-access-memory');
Hypercore = require('hypercore');
net = require('net');
"
.into(),
);
conf.before.push(
"
core = new Hypercore(RAM);
"
.into(),
);
conf.after.push("await core.close()".into());
conf.before.push(format!(
"
socket = net.connect('{}', '{LOOPBACK}');
socket.pipe(core.replicate(false)).pipe(socket);
",
listener.local_addr()?.port()
));
conf.after.push("socket.destroy();".into());
conf.path_to_node_modules = Some(path_to_node_modules()?.display().to_string());
let mut repl = conf.start()?;
// get the public key from the JS core for the RS core
let key = repl
.run("process.stdout.write(core.key.toString('hex'))")
.await?;
let key: [u8; 32] = data_encoding::HEXLOWER
.decode(&key)?
.as_slice()
.try_into()?;
let core: ReplicatingCore = ram_core(Some(&PartialKeypair {
public: VerifyingKey::from_bytes(&key)?,
secret: None,
}))
.await
.into();
let replicating_core = core.clone();
spawn(async move {
let stream = listener.accept().await?.0;
replicating_core.add_stream(stream.compat(), true).await;
Ok::<(), std::io::Error>(())
});
repl.run("await core.ready();").await?;
Ok((core, repl))
}
#[start_func_with(require_js_data()?;)]
#[tokio::test]
async fn initial_rust_data_replicates_to_js() -> Result<()> {
let batch: &[&[u8]] = &[b"hi\n", b"ola\n", b"hello\n", b"mundo\n"];
let (_core, mut repl) = rust_writer_js_reader(batch).await?;
// print the length of the core so we can check it in rust
let result = repl
.run("process.stdout.write(String((await core.info()).length));")
.await?;
let expected: Vec<u8> = format!("{}", batch.len()).bytes().collect();
assert_eq!(result, expected);
for (i, val) in batch.iter().enumerate() {
let code = format!("process.stdout.write(String((await core.get({i}))));");
let stdout = repl.run(&code).await?;
assert_eq!(stdout, *val);
}
// stop the repl. When repl is stopped hypercore & socket are closed
let _ = repl.stop().await?;
// ensure js process closes properly
assert_eq!(repl.child.output().await?.status.code(), Some(0));
Ok(())
}
#[start_func_with(require_js_data()?;)]
#[tokio::test]
async fn added_rust_data_replicates_to_js() -> Result<()> {
let initial_datas = [b"a", b"b", b"c"];
let (core, mut repl) = rust_writer_js_reader(&initial_datas).await?;
let _res = repl
.run(
"
await core.update({wait: true});
await new Promise(r => setTimeout(r, 1e2))
",
)
.await?;
let stdout = repl
.run("process.stdout.write(String((await core.info()).length));")
.await?;
assert_eq!(stdout, b"3");
for (i, l) in "defghijklmnopqrstuvwxyz".bytes().enumerate() {
core.append(&[l]).await?;
let new_size = initial_datas.len() + i;
let stdout = repl
.run(
"
await core.update({wait: true});
await new Promise(r => setTimeout(r, 1e2))
process.stdout.write(String((await core.info()).length));",
)
.await?;
let len: Vec<u8> = format!("{}", new_size + 1).bytes().collect();
assert_eq!(stdout, len);
let code = format!("process.stdout.write(String((await core.get({new_size}))));");
let stdout = repl.run(&code).await?;
assert_eq!(stdout, &[l]);
}
// stop the repl. When repl is stopped hypercore & socket are closed
let _ = repl.stop().await?;
let _ = repl.child.output().await?;
Ok(())
}
#[start_func_with(require_js_data()?;)]
#[tokio::test]
async fn js_writer_replicates_to_rust_reader() -> Result<()> {
let (core, mut repl) = setup_js_writer_rust_reader().await?;
for i in 0..10 {
repl.run(&format!("await core.append(Buffer.from([{i}]));"))
.await?;
loop {
// this does not work without thin check to info..
if let Some(x) = core.get(i).await? {
assert_eq!(x, vec![i as u8]);
break;
}
tokio::time::sleep(Duration::from_millis(25)).await;
}
}
Ok(())
}
// TODO finish this to replace other replication setup code
#[allow(unused)]
async fn make_js_slave_from_rs(
core: ReplicatingCore,
js_core_name: &str,
repl: &mut Repl,
) -> Result<()> {
let listener = TcpListener::bind(format!("{}:0", LOOPBACK)).await?;
let port = format!("{}", listener.local_addr()?.port());
let hostname = LOOPBACK;
let replicating_core = core.clone();
spawn(async move {
let stream = listener.accept().await?.0;
replicating_core.add_stream(stream.compat(), false).await;
Ok::<(), std::io::Error>(())
});
let k = core.key_pair().await;
repl.run(&format!(
"
{js_core_name} = (async () => {{
const key = '{}';
const core = new Hypercore(RAM, key);
await core.ready();
const socket = net.connect('{port}', '{hostname}');
socket.pipe(core.replicate(true)).pipe(socket);
await core.update({{wait: true}});
return core;
}})();
",
serialize_public_key(&k),
))
.await?;
Ok(())
}
#[start_func_with(require_js_data()?;)]
#[tokio::test]
async fn new_js_slave() -> Result<()> {
let (core, mut repl) = setup_js_writer_rust_reader().await?;
for i in 0..10 {
repl.run(&format!("await core.append(Buffer.from([{i}]));"))
.await?;
loop {
// this does not work without thin check to info..
if let Some(x) = core.get(i).await? {
assert_eq!(x, vec![i as u8]);
break;
}
tokio::time::sleep(Duration::from_millis(25)).await;
}
}
Ok(())
}