Skip to content

Commit

Permalink
Path checks and version update. (#189)
Browse files Browse the repository at this point in the history
This PR adds checking before starting a `StaticServer` and prepares for
a new release.
  • Loading branch information
danfuzz authored May 9, 2023
2 parents 8588dc6 + 60aefc4 commit 685778c
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 27 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
Changelog
=========

### v0.5.13 -- 2023-05-09

Notable changes:

* Somewhat better error checking on startup.
* Build infrastructure update:
* Introduced use of `package-lock.json`.
* Pulled in improved `bashy-lib`.

### v0.5.12 -- 2023-05-02

Notable changes:
Expand Down
1 change: 1 addition & 0 deletions src/app-util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"dependencies": {
"@this/app-config": "*",
"@this/async": "*",
"@this/fs-util": "*",
"@this/loggy": "*",
"@this/typey": "*"
}
Expand Down
22 changes: 2 additions & 20 deletions src/app-util/private/BaseFilePreserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as fs from 'node:fs/promises';

import { FileServiceConfig } from '@this/app-config';
import { Condition, Threadlet } from '@this/async';
import { FsUtil } from '@this/fs-util';
import { IntfLogger } from '@this/loggy';
import { Methods, MustBe } from '@this/typey';

Expand Down Expand Up @@ -305,7 +306,7 @@ export class BaseFilePreserver {
// turns out to be wrong, we'll fall back to the more involved code.
const count = this.#lastInfixCount + 1;
const firstTry = resolve(count);
if (!await BaseFilePreserver.#fileExists(firstTry)) {
if (!await FsUtil.fileExists(firstTry)) {
this.#lastInfixCount = count;
return firstTry;
}
Expand Down Expand Up @@ -334,25 +335,6 @@ export class BaseFilePreserver {
// Static members
//

/**
* Checks to see if the given file exists.
*
* @param {string} filePath Path to the file.
* @returns {boolean} The answer.
*/
static async #fileExists(filePath) {
try {
await fs.stat(filePath);
return true;
} catch (e) {
if (e.code === 'ENOENT') {
// Not found. Not a real error in this case.
return false;
}
throw e;
}
}

/**
* Gets a date string with optional count to use as an "infix" for a preserved
* file.
Expand Down
3 changes: 2 additions & 1 deletion src/async/tests/EventSink.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import * as timers from 'node:timers/promises';

import { EventPayload, EventSink, LinkedEvent, ManualPromise, PromiseState,
PromiseUtil } from '@this/async';
PromiseUtil }
from '@this/async';


const payload1 = new EventPayload('wacky');
Expand Down
3 changes: 1 addition & 2 deletions src/async/tests/EventTracker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

import * as timers from 'node:timers/promises';

import { EventPayload, EventTracker, LinkedEvent, ManualPromise, PromiseState }
from '@this/async';
import { EventPayload, EventTracker, LinkedEvent, ManualPromise, PromiseState } from '@this/async';


const payload1 = new EventPayload('1:wacky:1');
Expand Down
19 changes: 16 additions & 3 deletions src/builtin-applications/export/StaticFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import express from 'express';

import { ApplicationConfig, Files } from '@this/app-config';
import { BaseApplication } from '@this/app-framework';
import { FsUtil } from '@this/fs-util';
import { IntfLogger } from '@this/loggy';


Expand Down Expand Up @@ -33,8 +34,10 @@ export class StaticFiles extends BaseApplication {
constructor(config, logger) {
super(config, logger);

this.#notFoundPath = config.notFoundPath;
this.#staticMiddleware = express.static(config.siteDirectory);
const { notFoundPath, siteDirectory } = config;

this.#notFoundPath = notFoundPath;
this.#staticMiddleware = express.static(siteDirectory);
}

/** @override */
Expand All @@ -52,7 +55,17 @@ export class StaticFiles extends BaseApplication {

/** @override */
async _impl_start(isReload_unused) {
// Nothing to do here.
const { notFoundPath, siteDirectory } = this.config;

if (!await FsUtil.directoryExists(siteDirectory)) {
throw new Error(`Not found or not a directory: ${siteDirectory}`);
}

if (notFoundPath) {
if (!await FsUtil.fileExists(notFoundPath)) {
throw new Error(`Not found or not a file: ${notFoundPath}`);
}
}
}

/** @override */
Expand Down
1 change: 1 addition & 0 deletions src/builtin-applications/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"dependencies": {
"@this/app-config": "*",
"@this/app-framework": "*",
"@this/fs-util": "*",
"@this/loggy": "*",
"@this/typey": "*",
"express": "^4.18.2"
Expand Down
10 changes: 10 additions & 0 deletions src/fs-util/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@this/fs-util
=============

Filesystem utility classes.

- - - - - - - - - -
```
Copyright 2022-2023 the Lactoserv Authors (Dan Bornstein et alia).
SPDX-License-Identifier: Apache-2.0
```
83 changes: 83 additions & 0 deletions src/fs-util/export/FsUtil.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2022-2023 the Lactoserv Authors (Dan Bornstein et alia).
// SPDX-License-Identifier: Apache-2.0

import * as fs from 'node:fs/promises';

import { MustBe } from '@this/typey';


/**
* General filesystem utility class.
*/
export class FsUtil {
/**
* Checks to see if the given path exists in the filesystem, and is a
* directory (not a regular or special file).
*
* @param {string} path Path to check.
* @returns {boolean} The answer.
*/
static async directoryExists(path) {
const stats = await this.#statOrNull(path);

return stats && stats.isDirectory();
}

/**
* Checks to see if the given path exists in the filesystem, and is a regular
* file (not a directory or special file).
*
* @param {string} path Path to check.
* @returns {boolean} The answer.
*/
static async fileExists(path) {
const stats = await this.#statOrNull(path);

return stats && stats.isFile();
}

/**
* Checks to see if the given path exists in the filesystem.
*
* @param {string} path Path to check.
* @returns {boolean} The answer.
*/
static async pathExists(path) {
const stats = await this.#statOrNull(path);

return (stats !== null);
}

/**
* Checks to see if the given path exists in the filesystem, and is a socket.
*
* @param {string} path Path to check.
* @returns {boolean} The answer.
*/
static async socketExists(path) {
const stats = await this.#statOrNull(path);

return stats && stats.isSocket();
}

/**
* Gets the `fs.Stats` of the path if it exists, or returns `null` if the
* path does not exist in the filesystem.
*
* @param {string} path Path to check.
* @returns {?fs.Stats} The stats, if the path exists, or `null` if not.
*/
static async #statOrNull(path) {
MustBe.string(path);

try {
return await fs.stat(path);
} catch (e) {
if (e.code === 'ENOENT') {
// Not found. Not a real error in this case.
return null;
}
throw e;
}
}
}
4 changes: 4 additions & 0 deletions src/fs-util/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Copyright 2022-2023 the Lactoserv Authors (Dan Bornstein et alia).
// SPDX-License-Identifier: Apache-2.0

export * from '#x/FsUtil';
17 changes: 17 additions & 0 deletions src/fs-util/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "@this/fs-util",
"version": "0.0.1",
"type": "module",
"private": true,
"license": "Apache-2.0",

"exports": "./index.js",
"imports": {
"#x/*": "./export/*.js",
"#p/*": "./private/*.js"
},

"dependencies": {
"@this/typey": "*"
}
}
2 changes: 1 addition & 1 deletion src/main-lactoserv/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@this/main-lactoserv",
"version": "0.5.12",
"version": "0.5.13",
"type": "module",
"private": true,
"license": "Apache-2.0",
Expand Down

0 comments on commit 685778c

Please sign in to comment.