Skip to content

Commit

Permalink
sdk/nodejs: add send() and json() express-esque methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lithdew committed Jun 15, 2020
1 parent 8fbccf1 commit 4f34ace
Show file tree
Hide file tree
Showing 3 changed files with 385 additions and 10 deletions.
9 changes: 9 additions & 0 deletions nodejs/src/flatend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ export class Context extends Duplex {
return this;
}

send(data: string | Buffer | Uint8Array) {
this.write(data);
this.end();
}

json(data: any) {
this.send(JSON.stringify(data));
}

_writeHeader() {
if (this._written) return;

Expand Down
12 changes: 3 additions & 9 deletions nodejs/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import {Node, Context} from "./flatend";
import {Context, Node} from "./flatend";
import * as fs from "fs";

async function main() {
const node = new Node();
node.register("get_todos", (ctx: Context) => {
ctx.write("hello world");
ctx.end();
});
node.register("all_todos", (ctx: Context) => {
const stream = fs.createReadStream("tsconfig.json");
stream.pipe(ctx);
});
node.register("get_todos", (ctx: Context) => ctx.json(ctx.headers));
node.register("all_todos", (ctx: Context) => fs.createReadStream("tsconfig.json").pipe(ctx));
node.register('pipe', (ctx: Context) => ctx.pipe(ctx));
await node.dial("0.0.0.0:9000");
}
Expand Down
Loading

0 comments on commit 4f34ace

Please sign in to comment.