Skip to content

Commit

Permalink
fix: npm run format error
Browse files Browse the repository at this point in the history
  • Loading branch information
zzswang committed Aug 12, 2018
1 parent c8deb1f commit eb182d6
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 85 deletions.
12 changes: 6 additions & 6 deletions examples/simple.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Whisper from '../src';
import Whisper from "../src";

const app = new Whisper();

Expand All @@ -20,7 +20,7 @@ const logMiddleware = async (ctx, next) => {

const dataMiddleware = async (ctx, next) => {
// step 2: handle request
ctx.req = ctx.data.toString('utf8', 0, 2);
ctx.req = ctx.data.toString("utf8", 0, 2);

await next();

Expand All @@ -32,19 +32,19 @@ const dataMiddleware = async (ctx, next) => {
app.use(logMiddleware);
app.use(dataMiddleware);

app.on('close', session => {
app.on("close", session => {
console.log(`session ${session.id}: closed`);
});

app.on('timeout', session => {
app.on("timeout", session => {
console.log(`session ${session.id}: timeout`);
});

app.on('end', session => {
app.on("end", session => {
console.log(`session ${session.id}: end`);
});

app.on('error', session => {
app.on("error", session => {
console.log(`session ${session.id}: error`);
});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"scripts": {
"start": "nodemon examples/simple.js --watch src --exec 'babel-node'",
"build": "rm -rf dist && babel ./src -d ./dist --ignore test.js",
"format": "prettier --trailing-comma es5 --single-quote --write",
"format": "prettier --trailing-comma es5 --write",
"lint": "eslint src",
"prepublishOnly": "npm run build",
"release": "standard-version && git push --follow-tags origin master",
Expand Down
12 changes: 6 additions & 6 deletions src/context.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import delegate from 'delegates';
import delegate from "delegates";

/**
* Context class.
Expand All @@ -10,8 +10,8 @@ export default class Context {
session;
}

delegate(Context.prototype, 'session')
.access('app')
.access('socket')
.method('send')
.method('onerror');
delegate(Context.prototype, "session")
.access("app")
.access("socket")
.method("send")
.method("onerror");
48 changes: 24 additions & 24 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import Emitter from 'events';
import net from 'net';
import util from 'util';
import Emitter from "events";
import net from "net";
import util from "util";

import compose from 'koa-compose';
import Debugger from 'debug';
import only from 'only';
import compose from "koa-compose";
import Debugger from "debug";
import only from "only";

import Session from './session';
import Context from './context';
import Session from "./session";
import Context from "./context";

const debug = new Debugger('whisper');
const debug = new Debugger("whisper");

/**
* Expose `Application` class.
Expand All @@ -28,7 +28,7 @@ export default class Application extends Emitter {

this.middleware = [];
this.sessions = [];
this.env = process.env.NODE_ENV || 'development';
this.env = process.env.NODE_ENV || "development";

if (util.inspect.custom) {
this[util.inspect.custom] = this.inspect;
Expand All @@ -46,7 +46,7 @@ export default class Application extends Emitter {
*/

listen(...args) {
debug('listen');
debug("listen");
const server = net.createServer(this.callback());
return server.listen(...args);
}
Expand All @@ -60,7 +60,7 @@ export default class Application extends Emitter {
*/

toJSON() {
return only(this, ['env']);
return only(this, ["env"]);
}

/**
Expand All @@ -85,8 +85,8 @@ export default class Application extends Emitter {
*/

use(fn) {
if (typeof fn !== 'function') throw new TypeError('middleware must be a function!');
debug('use %s', fn._name || fn.name || '-');
if (typeof fn !== "function") throw new TypeError("middleware must be a function!");
debug("use %s", fn._name || fn.name || "-");
this.middleware.push(fn);
return this;
}
Expand All @@ -102,7 +102,7 @@ export default class Application extends Emitter {
callback() {
const fn = compose(this.middleware);

if (!this.listenerCount('error')) this.on('error', this.onerror);
if (!this.listenerCount("error")) this.on("error", this.onerror);

const handleConnect = socket => {
// create session while new connection
Expand All @@ -118,11 +118,11 @@ export default class Application extends Emitter {
this.handleRequest(ctx, fn);
};

socket.on('close', handleClose);
socket.on('data', handleData);
socket.on('end', handleEnd);
socket.on('error', handleError);
socket.on('timeout', handleTimeout);
socket.on("close", handleClose);
socket.on("data", handleData);
socket.on("end", handleEnd);
socket.on("error", handleError);
socket.on("timeout", handleTimeout);
};

return handleConnect;
Expand All @@ -136,7 +136,7 @@ export default class Application extends Emitter {
* @param {*} data string/buffer/json
*/
broadcast(data, filt = () => true) {
debug('broadcast');
debug("broadcast");
this.sessions.filter(filt).forEach(session => session.send(data));
}

Expand All @@ -149,7 +149,7 @@ export default class Application extends Emitter {
*/

handleRequest(ctx, fnMiddleware) {
debug('data comming with seq no %s', ctx.no);
debug("data comming with seq no %s", ctx.no);
debug(ctx.data);

const onerror = err => ctx.onerror(err);
Expand All @@ -164,13 +164,13 @@ export default class Application extends Emitter {
*/

onerror(err) {
if (!(err instanceof Error)) throw new TypeError(util.format('non-error thrown: %j', err));
if (!(err instanceof Error)) throw new TypeError(util.format("non-error thrown: %j", err));

if (this.silent) return;

const msg = err.stack || err.toString();
console.error();
console.error(msg.replace(/^/gm, ' '));
console.error(msg.replace(/^/gm, " "));
console.error();
}

Expand Down
48 changes: 24 additions & 24 deletions src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
import net from 'net';
import net from "net";

import Whisper from './index';
import Whisper from "./index";

describe('app', () => {
test('should listen on timeout', done => {
describe("app", () => {
test("should listen on timeout", done => {
const app = new Whisper();
const connect = app.callback();
const s1 = new net.Socket();
connect(s1);
app.on('timeout', session => {
app.on("timeout", session => {
expect(session).toBe(app.sessions[0]);
done();
});

s1.emit('timeout');
s1.emit("timeout");
});

test('should listen on error after timeout', done => {
test("should listen on error after timeout", done => {
const app = new Whisper();
const connect = app.callback();
const s1 = new net.Socket();
connect(s1);

app.on('error', err => {
expect(err.message).toBe('session timeout');
app.on("error", err => {
expect(err.message).toBe("session timeout");
done();
});

s1.emit('timeout');
s1.emit("timeout");
});

test('should listen on close after error', done => {
test("should listen on close after error", done => {
const app = new Whisper();
const connect = app.callback();
const s1 = new net.Socket();
connect(s1);
s1.connect('baidu.com:80');
s1.connect("baidu.com:80");

const s = app.sessions[0];
app.on('close', session => {
app.on("close", session => {
expect(session).toBe(s);
expect(app.sessions).toHaveLength(0);
s1.destroy();
done();
});

s1.emit('error');
s1.emit("error");
});

test('should manage session list', () => {
test("should manage session list", () => {
const app = new Whisper();
const connect = app.callback();
const s1 = new net.Socket();
Expand All @@ -57,11 +57,11 @@ describe('app', () => {
connect(s2);

expect(app.sessions).toHaveLength(2);
s1.emit('close');
s1.emit("close");
expect(app.sessions).toHaveLength(1);
});

test('should have write session list', () => {
test("should have write session list", () => {
const app = new Whisper();
const connect = app.callback();
const s1 = new net.Socket();
Expand All @@ -72,15 +72,15 @@ describe('app', () => {
expect(app.sessions).toHaveLength(2);
});

test('should set development env when NODE_ENV missing', () => {
test("should set development env when NODE_ENV missing", () => {
const NODE_ENV = process.env.NODE_ENV;
process.env.NODE_ENV = '';
process.env.NODE_ENV = "";
const app = new Whisper();
process.env.NODE_ENV = NODE_ENV;
expect(app.env).toEqual('development');
expect(app.env).toEqual("development");
});

test('should broad cast to all clients', () => {
test("should broad cast to all clients", () => {
const app = new Whisper();
const connect = app.callback();
const s1 = new net.Socket();
Expand All @@ -92,8 +92,8 @@ describe('app', () => {
connect(s1);
connect(s2);

app.broadcast('some data');
expect(s1.write).toBeCalledWith('some data');
expect(s2.write).toBeCalledWith('some data');
app.broadcast("some data");
expect(s1.write).toBeCalledWith("some data");
expect(s2.write).toBeCalledWith("some data");
});
});
Loading

0 comments on commit eb182d6

Please sign in to comment.