custom-stream.js 1.1 KB

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