Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): Use uWebSockets #89

Merged
merged 32 commits into from
Apr 11, 2021
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
30916dc
init
maxpain Dec 21, 2020
8ce6e5e
fix spaces
maxpain Dec 21, 2020
9d142c8
change port to 3000
maxpain Dec 21, 2020
f1325d0
lint fixes
maxpain Dec 21, 2020
0bb728f
use one Map to keep all timeouts and handlers, implemented dispose fu…
maxpain Dec 21, 2020
8deb87d
link to question about compression
maxpain Dec 21, 2020
33edd0c
lint fixes
maxpain Dec 21, 2020
6e702d6
docs
maxpain Dec 21, 2020
692fad2
use Map as generic class
maxpain Dec 21, 2020
6c410cb
tests
maxpain Dec 21, 2020
01e6b4b
lin fixes
maxpain Dec 21, 2020
4272e00
Merge branch 'master' into uwebsockets
enisdenjo Apr 10, 2021
a2b0a76
refactor: unify server fixtures
enisdenjo Apr 10, 2021
ba45227
test(use): loop over tservers
enisdenjo Apr 10, 2021
30e03b7
style: smaller changes
enisdenjo Apr 10, 2021
2742ed5
refactor: tservers adjustments
enisdenjo Apr 10, 2021
db9de5e
refactor: tservers are utils
enisdenjo Apr 10, 2021
c7ee475
refactor: uses resemble the package names
enisdenjo Apr 10, 2021
ea00a9d
style: prefer lib names for default imports
enisdenjo Apr 10, 2021
413f3fc
feat: make behaviour and various adjustments
enisdenjo Apr 10, 2021
0eedc81
docs: generate
enisdenjo Apr 10, 2021
5b353eb
Merge branch 'master' into uwebsockets
enisdenjo Apr 11, 2021
7e8f851
refactor: get tserverclients and add onmessage
enisdenjo Apr 11, 2021
36b7ffd
fix: respect beNice flag during dispose
enisdenjo Apr 11, 2021
94bc2c5
feat: update uwebebsockets to v19
enisdenjo Apr 11, 2021
a11546a
fix: dont fix args size for version compatibility
enisdenjo Apr 11, 2021
054d8a6
Merge branch 'master' into uwebsockets
enisdenjo Apr 11, 2021
6958111
Merge branch 'master' into uwebsockets
enisdenjo Apr 11, 2021
62b0fdd
fix: use getclients
enisdenjo Apr 11, 2021
166c7c8
docs: makebehaviour
enisdenjo Apr 11, 2021
c8e4f43
docs: use default compresion, backpressure and payload length options
enisdenjo Apr 11, 2021
354feb3
feat: super simple backpressure handling
enisdenjo Apr 11, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1311,6 +1311,26 @@ async function ping() {

</details>

<details id="uws">
<summary><a href="#uws">🔗</a> Server usage with <a href="https://github.com/uNetworking/uWebSockets.js">uWebSockets.js</a></summary>

```ts
import uWS from 'uWebSockets.js'; // yarn add uWebSockets.js@uNetworking/uWebSockets.js#<tag>
import { makeBehavior } from 'graphql-ws/lib/use/uWebSockets';
import { schema } from './my-graphql-schema';

uWS
.App()
.ws('/graphql/is-performant', makeBehavior({ schema }))
.listen(80, (listenSocket) => {
if (listenSocket) {
console.log('Listening to port 80');
}
});
```

</details>

## [Documentation](docs/)

Check the [docs folder](docs/) out for [TypeDoc](https://typedoc.org) generated documentation.
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -11,4 +11,5 @@ graphql-ws
- [protocol](modules/protocol.md)
- [server](modules/server.md)
- [types](modules/types.md)
- [use/uWebSockets](modules/use_uwebsockets.md)
- [use/ws](modules/use_ws.md)
31 changes: 31 additions & 0 deletions docs/interfaces/use_uwebsockets.extra.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[graphql-ws](../README.md) / [use/uWebSockets](../modules/use_uwebsockets.md) / Extra

# Interface: Extra

[use/uWebSockets](../modules/use_uwebsockets.md).Extra

The extra that will be put in the `Context`.

## Table of contents

### Properties

- [request](use_uwebsockets.extra.md#request)
- [socket](use_uwebsockets.extra.md#socket)

## Properties

### request

• `Readonly` **request**: HttpRequest

The initial HTTP request before the actual
socket and connection is established.

___

### socket

• `Readonly` **socket**: WebSocket

The actual socket connection between the server and the client.
32 changes: 32 additions & 0 deletions docs/modules/use_uwebsockets.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[graphql-ws](../README.md) / use/uWebSockets

# Module: use/uWebSockets

## Table of contents

### Interfaces

- [Extra](../interfaces/use_uwebsockets.extra.md)

### Functions

- [makeBehavior](use_uwebsockets.md#makebehavior)

## Functions

### makeBehavior

▸ **makeBehavior**(`options`: [*ServerOptions*](../interfaces/server.serveroptions.md)<[*Extra*](../interfaces/use_uwebsockets.extra.md)\>, `behavior?`: uWS.WebSocketBehavior, `keepAlive?`: *number*): uWS.WebSocketBehavior

Make the behaviour for using a [uWebSockets.js](https://github.com/uNetworking/uWebSockets.js) WebSocket server.
This is a basic starter, feel free to copy the code over and adjust it to your needs

#### Parameters:

Name | Type |
:------ | :------ |
`options` | [*ServerOptions*](../interfaces/server.serveroptions.md)<[*Extra*](../interfaces/use_uwebsockets.extra.md)\> |
`behavior` | uWS.WebSocketBehavior |
`keepAlive` | *number* |

**Returns:** uWS.WebSocketBehavior
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -98,6 +98,7 @@
"typedoc": "^0.20.34",
"typedoc-plugin-markdown": "^3.6.0",
"typescript": "^4.2.3",
"uWebSockets.js": "uNetworking/uWebSockets.js#v19.0.0",
"ws": "^7.4.4"
}
}
26 changes: 13 additions & 13 deletions src/tests/client.ts
Original file line number Diff line number Diff line change
@@ -4,9 +4,9 @@

import WebSocket from 'ws';
import { EventEmitter } from 'events';
import { startTServer } from './fixtures/simple';
import { createClient, Client, EventListener } from '../client';
import { SubscribePayload } from '../message';
import { startWSTServer as startTServer } from './utils';

// simulate browser environment for easier client testing
beforeEach(() => {
@@ -291,7 +291,7 @@ it('should close the socket if the `connectionParams` rejects or throws', async
it('should not send the complete message if the socket is not open', async () => {
const {
url,
clients,
getClients,
waitForOperation,
waitForClientClose,
} = await startTServer();
@@ -319,7 +319,7 @@ it('should not send the complete message if the socket is not open', async () =>
await waitForOperation();

// kick the client off
for (const client of clients) {
for (const client of getClients()) {
client.close();
await waitForClientClose();
}
@@ -514,7 +514,7 @@ describe('subscription operation', () => {

await server.waitForClientClose();

expect(server.clients.size).toBe(0);
expect(server.getClients().length).toBe(0);
});

it('should dispose of the subscription on error', async () => {
@@ -534,13 +534,13 @@ describe('subscription operation', () => {

await server.waitForClientClose();

expect(server.clients.size).toBe(0);
expect(server.getClients().length).toBe(0);
});

it('should stop dispatching messages after completing a subscription', async () => {
const {
url,
clients,
getClients,
waitForOperation,
waitForComplete,
} = await startTServer();
@@ -553,8 +553,8 @@ describe('subscription operation', () => {
);
await waitForOperation();

for (const client of clients) {
client.once('message', () => {
for (const client of getClients()) {
client.onMessage(() => {
// no more messages from the client
fail("Shouldn't have dispatched a message");
});
@@ -597,7 +597,7 @@ describe('"concurrency"', () => {
}, 10);
await sub1.waitForComplete();

expect(server.clients.size).toBe(1);
expect(server.getClients().length).toBe(1);
});
});

@@ -1018,7 +1018,7 @@ describe('reconnecting', () => {
await server.waitForClientClose();

// and no clients should be left
expect(server.clients.size).toBe(0);
expect(server.getClients().length).toBe(0);
});

it('should lazy disconnect even if subscription is created during retries after all get completed', async () => {
@@ -1062,7 +1062,7 @@ describe('reconnecting', () => {
}, 10);

// and client should still be connected
expect(server.clients.size).toBe(1);
expect(server.getClients().length).toBe(1);

// dispose of the last subscription
sub2.dispose();
@@ -1072,7 +1072,7 @@ describe('reconnecting', () => {
await server.waitForClientClose();

// and all connections are gone
expect(server.clients.size).toBe(0);
expect(server.getClients().length).toBe(0);
});
});

@@ -1127,7 +1127,7 @@ describe('events', () => {

expect(closedFn).not.toBeCalled();

server.clients.forEach((client) => {
server.getClients().forEach((client) => {
client.close();
});

Loading