Giter VIP home page Giter VIP logo

Comments (8)

mtth avatar mtth commented on May 28, 2024

Thanks for reporting this. Could you tell me more about how the error occurs? Are you using iframes and passing the schema from one to another?

from avsc.

dceejay avatar dceejay commented on May 28, 2024

HI,

I am trying to wrap this into a Node-RED node (http://nodered.org)
To do that I'm trying to pass in the schema as a message so it is being handled in a callback

this.on("input", function(msg) {
            if (msg.hasOwnProperty("schema")) {
                try { this.s = avro.parse(msg.schema); }
                catch(e) { node.log(e.toString()); node.error("Invalid schema def",msg.schema); }
            }

This always fails even with a valid schema - it is fine handled outside of the callback.

from avsc.

mtth avatar mtth commented on May 28, 2024

I see. I'm not familiar with node-red, but my suspicion is that each node gets run in a separate context (similarly to an iframe) which causes this error when objects are shared between nodes.

I also did some preliminary benchmarking and switching to Array.isArray looks acceptable. However there are other instanceof checks which aren't as easily replaced, so this will require a bit more thought to make sure everything works as expected.

Do you think you'd be able to extract a small test case or script to reproduce this failure?

from avsc.

dceejay avatar dceejay commented on May 28, 2024

Yes - I did see lots of other instanceof in there - though quite a few seemed to be to do with ****Type properties which being defined by you should all be ok... so maybe just the other Array ones need to be looked at. Certainly (so far with the simple schema I have used) just those two lines seemed to fix it - but I guess that's naive. The lines I spotted were 134, 678, 724, 966, 1158, 1219, 1250, 1297, 1358, 1400, and 1473..

In meantime I'll go try to create simple test case.

from avsc.

stevelr avatar stevelr commented on May 28, 2024

Matthew, you're right that a problem with instanceof is that objects created in a different context (window, iframe. etc.) don't share the same prototype chain.

btw, here's an implementation that works even if Array.isArray isn't defined:
https://github.com/yields/isArray/blob/master/index.js

This is consistent with the Polyfill definition here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray

-Steve

from avsc.

dceejay avatar dceejay commented on May 28, 2024

Simple test example

var avro = require('avsc');
var vm = require("vm");

var schema = '{ "name": "Pet", "type": "record", "fields": [ {"name": "kind", "type": {"name": "Kind", "type": "enum", "symbols": ["CAT", "DOG"]}}, {"name": "name", "type": "string"} ]}';

var func = 'var schema = JSON.parse(schema);\
    console.log(schema);\
    var type = avro.parse(JSON.parse(s));\
    var buf = type.toBuffer({"kind":"CAT", "name":"Albert"});\
    console.log(buf);\
    var val = type.fromBuffer(buf);\
    console.log(val);';

var sandbox = {
    avro:avro,
    console:console,
    schema:schema
}
var context = vm.createContext(sandbox);
var script = vm.createScript(func);
script.runInContext(context);
console.log(context.results);

from avsc.

mtth avatar mtth commented on May 28, 2024

Thanks for the example @dceejay! I adapted it and was able to reproduce the failure.

Rather than only replace array instanceof checks, I went ahead and replaced all uses of instanceof with context-agnostic checks (this should prevent surprises if sharing types across node-red nodes for example). Version 3.4.0 is available with those changes.

@stevelr - thanks for the extra information.

from avsc.

dceejay avatar dceejay commented on May 28, 2024

confirmed working here - thanks for the fix(es).

from avsc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.