| 1 |
"use strict" |
| 2 |
|
| 3 |
import * as fs from "fs" |
| 4 |
import * as mupdf from "mupdf" |
| 5 |
|
| 6 |
// Use synchronous file descriptor API with Node. |
| 7 |
class NodeFileStream { |
| 8 |
constructor(path) { |
| 9 |
this.fd = fs.openSync(path) |
| 10 |
} |
| 11 |
fileSize() { |
| 12 |
return fs.fstatSync(this.fd).size |
| 13 |
} |
| 14 |
read(memory, offset, size, position) { |
| 15 |
return fs.readSync(this.fd, memory, offset, size, position) |
| 16 |
} |
| 17 |
close() { |
| 18 |
fs.closeSync(this.fd) |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
// Use FileReaderSync on Blob/File objects in Web Workers. |
| 23 |
class WorkerBlobStream { |
| 24 |
constructor(blob) { |
| 25 |
this.reader = new FileReaderSync() |
| 26 |
this.blob = blob |
| 27 |
} |
| 28 |
fileSize() { |
| 29 |
return this.blob.size |
| 30 |
} |
| 31 |
read(memory, offset, size, position) { |
| 32 |
var data = this.reader.readAsArrayBuffer(this.blob.slice(position, position + size)) |
| 33 |
memory.set(new Uint8Array(data), offset) |
| 34 |
return data.byteLength |
| 35 |
} |
| 36 |
close() { |
| 37 |
this.reader = null |
| 38 |
this.blob = null |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
/* to test: |
| 43 |
var stm = new mupdf.Stream(new NodeFileStream("pdfref17.pdf")) |
| 44 |
var doc = mupdf.Document.openDocument(stm, "application/pdf") |
| 45 |
*/ |