Skip to content

Commit 685778c

Browse files
authored
Path checks and version update. (#189)
This PR adds checking before starting a `StaticServer` and prepares for a new release.
2 parents 8588dc6 + 60aefc4 commit 685778c

File tree

12 files changed

+147
-27
lines changed

12 files changed

+147
-27
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
Changelog
22
=========
33

4+
### v0.5.13 -- 2023-05-09
5+
6+
Notable changes:
7+
8+
* Somewhat better error checking on startup.
9+
* Build infrastructure update:
10+
* Introduced use of `package-lock.json`.
11+
* Pulled in improved `bashy-lib`.
12+
413
### v0.5.12 -- 2023-05-02
514

615
Notable changes:

src/app-util/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"dependencies": {
1515
"@this/app-config": "*",
1616
"@this/async": "*",
17+
"@this/fs-util": "*",
1718
"@this/loggy": "*",
1819
"@this/typey": "*"
1920
}

src/app-util/private/BaseFilePreserver.js

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as fs from 'node:fs/promises';
55

66
import { FileServiceConfig } from '@this/app-config';
77
import { Condition, Threadlet } from '@this/async';
8+
import { FsUtil } from '@this/fs-util';
89
import { IntfLogger } from '@this/loggy';
910
import { Methods, MustBe } from '@this/typey';
1011

@@ -305,7 +306,7 @@ export class BaseFilePreserver {
305306
// turns out to be wrong, we'll fall back to the more involved code.
306307
const count = this.#lastInfixCount + 1;
307308
const firstTry = resolve(count);
308-
if (!await BaseFilePreserver.#fileExists(firstTry)) {
309+
if (!await FsUtil.fileExists(firstTry)) {
309310
this.#lastInfixCount = count;
310311
return firstTry;
311312
}
@@ -334,25 +335,6 @@ export class BaseFilePreserver {
334335
// Static members
335336
//
336337

337-
/**
338-
* Checks to see if the given file exists.
339-
*
340-
* @param {string} filePath Path to the file.
341-
* @returns {boolean} The answer.
342-
*/
343-
static async #fileExists(filePath) {
344-
try {
345-
await fs.stat(filePath);
346-
return true;
347-
} catch (e) {
348-
if (e.code === 'ENOENT') {
349-
// Not found. Not a real error in this case.
350-
return false;
351-
}
352-
throw e;
353-
}
354-
}
355-
356338
/**
357339
* Gets a date string with optional count to use as an "infix" for a preserved
358340
* file.

src/async/tests/EventSink.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import * as timers from 'node:timers/promises';
55

66
import { EventPayload, EventSink, LinkedEvent, ManualPromise, PromiseState,
7-
PromiseUtil } from '@this/async';
7+
PromiseUtil }
8+
from '@this/async';
89

910

1011
const payload1 = new EventPayload('wacky');

src/async/tests/EventTracker.test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

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

6-
import { EventPayload, EventTracker, LinkedEvent, ManualPromise, PromiseState }
7-
from '@this/async';
6+
import { EventPayload, EventTracker, LinkedEvent, ManualPromise, PromiseState } from '@this/async';
87

98

109
const payload1 = new EventPayload('1:wacky:1');

src/builtin-applications/export/StaticFiles.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import express from 'express';
55

66
import { ApplicationConfig, Files } from '@this/app-config';
77
import { BaseApplication } from '@this/app-framework';
8+
import { FsUtil } from '@this/fs-util';
89
import { IntfLogger } from '@this/loggy';
910

1011

@@ -33,8 +34,10 @@ export class StaticFiles extends BaseApplication {
3334
constructor(config, logger) {
3435
super(config, logger);
3536

36-
this.#notFoundPath = config.notFoundPath;
37-
this.#staticMiddleware = express.static(config.siteDirectory);
37+
const { notFoundPath, siteDirectory } = config;
38+
39+
this.#notFoundPath = notFoundPath;
40+
this.#staticMiddleware = express.static(siteDirectory);
3841
}
3942

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

5356
/** @override */
5457
async _impl_start(isReload_unused) {
55-
// Nothing to do here.
58+
const { notFoundPath, siteDirectory } = this.config;
59+
60+
if (!await FsUtil.directoryExists(siteDirectory)) {
61+
throw new Error(`Not found or not a directory: ${siteDirectory}`);
62+
}
63+
64+
if (notFoundPath) {
65+
if (!await FsUtil.fileExists(notFoundPath)) {
66+
throw new Error(`Not found or not a file: ${notFoundPath}`);
67+
}
68+
}
5669
}
5770

5871
/** @override */

src/builtin-applications/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"dependencies": {
1515
"@this/app-config": "*",
1616
"@this/app-framework": "*",
17+
"@this/fs-util": "*",
1718
"@this/loggy": "*",
1819
"@this/typey": "*",
1920
"express": "^4.18.2"

src/fs-util/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@this/fs-util
2+
=============
3+
4+
Filesystem utility classes.
5+
6+
- - - - - - - - - -
7+
```
8+
Copyright 2022-2023 the Lactoserv Authors (Dan Bornstein et alia).
9+
SPDX-License-Identifier: Apache-2.0
10+
```

src/fs-util/export/FsUtil.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright 2022-2023 the Lactoserv Authors (Dan Bornstein et alia).
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import * as fs from 'node:fs/promises';
5+
6+
import { MustBe } from '@this/typey';
7+
8+
9+
/**
10+
* General filesystem utility class.
11+
*/
12+
export class FsUtil {
13+
/**
14+
* Checks to see if the given path exists in the filesystem, and is a
15+
* directory (not a regular or special file).
16+
*
17+
* @param {string} path Path to check.
18+
* @returns {boolean} The answer.
19+
*/
20+
static async directoryExists(path) {
21+
const stats = await this.#statOrNull(path);
22+
23+
return stats && stats.isDirectory();
24+
}
25+
26+
/**
27+
* Checks to see if the given path exists in the filesystem, and is a regular
28+
* file (not a directory or special file).
29+
*
30+
* @param {string} path Path to check.
31+
* @returns {boolean} The answer.
32+
*/
33+
static async fileExists(path) {
34+
const stats = await this.#statOrNull(path);
35+
36+
return stats && stats.isFile();
37+
}
38+
39+
/**
40+
* Checks to see if the given path exists in the filesystem.
41+
*
42+
* @param {string} path Path to check.
43+
* @returns {boolean} The answer.
44+
*/
45+
static async pathExists(path) {
46+
const stats = await this.#statOrNull(path);
47+
48+
return (stats !== null);
49+
}
50+
51+
/**
52+
* Checks to see if the given path exists in the filesystem, and is a socket.
53+
*
54+
* @param {string} path Path to check.
55+
* @returns {boolean} The answer.
56+
*/
57+
static async socketExists(path) {
58+
const stats = await this.#statOrNull(path);
59+
60+
return stats && stats.isSocket();
61+
}
62+
63+
/**
64+
* Gets the `fs.Stats` of the path if it exists, or returns `null` if the
65+
* path does not exist in the filesystem.
66+
*
67+
* @param {string} path Path to check.
68+
* @returns {?fs.Stats} The stats, if the path exists, or `null` if not.
69+
*/
70+
static async #statOrNull(path) {
71+
MustBe.string(path);
72+
73+
try {
74+
return await fs.stat(path);
75+
} catch (e) {
76+
if (e.code === 'ENOENT') {
77+
// Not found. Not a real error in this case.
78+
return null;
79+
}
80+
throw e;
81+
}
82+
}
83+
}

src/fs-util/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Copyright 2022-2023 the Lactoserv Authors (Dan Bornstein et alia).
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
export * from '#x/FsUtil';

src/fs-util/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "@this/fs-util",
3+
"version": "0.0.1",
4+
"type": "module",
5+
"private": true,
6+
"license": "Apache-2.0",
7+
8+
"exports": "./index.js",
9+
"imports": {
10+
"#x/*": "./export/*.js",
11+
"#p/*": "./private/*.js"
12+
},
13+
14+
"dependencies": {
15+
"@this/typey": "*"
16+
}
17+
}

src/main-lactoserv/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@this/main-lactoserv",
3-
"version": "0.5.12",
3+
"version": "0.5.13",
44
"type": "module",
55
"private": true,
66
"license": "Apache-2.0",

0 commit comments

Comments
 (0)