Offroaders123 / NBTify

A library to read and write NBT files on the web!
http://npm.im/nbtify
MIT License
42 stars 5 forks source link

Accepting ArrayBufferLike for Reading #16

Closed Offroaders123 closed 1 year ago

Offroaders123 commented 1 year ago

Partially delayed because of this issue here.

Going to look more into different ways of implementing it first. Had to add a few extra unnecessary if checks that are only for type validation, and I wasn't fond of that. But, if that's the most straightforward way, then I'll probably just end up going with it.

Offroaders123 commented 1 year ago

Added! Super happy to have found a fix for the bug that was preventing this from being an easy addition. I made a comment about it in the thread mentioned above, here.

I think this will help the most when using the library on the web, for use with fetch() and Response(). Then you don't have to worry about making any intermediary Uint8Array objects all of the time.

This is what you could possibly do in the browser to open an NBT file from a URL, using fetch():

import NBT, { NBTData } from "nbtify"; // This would be an ESM URL instead of a package name

const result: NBTData = await fetch("./level.dat")
  .then(response => response.arrayBuffer())
  .then(NBT.read);
Offroaders123 commented 1 year ago

Here's a bit of a fever dream idea I thought of which kind of feels related to the demo above, ha.

Fetching NBT Directly