UNPKG

servatron

Version:

Create a handler that can server static files using the NodeJS http/http2 modules, or use the inbuilt cli server to quickly run a web server.

25 lines (24 loc) 717 B
import * as fs from 'fs'; export var PathType; (function (PathType) { PathType["NotFound"] = "NOT_FOUND"; PathType["File"] = "FILE"; PathType["Directory"] = "DIRECTORY"; })(PathType || (PathType = {})); export function getPathInfo(filePath) { return new Promise((resolve) => { const stream = fs.createReadStream(filePath); stream.on('readable', () => { resolve(PathType.File); stream.close(); }); stream.on('error', (error) => { if (error.code === 'ENOENT') { resolve(PathType.NotFound); return; } resolve(PathType.Directory); }); }); } export default getPathInfo;