From dc985da326254153e1948e77da6b4eb6214360b4 Mon Sep 17 00:00:00 2001 From: Ardalan Amini Date: Sun, 13 Nov 2022 23:09:32 +0330 Subject: [PATCH] chore: Move the Foxify demo to examples directory --- .changeset/config.json | 3 +- .changeset/good-rules-speak.md | 8 +++ benchmarks/benchmark/tsconfig.json | 2 - .../foxify/servers/foxify-with-middlewares.ts | 3 +- benchmarks/foxify/servers/foxify.ts | 3 +- benchmarks/foxify/tsconfig.json | 2 - benchmarks/fresh/suites/etag-several.ts | 4 +- benchmarks/fresh/suites/etag-single.ts | 4 +- benchmarks/fresh/suites/etag-star.ts | 4 +- benchmarks/fresh/suites/modified.ts | 4 +- benchmarks/fresh/suites/not-modified.ts | 4 +- benchmarks/fresh/tsconfig.json | 1 - benchmarks/router/routers/foxify.ts | 4 +- benchmarks/router/tsconfig.json | 1 - examples/foxify/README.md | 3 ++ examples/foxify/package.json | 22 ++++++++ .../src => examples/foxify/public}/style.css | 0 examples/foxify/src/index.ts | 38 +++++++++++++ examples/foxify/src/routes/index.ts | 48 +++++++++++++++++ examples/foxify/tsconfig.json | 7 +++ .../demo => examples/foxify}/views/footer.ejs | 0 .../demo => examples/foxify}/views/header.ejs | 2 +- .../demo => examples/foxify}/views/index.ejs | 4 +- jest.config.js | 4 +- package.json | 5 +- packages/config/package.json | 1 + packages/foxify/.npmignore | 1 - packages/foxify/demo/.env.example | 6 --- packages/foxify/demo/index.js | 31 ----------- packages/foxify/demo/routes/index.js | 48 ----------------- packages/foxify/package.json | 3 +- packages/foxify/src/Foxify.ts | 6 +-- packages/foxify/src/Server.ts | 6 +-- packages/foxify/src/constants/index.ts | 4 +- packages/foxify/src/index.ts | 4 +- packages/foxify/src/middlewares/index.ts | 2 +- packages/foxify/src/middlewares/init.ts | 2 +- packages/foxify/src/utils/index.ts | 4 +- packages/foxify/src/view/index.ts | 2 +- packages/fresh/package.json | 1 + packages/http/package.json | 1 + packages/http/src/Request.ts | 4 +- packages/http/src/Response.ts | 8 +-- packages/http/src/constants/index.ts | 8 +-- packages/http/src/errors/HttpError.ts | 2 +- packages/http/src/errors/MethodNotAllowed.ts | 4 +- packages/http/src/errors/NotFound.ts | 4 +- packages/http/src/errors/index.ts | 6 +-- packages/http/src/index.ts | 8 +-- packages/http/src/utils/accepts.ts | 2 +- packages/http/src/utils/index.ts | 12 ++--- packages/http/src/utils/vary.ts | 2 +- packages/http/src/view/index.ts | 2 +- packages/inject/package.json | 1 + packages/inject/src/index.ts | 4 +- packages/router/package.json | 1 + packages/router/src/Handlers.ts | 2 +- packages/router/src/Node.ts | 6 +-- packages/router/src/Options.ts | 2 +- packages/router/src/Router.ts | 6 +-- packages/router/src/constants.ts | 4 +- packages/router/src/index.ts | 4 +- packages/router/src/utils.ts | 4 +- pnpm-lock.yaml | 53 ++++++++++++++----- pnpm-workspace.yaml | 6 ++- tsconfig.cjs.json | 2 +- tsconfig.json | 2 +- 67 files changed, 258 insertions(+), 203 deletions(-) create mode 100644 .changeset/good-rules-speak.md create mode 100644 examples/foxify/README.md create mode 100644 examples/foxify/package.json rename {packages/foxify/demo/public/src => examples/foxify/public}/style.css (100%) create mode 100644 examples/foxify/src/index.ts create mode 100644 examples/foxify/src/routes/index.ts create mode 100644 examples/foxify/tsconfig.json rename {packages/foxify/demo => examples/foxify}/views/footer.ejs (100%) rename {packages/foxify/demo => examples/foxify}/views/header.ejs (71%) rename {packages/foxify/demo => examples/foxify}/views/index.ejs (76%) delete mode 100644 packages/foxify/demo/.env.example delete mode 100644 packages/foxify/demo/index.js delete mode 100644 packages/foxify/demo/routes/index.js diff --git a/.changeset/config.json b/.changeset/config.json index 224b4f1..3f59403 100644 --- a/.changeset/config.json +++ b/.changeset/config.json @@ -16,6 +16,7 @@ "@foxify/benchmark", "foxify-benchmarks", "fresh-benchmarks", - "router-benchmarks" + "router-benchmarks", + "foxify-example" ] } diff --git a/.changeset/good-rules-speak.md b/.changeset/good-rules-speak.md new file mode 100644 index 0000000..66a77e4 --- /dev/null +++ b/.changeset/good-rules-speak.md @@ -0,0 +1,8 @@ +--- +"foxify": patch +"@foxify/http": patch +"@foxify/inject": patch +"@foxify/router": patch +--- + +Fix module imports diff --git a/benchmarks/benchmark/tsconfig.json b/benchmarks/benchmark/tsconfig.json index 6911896..6cd41d6 100644 --- a/benchmarks/benchmark/tsconfig.json +++ b/benchmarks/benchmark/tsconfig.json @@ -1,9 +1,7 @@ { "extends": "../../tsconfig.json", "compilerOptions": { - "resolveJsonModule": true, "rootDir": ".", - "baseUrl": ".", "noEmit": true }, "files": [ diff --git a/benchmarks/foxify/servers/foxify-with-middlewares.ts b/benchmarks/foxify/servers/foxify-with-middlewares.ts index a9c0e0b..7c89c8c 100644 --- a/benchmarks/foxify/servers/foxify-with-middlewares.ts +++ b/benchmarks/foxify/servers/foxify-with-middlewares.ts @@ -11,8 +11,7 @@ import hsts from "hsts"; import ienoopen from "ienoopen"; import xXssProtection from "x-xss-protection"; -// eslint-disable-next-line new-cap -const app = (new Foxify.default); +const app = (new Foxify); app.disable("x-powered-by"); diff --git a/benchmarks/foxify/servers/foxify.ts b/benchmarks/foxify/servers/foxify.ts index 231485a..a401ca8 100644 --- a/benchmarks/foxify/servers/foxify.ts +++ b/benchmarks/foxify/servers/foxify.ts @@ -3,8 +3,7 @@ import { SchemaOptionsI } from "@foxify/router"; import Foxify from "foxify"; -// eslint-disable-next-line new-cap -const app = (new Foxify.default); +const app = (new Foxify); app.disable("x-powered-by"); diff --git a/benchmarks/foxify/tsconfig.json b/benchmarks/foxify/tsconfig.json index 40535cb..66981a5 100644 --- a/benchmarks/foxify/tsconfig.json +++ b/benchmarks/foxify/tsconfig.json @@ -1,9 +1,7 @@ { "extends": "../../tsconfig.json", "compilerOptions": { - "resolveJsonModule": true, "rootDir": ".", - "baseUrl": ".", "noEmit": true } } diff --git a/benchmarks/fresh/suites/etag-several.ts b/benchmarks/fresh/suites/etag-several.ts index 56292c2..5f41ad8 100644 --- a/benchmarks/fresh/suites/etag-several.ts +++ b/benchmarks/fresh/suites/etag-several.ts @@ -1,8 +1,6 @@ -import foxify from "@foxify/fresh"; +import foxifyFresh from "@foxify/fresh"; import fresh from "fresh"; -const foxifyFresh = foxify.default; - export default [ [ "@foxify/fresh", diff --git a/benchmarks/fresh/suites/etag-single.ts b/benchmarks/fresh/suites/etag-single.ts index fc1313c..ce07d25 100644 --- a/benchmarks/fresh/suites/etag-single.ts +++ b/benchmarks/fresh/suites/etag-single.ts @@ -1,8 +1,6 @@ -import foxify from "@foxify/fresh"; +import foxifyFresh from "@foxify/fresh"; import fresh from "fresh"; -const foxifyFresh = foxify.default; - export default [ [ "@foxify/fresh", diff --git a/benchmarks/fresh/suites/etag-star.ts b/benchmarks/fresh/suites/etag-star.ts index a4b8b82..85d28e5 100644 --- a/benchmarks/fresh/suites/etag-star.ts +++ b/benchmarks/fresh/suites/etag-star.ts @@ -1,8 +1,6 @@ -import foxify from "@foxify/fresh"; +import foxifyFresh from "@foxify/fresh"; import fresh from "fresh"; -const foxifyFresh = foxify.default; - export default [ [ "@foxify/fresh", diff --git a/benchmarks/fresh/suites/modified.ts b/benchmarks/fresh/suites/modified.ts index 4324325..1719ad4 100644 --- a/benchmarks/fresh/suites/modified.ts +++ b/benchmarks/fresh/suites/modified.ts @@ -1,8 +1,6 @@ -import foxify from "@foxify/fresh"; +import foxifyFresh from "@foxify/fresh"; import fresh from "fresh"; -const foxifyFresh = foxify.default; - export default [ [ "@foxify/fresh", diff --git a/benchmarks/fresh/suites/not-modified.ts b/benchmarks/fresh/suites/not-modified.ts index fb7cf1a..8b00103 100644 --- a/benchmarks/fresh/suites/not-modified.ts +++ b/benchmarks/fresh/suites/not-modified.ts @@ -1,8 +1,6 @@ -import foxify from "@foxify/fresh"; +import foxifyFresh from "@foxify/fresh"; import fresh from "fresh"; -const foxifyFresh = foxify.default; - export default [ [ "@foxify/fresh", diff --git a/benchmarks/fresh/tsconfig.json b/benchmarks/fresh/tsconfig.json index 68ff3af..66981a5 100644 --- a/benchmarks/fresh/tsconfig.json +++ b/benchmarks/fresh/tsconfig.json @@ -2,7 +2,6 @@ "extends": "../../tsconfig.json", "compilerOptions": { "rootDir": ".", - "baseUrl": ".", "noEmit": true } } diff --git a/benchmarks/router/routers/foxify.ts b/benchmarks/router/routers/foxify.ts index 022a2cc..899bf58 100644 --- a/benchmarks/router/routers/foxify.ts +++ b/benchmarks/router/routers/foxify.ts @@ -1,8 +1,6 @@ -import foxify from "@foxify/router"; +import Router from "@foxify/router"; import { routes, noop } from "./common.js"; -const Router = foxify.default; - const router = (new Router); routes.forEach((route) => { diff --git a/benchmarks/router/tsconfig.json b/benchmarks/router/tsconfig.json index 68ff3af..66981a5 100644 --- a/benchmarks/router/tsconfig.json +++ b/benchmarks/router/tsconfig.json @@ -2,7 +2,6 @@ "extends": "../../tsconfig.json", "compilerOptions": { "rootDir": ".", - "baseUrl": ".", "noEmit": true } } diff --git a/examples/foxify/README.md b/examples/foxify/README.md new file mode 100644 index 0000000..6300e9f --- /dev/null +++ b/examples/foxify/README.md @@ -0,0 +1,3 @@ +# Foxify Example + +> TODO diff --git a/examples/foxify/package.json b/examples/foxify/package.json new file mode 100644 index 0000000..5000d4b --- /dev/null +++ b/examples/foxify/package.json @@ -0,0 +1,22 @@ +{ + "name": "foxify-example", + "version": "0.0.0", + "private": true, + "description": "Foxify Benchmarks", + "type": "module", + "scripts": { + "start": "ts-node-esm src/index.ts" + }, + "dependencies": { + "@foxify/http": "workspace:^", + "@foxify/router": "workspace:^", + "foxify": "workspace:^", + "ejs": "^3.1.8", + "morgan": "^1.10.0" + }, + "devDependencies": { + "@types/ejs": "^3.1.1", + "@types/morgan": "^1.9.3", + "@types/node": "^18.11.9" + } +} diff --git a/packages/foxify/demo/public/src/style.css b/examples/foxify/public/style.css similarity index 100% rename from packages/foxify/demo/public/src/style.css rename to examples/foxify/public/style.css diff --git a/examples/foxify/src/index.ts b/examples/foxify/src/index.ts new file mode 100644 index 0000000..364fd11 --- /dev/null +++ b/examples/foxify/src/index.ts @@ -0,0 +1,38 @@ +import path from "node:path"; +import url from "node:url"; +import Foxify from "foxify"; +import morgan from "morgan"; +import routes from "./routes/index.js"; + +const ROOT_DIR = path.join(path.dirname(url.fileURLToPath(import.meta.url)), ".."); + +const app = (new Foxify); + +// Settings +app + .set("url", "localhost") + .set("port", 3000) + .set("workers", 1); + +// Template engine support +app.engine( + "ejs", + path.join(ROOT_DIR, "views"), + // eslint-disable-next-line no-underscore-dangle,@typescript-eslint/no-explicit-any + (await import("ejs") as any).__express, +); + +// Static serve support +app.use(Foxify.static(path.join(ROOT_DIR, "public"))); + +// Express middleware support +app.use(morgan("dev")); + +// Routes +app.use(routes); + +// Log the routes +console.info(app.prettyPrint()); + +// Start the app +app.start(() => console.info(`Foxify server running at http://${ app.setting("url") }:${ app.setting("port") } (worker: ${ process.pid })`)); diff --git a/examples/foxify/src/routes/index.ts b/examples/foxify/src/routes/index.ts new file mode 100644 index 0000000..cc9cf3d --- /dev/null +++ b/examples/foxify/src/routes/index.ts @@ -0,0 +1,48 @@ +import { STATUS, HttpError } from "@foxify/http"; +import Router, { SchemaOptionsI } from "@foxify/router"; + +const router = (new Router); + +router.get("/", (req, res) => { + res.render("index", { + logo : "https://avatars1.githubusercontent.com/u/36167886?s=200&v=4", + title: "Foxify", + }); +}); + +router.get("/greet", (req, res) => { + res.json({ + hello: "world", + }); +}); + +const schema: SchemaOptionsI = { + response: { + 200: { + type : "object", + properties: { + hello: { + type: "string", + }, + }, + }, + }, +}; + +router.get("/greet-fast", { + schema, +}, (req, res) => { + res.json({ + hello: "world", + }); +}); + +router.get("/404", () => { + throw new HttpError("This is a demo", STATUS.NOT_FOUND); +}); + +router.get("/error", async () => { + throw new Error("Oops!"); +}); + +export default router; diff --git a/examples/foxify/tsconfig.json b/examples/foxify/tsconfig.json new file mode 100644 index 0000000..66981a5 --- /dev/null +++ b/examples/foxify/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "rootDir": ".", + "noEmit": true + } +} diff --git a/packages/foxify/demo/views/footer.ejs b/examples/foxify/views/footer.ejs similarity index 100% rename from packages/foxify/demo/views/footer.ejs rename to examples/foxify/views/footer.ejs diff --git a/packages/foxify/demo/views/header.ejs b/examples/foxify/views/header.ejs similarity index 71% rename from packages/foxify/demo/views/header.ejs rename to examples/foxify/views/header.ejs index 9f66e09..d7ab469 100644 --- a/packages/foxify/demo/views/header.ejs +++ b/examples/foxify/views/header.ejs @@ -6,7 +6,7 @@ <%= title %> - + diff --git a/packages/foxify/demo/views/index.ejs b/examples/foxify/views/index.ejs similarity index 76% rename from packages/foxify/demo/views/index.ejs rename to examples/foxify/views/index.ejs index 0415f97..e87a842 100644 --- a/packages/foxify/demo/views/index.ejs +++ b/examples/foxify/views/index.ejs @@ -1,4 +1,4 @@ -<% include header.ejs %> +<%- include('header') -%>
@@ -8,4 +8,4 @@
-<% include footer.ejs %> +<%- include('footer') -%> diff --git a/jest.config.js b/jest.config.js index 1e3862c..82c1cdf 100644 --- a/jest.config.js +++ b/jest.config.js @@ -92,7 +92,9 @@ module.exports = { // A map from regular expressions to module names or to arrays of module names that allow to stub out resources // with a single module - // moduleNameMapper: {}, + moduleNameMapper: { + "^(\\.{1,2}/.*)\\.js$": "$1", + }, // An array of regexp pattern strings, // matched against all module paths before considered 'visible' to the module loader diff --git a/package.json b/package.json index 2d32487..dde00b6 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,8 @@ "typescript": "^4.8.4" }, "workspaces": [ - "packages/*", - "benchmarks/*" + "benchmarks/*", + "examples/*", + "packages/*" ] } diff --git a/packages/config/package.json b/packages/config/package.json index a2aa33f..a4dade8 100644 --- a/packages/config/package.json +++ b/packages/config/package.json @@ -39,6 +39,7 @@ "build:pre": "ts-node-esm ../../scripts/pre-build.mts", "build:post": "ts-node-esm ../../scripts/post-build.mts" }, + "type": "module", "engines": { "node": ">=16" }, diff --git a/packages/foxify/.npmignore b/packages/foxify/.npmignore index b3e0c1a..187ce7a 100644 --- a/packages/foxify/.npmignore +++ b/packages/foxify/.npmignore @@ -1,5 +1,4 @@ tests/ coverage/ -demo/ tsconfig*.json *.tsbuildinfo diff --git a/packages/foxify/demo/.env.example b/packages/foxify/demo/.env.example deleted file mode 100644 index b5e2569..0000000 --- a/packages/foxify/demo/.env.example +++ /dev/null @@ -1,6 +0,0 @@ -NODE_ENV=development - -APP_URL=localhost -APP_PORT=3000 - -WORKERS=1 diff --git a/packages/foxify/demo/index.js b/packages/foxify/demo/index.js deleted file mode 100644 index c301527..0000000 --- a/packages/foxify/demo/index.js +++ /dev/null @@ -1,31 +0,0 @@ -/* eslint-disable @typescript-eslint/no-require-imports,@typescript-eslint/no-var-requires */ -const path = require("path"); -const morgan = require("morgan"); -const Foxify = require("..").default; -const routes = require("./routes"); - -Foxify.dotenv(path.join(__dirname, ".env")); - -const app = (new Foxify); - -// Template engine support -// eslint-disable-next-line no-underscore-dangle -app.engine("ejs", path.join(__dirname, "views"), require("ejs").__express); - -// Middlewares & routes -app.use( - - // Static serve support - Foxify.static(path.join(__dirname, "public")), - - // Express middleware support - morgan("dev"), - - // Routes - routes, -); - -// Start the app -app.start(() => console.info(`Foxify server running at http://${ app.get("url") }:${ app.get("port") } (worker: ${ process.pid })`)); - -console.info(app.prettyPrint()); diff --git a/packages/foxify/demo/routes/index.js b/packages/foxify/demo/routes/index.js deleted file mode 100644 index 40bb174..0000000 --- a/packages/foxify/demo/routes/index.js +++ /dev/null @@ -1,48 +0,0 @@ -// eslint-disable-next-line @typescript-eslint/no-require-imports,@typescript-eslint/no-var-requires -const { Router, HttpException, HTTP } = require("../.."); - -const routes = (new Router); - -routes.get("/", (req, res) => { - res.render("index", { - logo : "https://avatars1.githubusercontent.com/u/36167886?s=200&v=4", - title: "Foxify", - }); -}); - -routes.get("/greet", (req, res) => { - res.json({ - hello: "world", - }); -}); - -const schema = { - response: { - 200: { - type : "object", - properties: { - hello: { - type: "string", - }, - }, - }, - }, -}; - -routes.get("/greet-fast", { - schema, -}, (req, res) => { - res.json({ - hello: "world", - }); -}); - -routes.get("/404", (req, res) => { - throw new HttpException("This is a demo", HTTP.NOT_FOUND); -}); - -routes.get("/error", async (req, res) => { - throw new Error("Oops!"); -}); - -module.exports = routes; diff --git a/packages/foxify/package.json b/packages/foxify/package.json index 07d0ca0..8bc3e71 100644 --- a/packages/foxify/package.json +++ b/packages/foxify/package.json @@ -53,6 +53,7 @@ "build:pre": "ts-node-esm ../../scripts/pre-build.mts", "build:post": "ts-node-esm ../../scripts/post-build.mts" }, + "type": "module", "engines": { "node": ">=16" }, @@ -94,8 +95,6 @@ "@types/safe-regex": "^1.1.4", "cookie": "^0.5.0", "cookie-parser": "^1.4.6", - "ejs": "^3.1.8", - "morgan": "^1.10.0", "prototyped.js": "^1.0.1" }, "publishConfig": { diff --git a/packages/foxify/src/Foxify.ts b/packages/foxify/src/Foxify.ts index dd7ceae..283266c 100644 --- a/packages/foxify/src/Foxify.ts +++ b/packages/foxify/src/Foxify.ts @@ -10,9 +10,9 @@ import Router from "@foxify/router"; import * as proxyAddr from "proxy-addr"; import * as qs from "qs"; import serveStatic from "serve-static"; -import Server from "./Server"; -import * as utils from "./utils"; -import { Engine } from "./view"; +import Server from "./Server.js"; +import * as utils from "./utils/index.js"; +import { Engine } from "./view/index.js"; const SETTINGS: Array = [ "env", diff --git a/packages/foxify/src/Server.ts b/packages/foxify/src/Server.ts index 99208af..9b3d818 100644 --- a/packages/foxify/src/Server.ts +++ b/packages/foxify/src/Server.ts @@ -7,9 +7,9 @@ import { Response, responseSettings, } from "@foxify/http"; -import { Engine } from "./view"; +import { Engine } from "./view/index.js"; // eslint-disable-next-line @typescript-eslint/consistent-type-imports -import type Foxify from "."; +import type Foxify from "./index.js"; // eslint-disable-next-line @typescript-eslint/no-namespace namespace Server { @@ -107,4 +107,4 @@ class Server { } -export = Server; +export default Server; diff --git a/packages/foxify/src/constants/index.ts b/packages/foxify/src/constants/index.ts index 564be5d..f3b4a57 100644 --- a/packages/foxify/src/constants/index.ts +++ b/packages/foxify/src/constants/index.ts @@ -1,2 +1,2 @@ -export { default as HTTP } from "./HTTP"; -export { default as METHOD } from "./METHOD"; +export { default as HTTP } from "./HTTP.js"; +export { default as METHOD } from "./METHOD.js"; diff --git a/packages/foxify/src/index.ts b/packages/foxify/src/index.ts index ce333ee..73a87a2 100644 --- a/packages/foxify/src/index.ts +++ b/packages/foxify/src/index.ts @@ -1,2 +1,2 @@ -export * from "./constants"; -export { default } from "./Foxify"; +export * from "./constants/index.js"; +export { default } from "./Foxify.js"; diff --git a/packages/foxify/src/middlewares/index.ts b/packages/foxify/src/middlewares/index.ts index d59a274..f8248c5 100644 --- a/packages/foxify/src/middlewares/index.ts +++ b/packages/foxify/src/middlewares/index.ts @@ -1 +1 @@ -export { default as init } from "./init"; +export { default as init } from "./init.js"; diff --git a/packages/foxify/src/middlewares/init.ts b/packages/foxify/src/middlewares/init.ts index 9e56ee4..520b5b1 100644 --- a/packages/foxify/src/middlewares/init.ts +++ b/packages/foxify/src/middlewares/init.ts @@ -1,5 +1,5 @@ import { HandlerT } from "@foxify/router"; -import Foxify from ".."; +import Foxify from "../index.js"; const init = (app: Foxify): HandlerT | null => { if (app.disabled("x-powered-by")) return null; diff --git a/packages/foxify/src/utils/index.ts b/packages/foxify/src/utils/index.ts index a3b638b..35275fb 100644 --- a/packages/foxify/src/utils/index.ts +++ b/packages/foxify/src/utils/index.ts @@ -1,8 +1,8 @@ import etag from "etag"; -export * from "./assertions"; +export * from "./assertions.js"; -export { default as decodeURIComponent } from "./fast-decode-uri-component"; +export { default as decodeURIComponent } from "./fast-decode-uri-component.js"; export const isHandler = (arg: unknown): boolean => typeof arg === "function" || arg instanceof Promise; diff --git a/packages/foxify/src/view/index.ts b/packages/foxify/src/view/index.ts index 0c07c4f..d6c6206 100644 --- a/packages/foxify/src/view/index.ts +++ b/packages/foxify/src/view/index.ts @@ -1 +1 @@ -export { default as Engine } from "./Engine"; +export { default as Engine } from "./Engine.js"; diff --git a/packages/fresh/package.json b/packages/fresh/package.json index 297528c..0ebcdb6 100644 --- a/packages/fresh/package.json +++ b/packages/fresh/package.json @@ -39,6 +39,7 @@ "build:pre": "ts-node-esm ../../scripts/pre-build.mts", "build:post": "ts-node-esm ../../scripts/post-build.mts" }, + "type": "module", "engines": { "node": ">=16" }, diff --git a/packages/http/package.json b/packages/http/package.json index 5e4d6f7..6d70803 100644 --- a/packages/http/package.json +++ b/packages/http/package.json @@ -41,6 +41,7 @@ "build:pre": "ts-node-esm ../../scripts/pre-build.mts", "build:post": "ts-node-esm ../../scripts/post-build.mts" }, + "type": "module", "engines": { "node": ">=16" }, diff --git a/packages/http/src/Request.ts b/packages/http/src/Request.ts index cc66a0f..7d9728d 100644 --- a/packages/http/src/Request.ts +++ b/packages/http/src/Request.ts @@ -4,14 +4,14 @@ import { UrlWithStringQuery } from "url"; import proxyAddr, { compile as proxyAddrCompile, all as proxyAddrAll } from "proxy-addr"; import * as qs from "qs"; import typeIs from "type-is"; -import { MethodT, ProtocolT } from "./constants"; +import { MethodT, ProtocolT } from "./constants/index.js"; import { Accepts, parseUrl, RANGE_PARSER_RESULT, rangeParser, RangeParserRangesI, -} from "./utils"; +} from "./utils/index.js"; // eslint-disable-next-line import/exports-last export const DEFAULT_SETTINGS: SettingsI = { diff --git a/packages/http/src/Response.ts b/packages/http/src/Response.ts index 93d6126..2de52cd 100644 --- a/packages/http/src/Response.ts +++ b/packages/http/src/Response.ts @@ -9,10 +9,10 @@ import { sign } from "cookie-signature"; import escapeHtml from "escape-html"; import onFinished from "on-finished"; import send, { mime as sendMime } from "send"; -import Request from "./Request"; -import { JsonT, METHOD, STATUS, StatusT, StringifyT } from "./constants"; -import { createETagGenerator, encodeUrl, vary } from "./utils"; -import { CallbackT as EngineCallbackT, Engine } from "./view"; +import Request from "./Request.js"; +import { JsonT, METHOD, STATUS, StatusT, StringifyT } from "./constants/index.js"; +import { createETagGenerator, encodeUrl, vary } from "./utils/index.js"; +import { CallbackT as EngineCallbackT, Engine } from "./view/index.js"; /** * Set the charset in a given Content-Type string. diff --git a/packages/http/src/constants/index.ts b/packages/http/src/constants/index.ts index 3a3284d..2f18eae 100644 --- a/packages/http/src/constants/index.ts +++ b/packages/http/src/constants/index.ts @@ -1,4 +1,4 @@ -export * from "./common"; -export * from "./METHOD"; -export * from "./PROTOCOL"; -export * from "./STATUS"; +export * from "./common.js"; +export * from "./METHOD.js"; +export * from "./PROTOCOL.js"; +export * from "./STATUS.js"; diff --git a/packages/http/src/errors/HttpError.ts b/packages/http/src/errors/HttpError.ts index 0adf27f..22f5484 100644 --- a/packages/http/src/errors/HttpError.ts +++ b/packages/http/src/errors/HttpError.ts @@ -1,5 +1,5 @@ import { STATUS_CODES } from "http"; -import { STATUS, StatusT } from "../constants"; +import { STATUS, StatusT } from "../constants/index.js"; export default class HttpError extends Error { diff --git a/packages/http/src/errors/MethodNotAllowed.ts b/packages/http/src/errors/MethodNotAllowed.ts index 73bae82..c50c021 100644 --- a/packages/http/src/errors/MethodNotAllowed.ts +++ b/packages/http/src/errors/MethodNotAllowed.ts @@ -1,6 +1,6 @@ import { STATUS_CODES } from "http"; -import { STATUS } from "../constants"; -import HttpError from "./HttpError"; +import { STATUS } from "../constants/index.js"; +import HttpError from "./HttpError.js"; export default class MethodNotAllowed extends HttpError { diff --git a/packages/http/src/errors/NotFound.ts b/packages/http/src/errors/NotFound.ts index f89d1e8..3fe1afa 100644 --- a/packages/http/src/errors/NotFound.ts +++ b/packages/http/src/errors/NotFound.ts @@ -1,6 +1,6 @@ import { STATUS_CODES } from "http"; -import { STATUS } from "../constants"; -import HttpError from "./HttpError"; +import { STATUS } from "../constants/index.js"; +import HttpError from "./HttpError.js"; export default class NotFound extends HttpError { diff --git a/packages/http/src/errors/index.ts b/packages/http/src/errors/index.ts index 04122b0..9eceadf 100644 --- a/packages/http/src/errors/index.ts +++ b/packages/http/src/errors/index.ts @@ -1,3 +1,3 @@ -export { default as HttpError } from "./HttpError"; -export { default as MethodNotAllowed } from "./MethodNotAllowed"; -export { default as NotFound } from "./NotFound"; +export { default as HttpError } from "./HttpError.js"; +export { default as MethodNotAllowed } from "./MethodNotAllowed.js"; +export { default as NotFound } from "./NotFound.js"; diff --git a/packages/http/src/index.ts b/packages/http/src/index.ts index 3cac461..f0343e9 100644 --- a/packages/http/src/index.ts +++ b/packages/http/src/index.ts @@ -1,16 +1,16 @@ -export * from "./constants"; -export * from "./errors"; +export * from "./constants/index.js"; +export * from "./errors/index.js"; export { default as Request, settings as requestSettings, DEFAULT_SETTINGS as REQUEST_DEFAULT_SETTINGS, type HeadersI as RequestHeadersI, type SettingsI as RequestSettingsI, -} from "./Request"; +} from "./Request.js"; export { default as Response, settings as responseSettings, DEFAULT_SETTINGS as RESPONSE_DEFAULT_SETTINGS, type HeadersI as ResponseHeadersI, type SettingsI as ResponseSettingsI, -} from "./Response"; +} from "./Response.js"; diff --git a/packages/http/src/utils/accepts.ts b/packages/http/src/utils/accepts.ts index ef03a8c..74daa89 100644 --- a/packages/http/src/utils/accepts.ts +++ b/packages/http/src/utils/accepts.ts @@ -34,7 +34,7 @@ import * as mime from "mime-types"; import Negotiator from "negotiator"; // eslint-disable-next-line @typescript-eslint/consistent-type-imports -import type { default as Request, HeadersI } from "../Request"; +import type { default as Request, HeadersI } from "../Request.js"; /** * Convert extnames to mime. diff --git a/packages/http/src/utils/index.ts b/packages/http/src/utils/index.ts index 8206a73..ded4207 100644 --- a/packages/http/src/utils/index.ts +++ b/packages/http/src/utils/index.ts @@ -1,11 +1,11 @@ -export { default as Accepts } from "./accepts"; -export { default as encodeUrl } from "./encode-url"; -export { createETagGenerator } from "./etag"; -export { default as parseUrl } from "./parse-url"; +export { default as Accepts } from "./accepts.js"; +export { default as encodeUrl } from "./encode-url.js"; +export { createETagGenerator } from "./etag.js"; +export { default as parseUrl } from "./parse-url.js"; export { default as rangeParser, type RangesI as RangeParserRangesI, type RangeI as RangeParserRangeI, RESULT as RANGE_PARSER_RESULT, -} from "./range-parser"; -export { default as vary } from "./vary"; +} from "./range-parser.js"; +export { default as vary } from "./vary.js"; diff --git a/packages/http/src/utils/vary.ts b/packages/http/src/utils/vary.ts index 2c4e42f..9e0ec8e 100644 --- a/packages/http/src/utils/vary.ts +++ b/packages/http/src/utils/vary.ts @@ -29,7 +29,7 @@ import assert from "assert"; // eslint-disable-next-line @typescript-eslint/consistent-type-imports -import type Response from "../Response"; +import type Response from "../Response.js"; /** * RegExp to match field-name in RFC 7230 sec 3.2 diff --git a/packages/http/src/view/index.ts b/packages/http/src/view/index.ts index 8a96539..3eace5a 100644 --- a/packages/http/src/view/index.ts +++ b/packages/http/src/view/index.ts @@ -1 +1 @@ -export { default as Engine, type CallbackT } from "./Engine"; +export { default as Engine, type CallbackT } from "./Engine.js"; diff --git a/packages/inject/package.json b/packages/inject/package.json index ccf1e84..c7ef2c3 100644 --- a/packages/inject/package.json +++ b/packages/inject/package.json @@ -40,6 +40,7 @@ "build:pre": "ts-node-esm ../../scripts/pre-build.mts", "build:post": "ts-node-esm ../../scripts/post-build.mts" }, + "type": "module", "engines": { "node": ">=16" }, diff --git a/packages/inject/src/index.ts b/packages/inject/src/index.ts index 19ed994..34a3659 100644 --- a/packages/inject/src/index.ts +++ b/packages/inject/src/index.ts @@ -10,7 +10,7 @@ import { Writable } from "readable-stream"; // eslint-disable-next-line @typescript-eslint/ban-ts-comment,@typescript-eslint/prefer-ts-expect-error // @ts-ignore -import { version } from "@foxify/inject/package.json"; +import pkg from "@foxify/inject/package.json" assert { type: "json" }; function getNullSocket(): Writable { return new Writable({ @@ -74,7 +74,7 @@ export default async function inject< // Readonly req.headers = headers; - req.headers["user-agent"] = req.headers["user-agent"] ?? `foxify-inject/${ version }`; + req.headers["user-agent"] = req.headers["user-agent"] ?? `foxify-inject/${ pkg.version }`; const hostHeaderFromUri = (): string | null => { if (uri.port) return uri.host; diff --git a/packages/router/package.json b/packages/router/package.json index 284eb64..6d84584 100644 --- a/packages/router/package.json +++ b/packages/router/package.json @@ -41,6 +41,7 @@ "build:pre": "ts-node-esm ../../scripts/pre-build.mts", "build:post": "ts-node-esm ../../scripts/post-build.mts" }, + "type": "module", "engines": { "node": ">=16" }, diff --git a/packages/router/src/Handlers.ts b/packages/router/src/Handlers.ts index cfc1689..ebfecbe 100644 --- a/packages/router/src/Handlers.ts +++ b/packages/router/src/Handlers.ts @@ -1,5 +1,5 @@ import { Request as RequestT, Response as ResponseT } from "@foxify/http"; -import { NodeHandlersT } from "./constants"; +import { NodeHandlersT } from "./constants.js"; export default class Handlers implements NodeHandlersT { diff --git a/packages/router/src/Node.ts b/packages/router/src/Node.ts index 8b5388e..53683ac 100644 --- a/packages/router/src/Node.ts +++ b/packages/router/src/Node.ts @@ -5,8 +5,8 @@ import { StatusT, } from "@foxify/http"; import fastJson from "fast-json-stringify"; -import Handlers from "./Handlers"; -import Options from "./Options"; +import Handlers from "./Handlers.js"; +import Options from "./Options.js"; import { HandlersResultT, HandlerT, @@ -19,7 +19,7 @@ import { PARAM_LABEL, ParamsT, WILDCARD_LABEL, -} from "./constants"; +} from "./constants.js"; interface Node { diff --git a/packages/router/src/Options.ts b/packages/router/src/Options.ts index 8daa078..8e1b4f9 100644 --- a/packages/router/src/Options.ts +++ b/packages/router/src/Options.ts @@ -1,4 +1,4 @@ -import { NodeOptionsT } from "./constants"; +import { NodeOptionsT } from "./constants.js"; export default class Options implements NodeOptionsT { diff --git a/packages/router/src/Router.ts b/packages/router/src/Router.ts index d8c60cf..30cca62 100644 --- a/packages/router/src/Router.ts +++ b/packages/router/src/Router.ts @@ -13,7 +13,7 @@ import { } from "@foxify/http"; import escapeHtml from "escape-html"; import { array } from "prototyped.js"; -import Node from "./Node"; +import Node from "./Node.js"; import { EMPTY_RESULT, ErrorHandlersT, @@ -32,13 +32,13 @@ import { RoutesT, ShortHandRouteT, WILDCARD_LABEL, -} from "./constants"; +} from "./constants.js"; import { assignMatchAllNode, assignParamNode, prettyPrint, routes, -} from "./utils"; +} from "./utils.js"; const { compact, deepFlatten } = array; diff --git a/packages/router/src/constants.ts b/packages/router/src/constants.ts index 13d2425..9bd0db8 100644 --- a/packages/router/src/constants.ts +++ b/packages/router/src/constants.ts @@ -7,9 +7,9 @@ import { } from "@foxify/http"; import { Schema as JsonSchemaT } from "fast-json-stringify"; // eslint-disable-next-line @typescript-eslint/consistent-type-imports -import type NodeT from "./Node"; +import type NodeT from "./Node.js"; // eslint-disable-next-line @typescript-eslint/consistent-type-imports -import type RouterT from "./Router"; +import type RouterT from "./Router.js"; export const PARAM_LABEL = ":" as const; diff --git a/packages/router/src/index.ts b/packages/router/src/index.ts index 5b1b2aa..1f2e8fa 100644 --- a/packages/router/src/index.ts +++ b/packages/router/src/index.ts @@ -1,2 +1,2 @@ -export * from "./constants"; -export { default } from "./Router"; +export * from "./constants.js"; +export { default } from "./Router.js"; diff --git a/packages/router/src/utils.ts b/packages/router/src/utils.ts index d6ffe7a..a617c16 100644 --- a/packages/router/src/utils.ts +++ b/packages/router/src/utils.ts @@ -3,8 +3,8 @@ import { Request as RequestT, Response as ResponseT, } from "@foxify/http"; -import Node from "./Node"; -import { RoutesT } from "./constants"; +import Node from "./Node.js"; +import { RoutesT } from "./constants.js"; // eslint-disable-next-line @typescript-eslint/no-shadow function color(string: string, color: COLOR): string { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7767a4e..2cf8032 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -186,6 +186,27 @@ importers: '@types/hapi__call': 9.0.0 '@types/koa-router': 7.4.4 + examples/foxify: + specifiers: + '@foxify/http': workspace:^ + '@foxify/router': workspace:^ + '@types/ejs': ^3.1.1 + '@types/morgan': ^1.9.3 + '@types/node': ^18.11.9 + ejs: ^3.1.8 + foxify: workspace:^ + morgan: ^1.10.0 + dependencies: + '@foxify/http': link:../../packages/http + '@foxify/router': link:../../packages/router + ejs: 3.1.8 + foxify: link:../../packages/foxify + morgan: 1.10.0 + devDependencies: + '@types/ejs': 3.1.1 + '@types/morgan': 1.9.3 + '@types/node': 18.11.9 + packages/config: specifiers: joi: ^17.7.0 @@ -208,11 +229,9 @@ importers: cookie: ^0.5.0 cookie-parser: ^1.4.6 dotenv: ^16.0.3 - ejs: ^3.1.8 escape-html: ^1.0.3 etag: ^1.8.1 fast-json-stringify: ^5.4.0 - morgan: ^1.10.0 prototyped.js: ^1.0.1 proxy-addr: ^2.0.7 qs: ^6.11.0 @@ -241,8 +260,6 @@ importers: '@types/safe-regex': 1.1.4 cookie: 0.5.0 cookie-parser: 1.4.6 - ejs: 3.1.8 - morgan: 1.10.0 prototyped.js: 1.0.1 packages/fresh: @@ -1778,6 +1795,10 @@ packages: resolution: {integrity: sha512-vt+kDhq/M2ayberEtJcIN/hxXy1Pk+59g2FV/ZQceeaTyCtCucjL2Q7FXlFjtWn4n15KCr1NE2lNNFhp0lEThw==} dev: true + /@types/ejs/3.1.1: + resolution: {integrity: sha512-RQul5wEfY7BjWm0sYY86cmUN/pcXWGyVxWX93DFFJvcrxax5zKlieLwA3T77xJGwNcZW0YW6CYG70p1m8xPFmA==} + dev: true + /@types/escape-html/1.0.2: resolution: {integrity: sha512-gaBLT8pdcexFztLSPRtriHeXY/Kn4907uOCZ4Q3lncFBkheAWOuNt53ypsF8szgxbEJ513UeBzcf4utN0EzEwA==} dev: true @@ -1980,6 +2001,12 @@ packages: resolution: {integrity: sha512-jhuKLIRrhvCPLqwPcx6INqmKeiA5EWrsCOPhrlFSrbrmU4ZMPjj5Ul/oLCMDO98XRUIwVm78xICz4EPCektzeQ==} dev: true + /@types/morgan/1.9.3: + resolution: {integrity: sha512-BiLcfVqGBZCyNCnCH3F4o2GmDLrpy0HeBVnNlyZG4fo88ZiE9SoiBe3C+2ezuwbjlEyT+PDZ17//TAlRxAn75Q==} + dependencies: + '@types/node': 18.11.9 + dev: true + /@types/negotiator/0.6.1: resolution: {integrity: sha512-c4mvXFByghezQ/eVGN5HvH/jI63vm3B7FiE81BUzDAWmuiohRecCO6ddU60dfq29oKUMiQujsoB2h0JQC7JHKA==} dev: true @@ -2454,7 +2481,7 @@ packages: /async/3.2.4: resolution: {integrity: sha512-iAB+JbDEGXhyIUavoDl9WP/Jj106Kz9DEn1DPgYw5ruDn0e3Wgi3sKFm55sASdGBNOQB8F59d9qQ7deqrHA8wQ==} - dev: true + dev: false /asynckit/0.4.0: resolution: {integrity: sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==} @@ -2588,7 +2615,7 @@ packages: engines: {node: '>= 0.8'} dependencies: safe-buffer: 5.1.2 - dev: true + dev: false /bcrypt-pbkdf/1.0.2: resolution: {integrity: sha512-qeFIXtP4MSoi6NLqO12WfqARWWuCKi2Rn/9hJLEmtB5yTNr9DqFWkJRCf2qShWzPeAMRnOgCrq0sg/KLv5ES9w==} @@ -2656,7 +2683,6 @@ packages: resolution: {integrity: sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==} dependencies: balanced-match: 1.0.0 - dev: true /braces/3.0.2: resolution: {integrity: sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==} @@ -2782,7 +2808,6 @@ packages: dependencies: ansi-styles: 4.3.0 supports-color: 7.2.0 - dev: true /chalk/4.1.2: resolution: {integrity: sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==} @@ -3302,7 +3327,7 @@ packages: hasBin: true dependencies: jake: 10.8.5 - dev: true + dev: false /electron-to-chromium/1.4.284: resolution: {integrity: sha512-M8WEXFuKXMYMVr45fo8mq0wUrrJHheiKZf6BArTKk9ZBYCKJEOU5H8cdWgDT+qCVZf7Na4lVUaZsA+h6uA9+PA==} @@ -3912,7 +3937,7 @@ packages: resolution: {integrity: sha512-w1cEuf3S+DrLCQL7ET6kz+gmlJdbq9J7yXCSjK/OZCPA+qEN1WyF4ZAf0YYJa4/shHJra2t/d/r8SV4Ji+x+8Q==} dependencies: minimatch: 5.1.0 - dev: true + dev: false /fill-range/7.0.1: resolution: {integrity: sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==} @@ -4750,7 +4775,7 @@ packages: chalk: 4.1.0 filelist: 1.0.4 minimatch: 3.1.2 - dev: true + dev: false /jest-changed-files/29.2.0: resolution: {integrity: sha512-qPVmLLyBmvF5HJrY7krDisx6Voi8DmlV3GZYX0aFNbaQsZeoz1hfxcCMbqDGuQCxU1dJy9eYc2xscE8QrCCYaA==} @@ -5670,7 +5695,6 @@ packages: engines: {node: '>=10'} dependencies: brace-expansion: 2.0.1 - dev: true /minimist-options/4.1.0: resolution: {integrity: sha512-Q4r8ghd80yhO/0j1O3B2BjweX3fiHg9cdOwjJd2J76Q135c+NDxGCqdYKQ1SKBuFfgWbAUzBfvYjPUEeNgqN1A==} @@ -5713,7 +5737,7 @@ packages: on-headers: 1.0.2 transitivePeerDependencies: - supports-color - dev: true + dev: false /ms/2.0.0: resolution: {integrity: sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==} @@ -5877,6 +5901,7 @@ packages: engines: {node: '>= 0.8'} dependencies: ee-first: 1.1.1 + dev: false /on-finished/2.4.1: resolution: {integrity: sha512-oVlzkg3ENAhCk2zdv7IJwd/QUD4z2RxRwpkcGY8psCVcCYZNq4wYnVWALHM+brtuJjePWiYF/ClmuDr8Ch5+kg==} @@ -5887,7 +5912,7 @@ packages: /on-headers/1.0.2: resolution: {integrity: sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA==} engines: {node: '>= 0.8'} - dev: true + dev: false /on-net-listen/1.1.2: resolution: {integrity: sha512-y1HRYy8s/RlcBvDUwKXSmkODMdx4KSuIvloCnQYJ2LdBBC1asY4HtfhXwe3UWknLakATZDnbzht2Ijw3M1EqFg==} diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 4c519b9..38fdafd 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -1,5 +1,7 @@ packages: - # all packages in direct subdirectories of packages/ - - 'packages/*' # all packages in direct subdirectories of benchmarks/ - 'benchmarks/*' + # all packages in direct subdirectories of examples/ + - 'examples/*' + # all packages in direct subdirectories of packages/ + - 'packages/*' diff --git a/tsconfig.cjs.json b/tsconfig.cjs.json index c4a9687..5b34dd6 100644 --- a/tsconfig.cjs.json +++ b/tsconfig.cjs.json @@ -1,7 +1,7 @@ { "extends": "./tsconfig.json", "compilerOptions": { - "module": "Node16", + "module": "CommonJS", "moduleResolution": "Node16", } } diff --git a/tsconfig.json b/tsconfig.json index d1533e6..f970b0f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -33,7 +33,7 @@ // "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */ // "types": [], /* Specify type package names to be included without being referenced in a source file. */ // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ - // "resolveJsonModule": true, /* Enable importing .json files */ + "resolveJsonModule": true, /* Enable importing .json files */ // "noResolve": true, /* Disallow `import`s, `require`s or ``s from expanding the number of files TypeScript should add to a project. */ /* JavaScript Support */