Skip to content

Commit

Permalink
chore: update command-line-basics, minimatch, devDeps
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

Requires Node 18+

Also:
- chore: switches to flat config/ESLint 9
  • Loading branch information
brettz9 committed Aug 22, 2024
1 parent bd48e28 commit e49fbd7
Show file tree
Hide file tree
Showing 11 changed files with 1,469 additions and 3,727 deletions.
1 change: 0 additions & 1 deletion .eslintignore

This file was deleted.

27 changes: 0 additions & 27 deletions .eslintrc.cjs

This file was deleted.

6 changes: 5 additions & 1 deletion .ncurc.js → .ncurc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ module.exports = {
'colors',

// ESM-only; only switch when dropping dual CJS support
'node-fetch'
'node-fetch',
'mime',

// Node 20+
'minimatch'
]
};
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### User-facing

- **Breaking change** (npm): Set `engines` to 14.0.0+
- **Breaking change** (npm): Set `engines` to 18.0.0+
- **Breaking change**: Add `type: 'module'` and `exports` to `package.json`;
change internal CJS path
- Security: Fix dependency vulnerabilities by switching from `optimist` to
Expand Down
51 changes: 19 additions & 32 deletions dist/node-static.cjs
Original file line number Diff line number Diff line change
@@ -1,30 +1,20 @@
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var fs = require('fs');
var events = require('events');
var http = require('http');
var path = require('path');
var mime = require('mime');
var minimatch = require('minimatch');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
var events__default = /*#__PURE__*/_interopDefaultLegacy(events);
var http__default = /*#__PURE__*/_interopDefaultLegacy(http);
var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
var mime__default = /*#__PURE__*/_interopDefaultLegacy(mime);
var minimatch__default = /*#__PURE__*/_interopDefaultLegacy(minimatch);

var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
function mstat (dir, files, callback) {
(function mstat(files, stats) {
const file = files.shift();

if (file) {
try {
fs__default["default"].stat(path__default["default"].join(dir, file), function (e, stat) {
fs.stat(path.join(dir, file), function (e, stat) {
if (e) {
callback(e);
} else {
Expand All @@ -50,15 +40,15 @@ function mstat (dir, files, callback) {
})(files.slice(0), []);
}

const pkg = JSON.parse(fs__default["default"].readFileSync(
new URL('../package.json', (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('node-static.cjs', document.baseURI).href)))
const pkg = JSON.parse(fs.readFileSync(
new URL('../package.json', (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('node-static.cjs', document.baseURI).href)))
));

const version = pkg.version.split('.');

function tryStat(p, callback) {
try {
fs__default["default"].stat(p, callback);
fs.stat(p, callback);
} catch (e) {
callback(e);
}
Expand All @@ -69,7 +59,7 @@ class Server {
if (root && typeof root === 'object') { options = root; root = null; }

// resolve() doesn't normalize (to lowercase) drive letters on Windows
this.root = path__default["default"].normalize(path__default["default"].resolve(root || '.'));
this.root = path.normalize(path.resolve(root || '.'));
this.options = options || {};
this.cache = {'**': 3600};

Expand Down Expand Up @@ -111,7 +101,7 @@ class Server {
}

serveDir (pathname, req, res, finish) {
const htmlIndex = path__default["default"].join(pathname, this.options.indexFile),
const htmlIndex = path.join(pathname, this.options.indexFile),
that = this;

tryStat(htmlIndex, function (e, stat) {
Expand All @@ -126,7 +116,7 @@ class Server {
}
} else {
// Stream a directory of files as a single file.
fs__default["default"].readFile(path__default["default"].join(pathname, 'index.json'), function (e, contents) {
fs.readFile(path.join(pathname, 'index.json'), function (e, contents) {
if (e) { return finish(404, {}) }
const index = JSON.parse(contents);
streamFiles(index.files);
Expand All @@ -143,7 +133,7 @@ class Server {

serveFile (pathname, status, headers, req, res) {
const that = this;
const promise = new(events__default["default"].EventEmitter);
const promise = new(events.EventEmitter);

pathname = this.resolve(pathname);

Expand All @@ -162,7 +152,7 @@ class Server {
const result = {
status,
headers,
message: http__default["default"].STATUS_CODES[status]
message: http.STATUS_CODES[status]
};

if (this.options.serverInfo !== null) {
Expand Down Expand Up @@ -195,7 +185,7 @@ class Server {

servePath (pathname, status, headers, req, res, finish) {
const that = this,
promise = new(events__default["default"].EventEmitter);
promise = new(events.EventEmitter);

pathname = this.resolve(pathname);

Expand Down Expand Up @@ -235,12 +225,12 @@ class Server {
}

resolve (pathname) {
return path__default["default"].resolve(path__default["default"].join(this.root, pathname));
return path.resolve(path.join(this.root, pathname));
}

serve (req, res, callback) {
const that = this,
promise = new(events__default["default"].EventEmitter);
promise = new(events.EventEmitter);
let pathname;

const finish = function (status, headers) {
Expand All @@ -250,7 +240,7 @@ class Server {
try {
pathname = decodeURIComponent(new URL(req.url, 'http://localhost').pathname);
}
catch(e) {
catch {
return process.nextTick(function() {
return finish(400, {});
});
Expand Down Expand Up @@ -417,7 +407,7 @@ class Server {

respond (pathname, status, _headers, files, stat, req, res, finish) {
const contentType = _headers['Content-Type'] ||
mime__default["default"].getType(files[0]) ||
mime.getType(files[0]) ||
'application/octet-stream';
_headers = this.setCacheHeaders(_headers, req);

Expand All @@ -434,10 +424,10 @@ class Server {
let file = files.shift();

if (file) {
file = path__default["default"].resolve(file) === path__default["default"].normalize(file) ? file : path__default["default"].join(pathname || '.', file);
file = path.resolve(file) === path.normalize(file) ? file : path.join(pathname || '.', file);

// Stream the file to the client
fs__default["default"].createReadStream(file, {
fs.createReadStream(file, {
flags: 'r',
mode: 0o666,
start: startByte,
Expand Down Expand Up @@ -472,7 +462,7 @@ class Server {
getMaxAge (requestUrl) {
if (this.cache) {
for (const pattern in this.cache) {
if (minimatch__default["default"](requestUrl, pattern)) {
if (minimatch.minimatch(requestUrl, pattern)) {
return this.cache[pattern];
}
}
Expand All @@ -481,9 +471,6 @@ class Server {
}
}

Object.defineProperty(exports, 'mime', {
enumerable: true,
get: function () { return mime__default["default"]; }
});
exports.mime = mime;
exports.Server = Server;
exports.version = version;
30 changes: 30 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import js from '@eslint/js';
import globals from 'globals';

export default [
{
ignores: ['dist']
},
js.configs.recommended,
{
languageOptions: {
globals: globals.node,
parserOptions: {
sourceType: 'module',
ecmaVersion: 2021
}
},
rules: {
indent: ['error', 4],
'no-var': ['error'],
'no-unused-vars': ['error', {args: 'none'}],
'prefer-const': ['error']
}
},
{
files: ['test/**'],
languageOptions: {
globals: globals.mocha
}
}
];
4 changes: 2 additions & 2 deletions lib/node-static.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import path from 'path';
// import buffer from 'buffer';

import mime from 'mime';
import minimatch from 'minimatch';
import {minimatch} from 'minimatch';
import {mstat} from './node-static/util.js';

const pkg = JSON.parse(fs.readFileSync(
Expand Down Expand Up @@ -208,7 +208,7 @@ class Server {
try {
pathname = decodeURIComponent(new URL(req.url, 'http://localhost').pathname);
}
catch(e) {
catch {
return process.nextTick(function() {
return finish(400, {});
});
Expand Down
Loading

0 comments on commit e49fbd7

Please sign in to comment.