-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
71 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) | ||
}) |