Skip to content

Commit

Permalink
sdk/nodejs, readme: prepare to publish for npm, cleanup readme
Browse files Browse the repository at this point in the history
  • Loading branch information
lithdew committed Jun 16, 2020
1 parent aeeac8a commit bd47556
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.idea/
**/node_modules/
**/build/
**/dist/
12 changes: 3 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,13 @@ Write functions in your favorite language, using your favorite tools and platfor

### Keep it simple, be flexible.

If you're like me, you dream of spending only 5$ a month to leave your product/service sustaining thousands of customers and requests per second.

At this golden age of SaaS' and serverless and Docker containers and low-code tools and configuration hell, that dream is slowly starting to fade away.
If you're like me, you dream of spending only 5$ a month to leave your product/service sustaining thousands of customers and requests per second. At this golden age of SaaS' and serverless and Docker containers and low-code tools and configuration hell, that dream is slowly starting to fade away.

Flatend uses the battle-tested p2p overlay networking protocol Kademlia to give you the building blocks necessary to achieve this dream with as much flexibility and as little code as possible.

With Kademlia, microservices built with Flatend come with service discovery, load balancing, routing, and fully-encrypted industrial-grade end-to-end encryption built in. All of that without the rigor of configuring Kubernetes/Istio/Consul over and over again.

Of course, if you love Kubernetes/Istio/Consul, then use it with Flatend. Run your Flatend services in Docker containers, on Kubernetes, or on AWS hassle-free.

Either way, Flatend is as minimal as it gets for you to be able to make the most out of your single 2$/month bare-metal server, or fleet of 10,000$/month cloud instances.
Of course, if you love Kubernetes/Istio/Consul, then use it with Flatend. Run your Flatend services in Docker containers, on Kubernetes, or on AWS hassle-free. Either way, Flatend is as minimal as it gets for you to be able to make the most out of your single 2$/month bare-metal server, or fleet of 10,000$/month cloud instances.

### Production-ready from the start.

Expand All @@ -53,9 +49,7 @@ It's agnostic to the point that you can serve your microservice on your laptop,

### Security as a service.

All communication across microservices written with Flatend are fully-encrypted end-to-end using industrial-grade AES-256 Galois Counter Mode (GCM).

The encryption key is established by a X25519 Diffie-Hellman handshake, whose results are passed through BLAKE-2b 256-bit.
All communication across microservices written with Flatend are fully-encrypted end-to-end using industrial-grade AES-256 Galois Counter Mode (GCM). The encryption key is established by a X25519 Diffie-Hellman handshake, whose results are passed through BLAKE-2b 256-bit.

Nonces used for encrypting/decrypting messages are unsigned big-endian 64-bit integers incremented per message. The only information exposed on each packet is a single prefixed 32-bit unsigned integer designating a packet's length.

Expand Down
9 changes: 9 additions & 0 deletions nodejs/example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "example",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"devDependencies": {
"typescript": "^3.9.5"
}
}
18 changes: 18 additions & 0 deletions nodejs/example/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {Context, Node} from "flatend";
import * as fs from "fs";

async function main() {
const node = new Node();

node.register("get_todos", (ctx: Context) => ctx.json(ctx.headers));
node.register("all_todos", (ctx: Context) => fs.createReadStream("./nodejs/package.json").pipe(ctx));

node.register('pipe', async (ctx: Context) => {
const body = await ctx.body({limit: 65536});
ctx.json(JSON.parse(body.toString("utf8")));
});

await node.dial("0.0.0.0:9000");
}

main().catch(err => console.error(err));
70 changes: 70 additions & 0 deletions nodejs/example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */

/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "dist", /* Redirect output structure to the directory. */
"rootDir": "src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */

/* Advanced Options */
"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
},
"exclude": ["node_modules", "dist"]
}
8 changes: 8 additions & 0 deletions nodejs/example/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


typescript@^3.9.5:
version "3.9.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.9.5.tgz#586f0dba300cde8be52dd1ac4f7e1009c1b13f36"
integrity sha512-hSAifV3k+i6lEoCJ2k6R2Z/rp/H3+8sdmcn5NrS3/3kE7+RyZXm9aqvxWqjEXHAd8b0pShatpcdMTvEdvAJltQ==
5 changes: 4 additions & 1 deletion nodejs/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"name": "flatend",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"start": "rm -rf ./build && yarn tsc && node build/index.js"
"prepublish": "yarn tsc",
"start": "rm -rf dist && yarn tsc && node dist/index.js"
},
"dependencies": {
"blake2b": "^2.1.3",
Expand Down
3 changes: 2 additions & 1 deletion nodejs/src/flatend.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "core-js/shim";
import "core-js";

import {DataPacket, HandshakePacket, ID, Opcode, ServiceRequestPacket, ServiceResponsePacket} from "./packet";
import nacl from "tweetnacl";
import assert from "assert";
Expand Down
19 changes: 1 addition & 18 deletions nodejs/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1 @@
import {Context, Node} from "./flatend";
import * as fs from "fs";

async function main() {
const node = new Node();

node.register("get_todos", (ctx: Context) => ctx.json(ctx.headers));
node.register("all_todos", (ctx: Context) => fs.createReadStream("./nodejs/package.json").pipe(ctx));

node.register('pipe', async (ctx: Context) => {
const body = await ctx.body({limit: 65536});
ctx.json(JSON.parse(body.toString("utf8")));
});

await node.dial("0.0.0.0:9000");
}

main().catch(err => console.error(err));
export {Context, Node} from "./flatend";
1 change: 0 additions & 1 deletion nodejs/src/packet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ export class ServiceResponsePacket implements Packet {
handled: boolean = false;
headers: { [key: string]: string };


public constructor(id: number, handled: boolean, headers: { [key: string]: string }) {
this.id = id;
this.handled = handled;
Expand Down
13 changes: 8 additions & 5 deletions nodejs/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
"jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
"declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "build",
"outDir": "dist",
/* Redirect output structure to the directory. */
"rootDir": "src",

/* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
Expand Down Expand Up @@ -80,5 +81,7 @@
/* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true
/* Disallow inconsistently-cased references to the same file. */
}
},
"exclude": ["example",
"dist", "node_modules"]
}

0 comments on commit bd47556

Please sign in to comment.