Skip to content

Commit f4c6578

Browse files
committed
sdk/node, net, node, readme: create tests using mocha and chai, recode away all streams to use async generator instead to adhere to backpressure support, add support for sending messages to other nodes and listening for other nodes in nodejs, expose peer id in Context for both nodejs and go, update readme with new doc info
1 parent 2f4e1b5 commit f4c6578

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+11350
-944
lines changed

README.md

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,9 @@ import "io"
307307
import "io/ioutil"
308308

309309
func helloWorld(ctx *flatend.Context) {
310+
// The ID of the requester may be accessed via `ctx.ID`.
311+
_ = ctx.ID
312+
310313
// All headers must be written before writing any response body data.
311314

312315
// Headers are used to send small amounts of metadata to a requester.
@@ -347,27 +350,29 @@ func helloWorld(ctx *flatend.Context) {
347350
```js
348351
const {Node} = require("flatend");
349352

350-
interface NodeOpts {
351-
// A reachable, public address which peers may reach you on.
352-
// The format of the address must be [host]:[port].
353-
addr?: string;
354-
355-
// A list of addresses to nodes to initially reach out
356-
// for/bootstrap from first.
357-
addrs?: string;
358-
359-
// An Ed25519 keypair. A keypair must be provided to allow for
360-
// peers to reach you. A secret key may be generated by calling
361-
// 'nacl.sign.keyPair()' using the 'tweetnacl' library.
362-
keys?: nacl.SignKeyPair;
363-
364-
// A mapping of service names to their respective handlers.
365-
services?: { [key: string]: Handler };
366-
367-
// bindAddrs?: string[], (not supported yet)
353+
export interface NodeOptions {
354+
// A reachable, public address which peers may reach you on.
355+
// The format of the address must be [host]:[port].
356+
publicAddr?: string;
357+
358+
// A list of [host]:[port] addresses which this node will bind a listener
359+
// against to accept new Flatend nodes.
360+
bindAddrs?: string[];
361+
362+
// A list of addresses to nodes to initially reach out
363+
// for/bootstrap from first.
364+
addrs?: string[];
365+
366+
// An Ed25519 secret key. A secret key must be provided to allow for
367+
// peers to reach you. A secret key may be generated by calling
368+
// 'flatend.generateSecretKey()'.
369+
secretKey?: Uint8Array;
370+
371+
// A mapping of service names to their respective handlers.
372+
services?: { [key: string]: Handler };
368373
}
369374

370-
await Node.start(opts?: NodeOpts);
375+
await Node.start(opts: NodeOpts);
371376

372377
const {Context} = require("flatend");
373378

@@ -378,6 +383,8 @@ const helloWorld = async ctx => {
378383
// 'ctx' is a NodeJS Duplex stream. Writing to it writes a response
379384
// body, and reading from it reads a request body.
380385

386+
_ = ctx.id; // The ID of the requester.
387+
381388
ctx.pipe(ctx); // This would pipe all request data as response data.
382389

383390
// Headers are used to send small amounts of metadata to a requester.

examples/go/clock/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,4 @@ func main() {
149149
signal.Notify(ch, os.Interrupt)
150150
<-ch
151151
}
152-
```
152+
```

examples/go/counter/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ $ flatend
88
2020/06/17 02:12:53 Listening for HTTP requests on '[::]:3000'.
99
2020/06/17 02:12:59 <anon> has connected to you. Services: [count]
1010
11-
$ go run main.go
11+
$ go run main.go
1212
2020/06/17 02:13:00 You are now connected to 127.0.0.1:9000. Services: []
1313
1414
$ http://localhost:3000
@@ -68,4 +68,4 @@ func main() {
6868

6969
node.Shutdown()
7070
}
71-
```
71+
```

examples/go/file/README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ $ flatend
88
2020/06/17 02:36:30 Listening for HTTP requests on '[::]:3000'.
99
2020/06/17 02:36:41 <anon> has connected to you. Services: [file]
1010
11-
$ node index.js
11+
$ node index.js
1212
Successfully dialed 127.0.0.1:9000. Services: []
1313
1414
$ http://localhost:3000/
@@ -36,14 +36,14 @@ service = "file"
3636
```
3737

3838
```js
39-
const {Node} = require("flatend");
39+
const { Node } = require("flatend");
4040
const fs = require("fs");
4141

4242
const main = async () => {
43-
const node = new Node();
44-
node.register('file', ctx => fs.createReadStream("index.js").pipe(ctx));
45-
await node.dial("127.0.0.1:9000");
46-
}
43+
const node = new Node();
44+
node.register("file", (ctx) => fs.createReadStream("index.js").pipe(ctx));
45+
await node.dial("127.0.0.1:9000");
46+
};
4747

48-
main().catch(err => console.error(err));
49-
```
48+
main().catch((err) => console.error(err));
49+
```

examples/go/hello_world/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,4 @@ func main() {
6464

6565
node.Shutdown()
6666
}
67-
```
67+
```

examples/go/pipe/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,4 @@ func main() {
5959

6060
node.Shutdown()
6161
}
62-
```
62+
```

examples/go/todo/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,4 @@ func main() {
191191
node.Shutdown()
192192
check(db.Close())
193193
}
194-
```
194+
```

examples/go/todo/public/index.html

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
<!DOCTYPE html>
22
<html lang="en">
3-
<head>
4-
<meta charset="utf-8">
5-
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6-
<meta name="viewport" content="width=device-width,initial-scale=1.0">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
77
<title>Flatend Demo - Todo</title>
8-
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.8.0/css/bulma.min.css">
9-
<link rel="stylesheet" href="/style.css">
10-
</head>
11-
<body>
12-
<div id="root"></div>
13-
<script src="/main.js"></script>
14-
</body>
15-
</html>
8+
<link
9+
rel="stylesheet"
10+
href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.8.0/css/bulma.min.css"
11+
/>
12+
<link rel="stylesheet" href="/style.css" />
13+
</head>
14+
<body>
15+
<div id="root"></div>
16+
<script src="/main.js"></script>
17+
</body>
18+
</html>

examples/go/todo/public/main.js

Lines changed: 8230 additions & 3 deletions
Large diffs are not rendered by default.

examples/go/todo/public/style.css

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
form {
2-
display: flex;
3-
padding: 10px;
2+
display: flex;
3+
padding: 10px;
44
}
55

66
.wrapper {
7-
min-height: 100vh;
8-
display: flex;
9-
justify-content: center;
10-
align-items: center;
11-
background-image: url('data:image/svg+xml,%3Csvg xmlns="http://www.w3.org/2000/svg" width="4" height="4" viewBox="0 0 4 4"%3E%3Cpath fill="%239C92AC" fill-opacity="0.4" d="M1 3h1v1H1V3zm2-2h1v1H3V1z"%3E%3C/path%3E%3C/svg%3E');
7+
min-height: 100vh;
8+
display: flex;
9+
justify-content: center;
10+
align-items: center;
11+
background-image: url('data:image/svg+xml,%3Csvg xmlns="http://www.w3.org/2000/svg" width="4" height="4" viewBox="0 0 4 4"%3E%3Cpath fill="%239C92AC" fill-opacity="0.4" d="M1 3h1v1H1V3zm2-2h1v1H3V1z"%3E%3C/path%3E%3C/svg%3E');
1212
}
1313

1414
.input {
15-
margin-right: 10px;
15+
margin-right: 10px;
1616
}
1717

1818
.frame {
19-
width: 40vw;
20-
max-width: 400px;
19+
width: 40vw;
20+
max-width: 400px;
2121
}
2222

2323
.header {
24-
display: inline;
25-
text-align: center;
24+
display: inline;
25+
text-align: center;
2626
}
2727

2828
.list-wrapper {
29-
max-height: 200px;
30-
overflow-y: auto;
31-
}
29+
max-height: 200px;
30+
overflow-y: auto;
31+
}

0 commit comments

Comments
 (0)