c9 / vfs-local

A VFS implementation for the local file-system.
MIT License
32 stars 39 forks source link

mkfile error throws instead of callback #6

Open maritz opened 11 years ago

maritz commented 11 years ago

In localfs.js#L343 there is an issue where the callback is called with an error but no meta object.

In the callback error case meta.stream is accessed. Since meta is undefined, this throws, making the entire point of a callback for the error case meaningless. (and as it so happens, this crashes my c9 install)

var callback = function (err, meta) {
            if (called) {
                if (err) {
                    if (meta.stream) meta.stream.emit("error", err);
                    else console.error(err.stack);
                }
                else if (meta.stream) meta.stream.emit("saved");
                return;
            }
            called = true;
            return realCallback.apply(this, arguments);
        };

        if (options.stream && !options.stream.readable) {
            return callback(new TypeError("options.stream must be readable."));
        }

Changing that like the following should fix this.

var callback = function (err, meta) {
            if (called) {
                if (err) {
                    if (meta && meta.stream) meta.stream.emit("error", err);
                    else console.error(err.stack);
                }
                else if (meta && meta.stream) meta.stream.emit("saved");
                return;
            }
            called = true;
            return realCallback.apply(this, arguments);
        };

        if (options.stream && !options.stream.readable) {
            return callback(new TypeError("options.stream must be readable."));
        }