@effect-gql/node 0.1.0 → 1.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.
Files changed (49) hide show
  1. package/README.md +100 -0
  2. package/index.cjs +315 -0
  3. package/index.cjs.map +1 -0
  4. package/index.d.cts +270 -0
  5. package/index.d.ts +270 -0
  6. package/index.js +310 -0
  7. package/index.js.map +1 -0
  8. package/package.json +14 -31
  9. package/dist/config.d.ts +0 -43
  10. package/dist/config.d.ts.map +0 -1
  11. package/dist/config.js +0 -52
  12. package/dist/config.js.map +0 -1
  13. package/dist/graphiql.d.ts +0 -5
  14. package/dist/graphiql.d.ts.map +0 -1
  15. package/dist/graphiql.js +0 -43
  16. package/dist/graphiql.js.map +0 -1
  17. package/dist/http-utils.d.ts +0 -20
  18. package/dist/http-utils.d.ts.map +0 -1
  19. package/dist/http-utils.js +0 -36
  20. package/dist/http-utils.js.map +0 -1
  21. package/dist/index.d.ts +0 -5
  22. package/dist/index.d.ts.map +0 -1
  23. package/dist/index.js +0 -18
  24. package/dist/index.js.map +0 -1
  25. package/dist/router.d.ts +0 -32
  26. package/dist/router.d.ts.map +0 -1
  27. package/dist/router.js +0 -77
  28. package/dist/router.js.map +0 -1
  29. package/dist/schema-builder-extensions.d.ts +0 -31
  30. package/dist/schema-builder-extensions.d.ts.map +0 -1
  31. package/dist/schema-builder-extensions.js +0 -35
  32. package/dist/schema-builder-extensions.js.map +0 -1
  33. package/dist/serve.d.ts +0 -73
  34. package/dist/serve.d.ts.map +0 -1
  35. package/dist/serve.js +0 -176
  36. package/dist/serve.js.map +0 -1
  37. package/dist/sse.d.ts +0 -86
  38. package/dist/sse.d.ts.map +0 -1
  39. package/dist/sse.js +0 -175
  40. package/dist/sse.js.map +0 -1
  41. package/dist/ws.d.ts +0 -97
  42. package/dist/ws.d.ts.map +0 -1
  43. package/dist/ws.js +0 -137
  44. package/dist/ws.js.map +0 -1
  45. package/src/http-utils.ts +0 -32
  46. package/src/index.ts +0 -15
  47. package/src/serve.ts +0 -217
  48. package/src/sse.ts +0 -234
  49. package/src/ws.ts +0 -183
package/dist/sse.js DELETED
@@ -1,175 +0,0 @@
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 DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"sse.js","sourceRoot":"","sources":["../src/sse.ts"],"names":[],"mappings":";;;AAAA,mCAAwD;AAGxD,2CAOyB;AACzB,6CAA2C;AAa3C;;;;;;;;;;;;;;;;;;;;;;;;;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,CAAC,CAAA;AAxBY,QAAA,eAAe,mBAwB3B"}
package/dist/ws.d.ts DELETED
@@ -1,97 +0,0 @@
1
- import { Layer } from "effect";
2
- import type { 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 DELETED
@@ -1 +0,0 @@
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,KACzB;IACD,8CAA8C;IAC9C,GAAG,EAAE,eAAe,CAAA;IACpB,mCAAmC;IACnC,aAAa,EAAE,CAAC,OAAO,EAAE,eAAe,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,KAAK,IAAI,CAAA;IAC/E,iCAAiC;IACjC,KAAK,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;CAgE3B,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,eAAO,MAAM,uBAAuB,GAAI,CAAC,EACvC,QAAQ,MAAM,EACd,QAAQ,aAAa,EACrB,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,EACrB,UAAU,aAAa,CAAC,CAAC,CAAC,KACzB;IAAE,KAAK,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;CAQ9B,CAAA"}
package/dist/ws.js DELETED
@@ -1,137 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.attachWebSocketToServer = exports.createGraphQLWSServer = exports.toEffectWebSocket = void 0;
4
- const effect_1 = require("effect");
5
- const ws_1 = require("ws");
6
- const core_1 = require("@effect-gql/core");
7
- /**
8
- * Convert a Node.js WebSocket (from 'ws' library) to an EffectWebSocket.
9
- *
10
- * This creates an Effect-based wrapper around the ws WebSocket instance,
11
- * providing a Stream for incoming messages and Effect-based send/close operations.
12
- *
13
- * @param ws - The WebSocket instance from the 'ws' library
14
- * @returns An EffectWebSocket that can be used with makeGraphQLWSHandler
15
- *
16
- * @example
17
- * ```typescript
18
- * wss.on("connection", (ws, req) => {
19
- * const effectSocket = toEffectWebSocket(ws)
20
- * Effect.runPromise(handler(effectSocket))
21
- * })
22
- * ```
23
- */
24
- const toEffectWebSocket = (ws) => (0, core_1.toEffectWebSocketFromWs)(ws);
25
- exports.toEffectWebSocket = toEffectWebSocket;
26
- /**
27
- * Create a WebSocket server that handles GraphQL subscriptions.
28
- *
29
- * This function creates a WebSocketServer and returns utilities for
30
- * integrating it with an HTTP server via the upgrade event.
31
- *
32
- * @param schema - The GraphQL schema with subscription definitions
33
- * @param layer - Effect layer providing services required by resolvers
34
- * @param options - Optional configuration and lifecycle hooks
35
- * @returns Object containing the WebSocketServer and handlers
36
- *
37
- * @example
38
- * ```typescript
39
- * const httpServer = createServer(requestHandler)
40
- * const { wss, handleUpgrade } = createGraphQLWSServer(schema, serviceLayer)
41
- *
42
- * httpServer.on("upgrade", (request, socket, head) => {
43
- * if (request.url === "/graphql") {
44
- * handleUpgrade(request, socket, head)
45
- * }
46
- * })
47
- *
48
- * httpServer.listen(4000)
49
- * ```
50
- */
51
- const createGraphQLWSServer = (schema, layer, options) => {
52
- const wss = new ws_1.WebSocketServer({ noServer: true });
53
- const path = options?.path ?? "/graphql";
54
- // Create the handler from core
55
- const handler = (0, core_1.makeGraphQLWSHandler)(schema, layer, options);
56
- // Track active connections for cleanup
57
- const activeConnections = new Set();
58
- wss.on("connection", (ws, _request) => {
59
- activeConnections.add(ws);
60
- const effectSocket = (0, exports.toEffectWebSocket)(ws);
61
- // Run the handler
62
- effect_1.Effect.runPromise(handler(effectSocket).pipe(effect_1.Effect.catchAll((error) => effect_1.Effect.logError("GraphQL WebSocket handler error", error)))).finally(() => {
63
- activeConnections.delete(ws);
64
- });
65
- });
66
- const handleUpgrade = (request, socket, head) => {
67
- // Check if this is the GraphQL WebSocket path
68
- const url = new URL(request.url ?? "/", `http://${request.headers.host}`);
69
- if (url.pathname !== path) {
70
- socket.destroy();
71
- return;
72
- }
73
- // Check for correct WebSocket subprotocol
74
- const protocol = request.headers["sec-websocket-protocol"];
75
- if (!protocol?.includes("graphql-transport-ws")) {
76
- socket.write("HTTP/1.1 400 Bad Request\r\n\r\n");
77
- socket.destroy();
78
- return;
79
- }
80
- wss.handleUpgrade(request, socket, head, (ws) => {
81
- wss.emit("connection", ws, request);
82
- });
83
- };
84
- const close = async () => {
85
- // Close all active connections
86
- for (const ws of activeConnections) {
87
- ws.close(1001, "Server shutting down");
88
- }
89
- activeConnections.clear();
90
- // Close the WebSocket server
91
- return new Promise((resolve, reject) => {
92
- wss.close((error) => {
93
- if (error)
94
- reject(error);
95
- else
96
- resolve();
97
- });
98
- });
99
- };
100
- return { wss, handleUpgrade, close };
101
- };
102
- exports.createGraphQLWSServer = createGraphQLWSServer;
103
- /**
104
- * Attach WebSocket subscription support to an existing HTTP server.
105
- *
106
- * This is a convenience function that creates a GraphQL WebSocket server
107
- * and attaches it to an HTTP server's upgrade event.
108
- *
109
- * @param server - The HTTP server to attach to
110
- * @param schema - The GraphQL schema with subscription definitions
111
- * @param layer - Effect layer providing services required by resolvers
112
- * @param options - Optional configuration and lifecycle hooks
113
- * @returns Cleanup function to close the WebSocket server
114
- *
115
- * @example
116
- * ```typescript
117
- * const httpServer = createServer(requestHandler)
118
- *
119
- * const cleanup = attachWebSocketToServer(httpServer, schema, serviceLayer, {
120
- * path: "/graphql",
121
- * })
122
- *
123
- * httpServer.listen(4000)
124
- *
125
- * // Later, to cleanup:
126
- * await cleanup()
127
- * ```
128
- */
129
- const attachWebSocketToServer = (server, schema, layer, options) => {
130
- const { handleUpgrade, close } = (0, exports.createGraphQLWSServer)(schema, layer, options);
131
- server.on("upgrade", (request, socket, head) => {
132
- handleUpgrade(request, socket, head);
133
- });
134
- return { close };
135
- };
136
- exports.attachWebSocketToServer = attachWebSocketToServer;
137
- //# sourceMappingURL=ws.js.map
package/dist/ws.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"ws.js","sourceRoot":"","sources":["../src/ws.ts"],"names":[],"mappings":";;;AAAA,mCAAsC;AAGtC,2BAA+C;AAE/C,2CAKyB;AAazB;;;;;;;;;;;;;;;;GAgBG;AACI,MAAM,iBAAiB,GAAG,CAAC,EAAa,EAAmB,EAAE,CAAC,IAAA,8BAAuB,EAAC,EAAE,CAAC,CAAA;AAAnF,QAAA,iBAAiB,qBAAkE;AAEhG;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACI,MAAM,qBAAqB,GAAG,CACnC,MAAqB,EACrB,KAAqB,EACrB,OAA0B,EAQ1B,EAAE;IACF,MAAM,GAAG,GAAG,IAAI,oBAAe,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAA;IACnD,MAAM,IAAI,GAAG,OAAO,EAAE,IAAI,IAAI,UAAU,CAAA;IAExC,+BAA+B;IAC/B,MAAM,OAAO,GAAG,IAAA,2BAAoB,EAAC,MAAM,EAAE,KAAK,EAAE,OAAO,CAAC,CAAA;IAE5D,uCAAuC;IACvC,MAAM,iBAAiB,GAAG,IAAI,GAAG,EAAa,CAAA;IAE9C,GAAG,CAAC,EAAE,CAAC,YAAY,EAAE,CAAC,EAAE,EAAE,QAAQ,EAAE,EAAE;QACpC,iBAAiB,CAAC,GAAG,CAAC,EAAE,CAAC,CAAA;QAEzB,MAAM,YAAY,GAAG,IAAA,yBAAiB,EAAC,EAAE,CAAC,CAAA;QAE1C,kBAAkB;QAClB,eAAM,CAAC,UAAU,CACf,OAAO,CAAC,YAAY,CAAC,CAAC,IAAI,CACxB,eAAM,CAAC,QAAQ,CAAC,CAAC,KAAK,EAAE,EAAE,CA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zB,6BAA6B;QAC7B,OAAO,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YAC3C,GAAG,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;gBAClB,IAAI,KAAK;oBAAE,MAAM,CAAC,KAAK,CAAC,CAAA;;oBACnB,OAAO,EAAE,CAAA;YAChB,CAAC,CAAC,CAAA;QACJ,CAAC,CAAC,CAAA;IACJ,CAAC,CAAA;IAED,OAAO,EAAE,GAAG,EAAE,aAAa,EAAE,KAAK,EAAE,CAAA;AACtC,CAAC,CAAA;AA1EY,QAAA,qBAAqB,yBA0EjC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACI,MAAM,uBAAuB,GAAG,CACrC,MAAc,EACd,MAAqB,EACrB,KAAqB,EACrB,OAA0B,EACM,EAAE;IAClC,MAAM,EAAE,aAAa,EAAE,KAAK,EAAE,GAAG,IAAA,6BAAqB,EAAC,MAAM,EAAE,KAAK,EAAE,OAAO,CAAC,CAAA;IAE9E,MAAM,CAAC,EAAE,CAAC,SAAS,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE;QAC7C,aAAa,CAAC,OAAO,EAAE,MAAgB,EAAE,IAAI,CAAC,CAAA;IAChD,CAAC,CAAC,CAAA;IAEF,OAAO,EAAE,KAAK,EAAE,CAAA;AAClB,CAAC,CAAA;AAbY,QAAA,uBAAuB,2BAanC"}
package/src/http-utils.ts DELETED
@@ -1,32 +0,0 @@
1
- import type { IncomingHttpHeaders } from "node:http"
2
-
3
- /**
4
- * Convert Node.js IncomingHttpHeaders to web standard Headers.
5
- *
6
- * This handles the difference between Node.js headers (which can be
7
- * string | string[] | undefined) and web Headers (which are always strings).
8
- *
9
- * @param nodeHeaders - Headers from IncomingMessage.headers
10
- * @returns A web standard Headers object
11
- *
12
- * @example
13
- * ```typescript
14
- * import { toWebHeaders } from "@effect-gql/node"
15
- *
16
- * const webHeaders = toWebHeaders(req.headers)
17
- * const auth = webHeaders.get("authorization")
18
- * ```
19
- */
20
- export const toWebHeaders = (nodeHeaders: IncomingHttpHeaders): Headers => {
21
- const headers = new Headers()
22
- for (const [key, value] of Object.entries(nodeHeaders)) {
23
- if (value) {
24
- if (Array.isArray(value)) {
25
- value.forEach((v) => headers.append(key, v))
26
- } else {
27
- headers.set(key, value)
28
- }
29
- }
30
- }
31
- return headers
32
- }
package/src/index.ts DELETED
@@ -1,15 +0,0 @@
1
- export { serve, type ServeOptions } from "./serve"
2
-
3
- // HTTP utilities
4
- export { toWebHeaders } from "./http-utils"
5
-
6
- // WebSocket subscription support
7
- export {
8
- toEffectWebSocket,
9
- createGraphQLWSServer,
10
- attachWebSocketToServer,
11
- type NodeWSOptions,
12
- } from "./ws"
13
-
14
- // SSE (Server-Sent Events) subscription support
15
- export { createSSEHandler, createSSEServer, type NodeSSEOptions } from "./sse"