Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

haxe.io.Bytes.getData(): BytesData != ArrayBuffer

Aperta
#173 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
28/100
Tipo di issue
Bug
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
javascript, node.js
Ambito
backend

Direzione di ricerca

Non è indicato alcun file sorgente né alcun test. Individua le implementazioni per browser e Node.js di haxe.io.Bytes.getData(), confronta i tipi restituiti e esamina prima gli esempi di ArrayBuffer e Buffer dell’issue. Il lavoro è completo quando il comportamento della piattaforma è chiaramente stabilito e qualsiasi modifica necessaria di coerenza o documentazione è stata coperta.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Hi! I'm not sure if that is even an issue, but it took me a while to understand the difference between the Browser based implementation and the one used by node (electron). So I figured others might run into this very specific case as well:

In a nutshell this is the actual runtime difference: Bytes.getData() returns ArrayBuffer in the browser but Buffer in node (electron):

// Browser:
haxe.io.Bytes.getData() : js.lib.ArrayBuffer
// Node (electron):
haxe.io.Bytes.getData() : js.node.Buffer

If you're interested - first the JavaScript runtime error I ran into:

Uncaught (in promise) TypeError: Failed to execute 'decodeAudioData' on 'BaseAudioContext': parameter 1 is not of type 'ArrayBuffer'.
    at AudioContextWaveformVc._onArrayBuffer (AudioContextWaveformVc.hx:126)

This is my little handler causing problems where _audioContext is an AudioContext instance:

function _onArrayBuffer( arrayBuffer : ArrayBuffer ) {
    _audioContext.decodeAudioData( arrayBuffer, _onDecodeAudioSuccess, _onDecodeAudioError );
}

This is my bytes receiving handler, which extracts the needed ArrayBuffer and forwards it:

function _onBytes( data : Bytes ): Void {
    var arrayBuffer : ArrayBuffer = data.getData();
    _onArrayBuffer( arrayBuffer );
}

So if I use Google Chrome and Http to load the bytes it works fine:

var http : Http = new Http( url );
    http.onBytes = _onBytes;
    http.request( true );

BUT if I use the File API to read the bytes, decoding audio throws the TypeError:

var bytes : Bytes = File.getBytes( path );
_onBytes( bytes );

After some research I figured out, that running node (electron), Bytes.getData() actually returns a js.node.Buffer instance:
Bildschirmfoto 2020-09-04 um 12 44 23

So in my case I had to convert the node buffer into an array buffer, which led me to this quite old stack overflow answer:
https://stackoverflow.com/questions/8609289/convert-a-binary-nodejs-buffer-to-javascript-arraybuffer/31394257#31394257

Just in case you need that:

class BufferUtil {
    public static function toArrayBuffer(buf:Buffer): ArrayBuffer {
        var ab = new ArrayBuffer(buf.length);
        var view = new Uint8Array(ab);
        for (i in 0...buf.length) {
            view[i] = buf[i];
        }
        return ab;
    }
}

So is Bytes.getData() not consistent on all platforms?
Is this expected behaviour?
Or is this just a very rare edge case?

Lingua principale
Haxe
Stelle
177
Fork
58
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di HaxeFoundation/hxnodejs

Tutte le issue di HaxeFoundation/hxnodejs

Issue simili

Altre issue su Backend & API Design

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.