[Pkg-javascript-commits] [node-leveldown] 39/492: return to classical prototypal classes

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Sun Jul 6 17:13:42 UTC 2014


This is an automated email from the git hooks/post-receive script.

andrewrk-guest pushed a commit to annotated tag rocksdb-0.10.1
in repository node-leveldown.

commit c57374c8bd41e6f901c351936cf84acf9ee9a701
Author: Rod Vagg <rod at vagg.org>
Date:   Tue Sep 4 10:45:43 2012 +1000

    return to classical prototypal classes
---
 lib/creator.js            | 101 -------------
 lib/levelup.js            | 372 +++++++++++++++++++++++-----------------------
 lib/read-stream.js        | 227 +++++++++++++---------------
 lib/util.js               |   6 +
 lib/write-stream.js       | 267 ++++++++++++++++-----------------
 package.json              |   3 +
 test/binary-test.js       |   4 -
 test/common.js            |   6 +-
 test/copy-test.js         |   3 -
 test/json-test.js         |   5 +-
 test/read-stream-test.js  |   6 -
 test/simple-test.js       |  21 +--
 test/write-stream-test.js |   5 -
 13 files changed, 437 insertions(+), 589 deletions(-)

diff --git a/lib/creator.js b/lib/creator.js
deleted file mode 100644
index 278b812..0000000
--- a/lib/creator.js
+++ /dev/null
@@ -1,101 +0,0 @@
-/* Copyright (c) 2012 Rod Vagg <@rvagg> */
-
-// I probably wouldn't recommend reusing this at this stage, it's more of an experiment; we'll see.
-
-function defineReadOnlyProperty (obj, key, value) {
-  Object.defineProperty(obj, key, {
-      value        : value
-    , writeable    : false
-    , enumerable   : true
-    , configurable : false
-  })
-}
-
-function Creator (name) {
-  this._name = name
-}
-
-Creator.prototype = {
-    setBase: function (base) {
-      this._base = base
-      return this
-    }
-
-  , setConstructor: function (constructor) {
-      this._constructor = constructor
-      return this
-    }
-
-  , setPrototype: function (proto) {
-      this._proto = proto
-      return this
-    }
-
-  , setMethods: function (methods) {
-      this._methods = methods
-      return this
-    }
-
-  , setPrivateMethods: function (privateMethods) {
-      this._privateMethods = privateMethods
-      return this
-    }
-
-  , setReadOnlyProperties: function (readOnlyPropertiesCallback) {
-      this._readOnlyPropertiesCallback = readOnlyPropertiesCallback
-      return this
-    }
-
-  , setPrivateReadOnlyProperties: function (privateReadOnlyPropertiesCallback) {
-      this._privateReadOnlyPropertiesCallback = privateReadOnlyPropertiesCallback
-      return this
-    }
-
-  , setGetters: function (getters) {
-      this._getters = getters
-      return this
-    }
-
-  , create: function () {
-      var args         = Array.prototype.slice.call(arguments)
-        , obj          = !!this._base ? new this._base() : {}
-        , ctx          = { pub: obj }
-        , readOnly     = this._readOnlyPropertiesCallback && this._readOnlyPropertiesCallback(args)
-        , privReadOnly = this._privateReadOnlyPropertiesCallback && this._privateReadOnlyPropertiesCallback(args)
-        , name         = this._name
-
-      if (this._proto)
-        obj.__proto__ = Object.create(this._proto.prototype)
-
-      readOnly && Object.keys(readOnly).forEach(function (p) {
-        defineReadOnlyProperty(obj, p, readOnly[p])
-        defineReadOnlyProperty(ctx, p, readOnly[p])
-      })
-
-      privReadOnly && Object.keys(privReadOnly).forEach(function (p) {
-        defineReadOnlyProperty(ctx, p, privReadOnly[p])
-      })
-
-      this._methods && Object.keys(this._methods).forEach(function (m) {
-        obj[m] = ctx[m] = this._methods[m].bind(ctx)
-      }.bind(this))
-
-      this._privateMethods && Object.keys(this._privateMethods).forEach(function (m) {
-        ctx[m] = this._privateMethods[m].bind(ctx)
-      }.bind(this))
-
-      this._getters && Object.keys(this._getters).forEach(function (g) {
-        obj.__defineGetter__(g, this._getters[g].bind(ctx))
-      }.bind(this))
-
-      if (name)
-        obj.toString = function () { return name }
-
-      if (this._constructor)
-        this._constructor.apply(ctx, args)
-
-      return obj
-    }
-}
-
-module.exports.Creator = Creator
\ No newline at end of file
diff --git a/lib/levelup.js b/lib/levelup.js
index 4352c74..ff067ad 100644
--- a/lib/levelup.js
+++ b/lib/levelup.js
@@ -7,8 +7,8 @@ var bridge       = require('bindings')('levelup.node')
   , writeStream  = require('./write-stream')
   , toEncoding   = require('./util').toEncoding
   , toBuffer     = require('./util').toBuffer
+  , extend       = require('./util').extend
   , EventEmitter = require('events').EventEmitter
-  , Creator      = require('./creator').Creator
 
   , defaultOptions = {
         createIfMissing : false
@@ -24,11 +24,6 @@ var bridge       = require('bindings')('levelup.node')
       return eo
     }('hex utf8 utf-8 ascii binary base64 ucs2 ucs-2 utf16le utf-16le'.split(' ')))
 
-  , extend = function (dst, src) {
-      for (var p in src) dst[p] = src[p]
-      return dst
-    }
-
   , getOptions = function (options, globalOptions) {
       if (typeof options == 'string') // just an encoding
         options = extend({}, encodingOpts[options] || encodingOpts[defaultOptions.encoding])
@@ -41,214 +36,211 @@ var bridge       = require('bindings')('levelup.node')
       return callback_
     }
 
-  , Database = {
-        open: function (callback) {
-          var execute = function () {
-                var db = bridge.createDatabase()
-                db.open(this.location, this.options, function (err) {
-                  if (err) {
-                    err = new errors.OpenError(err)
-                    if (callback)
-                      return callback(err)
-                    this.pub.emit('error', err)
-                  } else {
-                    this.db = db
-                    callback(null, this.pub)
-                    this.pub.emit('ready')
-                  }
-                }.bind(this))
-              }.bind(this)
-
-          if (this.isOpen())
-            this.close(execute)
-          else
-            execute()
-        }
-
-        //TODO: we can crash Node by submitting an operation between close() and the actual closing of the database
-      , close: function (callback) {
-          if (this.isOpen()) {
-            this.db.close(function () {
-              this.pub.emit('closed')
-              callback.apply(null, arguments)
-            }.bind(this))
-            this.db = null
-          } else {
-            callback()
-          }
-        }
-
-      , isOpen: function () {
-          return !!this.db
-        }
-
-      , get: function (key_, options_, callback_) {
-          var callback = getCallback(options_, callback_)
-            , options, key, err
-
-          if (this.isOpen()) {
-            options  = getOptions(options_, this.options)
-            key      = toBuffer(key_, options.keyEncoding || options.encoding)
-            this.db.get(key, options, function (err, value) {
-              if (err) {
-                err = new errors.NotFoundError('Key not found in database [' + key_ + ']')
-                if (callback)
-                  return callback(err)
-                throw err
-              }
-              callback && callback(null, toEncoding(value, options.valueEncoding || options.encoding), key_)
-            })
-          } else {
-            err = new errors.ReadError('Database has not been opened')
-            if (callback)
-              callback(err)
-            else
-              throw err
-          }
-        }
-
-      , put: function (key, value, options_, callback_) {
-          var callback = getCallback(options_, callback_)
-            , options, err
-
-          if (this.isOpen()) {
-            options  = getOptions(options_, this.options)
-            key      = toBuffer(key,   options.keyEncoding   || options.encoding)
-            value    = toBuffer(value, options.valueEncoding || options.encoding)
-            this.db.put(key, value, options, function (err) {
+function LevelUP (location, options) {
+  this.__proto__.__proto__ = EventEmitter.prototype
+  EventEmitter.call(this)
+  this._options = extend(extend({}, defaultOptions), options)
+  this._location = location
+}
+
+LevelUP.prototype = {
+    open: function (callback) {
+      var execute = function () {
+            var db = bridge.createDatabase()
+            db.open(this._location, this._options, function (err) {
               if (err) {
-                err = new errors.WriteError(err)
+                err = new errors.OpenError(err)
                 if (callback)
                   return callback(err)
-                this.pub.emit('error', err)
+                this.emit('error', err)
               } else {
-                this.pub.emit('put', key, value)
-                callback && callback(null, key, value)
+                this._db = db
+                callback(null, this)
+                this.emit('ready')
               }
             }.bind(this))
-          } else {
-            err = new errors.WriteError('Database has not been opened')
+          }.bind(this)
+
+      if (this.isOpen())
+        this.close(execute)
+      else
+        execute()
+    }
+
+    //TODO: we can crash Node by submitting an operation between close() and the actual closing of the database
+  , close: function (callback) {
+      if (this.isOpen()) {
+        this._db.close(function () {
+          this.emit('closed')
+          callback.apply(null, arguments)
+        }.bind(this))
+        this._db = null
+      } else {
+        callback()
+      }
+    }
+
+  , isOpen: function () {
+      return !!this._db
+    }
+
+  , get: function (key_, options_, callback_) {
+      var callback = getCallback(options_, callback_)
+        , options, key, err
+
+      if (this.isOpen()) {
+        options  = getOptions(options_, this._options)
+        key      = toBuffer(key_, options.keyEncoding || options.encoding)
+        this._db.get(key, options, function (err, value) {
+          if (err) {
+            err = new errors.NotFoundError('Key not found in database [' + key_ + ']')
             if (callback)
-              callback(err)
-            else
-              throw err
+              return callback(err)
+            throw err
           }
-        }
-
-      , del: function (key, options_, callback_) {
-          var callback = getCallback(options_, callback_)
-            , options, err
+          callback && callback(null, toEncoding(value, options.valueEncoding || options.encoding), key_)
+        })
+      } else {
+        err = new errors.ReadError('Database has not been opened')
+        if (callback)
+          callback(err)
+        else
+          throw err
+      }
+    }
 
-          if (this.isOpen()) {
-            options  = getOptions(options_, this.options)
-            key      = toBuffer(key,   options.keyEncoding   || options.encoding)
-            this.db.del(key, options, function (err) {
-              if (err) {
-                err = new errors.WriteError(err)
-                if (callback)
-                  return callback(err)
-                this.pub.emit('error', err)
-              } else {
-                this.pub.emit('del', key)
-                callback && callback(null, key)
-              }
-            }.bind(this))
-          } else {
-            err = new errors.WriteError('Database has not been opened')
+  , put: function (key, value, options_, callback_) {
+      var callback = getCallback(options_, callback_)
+        , options, err
+
+      if (this.isOpen()) {
+        options  = getOptions(options_, this._options)
+        key      = toBuffer(key,   options.keyEncoding   || options.encoding)
+        value    = toBuffer(value, options.valueEncoding || options.encoding)
+        this._db.put(key, value, options, function (err) {
+          if (err) {
+            err = new errors.WriteError(err)
             if (callback)
-              callback(err)
-            else
-              throw err
+              return callback(err)
+            this.emit('error', err)
+          } else {
+            this.emit('put', key, value)
+            callback && callback(null, key, value)
           }
-        }
+        }.bind(this))
+      } else {
+        err = new errors.WriteError('Database has not been opened')
+        if (callback)
+          callback(err)
+        else
+          throw err
+      }
+    }
 
-      , batch: function (arr, options_, callback_) {
-          var callback = getCallback(options_, callback_)
-            , empty    = {}
-            , options, keyEncoding, valueEncoding, err
+  , del: function (key, options_, callback_) {
+      var callback = getCallback(options_, callback_)
+        , options, err
 
-          if (!this.isOpen()) {
-            err = new errors.WriteError('Database has not been opened')
+      if (this.isOpen()) {
+        options  = getOptions(options_, this._options)
+        key      = toBuffer(key,   options.keyEncoding   || options.encoding)
+        this._db.del(key, options, function (err) {
+          if (err) {
+            err = new errors.WriteError(err)
             if (callback)
               return callback(err)
-            else
-              throw err
+            this.emit('error', err)
+          } else {
+            this.emit('del', key)
+            callback && callback(null, key)
           }
+        }.bind(this))
+      } else {
+        err = new errors.WriteError('Database has not been opened')
+        if (callback)
+          callback(err)
+        else
+          throw err
+      }
+    }
 
-          options       = getOptions(options_, this.options)
-          keyEncoding   = options.keyEncoding   || options.encoding
-          valueEncoding = options.valueEncoding || options.encoding
-          arr           = arr.map(function (e) {
-            if (e.type !== undefined && e.key !== undefined) {
-              var o = {
-                  type  : e.type
-                , key   : toBuffer(e.key, keyEncoding)
-              }
-              if (e.value !== undefined)
-                o.value = toBuffer(e.value, valueEncoding)
-              return o
-            }
-            return empty
-          })
-          this.db.batch(arr, options, function (err) {
-            if (err) {
-              err = new errors.WriteError(err)
-              if (callback)
-                return callback(err)
-              this.pub.emit('error', err)
-            } else {
-              this.pub.emit('batch', arr)
-              callback && callback()
-            }
-          }.bind(this))
-        }
+  , batch: function (arr, options_, callback_) {
+      var callback = getCallback(options_, callback_)
+        , empty    = {}
+        , options, keyEncoding, valueEncoding, err
+
+      if (!this.isOpen()) {
+        err = new errors.WriteError('Database has not been opened')
+        if (callback)
+          return callback(err)
+        else
+          throw err
+      }
 
-      , readStream: function (options) {
-          if (typeof options != 'object')
-            options = {}
-          return readStream.create(
-              options
-            , this
-            , function () {
-                return bridge.createIterator(this.db, options)
-              }.bind(this)
-          )
+      options       = getOptions(options_, this._options)
+      keyEncoding   = options.keyEncoding   || options.encoding
+      valueEncoding = options.valueEncoding || options.encoding
+      arr           = arr.map(function (e) {
+        if (e.type !== undefined && e.key !== undefined) {
+          var o = {
+              type  : e.type
+            , key   : toBuffer(e.key, keyEncoding)
+          }
+          if (e.value !== undefined)
+            o.value = toBuffer(e.value, valueEncoding)
+          return o
         }
-
-      , writeStream: function (options) {
-         return writeStream.create(
-              options || {}
-            , this
-          )
+        return empty
+      })
+      this._db.batch(arr, options, function (err) {
+        if (err) {
+          err = new errors.WriteError(err)
+          if (callback)
+            return callback(err)
+          this.emit('error', err)
+        } else {
+          this.emit('batch', arr)
+          callback && callback()
         }
+      }.bind(this))
     }
 
-  , databaseCreator = new Creator('LevelUPDatabase')
-      .setBase(EventEmitter)
-      .setMethods(Database)
-      .setReadOnlyProperties(function (args) {
-        var props = { options: {} }
-        Object.keys(defaultOptions).forEach(function (p) {
-          props.options[p] = (args[1] && args[1][p]) || defaultOptions[p]
-        })
-        props.location = args[0]
-        return props
-      })
-
-  , createDatabase = function (location, options, callback) {
-      if (typeof options == 'function') {
-        callback = options
+  , readStream: function (options) {
+      if (typeof options != 'object')
         options = {}
-      }
+      return readStream.create(
+          options
+        , this
+        , function (options) {
+            return bridge.createIterator(this._db, options)
+          }.bind(this)
+      )
+    }
 
-      if (typeof callback != 'function')
-        throw new errors.InitializationError('Must provide a callback function')
-      if (typeof location != 'string')
-        return callback(new errors.InitializationError('Must provide a location for the database'))
+  , writeStream: function (options) {
+     return writeStream.create(
+          options || {}
+        , this
+      )
+    }
 
-      var db = databaseCreator.create(location, options)
-      db.open(callback)
+  , toString: function () {
+      return 'LevelUP'
     }
+}
+
+module.exports = function (location, options, callback) {
+  if (typeof options == 'function') {
+    callback = options
+    options = {}
+  }
+
+  if (typeof callback != 'function')
+    throw new errors.InitializationError('Must provide a callback function')
+  if (typeof location != 'string')
+    return callback(new errors.InitializationError('Must provide a location for the database'))
+
+  ;new LevelUP(location, options).open(callback)
+}
 
-module.exports      = createDatabase
-module.exports.copy = require('./util').copy
+module.exports.copy = require('./util').copy
\ No newline at end of file
diff --git a/lib/read-stream.js b/lib/read-stream.js
index a597d81..344049f 100644
--- a/lib/read-stream.js
+++ b/lib/read-stream.js
@@ -5,141 +5,124 @@ var Stream       = require("stream").Stream
 
   , toEncoding   = require('./util').toEncoding
   , toBuffer     = require('./util').toBuffer
-  , Creator      = require('./creator').Creator
+  , extend       = require('./util').extend
 
   , defaultOptions = {}
 
-  , ReadStream = function (options, db, iteratorFactory) {
-      Stream.call(this)
-      this._options = options
-      this._status  = 'ready'
-      this._dataEvent = 'data'
-      this.readable = true
-      this.writable = false
-
-      if (this._options.start)
-        this._options.start = toBuffer(this._options.start)
-      if (this._options.end)
-        this._options.end = toBuffer(this._options.end)
-
-      var ready = function () {
-        if (this._status == 'ended')
-          return
-        this._iterator = iteratorFactory()
-        this.pub.emit('ready')
-        this._read()
-      }.bind(this)
-
-      if (db.isOpen())
-        process.nextTick(ready)
-      else
-        db.pub.once('ready', ready)
+function ReadStream (options, db, iteratorFactory) {
+  this.__proto__.__proto__ = Stream.prototype
+  Stream.call(this)
+
+  this._status  = 'ready'
+  this._dataEvent = 'data'
+  this.readable = true
+  this.writable = false
+
+  this._options = extend(extend({}, defaultOptions), options)
+  if (this._options.start)
+    this._options.start = toBuffer(this._options.start)
+  if (this._options.end)
+    this._options.end = toBuffer(this._options.end)
+
+  var ready = function () {
+    if (this._status == 'ended')
+      return
+    this._iterator = iteratorFactory(this._options)
+    this.emit('ready')
+    this._read()
+  }.bind(this)
+
+  if (db.isOpen())
+    process.nextTick(ready)
+  else
+    db.once('ready', ready)
+}
+
+ReadStream.prototype = {
+    destroy: function () {
+      this._status = 'destroyed'
+      this._cleanup()
     }
 
-  , Methods = {
-        destroy: function () {
-          this._status = 'destroyed'
-          this._cleanup()
-        }
+  , pause: function () {
+      if (this._status != 'ended' && !/\+paused$/.test(this._status)) {
+        this.emit('pause')
+        this._status += '+paused' // preserve existing status
+      }
+    }
 
-      , pause: function () {
-          if (this._status != 'ended' && !/\+paused$/.test(this._status)) {
-            this.pub.emit('pause')
-            this._status += '+paused' // preserve existing status
-          }
-        }
+  , resume: function () {
+      if (this._status != 'ended') {
+        this.emit('resume')
+        this._status = this._status.replace(/\+paused$/, '')
+        this._read()
+      }
+    }
 
-      , resume: function () {
-          if (this._status != 'ended') {
-            this.pub.emit('resume')
-            this._status = this._status.replace(/\+paused$/, '')
-            this._read()
+  , pipe: function (dest) {
+      if (typeof dest.add == 'function' && this._options.type == 'fstream') {
+        this._dataEvent = 'entry'
+        this.on('entry', function (data) {
+          var entry = new BufferStream()
+          entry.path = data.key.toString()
+          entry.type = 'File'
+          entry.props = {
+              type: 'File'
+            , path: data.key.toString()
           }
-        }
-
-      , pipe: function (dest) {
-          if (typeof dest.add == 'function' && this._options.type == 'fstream') {
-            this._dataEvent = 'entry'
-            this.pub.on('entry', function (data) {
-              var entry = new BufferStream()
-              entry.path = data.key.toString()
-              entry.type = 'File'
-              entry.props = {
-                  type: 'File'
-                , path: data.key.toString()
-              }
-              entry.once('data', process.nextTick.bind(null, entry.end.bind(entry)))
-              entry.pause()
-              if (dest.add(entry) === false) {
-                this.pause()
-              }
-              entry.write(data.value)
-            }.bind(this))
+          entry.once('data', process.nextTick.bind(null, entry.end.bind(entry)))
+          entry.pause()
+          if (dest.add(entry) === false) {
+            this.pause()
           }
-          return Stream.prototype.pipe.apply(this.pub, arguments)
-        }
+          entry.write(data.value)
+        }.bind(this))
+      }
+      return Stream.prototype.pipe.apply(this, arguments)
     }
 
-  , PrivateMethods = {
-        _read: function () {
-          if (this._status == 'ready') {
-            this._status = 'reading'
-            this._iterator.next(this._cleanup.bind(this), this._onData.bind(this))
-          }
-        }
-
-      , _onData: function (err, key, value) {
-          if (err)
-            return this._cleanup(err)
-          if (this._status == 'ended')
-            return
-          if (/^reading/.test(this._status))
-            this._status = this._status.replace(/^reading/, 'ready')
-          this._read()
-          this.pub.emit(this._dataEvent, {
-              key   : toEncoding(key   , this._options.keyEncoding   || this._options.encoding)
-            , value : toEncoding(value , this._options.valueEncoding || this._options.encoding)
-          })
-        }
-
-      , _cleanup: function (err) {
-          var s = this._status
-          this._status = 'ended'
-          this.readable = false
-          if (this._iterator) {
-            this._iterator.end(function () {
-              this.pub.emit('close')
-            }.bind(this))
-          } else
-            this.pub.emit('close')
-          if (err)
-            this.pub.emit('error', err)
-          else (s != 'destroyed')
-            this.pub.emit('end')
-        }
+  , _read: function () {
+      if (this._status == 'ready') {
+        this._status = 'reading'
+        this._iterator.next(this._cleanup.bind(this), this._onData.bind(this))
+      }
     }
 
-  , Getters = {
-        writable: function () {
-          return this.writable
-        }
-      , readable: function () {
-          return this.readable
-        }
+  , _onData: function (err, key, value) {
+      if (err)
+        return this._cleanup(err)
+      if (this._status == 'ended')
+        return
+      if (/^reading/.test(this._status))
+        this._status = this._status.replace(/^reading/, 'ready')
+      this._read()
+      this.emit(this._dataEvent, {
+          key   : toEncoding(key   , this._options.keyEncoding   || this._options.encoding)
+        , value : toEncoding(value , this._options.valueEncoding || this._options.encoding)
+      })
     }
 
-  , readStreamCreator = new Creator('LevelUP.ReadStream')
-      .setConstructor(ReadStream)
-      .setPrototype(Stream)
-      .setMethods(Methods)
-      .setPrivateMethods(PrivateMethods)
-      .setReadOnlyProperties(function (args) {
-        var props = {}
-        Object.keys(defaultOptions).forEach(function (p) {
-          props[p] = (args[0] && args[0][p]) || defaultOptions[p]
-        })
-        return props
-      })
-      .setGetters(Getters)
+  , _cleanup: function (err) {
+      var s = this._status
+      this._status = 'ended'
+      this.readable = false
+      if (this._iterator) {
+        this._iterator.end(function () {
+          this.emit('close')
+        }.bind(this))
+      } else
+        this.emit('close')
+      if (err)
+        this.emit('error', err)
+      else (s != 'destroyed')
+        this.emit('end')
+    }
+
+  , toString: function () {
+      return 'LevelUP.ReadStream'
+    }
+}
 
-module.exports.create = readStreamCreator.create.bind(readStreamCreator)
\ No newline at end of file
+module.exports.create = function (options, db, iteratorFactory) {
+  return new ReadStream(options, db, iteratorFactory)
+}
\ No newline at end of file
diff --git a/lib/util.js b/lib/util.js
index 211f3f5..4531c5e 100644
--- a/lib/util.js
+++ b/lib/util.js
@@ -16,6 +16,11 @@ var toBuffer = function (data, encoding) {
           : buffer.toString(encoding)
     }
 
+  , extend = function (dst, src) {
+      for (var p in src) dst[p] = src[p]
+      return dst
+    }
+
   , copy = function (srcdb, dstdb, callback) {
       srcdb.readStream().pipe(dstdb.writeStream().on('close', callback))
     }
@@ -23,5 +28,6 @@ var toBuffer = function (data, encoding) {
 module.exports = {
     toBuffer   : toBuffer
   , toEncoding : toEncoding
+  , extend     : extend
   , copy       : copy
 }
\ No newline at end of file
diff --git a/lib/write-stream.js b/lib/write-stream.js
index 4a62ec4..7eb2685 100644
--- a/lib/write-stream.js
+++ b/lib/write-stream.js
@@ -3,166 +3,147 @@
 var Stream       = require("stream").Stream
   , concatStream = require('concat-stream')
 
-  , toEncoding   = require('./util').toEncoding
-  , Creator      = require('./creator').Creator
+  , extend       = require('./util').extend
 
   , defaultOptions = {}
 
-  , WriteStream = function (options, db) {
-      Stream.call(this)
-      this._options = options
-      this._db      = db
-      this._buffer  = []
-      this._status  = 'init'
-      this._end     = false
-      this.writable = true
-      this.readable = false
-
-      var ready = function () {
-        if (!this.writable)
-          return
-        this._status = 'ready'
-        this.pub.emit('ready')
-        this._process()
-      }.bind(this)
-
-      if (db.isOpen())
-        process.nextTick(ready)
-      else
-        db.pub.once('ready', ready)
+function WriteStream (options, db) {
+  this.__proto__.__proto__ = Stream.prototype
+  Stream.call(this)
+  this._options = extend(extend({}, defaultOptions), options)
+  this._db      = db
+  this._buffer  = []
+  this._status  = 'init'
+  this._end     = false
+  this.writable = true
+  this.readable = false
+
+  var ready = function () {
+    if (!this.writable)
+      return
+    this._status = 'ready'
+    this.emit('ready')
+    this._process()
+  }.bind(this)
+
+  if (db.isOpen())
+    process.nextTick(ready)
+  else
+    db.once('ready', ready)
+}
+
+WriteStream.prototype = {
+    write: function (data) {
+      if (!this.writable)
+        return false
+      this._buffer.push(data)
+      if (this._status != 'init')
+        this._processDelayed()
+      if (this._options.maxBufferLength && this._buffer.length > this._options.maxBufferLength) {
+        this._writeBlock = true
+        return false
+      }
+      return true
     }
 
-  , Methods = {
-        write: function (data) {
-          if (!this.writable)
-            return false
-          this._buffer.push(data)
-          if (this._status != 'init')
-            this._processDelayed()
-          if (this._options.maxBufferLength && this._buffer.length > this._options.maxBufferLength) {
-            this._writeBlock = true
-            return false
-          }
-          return true
-        }
-
-      , end: function() {
-          process.nextTick(function () {
-            this._end = true
-            this._process()
-          }.bind(this))
-        }
-
-      , destroy: function() {
-          this.writable = false
-          this.end()
-        }
-
-      , destroySoon: function() {
-          this.end()
-        }
-
-      , add: function(entry) {
-          if (!entry.props)
-            return
-          if (entry.props.Directory)
-            entry.pipe(this._db.writeStream(this._options))
-          else if (entry.props.File || entry.File || entry.type == 'File')
-            this._write(entry)
-          return true
-        }
+  , end: function() {
+      process.nextTick(function () {
+        this._end = true
+        this._process()
+      }.bind(this))
     }
 
-  , PrivateMethods = {
-        _processDelayed: function() {
-          process.nextTick(this._process.bind(this))
-        }
+  , destroy: function() {
+      this.writable = false
+      this.end()
+    }
 
-      , _process: function() {
-          var entry
-            , cb = function (err) {
-                if (!this.writable)
-                  return
-                if (this._status != 'closed')
-                  this._status = 'ready'
-                if (err) {
-                  this.writable = false
-                  return this.pub.emit('error', err)
-                }
-                this._process()
-              }.bind(this)
-
-          if (this._status != 'ready' && this.writable) {
-            if (this._buffer.length && this._status != 'closed')
-              this._processDelayed()
-            return
-          }
+  , destroySoon: function() {
+      this.end()
+    }
 
-          if (this._buffer.length && this.writable) {
-            if (this._buffer.length == 1) {
-              entry = this._buffer.pop()
-              if (entry.key !== undefined && entry.value !== undefined) {
-                this._status = 'writing'
-                this._db.put(entry.key, entry.value, cb)
-              }
-            } else {
-              this._status = 'writing'
-              this._db.batch(this._buffer.map(function (d) {
-                return { type: 'put', key: d.key, value: d.value }
-              }), cb)
-              this._buffer = []
-            }
-            if (this._writeBlock) {
-              this._writeBlock = false
-              this.pub.emit('drain')
-            }
-          }
+  , add: function(entry) {
+      if (!entry.props)
+        return
+      if (entry.props.Directory)
+        entry.pipe(this._db.writeStream(this._options))
+      else if (entry.props.File || entry.File || entry.type == 'File')
+        this._write(entry)
+      return true
+    }
 
-          if (this._end && this._status != 'closed') {
-            this._status = 'closed'
-            this.writable = false
-            this.pub.emit('close')
-            return
-          }
-        }
+  , _processDelayed: function() {
+      process.nextTick(this._process.bind(this))
+    }
 
-      , _write: function (entry) {
-          var key = entry.path || entry.props.path
-          if (!key)
-            return
-          entry.pipe(concatStream(function (err, data) {
+  , _process: function() {
+      var entry
+        , cb = function (err) {
+            if (!this.writable)
+              return
+            if (this._status != 'closed')
+              this._status = 'ready'
             if (err) {
               this.writable = false
-              return this.pub.emit('error', err)
+              return this.emit('error', err)
             }
-            if (this._options.fstreamRoot && key.indexOf(this._options.fstreamRoot) > -1)
-              key = key.substr(this._options.fstreamRoot.length + 1)
-            this.write({ key: key, value: data })
-          }.bind(this)))
+            this._process()
+          }.bind(this)
+
+      if (this._status != 'ready' && this.writable) {
+        if (this._buffer.length && this._status != 'closed')
+          this._processDelayed()
+        return
+      }
+
+      if (this._buffer.length && this.writable) {
+        if (this._buffer.length == 1) {
+          entry = this._buffer.pop()
+          if (entry.key !== undefined && entry.value !== undefined) {
+            this._status = 'writing'
+            this._db.put(entry.key, entry.value, cb)
+          }
+        } else {
+          this._status = 'writing'
+          this._db.batch(this._buffer.map(function (d) {
+            return { type: 'put', key: d.key, value: d.value }
+          }), cb)
+          this._buffer = []
+        }
+        if (this._writeBlock) {
+          this._writeBlock = false
+          this.emit('drain')
         }
+      }
+
+      if (this._end && this._status != 'closed') {
+        this._status = 'closed'
+        this.writable = false
+        this.emit('close')
+        return
+      }
     }
 
-  , Getters = {
-        writable: function () {
-          return this.writable
-        }
-      , readable: function () {
-          return this.readable
+  , _write: function (entry) {
+      var key = entry.path || entry.props.path
+      if (!key)
+        return
+      entry.pipe(concatStream(function (err, data) {
+        if (err) {
+          this.writable = false
+          return this.emit('error', err)
         }
+        if (this._options.fstreamRoot && key.indexOf(this._options.fstreamRoot) > -1)
+          key = key.substr(this._options.fstreamRoot.length + 1)
+        this.write({ key: key, value: data })
+      }.bind(this)))
+    }
+
+  , toString: function () {
+      return 'LevelUP.WriteStream'
     }
+}
 
-  , writeStreamCreator = new Creator('LevelUP.WriteStream')
-      .setConstructor(WriteStream)
-      .setPrototype(Stream)
-      .setMethods(Methods)
-      .setPrivateMethods(PrivateMethods)
-      .setReadOnlyProperties(function (args) {
-        var props = {}
-        Object.keys(defaultOptions).forEach(function (p) {
-          props[p] = (args[0] && args[0][p]) || defaultOptions[p]
-        })
-        return props
-      })
-      .setGetters(Getters)
-
-module.exports.create = writeStreamCreator.create.bind(writeStreamCreator)
\ No newline at end of file
+module.exports.create = function (options, db) {
+  return new WriteStream(options, db)
+}
\ No newline at end of file
diff --git a/package.json b/package.json
index de6e286..20362d2 100644
--- a/package.json
+++ b/package.json
@@ -1,5 +1,8 @@
 {
     "name"            : "levelup"
+  , "authors": [
+       "Rod Vagg @rvagg <rod at vagg.org> (https://github.com/rvagg)"
+    ]
   , "keywords"        : [ "leveldb", "levelup", "stream", "database", "db", "store", "storage", "json" ]
   , "version"         : "0.0.1"
   , "main"            : "lib/levelup.js"
diff --git a/test/binary-test.js b/test/binary-test.js
index b13ae53..ac10827 100644
--- a/test/binary-test.js
+++ b/test/binary-test.js
@@ -2,11 +2,7 @@
 
 var buster  = require('buster')
   , assert  = buster.assert
-  , levelup = require('../lib/levelup.js')
-  , errors  = require('../lib/errors.js')
-  , rimraf  = require('rimraf')
   , async   = require('async')
-  , fs      = require('fs')
   , common  = require('./common')
 
 buster.testCase('Binary API', {
diff --git a/test/common.js b/test/common.js
index 13108f9..4ab65d6 100644
--- a/test/common.js
+++ b/test/common.js
@@ -44,8 +44,10 @@ module.exports.openTestDatabase = function () {
     this.cleanupDirs.push(location)
     levelup(location, options, function (err, db) {
       refute(err)
-      this.closeableDatabases.push(db)
-      callback(db)
+      if (!err) {
+        this.closeableDatabases.push(db)
+        callback(db)
+      }
     }.bind(this))
   }.bind(this))
 }
diff --git a/test/copy-test.js b/test/copy-test.js
index 743bf40..966d0f9 100644
--- a/test/copy-test.js
+++ b/test/copy-test.js
@@ -3,10 +3,7 @@
 var buster  = require('buster')
   , assert  = buster.assert
   , levelup = require('../lib/levelup.js')
-  , errors  = require('../lib/errors.js')
-  , rimraf  = require('rimraf')
   , async   = require('async')
-  , fs      = require('fs')
   , common  = require('./common')
 
 buster.testCase('Copy', {
diff --git a/test/json-test.js b/test/json-test.js
index 5349149..77d3864 100644
--- a/test/json-test.js
+++ b/test/json-test.js
@@ -3,10 +3,7 @@
 var buster  = require('buster')
   , assert  = buster.assert
   , levelup = require('../lib/levelup.js')
-  , errors  = require('../lib/errors.js')
-  , rimraf  = require('rimraf')
   , async   = require('async')
-  , fs      = require('fs')
   , common  = require('./common')
 
 buster.testCase('JSON API', {
@@ -22,7 +19,7 @@ buster.testCase('JSON API', {
           this.closeableDatabases.push(db)
 
           async.parallel(
-              testData.map(function (d) { return db.put.bind(null, d.key, d.value) })
+              testData.map(function (d) { return db.put.bind(db, d.key, d.value) })
             , function (err) {
                 refute(err)
 
diff --git a/test/read-stream-test.js b/test/read-stream-test.js
index 2a6769e..db6fad2 100644
--- a/test/read-stream-test.js
+++ b/test/read-stream-test.js
@@ -2,12 +2,6 @@
 
 var buster  = require('buster')
   , assert  = buster.assert
-  , levelup = require('../lib/levelup.js')
-  , errors  = require('../lib/errors.js')
-  , rimraf  = require('rimraf')
-  , async   = require('async')
-  , fs      = require('fs')
-  , path    = require('path')
   , common  = require('./common')
 
 buster.testCase('ReadStream', {
diff --git a/test/simple-test.js b/test/simple-test.js
index dbfe452..4890071 100644
--- a/test/simple-test.js
+++ b/test/simple-test.js
@@ -4,7 +4,6 @@ var buster  = require('buster')
   , assert  = buster.assert
   , levelup = require('../lib/levelup.js')
   , errors  = require('../lib/errors.js')
-  , rimraf  = require('rimraf')
   , async   = require('async')
   , fs      = require('fs')
   , common  = require('./common')
@@ -34,13 +33,15 @@ buster.testCase('Basic API', {
           levelup(location, function (err, db) { // no options object
             refute(err)
             assert.isObject(db)
-            assert.isFalse(db.options.createIfMissing)
-            assert.isFalse(db.options.errorIfExists)
-            assert.equals(db.location, location)
+            assert.isFalse(db._options.createIfMissing)
+            assert.isFalse(db._options.errorIfExists)
+            assert.equals(db._location, location)
 
+            /*
             // read-only properties
             db.location = 'foo'
             assert.equals(db.location, location)
+            */
             done()
           }.bind(this))
         }.bind(this))
@@ -54,13 +55,15 @@ buster.testCase('Basic API', {
         this.closeableDatabases.push(db)
         this.cleanupDirs.push(location)
         assert.isObject(db)
-        assert.isTrue(db.options.createIfMissing)
-        assert.isTrue(db.options.errorIfExists)
-        assert.equals(db.location, location)
+        assert.isTrue(db._options.createIfMissing)
+        assert.isTrue(db._options.errorIfExists)
+        assert.equals(db._location, location)
 
+        /*
         // read-only properties
-        db.location = 'bar'
-        assert.equals(db.location, location)
+        db._location = 'bar'
+        assert.equals(db._location, location)
+        */
         done()
       }.bind(this))
     }
diff --git a/test/write-stream-test.js b/test/write-stream-test.js
index 6b91f7c..b51efa8 100644
--- a/test/write-stream-test.js
+++ b/test/write-stream-test.js
@@ -2,12 +2,7 @@
 
 var buster  = require('buster')
   , assert  = buster.assert
-  , levelup = require('../lib/levelup.js')
-  , errors  = require('../lib/errors.js')
-  , rimraf  = require('rimraf')
   , async   = require('async')
-  , fs      = require('fs')
-  , path    = require('path')
   , common  = require('./common')
 
 buster.testCase('WriteStream', {

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-leveldown.git



More information about the Pkg-javascript-commits mailing list