Skip to content

Commit 771e7b0

Browse files
committed
feat: allow overriding std/native HTTP servers
Resolves: #320
1 parent fe7ad3c commit 771e7b0

File tree

5 files changed

+62
-10
lines changed

5 files changed

+62
-10
lines changed

README.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,11 +513,32 @@ need to start your server with the `--unstable` flag. For example:
513513
> deno run --allow-net --unstable server.ts
514514
```
515515

516-
Currently there are two features that are not yet supported with the native
517-
server, those are:
516+
The only feature known to be a limitation on the native HTTP server is the
517+
ability to upgrade a connection to a web-socket.
518518

519-
- Server Sent Events
520-
- Upgrading a request to a WebSocket
519+
#### Overriding the HTTP server
520+
521+
If you wish to not utilise the default behavior of detecting the capability, you
522+
can force the server during application creation. For example to force the
523+
`std/http` server, you would do the following:
524+
525+
```ts
526+
import { Application, HttpServerStd } from "https://deno.land/x/oak/mod.ts";
527+
528+
const app = new Application({
529+
serverConstructor: HttpServerStd,
530+
});
531+
```
532+
533+
Of if you wanted to force the native:
534+
535+
```ts
536+
import { Application, HttpServerNative } from "https://deno.land/x/oak/mod.ts";
537+
538+
const app = new Application({
539+
serverConstructor: HttpServerNative,
540+
});
541+
```
521542

522543
### Just handling requests
523544

application.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,10 @@ export interface ApplicationOptions<S> {
108108
proxy?: boolean;
109109

110110
/** A server constructor to use instead of the default server for receiving
111-
* requests.
112-
*
113-
* _This is not generally used, except for mocking and testing._
111+
* requests. When the native HTTP server is detected in the environment, then
112+
* the native server will be used, otherwise the `std/http` server will be
113+
* used. Passing either `HTTPServerStd` or `HTTPServerNative` will override
114+
* this behavior.
114115
*/
115116
serverConstructor?: ServerConstructor<ServerRequest | NativeRequest>;
116117

application_test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import {
1212
} from "./application.ts";
1313
import { Context } from "./context.ts";
1414
import { Status } from "./deps.ts";
15-
import { NativeRequest } from "./http_server_native.ts";
15+
import { HttpServerNative } from "./http_server_native.ts";
16+
import type { NativeRequest } from "./http_server_native.ts";
17+
import { HttpServerStd } from "./http_server_std.ts";
1618
import type { ServerRequest, ServerResponse } from "./http_server_std.ts";
1719
import { httpErrors } from "./httpError.ts";
1820
import { Data, KeyStack } from "./keyStack.ts";
@@ -635,3 +637,21 @@ test({
635637
}
636638
},
637639
});
640+
641+
test({
642+
name: "new Application() - HttpServerStd",
643+
fn() {
644+
new Application({
645+
serverConstructor: HttpServerStd,
646+
});
647+
},
648+
});
649+
650+
test({
651+
name: "new Application() - HttpServerNative",
652+
fn() {
653+
new Application({
654+
serverConstructor: HttpServerNative,
655+
});
656+
},
657+
});

mod.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ export * as helpers from "./helpers.ts";
2727
export { Cookies } from "./cookies.ts";
2828
export type { CookiesGetOptions, CookiesSetDeleteOptions } from "./cookies.ts";
2929
export * as etag from "./etag.ts";
30+
export { HttpServerNative } from "./http_server_native.ts";
31+
export type { NativeRequest } from "./http_server_native.ts";
32+
export { HttpServerStd } from "./http_server_std.ts";
3033
export type { ServerRequest, ServerResponse } from "./http_server_std.ts";
3134
export { HttpError, httpErrors, isHttpError } from "./httpError.ts";
3235
export { compose as composeMiddleware } from "./middleware.ts";
@@ -58,7 +61,12 @@ export type {
5861
ServerSentEventInit,
5962
ServerSentEventTarget,
6063
} from "./server_sent_event.ts";
61-
export type { ErrorStatus, HTTPMethods, RedirectStatus } from "./types.d.ts";
64+
export type {
65+
ErrorStatus,
66+
HTTPMethods,
67+
RedirectStatus,
68+
ServerConstructor,
69+
} from "./types.d.ts";
6270
export { isErrorStatus, isRedirectStatus } from "./util.ts";
6371

6472
// Re-exported from `net`

mod_test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ test({
1818
assertEquals(Object.keys(mod.helpers).length, 1);
1919
assertEquals(typeof mod.HttpError, "function");
2020
assertEquals(typeof mod.httpErrors, "object");
21+
assertEquals(typeof mod.HttpServerNative, "function");
22+
assertEquals(typeof mod.HttpServerStd, "function");
2123
assertEquals(typeof mod.isErrorStatus, "function");
2224
assertEquals(typeof mod.isHttpError, "function");
2325
assertEquals(typeof mod.isRedirectStatus, "function");
@@ -34,6 +36,6 @@ test({
3436
assertEquals(typeof mod.STATUS_TEXT, "object");
3537
assertEquals(typeof mod.Status, "object");
3638
assertEquals(typeof mod.send, "function");
37-
assertEquals(Object.keys(mod).length, 23);
39+
assertEquals(Object.keys(mod).length, 25);
3840
},
3941
});

0 commit comments

Comments
 (0)