Skip to content

Commit

Permalink
v0.6.1-rc (#35)
Browse files Browse the repository at this point in the history
* Fix [ERROR HANDLING] description on readme

* Add PATCH and HEAD to app.all()

* Hide RoutePathHandler.splitPathSegments

* Fix RoutePathHandler.sanitizePath

* Move tests to /test

* Modify ci

* Move deps.ts and dev_deps.ts to root directory

* Update ci to cache deps
  • Loading branch information
rikilele authored Jul 23, 2021
1 parent 66d80a5 commit cb634f2
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 35 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ jobs:

# Uncomment this step when we start using dependencies
- name: Cache dependencies
run: deno cache src/deps.ts
run: deno cache deps.ts

- name: Cache dev dependencies
run: deno cache src/dev_deps.ts
run: deno cache dev_deps.ts

- name: Run tests
run: deno test
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,11 @@ responded to within a Kyuko app:

1. **`[ERROR HANDLING]` Handling errors**

This step is run only if an error was thrown during the `[MIDDLEWARE]` or
`[ROUTE HANDLING]` steps. Error handlers registered via `app.error()` are run
in order of registration and until completion. They are given access to the
`err` thrown and the `req` and `res` objects, and are free to modify them as
needed.
This step is run only if an error was thrown during the `[MIDDLEWARE]`,
`[ROUTE HANDLING]`, or `[DEFERRED HANDLERS]` steps. Error handlers registered
via `app.error()` are run in order of registration and until completion. They
are given access to the `err` thrown and the `req` and `res` objects, and are
free to modify them as needed.

Error handlers can choose to respond to the request by calling `res.send()`,
`res.redirect()`, etc. If not, a 500 Internal Server Error is used as a
Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 4 additions & 2 deletions src/Kyuko.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/// <reference path="https://deno.land/x/[email protected]/types/deploy.ns.d.ts" />
/// <reference path="https://deno.land/x/[email protected]/types/deploy.window.d.ts" />

import { brightRed, Status } from "./deps.ts";
import { brightRed, Status } from "../deps.ts";
import { KyukoRequest, KyukoRequestImpl } from "./KyukoRequest.ts";
import { KyukoResponse, KyukoResponseImpl } from "./KyukoResponse.ts";
import { RoutePathHandler } from "./RoutePathHandler.ts";
Expand Down Expand Up @@ -182,6 +182,8 @@ export class Kyuko {
this.#customHandlers.get("POST")?.set(routePath, handler);
this.#customHandlers.get("PUT")?.set(routePath, handler);
this.#customHandlers.get("DELETE")?.set(routePath, handler);
this.#customHandlers.get("PATCH")?.set(routePath, handler);
this.#customHandlers.get("HEAD")?.set(routePath, handler);
}

/**
Expand Down Expand Up @@ -243,7 +245,7 @@ export class Kyuko {
});

// Fill req.path
req.path = RoutePathHandler.splitPathSegments(pathname).join("/") || "/";
req.path = RoutePathHandler.sanitizePath(pathname);

this.invokeHandlers(req, res, routeHandler);
}
Expand Down
2 changes: 1 addition & 1 deletion src/KyukoResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/// <reference path="https://deno.land/x/[email protected]/types/deploy.ns.d.ts" />
/// <reference path="https://deno.land/x/[email protected]/types/deploy.window.d.ts" />

import { Status, STATUS_TEXT } from "./deps.ts";
import { Status, STATUS_TEXT } from "../deps.ts";

/**
* The response object that is handled in Kyuko applications.
Expand Down
18 changes: 17 additions & 1 deletion src/RoutePathHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ export class RoutePathHandler {
return result;
}

/**
* Returns the sanitized input `path`.
* See "Note on handling slashes" from `RoutePathHandler` class description.
*
* @param path The path to sanitize.
* @returns The sanitized path.
*/
static sanitizePath(path: string) {
const split = RoutePathHandler.splitPathSegments(path);
if (split.at(-1) === "") {
split.push("");
}

return split.join("/");
}

/**
* Splits the given path into an array of path segments.
* Note that `splitPathSegments(path).join('/') !== path`.
Expand All @@ -52,7 +68,7 @@ export class RoutePathHandler {
*
* @param path The route or url path to split
*/
static splitPathSegments(path: string): string[] {
private static splitPathSegments(path: string): string[] {
const result = path.split("/");
const divider = result.findIndex((seg) => seg !== "");
if (divider === -1) {
Expand Down
42 changes: 18 additions & 24 deletions src/RoutePathHandler.test.ts → test/RoutePathHandler.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2021 Riki Singh Khorana. All rights reserved. MIT license.

import { assertEquals } from "./dev_deps.ts";
import { RoutePathHandler } from "./RoutePathHandler.ts";
import { assertEquals } from "../dev_deps.ts";
import { RoutePathHandler } from "../src/RoutePathHandler.ts";

Deno.test("empty handler", () => {
const pathHandler = new RoutePathHandler();
Expand Down Expand Up @@ -281,30 +281,24 @@ Deno.test("creates req.params properly", () => {
);
});

Deno.test("splits leading slashes correctly", () => {
const { splitPathSegments } = RoutePathHandler;
assertEquals(splitPathSegments("/"), [""]);
assertEquals(splitPathSegments("//"), [""]);
assertEquals(splitPathSegments("///users"), ["", "users"]);
assertEquals(splitPathSegments("///users/Alice"), ["", "users", "Alice"]);
Deno.test("sanitizes leading slashes correctly", () => {
const { sanitizePath } = RoutePathHandler;
assertEquals(sanitizePath("/"), "/");
assertEquals(sanitizePath("//"), "/");
assertEquals(sanitizePath("///users"), "/users");
assertEquals(sanitizePath("///users/Alice"), "/users/Alice");
});

Deno.test("splits trailing slashes correctly", () => {
const { splitPathSegments } = RoutePathHandler;
assertEquals(splitPathSegments("/users/"), ["", "users"]);
assertEquals(splitPathSegments("/users/Alice/"), ["", "users", "Alice"]);
Deno.test("sanitizes trailing slashes correctly", () => {
const { sanitizePath } = RoutePathHandler;
assertEquals(sanitizePath("/users/"), "/users");
assertEquals(sanitizePath("/users/Alice/"), "/users/Alice");
});

Deno.test("splits mid-path slashes correctly", () => {
const { splitPathSegments } = RoutePathHandler;
assertEquals(splitPathSegments("/users//"), ["", "users", ""]);
assertEquals(splitPathSegments("/users///"), ["", "users", "", ""]);
assertEquals(splitPathSegments("/users//Alice/"), ["", "users", "", "Alice"]);
assertEquals(splitPathSegments("/users///Alice/"), [
"",
"users",
"",
"",
"Alice",
]);
Deno.test("sanitizes mid-path slashes correctly", () => {
const { sanitizePath } = RoutePathHandler;
assertEquals(sanitizePath("/users//"), "/users//");
assertEquals(sanitizePath("/users///"), "/users///");
assertEquals(sanitizePath("/users//Alice/"), "/users//Alice");
assertEquals(sanitizePath("/users///Alice/"), "/users///Alice");
});

0 comments on commit cb634f2

Please sign in to comment.