@as-integrations/h3 1.2.1 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -4
- package/dist/index.cjs +0 -53
- package/dist/index.d.cts +5 -30
- package/dist/index.d.mts +5 -30
- package/dist/index.d.ts +5 -30
- package/dist/index.mjs +2 -53
- package/dist/websocket.cjs +62 -0
- package/dist/websocket.d.cts +31 -0
- package/dist/websocket.d.mts +31 -0
- package/dist/websocket.d.ts +31 -0
- package/dist/websocket.mjs +59 -0
- package/package.json +37 -30
package/README.md
CHANGED
|
@@ -90,10 +90,8 @@ Then you can add a WebSocket handler to your `h3` app using the `defineGraphqlWe
|
|
|
90
90
|
```js
|
|
91
91
|
import { createApp } from 'h3'
|
|
92
92
|
import { ApolloServer } from '@apollo/server'
|
|
93
|
-
import {
|
|
94
|
-
|
|
95
|
-
defineGraphqlWebSocketHandler,
|
|
96
|
-
} from '@as-integrations/h3'
|
|
93
|
+
import { startServerAndCreateH3Handler } from '@as-integrations/h3'
|
|
94
|
+
import { defineGraphqlWebSocketHandler } from '@as-integrations/h3/websocket'
|
|
97
95
|
import { makeExecutableSchema } from '@graphql-tools/schema'
|
|
98
96
|
|
|
99
97
|
// Define your schema and resolvers
|
package/dist/index.cjs
CHANGED
|
@@ -2,57 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
const server = require('@apollo/server');
|
|
4
4
|
const h3 = require('h3');
|
|
5
|
-
const graphqlWs = require('graphql-ws');
|
|
6
|
-
|
|
7
|
-
function defineGraphqlWebSocket(options) {
|
|
8
|
-
const server = graphqlWs.makeServer(options);
|
|
9
|
-
const peers = /* @__PURE__ */ new WeakMap();
|
|
10
|
-
return h3.defineWebSocket({
|
|
11
|
-
open(peer) {
|
|
12
|
-
const client = {
|
|
13
|
-
handleMessage: () => {
|
|
14
|
-
throw new Error("Message received before handler was registered");
|
|
15
|
-
},
|
|
16
|
-
closed: () => {
|
|
17
|
-
throw new Error("Closed before handler was registered");
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
|
-
client.closed = server.opened(
|
|
21
|
-
{
|
|
22
|
-
// TODO: use protocol on socket once h3 exposes it
|
|
23
|
-
// https://github.com/unjs/crossws/issues/31
|
|
24
|
-
protocol: graphqlWs.GRAPHQL_TRANSPORT_WS_PROTOCOL,
|
|
25
|
-
send: (message) => {
|
|
26
|
-
if (peers.has(peer)) {
|
|
27
|
-
peer.send(message);
|
|
28
|
-
}
|
|
29
|
-
},
|
|
30
|
-
close: (_code, _reason) => {
|
|
31
|
-
if (peers.has(peer)) ;
|
|
32
|
-
},
|
|
33
|
-
onMessage: (cb) => client.handleMessage = cb
|
|
34
|
-
},
|
|
35
|
-
{ peer }
|
|
36
|
-
);
|
|
37
|
-
peers.set(peer, client);
|
|
38
|
-
},
|
|
39
|
-
message(peer, message) {
|
|
40
|
-
const client = peers.get(peer);
|
|
41
|
-
if (!client)
|
|
42
|
-
throw new Error("Message received for a missing client");
|
|
43
|
-
return client.handleMessage(message.text());
|
|
44
|
-
},
|
|
45
|
-
close(peer, details) {
|
|
46
|
-
const client = peers.get(peer);
|
|
47
|
-
if (!client)
|
|
48
|
-
throw new Error("Closing a missing client");
|
|
49
|
-
return client.closed(details.code ?? 1e3, details.reason ?? "");
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
function defineGraphqlWebSocketHandler(options) {
|
|
54
|
-
return h3.defineWebSocketHandler(defineGraphqlWebSocket(options));
|
|
55
|
-
}
|
|
56
5
|
|
|
57
6
|
function startServerAndCreateH3Handler(server, options) {
|
|
58
7
|
server.startInBackgroundHandlingStartupErrorsByLoggingAndFailingAllRequests();
|
|
@@ -119,6 +68,4 @@ async function normalizeBody(event) {
|
|
|
119
68
|
}
|
|
120
69
|
}
|
|
121
70
|
|
|
122
|
-
exports.defineGraphqlWebSocket = defineGraphqlWebSocket;
|
|
123
|
-
exports.defineGraphqlWebSocketHandler = defineGraphqlWebSocketHandler;
|
|
124
71
|
exports.startServerAndCreateH3Handler = startServerAndCreateH3Handler;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,34 +1,8 @@
|
|
|
1
1
|
import { BaseContext, ContextFunction, ApolloServer } from '@apollo/server';
|
|
2
|
-
import { Hooks
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import { Hooks } from 'crossws';
|
|
3
|
+
import { H3Event, EventHandler } from 'h3';
|
|
4
|
+
import { WithRequired } from '@apollo/utils.withrequired';
|
|
5
5
|
|
|
6
|
-
/**
|
|
7
|
-
* The extra that will be put in the `Context`.
|
|
8
|
-
*
|
|
9
|
-
* @category Server/h3
|
|
10
|
-
*/
|
|
11
|
-
interface Extra {
|
|
12
|
-
/**
|
|
13
|
-
* The underlying WebSocket peer.
|
|
14
|
-
*/
|
|
15
|
-
readonly peer: Peer;
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Create the WebSocket hooks to be used with [h3](https://h3.unjs.io/).
|
|
19
|
-
*
|
|
20
|
-
* Use this over {@link defineGraphqlWebSocketHandler} if you need more control over the WebSocket server or
|
|
21
|
-
* if you want to add custom hooks (e.g. for authentication or logging).
|
|
22
|
-
*/
|
|
23
|
-
declare function defineGraphqlWebSocket<P extends ConnectionInitMessage['payload'] = ConnectionInitMessage['payload'], E extends Record<PropertyKey, unknown> = Record<PropertyKey, never>>(options: ServerOptions<P, Extra & Partial<E>>): Partial<Hooks>;
|
|
24
|
-
/**
|
|
25
|
-
* Create a event handler to be used with [h3](https://h3.unjs.io/).
|
|
26
|
-
*
|
|
27
|
-
* @category Server/h3
|
|
28
|
-
*/
|
|
29
|
-
declare function defineGraphqlWebSocketHandler<P extends ConnectionInitMessage['payload'] = ConnectionInitMessage['payload'], E extends Record<PropertyKey, unknown> = Record<PropertyKey, never>>(options: ServerOptions<P, Extra & Partial<E>>): EventHandler<EventHandlerRequest, never>;
|
|
30
|
-
|
|
31
|
-
type WithRequired<T, K extends keyof T> = T & Required<Pick<T, K>>;
|
|
32
6
|
interface H3ContextFunctionArgument {
|
|
33
7
|
event: H3Event;
|
|
34
8
|
}
|
|
@@ -39,4 +13,5 @@ interface H3HandlerOptions<TContext extends BaseContext> {
|
|
|
39
13
|
declare function startServerAndCreateH3Handler(server: ApolloServer<BaseContext>, options?: H3HandlerOptions<BaseContext>): EventHandler;
|
|
40
14
|
declare function startServerAndCreateH3Handler<TContext extends BaseContext>(server: ApolloServer<TContext>, options: WithRequired<H3HandlerOptions<TContext>, 'context'>): EventHandler;
|
|
41
15
|
|
|
42
|
-
export {
|
|
16
|
+
export { startServerAndCreateH3Handler };
|
|
17
|
+
export type { H3ContextFunctionArgument, H3HandlerOptions };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,34 +1,8 @@
|
|
|
1
1
|
import { BaseContext, ContextFunction, ApolloServer } from '@apollo/server';
|
|
2
|
-
import { Hooks
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import { Hooks } from 'crossws';
|
|
3
|
+
import { H3Event, EventHandler } from 'h3';
|
|
4
|
+
import { WithRequired } from '@apollo/utils.withrequired';
|
|
5
5
|
|
|
6
|
-
/**
|
|
7
|
-
* The extra that will be put in the `Context`.
|
|
8
|
-
*
|
|
9
|
-
* @category Server/h3
|
|
10
|
-
*/
|
|
11
|
-
interface Extra {
|
|
12
|
-
/**
|
|
13
|
-
* The underlying WebSocket peer.
|
|
14
|
-
*/
|
|
15
|
-
readonly peer: Peer;
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Create the WebSocket hooks to be used with [h3](https://h3.unjs.io/).
|
|
19
|
-
*
|
|
20
|
-
* Use this over {@link defineGraphqlWebSocketHandler} if you need more control over the WebSocket server or
|
|
21
|
-
* if you want to add custom hooks (e.g. for authentication or logging).
|
|
22
|
-
*/
|
|
23
|
-
declare function defineGraphqlWebSocket<P extends ConnectionInitMessage['payload'] = ConnectionInitMessage['payload'], E extends Record<PropertyKey, unknown> = Record<PropertyKey, never>>(options: ServerOptions<P, Extra & Partial<E>>): Partial<Hooks>;
|
|
24
|
-
/**
|
|
25
|
-
* Create a event handler to be used with [h3](https://h3.unjs.io/).
|
|
26
|
-
*
|
|
27
|
-
* @category Server/h3
|
|
28
|
-
*/
|
|
29
|
-
declare function defineGraphqlWebSocketHandler<P extends ConnectionInitMessage['payload'] = ConnectionInitMessage['payload'], E extends Record<PropertyKey, unknown> = Record<PropertyKey, never>>(options: ServerOptions<P, Extra & Partial<E>>): EventHandler<EventHandlerRequest, never>;
|
|
30
|
-
|
|
31
|
-
type WithRequired<T, K extends keyof T> = T & Required<Pick<T, K>>;
|
|
32
6
|
interface H3ContextFunctionArgument {
|
|
33
7
|
event: H3Event;
|
|
34
8
|
}
|
|
@@ -39,4 +13,5 @@ interface H3HandlerOptions<TContext extends BaseContext> {
|
|
|
39
13
|
declare function startServerAndCreateH3Handler(server: ApolloServer<BaseContext>, options?: H3HandlerOptions<BaseContext>): EventHandler;
|
|
40
14
|
declare function startServerAndCreateH3Handler<TContext extends BaseContext>(server: ApolloServer<TContext>, options: WithRequired<H3HandlerOptions<TContext>, 'context'>): EventHandler;
|
|
41
15
|
|
|
42
|
-
export {
|
|
16
|
+
export { startServerAndCreateH3Handler };
|
|
17
|
+
export type { H3ContextFunctionArgument, H3HandlerOptions };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,34 +1,8 @@
|
|
|
1
1
|
import { BaseContext, ContextFunction, ApolloServer } from '@apollo/server';
|
|
2
|
-
import { Hooks
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import { Hooks } from 'crossws';
|
|
3
|
+
import { H3Event, EventHandler } from 'h3';
|
|
4
|
+
import { WithRequired } from '@apollo/utils.withrequired';
|
|
5
5
|
|
|
6
|
-
/**
|
|
7
|
-
* The extra that will be put in the `Context`.
|
|
8
|
-
*
|
|
9
|
-
* @category Server/h3
|
|
10
|
-
*/
|
|
11
|
-
interface Extra {
|
|
12
|
-
/**
|
|
13
|
-
* The underlying WebSocket peer.
|
|
14
|
-
*/
|
|
15
|
-
readonly peer: Peer;
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Create the WebSocket hooks to be used with [h3](https://h3.unjs.io/).
|
|
19
|
-
*
|
|
20
|
-
* Use this over {@link defineGraphqlWebSocketHandler} if you need more control over the WebSocket server or
|
|
21
|
-
* if you want to add custom hooks (e.g. for authentication or logging).
|
|
22
|
-
*/
|
|
23
|
-
declare function defineGraphqlWebSocket<P extends ConnectionInitMessage['payload'] = ConnectionInitMessage['payload'], E extends Record<PropertyKey, unknown> = Record<PropertyKey, never>>(options: ServerOptions<P, Extra & Partial<E>>): Partial<Hooks>;
|
|
24
|
-
/**
|
|
25
|
-
* Create a event handler to be used with [h3](https://h3.unjs.io/).
|
|
26
|
-
*
|
|
27
|
-
* @category Server/h3
|
|
28
|
-
*/
|
|
29
|
-
declare function defineGraphqlWebSocketHandler<P extends ConnectionInitMessage['payload'] = ConnectionInitMessage['payload'], E extends Record<PropertyKey, unknown> = Record<PropertyKey, never>>(options: ServerOptions<P, Extra & Partial<E>>): EventHandler<EventHandlerRequest, never>;
|
|
30
|
-
|
|
31
|
-
type WithRequired<T, K extends keyof T> = T & Required<Pick<T, K>>;
|
|
32
6
|
interface H3ContextFunctionArgument {
|
|
33
7
|
event: H3Event;
|
|
34
8
|
}
|
|
@@ -39,4 +13,5 @@ interface H3HandlerOptions<TContext extends BaseContext> {
|
|
|
39
13
|
declare function startServerAndCreateH3Handler(server: ApolloServer<BaseContext>, options?: H3HandlerOptions<BaseContext>): EventHandler;
|
|
40
14
|
declare function startServerAndCreateH3Handler<TContext extends BaseContext>(server: ApolloServer<TContext>, options: WithRequired<H3HandlerOptions<TContext>, 'context'>): EventHandler;
|
|
41
15
|
|
|
42
|
-
export {
|
|
16
|
+
export { startServerAndCreateH3Handler };
|
|
17
|
+
export type { H3ContextFunctionArgument, H3HandlerOptions };
|
package/dist/index.mjs
CHANGED
|
@@ -1,56 +1,5 @@
|
|
|
1
1
|
import { HeaderMap } from '@apollo/server';
|
|
2
|
-
import {
|
|
3
|
-
import { makeServer, GRAPHQL_TRANSPORT_WS_PROTOCOL } from 'graphql-ws';
|
|
4
|
-
|
|
5
|
-
function defineGraphqlWebSocket(options) {
|
|
6
|
-
const server = makeServer(options);
|
|
7
|
-
const peers = /* @__PURE__ */ new WeakMap();
|
|
8
|
-
return defineWebSocket({
|
|
9
|
-
open(peer) {
|
|
10
|
-
const client = {
|
|
11
|
-
handleMessage: () => {
|
|
12
|
-
throw new Error("Message received before handler was registered");
|
|
13
|
-
},
|
|
14
|
-
closed: () => {
|
|
15
|
-
throw new Error("Closed before handler was registered");
|
|
16
|
-
}
|
|
17
|
-
};
|
|
18
|
-
client.closed = server.opened(
|
|
19
|
-
{
|
|
20
|
-
// TODO: use protocol on socket once h3 exposes it
|
|
21
|
-
// https://github.com/unjs/crossws/issues/31
|
|
22
|
-
protocol: GRAPHQL_TRANSPORT_WS_PROTOCOL,
|
|
23
|
-
send: (message) => {
|
|
24
|
-
if (peers.has(peer)) {
|
|
25
|
-
peer.send(message);
|
|
26
|
-
}
|
|
27
|
-
},
|
|
28
|
-
close: (_code, _reason) => {
|
|
29
|
-
if (peers.has(peer)) ;
|
|
30
|
-
},
|
|
31
|
-
onMessage: (cb) => client.handleMessage = cb
|
|
32
|
-
},
|
|
33
|
-
{ peer }
|
|
34
|
-
);
|
|
35
|
-
peers.set(peer, client);
|
|
36
|
-
},
|
|
37
|
-
message(peer, message) {
|
|
38
|
-
const client = peers.get(peer);
|
|
39
|
-
if (!client)
|
|
40
|
-
throw new Error("Message received for a missing client");
|
|
41
|
-
return client.handleMessage(message.text());
|
|
42
|
-
},
|
|
43
|
-
close(peer, details) {
|
|
44
|
-
const client = peers.get(peer);
|
|
45
|
-
if (!client)
|
|
46
|
-
throw new Error("Closing a missing client");
|
|
47
|
-
return client.closed(details.code ?? 1e3, details.reason ?? "");
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
function defineGraphqlWebSocketHandler(options) {
|
|
52
|
-
return defineWebSocketHandler(defineGraphqlWebSocket(options));
|
|
53
|
-
}
|
|
2
|
+
import { eventHandler, isMethod, setHeaders, getHeaders, readBody } from 'h3';
|
|
54
3
|
|
|
55
4
|
function startServerAndCreateH3Handler(server, options) {
|
|
56
5
|
server.startInBackgroundHandlingStartupErrorsByLoggingAndFailingAllRequests();
|
|
@@ -117,4 +66,4 @@ async function normalizeBody(event) {
|
|
|
117
66
|
}
|
|
118
67
|
}
|
|
119
68
|
|
|
120
|
-
export {
|
|
69
|
+
export { startServerAndCreateH3Handler };
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const graphqlWs = require('graphql-ws');
|
|
4
|
+
const h3 = require('h3');
|
|
5
|
+
|
|
6
|
+
function defineGraphqlWebSocket(options) {
|
|
7
|
+
const server = graphqlWs.makeServer(options);
|
|
8
|
+
const peers = /* @__PURE__ */ new WeakMap();
|
|
9
|
+
return h3.defineWebSocket({
|
|
10
|
+
open(peer) {
|
|
11
|
+
const client = {
|
|
12
|
+
handleMessage: () => {
|
|
13
|
+
throw new Error("Message received before handler was registered");
|
|
14
|
+
},
|
|
15
|
+
closed: () => {
|
|
16
|
+
throw new Error("Closed before handler was registered");
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
client.closed = server.opened(
|
|
20
|
+
{
|
|
21
|
+
protocol: peer.request.headers?.get("Sec-WebSocket-Protocol") ?? "",
|
|
22
|
+
send: (message) => {
|
|
23
|
+
if (peers.has(peer)) {
|
|
24
|
+
peer.send(message);
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
close: (code, reason) => {
|
|
28
|
+
if (peers.has(peer)) {
|
|
29
|
+
peer.close(code, reason);
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
onMessage: (cb) => client.handleMessage = cb
|
|
33
|
+
},
|
|
34
|
+
{ peer }
|
|
35
|
+
);
|
|
36
|
+
peers.set(peer, client);
|
|
37
|
+
},
|
|
38
|
+
message(peer, message) {
|
|
39
|
+
const client = peers.get(peer);
|
|
40
|
+
if (!client) throw new Error("Message received for a missing client");
|
|
41
|
+
return client.handleMessage(message.text());
|
|
42
|
+
},
|
|
43
|
+
close(peer, details) {
|
|
44
|
+
const client = peers.get(peer);
|
|
45
|
+
if (!client) throw new Error("Closing a missing client");
|
|
46
|
+
const upgradeProtocol = peer.request.headers?.get(
|
|
47
|
+
"Sec-WebSocket-Protocol"
|
|
48
|
+
);
|
|
49
|
+
if (details.code === graphqlWs.CloseCode.SubprotocolNotAcceptable && upgradeProtocol === graphqlWs.DEPRECATED_GRAPHQL_WS_PROTOCOL)
|
|
50
|
+
console.warn(
|
|
51
|
+
`Client provided the unsupported and deprecated subprotocol "${upgradeProtocol}" used by subscriptions-transport-ws.Please see https://www.apollographql.com/docs/apollo-server/data/subscriptions/#switching-from-subscriptions-transport-ws.`
|
|
52
|
+
);
|
|
53
|
+
return client.closed(details.code, details.reason);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
async function defineGraphqlWebSocketHandler(options) {
|
|
58
|
+
return h3.defineWebSocketHandler(defineGraphqlWebSocket(options));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
exports.defineGraphqlWebSocket = defineGraphqlWebSocket;
|
|
62
|
+
exports.defineGraphqlWebSocketHandler = defineGraphqlWebSocketHandler;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { ConnectionInitMessage, ServerOptions } from 'graphql-ws';
|
|
2
|
+
import { EventHandler, EventHandlerRequest } from 'h3';
|
|
3
|
+
import { Peer, Hooks } from 'crossws';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The extra that will be put in the `Context`.
|
|
7
|
+
*
|
|
8
|
+
* @category Server/h3
|
|
9
|
+
*/
|
|
10
|
+
interface Extra {
|
|
11
|
+
/**
|
|
12
|
+
* The underlying WebSocket peer.
|
|
13
|
+
*/
|
|
14
|
+
readonly peer: Peer;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Create the WebSocket hooks to be used with [h3](https://h3.unjs.io/).
|
|
18
|
+
*
|
|
19
|
+
* Use this over {@link defineGraphqlWebSocketHandler} if you need more control over the WebSocket server or
|
|
20
|
+
* if you want to add custom hooks (e.g. for authentication or logging).
|
|
21
|
+
*/
|
|
22
|
+
declare function defineGraphqlWebSocket<P extends ConnectionInitMessage['payload'] = ConnectionInitMessage['payload'], E extends Record<PropertyKey, unknown> = Record<PropertyKey, never>>(options: ServerOptions<P, Extra & Partial<E>>): Partial<Hooks>;
|
|
23
|
+
/**
|
|
24
|
+
* Create a event handler to be used with [h3](https://h3.unjs.io/).
|
|
25
|
+
*
|
|
26
|
+
* @category Server/h3
|
|
27
|
+
*/
|
|
28
|
+
declare function defineGraphqlWebSocketHandler<P extends ConnectionInitMessage['payload'] = ConnectionInitMessage['payload'], E extends Record<PropertyKey, unknown> = Record<PropertyKey, never>>(options: ServerOptions<P, Extra & Partial<E>>): Promise<EventHandler<EventHandlerRequest, never>>;
|
|
29
|
+
|
|
30
|
+
export { defineGraphqlWebSocket, defineGraphqlWebSocketHandler };
|
|
31
|
+
export type { Extra };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { ConnectionInitMessage, ServerOptions } from 'graphql-ws';
|
|
2
|
+
import { EventHandler, EventHandlerRequest } from 'h3';
|
|
3
|
+
import { Peer, Hooks } from 'crossws';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The extra that will be put in the `Context`.
|
|
7
|
+
*
|
|
8
|
+
* @category Server/h3
|
|
9
|
+
*/
|
|
10
|
+
interface Extra {
|
|
11
|
+
/**
|
|
12
|
+
* The underlying WebSocket peer.
|
|
13
|
+
*/
|
|
14
|
+
readonly peer: Peer;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Create the WebSocket hooks to be used with [h3](https://h3.unjs.io/).
|
|
18
|
+
*
|
|
19
|
+
* Use this over {@link defineGraphqlWebSocketHandler} if you need more control over the WebSocket server or
|
|
20
|
+
* if you want to add custom hooks (e.g. for authentication or logging).
|
|
21
|
+
*/
|
|
22
|
+
declare function defineGraphqlWebSocket<P extends ConnectionInitMessage['payload'] = ConnectionInitMessage['payload'], E extends Record<PropertyKey, unknown> = Record<PropertyKey, never>>(options: ServerOptions<P, Extra & Partial<E>>): Partial<Hooks>;
|
|
23
|
+
/**
|
|
24
|
+
* Create a event handler to be used with [h3](https://h3.unjs.io/).
|
|
25
|
+
*
|
|
26
|
+
* @category Server/h3
|
|
27
|
+
*/
|
|
28
|
+
declare function defineGraphqlWebSocketHandler<P extends ConnectionInitMessage['payload'] = ConnectionInitMessage['payload'], E extends Record<PropertyKey, unknown> = Record<PropertyKey, never>>(options: ServerOptions<P, Extra & Partial<E>>): Promise<EventHandler<EventHandlerRequest, never>>;
|
|
29
|
+
|
|
30
|
+
export { defineGraphqlWebSocket, defineGraphqlWebSocketHandler };
|
|
31
|
+
export type { Extra };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { ConnectionInitMessage, ServerOptions } from 'graphql-ws';
|
|
2
|
+
import { EventHandler, EventHandlerRequest } from 'h3';
|
|
3
|
+
import { Peer, Hooks } from 'crossws';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The extra that will be put in the `Context`.
|
|
7
|
+
*
|
|
8
|
+
* @category Server/h3
|
|
9
|
+
*/
|
|
10
|
+
interface Extra {
|
|
11
|
+
/**
|
|
12
|
+
* The underlying WebSocket peer.
|
|
13
|
+
*/
|
|
14
|
+
readonly peer: Peer;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Create the WebSocket hooks to be used with [h3](https://h3.unjs.io/).
|
|
18
|
+
*
|
|
19
|
+
* Use this over {@link defineGraphqlWebSocketHandler} if you need more control over the WebSocket server or
|
|
20
|
+
* if you want to add custom hooks (e.g. for authentication or logging).
|
|
21
|
+
*/
|
|
22
|
+
declare function defineGraphqlWebSocket<P extends ConnectionInitMessage['payload'] = ConnectionInitMessage['payload'], E extends Record<PropertyKey, unknown> = Record<PropertyKey, never>>(options: ServerOptions<P, Extra & Partial<E>>): Partial<Hooks>;
|
|
23
|
+
/**
|
|
24
|
+
* Create a event handler to be used with [h3](https://h3.unjs.io/).
|
|
25
|
+
*
|
|
26
|
+
* @category Server/h3
|
|
27
|
+
*/
|
|
28
|
+
declare function defineGraphqlWebSocketHandler<P extends ConnectionInitMessage['payload'] = ConnectionInitMessage['payload'], E extends Record<PropertyKey, unknown> = Record<PropertyKey, never>>(options: ServerOptions<P, Extra & Partial<E>>): Promise<EventHandler<EventHandlerRequest, never>>;
|
|
29
|
+
|
|
30
|
+
export { defineGraphqlWebSocket, defineGraphqlWebSocketHandler };
|
|
31
|
+
export type { Extra };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { makeServer, CloseCode, DEPRECATED_GRAPHQL_WS_PROTOCOL } from 'graphql-ws';
|
|
2
|
+
import { defineWebSocket, defineWebSocketHandler } from 'h3';
|
|
3
|
+
|
|
4
|
+
function defineGraphqlWebSocket(options) {
|
|
5
|
+
const server = makeServer(options);
|
|
6
|
+
const peers = /* @__PURE__ */ new WeakMap();
|
|
7
|
+
return defineWebSocket({
|
|
8
|
+
open(peer) {
|
|
9
|
+
const client = {
|
|
10
|
+
handleMessage: () => {
|
|
11
|
+
throw new Error("Message received before handler was registered");
|
|
12
|
+
},
|
|
13
|
+
closed: () => {
|
|
14
|
+
throw new Error("Closed before handler was registered");
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
client.closed = server.opened(
|
|
18
|
+
{
|
|
19
|
+
protocol: peer.request.headers?.get("Sec-WebSocket-Protocol") ?? "",
|
|
20
|
+
send: (message) => {
|
|
21
|
+
if (peers.has(peer)) {
|
|
22
|
+
peer.send(message);
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
close: (code, reason) => {
|
|
26
|
+
if (peers.has(peer)) {
|
|
27
|
+
peer.close(code, reason);
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
onMessage: (cb) => client.handleMessage = cb
|
|
31
|
+
},
|
|
32
|
+
{ peer }
|
|
33
|
+
);
|
|
34
|
+
peers.set(peer, client);
|
|
35
|
+
},
|
|
36
|
+
message(peer, message) {
|
|
37
|
+
const client = peers.get(peer);
|
|
38
|
+
if (!client) throw new Error("Message received for a missing client");
|
|
39
|
+
return client.handleMessage(message.text());
|
|
40
|
+
},
|
|
41
|
+
close(peer, details) {
|
|
42
|
+
const client = peers.get(peer);
|
|
43
|
+
if (!client) throw new Error("Closing a missing client");
|
|
44
|
+
const upgradeProtocol = peer.request.headers?.get(
|
|
45
|
+
"Sec-WebSocket-Protocol"
|
|
46
|
+
);
|
|
47
|
+
if (details.code === CloseCode.SubprotocolNotAcceptable && upgradeProtocol === DEPRECATED_GRAPHQL_WS_PROTOCOL)
|
|
48
|
+
console.warn(
|
|
49
|
+
`Client provided the unsupported and deprecated subprotocol "${upgradeProtocol}" used by subscriptions-transport-ws.Please see https://www.apollographql.com/docs/apollo-server/data/subscriptions/#switching-from-subscriptions-transport-ws.`
|
|
50
|
+
);
|
|
51
|
+
return client.closed(details.code, details.reason);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
async function defineGraphqlWebSocketHandler(options) {
|
|
56
|
+
return defineWebSocketHandler(defineGraphqlWebSocket(options));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export { defineGraphqlWebSocket, defineGraphqlWebSocketHandler };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@as-integrations/h3",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"description": "An Apollo Server integration for use with h3 or Nuxt",
|
|
5
5
|
"repository": "github:apollo-server-integrations/apollo-server-integration-h3",
|
|
6
6
|
"license": "MIT",
|
|
@@ -11,6 +11,11 @@
|
|
|
11
11
|
"types": "./dist/index.d.ts",
|
|
12
12
|
"import": "./dist/index.mjs",
|
|
13
13
|
"require": "./dist/index.cjs"
|
|
14
|
+
},
|
|
15
|
+
"./websocket": {
|
|
16
|
+
"types": "./dist/websocket.d.ts",
|
|
17
|
+
"import": "./dist/websocket.mjs",
|
|
18
|
+
"require": "./dist/websocket.cjs"
|
|
14
19
|
}
|
|
15
20
|
},
|
|
16
21
|
"main": "./dist/index.cjs",
|
|
@@ -20,45 +25,47 @@
|
|
|
20
25
|
"dist"
|
|
21
26
|
],
|
|
22
27
|
"peerDependencies": {
|
|
23
|
-
"@apollo/server": "^4.1.1",
|
|
24
|
-
"
|
|
28
|
+
"@apollo/server": "^4.1.1 || ^5.0.0",
|
|
29
|
+
"crossws": "^0.3.0",
|
|
25
30
|
"graphql": "^16.0.0",
|
|
26
|
-
"graphql-ws": "^5.0.0",
|
|
27
|
-
"
|
|
31
|
+
"graphql-ws": "^5.0.0 || ^6.0.0",
|
|
32
|
+
"h3": "^1.11.0"
|
|
28
33
|
},
|
|
29
34
|
"peerDependenciesMeta": {
|
|
30
35
|
"graphql-ws": {
|
|
31
36
|
"optional": true
|
|
32
37
|
}
|
|
33
38
|
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@apollo/utils.withrequired": "3.0.0"
|
|
41
|
+
},
|
|
34
42
|
"devDependencies": {
|
|
35
|
-
"@apollo/server": "
|
|
36
|
-
"@apollo/server-integration-testsuite": "
|
|
37
|
-
"@
|
|
38
|
-
"@
|
|
39
|
-
"@
|
|
40
|
-
"@
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"eslint": "
|
|
44
|
-
"eslint-config-prettier": "
|
|
45
|
-
"eslint-config-unjs": "
|
|
46
|
-
"eslint-plugin-unused-imports": "
|
|
47
|
-
"graphql": "
|
|
48
|
-
"graphql-subscriptions": "
|
|
49
|
-
"graphql-ws": "
|
|
50
|
-
"h3": "
|
|
51
|
-
"jest": "
|
|
52
|
-
"listhen": "
|
|
53
|
-
"prettier": "
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"vitest": "^2.0.1"
|
|
43
|
+
"@apollo/server": "5.2.0",
|
|
44
|
+
"@apollo/server-integration-testsuite": "5.2.0",
|
|
45
|
+
"@graphql-tools/schema": "10.0.30",
|
|
46
|
+
"@jest/globals": "30.2.0",
|
|
47
|
+
"@typescript-eslint/parser": "8.48.1",
|
|
48
|
+
"@vitest/coverage-v8": "4.0.15",
|
|
49
|
+
"commit-and-tag-version": "12.6.1",
|
|
50
|
+
"crossws": "0.3.5",
|
|
51
|
+
"eslint": "9.39.1",
|
|
52
|
+
"eslint-config-prettier": "10.1.8",
|
|
53
|
+
"eslint-config-unjs": "0.5.0",
|
|
54
|
+
"eslint-plugin-unused-imports": "4.3.0",
|
|
55
|
+
"graphql": "16.12.0",
|
|
56
|
+
"graphql-subscriptions": "3.0.0",
|
|
57
|
+
"graphql-ws": "6.0.6",
|
|
58
|
+
"h3": "1.15.4",
|
|
59
|
+
"jest": "30.2.0",
|
|
60
|
+
"listhen": "1.9.0",
|
|
61
|
+
"prettier": "3.7.3",
|
|
62
|
+
"ts-jest": "29.4.6",
|
|
63
|
+
"typescript": "5.9.3",
|
|
64
|
+
"unbuild": "3.6.1",
|
|
65
|
+
"vitest": "4.0.15"
|
|
59
66
|
},
|
|
60
67
|
"engines": {
|
|
61
|
-
"node": "
|
|
68
|
+
"node": ">=20.0.0"
|
|
62
69
|
},
|
|
63
70
|
"scripts": {
|
|
64
71
|
"dev:prepare": "unbuild --stub",
|