-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
125 additions
and
0 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,17 @@ | ||
[graphql-sse](../README.md) / [use/http2](../modules/use_http2.md) / RequestContext | ||
|
||
# Interface: RequestContext | ||
|
||
[use/http2](../modules/use_http2.md).RequestContext | ||
|
||
## Table of contents | ||
|
||
### Properties | ||
|
||
- [res](use_http2.RequestContext.md#res) | ||
|
||
## Properties | ||
|
||
### res | ||
|
||
• **res**: `Http2ServerResponse` |
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,48 @@ | ||
[graphql-sse](../README.md) / use/http2 | ||
|
||
# Module: use/http2 | ||
|
||
## Table of contents | ||
|
||
### Interfaces | ||
|
||
- [RequestContext](../interfaces/use_http2.RequestContext.md) | ||
|
||
### Functions | ||
|
||
- [createHandler](use_http2.md#createhandler) | ||
|
||
## Functions | ||
|
||
### createHandler | ||
|
||
▸ **createHandler**<`Context`\>(`options`): (`req`: `Http2ServerRequest`, `res`: `Http2ServerResponse`) => `Promise`<`void`\> | ||
|
||
#### Type parameters | ||
|
||
| Name | Type | | ||
| :------ | :------ | | ||
| `Context` | extends [`OperationContext`](handler.md#operationcontext) = `undefined` | | ||
|
||
#### Parameters | ||
|
||
| Name | Type | | ||
| :------ | :------ | | ||
| `options` | [`HandlerOptions`](../interfaces/handler.HandlerOptions.md)<`Http2ServerRequest`, [`RequestContext`](../interfaces/use_http2.RequestContext.md), `Context`\> | | ||
|
||
#### Returns | ||
|
||
`fn` | ||
|
||
▸ (`req`, `res`): `Promise`<`void`\> | ||
|
||
##### Parameters | ||
|
||
| Name | Type | | ||
| :------ | :------ | | ||
| `req` | `Http2ServerRequest` | | ||
| `res` | `Http2ServerResponse` | | ||
|
||
##### Returns | ||
|
||
`Promise`<`void`\> |
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,54 @@ | ||
import type { Http2ServerRequest, Http2ServerResponse } from 'http2'; | ||
import { | ||
createHandler as createRawHandler, | ||
HandlerOptions, | ||
OperationContext, | ||
} from '../handler'; | ||
|
||
export interface RequestContext { | ||
res: Http2ServerResponse; | ||
} | ||
|
||
export function createHandler<Context extends OperationContext = undefined>( | ||
options: HandlerOptions<Http2ServerRequest, RequestContext, Context>, | ||
): (req: Http2ServerRequest, res: Http2ServerResponse) => Promise<void> { | ||
const handler = createRawHandler(options); | ||
return async function handleRequest(req, res) { | ||
const [body, init] = await handler({ | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- The method will always be available with http requests. | ||
method: req.method!, | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- The url will always be available with http requests. | ||
url: req.url!, | ||
headers: req.headers, | ||
body: () => | ||
new Promise((resolve, reject) => { | ||
let body = ''; | ||
req.on('data', (chunk) => (body += chunk)); | ||
req.once('error', reject); | ||
req.once('end', () => { | ||
req.off('error', reject); | ||
resolve(body); | ||
}); | ||
}), | ||
raw: req, | ||
context: { res }, | ||
}); | ||
|
||
res.writeHead(init.status, init.statusText, init.headers); | ||
|
||
if (!body || typeof body === 'string') { | ||
return new Promise<void>((resolve) => | ||
res.end(body || '', () => resolve()), | ||
); | ||
} | ||
|
||
res.once('close', body.return); | ||
for await (const value of body) { | ||
await new Promise<void>((resolve, reject) => | ||
res.write(value, (err) => (err ? reject(err) : resolve())), | ||
); | ||
} | ||
res.off('close', body.return); | ||
return new Promise((resolve) => res.end(resolve)); | ||
}; | ||
} |