NodeJS / TypeScript Readium-2 "streamer"
NodeJS implementation (written in TypeScript) and HTTP micro-services (Express middleware) for https://github.com/readium/architecture/tree/master/streamer
Build status
Changelog
Prerequisites
- https://nodejs.org NodeJS >= 8, NPM >= 5 (check with command line
node --version
and npm --version
) - OPTIONAL: https://yarnpkg.com Yarn >= 1.0 (check with command line
yarn --version
)
GitHub repository
https://github.com/readium/r2-streamer-js
There is no github.io site for this project (no gh-pages branch).
NPM package
https://www.npmjs.com/package/r2-streamer-js
Command line install:
npm install r2-streamer-js
OR
yarn add r2-streamer-js
...or manually add in your package.json
:
"dependencies": {
"r2-streamer-js": "latest"
}
The JavaScript code distributed in the NPM package is usable as-is (no transpilation required), as it is automatically-generated from the TypeScript source.
Several ECMAScript flavours are provided out-of-the-box: ES5, ES6-2015, ES7-2016, ES8-2017:
https://unpkg.com/r2-streamer-js/dist/
(alternatively, GitHub mirror with semantic-versioning release tags: https://github.com/edrlab/r2-streamer-js-dist/tree/develop/dist/ )
The JavaScript code is not bundled, and it uses require()
statement for imports (NodeJS style).
More information about NodeJS compatibility:
http://node.green
Note that web-browser Javascript is currently not supported (only NodeJS runtimes).
The type definitions (aka "typings") are included as *.d.ts
files in ./node_modules/r2-streamer-js/dist/**
, so this package can be used directly in a TypeScript project.
Example usage:
import { Server } from "r2-streamer-js";
import { Server } from "r2-streamer-js/dist/es5/src/http/server";
import { Server } from "@r2-streamer-js/http/server";
Dependencies
https://david-dm.org/readium/r2-streamer-js
A package-lock.json is provided (modern NPM replacement for npm-shrinkwrap.json
).
A yarn.lock file is currently not provided at the root of the source tree.
Continuous Integration
https://travis-ci.org/readium/r2-streamer-js
TravisCI builds are triggered automatically at every Git "push" in the develop
branch.
Live demos
A test server app (not production-ready) is automatically deployed at every Git "push" in the develop
branch:
https://streamer.edrlab.org
Version(s), Git revision(s)
NPM package (latest published):
https://unpkg.com/r2-streamer-js/dist/gitrev.json
Alternatively, GitHub mirror with semantic-versioning release tags:
https://raw.githack.com/edrlab/r2-streamer-js-dist/develop/dist/gitrev.json
Latest deployment:
https://streamer.edrlab.org/version
Developer quick start
Command line steps (NPM, but similar with YARN):
cd r2-streamer-js
git status
(please ensure there are no local changes, especially in package-lock.json
and the dependency versions in package.json
)rm -rf node_modules
(to start from a clean slate)npm install
, or alternatively npm ci
(both commands initialize the node_modules
tree of package dependencies, based on the strict package-lock.json
definition)npm run build:all
(invoke the main build script: clean, lint, compile)ls dist
(that's the build output which gets published as NPM package)npm run server-debug -- PATH_TO_EPUB_OR_DIR " -1"
(ES8-2017 dist, path is relative or absolute, -1 means no limits for HTTP header prefetch Links)- or:
npm run start -- 99
(ES6-2015 dist, default ./misc/epubs
folder, the 99 value overrides the default maximum number of HTTP header prefetch Links)
Documentation
Basic usage
import { Server } from "r2-streamer-js/dist/es5/src/http/server";
import { Server } from "@r2-streamer-js/http/server";
const server = new Server({
disableDecryption: false,
disableOPDS: true,
disableReaders: true,
disableRemotePubUrl: true,
maxPrefetchLinks: 5,
});
const url = await server.start(3000, false);
console.log(server.isSecured());
const nameValuePair = server.getSecureHTTPHeader(url + "/PATH_TO_RESOURCE");
console.log(nameValuePair.name);
console.log(nameValuePair.value);
console.log(url);
console.log(server.serverInfo());
server.stop();
console.log(server.isStarted());
To serve a /robots.txt
file that completely disables search robots:
server.preventRobots();
To add custom HTTP routes:
server.expressUse("/static-files", express.static("/path/to/files", {
dotfiles: "ignore",
etag: true,
fallthrough: false,
immutable: true,
index: false,
maxAge: "1d",
redirect: false,
}));
server.expressGet(["/hello.html"], (req: express.Request, res: express.Response) => {
server.setResponseCORS(res);
res.status(200).send("<html><body>Hello</body></html>");
});
To register publications references (local filesystem paths) inside the internal server state
(which is used to create the OPDS2 feed, see below):
const publicationURLs = server.addPublications(["/path/to/book.epub"]);
const publicationPaths = server.getPublications();
const publicationURLs = server.removePublications(["/path/to/book.epub"]);
To get the OPDS2 feed for the currently registered publications:
const opds2 = server.publicationsOPDS();
To actually load+parse a publication reference (local filesystem path) into a ReadiumWebPubManifest
Publication instance, stored in the server's state:
const publication = await server.loadOrGetCachedPublication("/path/to/book.epub");
const publication = server.cachedPublication("/path/to/book.epub");
if (!publication) {
publication = ...;
server.cachePublication("/path/to/book.epub", publication);
}
console.log(server.isPublicationCached("/path/to/book.epub"));
server.uncachePublication("/path/to/book.epub");
server.uncachePublications();
Note that HTTP/remote publications URLs can be loaded into the server's cache
and subsequently served by the streamer without prior registration via addPublications()
.
However, publications from the local filesytem will only be served when registered,
even if they are cached (in other words, the HTTP route is disabled when the publication is non-registered).
HTTP API (built-in routes / micro-services)
docs/http.md
Support for remote publications
docs/remote-epub.md
Support for OPDS feeds
docs/opds.md
Support for encrypted content
docs/encryption.md