ewei068 / anki-reader

An anki file reader module for Browser and Node
https://www.npmjs.com/package/anki-reader
3 stars 2 forks source link

TypeError: blob.arrayBuffer is not a function when using node js #1

Open crasu opened 5 months ago

crasu commented 5 months ago

Description

When running the sample code in nodejs I get the error above. Does this only work with bun?

TypeError: blob.arrayBuffer is not a function
    at BlobReader.readUint8Array (file:///home/christian/projects/funktionieren/test/node_modules/@zip.js/zip.js/lib/core/io.js:192:32)
    at readUint8Array (file:///home/christian/projects/funktionieren/test/node_modules/@zip.js/zip.js/lib/core/io.js:704:16)
    at Object.pull (file:///home/christian/projects/funktionieren/test/node_modules/@zip.js/zip.js/lib/core/io.js:78:30)
    at ensureIsPromise (node:internal/webstreams/util:185:19)
    at readableStreamDefaultControllerCallPullIfNeeded (node:internal/webstreams/readablestream:2354:5)
    at node:internal/webstreams/readablestream:2445:7

Code

import { readAnkiPackage } from 'anki-reader';

const ankiFile = "grundwortschatz-nrw.apkg";
readAnkiPackage(ankiFile)
  .then((extractedPackage) => {
    const { collection, media } = extractedPackage;

    const decks = collection.getDecks();
    for (const [deckId, deck] of Object.entries(decks)) {
      console.log(deckId, deck.getRawDeck());
      for (const [cardId, card] of Object.entries(deck.getCards())) {
        console.log(cardId, card.getRawCard());
      }
    }

    for (const [name, blob] of Object.entries(media)) {
      console.log(name, blob);
    }
  })

Node version

v20.11.0

ewei068 commented 5 months ago

Since you're not running this in a browser, you may need some polyfills

import 'web-streams-polyfill'; // Polyfill TransformStream globally
import 'isomorphic-fetch'; // Polyfill fetch globally
import { Blob } from 'blob-polyfill'; // Polyfill Blob globally
globalThis.Blob = Blob;