Skip to content

Commit

Permalink
refactor: TS 5.7 rewriteRelativeImportExtensions (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
fvsch authored Nov 24, 2024
1 parent 8bbb7ac commit 3851315
Show file tree
Hide file tree
Showing 29 changed files with 335 additions and 292 deletions.
4 changes: 4 additions & 0 deletions bin/servitsy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// > deno run --allow-net --allow-sys bin/servitsy.ts
// > node --experimental-strip-types bin/servitsy.ts
import { run } from '../src/cli.ts';
run();
16 changes: 8 additions & 8 deletions package-lock.json

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

9 changes: 2 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@
"import": "./lib/index.js"
}
},
"imports": {
"#src/*.js": "./src/*.js",
"#types": "./src/types.d.ts"
},
"files": [
"./bin",
"./lib",
"./LICENSE",
"./README.md"
Expand All @@ -47,11 +42,11 @@
"typecheck": "tsc -p tsconfig.json --noEmit && tsc -p test/tsconfig.json --noEmit"
},
"devDependencies": {
"@types/node": "^20.17.6",
"@types/node": "^20.17.7",
"fs-fixture": "^2.6.0",
"linkedom": "^0.18.5",
"prettier": "^3.3.3",
"typescript": "~5.6.3",
"typescript": "~5.7.2",
"vitest": "^2.1.5"
}
}
2 changes: 1 addition & 1 deletion scripts/prebuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async function main() {
Read text files from the assets folder and write
*/
async function bundleAssets() {
const outPath = pkgFilePath('src/page-assets.ts');
const outPath = pkgFilePath('src/assets.ts');
console.log(`Updating assets bundle:\n ${outPath}\n`);

const assets = {
Expand Down
4 changes: 2 additions & 2 deletions src/args.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CLI_OPTIONS, PORTS_CONFIG } from './constants.js';
import { CLI_OPTIONS, PORTS_CONFIG } from './constants.ts';
import type { HttpHeaderRule, OptionSpec, ServerOptions } from './types.d.ts';
import { intRange } from './utils.js';
import { intRange } from './utils.ts';

export class CLIArgs {
#map: Array<[string, string]> = [];
Expand Down
File renamed without changes.
21 changes: 13 additions & 8 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { createServer, type Server } from 'node:http';
import { createRequire } from 'node:module';
import { homedir, networkInterfaces, type NetworkInterfaceInfo } from 'node:os';
import { sep as dirSep } from 'node:path';
import process, { argv, exit, platform, stdin } from 'node:process';
import { emitKeypressEvents } from 'node:readline';

import { CLIArgs, parseArgs } from './args.js';
import { CLI_OPTIONS, HOSTS_LOCAL, HOSTS_WILDCARD } from './constants.js';
import { checkDirAccess, readPkgJson } from './fs-utils.js';
import { RequestHandler } from './handler.js';
import { color, Logger, requestLogLine } from './logger.js';
import { serverOptions } from './options.js';
import { FileResolver } from './resolver.js';
import { CLIArgs, parseArgs } from './args.ts';
import { CLI_OPTIONS, HOSTS_LOCAL, HOSTS_WILDCARD } from './constants.ts';
import { checkDirAccess } from './fs-utils.ts';
import { RequestHandler } from './handler.ts';
import { color, Logger, requestLogLine } from './logger.ts';
import { serverOptions } from './options.ts';
import { FileResolver } from './resolver.ts';
import type { OptionName, ServerOptions } from './types.d.ts';
import { clamp, errorList, getRuntime, isPrivateIPv4 } from './utils.js';
import { clamp, errorList, getRuntime, isPrivateIPv4 } from './utils.ts';

/**
Start servitsy with configuration from command line arguments.
Expand Down Expand Up @@ -295,3 +296,7 @@ function displayRoot(root: string): string {
}
return root;
}

function readPkgJson(): Record<string, any> {
return createRequire(import.meta.url)('../package.json');
}
28 changes: 3 additions & 25 deletions src/fs-utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { access, constants, lstat, readdir, realpath, stat } from 'node:fs/promises';
import { createRequire } from 'node:module';
import { isAbsolute, join, sep as dirSep } from 'node:path';
import { join } from 'node:path';

import type { FSKind, FSLocation } from './types.d.ts';
import { trimSlash } from './utils.js';

export async function checkDirAccess(
dirPath: string,
Expand Down Expand Up @@ -53,14 +51,6 @@ export async function getKind(filePath: string): Promise<FSKind> {
}
}

export function getLocalPath(root: string, filePath: string): string | null {
if (isSubpath(root, filePath)) {
return trimSlash(filePath.slice(root.length), { start: true, end: true });
}
return null;
}

/** @type {(filePath: string) => Promise<string | null>} */
export async function getRealpath(filePath: string): Promise<string | null> {
try {
const real = await realpath(filePath);
Expand All @@ -84,23 +74,11 @@ export async function isReadable(filePath: string, kind?: FSKind): Promise<boole
return false;
}

export function isSubpath(parent: string, filePath: string): boolean {
if (filePath.includes('..') || !isAbsolute(filePath)) return false;
parent = trimSlash(parent, { end: true });
return filePath === parent || filePath.startsWith(parent + dirSep);
}

export function readPkgJson(): Record<string, any> {
return createRequire(import.meta.url)('../package.json');
}

interface StatsLike {
function statsKind(stats: {
isSymbolicLink?(): boolean;
isDirectory?(): boolean;
isFile?(): boolean;
}

export function statsKind(stats: StatsLike): FSKind {
}): FSKind {
if (stats.isSymbolicLink?.()) return 'link';
if (stats.isDirectory?.()) return 'dir';
else if (stats.isFile?.()) return 'file';
Expand Down
12 changes: 5 additions & 7 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import { createReadStream } from 'node:fs';
import { open, stat, type FileHandle } from 'node:fs/promises';
import { createGzip, gzipSync } from 'node:zlib';

import { MAX_COMPRESS_SIZE, SUPPORTED_METHODS } from './constants.js';
import { getContentType, typeForFilePath } from './content-type.js';
import { getLocalPath, isSubpath } from './fs-utils.js';
import { dirListPage, errorPage } from './pages.js';
import { PathMatcher } from './path-matcher.js';
import { MAX_COMPRESS_SIZE, SUPPORTED_METHODS } from './constants.ts';
import { getContentType, typeForFilePath } from './content-type.ts';
import { dirListPage, errorPage } from './pages.ts';
import { FileResolver } from './resolver.ts';
import type {
FSLocation,
HttpHeaderRule,
Expand All @@ -16,8 +15,7 @@ import type {
ResMetaData,
ServerOptions,
} from './types.d.ts';
import { headerCase, trimSlash } from './utils.js';
import { FileResolver } from './resolver.js';
import { getLocalPath, headerCase, isSubpath, PathMatcher, trimSlash } from './utils.ts';

interface Config {
req: Request;
Expand Down
4 changes: 2 additions & 2 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { type Writable } from 'node:stream';
import { inspect } from 'node:util';

import type { ResMetaData } from './types.d.ts';
import { clamp, fwdSlash, getEnv, getRuntime, trimSlash, withResolvers } from './utils.js';
import { clamp, fwdSlash, getEnv, getRuntime, trimSlash, withResolvers } from './utils.ts';

interface LogItem {
group: 'header' | 'info' | 'request' | 'error';
Expand Down Expand Up @@ -160,7 +160,7 @@ function pathSuffix(basePath: string, fullPath: string): string | undefined {
/**
Basic implementation of 'node:util' styleText to support Node 18 + Deno.
*/
export function styleText(format: string | string[], text: string): string {
function styleText(format: string | string[], text: string): string {
let before = '';
let after = '';
for (const style of Array.isArray(format) ? format : [format]) {
Expand Down
2 changes: 1 addition & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isAbsolute, resolve } from 'node:path';

import { DEFAULT_OPTIONS, PORTS_CONFIG } from './constants.js';
import { DEFAULT_OPTIONS, PORTS_CONFIG } from './constants.ts';
import type { HttpHeaderRule, ServerOptions } from './types.d.ts';

export function serverOptions(
Expand Down
4 changes: 2 additions & 2 deletions src/pages.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { basename, dirname } from 'node:path';

import { FAVICON_LIST, FAVICON_ERROR, ICONS, STYLES } from './page-assets.js';
import { FAVICON_LIST, FAVICON_ERROR, ICONS, STYLES } from './assets.ts';
import type { FSLocation } from './types.d.ts';
import { clamp, escapeHtml, trimSlash } from './utils.js';
import { clamp, escapeHtml, trimSlash } from './utils.ts';

function htmlTemplate(data: {
base?: string;
Expand Down
74 changes: 0 additions & 74 deletions src/path-matcher.ts

This file was deleted.

5 changes: 2 additions & 3 deletions src/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { isAbsolute, join } from 'node:path';

import { getIndex, getKind, getLocalPath, getRealpath, isReadable, isSubpath } from './fs-utils.js';
import { PathMatcher } from './path-matcher.js';
import { getIndex, getKind, getRealpath, isReadable } from './fs-utils.ts';
import type { FSLocation, ServerOptions } from './types.d.ts';
import { trimSlash } from './utils.js';
import { getLocalPath, isSubpath, PathMatcher, trimSlash } from './utils.ts';

export class FileResolver {
#root: string;
Expand Down
Loading

0 comments on commit 3851315

Please sign in to comment.