Skip to content

Commit

Permalink
refactor: format code (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
levivilet authored Jan 6, 2024
1 parent c65443a commit bacde7d
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 87 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,10 @@
"prettier": "^3.1.1",
"rollup": "^4.9.4",
"typescript": "^5.3.3"
},
"prettier": {
"semi": false,
"singleQuote": true,
"printWidth": 150
}
}
15 changes: 4 additions & 11 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,11 @@ const writeJson = async (path, json) => {
}

const getGitTagFromGit = async () => {
const { stdout, stderr, exitCode } = await execa(
'git',
['describe', '--exact-match', '--tags'],
{
reject: false,
},
)
const { stdout, stderr, exitCode } = await execa('git', ['describe', '--exact-match', '--tags'], {
reject: false,
})
if (exitCode) {
if (
exitCode === 128 &&
stderr.startsWith('fatal: no tag exactly matches')
) {
if (exitCode === 128 && stderr.startsWith('fatal: no tag exactly matches')) {
return '0.0.0-dev'
}
return '0.0.0-dev'
Expand Down
39 changes: 17 additions & 22 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,44 @@
import { Buffer } from "node:buffer";
import * as _ws from "ws";
import { Buffer } from 'node:buffer'
import * as _ws from 'ws'

// workaround for jest or node bug
const WebSocketServer = _ws.WebSocketServer
? _ws.WebSocketServer
: // @ts-ignore
_ws.default.WebSocketServer;
_ws.default.WebSocketServer

const webSocketServer = new WebSocketServer({
noServer: true,

// TODO not sure if ws compress is working at all
// perMessageDeflate: true
});
})

const withResolvers = () => {
/**
* @type {any}
*/
let _resolve;
let _resolve
/**
* @type {any}
*/
let _reject;
let _reject
const promise = new Promise((resolve, reject) => {
_resolve = resolve;
_reject = reject;
});
_resolve = resolve
_reject = reject
})
return {
resolve: _resolve,
reject: _reject,
promise,
};
};
}
}

export const handleUpgrade = (request, socket) => {
const { promise, resolve } = withResolvers();
const { promise, resolve } = withResolvers()
const upgradeCallback = (ws) => {
resolve(ws);
};
webSocketServer.handleUpgrade(
request,
socket,
Buffer.alloc(0),
upgradeCallback
);
return promise;
};
resolve(ws)
}
webSocketServer.handleUpgrade(request, socket, Buffer.alloc(0), upgradeCallback)
return promise
}
6 changes: 3 additions & 3 deletions src/parts/IsSocket/IsSocket.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Socket } from "node:net";
import { Socket } from 'node:net'

export const isSocket = (value) => {
return value instanceof Socket;
};
return value instanceof Socket
}
14 changes: 7 additions & 7 deletions src/parts/Promises/Promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ export const withResolvers = () => {
/**
* @type {any}
*/
let _resolve;
let _resolve
/**
* @type {any}
*/
let _reject;
let _reject
const promise = new Promise((resolve, reject) => {
_resolve = resolve;
_reject = reject;
});
_resolve = resolve
_reject = reject
})
return {
resolve: _resolve,
reject: _reject,
promise,
};
};
}
}
31 changes: 13 additions & 18 deletions src/parts/WebSocketServer/WebSocketServer.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
import { Buffer } from "node:buffer";
import * as _ws from "ws";
import * as IsSocket from "../IsSocket/IsSocket.js";
import * as Promises from "../Promises/Promises.js";
import { Buffer } from 'node:buffer'
import * as _ws from 'ws'
import * as IsSocket from '../IsSocket/IsSocket.js'
import * as Promises from '../Promises/Promises.js'

// workaround for jest or node bug
const WebSocketServer = _ws.WebSocketServer
? _ws.WebSocketServer
: // @ts-ignore
_ws.default.WebSocketServer;
_ws.default.WebSocketServer

const webSocketServer = new WebSocketServer({
noServer: true,

// TODO not sure if ws compress is working at all
// perMessageDeflate: true
});
})

export const handleUpgrade = (request, socket) => {
if (!IsSocket.isSocket(socket)) {
throw new TypeError(`socket must be of type Socket`);
throw new TypeError(`socket must be of type Socket`)
}
const { promise, resolve } = Promises.withResolvers();
const { promise, resolve } = Promises.withResolvers()
const upgradeCallback = (ws) => {
resolve(ws);
};
webSocketServer.handleUpgrade(
request,
socket,
Buffer.alloc(0),
upgradeCallback
);
return promise;
};
resolve(ws)
}
webSocketServer.handleUpgrade(request, socket, Buffer.alloc(0), upgradeCallback)
return promise
}
48 changes: 22 additions & 26 deletions test/WebSocketServer.test.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,40 @@
import { jest } from "@jest/globals";
import { jest } from '@jest/globals'

jest.unstable_mockModule("ws", () => {
jest.unstable_mockModule('ws', () => {
return {
WebSocketServer: class {
handleUpgrade(message, handle, buffer, callback) {
callback({
__isWebSocket: true,
});
})
}
},
};
});
}
})

jest.unstable_mockModule("../src/parts/IsSocket/IsSocket.js", () => ({
jest.unstable_mockModule('../src/parts/IsSocket/IsSocket.js', () => ({
isSocket: jest.fn(),
}));
}))

const WebSocketServer = await import(
"../src/parts/WebSocketServer/WebSocketServer.js"
);
const WebSocketServer = await import('../src/parts/WebSocketServer/WebSocketServer.js')

const IsSocket = await import("../src/parts/IsSocket/IsSocket.js");
const IsSocket = await import('../src/parts/IsSocket/IsSocket.js')

test("handleUpgrade", async () => {
test('handleUpgrade', async () => {
// @ts-ignore
IsSocket.isSocket.mockImplementation(() => {
return true;
});
const webSocket = await WebSocketServer.handleUpgrade();
expect(webSocket).toEqual({ __isWebSocket: true });
});
return true
})
const webSocket = await WebSocketServer.handleUpgrade()
expect(webSocket).toEqual({ __isWebSocket: true })
})

test("handleUpgrade - error - socket is not of type Socket", async () => {
const request = {};
const socket = {};
test('handleUpgrade - error - socket is not of type Socket', async () => {
const request = {}
const socket = {}
// @ts-ignore
IsSocket.isSocket.mockImplementation(() => {
return false;
});
expect(() => WebSocketServer.handleUpgrade(request, socket)).toThrow(
new TypeError("socket must be of type Socket")
);
});
return false
})
expect(() => WebSocketServer.handleUpgrade(request, socket)).toThrow(new TypeError('socket must be of type Socket'))
})

0 comments on commit bacde7d

Please sign in to comment.