view mupdf-source/platform/wasm/examples/streams/fetch-stream.js @ 23:3b13504f9d89

Use the official packaging.version.Version to parse version strings. While there revert the previons change to _int_rc(): it is not needed now.
author Franz Glasner <fzglas.hg@dom66.de>
date Fri, 19 Sep 2025 12:40:07 +0200
parents b50eed0cc0ef
children
line wrap: on
line source

"use strict"

import * as mupdf from "mupdf"

class FetchStream {
	constructor(url) {
		fetch(url)
			.then((response) => {
				if (!response.ok)
					throw new Error("HTTP " + response.status)
				return response.arrayBuffer()
			})
			.then((buffer) => {
				this.buffer = new Uint8Array(buffer)
			})
	}
	fileSize() {
		if (this.buffer)
			return this.buffer.byteLength
		return -1 // signal try later
	}
	read(memory, offset, size, position) {
		if (this.buffer) {
			size = Math.min(size, this.buffer.byteLength - position)
			memory.set(this.buffer.subarray(position, position + size), offset)
			return size
		}
		return -1 // signal try later
	}
	close() {
		this.buffer = null
	}
}

var stm = new mupdf.Stream(new FetchStream("https://mupdf.com/docs/mupdf_explored.pdf"))
function loop() {
	try {
		var doc = mupdf.Document.openDocument(stm, "application/pdf")
		var n = doc.countPages()
		console.log("Document has " + n + " pages!")
	} catch (err) {
		if (err === "TRYLATER") {
			console.log("Waiting...")
			setTimeout(loop, 1000)
		} else {
			throw err
		}
	}
}
loop()