@effect-gql/node 0.1.0
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/LICENSE +7 -0
- package/dist/config.d.ts +43 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +52 -0
- package/dist/config.js.map +1 -0
- package/dist/graphiql.d.ts +5 -0
- package/dist/graphiql.d.ts.map +1 -0
- package/dist/graphiql.js +43 -0
- package/dist/graphiql.js.map +1 -0
- package/dist/http-utils.d.ts +20 -0
- package/dist/http-utils.d.ts.map +1 -0
- package/dist/http-utils.js +36 -0
- package/dist/http-utils.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/router.d.ts +32 -0
- package/dist/router.d.ts.map +1 -0
- package/dist/router.js +77 -0
- package/dist/router.js.map +1 -0
- package/dist/schema-builder-extensions.d.ts +31 -0
- package/dist/schema-builder-extensions.d.ts.map +1 -0
- package/dist/schema-builder-extensions.js +35 -0
- package/dist/schema-builder-extensions.js.map +1 -0
- package/dist/serve.d.ts +73 -0
- package/dist/serve.d.ts.map +1 -0
- package/dist/serve.js +176 -0
- package/dist/serve.js.map +1 -0
- package/dist/sse.d.ts +86 -0
- package/dist/sse.d.ts.map +1 -0
- package/dist/sse.js +175 -0
- package/dist/sse.js.map +1 -0
- package/dist/ws.d.ts +97 -0
- package/dist/ws.d.ts.map +1 -0
- package/dist/ws.js +137 -0
- package/dist/ws.js.map +1 -0
- package/package.json +57 -0
- package/src/http-utils.ts +32 -0
- package/src/index.ts +15 -0
- package/src/serve.ts +217 -0
- package/src/sse.ts +234 -0
- package/src/ws.ts +183 -0
package/dist/serve.js
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.serve = void 0;
|
|
37
|
+
const effect_1 = require("effect");
|
|
38
|
+
const platform_1 = require("@effect/platform");
|
|
39
|
+
const platform_node_1 = require("@effect/platform-node");
|
|
40
|
+
const node_http_1 = require("node:http");
|
|
41
|
+
const http_utils_1 = require("./http-utils");
|
|
42
|
+
/**
|
|
43
|
+
* Start a Node.js HTTP server with the given router.
|
|
44
|
+
*
|
|
45
|
+
* This is the main entry point for running a GraphQL server on Node.js.
|
|
46
|
+
* It handles all the Effect runtime setup and server lifecycle.
|
|
47
|
+
*
|
|
48
|
+
* @param router - The HttpRouter to serve (typically from makeGraphQLRouter or toRouter)
|
|
49
|
+
* @param layer - Layer providing the router's service dependencies
|
|
50
|
+
* @param options - Server configuration options
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* import { makeGraphQLRouter } from "@effect-gql/core"
|
|
55
|
+
* import { serve } from "@effect-gql/node"
|
|
56
|
+
*
|
|
57
|
+
* const schema = GraphQLSchemaBuilder.empty
|
|
58
|
+
* .query("hello", { type: S.String, resolve: () => Effect.succeed("world") })
|
|
59
|
+
* .buildSchema()
|
|
60
|
+
*
|
|
61
|
+
* const router = makeGraphQLRouter(schema, Layer.empty, { graphiql: true })
|
|
62
|
+
*
|
|
63
|
+
* // Without subscriptions
|
|
64
|
+
* serve(router, serviceLayer, {
|
|
65
|
+
* port: 4000,
|
|
66
|
+
* onStart: (url) => console.log(`Server running at ${url}`)
|
|
67
|
+
* })
|
|
68
|
+
*
|
|
69
|
+
* // With subscriptions
|
|
70
|
+
* serve(router, serviceLayer, {
|
|
71
|
+
* port: 4000,
|
|
72
|
+
* subscriptions: { schema },
|
|
73
|
+
* onStart: (url) => console.log(`Server running at ${url}`)
|
|
74
|
+
* })
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
const serve = (router, layer, options = {}) => {
|
|
78
|
+
const { port = 4000, host = "0.0.0.0", onStart, subscriptions } = options;
|
|
79
|
+
if (subscriptions) {
|
|
80
|
+
// With WebSocket subscriptions - we need to manage the HTTP server ourselves
|
|
81
|
+
serveWithSubscriptions(router, layer, port, host, subscriptions, onStart);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
// Without subscriptions - use the standard Effect approach
|
|
85
|
+
const app = router.pipe(effect_1.Effect.catchAllCause((cause) => effect_1.Effect.die(cause)), platform_1.HttpServer.serve());
|
|
86
|
+
const serverLayer = platform_node_1.NodeHttpServer.layer(() => (0, node_http_1.createServer)(), { port });
|
|
87
|
+
const fullLayer = effect_1.Layer.merge(serverLayer, layer);
|
|
88
|
+
if (onStart) {
|
|
89
|
+
onStart(`http://${host === "0.0.0.0" ? "localhost" : host}:${port}`);
|
|
90
|
+
}
|
|
91
|
+
platform_node_1.NodeRuntime.runMain(effect_1.Layer.launch(effect_1.Layer.provide(app, fullLayer)));
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
exports.serve = serve;
|
|
95
|
+
/**
|
|
96
|
+
* Internal implementation for serving with WebSocket subscriptions.
|
|
97
|
+
* Uses a custom HTTP server setup to enable WebSocket upgrade handling.
|
|
98
|
+
*/
|
|
99
|
+
function serveWithSubscriptions(router, layer, port, host, subscriptions, onStart) {
|
|
100
|
+
// Dynamically import ws module to keep it optional
|
|
101
|
+
const importWs = effect_1.Effect.tryPromise({
|
|
102
|
+
try: () => Promise.resolve().then(() => __importStar(require("./ws"))),
|
|
103
|
+
catch: (error) => error,
|
|
104
|
+
});
|
|
105
|
+
effect_1.Effect.runPromise(importWs.pipe(effect_1.Effect.catchAll((error) => effect_1.Effect.logError("Failed to load WebSocket support", error).pipe(effect_1.Effect.andThen(effect_1.Effect.logError("Make sure 'ws' package is installed: npm install ws")), effect_1.Effect.andThen(effect_1.Effect.sync(() => process.exit(1))), effect_1.Effect.andThen(effect_1.Effect.fail(error)))))).then(({ createGraphQLWSServer }) => {
|
|
106
|
+
// Create the web handler from the Effect router
|
|
107
|
+
const { handler } = platform_1.HttpApp.toWebHandlerLayer(router, layer);
|
|
108
|
+
// Create the HTTP server
|
|
109
|
+
const httpServer = (0, node_http_1.createServer)(async (req, res) => {
|
|
110
|
+
try {
|
|
111
|
+
// Collect request body
|
|
112
|
+
const chunks = [];
|
|
113
|
+
for await (const chunk of req) {
|
|
114
|
+
chunks.push(chunk);
|
|
115
|
+
}
|
|
116
|
+
const body = Buffer.concat(chunks).toString();
|
|
117
|
+
// Convert Node.js request to web standard Request
|
|
118
|
+
// Use URL constructor for safe URL parsing (avoids injection via req.url)
|
|
119
|
+
const baseUrl = `http://${host === "0.0.0.0" ? "localhost" : host}:${port}`;
|
|
120
|
+
const url = new URL(req.url || "/", baseUrl).href;
|
|
121
|
+
const headers = (0, http_utils_1.toWebHeaders)(req.headers);
|
|
122
|
+
const webRequest = new Request(url, {
|
|
123
|
+
method: req.method,
|
|
124
|
+
headers,
|
|
125
|
+
body: ["GET", "HEAD"].includes(req.method) ? undefined : body,
|
|
126
|
+
});
|
|
127
|
+
// Process through Effect handler
|
|
128
|
+
const webResponse = await handler(webRequest);
|
|
129
|
+
// Write response
|
|
130
|
+
res.statusCode = webResponse.status;
|
|
131
|
+
webResponse.headers.forEach((value, key) => {
|
|
132
|
+
res.setHeader(key, value);
|
|
133
|
+
});
|
|
134
|
+
const responseBody = await webResponse.text();
|
|
135
|
+
res.end(responseBody);
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
res.statusCode = 500;
|
|
139
|
+
res.end(JSON.stringify({ error: String(error) }));
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
// Create WebSocket server for subscriptions
|
|
143
|
+
const { handleUpgrade, close: closeWS } = createGraphQLWSServer(subscriptions.schema, layer, {
|
|
144
|
+
path: subscriptions.path,
|
|
145
|
+
complexity: subscriptions.complexity,
|
|
146
|
+
fieldComplexities: subscriptions.fieldComplexities,
|
|
147
|
+
onConnect: subscriptions.onConnect,
|
|
148
|
+
onDisconnect: subscriptions.onDisconnect,
|
|
149
|
+
onSubscribe: subscriptions.onSubscribe,
|
|
150
|
+
onComplete: subscriptions.onComplete,
|
|
151
|
+
onError: subscriptions.onError,
|
|
152
|
+
});
|
|
153
|
+
// Attach WebSocket upgrade handler
|
|
154
|
+
httpServer.on("upgrade", (request, socket, head) => {
|
|
155
|
+
handleUpgrade(request, socket, head);
|
|
156
|
+
});
|
|
157
|
+
// Handle shutdown
|
|
158
|
+
process.on("SIGINT", async () => {
|
|
159
|
+
await closeWS();
|
|
160
|
+
httpServer.close();
|
|
161
|
+
process.exit(0);
|
|
162
|
+
});
|
|
163
|
+
process.on("SIGTERM", async () => {
|
|
164
|
+
await closeWS();
|
|
165
|
+
httpServer.close();
|
|
166
|
+
process.exit(0);
|
|
167
|
+
});
|
|
168
|
+
// Start listening
|
|
169
|
+
httpServer.listen(port, host, () => {
|
|
170
|
+
if (onStart) {
|
|
171
|
+
onStart(`http://${host === "0.0.0.0" ? "localhost" : host}:${port}`);
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
//# sourceMappingURL=serve.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serve.js","sourceRoot":"","sources":["../src/serve.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,mCAAsC;AACtC,+CAAkE;AAClE,yDAAmE;AACnE,yCAAwC;AAGxC,6CAA2C;AAoC3C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACI,MAAM,KAAK,GAAG,CACnB,MAAmC,EACnC,KAAyB,EACzB,UAA2B,EAAE,EACvB,EAAE;IACR,MAAM,EAAE,IAAI,GAAG,IAAI,EAAE,IAAI,GAAG,SAAS,EAAE,OAAO,EAAE,aAAa,EAAE,GAAG,OAAO,CAAA;IAEzE,IAAI,aAAa,EAAE,CAAC;QAClB,6EAA6E;QAC7E,sBAAsB,CAAC,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,aAAa,EAAE,OAAO,CAAC,CAAA;IAC3E,CAAC;SAAM,CAAC;QACN,2DAA2D;QAC3D,MAAM,GAAG,GAAG,MAAM,CAAC,IAAI,CACrB,eAAM,CAAC,aAAa,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,eAAM,CAAC,GAAG,CAAC,KAAK,CAA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AAE,IAAI,EAAE,EAAE;YACjD,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,CAAA;QACtC,CAAC,CAAC,CAAA;QAEF,kBAAkB;QAClB,OAAO,CAAC,EAAE,CAAC,QAAQ,EAAE,KAAK,IAAI,EAAE;YAC9B,MAAM,OAAO,EAAE,CAAA;YACf,UAAU,CAAC,KAAK,EAAE,CAAA;YAClB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;QACjB,CAAC,CAAC,CAAA;QAEF,OAAO,CAAC,EAAE,CAAC,SAAS,EAAE,KAAK,IAAI,EAAE;YAC/B,MAAM,OAAO,EAAE,CAAA;YACf,UAAU,CAAC,KAAK,EAAE,CAAA;YAClB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;QACjB,CAAC,CAAC,CAAA;QAEF,kBAAkB;QAClB,UAAU,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE;YACjC,IAAI,OAAO,EAAE,CAAC;gBACZ,OAAO,CAAC,UAAU,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,IAAI,IAAI,IAAI,EAAE,CAAC,CAAA;YACtE,CAAC;QACH,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC"}
|
package/dist/sse.d.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { Layer } from "effect";
|
|
2
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
3
|
+
import { GraphQLSchema } from "graphql";
|
|
4
|
+
import { type GraphQLSSEOptions } from "@effect-gql/core";
|
|
5
|
+
/**
|
|
6
|
+
* Options for Node.js SSE handler
|
|
7
|
+
*/
|
|
8
|
+
export interface NodeSSEOptions<R> extends GraphQLSSEOptions<R> {
|
|
9
|
+
/**
|
|
10
|
+
* Path for SSE connections.
|
|
11
|
+
* @default "/graphql/stream"
|
|
12
|
+
*/
|
|
13
|
+
readonly path?: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Create an SSE handler for Node.js HTTP server.
|
|
17
|
+
*
|
|
18
|
+
* This function creates a handler that can process SSE subscription requests.
|
|
19
|
+
* It handles:
|
|
20
|
+
* - Parsing the GraphQL subscription request from the HTTP body
|
|
21
|
+
* - Setting up the SSE connection with proper headers
|
|
22
|
+
* - Streaming subscription events to the client
|
|
23
|
+
* - Detecting client disconnection and cleaning up
|
|
24
|
+
*
|
|
25
|
+
* @param schema - The GraphQL schema with subscription definitions
|
|
26
|
+
* @param layer - Effect layer providing services required by resolvers
|
|
27
|
+
* @param options - Optional lifecycle hooks and configuration
|
|
28
|
+
* @returns A request handler function
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* import { createServer } from "node:http"
|
|
33
|
+
* import { createSSEHandler } from "@effect-gql/node"
|
|
34
|
+
*
|
|
35
|
+
* const sseHandler = createSSEHandler(schema, serviceLayer, {
|
|
36
|
+
* path: "/graphql/stream",
|
|
37
|
+
* onConnect: (request, headers) => Effect.gen(function* () {
|
|
38
|
+
* const user = yield* AuthService.validateToken(headers.get("authorization"))
|
|
39
|
+
* return { user }
|
|
40
|
+
* }),
|
|
41
|
+
* })
|
|
42
|
+
*
|
|
43
|
+
* const server = createServer((req, res) => {
|
|
44
|
+
* const url = new URL(req.url, `http://${req.headers.host}`)
|
|
45
|
+
* if (url.pathname === "/graphql/stream" && req.method === "POST") {
|
|
46
|
+
* sseHandler(req, res)
|
|
47
|
+
* } else {
|
|
48
|
+
* // Handle other requests...
|
|
49
|
+
* }
|
|
50
|
+
* })
|
|
51
|
+
*
|
|
52
|
+
* server.listen(4000)
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export declare const createSSEHandler: <R>(schema: GraphQLSchema, layer: Layer.Layer<R>, options?: NodeSSEOptions<R>) => ((req: IncomingMessage, res: ServerResponse) => Promise<void>);
|
|
56
|
+
/**
|
|
57
|
+
* Create SSE middleware that can be used with the serve() function.
|
|
58
|
+
*
|
|
59
|
+
* This returns an object that can be used to integrate SSE subscriptions
|
|
60
|
+
* with the HTTP server when using the custom subscription mode.
|
|
61
|
+
*
|
|
62
|
+
* @param schema - The GraphQL schema with subscription definitions
|
|
63
|
+
* @param layer - Effect layer providing services required by resolvers
|
|
64
|
+
* @param options - Optional lifecycle hooks and configuration
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* // In serve.ts with custom HTTP server setup
|
|
69
|
+
* const sseServer = createSSEServer(schema, layer, { path: "/graphql/stream" })
|
|
70
|
+
*
|
|
71
|
+
* httpServer.on("request", (req, res) => {
|
|
72
|
+
* if (sseServer.shouldHandle(req)) {
|
|
73
|
+
* sseServer.handle(req, res)
|
|
74
|
+
* }
|
|
75
|
+
* })
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export declare const createSSEServer: <R>(schema: GraphQLSchema, layer: Layer.Layer<R>, options?: NodeSSEOptions<R>) => {
|
|
79
|
+
/** Path this SSE server handles */
|
|
80
|
+
readonly path: string;
|
|
81
|
+
/** Check if a request should be handled by this SSE server */
|
|
82
|
+
shouldHandle: (req: IncomingMessage) => boolean;
|
|
83
|
+
/** Handle an SSE request */
|
|
84
|
+
handle: (req: IncomingMessage, res: ServerResponse) => Promise<void>;
|
|
85
|
+
};
|
|
86
|
+
//# sourceMappingURL=sse.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sse.d.ts","sourceRoot":"","sources":["../src/sse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,KAAK,EAAoB,MAAM,QAAQ,CAAA;AACxD,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,WAAW,CAAA;AAChE,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AACvC,OAAO,EAIL,KAAK,iBAAiB,EAGvB,MAAM,kBAAkB,CAAA;AAGzB;;GAEG;AACH,MAAM,WAAW,cAAc,CAAC,CAAC,CAAE,SAAQ,iBAAiB,CAAC,CAAC,CAAC;IAC7D;;;OAGG;IACH,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CACvB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,eAAO,MAAM,gBAAgB,GAAI,CAAC,EAChC,QAAQ,aAAa,EACrB,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,EACrB,UAAU,cAAc,CAAC,CAAC,CAAC,KAC1B,CAAC,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,EAAE,cAAc,KAAK,OAAO,CAAC,IAAI,CAAC,CAyG/D,CAAA;AAcD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,eAAO,MAAM,eAAe,GAAI,CAAC,EAC/B,QAAQ,aAAa,EACrB,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,EACrB,UAAU,cAAc,CAAC,CAAC,CAAC,KAC1B;IACD,mCAAmC;IACnC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,8DAA8D;IAC9D,YAAY,EAAE,CAAC,GAAG,EAAE,eAAe,KAAK,OAAO,CAAA;IAC/C,4BAA4B;IAC5B,MAAM,EAAE,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,EAAE,cAAc,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;CAcrE,CAAA"}
|
package/dist/sse.js
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createSSEServer = exports.createSSEHandler = void 0;
|
|
4
|
+
const effect_1 = require("effect");
|
|
5
|
+
const core_1 = require("@effect-gql/core");
|
|
6
|
+
const http_utils_1 = require("./http-utils");
|
|
7
|
+
/**
|
|
8
|
+
* Create an SSE handler for Node.js HTTP server.
|
|
9
|
+
*
|
|
10
|
+
* This function creates a handler that can process SSE subscription requests.
|
|
11
|
+
* It handles:
|
|
12
|
+
* - Parsing the GraphQL subscription request from the HTTP body
|
|
13
|
+
* - Setting up the SSE connection with proper headers
|
|
14
|
+
* - Streaming subscription events to the client
|
|
15
|
+
* - Detecting client disconnection and cleaning up
|
|
16
|
+
*
|
|
17
|
+
* @param schema - The GraphQL schema with subscription definitions
|
|
18
|
+
* @param layer - Effect layer providing services required by resolvers
|
|
19
|
+
* @param options - Optional lifecycle hooks and configuration
|
|
20
|
+
* @returns A request handler function
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* import { createServer } from "node:http"
|
|
25
|
+
* import { createSSEHandler } from "@effect-gql/node"
|
|
26
|
+
*
|
|
27
|
+
* const sseHandler = createSSEHandler(schema, serviceLayer, {
|
|
28
|
+
* path: "/graphql/stream",
|
|
29
|
+
* onConnect: (request, headers) => Effect.gen(function* () {
|
|
30
|
+
* const user = yield* AuthService.validateToken(headers.get("authorization"))
|
|
31
|
+
* return { user }
|
|
32
|
+
* }),
|
|
33
|
+
* })
|
|
34
|
+
*
|
|
35
|
+
* const server = createServer((req, res) => {
|
|
36
|
+
* const url = new URL(req.url, `http://${req.headers.host}`)
|
|
37
|
+
* if (url.pathname === "/graphql/stream" && req.method === "POST") {
|
|
38
|
+
* sseHandler(req, res)
|
|
39
|
+
* } else {
|
|
40
|
+
* // Handle other requests...
|
|
41
|
+
* }
|
|
42
|
+
* })
|
|
43
|
+
*
|
|
44
|
+
* server.listen(4000)
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
const createSSEHandler = (schema, layer, options) => {
|
|
48
|
+
const sseHandler = (0, core_1.makeGraphQLSSEHandler)(schema, layer, options);
|
|
49
|
+
return async (req, res) => {
|
|
50
|
+
// Check Accept header for SSE support
|
|
51
|
+
const accept = req.headers.accept ?? "";
|
|
52
|
+
if (!accept.includes("text/event-stream") && !accept.includes("*/*")) {
|
|
53
|
+
res.statusCode = 406;
|
|
54
|
+
res.end(JSON.stringify({
|
|
55
|
+
errors: [{ message: "Client must accept text/event-stream" }],
|
|
56
|
+
}));
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
// Read the request body
|
|
60
|
+
let body;
|
|
61
|
+
try {
|
|
62
|
+
body = await readBody(req);
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
res.statusCode = 400;
|
|
66
|
+
res.end(JSON.stringify({
|
|
67
|
+
errors: [{ message: "Failed to read request body" }],
|
|
68
|
+
}));
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
// Parse the GraphQL request
|
|
72
|
+
let request;
|
|
73
|
+
try {
|
|
74
|
+
const parsed = JSON.parse(body);
|
|
75
|
+
if (typeof parsed.query !== "string") {
|
|
76
|
+
throw new Error("Missing query");
|
|
77
|
+
}
|
|
78
|
+
request = {
|
|
79
|
+
query: parsed.query,
|
|
80
|
+
variables: parsed.variables,
|
|
81
|
+
operationName: parsed.operationName,
|
|
82
|
+
extensions: parsed.extensions,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
res.statusCode = 400;
|
|
87
|
+
res.end(JSON.stringify({
|
|
88
|
+
errors: [{ message: "Invalid GraphQL request body" }],
|
|
89
|
+
}));
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
// Convert Node.js headers to web Headers
|
|
93
|
+
const headers = (0, http_utils_1.toWebHeaders)(req.headers);
|
|
94
|
+
// Set SSE headers
|
|
95
|
+
res.writeHead(200, core_1.SSE_HEADERS);
|
|
96
|
+
// Get the event stream
|
|
97
|
+
const eventStream = sseHandler(request, headers);
|
|
98
|
+
// Create the streaming effect
|
|
99
|
+
const streamEffect = effect_1.Effect.gen(function* () {
|
|
100
|
+
// Track client disconnection
|
|
101
|
+
const clientDisconnected = yield* effect_1.Deferred.make();
|
|
102
|
+
req.on("close", () => {
|
|
103
|
+
effect_1.Effect.runPromise(effect_1.Deferred.succeed(clientDisconnected, undefined)).catch(() => { });
|
|
104
|
+
});
|
|
105
|
+
req.on("error", (error) => {
|
|
106
|
+
effect_1.Effect.runPromise(effect_1.Deferred.fail(clientDisconnected, new core_1.SSEError({ cause: error }))).catch(() => { });
|
|
107
|
+
});
|
|
108
|
+
// Stream events to the client
|
|
109
|
+
const runStream = effect_1.Stream.runForEach(eventStream, (event) => effect_1.Effect.async((resume) => {
|
|
110
|
+
const message = (0, core_1.formatSSEMessage)(event);
|
|
111
|
+
res.write(message, (error) => {
|
|
112
|
+
if (error) {
|
|
113
|
+
resume(effect_1.Effect.fail(new core_1.SSEError({ cause: error })));
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
resume(effect_1.Effect.succeed(undefined));
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
}));
|
|
120
|
+
// Race between stream completion and client disconnection
|
|
121
|
+
yield* effect_1.Effect.race(runStream.pipe(effect_1.Effect.catchAll((error) => effect_1.Effect.logWarning("SSE stream error", error))), effect_1.Deferred.await(clientDisconnected));
|
|
122
|
+
});
|
|
123
|
+
await effect_1.Effect.runPromise(streamEffect.pipe(effect_1.Effect.ensuring(effect_1.Effect.sync(() => res.end())), effect_1.Effect.catchAll(() => effect_1.Effect.void)));
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
exports.createSSEHandler = createSSEHandler;
|
|
127
|
+
/**
|
|
128
|
+
* Read the request body as a string.
|
|
129
|
+
*/
|
|
130
|
+
function readBody(req) {
|
|
131
|
+
return new Promise((resolve, reject) => {
|
|
132
|
+
const chunks = [];
|
|
133
|
+
req.on("data", (chunk) => chunks.push(chunk));
|
|
134
|
+
req.on("end", () => resolve(Buffer.concat(chunks).toString()));
|
|
135
|
+
req.on("error", reject);
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Create SSE middleware that can be used with the serve() function.
|
|
140
|
+
*
|
|
141
|
+
* This returns an object that can be used to integrate SSE subscriptions
|
|
142
|
+
* with the HTTP server when using the custom subscription mode.
|
|
143
|
+
*
|
|
144
|
+
* @param schema - The GraphQL schema with subscription definitions
|
|
145
|
+
* @param layer - Effect layer providing services required by resolvers
|
|
146
|
+
* @param options - Optional lifecycle hooks and configuration
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* ```typescript
|
|
150
|
+
* // In serve.ts with custom HTTP server setup
|
|
151
|
+
* const sseServer = createSSEServer(schema, layer, { path: "/graphql/stream" })
|
|
152
|
+
*
|
|
153
|
+
* httpServer.on("request", (req, res) => {
|
|
154
|
+
* if (sseServer.shouldHandle(req)) {
|
|
155
|
+
* sseServer.handle(req, res)
|
|
156
|
+
* }
|
|
157
|
+
* })
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
const createSSEServer = (schema, layer, options) => {
|
|
161
|
+
const path = options?.path ?? "/graphql/stream";
|
|
162
|
+
const handler = (0, exports.createSSEHandler)(schema, layer, options);
|
|
163
|
+
return {
|
|
164
|
+
path,
|
|
165
|
+
shouldHandle: (req) => {
|
|
166
|
+
if (req.method !== "POST")
|
|
167
|
+
return false;
|
|
168
|
+
const url = new URL(req.url ?? "/", `http://${req.headers.host ?? "localhost"}`);
|
|
169
|
+
return url.pathname === path;
|
|
170
|
+
},
|
|
171
|
+
handle: handler,
|
|
172
|
+
};
|
|
173
|
+
};
|
|
174
|
+
exports.createSSEServer = createSSEServer;
|
|
175
|
+
//# sourceMappingURL=sse.js.map
|
package/dist/sse.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sse.js","sourceRoot":"","sources":["../src/sse.ts"],"names":[],"mappings":";;;AAAA,mCAAwD;AAGxD,2CAOyB;AACzB,6CAA2C;AAa3C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACI,MAAM,gBAAgB,GAAG,CAC9B,MAAqB,EACrB,KAAqB,EACrB,OAA2B,EACqC,EAAE;IAClE,MAAM,UAAU,GAAG,IAAA,4B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IncomingMessage, Server } from "node:http";
|
|
3
|
+
import type { Duplex } from "node:stream";
|
|
4
|
+
import { WebSocket, WebSocketServer } from "ws";
|
|
5
|
+
import { GraphQLSchema } from "graphql";
|
|
6
|
+
import { type EffectWebSocket, type GraphQLWSOptions } from "@effect-gql/core";
|
|
7
|
+
/**
|
|
8
|
+
* Options for Node.js WebSocket server
|
|
9
|
+
*/
|
|
10
|
+
export interface NodeWSOptions<R> extends GraphQLWSOptions<R> {
|
|
11
|
+
/**
|
|
12
|
+
* Path for WebSocket connections.
|
|
13
|
+
* @default "/graphql"
|
|
14
|
+
*/
|
|
15
|
+
readonly path?: string;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Convert a Node.js WebSocket (from 'ws' library) to an EffectWebSocket.
|
|
19
|
+
*
|
|
20
|
+
* This creates an Effect-based wrapper around the ws WebSocket instance,
|
|
21
|
+
* providing a Stream for incoming messages and Effect-based send/close operations.
|
|
22
|
+
*
|
|
23
|
+
* @param ws - The WebSocket instance from the 'ws' library
|
|
24
|
+
* @returns An EffectWebSocket that can be used with makeGraphQLWSHandler
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* wss.on("connection", (ws, req) => {
|
|
29
|
+
* const effectSocket = toEffectWebSocket(ws)
|
|
30
|
+
* Effect.runPromise(handler(effectSocket))
|
|
31
|
+
* })
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare const toEffectWebSocket: (ws: WebSocket) => EffectWebSocket;
|
|
35
|
+
/**
|
|
36
|
+
* Create a WebSocket server that handles GraphQL subscriptions.
|
|
37
|
+
*
|
|
38
|
+
* This function creates a WebSocketServer and returns utilities for
|
|
39
|
+
* integrating it with an HTTP server via the upgrade event.
|
|
40
|
+
*
|
|
41
|
+
* @param schema - The GraphQL schema with subscription definitions
|
|
42
|
+
* @param layer - Effect layer providing services required by resolvers
|
|
43
|
+
* @param options - Optional configuration and lifecycle hooks
|
|
44
|
+
* @returns Object containing the WebSocketServer and handlers
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* const httpServer = createServer(requestHandler)
|
|
49
|
+
* const { wss, handleUpgrade } = createGraphQLWSServer(schema, serviceLayer)
|
|
50
|
+
*
|
|
51
|
+
* httpServer.on("upgrade", (request, socket, head) => {
|
|
52
|
+
* if (request.url === "/graphql") {
|
|
53
|
+
* handleUpgrade(request, socket, head)
|
|
54
|
+
* }
|
|
55
|
+
* })
|
|
56
|
+
*
|
|
57
|
+
* httpServer.listen(4000)
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export declare const createGraphQLWSServer: <R>(schema: GraphQLSchema, layer: Layer.Layer<R>, options?: NodeWSOptions<R>) => {
|
|
61
|
+
/** The underlying WebSocketServer instance */
|
|
62
|
+
wss: WebSocketServer;
|
|
63
|
+
/** Handle HTTP upgrade requests */
|
|
64
|
+
handleUpgrade: (request: IncomingMessage, socket: Duplex, head: Buffer) => void;
|
|
65
|
+
/** Close the WebSocket server */
|
|
66
|
+
close: () => Promise<void>;
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* Attach WebSocket subscription support to an existing HTTP server.
|
|
70
|
+
*
|
|
71
|
+
* This is a convenience function that creates a GraphQL WebSocket server
|
|
72
|
+
* and attaches it to an HTTP server's upgrade event.
|
|
73
|
+
*
|
|
74
|
+
* @param server - The HTTP server to attach to
|
|
75
|
+
* @param schema - The GraphQL schema with subscription definitions
|
|
76
|
+
* @param layer - Effect layer providing services required by resolvers
|
|
77
|
+
* @param options - Optional configuration and lifecycle hooks
|
|
78
|
+
* @returns Cleanup function to close the WebSocket server
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```typescript
|
|
82
|
+
* const httpServer = createServer(requestHandler)
|
|
83
|
+
*
|
|
84
|
+
* const cleanup = attachWebSocketToServer(httpServer, schema, serviceLayer, {
|
|
85
|
+
* path: "/graphql",
|
|
86
|
+
* })
|
|
87
|
+
*
|
|
88
|
+
* httpServer.listen(4000)
|
|
89
|
+
*
|
|
90
|
+
* // Later, to cleanup:
|
|
91
|
+
* await cleanup()
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
export declare const attachWebSocketToServer: <R>(server: Server, schema: GraphQLSchema, layer: Layer.Layer<R>, options?: NodeWSOptions<R>) => {
|
|
95
|
+
close: () => Promise<void>;
|
|
96
|
+
};
|
|
97
|
+
//# sourceMappingURL=ws.d.ts.map
|
package/dist/ws.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ws.d.ts","sourceRoot":"","sources":["../src/ws.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,KAAK,EAAE,MAAM,QAAQ,CAAA;AACtC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AACxD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACzC,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,IAAI,CAAA;AAC/C,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AACvC,OAAO,EAGL,KAAK,eAAe,EACpB,KAAK,gBAAgB,EACtB,MAAM,kBAAkB,CAAA;AAEzB;;GAEG;AACH,MAAM,WAAW,aAAa,CAAC,CAAC,CAAE,SAAQ,gBAAgB,CAAC,CAAC,CAAC;IAC3D;;;OAGG;IACH,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CACvB;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,iBAAiB,GAAI,IAAI,SAAS,KAAG,eAA8C,CAAA;AAEhG;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,eAAO,MAAM,qBAAqB,GAAI,CAAC,EACrC,QAAQ,aAAa,EACrB,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,EACrB,UAAU,aAAa,CAAC,CAAC,CAAC,KACz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