Skip to content

Commit

Permalink
Merge branch 'main' into pr/Mastercuber/80
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 2, 2023
2 parents 80326ea + ede3999 commit de47294
Show file tree
Hide file tree
Showing 17 changed files with 399 additions and 268 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ dist
types
.conf*
.idea/
test/cert
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ An elegant HTTP listener.

## Features

- Built-in CLI To run your applications with watch mode and typescript support (with [unjs/jiti](https://github.com/unjs/jiti))
- Built-in CLI To run your applications with watch mode and typescript support (with [unjs/jiti](https://github.com/unjs/jiti)) and serve static files
- Promisified interface for listening and closing server
- Work with express/connect or plain http handle function
- Support HTTP and HTTPS
Expand All @@ -27,18 +27,28 @@ An elegant HTTP listener.

You can run your applications in localhost with typescript support and watch mode using `listhen` CLI:

Create `app.ts`:
Create `index.ts`:

```ts
export default (req, res) => {
res.end("Hello World!");
};
```

Using [unjs/h3](https://github.com/unjs/h3):

```ts
import { createApp, eventHandler } from "h3";

export const app = createApp();

app.use("/", () => "Hello world!");
```

Use npx to invoke `listhen` command:

```sh
npx listhen -w ./app.ts
npx listhen -w ./index.ts
```

## Usage (API)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"consola": "^3.2.3",
"defu": "^6.1.2",
"get-port-please": "^3.0.1",
"h3": "^1.8.0-rc.2",
"http-shutdown": "^1.2.2",
"jiti": "^1.19.1",
"mlly": "^1.4.0",
Expand All @@ -58,7 +59,6 @@
"changelogen": "^0.5.4",
"eslint": "^8.46.0",
"eslint-config-unjs": "^0.2.1",
"h3": "^1.8.0-rc.2",
"ip-regex": "^5.0.0",
"prettier": "^3.0.0",
"typescript": "^5.1.6",
Expand Down
8 changes: 0 additions & 8 deletions playground/app.ts

This file was deleted.

10 changes: 7 additions & 3 deletions playground/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { toNodeListener } from "h3";
import { app } from "./app";
import { createApp, eventHandler } from "h3";

export default toNodeListener(app);
export const app = createApp();

app.use(
"/",
eventHandler(() => ({ hello: "world!" })),
);
Binary file added playground/public/favicon.ico
Binary file not shown.
1 change: 1 addition & 0 deletions playground/public/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Static asset works!
22 changes: 10 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 0 additions & 33 deletions src/_utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { networkInterfaces } from "node:os";
import { relative, resolve } from "node:path";
import { colors } from "consola/utils";
import { fileURLToPath } from "mlly";
import { isAbsolute } from "pathe";

export function getNetworkInterfaces(v4Only = true): string[] {
const addrs = new Set<string>();
Expand Down Expand Up @@ -39,33 +36,3 @@ export function formatURL(url: string) {
),
);
}

export async function createImporter(input: string, _cwd?: string) {
const cwd = resolve(_cwd ? fileURLToPath(_cwd) : ".");

const jiti = await import("jiti").then((r) => r.default || r);
const _jitiRequire = jiti(cwd, {
esmResolve: true,
requireCache: false,
interopDefault: true,
});

if (!isAbsolute(input) && !input.startsWith(".")) {
input = `./${input}`;
}

const entry = _jitiRequire.resolve(input);

const _import = () => {
const r = _jitiRequire(input);
return Promise.resolve(r.default || r);
};

return {
cwd,
relative: (path: string) => relative(cwd, path),
formateRelative: (path: string) => `\`./${relative(cwd, path)}\``,
entry,
import: _import,
};
}
28 changes: 15 additions & 13 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { resolve } from "node:path";
import { WatchOptions } from "node:fs";
import { defineCommand, runMain as _runMain } from "citty";
import { isAbsolute } from "pathe";
import { name, description, version } from "../package.json";
import { listen } from "./listen";
import { listenAndWatch } from "./watch";
import type { ListenOptions, WatchOptions } from "./types";
import { createImporter } from "./_utils";
import { listenAndWatch } from "./server";
import type { ListenOptions } from "./types";
import { DevServerOptions, createDevServer } from "./server/dev";

export const main = defineCommand({
meta: {
Expand Down Expand Up @@ -63,12 +64,8 @@ export const main = defineCommand({
},
},
async run({ args }) {
const cwd = resolve(args.cwd || ".");
process.chdir(cwd);

const opts: Partial<ListenOptions & WatchOptions> = {
const opts: Partial<ListenOptions & WatchOptions & DevServerOptions> = {
...args,
cwd,
port: args.port,
hostname: args.host,
clipboard: args.clipboard,
Expand All @@ -78,12 +75,17 @@ export const main = defineCommand({
https: args.https, // TODO: Support custom cert
};

const entry =
isAbsolute(args.entry) || args.entry.startsWith(".")
? args.entry
: `./${args.entry}`;

if (args.watch) {
await listenAndWatch(args.entry, opts);
await listenAndWatch(entry, opts);
} else {
const importer = await createImporter(args.entry);
const handler = await importer.import();
await listen(handler, opts);
const devServer = await createDevServer(entry, opts);
await listen(devServer.nodeListener, opts);
await devServer.reload(true);
}
},
});
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from "./listen";
export * from "./types";
export * from "./watch";
export * from "./server";
33 changes: 33 additions & 0 deletions src/server/_resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { relative } from "node:path";

export async function createResolver() {
const jiti = await import("jiti").then((r) => r.default || r);

const _jitiRequire = jiti(process.cwd(), {
cache: true,
esmResolve: true,
requireCache: false,
interopDefault: true,
});

const _import = (id: string) => {
const r = _jitiRequire(id);
return Promise.resolve(r.default || r);
};

const resolve = (id: string) => _jitiRequire.resolve(id);

const tryResolve = (id: string) => {
try {
return resolve(id);
} catch {}
};

return {
relative: (path: string) => relative(process.cwd(), path),
formateRelative: (path: string) => `\`./${relative(process.cwd(), path)}\``,
import: _import,
resolve,
tryResolve,
};
}
Loading

0 comments on commit de47294

Please sign in to comment.