PolCPP avatar PolCPP committed a10c3d7

Initial nonworking alpha version

Comments (0)

Files changed (123)

+Copyright (C) 2012 Pol Cámara
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+Vesperia: A Tethealla management port proxy and api server
+=======================================================
+
+Vesperia is a bridge that implements extra security and acts like an api server to extend easily the Tethealla psobb server. It's mostly just compatible with the edenserv.net server but it includes a mock server, and implementing inside your tethealla server http://pioneer2.net/forum/viewtopic.php?t=569 and extending it to comply with the protocol on the docs can be pretty useful.
+
+Status
+------------
+
+Non-functional alpha. Security and auth is done, message passing between client and server is not done yet.
+
+Requirements
+------------
+
+   - Nodejs
+   - Coffeescript
+   - Modified tethealla with mana port that complies with the simple protocol specifications
+
+Installation
+------------
+   - Install node and coffeescript
+   - Download the repo
+   - Configure the settings.coffee according to your configuration
+   - Register an api key running "coffee manage_key.coffee add Mysecretkey"
+   - Run tethealla
+   - Start the ApiProxy doing "coffeee init.coffee"
+
+In case you want to run the mock server (which at this moment is just a very simple version that only replies to "200" commands with the correct aknowledge).
+
+###
+ Vesperia: A Tethealla management port proxy and api server
+
+ Copyright 2012 Pol Cámara
+ Released under the MIT license
+ Check LICENSE.MIT for more details.
+###
+
+settings  = require './settings'
+events = require 'events'
+
+class Client
+  connected: false
+  attempts: 0
+  stream: false
+  events: false
+
+  constructor: (@net, @port, @host) ->
+
+  ###
+  Initializes the client
+  Example usage:
+    clientInstance.initialize()
+  ###
+  initialize: ->
+    this.stream = new this.net.Socket
+    this.stream.client = this
+    this.stream.connect(this.port,this.host)
+    this.stream.on "error", this.onError 
+    this.stream.on "data", this.onData  
+    this.stream.on "connect", this.onConnect
+    this.stream.on "end", this.onEnd 
+    this.events = new events.EventEmitter()
+    this.events.on 'message', (message) -> 
+      console.log "Recieved message: '#{message}'"
+
+  onConnect: (socket) -> 
+    this.client.attempts = 0
+    this.client.connected = true
+    console.log "Connected"
+
+  onData: (data)  ->
+    this.client.events.emit 'message', data.toString()
+
+  onError: (err) ->
+    client = this.client
+    this.client.attempts++
+    if this.client.attempts <= settings.maxAttempts
+      # This looks a bit strange but setTimeout need to run 
+      # Using a closure, and thats the way the work o coffeescriptn
+      setTimeout ( ->
+        client.reconnect()
+      ), settings.attemptWait
+
+  onEnd: ->
+    client = this.client
+    this.client.attempts++
+    setTimeout ( ->
+      client.reconnect()
+    ), settings.attemptWait
+
+  reconnect: ->
+    console.log("Connecting to Tethealla failed... reconnect attempt: " + this.attempts )
+    this.stream.connect(this.port, this.host)
+
+  endConnection: ->
+    this.stream.close
+    console.log "Socket closed"
+  
+module.exports.client = Client
+15je8n2f	{"secret":"5ebe2294ecd0e0f08eab7690d2a6ee69"}
+1v8rb71g	{"secret":"5ebe2294ecd0e0f08eab7690d2a6ee69"}
+2eobnlah	{"secret":"5ebe2294ecd0e0f08eab7690d2a6ee69"}
+15je8n2f	
+1v8rb71g	
+2eobnlah	
+2hroj4o2	{"secret":"5ebe2294ecd0e0f08eab7690d2a6ee69"}
+3thrnv9e	{"secret":"5ebe2294ecd0e0f08eab7690d2a6ee69"}
+Client Messages
+ Auth
+  110+Password: Log me in with unhashed password (NOT SAFE)
+  111+Password: Log me in with hashed password
+
+ Proxy
+  210+Command+attributes: Send command to server
+
+Vesperia Messages
+ Auth
+  100: Login Required. (Sent just after a client connects if login required)
+  101: Unauthorized, disconnecting.
+  102: Logged In succesffully.
+
+ Proxy
+  To Tethealla
+   200+UID+Command: Execute Command and reply to UID (Check Tethealla proxy messages for replies)
+
+  To client
+   201+Response from tethealla from a 200 command. (Read 220/221)
+   202+Tethealla not available. Trying to reconnect. Wait 10 seconds and send this message again 
+
+ Chat
+  205: Send me all the lobby messages to this socket till it closes.
+  205+LID: Send me only the messages from the socket with the attached id till it closes.
+
+Tethealla Messages
+ Proxy
+  220+UID: The executed command was run successfully.
+  220+UID+Reply: The executed command was run successfully and had this reply.
+  221+UID+Error: The executed command failed. Here's the reason.
+
+ Chat
+  225+LID+PSOUSER+message Chat message from PSO.
+
+*UID is a 32 character md5 hash.
+*LID Lobby id. 2 digits
+*PSOUSER has the max characters allowed for pso users (16 i think). If name is
+ shorter filed with spaces till the 16th Character
+###
+ Vesperia: A Tethealla management port proxy and api server
+
+ Copyright 2012 Pol Cámara
+ Released under the MIT license
+ Check LICENSE.MIT for more details.
+###
+
+net = require 'net'
+mysql = require 'mysql'
+settings = require './settings'
+vespServer = require './server'
+tethClient = require './client'
+
+console.log ""
+console.log "############################################################"
+console.log "# Vesperia: A tethealla management port proxy and api server"
+console.log "#"
+console.log "# Copyright 2012 Pol Cámara"
+console.log "# Released under the MIT License"
+console.log "# Check LICENSE.MIT for more details"
+console.log "############################################################"
+
+connection = mysql.createConnection settings.dbConnection
+
+client = new tethClient.client net, settings.tetheallaPort, settings.tetheallaHost
+client.initialize()
+
+server = new vespServer.Server net, settings.vesperiaPort, settings.vesperiaHost, connection, client
+server.initialize()

manage_key.coffee

+###
+ Vesperia: A Tethealla management port proxy and api server
+
+ Copyright 2012 Pol Cámara
+ Released under the MIT license
+ Check LICENSE.MIT for more details.
+###
+
+mysql = require 'mysql'
+settings = require './settings'
+Security = require "./model/security"
+wrongArgs = true
+command = ""
+value = ""
+wrongMessage = "Wrong arguments set, it must be:\n manage_key add secretkey \n manage_key remove secretkey"
+connection = mysql.createConnection settings.dbConnection
+
+
+if process.argv.length == 3 && process.argv[0] != "coffee"
+  wrongArgs = false
+  command = process.argv[1]
+  value = process.arv[2]
+
+if process.argv.length == 4 && process.argv[0] == "coffee" 
+  wrongArgs = false
+  command = process.argv[2]
+  value = process.argv[3]
+
+connection.connect (err) ->
+  if not err
+    if wrongArgs
+      console.log wrongMessage
+    else
+      security = new Security connection
+      if command == "add"
+        security.addKey value, (dbKey) -> console.log "Key added successfully ID: " + dbKey  
+      else if command == "remove"
+        security.delKey value, (rows) -> 
+          if rows > 0
+             console.log "Key removed successfully"
+          else 
+             console.log "Key wasn't found"
+      else if command = "search"
+        security.valKey value, false, (result) -> 
+          if result
+            console.log "Key found"
+          else
+            console.log "Key not found"
+      else 
+        console.log wrongMessage
+      connection.end()
+  else
+    console.log err

mock/mockTethealla.coffee

+###
+ Vesperia: A Tethealla management port proxy and api server
+
+ Copyright 2012 Pol Cámara
+ Released under the MIT license
+ Check LICENSE.MIT for more details.
+###
+
+# This is a little mock server that/will imitate tethealla mana port
+net = require 'net'
+
+domain = 'localhost'
+port = 8825
+
+server = net.createServer (socket) ->
+  console.log "#{socket.remoteAddress} connected"
+
+  socket.on 'data', (data) ->
+    data = data.toString()
+    if data.length > 35
+      command = data.substring 0, 3
+      uid = data.substring 3, 35
+      action = data.substring 35, data.length
+      if command == "200"
+        socket.write "220" + uid + "Command ran successfully" 
+
+console.log "Listening to #{domain}:#{port}"
+server.listen port, domain

model/security.coffee

+###
+ Vesperia: A Tethealla management port proxy and api server
+
+ Copyright 2012 Pol Cámara
+ Released under the MIT license
+ Check LICENSE.MIT for more details.
+###
+
+crypto = require 'crypto'
+
+gConnection = null
+
+class Security
+  constructor: (@connection) ->
+    gConnection = connection    
+  ###
+  (Private) Generates the password hash
+  Example usage:
+    hash = this.makeHash key
+  ###
+  _makeHash: (key) -> crypto.createHash("md5").update(key).digest("hex")
+
+  ###
+  Adds a new security (api) key to the database 
+  Example usage: 
+    securityInstance.addKey "MyKey", (dbKey) -> console.log dbKey
+  ###
+  addKey: (secretKey, cb) ->
+    hash = this._makeHash secretKey
+    gConnection.query "INSERT INTO api_keys (id, value) values (null,?)", [hash], (err, results) -> 
+      cb results.insertId if results
+
+  ###
+  Validates if the security (api) key is correct. Hashed means that the password is hashed
+  Example usage:
+    security_Instance.valKey "MyKey", false, (keyExists) -> console.log "Logged in" if keyExists
+  ###
+  valKey: (secretKey,hashed,cb) ->
+    hash = this._makeHash secretKey if !hashed
+    gConnection.query "SELECT * FROM api_keys where value = ?", [hash], (err, results) ->
+      cb results.length != 0
+
+  ###
+  Deletes a security (api) key by secret.
+  Example usage:
+    security_instance.delKey "MyKey", (key, err) -> console.log "Deleted" if not err
+  ###
+  delKey: (secretKey, cb) ->
+    hash = this._makeHash secretKey
+    gConnection.query "DELETE FROM api_keys where value = ?", [hash], (err, results) ->
+      cb results.affectedRows
+
+module.exports = Security

model/security.sql

+CREATE TABLE  `psotest`.`api_keys` (
+`id` INT NOT NULL AUTO_INCREMENT ,
+`value` VARCHAR( 32 ) NOT NULL ,
+PRIMARY KEY (  `id` ) ,
+UNIQUE (
+`value`
+)
+) ENGINE = INNODB;

node_modules/mysql/.npmignore

+*.un~
+
+/node_modules

node_modules/mysql/.travis.yml

+language: node_js
+node_js:
+  - 0.4
+  - 0.6

node_modules/mysql/Changes.md

+# Changes
+
+This file is a manually maintained list of changes for each release. Feel free
+to add your changes here when sending pull requests. Also send corrections if
+you spot any mistakes.
+
+## v2.0.0-alpha3 (2012-06-12)
+
+* Implement support for `LOAD DATA LOCAL INFILE` queries (#182).
+* Support OLD\_PASSWORD() accounts like 0.9.x did. You should still upgrade any
+  user accounts in your your MySQL user table that has short (16 byte) Password
+  values. Connecting to those accounts is not secure. (#204)
+* Ignore function values when escaping objects, allows to use RowDataPacket
+  objects as query arguments. (Alex Gorbatchev, #213)
+* Handle initial error packets from server such as `ER_HOST_NOT_PRIVILEGED`.
+* Treat `utf8\_bin` as a String, not Buffer. (#214)
+* Handle empty strings in first row column value. (#222)
+* Honor Connection#nestTables setting for queries. (#221)
+* Remove `CLIENT_INTERACTIVE` flag from config. Improves #225.
+* Improve docs for connections settings.
+* Implement url string support for Connection configs.
+
+## v2.0.0-alpha2 (2012-05-31)
+
+* Specify escaping before for NaN / Infinity (they are as unquoted constants).
+* Support for unix domain socket connections (use: {socketPath: '...'}).
+* Fix type casting for NULL values for Date/Number fields
+* Add `fields` argument to `query()` as well as `'fields'` event. This is
+  similar to what was available in 0.9.x.
+* Support connecting to the sphinx searchd daemon as well as MariaDB (#199).
+* Implement long stack trace support, will be removed / disabled if the node
+  core ever supports it natively.
+* Implement `nestTables` option for queries, allows fetching JOIN result sets
+  with overlapping column names.
+* Fix ? placeholder mechanism for values containing '?' characters (#205).
+* Detect when `connect()` is called more than once on a connection and provide
+  the user with a good error message for it (#204).
+* Switch to `UTF8_GENERAL_CI` (previously `UTF8_UNICODE_CI`) as the default
+  charset for all connections to avoid strange MySQL performance issues (#200),
+  and also make the charset user configurable.
+* Fix BLOB type casting for `TINY_BLOG`, `MEDIUM_BLOB` and `LONG_BLOB`.
+* Add support for sending and receiving large (> 16 MB) packets.
+
+## v2.0.0-alpha (2012-05-15)
+
+This release is a rewrite. You should carefully test your application after
+upgrading to avoid problems. This release features many improvements, most
+importantly:
+
+* ~5x faster than v0.9.x for parsing query results
+* Support for pause() / resume() (for streaming rows)
+* Support for multiple statement queries
+* Support for stored procedures
+* Support for transactions
+* Support for binary columns (as blobs)
+* Consistent & well documented error handling
+* A new Connection class that has well defined semantics (unlike the old Client class).
+* Convenient escaping of objects / arrays that allows for simpler query construction
+* A significantly simpler code base
+* Many bug fixes & other small improvements (Closed 62 out of 66 GitHub issues)
+
+Below are a few notes on the upgrade process itself:
+
+The first thing you will run into is that the old `Client` class is gone and
+has been replaced with a less ambitious `Connection` class. So instead of
+`mysql.createClient()`, you now have to:
+
+```js
+var mysql      = require('mysql');
+var connection = mysql.createConnection({
+  host     : 'localhost',
+  user     : 'me',
+  password : 'secret',
+});
+
+connection.query('SELECT 1', function(err, rows) {
+  if (err) throw err;
+
+  console.log('Query result: ', rows);
+});
+
+connection.end();
+```
+
+The new `Connection` class does not try to handle re-connects, please study the
+`Server disconnects` section in the new Readme.
+
+Other than that, the interface has stayed very similar. Here are a few things
+to check out so:
+
+* BIGINT's are now cast into strings
+* Binary data is now cast to buffers
+* The `'row'` event on the `Query` object is now called `'result'` and will
+  also be emitted for queries that produce an OK/Error response.
+* Error handling is consistently defined now, check the Readme
+* Escaping has become more powerful which may break your code if you are
+  currently using objects to fill query placeholders.
+* Connections can now be established explicitly again, so you may wish to do so
+  if you want to handle connection errors specifically.
+
+That should be most of it, if you run into anything else, please send a patch
+or open an issue to improve this document.
+
+## v0.9.6 (2012-03-12)
+
+* Escape array values so they produce sql arrays (Roger Castells, Colin Smith)
+* docs: mention mysql transaction stop gap solution (Blake Miner)
+* docs: Mention affectedRows in FAQ (Michael Baldwin)
+
+## v0.9.5 (2011-11-26)
+
+* Fix #142 Driver stalls upon reconnect attempt that's immediately closed
+* Add travis build
+* Switch to urun as a test runner
+* Switch to utest for unit tests
+* Remove fast-or-slow dependency for tests
+* Split integration tests into individual files again
+
+## v0.9.4 (2011-08-31)
+
+* Expose package.json as `mysql.PACKAGE` (#104)
+
+## v0.9.3 (2011-08-22)
+
+* Set default `client.user` to root
+* Fix #91: Client#format should not mutate params array
+* Fix #94: TypeError in client.js
+* Parse decimals as string (vadimg)
+
+## v0.9.2 (2011-08-07)
+
+* The underlaying socket connection is now managed implicitly rather than explicitly.
+* Check the [upgrading guide][] for a full list of changes.
+
+## v0.9.1 (2011-02-20)
+
+* Fix issue #49 / `client.escape()` throwing exceptions on objects. (Nick Payne)
+* Drop < v0.4.x compatibility. From now on you need node v0.4.x to use this module.
+
+## Older releases
+
+These releases were done before maintaining this file:
+
+* [v0.9.0](https://github.com/felixge/node-mysql/compare/v0.8.0...v0.9.0)
+  (2011-01-04)
+* [v0.8.0](https://github.com/felixge/node-mysql/compare/v0.7.0...v0.8.0)
+  (2010-10-30)
+* [v0.7.0](https://github.com/felixge/node-mysql/compare/v0.6.0...v0.7.0)
+  (2010-10-14)
+* [v0.6.0](https://github.com/felixge/node-mysql/compare/v0.5.0...v0.6.0)
+  (2010-09-28)
+* [v0.5.0](https://github.com/felixge/node-mysql/compare/v0.4.0...v0.5.0)
+  (2010-09-17)
+* [v0.4.0](https://github.com/felixge/node-mysql/compare/v0.3.0...v0.4.0)
+  (2010-09-02)
+* [v0.3.0](https://github.com/felixge/node-mysql/compare/v0.2.0...v0.3.0)
+  (2010-08-25)
+* [v0.2.0](https://github.com/felixge/node-mysql/compare/v0.1.0...v0.2.0)
+  (2010-08-22)
+* [v0.1.0](https://github.com/felixge/node-mysql/commits/v0.1.0)
+  (2010-08-22)

node_modules/mysql/License

+Copyright (c) 2012 Felix Geisendörfer (felix@debuggable.com) and contributors
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.

node_modules/mysql/Makefile

+test:
+	node test/run.js
+
+.PHONY: test

node_modules/mysql/Readme.md

+# node-mysql
+
+[![Build Status](https://secure.travis-ci.org/felixge/node-mysql.png?branch=v2.0)](http://travis-ci.org/felixge/node-mysql)
+
+## Install
+
+```bash
+npm install mysql@2.0.0-alpha3
+```
+
+Despite the alpha tag, this is the recommended version for new applications.
+For information about the previous 0.9.x releases, visit the [v0.9 branch][].
+
+Sometimes I may also ask you to install the latest version from Github to check
+if a bugfix is working. In this case, please do:
+
+```
+npm install git://github.com/felixge/node-mysql.git
+```
+
+[v0.9 branch]: https://github.com/felixge/node-mysql/tree/v0.9
+
+## Introduction
+
+This is a node.js driver for mysql. It is written in JavaScript, does not
+require compiling, and is 100% MIT licensed.
+
+Here is an example on how to use it:
+
+```js
+var mysql      = require('mysql');
+var connection = mysql.createConnection({
+  host     : 'localhost',
+  user     : 'me',
+  password : 'secret',
+});
+
+connection.connect();
+
+connection.query('SELECT 1', function(err, rows, fields) {
+  if (err) throw err;
+
+  console.log('Query result: ', rows);
+});
+
+connection.end();
+```
+
+From this example, you can learn the following:
+
+* Every method you invoke on a connection is queued and executed in sequence.
+* Closing the connection is done using `end()` which makes sure all remaining
+  queries are executed before sending a quit packet to the mysql server.
+
+## Contributors
+
+Thanks goes to the people who have contributed code to this module, see the
+[GitHub Contributors page][].
+
+[GitHub Contributors page]: https://github.com/felixge/node-mysql/graphs/contributors
+
+Additionally I'd like to thank the following people:
+
+* [Andrey Hristov][] (Oracle) - for helping me with protocol questions.
+* [Ulf Wendel][] (Oracle) - for helping me with protocol questions.
+
+[Ulf Wendel]: http://blog.ulf-wendel.de/
+[Andrey Hristov]: http://andrey.hristov.com/
+## Sponsors
+
+The following companies have supported this project financially, allowing me to
+spend more time on it (ordered by time of contribution):
+
+* [Transloadit](http://transloadit.com) (my startup, we do file uploading &
+  video encoding as a service, check it out)
+* [Joyent](http://www.joyent.com/)
+* [pinkbike.com](http://pinkbike.com/)
+* [Holiday Extras](http://www.holidayextras.co.uk/) (they are [hiring](http://join.holidayextras.co.uk/vacancy/senior-web-technologist/))
+* [Newscope](http://newscope.com/) (they are [hiring](http://www.newscope.com/stellenangebote))
+
+If you are interested in sponsoring a day or more of my time, please
+[get in touch][].
+
+[get in touch]: http://felixge.de/consulting
+
+## Community
+
+So far all community activity has happened via the GitHub Issue system, however
+additionally I have just started a mailing list and IRC channel where people
+can ask questions and discuss things:
+
+* **Mailing list**: https://groups.google.com/forum/#!forum/node-mysql
+* **IRC Channel**: #node-mysql (on freenode.net)
+
+## Establishing connections
+
+The recommended way to establish a connection is this:
+
+```js
+var mysql      = require('mysql');
+var connection = mysql.createConnection({
+  host     : 'example.org',
+  user     : 'bob',
+  password : 'secret',
+});
+
+connection.connect(function(err) {
+  // connected! (unless `err` is set)
+});
+```
+
+However, a connection can also be implicitly established by invoking a query:
+
+```js
+var mysql      = require('mysql');
+var connection = mysql.createConnection(...);
+
+connection.query('SELECT 1', function(err, rows) {
+  // connected! (unless `err` is set)
+});
+```
+
+Depending on how you like to handle your errors, either method may be
+appropriate. Any type of connection error (handshake or network) is considered
+a fatal error, see the [Error Handling](#error-handling) section for more
+information.
+
+## Connection options
+
+When establishing a connection, you can set the following options:
+
+* `host`: The hostname of the database you are connecting to. (Default:
+  `localhost`)
+* `port`: The port number to connect to. (Default: `3306`)
+* `socketPath`: The path to a unix domain socket to connect to. When used `host`
+  and `port` are ignored.
+* `user`: The MySQL user to authenticate as.
+* `password`: The passqword of that MySQL user.
+* `database`: Name of the database to use for this connection (Optional).
+* `charset`: The charset for the connection. (Default: `'UTF8_GENERAL_CI'`)
+* `insecureAuth`: Allow connecting to MySQL instances that ask for the old
+  (insecure) authentication method. (Default: `false`)
+* `typeCast`: Determines if column values should be converted to native
+   JavaScript types. (Default: `true`)
+* `debug`: Prints protocol details to stdout. (Default: `false`)
+* `multipleStatements`: Allow multiple mysql statements per query. Be careful
+  with this, it exposes you to SQL injection attacks. (Default: `false)
+
+In addition to passing these options as an object, you can also use a url
+string. For example:
+
+```js
+var connection = mysql.createConnection('mysql://user:pass@host/db?debug=true&charset=BIG5_CHINESE_CI');
+```
+
+Note: The query values are first attempted to be parsed as JSON, and if that
+fails assumed to be plaintext strings.
+
+## Terminating connections
+
+There are two ways to end a connection. Terminating a connection gracefully is
+done by calling the `end()` method:
+
+```js
+connection.end(function(err) {
+  // The connection is terminated now
+});
+```
+
+This will make sure all previously enqueued queries are still before sending a
+`COM_QUIT` packet to the MySQL server. If a fatal error occurs before the
+`COM_QUIT` packet can be sent, an `err` argument will be provided to the
+callback, but the connection will be terminated regardless of that.
+
+An alternative way to end the connection is to call the `destroy()` method.
+This will cause an immediate termination of the underlaying socket.
+Additionally `destroy()` guarantees that no more events or callbacks will be
+triggered for the connection.
+
+```js
+connection.destroy();
+```
+
+Unlike `end()` the `destroy()` method does not take a callback argument.
+
+## Server disconnects
+
+You may loose the connection to a MySQL server due to network problems, the
+server timing you out, or the server crashing. All of these events are
+considered fatal errors, and will have the `err.code =
+'PROTOCOL_CONNECTION_LOST'`.  See the [Error Handling](#error-handling) section
+for more information.
+
+The best way to be notified about a connection termination is to listen for the
+`'close'` event:
+
+```js
+connection.on('close', function(err) {
+  if (err) {
+    // We did not expect this connection to terminate
+    connection = mysql.createConnection(connection.config);
+  } else {
+    // We expected this to happen, end() was called.
+  }
+});
+```
+
+As you can see in the example above, re-connecting a connection is done by
+establishing a new connection. Once terminated, an existing connection object
+cannot be re-connected by design.
+
+Please note that you will also receive a `'close'` event with an `err` argument
+when a connection attempt fails because of bad credentials. If you find this
+cumbersome to work with, please post to the node-mysql mailing list to discuss
+improvements.
+
+## Escaping query values
+
+In order to avoid SQL Injection attacks, you should always escape any user
+provided data before using it inside a SQL query. You can do so using the
+`connection.escape()` method:
+
+```js
+var userId = 'some user provided value';
+var sql    = 'SELECT * FROM users WHERE id = ' + connection.escape(userId);
+connection.query(sql, function(err, results) {
+  // ...
+});
+```
+
+Alternatively, you can use `?` characters as placeholders for values you would
+like to have escaped like this:
+
+```js
+connection.query('SELECT * FROM users WHERE id = ?', [userId], function(err, results) {
+  // ...
+});
+```
+
+This looks similar to prepared statements in MySQL, however it really just uses
+the same `connection.escape()` method internally.
+
+Different value types are escaped differently, here is how:
+
+* Numbers are left untouched
+* Booleans are converted to `true` / `false` strings
+* Date objects are converted to `'YYYY-mm-dd HH:ii:ss'` strings
+* Buffers are converted to hex strings, e.g. `X'0fa5'`
+* Strings are safely escaped
+* Arrays are turned into list, e.g. ['a', 'b'] turns into `'a', 'b'`
+* Objects are turned into `key = 'val'` pairs. Nested objects are cast to
+  strings.
+* `undefined` / `null` are converted to `NULL`
+* `NaN` / `Infinity` are left as-is. MySQL does not support these, and trying
+  to insert them as values will trigger MySQL errors until they implement
+  support.
+
+If you paid attention, you may have noticed that this escaping allows you
+to do neat things like this:
+
+```js
+var post  = {id: 1, title: 'Hello MySQL'};
+var query = connection.query('INSERT INTO posts SET ?', post, function(err, result) {
+  // Neat!
+});
+console.log(query.sql); // INSERT INTO posts SET `id` = 1, `title` = 'Hello MySQL'
+
+```
+
+## Getting the id of an inserted row
+
+If you are inserting a row into a table with an auto increment primary key, you
+can retrieve the insert id like this:
+
+```js
+connection.query('INSERT INTO posts SET ?', {title: 'test'}, function(err, result) {
+  if (err) throw err;
+
+  console.log(result.insertId);
+});
+```
+
+## Executing queries in parallel
+
+The MySQL protocol is sequential, this means that you need multiple connections
+to execute queries in parallel. Future version of this module may ship with a
+connection pool implementation, but for now you have to figure out how to
+manage multiple connections yourself if you want to execute queries in
+parallel.
+
+One simple approach is to create one connection per incoming http request.
+
+## Streaming query rows
+
+Sometimes you may want to select large quantities of rows and process each of
+them as they are received. This can be done like this:
+
+```js
+var query = connection.query('SELECT * FROM posts');
+query
+  .on('error', function(err) {
+    // Handle error, an 'end' event will be emitted after this as well
+  })
+  .on('fields', function(fields) {
+    // the field packets for the rows to follow
+  })
+  .on('result', function(row) {
+    // Pausing the connnection is useful if your processing involves I/O
+    connection.pause();
+
+    processRow(row, function() {
+      connection.resume();
+    });
+  })
+  .on('end', function() {
+    // all rows have been received
+  });
+```
+
+Please note a few things about the example above:
+
+* Usually you will want to receive a certain amount of rows before starting to
+  throttle the connection using `pause()`. This number will depend on the
+  amount and size of your rows.
+* `pause()` / `resume()` operate on the underlaying socket and parser. You are
+  guaranteed that no more `'result'` events will fire after calling `pause()`.
+* You MUST NOT provide a callback to the `query()` method when streaming rows.
+* The `'result'` event will fire for both rows as well as OK packets
+  confirming the success of a INSERT/UPDATE query.
+
+Additionally you may be interested to know that it is currently not possible to
+stream individual row columns, they will always be buffered up entirely. If you
+have a good use case for streaming large fields to and from MySQL, I'd love to
+get your thoughts and conributions on this.
+
+## Multiple statement queries
+
+Support for multiple statements is disabled for security reasons (it allows for
+SQL injection attacks if values are not properly escaped). To use this feature
+you have to enable it for your connection:
+
+```js
+var connection = mysql.createConnection({multipleStatements: true});
+```
+
+Once enabled, you can execute multiple statement queries like any other query:
+
+```js
+connection.query('SELECT 1; SELECT 2', function(err, results) {
+  if (err) throw err;
+
+  // `results` is an array with one element for every statement in the query:
+  console.log(results[0]); // [{1: 1}]
+  console.log(results[1]); // [{2: 2}]
+});
+```
+
+Additionally you can also stream the results of multiple statement queries:
+
+```js
+var query = connection.query('SELECT 1; SELECT 2');
+
+query
+  .on('fields', function(fields, index) {
+    // the fields for the result rows that follow
+  })
+  .on('result', function(row, index) {
+    // index refers to the statement this result belongs to (starts at 0)
+  });
+```
+
+If one of the statements in your query causes an error, the resulting Error
+object contains a `err.index` property which tells you which statement caused
+it. MySQL will also stop executing any remaining statements when an error
+occurs.
+
+Please note that the interface for streaming multiple statement queries is
+experimental and I am looking forward to feedback on it.
+
+## Stored procedures
+
+You can call stored procedures from your queries as with any other mysql driver.
+If the stored procedure produces several result sets, they are exposed to you
+the same way as the results for multiple statement queries.
+
+## Joins with overlapping column names
+
+When executing joins, you are likely to get result sets with overlapping column
+names.
+
+By default, node-mysql will overwrite colliding column names in the
+order the columns are received from MySQL, causing some of the received values
+to be unavailable.
+
+However, you can also specify that you want your columns to be nested below
+the table name like this:
+
+```js
+var options = {sql: '...', nestTables: true};
+connection.query(options, function(err, results) {
+  /* results will be an array like this now:
+  [{
+    table1: {
+      fieldA: '...',
+      fieldB: '...',
+    },
+    table2: {
+      fieldA: '...',
+      fieldB: '...',
+    },
+  }, ...]
+  */
+});
+```
+
+## Error handling
+
+This module comes with a consistent approach to error handling that you should
+review carefully in order to write solid applications.
+
+All errors created by this module are instances of the JavaScript [Error][]
+object. Additionally they come with two properties:
+
+* `err.code`: Either a [MySQL server error][] (e.g.
+  `'ER_ACCESS_DENIED_ERROR'`), a node.js error (e.g. `'ECONNREFUSED'`) or an
+  internal error (e.g.  `'PROTOCOL_PARSER_EXCEPTION'`).
+* `err.fatal`: Boolean, indicating if this error is terminal to the connection
+  object.
+
+[Error]: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error
+[MySQL server error]: http://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html
+
+Fatal errors are propagated to *all* pending callbacks. In the example below, a
+fatal error is triggered by trying to connect to an invalid port. Therefore the
+error object is propagated to both pending callbacks:
+
+```js
+var connection = require('mysql').createConnection({
+  port: 84943, // WRONG PORT
+});
+
+connection.connect(function(err) {
+  console.log(err.code); // 'ECONNREFUSED'
+  console.log(err.fatal); // true
+});
+
+connection.query('SELECT 1', function(err) {
+  console.log(err.code); // 'ECONNREFUSED'
+  console.log(err.fatal); // true
+});
+```
+
+Normal errors however are only delegated to the callback they belong to.  So in
+the example below, only the first callback receives an error, the second query
+works as expected:
+
+```js
+connection.query('USE name_of_db_that_does_not_exist', function(err, rows) {
+  console.log(err.code); // 'ER_BAD_DB_ERROR'
+});
+
+connection.query('SELECT 1', function(err, rows) {
+  console.log(err); // null
+  console.log(rows.length); // 1
+});
+```
+
+Last but not least: If a fatal errors occurs and there are no pending
+callbacks, or a normal error occurs which has no callback belonging to it, the
+error is emitted as an `'error'` event on the connection object. This is
+demonstrated in the example below:
+
+```js
+connection.on('error', function(err) {
+  console.log(err.code); // 'ER_BAD_DB_ERROR'
+});
+
+connection.query('USE name_of_db_that_does_not_exist');
+```
+
+Note: `'error'` are special in node. If they occur without an attached
+listener, a stack trace is printed and your process is killed.
+
+**tl;dr:** This module does not want you to to deal with silent failures. You
+should always provide callbacks to your method calls. If you want to ignore
+this advice and suppress unhandled errors, you can do this:
+
+```js
+// I am Chuck Noris:
+connection.on('error', function() {});
+```
+
+## Type casting
+
+For your convenience, this driver will cast mysql types into native JavaScript
+types by default. The following mappings exist:
+
+### Number
+
+* TINYINT
+* SMALLINT
+* INT
+* MEDIUMINT
+* YEAR
+* FLOAT
+* DOUBLE
+
+### Date
+
+* TIMESTAMP
+* DATE
+* DATETIME
+
+### Buffer
+
+* TINYBLOB
+* MEDIUMBLOB
+* LONGBLOB
+* BLOB
+* BINARY
+* VARBINARY
+* BIT (last byte will be filled with 0 bits as neccessary)
+
+### String
+
+* CHAR
+* VARCHAR
+* TINYTEXT
+* MEDIUMTEXT
+* LONGTEXT
+* TEXT
+* ENUM
+* SET
+* DECIMAL (may exceed float precision)
+* BIGINT (may exceed float precision)
+* TIME (could be mapped to Date, but what date would be set?)
+* GEOMETRY (never used those, get in touch if you do)
+
+It is not recommended (and may go away / change in the future) to disable type
+casting, but you can currently do so on either the connection:
+
+```js
+var connection = require('mysql').createConnection({typeCast: false});
+```
+
+Or on the query level:
+
+```js
+var options = {sql: '...', typeCast: false};
+var query = connection.query(options, function(err, results) {
+
+}):
+```
+
+## Debugging and reporting problems
+
+If you are running into problems, one thing that may help is enabling the
+`debug` mode for the connection:
+
+```js
+var connection = mysql.createConnection({debug: true});
+```
+
+This will print all incoming and outgoing packets on stdout.
+
+If that does not help, feel free to open a GitHub issue. A good GitHub issue
+will have:
+
+* The minimal amount of code required to reproduce the problem (if possible)
+* As much debugging output and information about your environment (mysql
+  version, node version, os, etc.) as you can gather.
+
+## Todo
+
+* Prepared statements
+* setTimeout() for Connection / Query
+* connection pooling
+* Support for encodings other than UTF-8 / ASCII
+* API support for transactions, similar to [php](http://www.php.net/manual/en/mysqli.quickstart.transactions.php)

node_modules/mysql/benchmark/analyze.js

+var script = process.cwd() + '/' + process.argv[2];
+var spawn  = require('child_process').spawn;
+
+var numbers       = [];
+var boringResults = 0;
+var scriptRuns    = 0;
+
+function runScript() {
+  scriptRuns++;
+
+  var child = spawn(process.execPath, [script]);
+
+  var buffer = '';
+  child.stdout.on('data', function(chunk) {
+    buffer += chunk;
+
+    var offset;
+    while ((offset = buffer.indexOf('\n')) > -1) {
+      var number = parseInt(buffer.substr(0, offset), 10);
+      buffer = buffer.substr(offset + 1);
+
+      var maxBefore = max();
+      var minBefore = min();
+
+      numbers.push(number);
+
+      if (maxBefore === max() && minBefore === min()) {
+        boringResults++;
+      }
+
+      if (boringResults > 10) {
+        boringResults = 0;
+        child.kill();
+        runScript();
+      }
+    }
+  });
+}
+
+function report() {
+  console.log(
+    'max: %s | median: %s | sdev: %s | last: %s | min: %s | runs: %s | results: %s',
+    max(),
+    median(),
+    sdev(),
+    numbers[numbers.length - 1],
+    min(),
+    scriptRuns,
+    numbers.length
+  );
+}
+
+function min() {
+  if (!numbers.length) return undefined;
+
+  return numbers.reduce(function(min, number) {
+    return (number < min)
+      ? number
+      : min;
+  });
+}
+
+function max() {
+  if (!numbers.length) return undefined;
+
+  return numbers.reduce(function(max, number) {
+    return (number > max)
+      ? number
+      : max;
+  });
+}
+
+function median() {
+  return numbers[Math.floor(numbers.length / 2)];
+}
+
+function sdev() {
+  if (!numbers.length) return undefined;
+
+  return Math.round(Math.sqrt(variance()));
+}
+
+function variance() {
+  var t = 0, squares = 0, len = numbers.length;
+
+  for (var i=0; i<len; i++) {
+    var obs = numbers[i];
+    t += obs;
+    squares += Math.pow(obs, 2);
+  }
+  return (squares/len) - Math.pow(t/len, 2);
+}
+
+setInterval(report, 1000);
+
+runScript();

node_modules/mysql/benchmark/parse-100k-blog-rows.js

+var lib          = __dirname + '/../lib';
+var Protocol     = require(lib + '/protocol/protocol');
+var Packets      = require(lib + '/protocol/packets');
+var PacketWriter = require(lib + '/protocol/PacketWriter');
+var Parser       = require(lib + '/protocol/Parser');
+
+var options = {
+  rows       : 100000,
+  bufferSize : 64 * 1024,
+};
+
+console.error('Config:', options);
+
+function createBuffers() {
+  var parser = new Parser();
+
+  process.stderr.write('Creating row buffers ... ');
+
+  var number = 1;
+  var id     = 0;
+  var start  = Date.now();
+
+  var buffers = [
+    createPacketBuffer(parser, new Packets.ResultSetHeaderPacket({fieldCount: 2})),
+    createPacketBuffer(parser, new Packets.FieldPacket({catalog: 'foo', name: 'id'})),
+    createPacketBuffer(parser, new Packets.FieldPacket({catalog: 'foo', name: 'text'})),
+    createPacketBuffer(parser, new Packets.EofPacket()),
+  ];
+
+  for (var i = 0; i < options.rows; i++) {
+    buffers.push(createRowDataPacketBuffer(parser, number++));
+  }
+
+  buffers.push(createPacketBuffer(parser, new Packets.EofPacket));
+
+  buffers = mergeBuffers(buffers);
+
+  var bytes = buffers.reduce(function(bytes, buffer) {
+    return bytes + buffer.length;
+  }, 0);
+
+  var mb = (bytes / 1024 / 1024).toFixed(2)
+
+  console.error('%s buffers (%s mb) in %s ms', buffers.length, mb, (Date.now() - start));
+
+  return buffers;
+}
+
+function createPacketBuffer(parser, packet) {
+  var writer = new PacketWriter();
+  packet.write(writer);
+  return writer.toBuffer(parser);
+}
+
+function createRowDataPacketBuffer(parser, number) {
+  var writer = new PacketWriter();
+
+  writer.writeLengthCodedString(parser._nextPacketNumber);
+  writer.writeLengthCodedString('Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has sur');
+
+  return writer.toBuffer(parser);
+}
+
+function mergeBuffers(buffers) {
+  var mergeBuffer  = new Buffer(options.bufferSize);
+  var mergeBuffers = [];
+  var offset       = 0;
+
+  for (var i = 0; i < buffers.length; i++) {
+    var buffer = buffers[i];
+
+    var bytesRemaining = mergeBuffer.length - offset;
+    if (buffer.length < bytesRemaining) {
+      buffer.copy(mergeBuffer, offset);
+      offset += buffer.length;
+    } else {
+      buffer.copy(mergeBuffer, offset, 0, bytesRemaining);
+      mergeBuffers.push(mergeBuffer);
+
+      mergeBuffer = new Buffer(options.bufferSize);
+      buffer.copy(mergeBuffer, 0, bytesRemaining);
+      offset = buffer.length - bytesRemaining;
+    }
+  }
+
+  if (offset > 0) {
+    mergeBuffers.push(mergeBuffer.slice(0, offset));
+  }
+
+  return mergeBuffers;
+}
+
+function benchmark(buffers) {
+  var protocol = new Protocol();
+  protocol._handshakeInitializationPacket = true;
+  protocol.query({typeCast: false, sql: 'SELECT ...'});
+
+  var start = +new Date;
+
+  for (var i = 0; i < buffers.length; i++) {
+    protocol.write(buffers[i]);
+  }
+
+  var duration = Date.now() - start;
+  var hz = Math.round(options.rows / (duration / 1000));
+  console.log(hz);
+}
+
+var buffers = createBuffers();
+while (true) {
+  benchmark(buffers);
+}

node_modules/mysql/benchmark/select-100k-blog-rows.js

+var common     = require('../test/common');
+var client     = common.createConnection({typeCast: false});
+var rowsPerRun = 100000;
+
+client.connect(function(err) {
+  if (err) throw err;
+
+  client.query('USE node_mysql_test', function(err, results) {
+    if (err) throw err;
+
+    selectRows();
+  });
+});
+
+var firstSelect;
+var rowCount = 0;
+
+console.error('Benchmarking rows per second in hz:');
+
+function selectRows() {
+  firstSelect = firstSelect || Date.now();
+
+  client.query('SELECT * FROM posts', function(err, rows) {
+    if (err) throw err;
+
+    rowCount += rows.length;
+    if (rowCount < rowsPerRun) {
+      selectRows();
+      return;
+    }
+
+    var duration = (Date.now() - firstSelect) / 1000;
+    var hz = Math.round(rowCount / duration);
+
+    console.log(hz);
+
+    rowCount    = 0;
+    firstSelect = null;
+
+    selectRows();
+  });
+};

node_modules/mysql/index.js

+var Connection = require('./lib/Connection');
+
+exports.createConnection = function(config) {
+  return new Connection({config: config});
+};

node_modules/mysql/lib/Config.js

+var urlParse        = require('url').parse;
+var ClientConstants = require('./protocol/constants/client');
+var Charsets        = require('./protocol/constants/charsets');
+
+module.exports = Config;
+function Config(options) {
+  if (typeof options === 'string') {
+    options = Config.parseUrl(options);
+  }
+
+  this.host         = options.host || 'localhost';
+  this.port         = options.port || 3306;
+  this.socketPath   = options.socketPath;
+  this.user         = options.user || undefined;
+  this.password     = options.password || undefined;
+  this.database     = options.database;
+  this.insecureAuth = options.insecureAuth || false;
+  this.debug        = options.debug;
+  this.typeCast     = (options.typeCast === undefined)
+    ? true
+    : options.typeCast;
+
+  this.maxPacketSize = 0;
+  this.charsetNumber = (options.charset)
+    ? Charsets[options.charset]
+    : Charsets.UTF8_GENERAL_CI;
+
+  this.clientFlags =
+    ClientConstants.CLIENT_LONG_PASSWORD |
+    ClientConstants.CLIENT_FOUND_ROWS |
+    ClientConstants.CLIENT_LONG_FLAG |
+    ClientConstants.CLIENT_CONNECT_WITH_DB |
+    ClientConstants.CLIENT_ODBC |
+    ClientConstants.CLIENT_LOCAL_FILES |
+    ClientConstants.CLIENT_IGNORE_SPACE |
+    ClientConstants.CLIENT_PROTOCOL_41 |
+    ClientConstants.CLIENT_IGNORE_SIGPIPE |
+    ClientConstants.CLIENT_TRANSACTIONS |
+    ClientConstants.CLIENT_RESERVED |
+    ClientConstants.CLIENT_SECURE_CONNECTION |
+    ClientConstants.CLIENT_MULTI_RESULTS;
+
+  if (options.multipleStatements) {
+    this.clientFlags |= ClientConstants.CLIENT_MULTI_STATEMENTS;
+  }
+}
+
+Config.parseUrl = function(url) {
+  url = urlParse(url, true);
+
+  var options = {
+    host     : url.hostname,
+    port     : url.port,
+    database : url.pathname.substr(1),
+  };
+
+  if (url.auth) {
+    var auth = url.auth.split(':');
+    options.user     = auth[0];
+    options.password = auth[1];
+  }
+
+  if (url.query) {
+    for (var key in url.query) {
+      var value = url.query[key];
+
+      try {
+        // Try to parse this as a JSON expression first
+        options[key] = JSON.parse(value);
+      } catch (err) {
+        // Otherwise assume it is a plain string
+        options[key] = value;
+      }
+    }
+  }
+
+  return options;
+};

node_modules/mysql/lib/Connection.js

+var Net          = require('net');
+var Config       = require('./Config');
+var Protocol     = require('./protocol/Protocol');
+var SqlString    = require('./protocol/SqlString');
+var EventEmitter = require('events').EventEmitter;
+var Util         = require('util');
+
+module.exports = Connection;
+Util.inherits(Connection, EventEmitter);
+function Connection(options) {
+  EventEmitter.call(this);
+
+  this.config = new Config(options.config);
+
+  this._socket        = options.socket;
+  this._protocol      = new Protocol({config: this.config});
+  this._connectCalled = false;
+}
+
+Connection.prototype.connect = function(cb) {
+  if (!this._connectCalled) {
+    this._connectCalled = true;
+
+    if (!this._socket) {
+      this._socket = (this.config.socketPath)
+        ? Net.createConnection(this.config.socketPath)
+        : Net.createConnection(this.config.port, this.config.host);
+    }
+
+    this._socket.pipe(this._protocol);
+    this._protocol.pipe(this._socket);
+
+    this._socket.on('error', this._handleNetworkError.bind(this));
+    this._protocol.on('unhandledError', this._handleProtocolError.bind(this));
+    this._protocol.on('close', this._handleProtocolClose.bind(this));
+  }
+
+  this._protocol.handshake(cb);
+};
+
+Connection.prototype.query = function(sql, values, cb) {
+  this._implyConnect();
+
+  var options = {};
+
+  if (typeof sql === 'object') {
+    // query(options, cb)
+    options = sql;
+    cb      = values;
+    values  = options.values;
+
+    delete options.values;
+  } else if (typeof values === 'function') {
+    // query(sql, cb)
+    cb          = values;
+    options.sql = sql;
+    values      = undefined;
+  } else {
+    // query(sql, values, cb)
+    options.sql    = sql;
+    options.values = values;
+  }
+
+  options.sql = this.format(options.sql, values || []);
+
+  if (!('typeCast' in options)) {
+    options.typeCast = this.config.typeCast;
+  }
+
+  return this._protocol.query(options, cb);
+};
+
+Connection.prototype.end = function(cb) {
+  this._implyConnect();
+  this._protocol.quit(cb);
+};
+
+Connection.prototype.destroy = function() {
+  this._implyConnect();
+  this._socket.destroy();
+  this._protocol.destroy();
+};
+
+Connection.prototype.pause = function() {
+  this._socket.pause();
+  this._protocol.pause();
+};
+
+Connection.prototype.resume = function() {
+  this._socket.resume();
+  this._protocol.resume();
+};
+
+Connection.prototype.escape = function(value) {
+  return SqlString.escape(value);
+};
+
+Connection.prototype.format = function(sql, values) {
+  return SqlString.format(sql, values);
+};
+
+Connection.prototype._handleNetworkError = function(err) {
+  this._protocol.handleNetworkError(err);
+};
+
+Connection.prototype._handleProtocolError = function(err) {
+  this.emit('error', err);
+};
+
+Connection.prototype._handleProtocolClose = function(err) {
+  this.emit('close', err);
+};
+
+Connection.prototype._implyConnect = function() {
+  if (!this._connectCalled) {
+    this.connect();
+  }
+};

node_modules/mysql/lib/protocol/Auth.js

+var Buffer = require('buffer').Buffer;
+var Crypto = require('crypto');
+var Auth   = exports;
+
+function sha1(msg) {
+  var hash = Crypto.createHash('sha1');
+  hash.update(msg);
+  // hash.digest() does not output buffers yet
+  return hash.digest('binary');
+};
+Auth.sha1 = sha1;
+
+function xor(a, b) {
+  a = new Buffer(a, 'binary');
+  b = new Buffer(b, 'binary');
+  var result = new Buffer(a.length);
+  for (var i = 0; i < a.length; i++) {
+    result[i] = (a[i] ^ b[i]);
+  }
+  return result;
+};
+Auth.xor = xor;
+
+Auth.token = function(password, scramble) {
+  if (!password) {
+    return new Buffer(0);
+  }
+
+  var stage1 = sha1(password);
+  var stage2 = sha1(stage1);
+  var stage3 = sha1(scramble.toString('binary') + stage2);
+  return xor(stage3, stage1);
+};
+
+// This is a port of sql/password.c:hash_password which needs to be used for
+// pre-4.1 passwords.
+Auth.hashPassword = function(password) {
+  var nr = [0x5030, 0x5735],
+      add = 7,
+      nr2 = [0x1234, 0x5671],
+      result = new Buffer(8);
+
+  if (typeof password == 'string'){
+    password = new Buffer(password);
+  }
+
+  for (var i = 0; i < password.length; i++) {
+    var c = password[i];
+    if (c == 32 || c == 9) {
+      // skip space in password
+      continue;
+    }
+
+    // nr^= (((nr & 63)+add)*c)+ (nr << 8);
+    // nr = xor(nr, add(mul(add(and(nr, 63), add), c), shl(nr, 8)))
+    nr = this.xor32(nr, this.add32(this.mul32(this.add32(this.and32(nr, [0,63]), [0,add]), [0,c]), this.shl32(nr, 8)));
+
+    // nr2+=(nr2 << 8) ^ nr;
+    // nr2 = add(nr2, xor(shl(nr2, 8), nr))
+    nr2 = this.add32(nr2, this.xor32(this.shl32(nr2, 8), nr));
+
+    // add+=tmp;
+    add += c;
+  }
+
+  this.int31Write(result, nr, 0);
+  this.int31Write(result, nr2, 4);
+
+  return result;
+};
+
+Auth.randomInit = function(seed1, seed2) {
+  return {
+    max_value: 0x3FFFFFFF,
+    max_value_dbl: 0x3FFFFFFF,
+    seed1: seed1 % 0x3FFFFFFF,
+    seed2: seed2 % 0x3FFFFFFF
+  };
+};
+
+Auth.myRnd = function(r){
+  r.seed1 = (r.seed1 * 3 + r.seed2) % r.max_value;
+  r.seed2 = (r.seed1 + r.seed2 + 33) % r.max_value;
+
+  return r.seed1 / r.max_value_dbl;
+};
+
+Auth.scramble323 = function(message, password) {
+  var to = new Buffer(8),
+      hashPass = this.hashPassword(password),
+      hashMessage = this.hashPassword(message.slice(0, 8)),
+      seed1 = this.int32Read(hashPass, 0) ^ this.int32Read(hashMessage, 0),
+      seed2 = this.int32Read(hashPass, 4) ^ this.int32Read(hashMessage, 4),
+      r = this.randomInit(seed1, seed2);
+
+  for (var i = 0; i < 8; i++){
+    to[i] = Math.floor(this.myRnd(r) * 31) + 64;
+  }
+  var extra = (Math.floor(this.myRnd(r) * 31));
+
+  for (var i = 0; i < 8; i++){
+    to[i] ^= extra;
+  }
+
+  return to;
+};
+
+Auth.fmt32 = function(x){
+  var a = x[0].toString(16),
+      b = x[1].toString(16);
+
+  if (a.length == 1) a = '000'+a;
+  if (a.length == 2) a = '00'+a;
+  if (a.length == 3) a = '0'+a;
+  if (b.length == 1) b = '000'+b;
+  if (b.length == 2) b = '00'+b;
+  if (b.length == 3) b = '0'+b;
+  return '' + a + '/' + b;
+};
+
+Auth.xor32 = function(a,b){
+  return [a[0] ^ b[0], a[1] ^ b[1]];
+};
+
+Auth.add32 = function(a,b){
+  var w1 = a[1] + b[1],
+      w2 = a[0] + b[0] + ((w1 & 0xFFFF0000) >> 16);
+
+  return [w2 & 0xFFFF, w1 & 0xFFFF];
+};
+
+Auth.mul32 = function(a,b){
+  // based on this example of multiplying 32b ints using 16b
+  // http://www.dsprelated.com/showmessage/89790/1.php
+  var w1 = a[1] * b[1],
+      w2 = (((a[1] * b[1]) >> 16) & 0xFFFF) + ((a[0] * b[1]) & 0xFFFF) + (a[1] * b[0] & 0xFFFF);
+
+  return [w2 & 0xFFFF, w1 & 0xFFFF];
+};
+
+Auth.and32 = function(a,b){
+  return [a[0] & b[0], a[1] & b[1]];
+};
+
+Auth.shl32 = function(a,b){
+  // assume b is 16 or less
+  var w1 = a[1] << b,
+      w2 = (a[0] << b) | ((w1 & 0xFFFF0000) >> 16);
+
+  return [w2 & 0xFFFF, w1 & 0xFFFF];
+};
+
+Auth.int31Write = function(buffer, number, offset) {
+  buffer[offset] = (number[0] >> 8) & 0x7F;
+  buffer[offset + 1] = (number[0]) & 0xFF;
+  buffer[offset + 2] = (number[1] >> 8) & 0xFF;
+  buffer[offset + 3] = (number[1]) & 0xFF;
+};
+
+Auth.int32Read = function(buffer, offset){
+  return (buffer[offset] << 24)
+       + (buffer[offset+1] << 16)
+       + (buffer[offset+2] << 8)
+       + (buffer[offset+3]);
+};

node_modules/mysql/lib/protocol/PacketHeader.js

+module.exports = PacketHeader;
+function PacketHeader(length, number) {
+  this.length = length;
+  this.number = number;
+}

node_modules/mysql/lib/protocol/PacketWriter.js

+var BIT_16 = Math.pow(2, 16);
+var BIT_24 = Math.pow(2, 24);
+// The maximum precision JS Numbers can hold precisely
+// Don't panic: Good enough to represent byte values up to 8192 TB
+var IEEE_754_BINARY_64_PRECISION = Math.pow(2, 53);
+var MAX_PACKET_LENGTH            = Math.pow(2, 24) - 1;
+
+module.exports = PacketWriter;
+function PacketWriter() {
+  this._buffer = new Buffer(0);
+  this._offset = 0;
+}
+
+PacketWriter.prototype.toBuffer = function(parser) {
+  var packets  = Math.floor(this._buffer.length / MAX_PACKET_LENGTH) + 1;
+  var buffer   = this._buffer;
+  this._buffer = new Buffer(this._buffer.length + packets * 4);
+
+  for (var packet = 0; packet < packets; packet++) {
+    this._offset = packet * (MAX_PACKET_LENGTH + 4);
+
+    var isLast = (packet + 1 === packets);
+    var packetLength = (isLast)
+      ? buffer.length % MAX_PACKET_LENGTH
+      : MAX_PACKET_LENGTH;
+
+    var packetNumber = parser.incrementPacketNumber();
+
+    this.writeUnsignedNumber(3, packetLength);
+    this.writeUnsignedNumber(1, packetNumber);
+
+    var start = packet * MAX_PACKET_LENGTH;
+    var end   = start + packetLength;
+
+    this.writeBuffer(buffer.slice(start, end));
+  }
+
+  return this._buffer;
+};
+
+PacketWriter.prototype.writeUnsignedNumber = function(bytes, value) {
+  this._allocate(bytes);
+
+  for (var i = 0; i < bytes; i++) {
+    this._buffer[this._offset++] = (value >> (i * 8)) & 0xff;
+  }
+};
+
+PacketWriter.prototype.writeFiller = function(bytes) {
+  this._allocate(bytes);
+
+  for (var i = 0; i < bytes; i++) {
+    this._buffer[this._offset++] = 0x00;
+  }
+};
+
+PacketWriter.prototype.writeNullTerminatedString = function(value, encoding) {
+  // Typecast undefined into '' and numbers into strings
+  value = value || '';
+  value = value + '';
+
+  var bytes = Buffer.byteLength(value, encoding || 'utf-8') + 1;
+  this._allocate(bytes);
+
+  this._buffer.write(value, this._offset, encoding);
+  this._buffer[this._offset + bytes - 1] = 0x00;
+
+  this._offset += bytes;
+};
+
+PacketWriter.prototype.writeString = function(value) {
+  // Typecast undefined into '' and numbers into strings
+  value = value || '';
+  value = value + '';
+
+  var bytes = Buffer.byteLength(value, 'utf-8');
+  this._allocate(bytes);
+
+  this._buffer.write(value, this._offset, 'utf-8');
+
+  this._offset += bytes;
+};
+
+PacketWriter.prototype.writeBuffer = function(value) {
+  var bytes = value.length;
+
+  this._allocate(bytes);
+  value.copy(this._buffer, this._offset);
+  this._offset += bytes;
+};
+
+PacketWriter.prototype.writeLengthCodedNumber = function(value) {
+  if (value === null) {
+    this._allocate(1);
+    this._buffer[this._offset++] = 251;
+    return;
+  }
+
+  if (value <= 250) {
+    this._allocate(1);
+    this._buffer[this._offset++] = value;
+    return;
+  }
+
+  if (value > IEEE_754_BINARY_64_PRECISION) {
+    throw new Error(
+      'writeLengthCodedNumber: JS precision range exceeded, your ' +
+      'number is > 53 bit: "' + value + '"'
+    );
+  }
+
+  if (value <= BIT_16) {
+    this._allocate(3)
+    this._buffer[this._offset++] = 252;
+  } else if (value <= BIT_24) {
+    this._allocate(4)
+    this._buffer[this._offset++] = 253;
+  } else {
+    this._allocate(9);
+    this._buffer[this._offset++] = 254;
+  }
+
+  // 16 Bit
+  this._buffer[this._offset++] = value & 0xff;
+  this._buffer[this._offset++] = (value >> 8) & 0xff;
+
+  if (value <= BIT_16) return;
+
+  // 24 Bit
+  this._buffer[this._offset++] = (value >> 16) & 0xff;
+
+  if (value <= BIT_24) return;
+
+  this._buffer[this._offset++] = (value >> 24) & 0xff;
+
+  // Hack: Get the most significant 32 bit (JS bitwise operators are 32 bit)
+  value = value.toString(2);
+  value = value.substr(0, value.length - 32);
+  value = parseInt(value, 2);
+
+  this._buffer[this._offset++] = value & 0xff;
+  this._buffer[this._offset++] = (value >> 8) & 0xff;
+  this._buffer[this._offset++] = (value >> 16) & 0xff;
+
+  // Set last byte to 0, as we can only support 53 bits in JS (see above)
+  this._buffer[this._offset++] = 0;
+};
+
+PacketWriter.prototype.writeLengthCodedBuffer = function(value) {
+  var bytes = value.length;
+  this.writeLengthCodedNumber(bytes);
+  this.writeBuffer(value);
+};
+
+PacketWriter.prototype.writeNullTerminatedBuffer = function(value) {
+  this.writeBuffer(value);
+  this.writeFiller(1); // 0x00 terminator
+};
+
+PacketWriter.prototype.writeLengthCodedString = function(value) {
+  if (value === null) {
+    this.writeLengthCodedNumber(null);
+    return;
+  }
+
+  value = (value === undefined)
+    ? ''
+    : String(value);
+
+  var bytes = Buffer.byteLength(value, 'utf-8');
+  this.writeLengthCodedNumber(bytes);
+
+  if (!bytes) {
+    return;
+  }
+
+  this._allocate(bytes);
+  this._buffer.write(value, this._offset, 'utf-8');
+  this._offset += bytes;
+};
+
+PacketWriter.prototype._allocate = function(bytes) {
+  if (!this._buffer) {
+    this._buffer = new Buffer(bytes);
+    return;
+  }
+
+  var bytesRemaining = this._buffer.length - this._offset;
+  if (bytesRemaining >= bytes) {
+    return;
+  }
+
+  var oldBuffer = this._buffer;
+
+  this._buffer = new Buffer(oldBuffer.length + bytes);
+  oldBuffer.copy(this._buffer);
+};

node_modules/mysql/lib/protocol/Parser.js

+var IEEE_754_BINARY_64_PRECISION = Math.pow(2, 53);
+var MAX_PACKET_LENGTH            = Math.pow(2, 24) - 1;
+var PacketHeader                 = require('./PacketHeader');
+
+module.exports = Parser;
+function Parser(options) {
+  options = options || {};
+
+  this._buffer            = null;
+  this._longPacketBuffers = [];
+  this._offset            = 0;
+  this._packetEnd         = null;