@effect-gql/express 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.
- package/README.md +100 -0
- package/index.cjs +252 -0
- package/index.cjs.map +1 -0
- package/index.d.cts +184 -0
- package/index.d.ts +184 -0
- package/index.js +247 -0
- package/index.js.map +1 -0
- package/package.json +14 -31
- package/dist/http-utils.d.ts +0 -12
- package/dist/http-utils.d.ts.map +0 -1
- package/dist/http-utils.js +0 -28
- package/dist/http-utils.js.map +0 -1
- package/dist/index.d.ts +0 -4
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -13
- package/dist/index.js.map +0 -1
- package/dist/middleware.d.ts +0 -30
- package/dist/middleware.d.ts.map +0 -1
- package/dist/middleware.js +0 -61
- package/dist/middleware.js.map +0 -1
- package/dist/sse.d.ts +0 -84
- package/dist/sse.d.ts.map +0 -1
- package/dist/sse.js +0 -217
- package/dist/sse.js.map +0 -1
- package/dist/ws.d.ts +0 -73
- package/dist/ws.d.ts.map +0 -1
- package/dist/ws.js +0 -118
- package/dist/ws.js.map +0 -1
- package/src/http-utils.ts +0 -24
- package/src/index.ts +0 -7
- package/src/middleware.ts +0 -65
- package/src/sse.ts +0 -285
- package/src/ws.ts +0 -152
package/dist/middleware.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAA;AAC9B,OAAO,EAAW,UAAU,EAAE,MAAM,kBAAkB,CAAA;AACtD,OAAO,KAAK,EAAmC,cAAc,EAAE,MAAM,SAAS,CAAA;AAG9E;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,eAAO,MAAM,YAAY,GAAI,CAAC,EAAE,CAAC,EAAE,EAAE,EACnC,QAAQ,UAAU,CAAC,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,EACnC,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,KACxB,cA+BF,CAAA"}
|
package/dist/middleware.js
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.toMiddleware = void 0;
|
|
4
|
-
const platform_1 = require("@effect/platform");
|
|
5
|
-
const http_utils_1 = require("./http-utils");
|
|
6
|
-
/**
|
|
7
|
-
* Convert an HttpRouter to Express middleware.
|
|
8
|
-
*
|
|
9
|
-
* This creates Express-compatible middleware that can be mounted on any Express app.
|
|
10
|
-
* The middleware converts Express requests to web standard Requests, processes them
|
|
11
|
-
* through the Effect router, and writes the response back to Express.
|
|
12
|
-
*
|
|
13
|
-
* @param router - The HttpRouter to convert (typically from makeGraphQLRouter or toRouter)
|
|
14
|
-
* @param layer - Layer providing any services required by the router
|
|
15
|
-
* @returns Express middleware function
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
* ```typescript
|
|
19
|
-
* import express from "express"
|
|
20
|
-
* import { makeGraphQLRouter } from "@effect-gql/core"
|
|
21
|
-
* import { toMiddleware } from "@effect-gql/express"
|
|
22
|
-
* import { Layer } from "effect"
|
|
23
|
-
*
|
|
24
|
-
* const router = makeGraphQLRouter(schema, Layer.empty, { graphiql: true })
|
|
25
|
-
*
|
|
26
|
-
* const app = express()
|
|
27
|
-
* app.use(toMiddleware(router, Layer.empty))
|
|
28
|
-
* app.listen(4000, () => console.log("Server running on http://localhost:4000"))
|
|
29
|
-
* ```
|
|
30
|
-
*/
|
|
31
|
-
const toMiddleware = (router, layer) => {
|
|
32
|
-
const { handler } = platform_1.HttpApp.toWebHandlerLayer(router, layer);
|
|
33
|
-
return async (req, res, next) => {
|
|
34
|
-
try {
|
|
35
|
-
// Convert Express request to web standard Request
|
|
36
|
-
// Use URL constructor for safe URL parsing (avoids Host header injection)
|
|
37
|
-
const baseUrl = `${req.protocol}://${req.hostname}`;
|
|
38
|
-
const url = new URL(req.originalUrl || "/", baseUrl).href;
|
|
39
|
-
const headers = (0, http_utils_1.toWebHeaders)(req.headers);
|
|
40
|
-
const webRequest = new Request(url, {
|
|
41
|
-
method: req.method,
|
|
42
|
-
headers,
|
|
43
|
-
body: ["GET", "HEAD"].includes(req.method) ? undefined : JSON.stringify(req.body),
|
|
44
|
-
});
|
|
45
|
-
// Process through Effect handler
|
|
46
|
-
const webResponse = await handler(webRequest);
|
|
47
|
-
// Write response back to Express
|
|
48
|
-
res.status(webResponse.status);
|
|
49
|
-
webResponse.headers.forEach((value, key) => {
|
|
50
|
-
res.setHeader(key, value);
|
|
51
|
-
});
|
|
52
|
-
const body = await webResponse.text();
|
|
53
|
-
res.send(body);
|
|
54
|
-
}
|
|
55
|
-
catch (error) {
|
|
56
|
-
next(error);
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
};
|
|
60
|
-
exports.toMiddleware = toMiddleware;
|
|
61
|
-
//# sourceMappingURL=middleware.js.map
|
package/dist/middleware.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.js","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":";;;AACA,+CAAsD;AAEtD,6CAA2C;AAE3C;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACI,MAAM,YAAY,GAAG,CAC1B,MAAmC,EACnC,KAAyB,EACT,EAAE;IAClB,MAAM,EAAE,OAAO,EAAE,GAAG,kBAAO,CAAC,iBAAiB,CAAC,MAAM,EAAE,KAAK,CAAC,CAAA;IAE5D,OAAO,KAAK,EAAE,GAAY,EAAE,GAAa,EAAE,IAAkB,EAAE,EAAE;QAC/D,IAAI,CAAC;YACH,kDAAkD;YAClD,0EAA0E;YAC1E,MAAM,OAAO,GAAG,GAAG,GAAG,CAAC,QAAQ,MAAM,GAAG,CAAC,QAAQ,EAAE,CAAA;YACnD,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,WAAW,IAAI,GAAG,EAAE,OAAO,CAAC,CAAC,IAAI,CAAA;YACzD,MAAM,OAAO,GAAG,IAAA,yBAAY,EAAC,GAAG,CAAC,OAAO,CAAC,CAAA;YAEzC,MAAM,UAAU,GAAG,IAAI,OAAO,CAAC,GAAG,EAAE;gBAClC,MAAM,EAAE,GAAG,CAAC,MAAM;gBAClB,OAAO;gBACP,IAAI,EAAE,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC;aAClF,CAAC,CAAA;YAEF,iCAAiC;YACjC,MAAM,WAAW,GAAG,MAAM,OAAO,CAAC,UAAU,CAAC,CAAA;YAE7C,iCAAiC;YACjC,GAAG,CAAC,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,CAAA;YAC9B,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE;gBACzC,GAAG,CAAC,SAAS,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;YAC3B,CAAC,CAAC,CAAA;YACF,MAAM,IAAI,GAAG,MAAM,WAAW,CAAC,IAAI,EAAE,CAAA;YACrC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QAChB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,KAAK,CAAC,CAAA;QACb,CAAC;IACH,CAAC,CAAA;AACH,CAAC,CAAA;AAlCY,QAAA,YAAY,gBAkCxB"}
|
package/dist/sse.d.ts
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import { Layer } from "effect";
|
|
2
|
-
import type { RequestHandler } from "express";
|
|
3
|
-
import { GraphQLSchema } from "graphql";
|
|
4
|
-
import { type GraphQLSSEOptions } from "@effect-gql/core";
|
|
5
|
-
/**
|
|
6
|
-
* Options for Express SSE middleware
|
|
7
|
-
*/
|
|
8
|
-
export interface ExpressSSEOptions<R> extends GraphQLSSEOptions<R> {
|
|
9
|
-
/**
|
|
10
|
-
* Path for SSE connections.
|
|
11
|
-
* @default "/graphql/stream"
|
|
12
|
-
*/
|
|
13
|
-
readonly path?: string;
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Create an Express middleware for SSE subscriptions.
|
|
17
|
-
*
|
|
18
|
-
* This middleware handles POST requests to the configured path and streams
|
|
19
|
-
* GraphQL subscription events as Server-Sent Events.
|
|
20
|
-
*
|
|
21
|
-
* @param schema - The GraphQL schema with subscription definitions
|
|
22
|
-
* @param layer - Effect layer providing services required by resolvers
|
|
23
|
-
* @param options - Optional lifecycle hooks and configuration
|
|
24
|
-
* @returns An Express middleware function
|
|
25
|
-
*
|
|
26
|
-
* @example
|
|
27
|
-
* ```typescript
|
|
28
|
-
* import express from "express"
|
|
29
|
-
* import { createServer } from "node:http"
|
|
30
|
-
* import { toMiddleware, sseMiddleware, attachWebSocket } from "@effect-gql/express"
|
|
31
|
-
* import { makeGraphQLRouter } from "@effect-gql/core"
|
|
32
|
-
*
|
|
33
|
-
* const app = express()
|
|
34
|
-
* app.use(express.json())
|
|
35
|
-
*
|
|
36
|
-
* // Regular GraphQL endpoint
|
|
37
|
-
* const router = makeGraphQLRouter(schema, Layer.empty, { graphiql: true })
|
|
38
|
-
* app.use(toMiddleware(router, Layer.empty))
|
|
39
|
-
*
|
|
40
|
-
* // SSE subscriptions endpoint
|
|
41
|
-
* app.use(sseMiddleware(schema, Layer.empty, {
|
|
42
|
-
* path: "/graphql/stream",
|
|
43
|
-
* onConnect: (request, headers) => Effect.gen(function* () {
|
|
44
|
-
* const token = headers.get("authorization")
|
|
45
|
-
* const user = yield* AuthService.validateToken(token)
|
|
46
|
-
* return { user }
|
|
47
|
-
* }),
|
|
48
|
-
* }))
|
|
49
|
-
*
|
|
50
|
-
* const server = createServer(app)
|
|
51
|
-
*
|
|
52
|
-
* // Optional: Also attach WebSocket subscriptions
|
|
53
|
-
* attachWebSocket(server, schema, Layer.empty)
|
|
54
|
-
*
|
|
55
|
-
* server.listen(4000)
|
|
56
|
-
* ```
|
|
57
|
-
*/
|
|
58
|
-
export declare const sseMiddleware: <R>(schema: GraphQLSchema, layer: Layer.Layer<R>, options?: ExpressSSEOptions<R>) => RequestHandler;
|
|
59
|
-
/**
|
|
60
|
-
* Create a standalone Express route handler for SSE subscriptions.
|
|
61
|
-
*
|
|
62
|
-
* Use this if you want more control over routing than the middleware provides.
|
|
63
|
-
*
|
|
64
|
-
* @param schema - The GraphQL schema with subscription definitions
|
|
65
|
-
* @param layer - Effect layer providing services required by resolvers
|
|
66
|
-
* @param options - Optional lifecycle hooks and configuration
|
|
67
|
-
* @returns An Express request handler
|
|
68
|
-
*
|
|
69
|
-
* @example
|
|
70
|
-
* ```typescript
|
|
71
|
-
* import express from "express"
|
|
72
|
-
* import { createSSEHandler } from "@effect-gql/express"
|
|
73
|
-
*
|
|
74
|
-
* const app = express()
|
|
75
|
-
* app.use(express.json())
|
|
76
|
-
*
|
|
77
|
-
* const sseHandler = createSSEHandler(schema, Layer.empty)
|
|
78
|
-
* app.post("/graphql/stream", sseHandler)
|
|
79
|
-
*
|
|
80
|
-
* app.listen(4000)
|
|
81
|
-
* ```
|
|
82
|
-
*/
|
|
83
|
-
export declare const createSSEHandler: <R>(schema: GraphQLSchema, layer: Layer.Layer<R>, options?: Omit<ExpressSSEOptions<R>, "path">) => RequestHandler;
|
|
84
|
-
//# sourceMappingURL=sse.d.ts.map
|
package/dist/sse.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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,EAAmC,cAAc,EAAE,MAAM,SAAS,CAAA;AAC9E,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AACvC,OAAO,EAIL,KAAK,iBAAiB,EAGvB,MAAM,kBAAkB,CAAA;AAGzB;;GAEG;AACH,MAAM,WAAW,iBAAiB,CAAC,CAAC,CAAE,SAAQ,iBAAiB,CAAC,CAAC,CAAC;IAChE;;;OAGG;IACH,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CACvB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,eAAO,MAAM,aAAa,GAAI,CAAC,EAC7B,QAAQ,aAAa,EACrB,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,EACrB,UAAU,iBAAiB,CAAC,CAAC,CAAC,KAC7B,cAkGF,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,eAAO,MAAM,gBAAgB,GAAI,CAAC,EAChC,QAAQ,aAAa,EACrB,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,EACrB,UAAU,IAAI,CAAC,iBAAiB,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,KAC3C,cAqFF,CAAA"}
|
package/dist/sse.js
DELETED
|
@@ -1,217 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createSSEHandler = exports.sseMiddleware = 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 Express middleware for SSE subscriptions.
|
|
9
|
-
*
|
|
10
|
-
* This middleware handles POST requests to the configured path and streams
|
|
11
|
-
* GraphQL subscription events as Server-Sent Events.
|
|
12
|
-
*
|
|
13
|
-
* @param schema - The GraphQL schema with subscription definitions
|
|
14
|
-
* @param layer - Effect layer providing services required by resolvers
|
|
15
|
-
* @param options - Optional lifecycle hooks and configuration
|
|
16
|
-
* @returns An Express middleware function
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* ```typescript
|
|
20
|
-
* import express from "express"
|
|
21
|
-
* import { createServer } from "node:http"
|
|
22
|
-
* import { toMiddleware, sseMiddleware, attachWebSocket } from "@effect-gql/express"
|
|
23
|
-
* import { makeGraphQLRouter } from "@effect-gql/core"
|
|
24
|
-
*
|
|
25
|
-
* const app = express()
|
|
26
|
-
* app.use(express.json())
|
|
27
|
-
*
|
|
28
|
-
* // Regular GraphQL endpoint
|
|
29
|
-
* const router = makeGraphQLRouter(schema, Layer.empty, { graphiql: true })
|
|
30
|
-
* app.use(toMiddleware(router, Layer.empty))
|
|
31
|
-
*
|
|
32
|
-
* // SSE subscriptions endpoint
|
|
33
|
-
* app.use(sseMiddleware(schema, Layer.empty, {
|
|
34
|
-
* path: "/graphql/stream",
|
|
35
|
-
* onConnect: (request, headers) => Effect.gen(function* () {
|
|
36
|
-
* const token = headers.get("authorization")
|
|
37
|
-
* const user = yield* AuthService.validateToken(token)
|
|
38
|
-
* return { user }
|
|
39
|
-
* }),
|
|
40
|
-
* }))
|
|
41
|
-
*
|
|
42
|
-
* const server = createServer(app)
|
|
43
|
-
*
|
|
44
|
-
* // Optional: Also attach WebSocket subscriptions
|
|
45
|
-
* attachWebSocket(server, schema, Layer.empty)
|
|
46
|
-
*
|
|
47
|
-
* server.listen(4000)
|
|
48
|
-
* ```
|
|
49
|
-
*/
|
|
50
|
-
const sseMiddleware = (schema, layer, options) => {
|
|
51
|
-
const path = options?.path ?? "/graphql/stream";
|
|
52
|
-
const sseHandler = (0, core_1.makeGraphQLSSEHandler)(schema, layer, options);
|
|
53
|
-
return async (req, res, next) => {
|
|
54
|
-
// Check if this request is for our path
|
|
55
|
-
if (req.path !== path) {
|
|
56
|
-
next();
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
// Only handle POST requests
|
|
60
|
-
if (req.method !== "POST") {
|
|
61
|
-
next();
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
// Check Accept header for SSE support
|
|
65
|
-
const accept = req.headers.accept ?? "";
|
|
66
|
-
if (!accept.includes("text/event-stream") && !accept.includes("*/*")) {
|
|
67
|
-
res.status(406).json({
|
|
68
|
-
errors: [{ message: "Client must accept text/event-stream" }],
|
|
69
|
-
});
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
// Parse the GraphQL request from the body
|
|
73
|
-
let subscriptionRequest;
|
|
74
|
-
try {
|
|
75
|
-
const body = req.body;
|
|
76
|
-
if (typeof body.query !== "string") {
|
|
77
|
-
throw new Error("Missing query");
|
|
78
|
-
}
|
|
79
|
-
subscriptionRequest = {
|
|
80
|
-
query: body.query,
|
|
81
|
-
variables: body.variables,
|
|
82
|
-
operationName: body.operationName,
|
|
83
|
-
extensions: body.extensions,
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
catch {
|
|
87
|
-
res.status(400).json({
|
|
88
|
-
errors: [{ message: "Invalid GraphQL request body" }],
|
|
89
|
-
});
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
// Convert Express 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(subscriptionRequest, 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.sseMiddleware = sseMiddleware;
|
|
127
|
-
/**
|
|
128
|
-
* Create a standalone Express route handler for SSE subscriptions.
|
|
129
|
-
*
|
|
130
|
-
* Use this if you want more control over routing than the middleware provides.
|
|
131
|
-
*
|
|
132
|
-
* @param schema - The GraphQL schema with subscription definitions
|
|
133
|
-
* @param layer - Effect layer providing services required by resolvers
|
|
134
|
-
* @param options - Optional lifecycle hooks and configuration
|
|
135
|
-
* @returns An Express request handler
|
|
136
|
-
*
|
|
137
|
-
* @example
|
|
138
|
-
* ```typescript
|
|
139
|
-
* import express from "express"
|
|
140
|
-
* import { createSSEHandler } from "@effect-gql/express"
|
|
141
|
-
*
|
|
142
|
-
* const app = express()
|
|
143
|
-
* app.use(express.json())
|
|
144
|
-
*
|
|
145
|
-
* const sseHandler = createSSEHandler(schema, Layer.empty)
|
|
146
|
-
* app.post("/graphql/stream", sseHandler)
|
|
147
|
-
*
|
|
148
|
-
* app.listen(4000)
|
|
149
|
-
* ```
|
|
150
|
-
*/
|
|
151
|
-
const createSSEHandler = (schema, layer, options) => {
|
|
152
|
-
const sseHandler = (0, core_1.makeGraphQLSSEHandler)(schema, layer, options);
|
|
153
|
-
return async (req, res, _next) => {
|
|
154
|
-
// Check Accept header for SSE support
|
|
155
|
-
const accept = req.headers.accept ?? "";
|
|
156
|
-
if (!accept.includes("text/event-stream") && !accept.includes("*/*")) {
|
|
157
|
-
res.status(406).json({
|
|
158
|
-
errors: [{ message: "Client must accept text/event-stream" }],
|
|
159
|
-
});
|
|
160
|
-
return;
|
|
161
|
-
}
|
|
162
|
-
// Parse the GraphQL request from the body
|
|
163
|
-
let subscriptionRequest;
|
|
164
|
-
try {
|
|
165
|
-
const body = req.body;
|
|
166
|
-
if (typeof body.query !== "string") {
|
|
167
|
-
throw new Error("Missing query");
|
|
168
|
-
}
|
|
169
|
-
subscriptionRequest = {
|
|
170
|
-
query: body.query,
|
|
171
|
-
variables: body.variables,
|
|
172
|
-
operationName: body.operationName,
|
|
173
|
-
extensions: body.extensions,
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
|
-
catch {
|
|
177
|
-
res.status(400).json({
|
|
178
|
-
errors: [{ message: "Invalid GraphQL request body" }],
|
|
179
|
-
});
|
|
180
|
-
return;
|
|
181
|
-
}
|
|
182
|
-
// Convert Express headers to web Headers
|
|
183
|
-
const headers = (0, http_utils_1.toWebHeaders)(req.headers);
|
|
184
|
-
// Set SSE headers
|
|
185
|
-
res.writeHead(200, core_1.SSE_HEADERS);
|
|
186
|
-
// Get the event stream
|
|
187
|
-
const eventStream = sseHandler(subscriptionRequest, headers);
|
|
188
|
-
// Create the streaming effect
|
|
189
|
-
const streamEffect = effect_1.Effect.gen(function* () {
|
|
190
|
-
// Track client disconnection
|
|
191
|
-
const clientDisconnected = yield* effect_1.Deferred.make();
|
|
192
|
-
req.on("close", () => {
|
|
193
|
-
effect_1.Effect.runPromise(effect_1.Deferred.succeed(clientDisconnected, undefined)).catch(() => { });
|
|
194
|
-
});
|
|
195
|
-
req.on("error", (error) => {
|
|
196
|
-
effect_1.Effect.runPromise(effect_1.Deferred.fail(clientDisconnected, new core_1.SSEError({ cause: error }))).catch(() => { });
|
|
197
|
-
});
|
|
198
|
-
// Stream events to the client
|
|
199
|
-
const runStream = effect_1.Stream.runForEach(eventStream, (event) => effect_1.Effect.async((resume) => {
|
|
200
|
-
const message = (0, core_1.formatSSEMessage)(event);
|
|
201
|
-
res.write(message, (error) => {
|
|
202
|
-
if (error) {
|
|
203
|
-
resume(effect_1.Effect.fail(new core_1.SSEError({ cause: error })));
|
|
204
|
-
}
|
|
205
|
-
else {
|
|
206
|
-
resume(effect_1.Effect.succeed(undefined));
|
|
207
|
-
}
|
|
208
|
-
});
|
|
209
|
-
}));
|
|
210
|
-
// Race between stream completion and client disconnection
|
|
211
|
-
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));
|
|
212
|
-
});
|
|
213
|
-
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)));
|
|
214
|
-
};
|
|
215
|
-
};
|
|
216
|
-
exports.createSSEHandler = createSSEHandler;
|
|
217
|
-
//# 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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACI,MAAM,aAAa,GAAG,CAC3B,MAAqB,EACrB,KAAqB,EACrB,OAA8B,EACd,EAAE;IAClB,MAAM,IAAI,GAAG,OAAO,EAAE,IAAI,IAAI,iBAAiB,CAAA;IAC/C,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AAS,CAAC,CAAC,CAAA;oBACnC,CAAC;gBACH,CAAC,CAAC,CAAA;YACJ,CAAC,CAAC,CACH,CAAA;YAED,0DAA0D;YAC1D,KAAK,CAAC,CAAC,eAAM,CAAC,IAAI,CAChB,SAAS,CAAC,IAAI,CAAC,eAAM,CAAC,QAAQ,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,eAAM,CAAC,UAAU,CAAC,kBAAkB,EAAE,KAAK,CAAC,CAAC,CAAC,EACxF,iBAAQ,CAAC,KAAK,CAAC,kBAAkB,CAAC,CACnC,CAAA;QACH,CAAC,CAAC,CAAA;QAEF,MAAM,eAAM,CAAC,UAAU,CACrB,YAAY,CAAC,IAAI,CACf,eAAM,CAAC,QAAQ,CAAC,eAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,EAC7C,eAAM,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAC,eAAM,CAAC,IAAI,CAAC,CACnC,CACF,CAAA;IACH,CAAC,CAAA;AACH,CAAC,CAAA;AAzFY,QAAA,gBAAgB,oBAyF5B"}
|
package/dist/ws.d.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { Layer } from "effect";
|
|
2
|
-
import type { Server } from "node:http";
|
|
3
|
-
import { GraphQLSchema } from "graphql";
|
|
4
|
-
import { type GraphQLWSOptions } from "@effect-gql/core";
|
|
5
|
-
/**
|
|
6
|
-
* Options for Express WebSocket server
|
|
7
|
-
*/
|
|
8
|
-
export interface ExpressWSOptions<R> extends GraphQLWSOptions<R> {
|
|
9
|
-
/**
|
|
10
|
-
* Path for WebSocket connections.
|
|
11
|
-
* @default "/graphql"
|
|
12
|
-
*/
|
|
13
|
-
readonly path?: string;
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Attach WebSocket subscription support to an Express HTTP server.
|
|
17
|
-
*
|
|
18
|
-
* Since Express middleware doesn't own the HTTP server, this function
|
|
19
|
-
* must be called separately with the HTTP server instance to enable
|
|
20
|
-
* WebSocket subscriptions.
|
|
21
|
-
*
|
|
22
|
-
* @param server - The HTTP server running the Express app
|
|
23
|
-
* @param schema - The GraphQL schema with subscription definitions
|
|
24
|
-
* @param layer - Effect layer providing services required by resolvers
|
|
25
|
-
* @param options - Optional configuration and lifecycle hooks
|
|
26
|
-
* @returns Object with cleanup function
|
|
27
|
-
*
|
|
28
|
-
* @example
|
|
29
|
-
* ```typescript
|
|
30
|
-
* import express from "express"
|
|
31
|
-
* import { createServer } from "node:http"
|
|
32
|
-
* import { toMiddleware, attachWebSocket } from "@effect-gql/express"
|
|
33
|
-
* import { makeGraphQLRouter, GraphQLSchemaBuilder } from "@effect-gql/core"
|
|
34
|
-
* import { Layer, Effect, Stream } from "effect"
|
|
35
|
-
* import * as S from "effect/Schema"
|
|
36
|
-
*
|
|
37
|
-
* // Build schema with subscriptions
|
|
38
|
-
* const schema = GraphQLSchemaBuilder.empty
|
|
39
|
-
* .query("hello", { type: S.String, resolve: () => Effect.succeed("world") })
|
|
40
|
-
* .subscription("counter", {
|
|
41
|
-
* type: S.Int,
|
|
42
|
-
* subscribe: () => Effect.succeed(Stream.fromIterable([1, 2, 3]))
|
|
43
|
-
* })
|
|
44
|
-
* .buildSchema()
|
|
45
|
-
*
|
|
46
|
-
* // Create Express app with middleware
|
|
47
|
-
* const app = express()
|
|
48
|
-
* app.use(express.json())
|
|
49
|
-
* const router = makeGraphQLRouter(schema, Layer.empty, { graphiql: true })
|
|
50
|
-
* app.use(toMiddleware(router, Layer.empty))
|
|
51
|
-
*
|
|
52
|
-
* // Create HTTP server and attach WebSocket support
|
|
53
|
-
* const server = createServer(app)
|
|
54
|
-
* const ws = attachWebSocket(server, schema, Layer.empty, {
|
|
55
|
-
* path: "/graphql"
|
|
56
|
-
* })
|
|
57
|
-
*
|
|
58
|
-
* server.listen(4000, () => {
|
|
59
|
-
* console.log("Server running on http://localhost:4000")
|
|
60
|
-
* console.log("WebSocket subscriptions available at ws://localhost:4000/graphql")
|
|
61
|
-
* })
|
|
62
|
-
*
|
|
63
|
-
* // Cleanup on shutdown
|
|
64
|
-
* process.on("SIGINT", async () => {
|
|
65
|
-
* await ws.close()
|
|
66
|
-
* server.close()
|
|
67
|
-
* })
|
|
68
|
-
* ```
|
|
69
|
-
*/
|
|
70
|
-
export declare const attachWebSocket: <R>(server: Server, schema: GraphQLSchema, layer: Layer.Layer<R>, options?: ExpressWSOptions<R>) => {
|
|
71
|
-
close: () => Promise<void>;
|
|
72
|
-
};
|
|
73
|
-
//# 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,MAAM,EAAE,MAAM,WAAW,CAAA;AAIvC,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AACvC,OAAO,EAGL,KAAK,gBAAgB,EACtB,MAAM,kBAAkB,CAAA;AAEzB;;GAEG;AACH,MAAM,WAAW,gBAAgB,CAAC,CAAC,CAAE,SAAQ,gBAAgB,CAAC,CAAC,CAAC;IAC9D;;;OAGG;IACH,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CACvB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsDG;AACH,eAAO,MAAM,eAAe,GAAI,CAAC,EAC/B,QAAQ,MAAM,EACd,QAAQ,aAAa,EACrB,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,EACrB,UAAU,gBAAgB,CAAC,CAAC,CAAC,KAC5B;IAAE,KAAK,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;CAoE9B,CAAA"}
|
package/dist/ws.js
DELETED
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.attachWebSocket = void 0;
|
|
4
|
-
const effect_1 = require("effect");
|
|
5
|
-
const ws_1 = require("ws");
|
|
6
|
-
const core_1 = require("@effect-gql/core");
|
|
7
|
-
/**
|
|
8
|
-
* Attach WebSocket subscription support to an Express HTTP server.
|
|
9
|
-
*
|
|
10
|
-
* Since Express middleware doesn't own the HTTP server, this function
|
|
11
|
-
* must be called separately with the HTTP server instance to enable
|
|
12
|
-
* WebSocket subscriptions.
|
|
13
|
-
*
|
|
14
|
-
* @param server - The HTTP server running the Express app
|
|
15
|
-
* @param schema - The GraphQL schema with subscription definitions
|
|
16
|
-
* @param layer - Effect layer providing services required by resolvers
|
|
17
|
-
* @param options - Optional configuration and lifecycle hooks
|
|
18
|
-
* @returns Object with cleanup function
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* ```typescript
|
|
22
|
-
* import express from "express"
|
|
23
|
-
* import { createServer } from "node:http"
|
|
24
|
-
* import { toMiddleware, attachWebSocket } from "@effect-gql/express"
|
|
25
|
-
* import { makeGraphQLRouter, GraphQLSchemaBuilder } from "@effect-gql/core"
|
|
26
|
-
* import { Layer, Effect, Stream } from "effect"
|
|
27
|
-
* import * as S from "effect/Schema"
|
|
28
|
-
*
|
|
29
|
-
* // Build schema with subscriptions
|
|
30
|
-
* const schema = GraphQLSchemaBuilder.empty
|
|
31
|
-
* .query("hello", { type: S.String, resolve: () => Effect.succeed("world") })
|
|
32
|
-
* .subscription("counter", {
|
|
33
|
-
* type: S.Int,
|
|
34
|
-
* subscribe: () => Effect.succeed(Stream.fromIterable([1, 2, 3]))
|
|
35
|
-
* })
|
|
36
|
-
* .buildSchema()
|
|
37
|
-
*
|
|
38
|
-
* // Create Express app with middleware
|
|
39
|
-
* const app = express()
|
|
40
|
-
* app.use(express.json())
|
|
41
|
-
* const router = makeGraphQLRouter(schema, Layer.empty, { graphiql: true })
|
|
42
|
-
* app.use(toMiddleware(router, Layer.empty))
|
|
43
|
-
*
|
|
44
|
-
* // Create HTTP server and attach WebSocket support
|
|
45
|
-
* const server = createServer(app)
|
|
46
|
-
* const ws = attachWebSocket(server, schema, Layer.empty, {
|
|
47
|
-
* path: "/graphql"
|
|
48
|
-
* })
|
|
49
|
-
*
|
|
50
|
-
* server.listen(4000, () => {
|
|
51
|
-
* console.log("Server running on http://localhost:4000")
|
|
52
|
-
* console.log("WebSocket subscriptions available at ws://localhost:4000/graphql")
|
|
53
|
-
* })
|
|
54
|
-
*
|
|
55
|
-
* // Cleanup on shutdown
|
|
56
|
-
* process.on("SIGINT", async () => {
|
|
57
|
-
* await ws.close()
|
|
58
|
-
* server.close()
|
|
59
|
-
* })
|
|
60
|
-
* ```
|
|
61
|
-
*/
|
|
62
|
-
const attachWebSocket = (server, schema, layer, options) => {
|
|
63
|
-
const wss = new ws_1.WebSocketServer({ noServer: true });
|
|
64
|
-
const path = options?.path ?? "/graphql";
|
|
65
|
-
// Create the handler from core
|
|
66
|
-
const handler = (0, core_1.makeGraphQLWSHandler)(schema, layer, options);
|
|
67
|
-
// Track active connections for cleanup
|
|
68
|
-
const activeConnections = new Set();
|
|
69
|
-
wss.on("connection", (ws) => {
|
|
70
|
-
activeConnections.add(ws);
|
|
71
|
-
const effectSocket = (0, core_1.toEffectWebSocketFromWs)(ws);
|
|
72
|
-
// Run the handler
|
|
73
|
-
effect_1.Effect.runPromise(handler(effectSocket).pipe(effect_1.Effect.catchAll((error) => effect_1.Effect.logError("GraphQL WebSocket handler error", error)))).finally(() => {
|
|
74
|
-
activeConnections.delete(ws);
|
|
75
|
-
});
|
|
76
|
-
});
|
|
77
|
-
const handleUpgrade = (request, socket, head) => {
|
|
78
|
-
// Check if this is the GraphQL WebSocket path
|
|
79
|
-
const url = new URL(request.url ?? "/", `http://${request.headers.host}`);
|
|
80
|
-
if (url.pathname !== path) {
|
|
81
|
-
socket.destroy();
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
// Check for correct WebSocket subprotocol
|
|
85
|
-
const protocol = request.headers["sec-websocket-protocol"];
|
|
86
|
-
if (!protocol?.includes("graphql-transport-ws")) {
|
|
87
|
-
socket.write("HTTP/1.1 400 Bad Request\r\n\r\n");
|
|
88
|
-
socket.destroy();
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
wss.handleUpgrade(request, socket, head, (ws) => {
|
|
92
|
-
wss.emit("connection", ws, request);
|
|
93
|
-
});
|
|
94
|
-
};
|
|
95
|
-
// Attach upgrade handler to server
|
|
96
|
-
server.on("upgrade", (request, socket, head) => {
|
|
97
|
-
handleUpgrade(request, socket, head);
|
|
98
|
-
});
|
|
99
|
-
const close = async () => {
|
|
100
|
-
// Close all active connections
|
|
101
|
-
for (const ws of activeConnections) {
|
|
102
|
-
ws.close(1001, "Server shutting down");
|
|
103
|
-
}
|
|
104
|
-
activeConnections.clear();
|
|
105
|
-
// Close the WebSocket server
|
|
106
|
-
return new Promise((resolve, reject) => {
|
|
107
|
-
wss.close((error) => {
|
|
108
|
-
if (error)
|
|
109
|
-
reject(error);
|
|
110
|
-
else
|
|
111
|
-
resolve();
|
|
112
|
-
});
|
|
113
|
-
});
|
|
114
|
-
};
|
|
115
|
-
return { close };
|
|
116
|
-
};
|
|
117
|
-
exports.attachWebSocket = attachWebSocket;
|
|
118
|
-
//# 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;AAItC,2BAA+C;AAE/C,2CAIyB;AAazB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsDG;AACI,MAAM,eAAe,GAAG,CAC7B,MAAc,EACd,MAAqB,EACrB,KAAqB,EACrB,OAA6B,EACG,EAAE;IAClC,MAAM,GAAG,GAAG,IAAI,oBAAe,CAAC,EAAE,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|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Convert Node.js/Express 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 req.headers
|
|
10
|
-
* @returns A web standard Headers object
|
|
11
|
-
*/
|
|
12
|
-
export const toWebHeaders = (nodeHeaders: IncomingHttpHeaders): Headers => {
|
|
13
|
-
const headers = new Headers()
|
|
14
|
-
for (const [key, value] of Object.entries(nodeHeaders)) {
|
|
15
|
-
if (value) {
|
|
16
|
-
if (Array.isArray(value)) {
|
|
17
|
-
value.forEach((v) => headers.append(key, v))
|
|
18
|
-
} else {
|
|
19
|
-
headers.set(key, value)
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
return headers
|
|
24
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export { toMiddleware } from "./middleware"
|
|
2
|
-
|
|
3
|
-
// WebSocket subscription support
|
|
4
|
-
export { attachWebSocket, type ExpressWSOptions } from "./ws"
|
|
5
|
-
|
|
6
|
-
// SSE (Server-Sent Events) subscription support
|
|
7
|
-
export { sseMiddleware, createSSEHandler, type ExpressSSEOptions } from "./sse"
|