Skip to content

Commit

Permalink
feat: migrate to tested third party module ports for some deps.
Browse files Browse the repository at this point in the history
  • Loading branch information
asos-craigmorten committed May 29, 2020
1 parent 7f39aab commit 916ad4f
Show file tree
Hide file tree
Showing 21 changed files with 200 additions and 348 deletions.
4 changes: 4 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# ChangeLog

## [0.6.1] - 29-05-2020

- refactor: Replace internal ports of NPM modules with tested third party module equivalents.

## [0.6.0] - 29-05-2020

- feat: deliver content negotiation with the `res.format()` and `res.vary()` methods.
Expand Down
9 changes: 7 additions & 2 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@ export {
contentType,
charset,
} from "https://deno.land/x/[email protected]/mod.ts";
export { createHttpError } from "https://deno.land/x/[email protected]/httpError.ts";
export { createError } from "https://deno.land/x/http_errors/mod.ts";
export { Accepts } from "https://deno.land/x/accepts@master/mod.ts";
export { typeofrequest } from "https://deno.land/x/type_is@master/mod.ts";
export {
typeofrequest,
hasBody,
} from "https://deno.land/x/type_is@master/mod.ts";
export { isIP } from "https://deno.land/x/isIP@master/mod.ts";
export { vary } from "https://deno.land/x/vary@master/mod.ts";
export { lookup } from "https://deno.land/x/[email protected]/mod.ts";
export { escapeHtml } from "https://deno.land/x/escape_html/mod.ts";
export { encodeUrl } from "https://deno.land/x/encodeurl/mod.ts";
272 changes: 143 additions & 129 deletions lock.json

Large diffs are not rendered by default.

17 changes: 0 additions & 17 deletions src/middleware/bodyParser/hasBody.ts

This file was deleted.

7 changes: 3 additions & 4 deletions src/middleware/bodyParser/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@
*
*/

import { createHttpError } from "../../../deps.ts";
import { createError, hasBody } from "../../../deps.ts";
import { read } from "./read.ts";
import { getCharset } from "./getCharset.ts";
import { typeChecker } from "./typeChecker.ts";
import { hasBody } from "./hasBody.ts";
import { Request, Response, NextFunction } from "../../types.ts";

/**
Expand Down Expand Up @@ -106,7 +105,7 @@ export function json(options: any = {}) {
}

// skip requests without bodies
if (!hasBody(req)) {
if (!hasBody(req.headers)) {
req.parsedBody = {};
next();
return;
Expand All @@ -122,7 +121,7 @@ export function json(options: any = {}) {
const charset = getCharset(req) || "utf-8";
if (charset.substr(0, 4) !== "utf-") {
next(
createHttpError(
createError(
415,
'unsupported charset "' + charset.toUpperCase() + '"',
),
Expand Down
4 changes: 2 additions & 2 deletions src/middleware/bodyParser/raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

import { read } from "./read.ts";
import { Request, Response, NextFunction } from "../../types.ts";
import { hasBody } from "./hasBody.ts";
import { hasBody } from "../../../deps.ts";
import { typeChecker } from "./typeChecker.ts";

/**
Expand Down Expand Up @@ -64,7 +64,7 @@ export function raw(options: any = {}) {
}

// skip requests without bodies
if (!hasBody(req)) {
if (!hasBody(req.headers)) {
req.parsedBody = "";
next();
return;
Expand Down
12 changes: 6 additions & 6 deletions src/middleware/bodyParser/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/

import { Request, Response, NextFunction } from "../../types.ts";
import { createHttpError } from "../../../deps.ts";
import { createError } from "../../../deps.ts";

const decoder = new TextDecoder();

Expand Down Expand Up @@ -71,15 +71,15 @@ export async function read(
const raw = await Deno.readAll(reader);
body = decoder.decode(raw);
} catch (err) {
next(createHttpError(400, err));
next(createError(400, err));
}

// verify
if (verify) {
try {
verify(req, res, body, encoding);
} catch (err) {
next(createHttpError(403, err));
next(createError(403, err));
return;
}
}
Expand All @@ -88,7 +88,7 @@ export async function read(
try {
req.parsedBody = parse(body);
} catch (err) {
next(createHttpError(400, err));
next(createError(400, err));
return;
}

Expand All @@ -108,7 +108,7 @@ function getBodyReader(req: Request, inflate: boolean = true) {
.toLowerCase();

if (inflate === false && encoding !== "identity") {
throw createHttpError(
throw createError(
415,
'unsupported content encoding "' + encoding + '"',
);
Expand All @@ -121,7 +121,7 @@ function getBodyReader(req: Request, inflate: boolean = true) {
case "deflate":
case "gzip":
default:
throw createHttpError(
throw createError(
415,
'unsupported content encoding "' + encoding + '"',
);
Expand Down
4 changes: 2 additions & 2 deletions src/middleware/bodyParser/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import { read } from "./read.ts";
import { getCharset } from "./getCharset.ts";
import { Request, Response, NextFunction } from "../../types.ts";
import { hasBody } from "./hasBody.ts";
import { hasBody } from "../../../deps.ts";
import { typeChecker } from "./typeChecker.ts";

/**
Expand Down Expand Up @@ -66,7 +66,7 @@ export function text(options: any = {}) {
}

// skip requests without bodies
if (!hasBody(req)) {
if (!hasBody(req.headers)) {
req.parsedBody = "";
next();
return;
Expand Down
17 changes: 2 additions & 15 deletions src/middleware/bodyParser/typeChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,7 @@
*/

import { Request } from "../../types.ts";
import { hasBody } from "./hasBody.ts";
import { contentType } from "../../../deps.ts";

function normalize(type: any) {
return contentType(type || "")?.split(";")[0];
}

function typeIs(req: Request, type: string) {
if (!hasBody(req)) {
return false;
}

return normalize(req.headers.get("Content-Type")) === normalize(type);
}
import { typeofrequest } from "../../../deps.ts";

/**
* Get the simple type checker.
Expand All @@ -53,6 +40,6 @@ function typeIs(req: Request, type: string) {
*/
export function typeChecker(type: string) {
return function checkType(req: Request) {
return Boolean(typeIs(req, type));
return Boolean(typeofrequest(req.headers, [type]));
};
}
8 changes: 4 additions & 4 deletions src/middleware/bodyParser/urlencoded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
*
*/

import { createHttpError } from "../../../deps.ts";
import { createError } from "../../../deps.ts";
import { read } from "./read.ts";
import { getCharset } from "./getCharset.ts";
import { Request, Response, NextFunction } from "../../types.ts";
import { hasBody } from "./hasBody.ts";
import { hasBody } from "../../../deps.ts";
import { typeChecker } from "./typeChecker.ts";

/**
Expand Down Expand Up @@ -74,7 +74,7 @@ export function urlencoded(options: any = {}) {
}

// skip requests without bodies
if (!hasBody(req)) {
if (!hasBody(req.headers)) {
(req as any).parsedBody = Object.fromEntries(
new URLSearchParams().entries(),
);
Expand All @@ -90,7 +90,7 @@ export function urlencoded(options: any = {}) {
const charset = getCharset(req) || "utf-8";
if (charset !== "utf-8") {
return next(
createHttpError(
createError(
415,
'unsupported charset "' + charset.toUpperCase() + '"',
),
Expand Down
14 changes: 9 additions & 5 deletions src/middleware/serveStatic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@
*
*/

import { join, fromFileUrl, createHttpError } from "../../deps.ts";
import { escapeHtml } from "../utils/escapeHtml.ts";
import { encodeUrl } from "../utils/encodeUrl.ts";
import {
join,
fromFileUrl,
createError,
escapeHtml,
encodeUrl,
} from "../../deps.ts";
import { originalUrl as original, parseUrl } from "../utils/parseUrl.ts";
import {
Response,
Expand Down Expand Up @@ -184,7 +188,7 @@ function createNotFoundDirectoryListener(): Function {
forwardError: boolean,
): void {
if (forwardError) {
return next(createHttpError(404));
return next(createError(404));
}

next();
Expand All @@ -206,7 +210,7 @@ function createRedirectDirectoryListener(): Function {
): void {
if (fullPath.endsWith("/")) {
if (forwardError) {
return next(createHttpError(404));
return next(createError(404));
}

return next();
Expand Down
2 changes: 1 addition & 1 deletion src/response.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { contentDisposition } from "./utils/contentDisposition.ts";
import { stringify } from "./utils/stringify.ts";
import { encodeUrl } from "./utils/encodeUrl.ts";
import { normalizeType, normalizeTypes } from "./utils/normalizeType.ts";
import {
setCookie,
Expand All @@ -12,6 +11,7 @@ import {
basename,
contentType,
vary,
encodeUrl,
} from "../deps.ts";
import {
Response as DenoResponse,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/contentDisposition.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { encodeUrl } from "./encodeUrl.ts";
import { encodeUrl } from "../../deps.ts";

/**
* Creates a Content-Disposition Header value from
Expand Down
72 changes: 0 additions & 72 deletions src/utils/encodeUrl.ts

This file was deleted.

Loading

0 comments on commit 916ad4f

Please sign in to comment.