Uploaded image for project: 'Apache Avro'
  1. Apache Avro
  2. AVRO-3322

JavaScript: Buffer is not defined in browser environment

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Blocker
    • Resolution: Fixed
    • None
    • 1.11.1, 1.12.0
    • js
    • Firefox 96 & Brave 1.34(based on chromium)

    Description

      When I use avro in my browser, I find that I get the `Buffer is undefined error`

      The code I use is simple, it comes from the github repository readme.md

      
      var avro = require('avro-js');
      var type = avro.parse({
        name: 'Pet',
        type: 'record',
        fields: [
          {name: 'kind', type: {name: 'Kind', type: 'enum', symbols: ['CAT', 'DOG']}},
          {name: 'name', type: 'string'}
        ]
      });
      var pet = {kind: 'CAT', name: 'Albert'};
      var buf = type.toBuffer(pet); // Serialized object.
      var obj = type.fromBuffer(buf); // {kind: 'CAT', name: 'Albert'}
      
      

      Later I traced the source code and found that Buffer is used directly in `/etc/browser/avro.js` and `/lib/schema.js`, which is not a problem in the node environment because the Buffer object is mounted globally, but it is a problem in the browser environment because using buffer polyfill will not mount the Buffer object globally, You need to  declare `var Buffer = require('buffer').Buffer` manually, so that it can be used in the browser environment without affecting the node environment, which also gets its Buffer from require('buffer').

      Attachments

        Issue Links

          Activity

            People

              mgrigorov Martin Tzvetanov Grigorov
              ruleeeer ruleeeer
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 3h 20m
                  3h 20m