Skip to content

Commit

Permalink
v1.0.43 (file watcher)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielweck committed Aug 5, 2022
1 parent c7b9a03 commit 41a8241
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 17 deletions.
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
"typescript.surveys.enabled": false,
"editor.roundedSelection": false,
"editor.wordWrap": "off",
"editor.quickSuggestions": false,
"editor.quickSuggestions": {
"comments": "off",
"strings": "off",
"other": "off"
},
"editor.autoClosingBrackets": "never",
"editor.scrollBeyondLastLine": false,
"editor.formatOnType": false,
Expand Down
19 changes: 18 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
# Next

Git diff:
* https://github.com/readium/r2-streamer-js/compare/v1.0.42...develop
* https://github.com/readium/r2-streamer-js/compare/v1.0.43...develop

Changes:
* TODO

# 1.0.43

> Build environment: NodeJS `16.16.0`, NPM `8.15.0`
Changes:
* Streamer server watch mode in publication directory (note: initial scan is flat / not recursive as per the original design brief, but the watcher triggers on deep added/removed files and updates the streamer accordingly)

Git revision info:
* https://unpkg.com/[email protected]/dist/gitrev.json
* https://github.com/edrlab/r2-streamer-js-dist/blob/v1.0.43/dist/gitrev.json

Git commit history:
* https://github.com/readium/r2-streamer-js/commits/v1.0.43

Git diff:
* https://github.com/readium/r2-streamer-js/compare/v1.0.42...v1.0.43

# 1.0.42

> Build environment: NodeJS `16.14.0`, NPM `8.5.4`
Expand Down
56 changes: 54 additions & 2 deletions package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "r2-streamer-js",
"version": "1.0.42",
"version": "1.0.43",
"description": "Readium 2 'streamer' for NodeJS (TypeScript)",
"keywords": [
"readium",
Expand Down Expand Up @@ -45,6 +45,7 @@
},
"homepage": "https://github.com/readium/r2-streamer-js",
"dependencies": {
"@parcel/watcher": "^2.0.5",
"@xmldom/xmldom": "^0.8.1",
"ajv": "^8.11.0",
"ajv-formats": "^2.1.1",
Expand Down Expand Up @@ -152,7 +153,7 @@
"server-bundlemin": "node \"./dist/bundle-es8-es2017/server-cli.min.js\"",
"server-bundlemin-debug": "cross-env DEBUG=r2:* npm run server-bundlemin",
"server-bundlemin-debug-x": "cross-env DEBUG=* npm run server-bundlemin",
"server": "node --version && node \"./dist/es8-es2017/src/http/server-cli.js\"",
"server": "node --version && cross-env STREAMER_WATCH=1 node \"./dist/es8-es2017/src/http/server-cli.js\"",
"server-debug": "cross-env DEBUG=r2:* npm run server",
"server-debug-x": "cross-env DEBUG=* npm run server",
"server-https-debug": "cross-env DEBUG=r2:* PORT=443 npm run server",
Expand Down
105 changes: 95 additions & 10 deletions src/http/server-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import * as debug_ from "debug";
import * as fs from "fs";
import * as path from "path";
import * as watcher from "@parcel/watcher";

import { setLcpNativePluginPath } from "@r2-lcp-js/parser/epub/lcp";
import { initGlobalConverters_OPDS } from "@r2-opds-js/opds/init-globals";
Expand Down Expand Up @@ -86,11 +87,23 @@ if (args[1]) {
}
debug(`maxPrefetchLinks: ${maxPrefetchLinks}`);

const doWatch = process.env.STREAMER_WATCH === "1";

const isAnEPUB = isEPUBlication(filePath);

if (stats.isDirectory() && (isAnEPUB !== EPUBis.LocalExploded)) {
debug("Analysing directory...");

const isFileAccepted = (pubPath: string) => {
return /((\.epub3?)|(\.cbz)|(\.audiobook)|(\.lcpaudiobook)|(\.lcpa)|(\.divina)|(\.lcpdivina))$/i.test(pubPath)
||
(
/_manifest\.json$/.test(pubPath)
&&
fs.existsSync(pubPath.replace(/_manifest\.json$/, ""))
);
};

// tslint:disable-next-line:no-floating-promises
(async () => {
// const files: string[] = await filehound.create()
Expand All @@ -99,16 +112,12 @@ if (stats.isDirectory() && (isAnEPUB !== EPUBis.LocalExploded)) {
// .paths(filePath)
// .ext([".epub", ".epub3", ".cbz", ".audiobook", ".lcpaudiobook", ".lcpa", ".divina", ".lcpdivina"])
// .find();
const files = fs.readdirSync(filePath, { withFileTypes: true }).
filter((f) => {
return f.isFile() &&
(
/((\.epub3?)|(\.cbz)|(\.audiobook)|(\.lcpaudiobook)|(\.lcpa)|(\.divina)|(\.lcpdivina))$/i.test(f.name)
||
(/_manifest\.json$/.test(f.name)
&& fs.existsSync(path.join(filePath, path.basename(f.name).replace(/_manifest\.json$/, ""))))
);
}).map((f) => path.join(filePath, f.name));
const files = fs.readdirSync(filePath, { withFileTypes: true })
.filter((f) => f.isFile())
.map((f) => path.join(filePath, f.name))
.filter((pubPath) => {
return isFileAccepted(pubPath);
});

const server = new Server({
maxPrefetchLinks,
Expand All @@ -118,6 +127,82 @@ if (stats.isDirectory() && (isAnEPUB !== EPUBis.LocalExploded)) {
server.addPublications(files);
const url = await server.start(0, false);
debug(url);

if (!doWatch) {
return;
}
debug("WATCHER: ", filePath);

// const watcherSubscription =
await watcher.subscribe(filePath, (err, events) => {

if (err) {
debug("WATCHER: ", filePath, err);
return;
}

// Can be used for temporarily pausing the watch action
const doWatchLive = process.env.STREAMER_WATCH === "1";
if (!doWatchLive) {
return;
}

const filesToAdd: string[] = [];
const filesToRemove: string[] = [];

for (const event of events) {
const fPath = event.path;
debug(`WATCHER: ${fPath} => ${event.type}`);

const fsStat = event.type === "delete" ? undefined : fs.lstatSync(fPath);
if (fsStat && (!fsStat.isFile() || !isFileAccepted(fPath))) {
continue;
}

if (event.type === "create") {
filesToAdd.push(fPath);

if (!/_manifest\.json$/.test(fPath)) {
const s = `${fPath}_manifest.json`;
if (fs.existsSync(s)) {
if (!server.getPublications().includes(s) && !filesToAdd.includes(s)) {
filesToAdd.push(s);
}
}
}
} else if (event.type === "update") {
if (!filesToRemove.includes(fPath)) {
filesToRemove.push(fPath);
}
if (!filesToAdd.includes(fPath)) {
filesToAdd.push(fPath);
}
} else if (event.type === "delete") {
filesToRemove.push(fPath);

if (!/_manifest\.json$/.test(fPath)) {
const s = `${fPath}_manifest.json`;
// fs.existsSync(s)
if (server.getPublications().includes(s) && !filesToRemove.includes(s)) {
filesToRemove.push(s);
}
}
}
}

try {

debug("WATCHER: REMOVE => ", filesToRemove);
server.removePublications(filesToRemove);

debug("WATCHER: ADD => ", filesToAdd);
server.addPublications(filesToAdd);

} catch (ex) {
debug("WATCHER: ", ex);
}
});
// await watcherSubscription.unsubscribe();
})();

// filePaths = fs.readdirSync(filePath);
Expand Down
6 changes: 5 additions & 1 deletion src/http/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,11 @@ Disallow: /
if (this.isPublicationCached(filePath)) {
const pub = this.cachedPublication(filePath);
if (pub) {
pub.freeDestroy();
try {
pub.freeDestroy();
} catch (ex) {
debug(ex);
}
}
this.pathPublicationMap[filePath] = undefined;
delete this.pathPublicationMap[filePath];
Expand Down

0 comments on commit 41a8241

Please sign in to comment.