Skip to content

Commit

Permalink
2.0.0: types & structure.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael committed Feb 2, 2019
1 parent 3dc9d43 commit 6d5501c
Show file tree
Hide file tree
Showing 16 changed files with 195 additions and 86 deletions.
2 changes: 2 additions & 0 deletions dist/src/SHA1.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare const _default: (str1: string) => string;
export default _default;
22 changes: 22 additions & 0 deletions dist/src/WS.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import "./types";
declare class WebSocketClient implements wsc.WebSocketClient {
private open;
private ws;
private forcibly_closed;
private reconnect_timeout;
private queue;
private messages;
private onReadyQueue;
private onCloseQueue;
private config;
private init_flush;
private log;
private connect;
readonly socket: any;
ready(): Promise<{}>;
on(event_name: string, handler: (data: any) => any, predicate?: (data: any) => boolean): void;
close(): wsc.AsyncErrCode;
send(message_data: any, opts?: wsc.SendOptions): wsc.AsyncErrCode;
constructor(user_config?: wsc.UserConfig);
}
export default WebSocketClient;
3 changes: 3 additions & 0 deletions dist/src/connectLib.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import "./types";
declare const connectLib: (ff: any) => any;
export default connectLib;
46 changes: 46 additions & 0 deletions dist/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
declare namespace wsc {
/** Stuff that in use by this lib. */
interface Socket {
readyState: number;
send(...any: any[]): void;
close(): void;
addEventListener(event: string, handler: ((event: any) => any), ...any: any[]): void;
}
type AsyncErrCode = Promise<number | null | {}>;
type EventHandler = (e: any) => void;
type DataPipe = (message: any) => any;
type DataType = 'json' | 'string';
interface Config {
data_type: DataType;
log(event: string, time?: number, message?: any): void;
log(event: string, message?: any): void;
timer: boolean;
url: string;
timeout: number;
reconnect: number;
lazy: boolean;
socket: Socket;
adapter: (host: string, protocols?: string[]) => Socket;
encode: (key: string, message: any, config: Config) => any;
decode: (rawMessage: any) => {
[id_or_data_key: string]: string;
};
protocols: string[];
pipes: DataPipe[];
server: {
id_key: string;
data_key: string;
};
}
type UserConfig = Partial<Config>;
interface SendOptions {
top: any;
data_type: DataType;
}
class WebSocketClient {
on(event_name: string, handler: (event: string) => void, predicate?: (event: string) => boolean): void;
close(): Promise<void | {}>;
send(user_message: any, opts: wsc.SendOptions): AsyncErrCode;
constructor(user_config: wsc.UserConfig);
}
}
4 changes: 4 additions & 0 deletions dist/src/utils.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import "./types";
declare const add_event: (o: wsc.Socket, e: string, handler: wsc.EventHandler) => void;
declare const once: (fn: Function) => (...args: any) => any;
export { add_event, once, };
2 changes: 1 addition & 1 deletion dist/ws.esm.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/ws.js.map

Large diffs are not rendered by default.

95 changes: 64 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"Easy",
"realtime",
"Middleware",
"JSON",
"Data",
"transport",
"API",
Expand All @@ -34,15 +35,15 @@
},
"scripts": {
"lint": "./node_modules/.bin/tslint 'src/**/*.ts'",
"test": "npm run test:compile && npm run test:exec",
"test": "npm run prod:es && npm run test:compile && npm run test:exec",
"test:compile": "./node_modules/.bin/tsc -p ./test/",
"test:exec": "npm run prod:es && ./node_modules/.bin/ava ./test/dist/test/src/index.js",
"dev": "rollup --watch -c --config src/rollup.config.js --environment INCLUDE_DEPS,BUILD:development",
"prod:cjs": "rollup -c --config src/rollup.config.js --environment INCLUDE_DEPS,BUILD:cjs",
"prod:es": "rollup -c --config src/rollup.config.js --environment INCLUDE_DEPS,BUILD:es",
"test:exec": "./node_modules/.bin/ava ./test/dist/test/src/index.js",
"dev": "rollup --watch -c --config rollup.config.js --environment INCLUDE_DEPS,BUILD:development",
"prod:cjs": "rollup -c --config rollup.config.js --environment INCLUDE_DEPS,BUILD:cjs",
"prod:es": "rollup -c --config rollup.config.js --environment INCLUDE_DEPS,BUILD:es",
"prod": "npm run prod:es && npm run prod:cjs"
},
"version": "1.1.3",
"version": "2.0.0",
"devDependencies": {
"@types/node": "^8.0.58",
"@types/ramda": "^0.25.8",
Expand All @@ -51,6 +52,7 @@
"axios": "^0.17.1",
"express": "^4.16.2",
"ramda": "^0.25.0",
"randomatic": "^3.1.1",
"rollup": "^0.52.1",
"rollup-plugin-alias": "^1.4.0",
"rollup-plugin-commonjs": "^8.2.6",
Expand All @@ -63,5 +65,5 @@
"uglify-es": "^3.3.9",
"ws": "^3.3.2"
},
"types": "./index.d.ts"
"types": "./dist/src/WS.d.ts"
}
2 changes: 1 addition & 1 deletion src/rollup.config.js → rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default {
commonjs(),
typescript({
typescript: require("typescript"),
tsconfig: "./src/tsconfig.json",
tsconfig: "./tsconfig.json",
tsconfigOverride: {
compilerOptions: {
sourceMap: false,
Expand Down
9 changes: 5 additions & 4 deletions src/SHA1.js → src/SHA1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
// SHA1 has been taken from https://github.com/jbt/js-crypto
// Thank you, James, for this tiny implementation!

export default (str1) => {
export default (str1: string) => {
for (
var blockstart = 0,
i = 0,
W = [],
A, B, C, D, F, G,
A: number | any[] | number[],
B: number, C: number, D: number, F: number, G: number,
H = [A=0x67452301, B=0xEFCDAB89, ~A, ~B, 0xC3D2E1F0],
word_array = [],
temp2,
temp2: number,
s = unescape(encodeURI(str1)),
str_len = s.length;

Expand All @@ -25,7 +26,7 @@ export default (str1) => {

for (; i < 80;
A = [[
(G = ((s = A[0]) << 5 | s >>> 27) + A[4] + (W[i] = (i<16) ? ~~word_array[blockstart + i] : G << 1 | G >>> 31) + 1518500249) + ((B = A[1]) & (C = A[2]) | ~B & (D = A[3])),
(G = ((s = A[0]) << 5 | (s as any >>> 27)) + A[4] + (W[i] = (i<16) ? ~~word_array[blockstart + i] : G << 1 | G >>> 31) + 1518500249) + ((B = A[1]) & (C = A[2]) | ~B & (D = A[3])),
F = G + (B ^ C ^ D) + 341275144,
G + (B & C | B & D | C & D) + 882459459,
F + 1535694389
Expand Down
30 changes: 18 additions & 12 deletions src/WS.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@

import SHA1 from './SHA1.js'
import * as types from '../types'
import SHA1 from './SHA1'
import connectLib from './connectLib'
import { add_event } from './utils'
import "./types"

/* .send(your_data) wraps request to server with {id: `hash`, data: `actually your data`},
returns a Promise, that will be rejected after a timeout or
resolved if server returns the same signature: {id: `same_hash`, data: `response data`}
*/

const sett = (
a: number,
b: { (): void; (...args: any[]): void; }
) => setTimeout(b, a)

const sett = (a, b) => setTimeout(b, a)

const default_config = <types.Config>{
const default_config = <wsc.Config>{
data_type: 'json', // ToDo some other stuff maybe.
// Debug features.
log: ((event = '', time = 0, message = '') => null),
Expand All @@ -38,7 +40,7 @@ const default_config = <types.Config>{
}


class WebSocketClient implements types.WebSocketClient {
class WebSocketClient implements wsc.WebSocketClient {

private open = null
private ws = null
Expand All @@ -48,7 +50,7 @@ class WebSocketClient implements types.WebSocketClient {
private messages = []
private onReadyQueue = []
private onCloseQueue = []
private config = <types.Config>{}
private config = <wsc.Config>{}

private init_flush(): void {
this.queue = {} // data queuse
Expand Down Expand Up @@ -89,15 +91,19 @@ class WebSocketClient implements types.WebSocketClient {
})
}

public on(event_name, handler, predicate?) {
public on(
event_name: string,
handler: (data: any) => any,
predicate?: (data: any) => boolean
) {
return add_event(this.ws, event_name, event => {
if(!predicate || predicate(event)) {
handler(event)
}
})
}

public async close(): types.AsyncErrCode {
public async close(): wsc.AsyncErrCode {
return new Promise((ff, rj) => {
if(this.ws === null) {
rj('WSP: closing a non-inited socket!')
Expand All @@ -114,7 +120,7 @@ class WebSocketClient implements types.WebSocketClient {
})
}

public async send(message_data, opts = <types.SendOptions>{}): types.AsyncErrCode {
public async send(message_data: any, opts = <wsc.SendOptions>{}): wsc.AsyncErrCode {
this.log('Send.', message_data)
const config = this.config
const message = {}
Expand Down Expand Up @@ -166,9 +172,9 @@ class WebSocketClient implements types.WebSocketClient {
}


constructor(user_config = {}) {
constructor(user_config: wsc.UserConfig = {}) {
// Config.
const config = {} as types.Config
const config = {} as wsc.Config
Object.assign(config, default_config)
Object.assign(config, user_config)
this.config = config
Expand Down

0 comments on commit 6d5501c

Please sign in to comment.