Skip to content

Commit

Permalink
feat: use Deno.serve() instead of serve()
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Jan 9, 2025
1 parent 8a596e5 commit ee4822b
Show file tree
Hide file tree
Showing 15 changed files with 47 additions and 54 deletions.
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ A clear and concise description of what the bug is.
**To Reproduce**

```ts
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import { Server } from "https://deno.land/x/[email protected]/mod.ts";

const io = new Server();
Expand All @@ -28,7 +27,8 @@ io.on("connection", (socket) => {
});
});

await serve(io.handler(), {
Deno.serve({
handler: io.handler(),
port: 3000,
});
```
Expand Down
4 changes: 2 additions & 2 deletions .github/PULL_REQUEST_TEMPLATE/feature.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ Related issue or discussion:
**New behavior**

```ts
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import { Server } from "https://deno.land/x/[email protected]/mod.ts";

const io = new Server();
Expand All @@ -26,7 +25,8 @@ io.on("connection", (socket) => {
});
});

await serve(io.handler(), {
Deno.serve({
handler: io.handler(),
port: 3000,
});
```
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ Table of content:
## Usage

```ts
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import { Server } from "https://deno.land/x/[email protected]/mod.ts";

const io = new Server();
Expand All @@ -39,7 +38,8 @@ io.on("connection", (socket) => {
});
});

await serve(io.handler(), {
Deno.serve({
handler: io.handler(),
port: 3000,
});
```
Expand Down Expand Up @@ -86,7 +86,6 @@ You need to use the [.handle()](https://github.com/oakserver/oak#handle-method)
method:

```ts
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import { Server } from "https://deno.land/x/[email protected]/mod.ts";
import { Application } from "https://deno.land/x/[email protected]/mod.ts";

Expand All @@ -112,7 +111,8 @@ const handler = io.handler(async (req) => {
return await app.handle(req) || new Response(null, { status: 404 });
});

await serve(handler, {
Deno.serve({
handler,
port: 3000,
});
```
Expand Down Expand Up @@ -281,7 +281,6 @@ servers.
Documentation: https://socket.io/docs/v4/redis-adapter/

```js
import { serve } from "https://deno.land/std/http/server.ts";
import {
createRedisAdapter,
createRedisClient,
Expand All @@ -301,7 +300,8 @@ const io = new Server({
adapter: createRedisAdapter(pubClient, subClient),
});

await serve(io.handler(), {
Deno.serve({
handler: io.handler(),
port: 3000,
});
```
Expand Down
5 changes: 0 additions & 5 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
export {
type ConnInfo,
type Handler,
} from "https://deno.land/[email protected]/http/server.ts";

export { getLogger } from "https://deno.land/[email protected]/log/mod.ts";
10 changes: 5 additions & 5 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/**
* @example
* import { serve } from "https://deno.land/[email protected]/http/server.ts";
* import { Server } from "https://deno.land/x/[email protected]/mod.ts";
*
* const io = new Server();
Expand All @@ -21,7 +20,8 @@
* });
* });
*
* await serve(io.handler(), {
* Deno.serve({
* handler: io.handler(),
* port: 3000,
* });
*/
Expand All @@ -40,7 +40,6 @@ export {
* Documentation: https://socket.io/docs/v4/redis-adapter/
*
* @example
* import { serve } from "https://deno.land/std/http/server.ts";
* import { Server, createRedisAdapter, createRedisClient } from "https://deno.land/x/socket_io/mod.ts";
*
* const [pubClient, subClient] = await Promise.all([
Expand All @@ -56,8 +55,9 @@ export {
* adapter: createRedisAdapter(pubClient, subClient)
* });
*
* await serve(io.handler(), {
* port: 3000
* Deno.serve({
* handler: io.handler(),
* port: 3000
* });
*/
export {
Expand Down
4 changes: 2 additions & 2 deletions packages/engine.io/examples/protocol-compliance-test/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { serve } from "../../../../test_deps.ts";
import { Server } from "../../mod.ts";

const engine = new Server({
Expand All @@ -17,6 +16,7 @@ engine.on("connection", (socket) => {
});
});

await serve(engine.handler(), {
Deno.serve({
handler: engine.handler(),
port: 3000,
});
18 changes: 9 additions & 9 deletions packages/engine.io/lib/server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConnInfo, getLogger, Handler } from "../../../deps.ts";
import { getLogger } from "../../../deps.ts";
import { EventEmitter } from "../../event-emitter/mod.ts";
import { Socket } from "./socket.ts";
import { Polling } from "./transports/polling.ts";
Expand Down Expand Up @@ -41,7 +41,7 @@ export interface ServerOptions {
*/
allowRequest?: (
req: Request,
connInfo: ConnInfo,
connInfo: Deno.ServeHandlerInfo,
) => Promise<void>;
/**
* The options related to Cross-Origin Resource Sharing (CORS)
Expand All @@ -53,15 +53,15 @@ export interface ServerOptions {
editHandshakeHeaders?: (
responseHeaders: Headers,
req: Request,
connInfo: ConnInfo,
connInfo: Deno.ServeHandlerInfo,
) => void | Promise<void>;
/**
* A function that allows to edit the response headers of all requests
*/
editResponseHeaders?: (
responseHeaders: Headers,
req: Request,
connInfo: ConnInfo,
connInfo: Deno.ServeHandlerInfo,
) => void | Promise<void>;
}

Expand All @@ -73,7 +73,7 @@ interface ConnectionError {
}

interface ServerReservedEvents {
connection: (socket: Socket, request: Request, connInfo: ConnInfo) => void;
connection: (socket: Socket, request: Request, connInfo: Deno.ServeHandlerInfo) => void;
connection_error: (err: ConnectionError) => void;
}

Expand Down Expand Up @@ -124,8 +124,8 @@ export class Server extends EventEmitter<
*
* @param additionalHandler - another handler which will receive the request if the path does not match
*/
public handler(additionalHandler?: Handler) {
return (req: Request, connInfo: ConnInfo): Response | Promise<Response> => {
public handler(additionalHandler?: Deno.ServeHandler) {
return (req: Request, connInfo: Deno.ServeHandlerInfo): Response | Promise<Response> => {
const url = new URL(req.url);
if (url.pathname === this.opts.path) {
return this.handleRequest(req, connInfo, url);
Expand All @@ -147,7 +147,7 @@ export class Server extends EventEmitter<
*/
private async handleRequest(
req: Request,
connInfo: ConnInfo,
connInfo: Deno.ServeHandlerInfo,
url: URL,
): Promise<Response> {
getLogger("engine.io").debug(`[server] handling ${req.method} ${req.url}`);
Expand Down Expand Up @@ -321,7 +321,7 @@ export class Server extends EventEmitter<
*/
private async handshake(
req: Request,
connInfo: ConnInfo,
connInfo: Deno.ServeHandlerInfo,
responseHeaders: Headers,
): Promise<Response> {
const id = generateId();
Expand Down
4 changes: 2 additions & 2 deletions packages/engine.io/lib/transports/polling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class Polling extends Transport {

req.signal.addEventListener("abort", () => {
// note: this gets never triggered
this.onError("poll connection closed prematurely");
// this.onError("poll connection closed prematurely");
});

getLogger("engine.io").debug(
Expand Down Expand Up @@ -77,7 +77,7 @@ export class Polling extends Transport {
): Promise<Response> {
req.signal.addEventListener("abort", () => {
// note: this gets never triggered
this.onError("data request connection closed prematurely");
// this.onError("data request connection closed prematurely");
});

getLogger("engine.io").debug(
Expand Down
4 changes: 2 additions & 2 deletions packages/engine.io/test/setup.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Server } from "../lib/server.ts";
import { serve } from "../../../test_deps.ts";
import { createPartialDone } from "../../util.test.ts";

export function setup(
Expand All @@ -10,7 +9,8 @@ export function setup(
return new Promise((resolve, reject) => {
const abortController = new AbortController();

serve(engine.handler(), {
Deno.serve({
handler: engine.handler(),
onListen: ({ port }) => {
const partialDone = createPartialDone(count, () => {
// close the server
Expand Down
4 changes: 2 additions & 2 deletions packages/socket.io-redis-adapter/test/setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Server, Socket } from "../../socket.io/mod.ts";
import { createPartialDone, runHandshake, waitFor } from "../../util.test.ts";
import { connect } from "../../../vendor/deno.land/x/[email protected]/mod.ts";
import { createAdapter } from "../mod.ts";
import { serve } from "../../../test_deps.ts";

function createRedisClient() {
return connect({
Expand All @@ -29,7 +28,8 @@ function createServer(port: number): Promise<TestServer> {

const abortController = new AbortController();

return serve(io.handler(), {
return Deno.serve({
handler: io.handler(),
port,
signal: abortController.signal,
onListen: async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/socket.io/examples/protocol-compliance-test/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { serve } from "../../../../test_deps.ts";
import { Server } from "../../mod.ts";

const io = new Server({
Expand Down Expand Up @@ -28,6 +27,7 @@ io.of("/custom").on("connection", (socket) => {
socket.emit("auth", socket.handshake.auth);
});

await serve(io.handler(), {
Deno.serve({
handler: io.handler(),
port: 3000,
});
4 changes: 2 additions & 2 deletions packages/socket.io/lib/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EventsMap } from "../../event-emitter/mod.ts";
import { Decoder, Packet, PacketType } from "../../socket.io-parser/mod.ts";
import { type Socket as RawSocket } from "../../engine.io/mod.ts";
import { ConnInfo, getLogger } from "../../../deps.ts";
import { getLogger } from "../../../deps.ts";
import { Handshake, Socket } from "./socket.ts";
import { Server } from "./server.ts";
import { RawData } from "../../engine.io-parser/mod.ts";
Expand Down Expand Up @@ -43,7 +43,7 @@ export class Client<
decoder: Decoder,
conn: RawSocket,
req: Request,
connInfo: ConnInfo,
connInfo: Deno.ServeHandlerInfo,
) {
this.server = server;
this.decoder = decoder;
Expand Down
18 changes: 9 additions & 9 deletions packages/socket.io/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
EventParams,
EventsMap,
} from "../../event-emitter/mod.ts";
import { getLogger, type Handler } from "../../../deps.ts";
import { getLogger } from "../../../deps.ts";
import { Client } from "./client.ts";
import { Decoder, Encoder } from "../../socket.io-parser/mod.ts";
import { Namespace, NamespaceReservedEvents } from "./namespace.ts";
Expand Down Expand Up @@ -75,7 +75,6 @@ type ParentNspNameMatchFn = (
* Represents a Socket.IO server.
*
* @example
* import { serve } from "https://deno.land/[email protected]/http/server.ts";
* import { Server } from "https://deno.land/x/[email protected]/mod.ts";
*
* const io = new Server();
Expand All @@ -96,7 +95,8 @@ type ParentNspNameMatchFn = (
* });
* });
*
* await serve(io.handler(), {
* Deno.serve({
* handler: io.handler(),
* port: 3000,
* });
*/
Expand Down Expand Up @@ -183,18 +183,18 @@ export class Server<
* Returns a request handler.
*
* @example
* import { serve } from "https://deno.land/[email protected]/http/server.ts";
* import { Server } from "https://deno.land/x/[email protected]/mod.ts";
*
* const io = new Server();
*
* await serve(io.handler(), {
* Deno.serve({
* handler: io.handler(),
* port: 3000,
* });
*
* @param additionalHandler - another handler which will receive the request if the path does not match
*/
public handler(additionalHandler?: Handler) {
public handler(additionalHandler?: Deno.ServeHandler) {
return this.engine.handler(additionalHandler);
}

Expand Down Expand Up @@ -296,21 +296,21 @@ export class Server<
* Closes the server.
*
* @example
* import { serve } from "https://deno.land/[email protected]/http/server.ts";
* import { Server } from "https://deno.land/x/[email protected]/mod.ts";
*
* const io = new Server();
* const abortController = new AbortController();
*
* await serve(io.handler(), {
* Deno.serve({
* handler: io.handler(),
* port: 3000,
* signal: abortController.signal,
* onListen: () => {
* setTimeout(() => {
* // close the HTTP server
* abortController.abort();
* // close the Socket.IO server
* server.close();
* io.close();
* }, 10000);
* }
* });
Expand Down
4 changes: 2 additions & 2 deletions packages/socket.io/test/setup.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Server } from "../lib/server.ts";
import { serve } from "../../../test_deps.ts";
import { createPartialDone } from "../../util.test.ts";

export function setup(
Expand All @@ -10,7 +9,8 @@ export function setup(
return new Promise((resolve, reject) => {
const abortController = new AbortController();

serve(server.handler(), {
Deno.serve({
handler: server.handler(),
onListen: ({ port }) => {
const partialDone = createPartialDone(count, () => {
setTimeout(() => {
Expand Down
2 changes: 0 additions & 2 deletions test_deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@ export * from "https://deno.land/[email protected]/testing/asserts.ts";

export { describe, it } from "https://deno.land/[email protected]/testing/bdd.ts";

export { serve } from "https://deno.land/[email protected]/http/server.ts";

export * from "https://deno.land/[email protected]/log/mod.ts";

0 comments on commit ee4822b

Please sign in to comment.