view mupdf-source/platform/wasm/examples/streams/custom-stream.js @ 46:7ee69f120f19 default tip

>>>>> tag v1.26.5+1 for changeset b74429b0f5c4
author Franz Glasner <fzglas.hg@dom66.de>
date Sat, 11 Oct 2025 17:17:30 +0200
parents b50eed0cc0ef
children
line wrap: on
line source

"use strict"

import * as fs from "fs"
import * as mupdf from "mupdf"

// Use synchronous file descriptor API with Node.
class NodeFileStream {
        constructor(path) {
                this.fd = fs.openSync(path)
        }
        fileSize() {
                return fs.fstatSync(this.fd).size
        }
        read(memory, offset, size, position) {
                return fs.readSync(this.fd, memory, offset, size, position)
        }
        close() {
                fs.closeSync(this.fd)
        }
}

// Use FileReaderSync on Blob/File objects in Web Workers.
class WorkerBlobStream {
	constructor(blob) {
		this.reader = new FileReaderSync()
		this.blob = blob
	}
	fileSize() {
		return this.blob.size
	}
	read(memory, offset, size, position) {
		var data = this.reader.readAsArrayBuffer(this.blob.slice(position, position + size))
		memory.set(new Uint8Array(data), offset)
		return data.byteLength
	}
	close() {
		this.reader = null
		this.blob = null
	}
}

/* to test:
var stm = new mupdf.Stream(new NodeFileStream("pdfref17.pdf"))
var doc = mupdf.Document.openDocument(stm, "application/pdf")
*/