-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #78
- Loading branch information
Showing
5 changed files
with
437 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import type { Middleware, Response } from 'koa'; | ||
import type { IncomingMessage } from 'http'; | ||
import { | ||
createHandler as createRawHandler, | ||
HandlerOptions as RawHandlerOptions, | ||
OperationContext, | ||
} from '../handler'; | ||
|
||
/** | ||
* @category Server/koa | ||
*/ | ||
export interface RequestContext { | ||
res: Response; | ||
} | ||
|
||
/** | ||
* Handler options when using the koa adapter. | ||
* | ||
* @category Server/koa | ||
*/ | ||
export type HandlerOptions<Context extends OperationContext = undefined> = | ||
RawHandlerOptions<IncomingMessage, RequestContext, Context>; | ||
|
||
/** | ||
* The ready-to-use handler for [Koa](https://expressjs.com). | ||
* | ||
* Errors thrown from the provided options or callbacks (or even due to | ||
* library misuse or potential bugs) will reject the handler or bubble to the | ||
* returned iterator. They are considered internal errors and you should take care | ||
* of them accordingly. | ||
* | ||
* For production environments, its recommended not to transmit the exact internal | ||
* error details to the client, but instead report to an error logging tool or simply | ||
* the console. | ||
* | ||
* ```js | ||
* import Koa from 'koa'; // yarn add koa | ||
* import mount from 'koa-mount'; // yarn add koa-mount | ||
* import { createHandler } from 'graphql-sse/lib/use/koa'; | ||
* import { schema } from './my-graphql'; | ||
* | ||
* const app = new Koa(); | ||
* app.use( | ||
* mount('/graphql/stream', async (ctx, next) => { | ||
* try { | ||
* await handler(ctx, next); | ||
* } catch (err) { | ||
* console.error(err); | ||
* ctx.response.status = 500; | ||
* ctx.response.message = 'Internal Server Error'; | ||
* } | ||
* }), | ||
* ); | ||
* | ||
* app.listen({ port: 4000 }); | ||
* console.log('Listening to port 4000'); | ||
* ``` | ||
* | ||
* @category Server/koa | ||
*/ | ||
export function createHandler<Context extends OperationContext = undefined>( | ||
options: HandlerOptions<Context>, | ||
): Middleware { | ||
const handler = createRawHandler(options); | ||
return async function requestListener(ctx) { | ||
const [body, init] = await handler({ | ||
url: ctx.url, | ||
method: ctx.method, | ||
headers: { | ||
get(key) { | ||
const header = ctx.headers[key]; | ||
return Array.isArray(header) ? header.join('\n') : header; | ||
}, | ||
}, | ||
body: () => { | ||
if (ctx.body) { | ||
// in case koa has a body parser | ||
return ctx.body; | ||
} | ||
return new Promise<string>((resolve) => { | ||
let body = ''; | ||
ctx.req.on('data', (chunk) => (body += chunk)); | ||
ctx.req.on('end', () => resolve(body)); | ||
}); | ||
}, | ||
raw: ctx.req, | ||
context: { res: ctx.response }, | ||
}); | ||
ctx.response.status = init.status; | ||
ctx.response.message = init.statusText; | ||
if (init.headers) { | ||
for (const [name, value] of Object.entries(init.headers)) { | ||
ctx.response.set(name, value); | ||
} | ||
} | ||
|
||
if (!body || typeof body === 'string') { | ||
ctx.body = body; | ||
return; | ||
} | ||
|
||
ctx.res.once('close', body.return); | ||
for await (const value of body) { | ||
const closed = await new Promise((resolve, reject) => { | ||
if (!ctx.res.writable) { | ||
// response's close event might be late | ||
resolve(true); | ||
} else { | ||
ctx.res.write(value, (err) => (err ? reject(err) : resolve(false))); | ||
} | ||
}); | ||
if (closed) { | ||
break; | ||
} | ||
} | ||
ctx.res.off('close', body.return); | ||
return new Promise((resolve) => ctx.res.end(resolve)); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.