@kiyasov/platform-hono 1.0.1 → 1.0.3

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.
Binary file
@@ -1,2 +1,3 @@
1
1
  export * from "./src/adapters";
2
2
  export * from "./src/interfaces";
3
+ export * from "./src/drivers";
package/dist/cjs/index.js CHANGED
@@ -16,4 +16,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./src/adapters"), exports);
18
18
  __exportStar(require("./src/interfaces"), exports);
19
+ __exportStar(require("./src/drivers"), exports);
19
20
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,iDAA+B;AAC/B,mDAAiC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,iDAA+B;AAC/B,mDAAiC;AACjC,gDAA8B"}
@@ -0,0 +1,16 @@
1
+ import { ApolloServer, BaseContext } from '@apollo/server';
2
+ import { AbstractGraphQLDriver } from '@nestjs/graphql';
3
+ import { ApolloDriverConfig } from '@nestjs/apollo';
4
+ export declare class HonoGraphQLDriver<T extends Record<string, any> = ApolloDriverConfig> extends AbstractGraphQLDriver {
5
+ protected apolloServer: ApolloServer<BaseContext>;
6
+ get instance(): ApolloServer<BaseContext>;
7
+ start(options: T): Promise<void>;
8
+ protected registerHono(options: T, { preStartHook }?: {
9
+ preStartHook?: () => void;
10
+ }): Promise<void>;
11
+ stop(): Promise<void>;
12
+ private httpHeadersToMap;
13
+ private parseBody;
14
+ private logError;
15
+ private parseFormURL;
16
+ }
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HonoGraphQLDriver = void 0;
4
+ const server_1 = require("@apollo/server");
5
+ const graphql_1 = require("@nestjs/graphql");
6
+ const drainHttpServer_1 = require("@apollo/server/plugin/drainHttpServer");
7
+ const common_1 = require("@nestjs/common");
8
+ class HonoGraphQLDriver extends graphql_1.AbstractGraphQLDriver {
9
+ get instance() {
10
+ return this.apolloServer;
11
+ }
12
+ async start(options) {
13
+ const { httpAdapter } = this.httpAdapterHost;
14
+ const platformName = httpAdapter.getType();
15
+ if (platformName !== 'hono') {
16
+ throw new Error('This driver is only compatible with the Hono platform');
17
+ }
18
+ return this.registerHono(options);
19
+ }
20
+ async registerHono(options, { preStartHook } = {}) {
21
+ const { path, typeDefs, resolvers, schema } = options;
22
+ const { httpAdapter } = this.httpAdapterHost;
23
+ const app = httpAdapter.getInstance();
24
+ const drainHttpServerPlugin = (0, drainHttpServer_1.ApolloServerPluginDrainHttpServer)({
25
+ httpServer: httpAdapter.getHttpServer(),
26
+ });
27
+ preStartHook?.();
28
+ const server = new server_1.ApolloServer({
29
+ typeDefs,
30
+ resolvers,
31
+ schema,
32
+ ...options,
33
+ plugins: (options.plugins || []).concat(drainHttpServerPlugin),
34
+ });
35
+ await server.start();
36
+ app.use(path, async (ctx) => {
37
+ const bodyData = await this.parseBody(ctx.req);
38
+ const httpGraphQLResponse = await server.executeHTTPGraphQLRequest({
39
+ httpGraphQLRequest: {
40
+ body: bodyData,
41
+ method: ctx.req.method,
42
+ headers: this.httpHeadersToMap(ctx.req.raw.headers),
43
+ search: new URL(ctx.req.url).search,
44
+ },
45
+ context: options?.context ?? (() => Promise.resolve({})),
46
+ });
47
+ const { headers, body, status } = httpGraphQLResponse;
48
+ for (const [headerKey, headerValue] of headers) {
49
+ ctx.header(headerKey, headerValue);
50
+ }
51
+ ctx.status(status === undefined ? 200 : status);
52
+ if (body.kind === 'complete') {
53
+ return ctx.body(body.string);
54
+ }
55
+ const readableStream = new ReadableStream({
56
+ async start(controller) {
57
+ for await (const chunk of body.asyncIterator) {
58
+ controller.enqueue(new TextEncoder().encode(chunk));
59
+ }
60
+ controller.close();
61
+ },
62
+ });
63
+ return new Response(readableStream, {
64
+ headers: { 'Content-Type': 'application/octet-stream' },
65
+ });
66
+ });
67
+ this.apolloServer = server;
68
+ }
69
+ stop() {
70
+ return this.apolloServer?.stop();
71
+ }
72
+ httpHeadersToMap(headers) {
73
+ const map = new server_1.HeaderMap();
74
+ headers.forEach((value, key) => map.set(key, value));
75
+ return map;
76
+ }
77
+ async parseBody(req) {
78
+ const contentType = req.header('content-type');
79
+ if (contentType === 'application/graphql')
80
+ return { query: await req.text() };
81
+ if (contentType === 'application/json')
82
+ return req.json().catch(this.logError);
83
+ if (contentType === 'application/x-www-form-urlencoded')
84
+ return this.parseFormURL(req);
85
+ return {};
86
+ }
87
+ logError(e) {
88
+ if (e instanceof Error) {
89
+ common_1.Logger.error(e.stack || e.message);
90
+ }
91
+ throw new Error(`POST body sent invalid JSON: ${e}`);
92
+ }
93
+ async parseFormURL(req) {
94
+ const searchParams = new URLSearchParams(await req.text());
95
+ return Object.fromEntries(searchParams.entries());
96
+ }
97
+ }
98
+ exports.HonoGraphQLDriver = HonoGraphQLDriver;
99
+ //# sourceMappingURL=graphql.driver.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graphql.driver.js","sourceRoot":"","sources":["../../../../src/drivers/graphql.driver.ts"],"names":[],"mappings":";;;AAAA,2CAAsE;AACtE,6CAAwD;AACxD,2EAA0F;AAI1F,2CAAwC;AAExC,MAAa,iBAEX,SAAQ,+BAAqB;IAG7B,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,YAAY,CAAC;IAC3B,CAAC;IAED,KAAK,CAAC,KAAK,CAAC,OAAU;QACpB,MAAM,EAAE,WAAW,EAAE,GAAG,IAAI,CAAC,eAAe,CAAC;QAC7C,MAAM,YAAY,GAAG,WAAW,CAAC,OAAO,EAAE,CAAC;QAE3C,IAAI,YAAY,KAAK,MAAM,EAAE,CAAC;YAC5B,MAAM,IAAI,KAAK,CAAC,uDAAuD,CAAC,CAAC;QAC3E,CA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,IAAI,kBAAS,EAAE,CAAC;QAC5B,OAAO,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC;QACrD,OAAO,GAAG,CAAC;IACb,CAAC;IAEO,KAAK,CAAC,SAAS,CAAC,GAAgB;QACtC,MAAM,WAAW,GAAG,GAAG,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC;QAC/C,IAAI,WAAW,KAAK,qBAAqB;YACvC,OAAO,EAAE,KAAK,EAAE,MAAM,GAAG,CAAC,IAAI,EAAE,EAAE,CAAC;QACrC,IAAI,WAAW,KAAK,kBAAkB;YACpC,OAAO,GAAG,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QACzC,IAAI,WAAW,KAAK,mCAAmC;YACrD,OAAO,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC;QAChC,OAAO,EAAE,CAAC;IACZ,CAAC;IAEO,QAAQ,CAAC,CAAU;QACzB,IAAI,CAAC,YAAY,KAAK,EAAE,CAAC;YACvB,eAAM,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC;QACrC,CAAC;QACD,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,EAAE,CAAC,CAAC;IACvD,CAAC;IAEO,KAAK,CAAC,YAAY,CAAC,GAAgB;QACzC,MAAM,YAAY,GAAG,IAAI,eAAe,CAAC,MAAM,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC;QAC3D,OAAO,MAAM,CAAC,WAAW,CAAC,YAAY,CAAC,OAAO,EAAE,CAAC,CAAC;IACpD,CAAC;CACF;AArHD,8CAqHC"}
@@ -0,0 +1 @@
1
+ export * from "./graphql.driver";
@@ -0,0 +1,18 @@
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./graphql.driver"), exports);
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/drivers/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,mDAAiC"}