koa-ws-socket
Advanced tools
Comparing version 1.0.2 to 2.0.0
@@ -0,4 +1,53 @@ | ||
import crypto from 'crypto'; | ||
import { Server } from 'ws'; | ||
import crypto from 'crypto'; | ||
/** | ||
* Expose compositor. | ||
*/ | ||
var koaCompose = compose; | ||
/** | ||
* Compose `middleware` returning | ||
* a fully valid middleware comprised | ||
* of all those which are passed. | ||
* | ||
* @param {Array} middleware | ||
* @return {Function} | ||
* @api public | ||
*/ | ||
function compose (middleware) { | ||
if (!Array.isArray(middleware)) throw new TypeError('Middleware stack must be an array!') | ||
for (const fn of middleware) { | ||
if (typeof fn !== 'function') throw new TypeError('Middleware must be composed of functions!') | ||
} | ||
/** | ||
* @param {Object} context | ||
* @return {Promise} | ||
* @api public | ||
*/ | ||
return function (context, next) { | ||
// last called middleware # | ||
let index = -1; | ||
return dispatch(0) | ||
function dispatch (i) { | ||
if (i <= index) return Promise.reject(new Error('next() called multiple times')) | ||
index = i; | ||
let fn = middleware[i]; | ||
if (i === middleware.length) fn = next; | ||
if (!fn) return Promise.resolve() | ||
try { | ||
return Promise.resolve(fn(context, function next () { | ||
return dispatch(i + 1) | ||
})) | ||
} catch (err) { | ||
return Promise.reject(err) | ||
} | ||
} | ||
} | ||
} | ||
function createCommonjsModule(fn, module) { | ||
@@ -128,20 +177,7 @@ return module = { exports: {} }, fn(module, module.exports), module.exports; | ||
***************************************************************************** */ | ||
/* global Reflect, Promise */ | ||
function __awaiter(thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
@@ -205,29 +241,20 @@ step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
var _this = this; | ||
this.ws.on(event, function (message) { | ||
var ctx = { | ||
event: event, | ||
data: message, | ||
request: _this.request, | ||
socket: _this.ws, | ||
}; | ||
_this.invokeHandler(handler, ctx, message); | ||
}); | ||
}; | ||
Socket.prototype.invokeHandler = function (handler, ctx, message) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
this.ws.on(event, function (message) { return __awaiter(_this, void 0, void 0, function () { | ||
var ctx; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!!this.middleware) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, handler(ctx, message)]; | ||
ctx = { | ||
event: event, | ||
data: message, | ||
request: this.request, | ||
socket: this.ws, | ||
}; | ||
return [4 /*yield*/, this.middleware(ctx, function () { return handler(ctx, message); })]; | ||
case 1: | ||
_a.sent(); | ||
return [2 /*return*/]; | ||
case 2: return [4 /*yield*/, this.middleware(ctx, function () { return handler(ctx, message); })]; | ||
case 3: | ||
_a.sent(); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}); }); | ||
}; | ||
@@ -237,3 +264,2 @@ return Socket; | ||
var compose = require('koa-compose'); | ||
var WsSocket = /** @class */ (function () { | ||
@@ -243,3 +269,3 @@ function WsSocket() { | ||
this.middleware = []; | ||
this.composed = null; | ||
this.composed = koaCompose([]); | ||
this.connections = new Map(); | ||
@@ -297,3 +323,3 @@ this.listeners = new Map(); | ||
this.middleware.push(fn); | ||
this.composed = compose(this.middleware); | ||
this.composed = koaCompose(this.middleware); | ||
this.updateConnections(); | ||
@@ -300,0 +326,0 @@ return this; |
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('ws'), require('crypto')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'ws', 'crypto'], factory) : | ||
(factory((global.koaWsSocket = {}),global.WebSocket,global.crypto)); | ||
}(this, (function (exports,WebSocket,crypto) { 'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('crypto'), require('ws')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'crypto', 'ws'], factory) : | ||
(factory((global.koaWsSocket = {}),global.crypto,global.WebSocket)); | ||
}(this, (function (exports,crypto,WebSocket) { 'use strict'; | ||
crypto = crypto && crypto.hasOwnProperty('default') ? crypto['default'] : crypto; | ||
/** | ||
* Expose compositor. | ||
*/ | ||
var koaCompose = compose; | ||
/** | ||
* Compose `middleware` returning | ||
* a fully valid middleware comprised | ||
* of all those which are passed. | ||
* | ||
* @param {Array} middleware | ||
* @return {Function} | ||
* @api public | ||
*/ | ||
function compose (middleware) { | ||
if (!Array.isArray(middleware)) throw new TypeError('Middleware stack must be an array!') | ||
for (const fn of middleware) { | ||
if (typeof fn !== 'function') throw new TypeError('Middleware must be composed of functions!') | ||
} | ||
/** | ||
* @param {Object} context | ||
* @return {Promise} | ||
* @api public | ||
*/ | ||
return function (context, next) { | ||
// last called middleware # | ||
let index = -1; | ||
return dispatch(0) | ||
function dispatch (i) { | ||
if (i <= index) return Promise.reject(new Error('next() called multiple times')) | ||
index = i; | ||
let fn = middleware[i]; | ||
if (i === middleware.length) fn = next; | ||
if (!fn) return Promise.resolve() | ||
try { | ||
return Promise.resolve(fn(context, function next () { | ||
return dispatch(i + 1) | ||
})) | ||
} catch (err) { | ||
return Promise.reject(err) | ||
} | ||
} | ||
} | ||
} | ||
function createCommonjsModule(fn, module) { | ||
@@ -133,20 +182,7 @@ return module = { exports: {} }, fn(module, module.exports), module.exports; | ||
***************************************************************************** */ | ||
/* global Reflect, Promise */ | ||
function __awaiter(thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
@@ -210,29 +246,20 @@ step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
var _this = this; | ||
this.ws.on(event, function (message) { | ||
var ctx = { | ||
event: event, | ||
data: message, | ||
request: _this.request, | ||
socket: _this.ws, | ||
}; | ||
_this.invokeHandler(handler, ctx, message); | ||
}); | ||
}; | ||
Socket.prototype.invokeHandler = function (handler, ctx, message) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
this.ws.on(event, function (message) { return __awaiter(_this, void 0, void 0, function () { | ||
var ctx; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!!this.middleware) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, handler(ctx, message)]; | ||
ctx = { | ||
event: event, | ||
data: message, | ||
request: this.request, | ||
socket: this.ws, | ||
}; | ||
return [4 /*yield*/, this.middleware(ctx, function () { return handler(ctx, message); })]; | ||
case 1: | ||
_a.sent(); | ||
return [2 /*return*/]; | ||
case 2: return [4 /*yield*/, this.middleware(ctx, function () { return handler(ctx, message); })]; | ||
case 3: | ||
_a.sent(); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}); }); | ||
}; | ||
@@ -242,3 +269,2 @@ return Socket; | ||
var compose = require('koa-compose'); | ||
var WsSocket = /** @class */ (function () { | ||
@@ -248,3 +274,3 @@ function WsSocket() { | ||
this.middleware = []; | ||
this.composed = null; | ||
this.composed = koaCompose([]); | ||
this.connections = new Map(); | ||
@@ -302,3 +328,3 @@ this.listeners = new Map(); | ||
this.middleware.push(fn); | ||
this.composed = compose(this.middleware); | ||
this.composed = koaCompose(this.middleware); | ||
this.updateConnections(); | ||
@@ -305,0 +331,0 @@ return this; |
@@ -8,3 +8,3 @@ /// <reference types="node" /> | ||
import * as Koa from 'koa'; | ||
import * as composeTypes from 'koa-compose'; | ||
import compose from 'koa-compose'; | ||
import * as WebSocket from 'ws'; | ||
@@ -22,3 +22,3 @@ import Socket from './socket'; | ||
middleware: Middleware[]; | ||
composed: composeTypes.ComposedMiddleware<SocketContext> | null; | ||
composed: compose.ComposedMiddleware<SocketContext>; | ||
connections: Map<string, Socket>; | ||
@@ -25,0 +25,0 @@ listeners: Map<string, EventHandler[]>; |
@@ -9,7 +9,7 @@ /// <reference types="koa-compose" /> | ||
export default class Socket { | ||
middleware: compose.ComposedMiddleware<SocketContext> | null; | ||
middleware: compose.ComposedMiddleware<SocketContext>; | ||
ws: WebSocket; | ||
request: http.ServerRequest; | ||
constructor(ws: WebSocket, request: http.ServerRequest, listeners: Map<string, EventHandler[]>, middleware: compose.ComposedMiddleware<SocketContext> | null); | ||
update(listeners: Map<string, EventHandler[]>, middleware: compose.ComposedMiddleware<SocketContext> | null): void; | ||
constructor(ws: WebSocket, request: http.ServerRequest, listeners: Map<string, EventHandler[]>, middleware: compose.ComposedMiddleware<SocketContext>); | ||
update(listeners: Map<string, EventHandler[]>, middleware: compose.ComposedMiddleware<SocketContext>): void; | ||
/** | ||
@@ -19,3 +19,2 @@ * Adds a specific event and callback to this socket | ||
on(event: string, handler: EventHandler): void; | ||
invokeHandler(handler: EventHandler, ctx: SocketContext, message: any): Promise<void>; | ||
} |
@@ -1,1 +0,1 @@ | ||
{"name":"koa-ws-socket","version":"1.0.2","description":"Attaches ws sockets to koa using koa-style middleware","files":["dist"],"main":"dist/koa-ws-socket.umd.js","module":"dist/koa-ws-socket.es5.js","types":"dist/types/index.d.ts","repository":"https://github.com/pbomb/koa-ws-socket.git","author":"Matt Parrish <matt.r.parrish@gmail.com>","engines":{"node":">=6.0.0"},"license":"MIT","private":false,"scripts":{"add-contributor":"kcd-scripts contributors add","prebuild":"rimraf dist && rimraf compiled","build":"rollup --config rollup.config.ts --no-clean","lint":"tslint -p . -c tslint.json -e **/node_modules/**/* -e **/typings/**/* **/*.ts","test":"kcd-scripts test","test:cover":"kcd-scripts test --coverage","test:update":"npm run test:cover -s -- --updateSnapshot","test:build":"kcd-scripts test --no-watch","build-and-test":"npm run build -s && npm run test:build -s","validate":"kcd-scripts validate lint,build-and-test,test:cover","precommit":"kcd-scripts precommit","prettier":"prettier --write \"**/*.ts\" \"**/*.js{,on}\"","tsc":"tsc"},"devDependencies":{"@types/jest":"21.1.5","@types/koa":"2.0.40","@types/koa-compose":"3.2.2","@types/ws":"3.2.0","kcd-scripts":"0.27.2","koa":"2.4.1","prettier":"1.8.2","rimraf":"2.6.2","rollup":"0.51.2","rollup-plugin-commonjs":"8.2.6","rollup-plugin-node-resolve":"3.0.0","rollup-plugin-sourcemaps":"0.4.2","rollup-plugin-typescript2":"0.8.0","ts-jest":"^22.0.0","tslint":"5.8.0","typescript":"2.6.1","ws":"3.3.1"},"peerDependencies":{"koa":">=2","ws":">=3"},"dependencies":{"base64id":"^1.0.0","koa-compose":"^4.0.0"},"release":{"analyzeCommits":{"releaseRules":[{"type":"refactor","release":"patch"}]}}} | ||
{"name":"koa-ws-socket","version":"2.0.0","description":"Attaches ws sockets to koa using koa-style middleware","files":["dist"],"main":"dist/koa-ws-socket.umd.js","module":"dist/koa-ws-socket.es5.js","types":"dist/types/index.d.ts","repository":"https://github.com/pbomb/koa-ws-socket.git","author":"Matt Parrish <matt.r.parrish@gmail.com>","engines":{"node":">=6.0.0"},"license":"MIT","private":false,"scripts":{"add-contributor":"kcd-scripts contributors add","prebuild":"rimraf dist && rimraf compiled","build":"rollup --config rollup.config.ts --no-clean","lint":"tslint -p . -c tslint.json -e **/node_modules/**/* -e **/typings/**/* **/*.ts","test":"kcd-scripts test","test:cover":"kcd-scripts test --coverage","test:update":"npm run test:cover -s -- --updateSnapshot","test:build":"kcd-scripts test --no-watch","build-and-test":"npm run build -s && npm run test:build -s","validate":"kcd-scripts validate lint,build-and-test,test:cover","precommit":"kcd-scripts precommit","prettier":"prettier --write \"**/*.ts\" \"**/*.js{,on}\"","tsc":"tsc"},"devDependencies":{"@types/jest":"22.2.0","@types/koa":"2.0.44","@types/koa-compose":"3.2.2","@types/ws":"4.0.1","kcd-scripts":"0.36.1","koa":"2.5.0","prettier":"1.11.1","rimraf":"2.6.2","rollup":"0.57.0","rollup-plugin-commonjs":"9.1.0","rollup-plugin-node-resolve":"3.2.0","rollup-plugin-sourcemaps":"0.4.2","rollup-plugin-typescript2":"0.12.0","ts-jest":"22.4.1","tslint":"5.9.1","typescript":"2.7.2","ws":"5.0.0"},"peerDependencies":{"koa":">=2","ws":">=5"},"dependencies":{"base64id":"^1.0.0","koa-compose":"^4.0.0"},"release":{"analyzeCommits":{"releaseRules":[{"type":"refactor","release":"patch"}]}}} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
94097
756