UNPKG

10.9 kBMarkdownView Raw
1# NodeJS / TypeScript Readium-2 "streamer"
2
3NodeJS implementation (written in TypeScript) and HTTP micro-services (Express middleware) for https://github.com/readium/architecture/tree/master/streamer
4
5[![License](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](/LICENSE)
6
7## Build status
8
9[![NPM](https://img.shields.io/npm/v/r2-streamer-js.svg)](https://www.npmjs.com/package/r2-streamer-js) [![David](https://david-dm.org/readium/r2-streamer-js/status.svg)](https://david-dm.org/readium/r2-streamer-js) [![Travis](https://travis-ci.org/readium/r2-streamer-js.svg?branch=develop)](https://travis-ci.org/readium/r2-streamer-js)
10
11[Changelog](/CHANGELOG.md)
12
13## Prerequisites
14
151) https://nodejs.org NodeJS >= 8, NPM >= 5 (check with command line `node --version` and `npm --version`)
162) OPTIONAL: https://yarnpkg.com Yarn >= 1.0 (check with command line `yarn --version`)
17
18## GitHub repository
19
20https://github.com/readium/r2-streamer-js
21
22There is no [github.io](https://readium.github.io/r2-streamer-js) site for this project (no [gh-pages](https://github.com/readium/r2-streamer-js/tree/gh-pages) branch).
23
24## NPM package
25
26https://www.npmjs.com/package/r2-streamer-js
27
28Command line install:
29
30`npm install r2-streamer-js`
31OR
32`yarn add r2-streamer-js`
33
34...or manually add in your `package.json`:
35```json
36 "dependencies": {
37 "r2-streamer-js": "latest"
38 }
39```
40
41The JavaScript code distributed in the NPM package is usable as-is (no transpilation required), as it is automatically-generated from the TypeScript source.
42
43Several ECMAScript flavours are provided out-of-the-box: ES5, ES6-2015, ES7-2016, ES8-2017:
44
45https://unpkg.com/r2-streamer-js/dist/
46
47(alternatively, GitHub mirror with semantic-versioning release tags: https://github.com/edrlab/r2-streamer-js-dist/tree/develop/dist/ )
48
49The JavaScript code is not bundled, and it uses `require()` statement for imports (NodeJS style).
50
51More information about NodeJS compatibility:
52
53http://node.green
54
55Note that web-browser Javascript is currently not supported (only NodeJS runtimes).
56
57The 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.
58
59Example usage:
60
61```javascript
62// from the index file
63import { Server } from "r2-streamer-js";
64
65// ES5 import (assuming node_modules/r2-streamer-js/):
66import { Server } from "r2-streamer-js/dist/es5/src/http/server";
67
68// ... or alternatively using a convenient path alias in the TypeScript config (+ WebPack etc.):
69import { Server } from "@r2-streamer-js/http/server";
70```
71
72## Dependencies
73
74https://david-dm.org/readium/r2-streamer-js
75
76A [package-lock.json](https://github.com/readium/r2-streamer-js/blob/develop/package-lock.json) is provided (modern NPM replacement for `npm-shrinkwrap.json`).
77
78A [yarn.lock](https://github.com/readium/r2-streamer-js/blob/develop/yarn.lock) file is currently *not* provided at the root of the source tree.
79
80## Continuous Integration
81
82https://travis-ci.org/readium/r2-streamer-js
83
84TravisCI builds are triggered automatically at every Git "push" in the `develop` branch.
85
86## Live demos
87
88A test server app (not production-ready) is automatically deployed at every Git "push" in the `develop` branch:
89
90https://streamer.edrlab.org
91
92## Version(s), Git revision(s)
93
94NPM package (latest published):
95
96https://unpkg.com/r2-streamer-js/dist/gitrev.json
97
98Alternatively, GitHub mirror with semantic-versioning release tags:
99
100https://raw.githack.com/edrlab/r2-streamer-js-dist/develop/dist/gitrev.json
101
102Latest deployment:
103
104https://streamer.edrlab.org/version
105
106## Developer quick start
107
108Command line steps (NPM, but similar with YARN):
109
1101) `cd r2-streamer-js`
1112) `git status` (please ensure there are no local changes, especially in `package-lock.json` and the dependency versions in `package.json`)
1123) `rm -rf node_modules` (to start from a clean slate)
1134) `npm install`, or alternatively `npm ci` (both commands initialize the `node_modules` tree of package dependencies, based on the strict `package-lock.json` definition)
1145) `npm run build:all` (invoke the main build script: clean, lint, compile)
1156) `ls dist` (that's the build output which gets published as NPM package)
1167) `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)
1178) 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)
118
119## Documentation
120
121### Basic usage
122
123```javascript
124// ES5 import (assuming node_modules/r2-streamer-js/):
125import { Server } from "r2-streamer-js/dist/es5/src/http/server";
126
127// ... or alternatively using a convenient path alias in the TypeScript config (+ WebPack etc.):
128import { Server } from "@r2-streamer-js/http/server";
129
130// Constructor parameter is optional:
131// disableDecryption: true
132// disableOPDS
133// disableReaders: true
134// disableRemotePubUrl: true to deactivate
135const server = new Server({
136 disableDecryption: false, // deactivates the decryption of encrypted resources (Readium LCP).
137 disableOPDS: true, // deactivates the HTTP routes for the OPDS "micro services" (browser, converter)
138 disableReaders: true, // deactivates the built-in "readers" for ReadiumWebPubManifest (HTTP static host / route).
139 disableRemotePubUrl: true, // deactivates the HTTP route for loading a remote publication.
140 maxPrefetchLinks: 5, // Link HTTP header, with rel = prefetch, see server.ts MAX_PREFETCH_LINKS (default = 10)
141});
142
143// First parameter: port number, zero means default (3000),
144// unless specified via the environment variable `PORT` (process.env.PORT).
145// Tip: the NPM package `portfinder` can be used to automatically find an available port number.
146const url = await server.start(3000, false);
147
148// Second constructor parameter: if true, HTTPS instead of HTTP, using a randomly-generated self-signed certificate.
149// Also validates encrypted HTTP header during request-request cycles, so should only be used in runtime
150// contexts where the client side has access to the private encryption key (i.e. Electron app, see r2-navigator-js)
151console.log(server.isSecured()); // false
152
153// A client app that is capable of setting HTTP headers for every request originating from content webviews
154// can obtain the special encrypted header using this function:
155// (as used internally by the Electron-based `r2-navigator-js` component to secure the transport layer)
156const nameValuePair = server.getSecureHTTPHeader(url + "/PATH_TO_RESOURCE");
157console.log(nameValuePair.name);
158console.log(nameValuePair.value);
159
160// http://127.0.0.1:3000
161// Note that ports 80 and 443 (HTTPS) are always implicit (ommitted).
162console.log(url);
163
164// `serverInfo.urlScheme` ("http")
165// `serverInfo.urlHost` ("127.0.0.1")
166// `serverInfo.urlPort` (3000)
167console.log(server.serverInfo());
168
169// Calls `uncachePublications()` (see below)
170server.stop();
171
172console.log(server.isStarted()); // false
173```
174
175To serve a `/robots.txt` file that completely disables search robots:
176
177```javascript
178// Call this before `server.start()`
179server.preventRobots();
180```
181
182To add custom HTTP routes:
183
184```javascript
185// Call these before `server.start()`.
186// They are equivalent to `app.use()` and `app.get()`, where `app` is the underlying Express instance:
187
188server.expressUse("/static-files", express.static("/path/to/files", {
189 dotfiles: "ignore",
190 etag: true,
191 fallthrough: false,
192 immutable: true,
193 index: false,
194 maxAge: "1d",
195 redirect: false,
196}));
197
198server.expressGet(["/hello.html"], (req: express.Request, res: express.Response) => {
199
200 // Optionally, to add permissive CORS headers to the HTTP response
201 server.setResponseCORS(res);
202
203 res.status(200).send("<html><body>Hello</body></html>");
204});
205```
206
207To register publications references (local filesystem paths) inside the internal server state
208(which is used to create the OPDS2 feed, see below):
209
210```javascript
211// This can be called before or after `server.start()`:
212
213// the returned array contains URL routes to the ReadiumWebPubManifests,
214// e.g. `/pub/ID/manifest.json`, where `ID` is the base64 encoding of the registered path.
215// Note that the returned base64 URL path components are already URI-encoded (escaped).
216// (`=` and `/` are typically problematic edge-cases)
217const publicationURLs = server.addPublications(["/path/to/book.epub"]);
218
219// ...then:
220const publicationPaths = server.getPublications(); // ["/path/to/book.epub"]
221
222// ...and (calls `uncachePublication()`, see below):
223const publicationURLs = server.removePublications(["/path/to/book.epub"]);
224```
225
226To get the OPDS2 feed for the currently registered publications:
227
228```javascript
229// This launches a potentially time-consuming Node process that scans (loads) each registered Publication,
230// and stores the generated OPDS2 feed inside a temporary filesystem location.
231// So this returns `undefined` at the first call, and the client must invoke the function again later.
232// Note that both `addPublications()` and `removePublications()` clear the OPDS2 feed entirely,
233// requiring its subsequent re-generation (full scan of registered publication paths).
234// (poor design, but at this stage really just an OPDS2 demo without real use-case)
235const opds2 = server.publicationsOPDS();
236```
237
238To actually load+parse a publication reference (local filesystem path) into a ReadiumWebPubManifest
239Publication instance, stored in the server's state:
240
241```javascript
242// The Publication object model is defined in `r2-shared-js`
243const publication = await server.loadOrGetCachedPublication("/path/to/book.epub");
244
245// The above is basically a lazy-loader that checks the cache before loading+parsing a publication,
246// equivalent to:
247const publication = server.cachedPublication("/path/to/book.epub");
248if (!publication) {
249 publication = ...; // load and parse "/path/to/book.epub"
250 server.cachePublication("/path/to/book.epub", publication);
251}
252
253console.log(server.isPublicationCached("/path/to/book.epub")); // true
254
255// see also:
256// (calls `publication.freeDestroy()` to cleanup allocated objects in the Publication,
257// particularly the file handle to the underlying zip/EPUB/CBZ file)
258server.uncachePublication("/path/to/book.epub");
259server.uncachePublications();
260```
261
262Note that HTTP/remote publications URLs can be loaded into the server's cache
263and subsequently served by the streamer without prior registration via `addPublications()`.
264However, publications from the local filesytem will only be served when registered,
265even if they are cached (in other words, the HTTP route is disabled when the publication is non-registered).
266
267### HTTP API (built-in routes / micro-services)
268
269[docs/http.md](/docs/http.md)
270
271### Support for remote publications
272
273[docs/remote-epub.md](/docs/remote-epub.md)
274
275### Support for OPDS feeds
276
277[docs/opds.md](/docs/opds.md)
278
279### Support for encrypted content
280
281[docs/encryption.md](/docs/encryption.md)