commit ec2e35c11742b7992e7ac84211c546f2e00f1f82 Author: Steve Saldivar Date: Tue May 7 14:03:51 2013 -0700 commit diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..412eeda --- /dev/null +++ b/.gitattributes @@ -0,0 +1,22 @@ +# Auto detect text files and perform LF normalization +* text=auto + +# Custom for Visual Studio +*.cs diff=csharp +*.sln merge=union +*.csproj merge=union +*.vbproj merge=union +*.fsproj merge=union +*.dbproj merge=union + +# Standard to msysgit +*.doc diff=astextplain +*.DOC diff=astextplain +*.docx diff=astextplain +*.DOCX diff=astextplain +*.dot diff=astextplain +*.DOT diff=astextplain +*.pdf diff=astextplain +*.PDF diff=astextplain +*.rtf diff=astextplain +*.RTF diff=astextplain diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5bfb3cf --- /dev/null +++ b/.gitignore @@ -0,0 +1,176 @@ +################# +## Eclipse +################# + +*.pydevproject +.project +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.classpath +.settings/ +.loadpath + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# CDT-specific +.cproject + +# PDT-specific +.buildpath + + +################# +## Visual Studio +################# + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.sln.docstates + +# Build results +[Dd]ebug/ +[Rr]elease/ +*_i.c +*_p.c +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.vspscc +.builds +*.dotCover + +## TODO: If you have NuGet Package Restore enabled, uncomment this +#packages/ + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf + +# Visual Studio profiler +*.psess +*.vsp + +# ReSharper is a .NET coding add-in +_ReSharper* + +# Installshield output folder +[Ee]xpress + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish + +# Others +[Bb]in +[Oo]bj +sql +TestResults +*.Cache +ClientBin +stylecop.* +~$* +*.dbmdl +Generated_Code #added for RIA/Silverlight projects + +# Backup & report files from converting an old project file to a newer +# Visual Studio version. Backup files are not needed, because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML + + + +############ +## Windows +############ + +# Windows image file caches +Thumbs.db + +# Folder config file +Desktop.ini + +# batch files +*.bat + +############# +## Python +############# + +*.py[co] + +# Packages +*.egg +*.egg-info +dist +build +eggs +parts +bin +var +sdist +develop-eggs +.installed.cfg + +# Installer logs +pip-log.txt + +# Unit test / coverage reports +.coverage +.tox + +#Translations +*.mo + +#Mr Developer +.mr.developer.cfg + +# Mac crap +.DS_Store + +################### +## Project Specific +################### + +twitch.css +tools/ +logs/* +bitly.js +update.js +users/ \ No newline at end of file diff --git a/License.md b/License.md new file mode 100644 index 0000000..bda2747 --- /dev/null +++ b/License.md @@ -0,0 +1,50 @@ +Software license for LoyaltyBot +=============================== + +SUMMARY +------- + +- Can be used on unlimited sites, servers +- Source-code cannot be resold +- Non-commercial use only +- Can modify source-code and distribute modifications (derivative works) authorized 3rd Parties +- Any code deemed awesome across any fork will be merged into the main branch + +TERMS AND CONDITIONS +-------------------- + +1. **Preamble:** This Agreement, signed on Feb 20, 2013 [hereinafter: Effective Date] governs the relationship between User, a private person, (hereinafter: Licensee) and LoyaltyBot, a private person whose principal place of business is CA, United States (Hereinafter: Licensor). This Agreement sets the terms, rights, restrictions and obligations on using [LoyaltyBot] (hereinafter: The Software) created and owned by Licensor, as detailed herein +2. **License Grant:** Licensor hereby grants Licensee a Personal, Non-assignable & non-transferable, Non-commercial, Including the rights to distribute derivative works, Non-exclusive license, all with accordance with the terms set forth and other legal restrictions set forth in 3rd party software used while running Software + * 2.1. **Limited:** Licensee may use Software for the purpose of: + - 2.1.1. Running Software on Licensee's Website[s] and Server[s]; + - 2.1.2. Allowing authorized 3rd Parties to run Software on Licensee's Website[s] and Server[s]; + - 2.1.3. Publishing Software's output to Licensee and authorized 3rd Parties; + - 2.1.4. Distribute verbatim copies of Software's output (including compiled binaries) to authorized 3rd Parties; + - 2.1.5. Modify Software to suit Licensee's needs and specifications. + * 2.2. **Non Assignable & Non-Transferable:** Licensee may not assign or transfer his rights and duties under this license. + * 2.3. **Non-Commercial:** Licensee may not use Software for commercial purposes. for the purpose of this license, commercial purposes means that a 3rd party has to pay in order to access Software or that the Website that runs Software is behind a paywall. + * 2.4. **Including the Right to Create Derivative Works:** Licensee may create derivative works based on Software, including amending Software's source code, modifying it, integrating it into a larger work or removing portions of Software, as long as no distribution of the derivative works is made + * 2.5. **Including the right to Distribute Derivative Works:** Licensee may create and distribute derivative works based on Software, including amending Software's source code, modifying it, integrating it into a larger work or removing portions of Software, as long as credit to the Licensor is granted and that redistribution is made under the terms of this license. + * 2.6. **[Multi-]Site:** Licensee may use Software on unlimited server[s] and unlimited website[s], for Licensee's websites only +3. **Term & Termination:** The Term of this license shall be until terminated. Licensor may terminate this Agreement, including Licensee's license in the case where Licensee : + * 3.1. became insolvent or otherwise entered into any liquidation process; or + * 3.2. exported The Software to any jurisdiction where licensor may not enforce his rights under this agreements in; or + * 3.3. Licenee was in breach of any of this license's terms and conditions and such breach was not cured, immediately upon notification; or + * 3.4. Licensee in breach of any of the terms of clause 2 to this license; or + * 3.5. Licensee otherwise entered into any arrangement which caused Licensor to be unable to enforce his rights under this License. +4. **Payment:** In consideration of the License granted under clause 2, Licensee may be required to pay the Licensor, via PayPal or any other mean which Licensor may deem adequate. Failure to perform payment shall construe as material breach of this Agreement. +5. **Upgrades, Updates and Fixes:** Licensor may provide Licensee, from time to time, with Upgrades, Updates or Fixes, as detailed herein and according to his sole discretion. Licensee hereby warrants to keep The Software up-to-date and install all relevant updates and fixes, and may, at his sole discretion, purchase upgrades, according to the rates set by Licensor. Licensor shall provide any update or Fix free of charge; however, nothing in this Agreement shall require Licensor to provide Updates or Fixes. + * 5.1. **Upgrades:** for the purpose of this license, an Upgrade shall be a material amendment in The Software, which contains new features and or major performance improvements which can and will be pulled from any of the given forks into the main code branch. For example, should Licensee make an upgrade that is beneficial to the main code branch, the Licensor reserves the right to merge the Licensee's ugraded code into the main branch. + * 5.2. **Updates:** for the purpose of this license, an update shall be a minor amendment in The Software, which may contain new features or minor improvements. For example, should Licensee make an update that is beneficial to the main code branch, the Licensor reserves the right to merge the Licensee's update into the main code branch. + * 5.3. **Fix:** for the purpose of this license, a fix shall be a minor amendment in The Software, intended to remove bugs or alter minor features which impair the The Software's functionality. For example, should Licensee make fix that is beneficial to the main code branch, the Licensor reserves the right to merge the Licensee's fix into the main code branch. +6. **Support:** Software is provided under an AS-IS basis and without any support, updates or maintenance. Nothing in this Agreement shall require Licensor to provide Licensee with support or fixes to any bug, failure, mis-performance or other defect in The Software. + * 6.1. **Bug Notification:** Licensee may provide Licensor of details regarding any bug, defect or failure in The Software promptly and with no delay from such event; Licensee shall comply with Licensor's request for information regarding bugs, defects or failures and furnish him with information, screenshots and try to reproduce such bugs, defects or failures. + * 6.2. **Feature Request:** Licensee may request additional features in Software, provided, however, that (i) Licesee shall waive any claim or right in such feature should feature be developed by Licensor; (ii) Licensee shall be prohibited from developing the feature, or disclose such feature request, or feature, to any 3rd party directly competing with Licensor or any 3rd party which may be, following the development of such feature, in direct competition with Licensor; (iii) Licensee warrants that feature does not infringe any 3rd party patent, trademark, trade-secret or any other intellectual property right; and (iv) Licensee developed, envisioned or created the feature solely by himself. +7. **Liability:** To the extent permitted under Law, The Software is provided under an AS-IS basis. Licensor shall never, and without any limit, be liable for any damage, cost, expense or any other payment incurred by Licesee as a result of Software's actions, failure, bugs and/or any other interaction between The Software and Licesee's end-equipment, computers, other software or any 3rd party, end-equipment, computer or services. Moreover, Licensor shall never be liable for any defect in source code written by Licensee when relying on The Software or using The Software's source code. +8. **Warranty:** + * 8.1. **Intellectual Property:** Licensor hereby warrants that The Software does not violate or infringe any 3rd party claims in regards to intellectual property, patents and/or trademarks and that to the best of its knowledge no legal action has been taken against it for any infringement or violation of any 3rd party intellectual property rights. + * 8.2. **No-Warranty:** The Software is provided without any warranty; Licensor hereby disclaims any warranty that The Software shall be error free, without defects or code which may cause damage to Licensee's computers or to Licensee, and that Software shall be functional. Licensee shall be solely liable to any damage, defect or loss incurred as a result of operating software and undertake the risks contained in running The Software on License's Server[s] and Website[s]. + * 8.3. **Prior Inspection:** Licensee hereby states that he inspected The Software thoroughly and found it satisfactory and adequate to his needs, that it does not interfere with his regular operation and that it does meet the standards and scope of his computer systems and architecture. Licensee found that The Software interacts with his development, website and server environment and that it does not infringe any of End User License Agreement of any software Licensee may use in performing his services. Licensee hereby waives any claims regarding The Software's incompatibility, performance, results and features, and warrants that he inspected the The Software. +9. **No Refunds:** Licensee warrants that he inspected The Software according to clause 7(c) and that it is adequate to his needs. Accordingly, as The Software is intangible goods, Licensee shall not be, ever, entitled to any refund, rebate, compensation or restitution for any reason whatsoever, even if The Software contains material flaws. +10. **Indemnification:** Licensee hereby warrants to hold Licensor harmless and indemnify Licensor for any lawsuit brought against it in regards to Licensee's use of The Software in means that violate, breach or otherwise circumvent this license, Licensor's intellectual property rights or Licensor's title in The Software. Licensor shall promptly notify Licensee in case of such legal action and request Licensee's consent prior to any settlement in relation to such lawsuit or claim. +11. **Governing Law, Jurisdiction:** Licensee hereby agrees not to initiate class-action lawsuits against Licensor in relation to this license and to compensate Licensor for any legal fees, cost or attorney fees should any claim brought by Licensee against Licensor be denied, in part or in full. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..8f2c14a --- /dev/null +++ b/README.md @@ -0,0 +1,140 @@ +LoyaltyBot +========= + +A TwitchTV viewer reward system + +Written in Node and JavaScript + +*By having access to this repository you and any authorized 3rd Parties agree to the [software license](https://github.com/SupremoRTD/LoyaltyBot/blob/master/License.md#software-license-for-loyaltybot)* + +Overview +-------- + +LoyaltyBot is a chat bot that allows you to reward viewers with loyalty points for hanging out on your stream. It's +main purpose is to allow you, the broadcaster, to reward the viewers that are dedicated to watching your stream as +opposed to the viewers that just stop by for a quick giveaway and leave. + +####Features + +- Fully functioning auction and raffle systems +- Double loyalty points for subscribers/loyal viewers + +####Extras + +- Moderator commands (currently in development, semi functional) + +####Future Features/Extras + +- Free subscriber/paid non-subscriber Jukebox (still in development) +- Stream title updates posted to twitter (still in development) + +Basic Setup +----------- + +example.js + +````javascript +var LoyaltyBot = require('./lib/initialize.js'); + +LoyaltyBot.initialize({ + // twitch info + twitch : { + channel : 'loyalty', + bot : {name: 'LoyaltyBot', password: 'loyalty!loyalty!loyalty!'}, + subscribers : 'https://spreadsheets.google.com/feeds/list/****/od6/public/basic?alt=json' + }, + + // currency system + currency : { + name : 'LoyaltyPoints', + payrate : 15, + host : '127.0.0.1', + user : 'mysql_user', + password : 'mysql_password', + database : 'mysql_database', + website : 'http://www.loyaltypoints.com' + }, + + // optional + commands: true +}); +```` + +Configuration Options +--------------------- +Twitch + +- ````channel````: the channel name (in lower case) +- ````bot.name````: the account name of the bot +- ````bot.password````: the password for the bot +- ````subscribers````: a google doc that contains subscriber names (more info on this later) + +Currency + +- ````name````: custom name for the loyalty points +- ````payrate````: how often to hand out loyalty points (in minutes) +- ````host````: mysql database hostname/ip +- ````user````: username for the mysql database +- ````password````: password for the mysql database +- ````database````: mysql database name +- ````website````: provides loyalty bot with an offsite location for checking currency. can also be and be an empty ````string```` + +Optional + +- ````commands````: enable/disable the ability to use moderator commands. boolean: accepts ````true```` or ````false```` + +Preparing to Setup Your Channel's Bot +------------------------------------- + +####Create a new bot account + +In order to use LoyaltyBot's features you will need to create a new [TwitchTV Account](http://www.twitch.tv/signup). You can name it anything +you wish, all you need to do is pass the username/password to ````bot.name```` and ````bot.password```` + +####Create the subscriber/loyal viewer list on Google Docs + +Since TwitchTV does not have subscriber information in their api, LoyaltyBot needs a way to obtain that information. +That's where [Google Docs](http://docs.google.com/) comes in. + +In the following order: + +- Create a new spreadsheet +- Set cell A1 as the header "Username" +- Subscriber names (must be lower case) will then be in column A starting in cell A2 and below (Fig. 1) +- Set the subscriber list to public and change the type to json (Fig. 2) + +Side Notes: + +- Even if you do not have a subscription button, you can still add loyal viewers to this list for the double loyalty point benefits. +- Why Google Docs and not the MySQL database? Simplicity. It's easier to manually update a google doc spreadsheet daily +then it is to update a MySQL table. + +*Figure 1:* + +![Column Setup](http://i.imgur.com/eyQOwGz.jpg) + +*Figure 2:* + +![Create Link](http://i.imgur.com/jDU9xOR.jpg) + +####Setting up MySQL tables + +LoyaltyBot stores all of the viewer info and moderator commands in a MySQL database and requires specifc table/field names. + +LoyaltyBot takes care of all table creation, however if by some chance you need to manually setup the tables the following +contains information about them: + +Viewer +- Table Name: ````viewers```` +- Field Names: ````user```` [primary key, not null, varchar], ````points```` [not null, integer] + +Commands +- Table Name: ````commands```` +- Field Names: ````id```` [primary key, autoincrement, not null, integer], ````command```` [not null, text], ````text```` [not null, longtext], ````auth```` [default: 1, integer] + +LoyaltyBot In Action +-------------------- +ArchonBot - [ArchonTheWizard](http://www.twitch.tv/archonthewizard) +BingBongBot - [CDXLIV](http://www.twitch.tv/cdxliv) + +If you're using LoyaltyBot send me a [twitch message](http://www.twitch.tv/message/compose?to=rvca18) so I can add you to this list! \ No newline at end of file diff --git a/lib/commands.js b/lib/commands.js new file mode 100644 index 0000000..39e8593 --- /dev/null +++ b/lib/commands.js @@ -0,0 +1,80 @@ +/** + * api: + * Commands(irc object, database object); + * + * example: + * commands = require('./mysql/commands.js')(irc, db, { + * bot_name : 'bot name', + * currency: 'currency name' + * }); + */ + +//-------- Construct --------- +function Commands(irc, db, options) { + var __self = this; + + __self.irc = irc; + __self.db = db; + + // config + __self.config = options || {}; + __self.config.bot_name = options.bot_name || ''; + __self.config.currency = options.currency || 'coins'; + __self.command_list = []; +} + +//-------- Methods -------- +Commands.prototype.start = function() { + var __self = this, + sql = 'SELECT * FROM commands'; + + __self.db.execute(sql, function(rows) { + for (var i = 0; i < rows.length; i++) { + __self.command_list.push(rows[i].command); + } + }); +}; + +Commands.prototype.commands = function(data) { + var __self = this, + command_check = data[3].slice(1).charAt(0), + command = data[3].slice(2); + + // check if potential command was called and match it with stored commands + // if the bots name is used as a command, then display all of the available commands + if(command_check === '!' && __self.command_list.indexOf(command) >= 0) { + var sql = 'SELECT * FROM commands WHERE command = \'' + command + '\''; + + // get command info from database + __self.db.execute(sql, function(rows) { + // filter through command results + for (var i = 0; i < rows.length; i++) { + // match db command with called command + if (rows[i].command = command) { + // display based on viewer auth + if (rows[i].auth === 1) { + __self.irc.emit('message',{message:rows[i].text, options:{caller: __self.irc.caller(data[0]), auth: 1}}); + break; + } else if (rows[i].auth === 0) { + __self.irc.emit('message',{message:rows[i].text, options:null}); + break; + } + } + } + }); + } else if (command_check === '!' && command === __self.config.bot_name.toLowerCase()) { + var commands = '> Commands: !' + __self.config.currency.toLowerCase() + ', '; + for (var i = 0; i < __self.command_list.length; i++) { + if (i !== __self.command_list.length - 1) { + commands += '!' + __self.command_list[i] + ', '; + } else { + commands += '!' + __self.command_list[i]; + } + } + __self.irc.emit('message',{message:commands, options:{caller: __self.irc.caller(data[0]), auth: 1}}); + } +}; + +module.exports = function(irc, db, options) { + return new Commands(irc, db, options); +}; \ No newline at end of file diff --git a/lib/currency.js b/lib/currency.js new file mode 100644 index 0000000..16c06f4 --- /dev/null +++ b/lib/currency.js @@ -0,0 +1,1354 @@ +/** + * api: + * Currency(irc object, database object, [required options]) + * + * example: + * var Currency = require('./lib/plugins/currency.js')(irc, db, { + * currency : 'currency name', + * subscribers : 'google doc spreadsheet until it's available from twitch api' + * }); + * + * commands: + * ! + * reply with currency amount + * + * ! on/[off repeat on/off] + * toggle currency request status, + * repeat status only available when + * turning off requests + * + * ! auction open + * open a new auction + * + * ! auction close + * close current auction + * + * ! auction cancel + * cancel current auction + * + * ! auction draw + * draw the next highest bidder + * + * !bid + * place a bid on an open auction, + * only valid amounts will be accepted + * + * ! raffle open + * open a new raffle + * price and max is optional + * default price: 10 + * default max: 10 + * + * ! raffle close + * draw the another ticket from raffle + * + * ! raffle cancel + * cancel the open raffle + * + * ! raffle draw + * open a new auction + * + * ! raffle restore + * restores a previous raffle if a new + * one is accidentally opened + * + * !ticket + * place a bid on an open auction, + * only valid amounts will be accepted + */ + +var https = require('https'), + http = require('http'), + utils = require('./utils.js'); + +//-------- Construct --------- +function Currency(irc, db, options) { + var __self = this; + + __self.irc = irc; + __self.db = db; + + // config + __self.config = options || {}; + __self.config.currency = options.currency || 'coins'; + __self.config.subscribers_json = options.subscribers || ''; + __self.config.website = options.website || ''; + + // general settings + __self.pre_text = '> ' + __self.config.currency + ': '; + __self.max_requests = 10;//response after 10 request + __self.temp = {}; + + // currency request settings + __self.coin_flood = []; + __self.coin_response = null; + __self.coin_response_timer = 3000; + __self.coin_response_reset = true; + __self.coin_toggle = false; + __self.coin_toggle_msg = null; + __self.coin_toggle_timer = 180000;//milliseconds + + // auction settings + __self.auction_status = false; + __self.auction_bids = []; + __self.auction_previous_bid = {}; + __self.auction_bid_response = null; + __self.auction_bid_response_long = null; + + // handout coins settings + __self.viewer_list = []; + __self.streaming = false; + __self.streaming_check = 4;//minutes + __self.give_coins = false; + __self.give_coins_timer = options.payrate || 30;//minutes + __self.subscriber_check = 4;//minutes + __self.subscribers = []; + + // raffle settings + __self.raffle_status = false; + __self.raffle_ticket_requests = []; + __self.raffle_tickets = []; + __self.raffle_ticket_cost = 10;//currency cost per ticket + __self.raffle_max_tickets = 10; + + // raffle restoration + __self.raffle_restore_ticket_requests = []; + __self.raffle_restore_tickets = []; + + // gambling settings + __self.bets_status = false; + __self.bets_board = []; + __self.bets_viewers = []; + __self.bets_payout = false; +} + +//-------- Methods --------- +Currency.prototype.start = function () { + var __self = this; + __self.handout_coins(); +}; + +Currency.prototype.commands = function (data) { + var __self = this, + broadcaster_bot_initiated = __self.irc.caller(data[0]).toLowerCase() === __self.irc.config.broadcaster.toLowerCase() || __self.irc.caller(data[0]).toLowerCase() === __self.irc.config.name.toLowerCase(), + moderator_initiated = __self.irc.mods.indexOf(__self.irc.caller(data[0])) > 0; + + // handle ! commands + if (data[3].slice(1) === '!' + __self.config.currency.toLowerCase()) { + // public commands + if (!__self.coin_toggle && data[4] === undefined) { + __self.get_coins(__self.irc.caller(data[0])); + } + + // broadcaster only commands + if (broadcaster_bot_initiated) { + //open / close auction system + if (data[4] === 'auction') { + switch (data[5]) { + case 'open': + __self.auction(true); + break; + case 'close': + __self.auction(false); + break; + case 'draw': + __self.next_auction_winner(); + break; + case 'cancel': + __self.auction('cancel'); + break; + } + } + + // open / close raffle system + if (data[4] === 'raffle') { + switch (data[5]) { + case 'open': + if (data[6] && data[7] && !__self.raffle_status) { + if(parseInt(data[6], 10) > 0 && parseInt(data[7], 10) > 0) { + // save default values + __self.temp.raffle_ticket_cost = __self.raffle_ticket_cost; + __self.temp.raffle_max_tickets = __self.raffle_max_tickets; + + // set new raffle cost / amount + __self.raffle_ticket_cost = data[6]; + __self.raffle_max_tickets = data[7]; + } + } else if (__self.temp.raffle_ticket_cost && __self.temp.raffle_max_tickets && !__self.raffle_status){ + __self.raffle_ticket_cost = __self.temp.raffle_ticket_cost; + __self.raffle_max_tickets = __self.temp.raffle_max_tickets; + delete __self.temp.raffle_ticket_cost; + delete __self.temp.raffle_max_tickets; + } + + __self.raffle(true); + break; + case 'close': + __self.raffle(false); + break; + case 'draw': + __self.next_raffle_winner(); + break; + case 'cancel': + __self.raffle('cancel'); + break; + case 'restore': + __self.raffle('restore'); + break; + } + } + + // open / close betting system + if (data[4] === 'bet') { + switch (data[5]) { + case 'open': + if (data[5] && data[6]) { + __self.bets(true, data); + } else { + __self.irc.emit('message', {message:__self.pre_text + 'Unable to open betting, need at least two items to bet against'}); + } + break; + case 'close': + __self.bets(false, null); + break; + case 'winner': + //__self.bets('winner'); + break; + } + } + + // add currency + if (data[4] === 'add') { + if(parseInt(data[5], 10) > 0 && data[6]) { + __self.adjust_currency('add', data[5], data[6]); + } + } + + // remove currency + if (data[4] === 'remove') { + if(parseInt(data[5], 10) > 0 && data[6]) { + __self.adjust_currency('remove', data[5], data[6]); + } + } + + // push currency to new viewer + if (data[4] === 'push') { + if(parseInt(data[5], 10) > 0 && data[6]) { + __self.adjust_currency('push', data[5], data[6]); + } + } + } + + // moderator commands + if (broadcaster_bot_initiated || moderator_initiated) { + // enable/disable currency requests + switch (data[4]) { + case 'on': + if (!__self.auction_status && !__self.raffle_status) { + __self.coin_toggle = false; + + // output currency request status + __self.irc.emit('message', {message:__self.pre_text + 'Currency requests are now enabled. Type !' + __self.config.currency.toLowerCase() + ' to view your total'}); + + // stop periodic message + clearInterval(__self.coin_toggle_msg); + } + break; + case 'off': + if (!__self.auction_status && !__self.raffle_status) { + var msg; + + if (!__self.coin_toggle) { + // output message depending on if an offsite is provided + if (__self.config.website !== '') { + msg = __self.pre_text + 'Currency requests have been disabled. To view your ' + __self.config.currency + ' please visit ' + __self.config.website; + __self.irc.emit('message', {message:msg}); + } else { + msg = __self.pre_text + 'Currency requests have been disabled'; + __self.irc.emit('message', {message:msg}); + } + } + + // start the periodic message + if (data[5] !== undefined && data[6] !== undefined) { + // manually enable / disable repeat + if (data[5] === 'repeat') { + switch (data[6]) { + case 'on': + __self.irc.emit('message', {message:'+ Periodic notification enabled'}); + __self.coin_toggle_msg = setInterval(function () { + if (__self.coin_toggle) { + msg = __self.pre_text + 'To view your ' + __self.config.currency + ' please visit ' + __self.config.website; + __self.irc.emit('message', {message:msg}); + } + }, __self.coin_toggle_timer); + break; + case 'off': + __self.irc.emit('message', {message:'+ Periodic notification disabled'}); + clearInterval(__self.coin_toggle_msg); + } + } + } + + __self.coin_toggle = true; + } + break; + } + + // adjust currency response rate + if (data[4] === 'timer') { + if (isNaN(parseInt(data[5], 10)) === false) { + if (data[5] >= 3 && data[5] % 1 === 0) { + __self.coin_response_timer = data[5] * 1000; + __self.irc.emit('message', {message:__self.pre_text + 'Currency totals will now show ' + data[5] + ' seconds after request'}); + if (data[6] && data[7]) { + if (data[6] === 'reset') { + switch(data[7]) { + case 'on': + __self.irc.emit('message', {message:'+ Timer will now reset after each new request'}); + __self.coin_response_reset = true; + break; + case 'off': + __self.irc.emit('message', {message:'+ Timer will not reset after each new request'}); + __self.coin_response_reset = false; + break; + } + } + } + } else if (data[5] < 3) { + __self.irc.emit('message', {message:__self.pre_text + 'Timer cannot be less than 2 seconds'}); + } + } + } + } + } + + // public commands related to ! + switch (data[3].slice(1)) { + // submit bid for the auction + case '!bid': + if (isNaN(parseInt(data[4], 10)) === false) { + if (data[4] > 0 && data[4] % 1 === 0) { + __self.bid(__self.irc.caller(data[0]), parseInt(data[4], 10)); + } + } + break; + // purchase a ticket for raffle + case '!ticket': + if (isNaN(parseInt(data[4], 10)) === false) { + if (data[4] >= 0 && data[4] % 1 === 0) { + __self.collect_tickets(__self.irc.caller(data[0]), parseInt(data[4], 10)); + } + } + break; + } + + // place a bet + if (__self.bets_status === true) { + for (var i = 0; i < __self.bets_board.length; i++) { + if (data[3].slice(1) === '!' + __self.bets_board[i]) { + if (isNaN(parseInt(data[4], 10)) === false) { + if (data[4] >= 0 && data[4] % 1 === 0) { + __self.collect_bets(__self.irc.caller(data[0]), __self.bets_board[i], parseInt(data[4], 10)); + break; + } + } + } + } + } +}; + +/** + * ============================================ + * CURRENCY REQUESTS + * -------------------------------------------- + */ +Currency.prototype.get_coins = function (caller) { + var __self = this; + + function fill_request(viewer, points) { + var request = '(' + points + ')'; + + if (__self.raffle_status) { + for (var i = 0; i < __self.raffle_ticket_requests.length; i++){ + if (__self.raffle_ticket_requests[i].viewer.toLowerCase() === viewer.toLowerCase() && (__self.raffle_ticket_requests[i].tickets * __self.raffle_ticket_cost) <= points) { + request = '(' + (points - (__self.raffle_ticket_requests[i].tickets * __self.raffle_ticket_cost)) + ') [' + __self.raffle_ticket_requests[i].tickets + ']'; + break; + } + } + return request; + } else { + return request; + } + } + + function do_work() { + var multi_response = ''; + + if (__self.coin_flood.length > 1) {// send flood requests + __self.query_coins(__self.coin_flood, function (rows) { + for (var i = 0; i < rows.length; i++) { + var currency_request = fill_request(rows[i].user, rows[i].points); + // setup currency response + if (i !== rows.length - 1) { + multi_response += rows[i].user + ' ' + currency_request + ', '; + } else { + multi_response += rows[i].user + ' ' + currency_request; + } + } + __self.irc.emit('message', {message:__self.pre_text + multi_response, timer: 1}); + }); + } else if (__self.coin_flood.length === 1) {// send single request + __self.query_coins(caller, function (rows) { + var currency_request = fill_request(rows[0].user, rows[0].points); + __self.irc.emit('message', {message:__self.pre_text + caller + ' ' + currency_request, timer: 1}); + }); + } + + // clear flood requests + __self.coin_flood = []; + } + + // add flood users to array + if (__self.coin_flood.indexOf(caller) < 0) { + __self.coin_flood.push(caller); + } + + // clear timer on flood + if (__self.coin_response_reset) { + clearTimeout(__self.coin_response); + } + + // check if flood has a set amount of requests and output + // if not, set the output timer + if (__self.coin_flood.length === __self.max_requests) { + do_work(); + } else { + if (__self.coin_response_reset) { + __self.coin_response = setTimeout(function () {do_work();}, __self.coin_response_timer); + } else { + setTimeout(function () {do_work();}, __self.coin_response_timer); + } + } +}; + +Currency.prototype.query_coins = function (data, callback) { + var __self = this, sql = ''; + + // build sql conditions + if (typeof data === 'string') { + sql = 'SELECT * FROM viewers WHERE ' + 'user = \'' + data.toLowerCase() + '\''; + } else { + for (var i = 0; i < data.length; i++) { + if (i !== data.length - 1) { + sql += 'SELECT * FROM viewers WHERE ' + 'user = \'' + data[i].toLowerCase() + '\'' + ';'; + } else { + sql += 'SELECT * FROM viewers WHERE ' + 'user = \'' + data[i].toLowerCase() + '\''; + } + } + } + + // execute query + __self.db.execute(sql, function (rows) { + var temp = [], newrows = []; + if (typeof data !== 'string') { + // get rid of the nested arrays + for (var i = 0; i < rows.length; i++) { + if (rows[i].length > 0) { + newrows.push(rows[i][0]); + } + } + + // separate users into their own array + for (var i = 0; i < newrows.length; i++) { + temp.push(newrows[i].user.charAt(0).toUpperCase() + newrows[i].user.slice(1)); + } + + // compare the users in the data array against the temp array + // if not found, push them to rows with 0 points + for (var i = 0; i < data.length; i++) { + if (temp.indexOf(data[i]) < 0) { + newrows.push({'user' : data[i], 'points' : 0}); + } + } + + // capitalize usernames on rows + for (var key in newrows) { + if (newrows.hasOwnProperty(key)) { + newrows[key].user = newrows[key].user.charAt(0).toUpperCase() + newrows[key].user.slice(1); + } + } + rows = newrows; + } else { + if (rows.length === 0) { + rows = [{'user' : data, 'points' : 0}]; + } else { + rows[0].user = rows[0].user.charAt(0).toUpperCase() + rows[0].user.slice(1); + } + } + callback(rows); + }); +}; + +/** + * ============================================ + * HANDOUT CURRENCY + * -------------------------------------------- + */ +Currency.prototype.handout_coins = function () { + var __self = this; + + // check stream status + function stream_status() { + var time = utils.make_interval(__self.streaming_check); + if (time === 0) { + // get stream status + https.get('https://api.twitch.tv/kraken/streams/' + __self.irc.config.channel.slice(1), function (response) { + var body = ''; + + // put together response + response.on('data', function (chunk) { + body += chunk; + }); + + // start / stop handing out coins based on stream status + response.on('end', function () { + var json = JSON.parse(body); + __self.streaming = json.stream !== null; + if (__self.streaming && __self.give_coins === false) { + insert_coins(); + } + __self.irc.emit('data', 'DATA - Online Status Check - Returned: ' + __self.streaming); + setTimeout(stream_status, 1000); + }); + }); + } else { + setTimeout(stream_status, time); + } + } + + // get subscribers + function subscribers() { + var time = utils.make_interval(__self.subscriber_check); + if (time === 0) { + // get stream status + http.get(__self.config.subscribers_json, function (response) { + var body = ''; + // put together response + response.on('data', function (chunk) { + body += chunk; + }); + + // start / stop handing out coins based on stream status + response.on('end', function () { + var json = JSON.parse(body); + var entries = json.feed.entry, subs = ''; + __self.subscribers = []; + for (var i = 0; i < entries.length; i++) { + __self.subscribers.push(entries[i].title['$t']); + subs += entries[i].title['$t'] + ' '; + } + __self.irc.emit('data', 'DATA - Subscriber Check - Returned: ' + subs); + setTimeout(subscribers, 1000); + }); + }); + } else { + setTimeout(subscribers, time); + } + } + + // trigger coin handout + function insert_coins() { + __self.give_coins = __self.streaming; + if (__self.give_coins) { + var time = utils.make_interval(__self.give_coins_timer); + if (time === 0) { + __self.irc.raw('WHO ' + __self.irc.config.channel); + setTimeout(insert_coins, 1000); + } else { + setTimeout(insert_coins, time); + } + } + } + + // monitor viewers in irc + __self.irc.on('data', function (data) { + if (__self.streaming) { + var data_split = data.split(' '), viewer = ''; + + // viewers from \who + if (data_split[3] == '352') { + if (data_split[6] !== undefined) { + viewer = data_split[6].toLowerCase(); + if (__self.viewer_list.indexOf(viewer) < 0) { + __self.viewer_list.push(viewer); + } + } + } + + // viewers chatting + if (data_split[3] == 'PRIVMSG') { + var servernick = data_split[2].toLowerCase().split('!'); + viewer = servernick[0]; + if (viewer != __self.irc.config.name.toLowerCase()) { + viewer = viewer.slice(1); + } + if (__self.viewer_list.indexOf(viewer) < 0) { + __self.viewer_list.push(viewer); + } + } + + // give coins after \who and handout_coins is true + if (__self.give_coins && data_split[3] == '315') { + var clone_viewer_list = __self.viewer_list; + + // clear old list and start recording + __self.viewer_list = []; + + // build sql from the saved viewer list + var sql = ''; + for (var i = 0; i < clone_viewer_list.length; i++) { + var currency_amount = __self.subscribers.indexOf(clone_viewer_list[i]) >= 0 ? 2 : 1; + if (clone_viewer_list[i] !== '') { + if (i != clone_viewer_list.length - 1) { + sql += 'INSERT INTO viewers (user, points) '; + sql += 'VALUES (\'' + clone_viewer_list[i] + '\', ' + currency_amount + ') '; + sql += 'ON DUPLICATE KEY UPDATE points = points + ' + currency_amount + '; '; + } else { + sql += 'INSERT INTO viewers (user, points) '; + sql += 'VALUES (\'' + clone_viewer_list[i] + '\', ' + currency_amount + ') '; + sql += 'ON DUPLICATE KEY UPDATE points = points + ' + currency_amount; + } + } + } + + // execute query + __self.db.execute(sql, function () {}); + } + } else { + __self.viewer_list = []; + } + }); + + stream_status(); + + // only start subscribers if gdoc is available + if (__self.config.subscribers_json !== '') { + subscribers(); + } +}; + +/** + * ============================================ + * Adjust Currency + * -------------------------------------------- + */ +Currency.prototype.adjust_currency = function (method, amount, viewer) { + var __self = this; + + viewer = viewer.toLowerCase(); + + __self.db.execute('SELECT * FROM viewers WHERE user=\'' + viewer + '\'', function(rows){ + if (rows.length > 0 || method === 'push') { + var check = rows.length > 0 ? rows[0].user : rows.push({user: viewer}); + if (check === viewer || method === 'push') { + var sql = '', settings = []; + + // push settings for message + if (method === 'add' || method === 'push') { + settings.push('+'); + settings.push('Added'); + settings.push('to'); + } else if (method === 'remove') { + settings.push('-'); + settings.push('Removed'); + settings.push('from'); + } + settings.push(rows[0].user.charAt(0).toUpperCase() + rows[0].user.slice(1)); + + // create sql + if (method === 'add' || method === 'remove') { + sql += 'UPDATE viewers '; + sql += 'SET points = points ' + settings[0] + ' ' + amount + ' '; + sql += 'WHERE user = \'' + rows[0].user + '\'; '; + } else if (method === 'push') { + sql += 'INSERT INTO viewers (user, points) '; + sql += 'VALUES (\'' + viewer + '\', ' + amount + ') '; + sql += 'ON DUPLICATE KEY UPDATE points = points + ' + amount + '; '; + } + + //execute adjustment + __self.db.execute(sql, function(){ + __self.irc.emit('message', {message:__self.pre_text + settings[1] + ' ' + amount + ' ' + __self.config.currency + ' ' + settings[2] + ' ' + settings[3]}); + }); + } + } else { + __self.irc.emit('message', {message:__self.pre_text + 'User was not found, use the push command to add a new user'}); + } + }); +}; + +/** + * ============================================ + * AUCTION SYSTEM + * -------------------------------------------- + */ +Currency.prototype.auction = function (status) { + var __self = this; + + switch (status) { + case true: + if (!__self.bets_status) { + if (!__self.raffle_status) { + if (!__self.auction_status) { + // open up the auction + __self.auction_status = true; + + // request toggle + if (__self.temp.raffle_toggle) { + __self.temp.auction_toggle = __self.temp.raffle_toggle; + } else { + __self.temp.auction_toggle = __self.coin_toggle; + } + __self.coin_toggle = false; + + // default request timer + if (__self.temp.raffle_timer && __self.temp.raffle_timer_reset) { + __self.temp.auction_timer = __self.temp.raffle_timer; + __self.temp.auction_timer_reset = __self.temp.raffle_timer_reset; + } else { + __self.temp.auction_timer = __self.coin_response_timer; + __self.temp.auction_timer_reset = __self.coin_response_reset; + } + __self.coin_response_timer = 3000; + __self.coin_response_reset = true; + + // clear previous bids + __self.auction_bids = []; + __self.auction_previous_bid = {}; + + // auction open response + __self.irc.emit('message', {message:__self.pre_text + 'Auction opened, accepting bids'}) + } else { + // auction is already open response + __self.irc.emit('message', {message:__self.pre_text + 'Auction already in progress'}); + } + } else { + // raffle currently running + __self.irc.emit('message', {message:__self.pre_text + 'You must close the raffle before you can open an auction'}); + } + } else { + // gambling currently running + __self.irc.emit('message', {message:__self.pre_text + 'Betting must be closed before you can open an auction'}); + } + break; + case false: + if (__self.auction_status) { + // close the auction + __self.auction_status = false; + + // request toggle + __self.coin_toggle = __self.temp.auction_toggle; + delete __self.temp.auction_toggle; + + // default request timer + __self.coin_response_timer = __self.temp.auction_timer; + __self.coin_response_reset = __self.temp.auction_timer_reset; + delete __self.temp.auction_timer; + delete __self.temp.auction_timer_reset; + + // clear response timers + clearTimeout(__self.auction_bid_response); + clearInterval(__self.auction_bid_response_long); + + if (__self.auction_bids.length > 0) { + // pick a winner response + for (var i = 0; i < __self.auction_bids.length; i++) { + if (__self.auction_bids[i].bid === utils.max(__self.auction_bids)) { + __self.irc.emit('message', {message:__self.pre_text + 'Auction closed, Winner: ' + __self.auction_bids[i].viewer + ' @ ' + __self.auction_bids[i].bid}); + + // save the winners info for draw refund + __self.auction_previous_bid.viewer = __self.auction_bids[i].viewer; + __self.auction_previous_bid.bid = __self.auction_bids[i].bid; + + // remove winners money + var sql = ''; + sql += 'UPDATE viewers '; + sql += 'SET points = points - ' + __self.auction_bids[i].bid + ' '; + sql += 'WHERE user = \'' + __self.auction_bids[i].viewer + '\''; + __self.db.execute(sql, function() {}); + + // remove winner from main list + __self.auction_bids.splice(i, 1); + + break; + } + } + } else { + // no bidders to pick from response + __self.irc.emit('message', {message:__self.pre_text + 'Auction closed, no bidders to pick a winner'}); + } + } else { + // auction is already open response + __self.irc.emit('message', {message:__self.pre_text + 'Auction is already closed'}); + } + break; + case 'cancel': + if (__self.auction_status) { + // close the auction + __self.auction_status = false; + + // request toggle + __self.coin_toggle = __self.temp.auction_toggle; + delete __self.temp.auction_toggle; + + // default request timer + __self.coin_response_timer = __self.temp.auction_timer; + __self.coin_response_reset = __self.temp.auction_timer_reset; + delete __self.temp.auction_timer; + delete __self.temp.auction_timer_reset; + + // clear response timers + clearTimeout(__self.auction_bid_response); + clearInterval(__self.auction_bid_response_long); + + // clear previous bids + __self.auction_bids = []; + __self.auction_previous_bid = {}; + + // auction cancelled notification + __self.irc.emit('message', {message:__self.pre_text + 'Auction has been cancelled'}); + } else { + // auction cancelled notification + __self.irc.emit('message', {message:__self.pre_text + 'Auction is not opened'}); + } + break; + } +}; + +Currency.prototype.bid = function (caller, amount) { + var __self = this; + + function find_duplicate(amount) { + var duplicate = false; + for (var i = 0; i < __self.auction_bids.length; i++) { + if (__self.auction_bids[i].bid === amount) { + duplicate = true; + break; + } + } + return duplicate; + } + + if (__self.auction_status) { + // verify that bidder has the coins for bidding + __self.query_coins(caller, function (rows) { + var has_tickets = false; + + // only add bid if they have the enough to pay + if (rows[0].points >= amount) { + if (__self.auction_bids.length > 0) { + // check if an existing bid exists and modify it + for (var i = 0; i < __self.auction_bids.length; i++) { + if (__self.auction_bids[i].viewer === caller) { + has_tickets = true; + // check if bid is higher then original and not a duplicate + if (__self.auction_bids[i].bid < amount && !find_duplicate(amount)) { + __self.auction_bids[i].bid = amount; + } + break; + } + } + + // add new bids to list if they are not a duplicate + if (!has_tickets && !find_duplicate(amount)) { + __self.auction_bids.push({viewer: caller, bid: amount}); + } + } else { + // push first bid + __self.auction_bids.push({viewer: caller, bid: amount}); + } + } + + // clear timers on flood + clearTimeout(__self.auction_bid_response); + clearInterval(__self.auction_bid_response_long); + + // reply after set amount of bids + if ((__self.auction_bids.length % __self.max_requests) === 0) { + // bulk flood response + for (var i = 0; i < __self.auction_bids.length; i++) { + if (__self.auction_bids[i].bid === utils.max(__self.auction_bids)) { + __self.irc.msg(__self.pre_text + 'Highest bid, ' + __self.auction_bids[i].viewer + ' @ ' + __self.auction_bids[i].bid); + } + } + } else { + // response after time without flood has passed + var viewer, bid; + for (var i = 0; i < __self.auction_bids.length; i++) { + if (__self.auction_bids[i].bid === utils.max(__self.auction_bids)) { + viewer = __self.auction_bids[i].viewer; + bid = __self.auction_bids[i].bid; + } + } + if (viewer !== undefined && bid !== undefined && __self.auction_status) { + var msg = __self.pre_text + 'Highest bid, ' + viewer + ' @ ' + bid; + __self.auction_bid_response = setTimeout(function () {__self.irc.emit('message', {message:msg, timer: 1});}, 5000); + __self.auction_bid_response_long = setInterval(function () {__self.irc.emit('message', {message:msg, timer: 1});}, 30000); + } + } + }); + } +}; + +Currency.prototype.next_auction_winner = function () { + var __self = this, empty_list = []; + + // custom dialog when the bidder list is empty + empty_list.push('Hey, I just met you and this is crazy, but there\'s no more bidders, so start an new auction maybe?'); + empty_list.push('Are there more bidders? Well, to tell you the truth, in all this excitement I kind of lost track myself.'); + empty_list.push('Heyyyyyy there\'s no more bidders, Op, op, op, op, Open Auction Style.'); + empty_list.push('Da bids! Da bids! Where are all da bids, boss?'); + + if (!__self.auction_status) { + // get next highest bidder or prompt to open new auction + if (__self.auction_bids.length > 0) { + for (var i = 0; i < __self.auction_bids.length; i++) { + if (__self.auction_bids[i].bid === utils.max(__self.auction_bids)) { + __self.irc.emit('message',{message:__self.pre_text + 'Drawing the next highest bid: ' + __self.auction_bids[i].viewer + ' @ ' + __self.auction_bids[i].bid}); + + // refund previous winner's money + var sql = ''; + sql += 'UPDATE viewers '; + sql += 'SET points = points + ' + __self.auction_previous_bid.bid + ' '; + sql += 'WHERE user = \'' + __self.auction_previous_bid.viewer + '\''; + __self.db.execute(sql, function() {}); + + // save the new winner's info for next draw + __self.auction_previous_bid.viewer = __self.auction_bids[i].viewer; + __self.auction_previous_bid.bid = __self.auction_bids[i].bid; + + // remove winners money + sql = ''; + sql += 'UPDATE viewers '; + sql += 'SET points = points - ' + __self.auction_bids[i].bid + ' '; + sql += 'WHERE user = \'' + __self.auction_bids[i].viewer + '\''; + __self.db.execute(sql, function() {}); + + // remove winner from main list + __self.auction_bids.splice(i, 1); + + break; + } + } + } else { + // check if a previous viewer is saved + if (__self.auction_previous_bid.viewer !== null) { + // refund previous winner's money + var sql = ''; + sql += 'UPDATE viewers '; + sql += 'SET points = points + ' + __self.auction_previous_bid.bid + ' '; + sql += 'WHERE user = \'' + __self.auction_previous_bid.viewer + '\''; + __self.db.execute(sql, function() {}); + + // clear previous bid + __self.auction_previous_bid = {}; + } + + // notify that there's no more bids + __self.irc.emit('message',{message:__self.pre_text + utils.selectRandomArrayItem(empty_list)}); + } + } +}; + +/** + * ============================================ + * RAFFLE SYSTEM + * -------------------------------------------- + */ +Currency.prototype.raffle = function (status) { + var __self = this; + + switch (status) { + case true: + if (!__self.bets_status) { + if (!__self.auction_status) { + if (!__self.raffle_status) { + // open up a raffle + __self.raffle_status = true; + + // request toggle + if (__self.temp.auction_toggle) { + __self.temp.raffle_toggle = __self.temp.auction_toggle; + } else { + __self.temp.raffle_toggle = __self.coin_toggle; + } + __self.coin_toggle = false; + + // default request timer + if (__self.temp.auction_timer && __self.temp.auction_timer_reset) { + __self.temp.raffle_timer = __self.temp.auction_timer; + __self.temp.raffle_timer_reset = __self.temp.auction_timer_reset; + } else { + __self.temp.raffle_timer = __self.coin_response_timer; + __self.temp.raffle_timer_reset = __self.coin_response_reset; + } + __self.coin_response_timer = 3000; + __self.coin_response_reset = true; + + // save previous raffle settings in case + // a new one is opened on accident + __self.raffle_restore_ticket_requests = __self.raffle_ticket_requests; + __self.raffle_restore_tickets = __self.raffle_tickets; + + // clear previous tickets + __self.raffle_ticket_requests = []; + __self.raffle_tickets = []; + + // raffle open response + __self.irc.emit('message',{message:__self.pre_text + 'Raffle opened'}); + __self.irc.emit('message',{message:'+ Tickets cost ' + __self.raffle_ticket_cost + ' ' + __self.config.currency.toLowerCase() + ' / Maximum of ' + __self.raffle_max_tickets + ' tickets per viewer'}); + } else { + // raffle in progress response + __self.irc.emit('message',{message:__self.pre_text + 'Raffle already in progress'}); + } + } else { + // auction in progress + __self.irc.emit('message', {message:__self.pre_text + 'You must close the auction before you can open an a raffle'}); + } + } else { + // gambling currently running + __self.irc.emit('message', {message:__self.pre_text + 'Betting must be closed before you can open a raffle'}); + } + break; + case false: + if (__self.raffle_status) { + // close the raffle + __self.raffle_status = false; + + // request toggle + __self.coin_toggle = __self.temp.raffle_toggle; + delete __self.temp.raffle_toggle; + + // default request timer + __self.coin_response_timer = __self.temp.raffle_timer; + __self.coin_response_reset = __self.temp.raffle_timer_reset; + delete __self.temp.raffle_timer; + delete __self.temp.raffle_timer_reset; + + // validation / winner / deduction + __self.raffle_winner(); + } else { + // raffle is already open response + __self.irc.emit('message',{message:__self.pre_text + 'Raffle is already closed'}); + } + break; + case 'cancel': + if (__self.raffle_status) { + // close the raffle + __self.raffle_status = false; + + // request toggle + __self.coin_toggle = __self.temp.raffle_toggle; + delete __self.temp.raffle_toggle; + + // default request timer + __self.coin_response_timer = __self.temp.raffle_timer; + __self.coin_response_reset = __self.temp.raffle_timer_reset; + delete __self.temp.raffle_timer; + delete __self.temp.raffle_timer_reset; + + // clear previous tickets + __self.raffle_ticket_requests = []; + __self.raffle_tickets = []; + + // raffle cancelled notification + __self.irc.emit('message', {message:__self.pre_text + 'Raffle has been cancelled'}); + } else { + // raffle cancelled notification + __self.irc.emit('message', {message:__self.pre_text + 'Raffle is not opened'}); + } + break; + case 'restore': + if (__self.raffle_status) { + // close raffle + __self.raffle_status = false; + + // restore previous raffle tickets + __self.raffle_ticket_requests = __self.raffle_restore_ticket_requests; + __self.raffle_tickets = __self.raffle_restore_tickets; + + __self.irc.emit('message', {message:__self.pre_text + 'Previous raffle has been restored'}); + } else { + // raffle restore failed notification + __self.irc.emit('message', {message:__self.pre_text + 'Raffle is closed, unable to restore'}); + } + break; + } +}; + +Currency.prototype.collect_tickets = function (caller, amount) { + var __self = this, has_tickets = false; + + if (__self.raffle_ticket_requests.length > 0) { + // check if viewer already has tickets + for (var i = 0; i < __self.raffle_ticket_requests.length; i++) { + if (__self.raffle_ticket_requests[i].viewer === caller) { + has_tickets = true; + if (amount <= __self.raffle_max_tickets && amount >= 1) { + __self.raffle_ticket_requests[i].tickets = amount; + } else if (amount === 0) { + __self.raffle_ticket_requests.splice(i, 1); + } + break; + } + } + + // if viewer doesn't have tickets and meets > 1 < max req add their request + if (!has_tickets && amount <= __self.raffle_max_tickets && amount >= 1 && amount !== 0) { + __self.raffle_ticket_requests.push({viewer: caller, tickets: amount}); + } + } else { + // push first ticket if > 1 < max + if (amount <= __self.raffle_max_tickets && amount >= 1 && amount !== 0) { + __self.raffle_ticket_requests.push({viewer: caller, tickets: amount}); + } + } +}; + +Currency.prototype.raffle_winner = function () { + var __self = this, sql = ''; + + if (__self.raffle_ticket_requests.length > 0) { + // setup sql to grab all viewers that request coins from the database + sql += 'SELECT * FROM viewers WHERE '; + for (var i = 0; i < __self.raffle_ticket_requests.length; i++) { + if (i !== __self.raffle_ticket_requests.length - 1) { + sql += 'user=\'' + __self.raffle_ticket_requests[i].viewer.toLowerCase() + '\' OR '; + } else { + sql += 'user=\'' + __self.raffle_ticket_requests[i].viewer.toLowerCase() + '\''; + } + } + + // execute viewer search query + __self.db.execute(sql, function(rows) { + // currency validation + // - this takes the results of the query and uses the names from the database + // to filter through the viewers that requested tickets (since they have to be in the + // database in the first place) + // - during the filtering process the viewers requested tickets are multiplied by the + // ticket cost and compared against their currency amount + // - if the viewer has the funds, their tickets are added and the sql is updated to include their + // deduction + sql = ''; + for (var i = 0; i < rows.length; i++) { + for (var j = 0; j < __self.raffle_ticket_requests.length; j++) { + if (__self.raffle_ticket_requests[j].viewer.toLowerCase() === rows[i].user) { + var money = __self.raffle_ticket_requests[j].tickets * __self.raffle_ticket_cost; + + if (rows[i].points >= money) { + for (var k = 1; k <= __self.raffle_ticket_requests[j].tickets; k++) { + __self.raffle_tickets.push(__self.raffle_ticket_requests[j].viewer); + } + if (i !== rows.length - 1) { + sql += 'UPDATE viewers '; + sql += 'SET points = points - ' + money + ' '; + sql += 'WHERE user = \'' + rows[i].user + '\'; '; + } else { + sql += 'UPDATE viewers '; + sql += 'SET points = points - ' + money + ' '; + sql += 'WHERE user = \'' + rows[i].user + '\''; + } + } + break; + } + } + } + + // randomize array before selecting a random winner + __self.raffle_tickets.sort(function () {return 0.5 - Math.random();}); + + // select random ticket from array + var winner = utils.selectRandomArrayItem(__self.raffle_tickets); + + // count winner's tickets + var winning_ticket_amount; + for (var i = 0; i < __self.raffle_ticket_requests.length; i++) { + if (__self.raffle_ticket_requests[i].viewer === winner) { + winning_ticket_amount = __self.raffle_ticket_requests[i].tickets; + break; + } + } + + // output winner to chat + __self.irc.emit('message', {message:__self.pre_text + 'Raffle closed, ' + __self.raffle_tickets.length + ' tickets purchased!'}); + __self.irc.emit('message', {message:'+ Winner: ' + winner + ' (' + winning_ticket_amount + ' tickets purchased)'}); + + // remove one ticket from raffle bowl + if (__self.raffle_tickets.indexOf(winner) >= 0 ) { + __self.raffle_tickets.splice(__self.raffle_tickets.indexOf(winner), 1); + } + + // execute query + __self.db.execute(sql, function () {}); + }); + } else { + // no tickets to pick from response + __self.irc.emit('message', {message:__self.pre_text + 'Raffle closed, no tickets to draw a winner'}); + } +}; + +Currency.prototype.next_raffle_winner = function () { + var __self = this, empty_list = []; + + // custom dialog when there are no more raffle tickets + empty_list.push('Hey, I just met you and this is crazy, but there\'s no more tickets, so start an new raffle maybe?'); + empty_list.push('Are there more tickets? Well, to tell you the truth, in all this excitement I kind of lost track myself.'); + empty_list.push('Heyyyyyy there\'s no more tickets, Op, op, op, op, Open Raffle Style.'); + empty_list.push('Da tickets! Da tickets! Where are all da tickets, boss?'); + + if (!__self.raffle_status) { + // draw next ticket or prompt to open new raffle + if (__self.raffle_tickets.length > 0) { + // randomize array before selecting a random winner + __self.raffle_tickets.sort(function () {return 0.5 - Math.random();}); + + // select random ticket from array + var winner = utils.selectRandomArrayItem(__self.raffle_tickets); + + // count next winner's tickets + var winning_ticket_amount; + for (var i = 0; i < __self.raffle_ticket_requests.length; i++) { + if (__self.raffle_ticket_requests[i].viewer === winner) { + winning_ticket_amount = __self.raffle_ticket_requests[i].tickets; + break; + } + } + + // output winner to chat + __self.irc.emit('message', {message:__self.pre_text + 'Drawing next ticket'}); + __self.irc.emit('message', {message:'+ Winner: ' + winner + ' (' + winning_ticket_amount + ' tickets purchased)'}); + + // remove one ticket from raffle bowl + if (__self.raffle_tickets.indexOf(winner) >= 0 ) { + __self.raffle_tickets.splice(__self.raffle_tickets.indexOf(winner), 1); + } + } else { + __self.irc.emit('message', {message:__self.pre_text + utils.selectRandomArrayItem(empty_list)}); + } + } +}; + +/** + * ============================================ + * BETTING SYSTEM + * -------------------------------------------- + */ +Currency.prototype.bets = function(status, data) { + var __self = this; + + switch(status){ + case true: + if (!__self.auction_status) { + if (!__self.raffle_status) { + if (!__self.bets_status && !__self.bets_payout) { + var wager_msg = ''; + + // open up bets + __self.bets_status = true; + __self.bets_payout = true; + + // clear previous board / bets + __self.bets_board = []; + __self.bets_viewers = []; + + // create new betting board + __self.bets_board = data.join().split(',').filter(function(n){return n}).slice(6); + + // create chat message on how to place a bet + for (var i = 0; i < __self.bets_board.length; i++) { + if (i !== __self.bets_board.length - 1) { + wager_msg += '"!' + __self.bets_board[i] + '" / '; + } else { + wager_msg += '"!' + __self.bets_board[i] + '"'; + } + } + + // output to chat + __self.irc.emit('message', {message:__self.pre_text + 'Betting is now open'}); + __self.irc.emit('message', {message:'+ Type ' + wager_msg + ' and the bet amount to enter'}); + } else { + if (__self.bets_payout) { + // payout pending message + __self.irc.emit('message', {message:__self.pre_text + 'Unable to take new bets until previous have been paid out'}); + } else { + // gambling is already open response + __self.irc.emit('message', {message:__self.pre_text + 'Betting already in progress'}); + } + } + } else { + // raffle in progress + __self.irc.emit('message', {message:__self.pre_text + 'Betting must be closed before you can open a raffle'}); + } + } else { + // auction currently running + __self.irc.emit('message', {message:__self.pre_text + 'Betting must be closed before you can open an auction'}); + } + break; + case false: + if (__self.bets_status && __self.bets_payout) { + // close out bets + __self.bets_status = false; + + // output to chat + if (__self.bets_viewers.length > 0) { + __self.irc.emit('message', {message:__self.pre_text + 'Betting is now closed'}); + } else { + __self.irc.emit('message', {message:__self.pre_text + 'Betting closed, no bets were placed'}); + } + + // deduct bets from viewers amounts + __self.bets_deduct_bets(); + } + break; + case 'winner': + // set payout to complete + __self.bets_payout = false; + break; + } +}; + +Currency.prototype.collect_bets = function (caller, bet, amount) { + var __self = this, has_bet = false; + + if (__self.bets_viewers.length > 0) { + for (var i = 0; i < __self.bets_viewers.length; i++) { + if (__self.bets_viewers[i].viewer === caller) { + has_bet = true; + if (amount >= 1) { + __self.bets_viewers[i].bet = bet; + __self.bets_viewers[i].amount = amount; + } else if (amount === 0 && bet === __self.bets_viewers[i].bet) { + __self.bets_viewers.splice(i, 1); + } + break; + } + } + if (!has_bet && amount >= 1 && amount !== 0) { + __self.bets_viewers.push({viewer: caller, bet: bet, amount: amount}); + } + } else { + if (amount >= 1 && amount !== 0) { + __self.bets_viewers.push({viewer: caller, bet: bet, amount: amount}); + } + } + console.log(__self.bets_viewers); +}; + +Currency.prototype.bets_deduct_bets = function () { + +}; + +module.exports = function (irc, db, options) { + return new Currency(irc, db, options); +}; \ No newline at end of file diff --git a/lib/dashboard/404.html b/lib/dashboard/404.html new file mode 100644 index 0000000..a75246c --- /dev/null +++ b/lib/dashboard/404.html @@ -0,0 +1,21 @@ + + + + ArchonBot 404 + + + + +
404
Whatever it is you are looking for, ArchonBot has not created.
+ + \ No newline at end of file diff --git a/lib/dashboard/css/dashboard.css b/lib/dashboard/css/dashboard.css new file mode 100644 index 0000000..79aaa14 --- /dev/null +++ b/lib/dashboard/css/dashboard.css @@ -0,0 +1,191 @@ +@font-face{font-family:"Korolev-Bold"; + src:url("/webfonts/22AE77_0_0.eot"); + src:url("/webfonts/22AE77_0_0.eot?#iefix") format("embedded-opentype"), url("/webfonts/22AE77_0_0.woff") format("woff"), url("/webfonts/22AE77_0_0.ttf") format("truetype");} +*{margin:0; + padding:0;} +html{overflow-y:scroll;} +body{color:#000; + font:12px "Helvetica Neue", Helvetica, Arial, sans-serif; + line-height:1.5; + text-rendering:optimizeLegibility; + background:#262626; + background-image:url('/images/background.jpg');} +a{text-decoration:none;} +a:hover{text-decoration:underline;} +img{border:0; + vertical-align:top;} +li{list-style-position:inside;} +h4{font-size:16px; + line-height:1.25; + font-weight:bold;} +h5{font-size:14px; + line-height:1.286; + font-weight:bold;} +button:focus{outline:none;} +input[type="submit"]:focus, input[type="button"]:focus, input[type="cancel"]:focus, input[type="reset"]:focus{outline:none;} +.bar{height:35px; + line-height:35px; + padding:0 10px 0 40px; + color:#666; + font-size:13px; + text-shadow:1px 1px 0 rgba(255, 255, 255, .8); + box-shadow:inset 0 -1px 0 rgba(0, 0, 0, .1); + background:url('/images/icons.png') no-repeat -4px -408px #e9e9e9;} +.bar h4{float:left; + width:130px; + margin:0; + padding:0; + font-size:13px; + line-height:35px; + text-overflow:ellipsis; + overflow:hidden;} +.bar span{float:right; + font-style:italic; + opacity:.7;} +.clear{clear:both;} +.nick{text-transform:capitalize;} +.main, .footer{width:980px;} +.main{margin:40px auto 20px; + padding:20px 0; + border:1px solid #fff; + border-bottom:1px solid #fff; + background:transparent url(/images/transparent_pxl_white-90.png); + background:rgba(255, 255, 255, 0.9); + box-shadow:0 0 5px rgba(0, 0, 0, 0.35); + -moz-box-shadow:0 0 5px rgba(0, 0, 0, 0.35); + -webkit-box-shadow:0 0 5px rgba(0, 0, 0, 0.35);} +.wrapper{width:940px; + margin:0 auto; + padding:0 20px; + position:relative;} +.primary_button, .normal_button{border-radius:3px; + -moz-border-radius:3px; + -webkit-border-radius:3px; + display:inline-block; + position:relative; + vertical-align:baseline; + font-size:inherit; + font-weight:500; + border:1px solid #000; + background:none; + cursor:pointer; + font-family:inherit; + font-weight:bold; + width:1; + overflow:visible;} +.primary_button span, .normal_button span{display:inline-block; + height:18px; + padding:5px 9px; + white-space:nowrap; + line-height:18px;} +.primary_button{background:-webkit-gradient(linear, left top, left bottom, from(#8266b6), to(#533787)); + background:-moz-linear-gradient(top, #8266b6, #533787); + background:-o-linear-gradient(top, #8266b6, #533787); + background:linear-gradient(top, #8266b6, #533787); + background-color:#6a4e9e; + border-top:1px solid #5b3f8f; + border-left:1px solid #412771; + border-right:1px solid #412771; + border-bottom:1px solid #2a1453; + box-shadow:inset 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 0 rgba(0, 0, 0, 0.15); + -moz-box-shadow:inset 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 0 rgba(0, 0, 0, 0.15); + -webkit-box-shadow:inset 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 0 rgba(0, 0, 0, 0.15); + text-shadow:0 1px 0 rgba(0, 0, 0, 0.75); + -moz-text-shadow:0 1px 0 rgba(0, 0, 0, 0.75); + -webkit-text-shadow:0 1px 0 rgba(0, 0, 0, 0.75); + color:#fff;} +.normal_button{background:-webkit-gradient(linear, left top, left bottom, from(#f5f5f5), to(#ddd)); + background:-moz-linear-gradient(top, #f5f5f5, #ddd); + background:-o-linear-gradient(top, #f5f5f5, #ddd); + background:linear-gradient(top, #f5f5f5, #ddd); + background-color:#e9e9e9; + border-top:1px solid rgba(0, 0, 0, 0.2); + border-left:1px solid rgba(0, 0, 0, 0.3); + border-right:1px solid rgba(0, 0, 0, 0.3); + border-bottom:1px solid rgba(0, 0, 0, 0.35); + box-shadow:inset 0 1px 0 #fff, 0 1px 0 rgba(0, 0, 0, 0.07); + -moz-box-shadow:inset 0 1px 0 #fff, 0 1px 0 rgba(0, 0, 0, 0.07); + -webkit-box-shadow:inset 0 1px 0 #fff, 0 1px 0 rgba(0, 0, 0, 0.07); + text-shadow:0 1px 0 rgba(255, 255, 255, 0.75); + -moz-text-shadow:0 1px 0 rgba(255, 255, 255, 0.75); + -webkit-text-shadow:0 1px 0 rgba(255, 255, 255, 0.75);} +a.button:hover, a.primary_button:hover, a.normal_button:hover{text-decoration:none;} +.form_submit{float:right; + margin-top:0; + margin-right:5px} +.auction_status{display:block; + float:right; + margin:-8px -8px 5px 0; + padding:0; + width:55px; + height:50px; + font-size:12px; + line-height:40px; + color:#E3DAF2; + background:url('/images/vector-web-icons.png') no-repeat 0 0 transparent; + border:0; + opacity:.2; + text-indent:100%; + overflow:hidden; + background-position:-160px -268px;} +.section_header{margin-bottom:10px; + color:#666;} +.bottom_box{border:1px solid #ccc; + border-top:none; + border-radius:0; + -moz-border-radius:0; + -webkit-border-radius:0; + background:#f1f1f1;} +#mantle_skin{z-index:-1000;} +#site_footer{margin:0 auto; + padding-bottom:60px;} +#site_footer a{color:#fff; + font-weight:500; + text-shadow:0 1px 0 #000;} +#footer_archonbot{float:left; + vertical-align:top;} +#footer_links{float:left;} +#footer_links li{display:inline; + display:inline-block; + margin:0 6px;} +#copyright_and_languages{float:right; + color:#666;} +#copyright_and_languages #copyright{margin-left:10px;} +#chat_lines{height:346px; + padding:10px 5px 10px 10px} +#twitch_chat{margin-bottom:10px; + border:1px solid #ccc; + border-radius:4px;} +#chat_line_list li{list-style:none;} +#chat_line_list li p{margin-bottom:5px;} +#chat_line_list li .nick{font-weight:bold; + text-transform:capitalize;} +#auction_submit_buttons{margin:0 -5px 0 0; + height:25px} +#auction_lines{height:173px; + margin-bottom:10px; + border:1px solid #ccc; + border-radius:4px;} +#irc_submit{float:right; + margin-top:-37px;} +#auction_box{width:325px; + margin-bottom:10px;} +#auction_box, #chat{position:relative; + padding:15px; + border:1px solid #CCC; + border-radius:0; + -moz-border-radius:0; + -webkit-border-radius:0; + border-top-right-radius:5px; + border-top-left-radius:5px; + -moz-border-radius-topright:5px; + -moz-border-radius-topleft:5px; + -webkit-border-top-left-radius:5px; + -webkit-border-top-right-radius:5px; + border-bottom-right-radius:5px; + border-bottom-left-radius:5px; + -moz-border-radius-bottomright:5px; + -moz-border-radius-bottomleft:5px; + -webkit-border-bottom-left-radius:5px; + -webkit-border-bottom-right-radius:5px; + background:#F1F1F1;} \ No newline at end of file diff --git a/lib/dashboard/css/jquery.mCustomScrollbar.css b/lib/dashboard/css/jquery.mCustomScrollbar.css new file mode 100644 index 0000000..5bb53e6 --- /dev/null +++ b/lib/dashboard/css/jquery.mCustomScrollbar.css @@ -0,0 +1,208 @@ +/* basic scrollbar styling */ +/* vertical scrollbar */ +.mCSB_container{ + width:auto; + margin-right:30px; + overflow:hidden; +} +.mCSB_container.mCS_no_scrollbar{ + margin-right:0; +} +.mCS_disabled .mCSB_container.mCS_no_scrollbar, +.mCS_destroyed .mCSB_container.mCS_no_scrollbar{ + margin-right:30px; +} +.mCustomScrollBox .mCSB_scrollTools{ + width:16px; + height:100%; + top:0; + right:-5px; +} +.mCSB_scrollTools .mCSB_draggerContainer{ + height:100%; + -webkit-box-sizing:border-box; + -moz-box-sizing:border-box; + box-sizing:border-box; +} +.mCSB_scrollTools .mCSB_buttonUp+.mCSB_draggerContainer{ + padding-bottom:40px; +} +.mCSB_scrollTools .mCSB_draggerRail{ + width:5px; + height:100%; + margin:0 auto; + -webkit-border-radius:10px; + -moz-border-radius:10px; + border-radius:10px; +} +.mCSB_scrollTools .mCSB_dragger{ + cursor:pointer; + width:100%; + height:30px; +} +.mCSB_scrollTools .mCSB_dragger .mCSB_dragger_bar{ + width:5px; + height:100%; + margin:0 auto; + -webkit-border-radius:10px; + -moz-border-radius:10px; + border-radius:10px; + text-align:center; +} +.mCSB_scrollTools .mCSB_buttonUp, +.mCSB_scrollTools .mCSB_buttonDown{ + height:20px; + overflow:hidden; + margin:0 auto; + cursor:pointer; +} +.mCSB_scrollTools .mCSB_buttonDown{ + bottom:0; + margin-top:-40px; +} +/* horizontal scrollbar */ +.mCSB_horizontal .mCSB_container{ + height:auto; + margin-right:0; + margin-bottom:30px; + overflow:hidden; +} +.mCSB_horizontal .mCSB_container.mCS_no_scrollbar{ + margin-bottom:0; +} +.mCS_disabled .mCSB_horizontal .mCSB_container.mCS_no_scrollbar, +.mCS_destroyed .mCSB_horizontal .mCSB_container.mCS_no_scrollbar{ + margin-right:0; + margin-bottom:30px; +} +.mCSB_horizontal.mCustomScrollBox .mCSB_scrollTools{ + width:100%; + height:16px; + top:auto; + right:auto; + bottom:0; + left:0; + overflow:hidden; +} +.mCSB_horizontal .mCSB_scrollTools .mCSB_draggerContainer{ + height:100%; + width:auto; + -webkit-box-sizing:border-box; + -moz-box-sizing:border-box; + box-sizing:border-box; + overflow:hidden; +} +.mCSB_horizontal .mCSB_scrollTools .mCSB_buttonLeft+.mCSB_draggerContainer{ + padding-bottom:0; + padding-right:20px; +} +.mCSB_horizontal .mCSB_scrollTools .mCSB_draggerRail{ + width:100%; + height:2px; + margin:7px 0; + -webkit-border-radius:10px; + -moz-border-radius:10px; + border-radius:10px; +} +.mCSB_horizontal .mCSB_scrollTools .mCSB_dragger{ + width:30px; + height:100%; +} +.mCSB_horizontal .mCSB_scrollTools .mCSB_dragger .mCSB_dragger_bar{ + width:100%; + height:4px; + margin:6px auto; + -webkit-border-radius:10px; + -moz-border-radius:10px; + border-radius:10px; +} +.mCSB_horizontal .mCSB_scrollTools .mCSB_buttonLeft, +.mCSB_horizontal .mCSB_scrollTools .mCSB_buttonRight{ + width:20px; + height:100%; + overflow:hidden; + margin:0 auto; + cursor:pointer; + float:left; +} +.mCSB_horizontal .mCSB_scrollTools .mCSB_buttonRight{ + right:0; + bottom:auto; + margin-left:-40px; + margin-top:-16px; + float:right; +} + +/* default scrollbar colors and backgrounds */ +.mCustomScrollBox .mCSB_scrollTools{ + opacity:0.75; +} +.mCustomScrollBox:hover .mCSB_scrollTools{ + opacity:1; +} +.mCSB_scrollTools .mCSB_draggerRail{ + background:#000; /* rgba fallback */ + background:rgba(0,0,0,0.4); + filter:"alpha(opacity=40)"; -ms-filter:"alpha(opacity=40)"; /* old ie */ +} +.mCSB_scrollTools .mCSB_dragger .mCSB_dragger_bar{ + background:#fff; /* rgba fallback */ + background:rgba(42,255,0,0.75); + filter:"alpha(opacity=75)"; -ms-filter:"alpha(opacity=75)"; /* old ie */ +} +.mCSB_scrollTools .mCSB_dragger:hover .mCSB_dragger_bar{ + background:rgba(42,255,0,0.85); + filter:"alpha(opacity=85)"; -ms-filter:"alpha(opacity=85)"; /* old ie */ +} +.mCSB_scrollTools .mCSB_dragger:active .mCSB_dragger_bar, +.mCSB_scrollTools .mCSB_dragger.mCSB_dragger_onDrag .mCSB_dragger_bar{ + background:rgba(42,255,0,0.9); + filter:"alpha(opacity=90)"; -ms-filter:"alpha(opacity=90)"; /* old ie */ +} +.mCSB_scrollTools .mCSB_buttonUp, +.mCSB_scrollTools .mCSB_buttonDown, +.mCSB_scrollTools .mCSB_buttonLeft, +.mCSB_scrollTools .mCSB_buttonRight{ + background-image:url(mCSB_buttons.png); + background-repeat:no-repeat; + opacity:0.4; + filter:"alpha(opacity=40)"; -ms-filter:"alpha(opacity=40)"; /* old ie */ +} +.mCSB_scrollTools .mCSB_buttonUp{ + background-position:0 0; + /* + sprites locations are 0 0/-16px 0/-32px 0/-48px 0 (light) and -80px 0/-96px 0/-112px 0/-128px 0 (dark) + */ +} +.mCSB_scrollTools .mCSB_buttonDown{ + background-position:0 -20px; + /* + sprites locations are 0 -20px/-16px -20px/-32px -20px/-48px -20px (light) and -80px -20px/-96px -20px/-112px -20px/-128px -20px (dark) + */ +} +.mCSB_scrollTools .mCSB_buttonLeft{ + background-position:0 -40px; + /* + sprites locations are 0 -40px/-20px -40px/-40px -40px/-60px -40px (light) and -80px -40px/-100px -40px/-120px -40px/-140px -40px (dark) + */ +} +.mCSB_scrollTools .mCSB_buttonRight{ + background-position:0 -56px; + /* + sprites locations are 0 -56px/-20px -56px/-40px -56px/-60px -56px (light) and -80px -56px/-100px -56px/-120px -56px/-140px -56px (dark) + */ +} +.mCSB_scrollTools .mCSB_buttonUp:hover, +.mCSB_scrollTools .mCSB_buttonDown:hover, +.mCSB_scrollTools .mCSB_buttonLeft:hover, +.mCSB_scrollTools .mCSB_buttonRight:hover{ + opacity:0.75; + filter:"alpha(opacity=75)"; -ms-filter:"alpha(opacity=75)"; /* old ie */ +} +.mCSB_scrollTools .mCSB_buttonUp:active, +.mCSB_scrollTools .mCSB_buttonDown:active, +.mCSB_scrollTools .mCSB_buttonLeft:active, +.mCSB_scrollTools .mCSB_buttonRight:active{ + opacity:0.9; + filter:"alpha(opacity=90)"; -ms-filter:"alpha(opacity=90)"; /* old ie */ +} \ No newline at end of file diff --git a/lib/dashboard/dashboard.html b/lib/dashboard/dashboard.html new file mode 100644 index 0000000..e00fac4 --- /dev/null +++ b/lib/dashboard/dashboard.html @@ -0,0 +1,85 @@ + + + +#{{channelname}} Dashboard - {{botname}} + + + + + + + + + + +
+
+
+
+
Close
+

Arcoins Auction

+
Status: Closed
+
+
+

Messages

+ updating in real time +
+
    +
    +
    +
    +
    +
    + + +
    +
    + +
    +
    +
    +
    + + +
    +
    + +
    +
    +
    +
    +
    +

    Twitch IRC Raw Chat

    +
    +
    + +
    +
    + +
    +
    +
    +
    +

    Messages

    + updating in real time +
    +
    +
      +
      +
      +
      +
      +
      +
      +
      + + + \ No newline at end of file diff --git a/lib/dashboard/dashboard.js b/lib/dashboard/dashboard.js new file mode 100644 index 0000000..79680ed --- /dev/null +++ b/lib/dashboard/dashboard.js @@ -0,0 +1,175 @@ +var file = require('fs'), + http = require('http'), + querystring = require('querystring'), + mustache = require('mustache'); + +function Server(irc, options) { + var __self = this; + __self.options = options || {}; + + __self.irc = irc; + + //config + __self.root = __self.options.root || ''; + __self.channel = __self.options.channel.toLowerCase() || ''; + __self.bot = __self.options.bot_name || ''; + __self.port = __self.options.dashboard_id || ''; +} + +Server.prototype.start = function () { + var __self = this; + + http.createServer(function (request, response) { + var handle, patterns, rest_uri, match, mimes; + + mimes = [ + {css: 'text/css', encoding: 'utf8'}, + {js: 'application/javascript', encoding: 'utf8'}, + {html: 'text/html', encoding: 'utf8'}, + {jpg: 'image/jpeg', encoding: 'binary'}, + {png: 'image/png', encoding: 'binary'}, + {gif: 'image/gif', encoding: 'binary'}, + {eot: 'application/vnd.ms-fontobject', encoding: 'binary'}, + {otf: 'application/octet-stream', encoding: 'binary'}, + {ttf: 'application/octet-stream', encoding: 'binary'}, + {woff: 'application/x-font-woff', encoding: 'binary'} + ]; + + // remove start and end slashes + rest_uri = request.url.replace(/^\/|\/$/i, ''); + + // reg patterns + patterns = { + realtimechat: /get\/chat$/i, + dashboard: /v1\/dashboard$/i, + actions: /actions$/i, + all: /(.*\/)(.*)\.(\w+)$|(.*)\.(\w+)$/i + }; + + // match with patterns + for (var key in patterns) { + if (patterns[key].test(rest_uri)) { + match = rest_uri.match(patterns[key]); + handle = key; + break; + } + } + + // organize requests + if (handle == 'realtimechat') { + response.writeHead(200, {'Content-Type': 'application/json'}); + file.exists(__self.log, function (exists) { + if (exists) { + file.readFile(__self.log, 'utf8', function (err, data) { + if (data !== null) { + var json = []; + data = data.split('\r\n'); + for (var i = 0; i < data.length - 1; i++) { + json[i] = {id: i, text: data[i]}; + } + response.end(JSON.stringify(json)); + } + // TODO: fix up the json request so it takes less memory + /*if (data !== null) { + var json = [], max_length; + data = data.split('\r\n'); + max_length = data.length > 200 ? 200 : data.length; + for (var i = 0; i < max_length; i++) { + var check_length = data.length > 200 ? (data.length - 200) + i : i; + json[i] = {id: check_length, text: data[check_length]}; + } + response.end(JSON.stringify(json)); + }*/ + }); + } else { + response.end('{"Error":"Chat does not exist"}'); + } + }); + } + + if (handle == 'dashboard') { + file.readFile(__self.root + '/dashboard.html', 'utf8', function (err, data) { + var template = {botname: __self.bot, channelname: __self.channel, user: __self.channel.charAt(0).toUpperCase() + __self.channel.slice(0).toLowerCase()}; + response.writeHead(200, {'Content-Type': 'text/html'}); + response.end(mustache.render(data, template)); + }); + } + + if (handle == 'actions') { + if (request.method == 'POST') { + var post_header = ''; + + request.on('data', function (chunk) { + post_header += chunk.toString(); + }); + + request.on('end', function () { + var decoded_header = querystring.parse(post_header); + + response.writeHead(200, {'Content-Type': 'text/html'}); + + switch(decoded_header._method) { + case 'reconnect': + __self.irc.reconnect(); + response.end(); + break; + case 'auction_open': + __self.irc.msg('!arcoins auction open'); + response.end(); + break; + case 'auction_close': + __self.irc.msg('!arcoins auction close'); + response.end(); + break; + default: + response.end(); + } + }); + } + } + + if (handle == 'all') { + file.exists(__self.root + '/' + match[0], function (exists) { + if (exists) { + var mime, encoding; + for (var i = 0; i < mimes.length; i++) { + for (var key in mimes[i]) { + if (match[3] == key || match[5] == key) { + mime = mimes[i][key]; + encoding = mimes[i].encoding; + break; + } + } + } + file.readFile(__self.root + '/' + match[0], encoding, function (err, data) { + response.writeHead(200, {'Content-Type': mime}); + if (encoding == 'binary') { + response.end(data, 'binary'); + } else { + response.end(data); + } + }); + } else { + fourohfour(); + } + }); + } + + if (handle === undefined) { + fourohfour(); + } + + function fourohfour() { + file.readFile(__self.root + '/404.html', 'utf8', function (err, data) { + response.writeHead(404, {'Content-Type': 'text/html'}); + response.end(data); + }); + } + + }).listen(__self.port); + console.log('> Dashboard for ' + __self.bot + ' running on http://localhost:' + __self.port + '/v1/dashboard'); +}; + +module.exports = function (irc, options) { + return new Server(irc, options); +}; \ No newline at end of file diff --git a/lib/dashboard/images/archonbot.png b/lib/dashboard/images/archonbot.png new file mode 100644 index 0000000..00eb598 Binary files /dev/null and b/lib/dashboard/images/archonbot.png differ diff --git a/lib/dashboard/images/background.jpg b/lib/dashboard/images/background.jpg new file mode 100644 index 0000000..736c023 Binary files /dev/null and b/lib/dashboard/images/background.jpg differ diff --git a/lib/dashboard/images/icons.png b/lib/dashboard/images/icons.png new file mode 100644 index 0000000..fa78eb1 Binary files /dev/null and b/lib/dashboard/images/icons.png differ diff --git a/lib/dashboard/images/transparent_pxl_white-90.png b/lib/dashboard/images/transparent_pxl_white-90.png new file mode 100644 index 0000000..e69de29 diff --git a/lib/dashboard/images/vector-web-icons.png b/lib/dashboard/images/vector-web-icons.png new file mode 100644 index 0000000..f9808ad Binary files /dev/null and b/lib/dashboard/images/vector-web-icons.png differ diff --git a/lib/dashboard/js/actions.js b/lib/dashboard/js/actions.js new file mode 100644 index 0000000..59e6486 --- /dev/null +++ b/lib/dashboard/js/actions.js @@ -0,0 +1,167 @@ +//-------- Realtime Chat -------- +(function ($) { + var previous_chat = [], origin = window.location.origin; + + function get_chat() { + $.ajax({ + url: origin + '/get/chat', + success: function (data) { + for (var key in data) { + if (data.hasOwnProperty(key)) { + if (data[key].id !== undefined || data[key].text !== undefined) { + if (previous_chat.indexOf(data[key].id) < 0) { + // add line id to array + previous_chat.push(data[key].id); + + // separate words + var data_split = data[key].text.split(' '); + + //-------- Raw IRC Module -------- + update_raw_irc_module(data_split); + update_scrollbar(); + + //-------- Auction Module -------- + update_auction_module(data_split); + } + } + } + } + get_chat(); + }, + error: function (x, t) { + if (t === 'timeout') { + $('#chat_line_list').append('
    • Server: Request timed out, trying to reconnect (server may be offline)

    • '); + update_scrollbar(); + } + get_chat(); + }, dataType: "json", timeout: 30000 + }); + } + // start "realtime" chat + get_chat(); + + function update_scrollbar () { + // update scrollbar + $('.chat-scroll').mCustomScrollbar("update"); + $('.chat-scroll').mCustomScrollbar("scrollTo","#chat_line_end"); + } + + function update_raw_irc_module (data) { + // raw irc status colors + switch(data[0]) { + case 'SENT': + data.splice(0, 1, '' + data[0] + ' '); + break; + case 'RECV': + data.splice(0, 1, '' + data[0] + ' '); + break; + } + + // raw irc message / server response colors + if (data[3] === 'PRIVMSG') { + var user = data[2].split('!'); + if (user.length > 1) { + data.splice(2, 1, '' + user[0].slice(1).charAt(0).toUpperCase() + user[0].slice(2).toLowerCase() + ''); + } else { + data.splice(2, 1, '' + user + ''); + } + data.splice(3, 1, '' + data[3] + ''); + data.splice(4, 1, '' + data[4] + ''); + data.splice(5, 1, data[5].slice(1)); + } else { + for (var i = 2; i < data.length; i++) { + if (i === 2) { + data.splice(i, 1, '' + data[i]); + } else if (i === data.length - 1) { + data.splice(i, 1, data[i] + ''); + } + } + } + + // add chat line to view + if (data[3] !== '352') { + $('#chat_line_list').append('
    • ' + data.join(' ') + '

    • '); + } + + // remove first chat line if over limit + if (previous_chat.length > 200 && $('#chat_line_list li').length > 200) { + $('#chat_line_list li').first().remove(); + previous_chat = previous_chat.slice(1); + } + } + + function update_auction_module (data) { + var data_text = ''; + + for (var i = 6; i < data.length; i++) { + data_text += i !== data.length - 1 ? data[i] + ' ' : data[i]; + } + + // set auction info + if (data_text === 'Auction opened, accepting bids') { + $('.auction_status').css({'opacity':'1', 'background-position': '-230px -268px'}).text('Opened'); + $('#auction_highest_bid').html('').text('Status: ').append('Checking Bids...'); + } + if (data[6] + ' ' + data[7] === 'Highest bid,') { + $('#auction_highest_bid').html('').text('Highest Bid: ').append('' + data[8] + ' @ ' + data[10] + ''); + } + if (data[6] + ' ' + data[7] + ' ' + data[8] == 'Auction closed, Winner:') { + $('.auction_status').css({'opacity': '.2', 'background-position': '-160px -268px'}).text('Closed'); + $('#auction_highest_bid').html('').text('Winner: ').append('' + data[9] + ' @ ' + data[11] + ''); + } + if (data_text === 'Auction closed, no bidders to pick a winner') { + $('.auction_status').css({'opacity': '.2', 'background-position': '-160px -268px'}).text('Closed'); + $('#auction_highest_bid').html('').text('Status: ').append('Closed'); + } + } +})(jQuery); + +//-------- Scrollbars -------- +(function ($) { + $(document).ready(function () { + $('.auction-scroll').mCustomScrollbar({ + scrollInertia:0, + advanced:{ + updateOnContentResize: true + } + }); + $('.chat-scroll').mCustomScrollbar({ + scrollInertia:0, + advanced:{ + updateOnContentResize: true + } + }); + }); +})(jQuery); + +//-------- Form Actions -------- +(function ($) { + $(document).ready(function () { + // reconnect to irc + $('#irc_reconnect').submit(function (e) { + e.preventDefault(); + $.ajax({ + type: 'POST', + url: window.location.origin + '/actions', + data: {_method: 'reconnect', auth_token: $(this).find('input[name="authenticity_token"]').val()} + }); + }); + // open / close auction + $('#auction_open').submit(function (e) { + e.preventDefault(); + $.ajax({ + type: 'POST', + url: window.location.origin + '/actions', + data: {_method: 'auction_open', auth_token: $(this).find('input[name="authenticity_token"]').val(), user: $(this).find('input[name="user"]').val()} + }); + }); + $('#auction_close').submit(function (e) { + e.preventDefault(); + $.ajax({ + type: 'POST', + url: window.location.origin + '/actions', + data: {_method: 'auction_close', auth_token: $(this).find('input[name="authenticity_token"]').val(), user: $(this).find('input[name="user"]').val()} + }); + }); + }); +})(jQuery); \ No newline at end of file diff --git a/lib/dashboard/js/jquery.mCustomScrollbar.js b/lib/dashboard/js/jquery.mCustomScrollbar.js new file mode 100644 index 0000000..fd3471a --- /dev/null +++ b/lib/dashboard/js/jquery.mCustomScrollbar.js @@ -0,0 +1,874 @@ +/* +== malihu jquery custom scrollbars plugin == +version: 2.3.2 +author: malihu (http://manos.malihu.gr) +plugin home: http://manos.malihu.gr/jquery-custom-content-scroller +*/ +(function($){ + var methods={ + init:function(options){ + var defaults={ + set_width:false, /*optional element width: boolean, pixels, percentage*/ + set_height:false, /*optional element height: boolean, pixels, percentage*/ + horizontalScroll:false, /*scroll horizontally: boolean*/ + scrollInertia:550, /*scrolling inertia: integer (milliseconds)*/ + scrollEasing:"easeOutCirc", /*scrolling easing: string*/ + mouseWheel:"pixels", /*mousewheel support and velocity: boolean, "auto", integer, "pixels"*/ + mouseWheelPixels:60, /*mousewheel pixels amount: integer*/ + autoDraggerLength:true, /*auto-adjust scrollbar dragger length: boolean*/ + scrollButtons:{ /*scroll buttons*/ + enable:false, /*scroll buttons support: boolean*/ + scrollType:"continuous", /*scroll buttons scrolling type: "continuous", "pixels"*/ + scrollSpeed:20, /*scroll buttons continuous scrolling speed: integer*/ + scrollAmount:40 /*scroll buttons pixels scroll amount: integer (pixels)*/ + }, + advanced:{ + updateOnBrowserResize:true, /*update scrollbars on browser resize (for layouts based on percentages): boolean*/ + updateOnContentResize:false, /*auto-update scrollbars on content resize (for dynamic content): boolean*/ + autoExpandHorizontalScroll:false, /*auto-expand width for horizontal scrolling: boolean*/ + autoScrollOnFocus:true /*auto-scroll on focused elements: boolean*/ + }, + callbacks:{ + onScrollStart:function(){}, /*user custom callback function on scroll start event*/ + onScroll:function(){}, /*user custom callback function on scroll event*/ + onTotalScroll:function(){}, /*user custom callback function on scroll end reached event*/ + onTotalScrollBack:function(){}, /*user custom callback function on scroll begin reached event*/ + onTotalScrollOffset:0, /*scroll end reached offset: integer (pixels)*/ + whileScrolling:false, /*user custom callback function on scrolling event*/ + whileScrollingInterval:30 /*interval for calling whileScrolling callback: integer (milliseconds)*/ + } + }, + options=$.extend(true,defaults,options); + /*check for touch device*/ + $(document).data("mCS-is-touch-device",false); + if(is_touch_device()){ + $(document).data("mCS-is-touch-device",true); + } + function is_touch_device(){ + return !!("ontouchstart" in window) ? 1 : 0; + } + return this.each(function(){ + var $this=$(this); + /*set element width/height, create markup for custom scrollbars, add classes*/ + if(options.set_width){ + $this.css("width",options.set_width); + } + if(options.set_height){ + $this.css("height",options.set_height); + } + if(!$(document).data("mCustomScrollbar-index")){ + $(document).data("mCustomScrollbar-index","1"); + }else{ + var mCustomScrollbarIndex=parseInt($(document).data("mCustomScrollbar-index")); + $(document).data("mCustomScrollbar-index",mCustomScrollbarIndex+1); + } + $this.wrapInner("
      ").addClass("mCustomScrollbar _mCS_"+$(document).data("mCustomScrollbar-index")); + var mCustomScrollBox=$this.children(".mCustomScrollBox"); + if(options.horizontalScroll){ + mCustomScrollBox.addClass("mCSB_horizontal").wrapInner("
      "); + var mCSB_h_wrapper=mCustomScrollBox.children(".mCSB_h_wrapper"); + mCSB_h_wrapper.wrapInner("
      ").children(".mCSB_container").css({"width":mCSB_h_wrapper.children().outerWidth(),"position":"relative"}).unwrap(); + }else{ + mCustomScrollBox.wrapInner("
      "); + } + var mCSB_container=mCustomScrollBox.children(".mCSB_container"); + if($(document).data("mCS-is-touch-device")){ + mCSB_container.addClass("mCS_touch"); + } + mCSB_container.after("
      "); + var mCSB_scrollTools=mCustomScrollBox.children(".mCSB_scrollTools"), + mCSB_draggerContainer=mCSB_scrollTools.children(".mCSB_draggerContainer"), + mCSB_dragger=mCSB_draggerContainer.children(".mCSB_dragger"); + if(options.horizontalScroll){ + mCSB_dragger.data("minDraggerWidth",mCSB_dragger.width()); + }else{ + mCSB_dragger.data("minDraggerHeight",mCSB_dragger.height()); + } + if(options.scrollButtons.enable){ + if(options.horizontalScroll){ + mCSB_scrollTools.prepend("").append(""); + }else{ + mCSB_scrollTools.prepend("").append(""); + } + } + /*mCustomScrollBox scrollTop and scrollLeft is always 0 to prevent browser focus scrolling*/ + mCustomScrollBox.bind("scroll",function(){ + if(!$this.is(".mCS_disabled")){ /*native focus scrolling for disabled scrollbars*/ + mCustomScrollBox.scrollTop(0).scrollLeft(0); + } + }); + /*store options, global vars/states, intervals and update element*/ + $this.data({ + /*init state*/ + "mCS_Init":true, + /*option parameters*/ + "horizontalScroll":options.horizontalScroll, + "scrollInertia":options.scrollInertia, + "scrollEasing":options.scrollEasing, + "mouseWheel":options.mouseWheel, + "mouseWheelPixels":options.mouseWheelPixels, + "autoDraggerLength":options.autoDraggerLength, + "scrollButtons_enable":options.scrollButtons.enable, + "scrollButtons_scrollType":options.scrollButtons.scrollType, + "scrollButtons_scrollSpeed":options.scrollButtons.scrollSpeed, + "scrollButtons_scrollAmount":options.scrollButtons.scrollAmount, + "autoExpandHorizontalScroll":options.advanced.autoExpandHorizontalScroll, + "autoScrollOnFocus":options.advanced.autoScrollOnFocus, + "onScrollStart_Callback":options.callbacks.onScrollStart, + "onScroll_Callback":options.callbacks.onScroll, + "onTotalScroll_Callback":options.callbacks.onTotalScroll, + "onTotalScrollBack_Callback":options.callbacks.onTotalScrollBack, + "onTotalScroll_Offset":options.callbacks.onTotalScrollOffset, + "whileScrolling_Callback":options.callbacks.whileScrolling, + "whileScrolling_Interval":options.callbacks.whileScrollingInterval, + /*events binding state*/ + "bindEvent_scrollbar_click":false, + "bindEvent_mousewheel":false, + "bindEvent_focusin":false, + "bindEvent_buttonsContinuous_y":false, + "bindEvent_buttonsContinuous_x":false, + "bindEvent_buttonsPixels_y":false, + "bindEvent_buttonsPixels_x":false, + "bindEvent_scrollbar_touch":false, + "bindEvent_content_touch":false, + /*buttons intervals*/ + "mCSB_buttonScrollRight":false, + "mCSB_buttonScrollLeft":false, + "mCSB_buttonScrollDown":false, + "mCSB_buttonScrollUp":false, + /*callback intervals*/ + "whileScrolling":false + }).mCustomScrollbar("update"); + /*detect max-width*/ + if(options.horizontalScroll){ + if($this.css("max-width")!=="none"){ + if(!options.advanced.updateOnContentResize){ /*needs updateOnContentResize*/ + options.advanced.updateOnContentResize=true; + } + $this.data({"mCS_maxWidth":parseInt($this.css("max-width")),"mCS_maxWidth_Interval":setInterval(function(){ + if(mCSB_container.outerWidth()>$this.data("mCS_maxWidth")){ + clearInterval($this.data("mCS_maxWidth_Interval")); + $this.mCustomScrollbar("update"); + } + },150)}); + } + }else{ + /*detect max-height*/ + if($this.css("max-height")!=="none"){ + $this.data({"mCS_maxHeight":parseInt($this.css("max-height")),"mCS_maxHeight_Interval":setInterval(function(){ + mCustomScrollBox.css("max-height",$this.data("mCS_maxHeight")); + if(mCSB_container.outerHeight()>$this.data("mCS_maxHeight")){ + clearInterval($this.data("mCS_maxHeight_Interval")); + $this.mCustomScrollbar("update"); + } + },150)}); + } + } + /*window resize fn (for layouts based on percentages)*/ + if(options.advanced.updateOnBrowserResize){ + var mCSB_resizeTimeout; + $(window).resize(function(){ + if(mCSB_resizeTimeout){ + clearTimeout(mCSB_resizeTimeout); + } + mCSB_resizeTimeout=setTimeout(function(){ + if(!$this.is(".mCS_disabled") && !$this.is(".mCS_destroyed")){ + $this.mCustomScrollbar("update"); + } + },150); + }); + } + /*content resize fn (for dynamically generated content)*/ + if(options.advanced.updateOnContentResize){ + var mCSB_onContentResize; + if(options.horizontalScroll){ + var mCSB_containerOldSize=mCSB_container.outerWidth(); + }else{ + var mCSB_containerOldSize=mCSB_container.outerHeight(); + } + mCSB_onContentResize=setInterval(function(){ + if(options.horizontalScroll){ + if(options.advanced.autoExpandHorizontalScroll){ + mCSB_container.css({"position":"absolute","width":"auto"}).wrap("
      ").css({"width":mCSB_container.outerWidth(),"position":"relative"}).unwrap(); + } + var mCSB_containerNewSize=mCSB_container.outerWidth(); + }else{ + var mCSB_containerNewSize=mCSB_container.outerHeight(); + } + if(mCSB_containerNewSize!=mCSB_containerOldSize){ + $this.mCustomScrollbar("update"); + mCSB_containerOldSize=mCSB_containerNewSize; + } + },300); + } + }); + }, + update:function(){ + var $this=$(this), + mCustomScrollBox=$this.children(".mCustomScrollBox"), + mCSB_container=mCustomScrollBox.children(".mCSB_container"); + mCSB_container.removeClass("mCS_no_scrollbar"); + $this.removeClass("mCS_disabled mCS_destroyed"); + mCustomScrollBox.scrollTop(0).scrollLeft(0); /*reset scrollTop/scrollLeft to prevent browser focus scrolling*/ + var mCSB_scrollTools=mCustomScrollBox.children(".mCSB_scrollTools"), + mCSB_draggerContainer=mCSB_scrollTools.children(".mCSB_draggerContainer"), + mCSB_dragger=mCSB_draggerContainer.children(".mCSB_dragger"); + if($this.data("horizontalScroll")){ + var mCSB_buttonLeft=mCSB_scrollTools.children(".mCSB_buttonLeft"), + mCSB_buttonRight=mCSB_scrollTools.children(".mCSB_buttonRight"), + mCustomScrollBoxW=mCustomScrollBox.width(); + if($this.data("autoExpandHorizontalScroll")){ + mCSB_container.css({"position":"absolute","width":"auto"}).wrap("
      ").css({"width":mCSB_container.outerWidth(),"position":"relative"}).unwrap(); + } + var mCSB_containerW=mCSB_container.outerWidth(); + }else{ + var mCSB_buttonUp=mCSB_scrollTools.children(".mCSB_buttonUp"), + mCSB_buttonDown=mCSB_scrollTools.children(".mCSB_buttonDown"), + mCustomScrollBoxH=mCustomScrollBox.height(), + mCSB_containerH=mCSB_container.outerHeight(); + } + if(mCSB_containerH>mCustomScrollBoxH && !$this.data("horizontalScroll")){ /*content needs vertical scrolling*/ + mCSB_scrollTools.css("display","block"); + var mCSB_draggerContainerH=mCSB_draggerContainer.height(); + /*auto adjust scrollbar dragger length analogous to content*/ + if($this.data("autoDraggerLength")){ + var draggerH=Math.round(mCustomScrollBoxH/mCSB_containerH*mCSB_draggerContainerH), + minDraggerH=mCSB_dragger.data("minDraggerHeight"); + if(draggerH<=minDraggerH){ /*min dragger height*/ + mCSB_dragger.css({"height":minDraggerH}); + }else if(draggerH>=mCSB_draggerContainerH-10){ /*max dragger height*/ + var mCSB_draggerContainerMaxH=mCSB_draggerContainerH-10; + mCSB_dragger.css({"height":mCSB_draggerContainerMaxH}); + }else{ + mCSB_dragger.css({"height":draggerH}); + } + mCSB_dragger.children(".mCSB_dragger_bar").css({"line-height":mCSB_dragger.height()+"px"}); + } + var mCSB_draggerH=mCSB_dragger.height(), + /*calculate and store scroll amount, add scrolling*/ + scrollAmount=(mCSB_containerH-mCustomScrollBoxH)/(mCSB_draggerContainerH-mCSB_draggerH); + $this.data("scrollAmount",scrollAmount).mCustomScrollbar("scrolling",mCustomScrollBox,mCSB_container,mCSB_draggerContainer,mCSB_dragger,mCSB_buttonUp,mCSB_buttonDown,mCSB_buttonLeft,mCSB_buttonRight); + /*scroll*/ + var mCSB_containerP=Math.abs(Math.round(mCSB_container.position().top)); + $this.mCustomScrollbar("scrollTo",mCSB_containerP,{callback:false}); + }else if(mCSB_containerW>mCustomScrollBoxW && $this.data("horizontalScroll")){ /*content needs horizontal scrolling*/ + mCSB_scrollTools.css("display","block"); + var mCSB_draggerContainerW=mCSB_draggerContainer.width(); + /*auto adjust scrollbar dragger length analogous to content*/ + if($this.data("autoDraggerLength")){ + var draggerW=Math.round(mCustomScrollBoxW/mCSB_containerW*mCSB_draggerContainerW), + minDraggerW=mCSB_dragger.data("minDraggerWidth"); + if(draggerW<=minDraggerW){ /*min dragger height*/ + mCSB_dragger.css({"width":minDraggerW}); + }else if(draggerW>=mCSB_draggerContainerW-10){ /*max dragger height*/ + var mCSB_draggerContainerMaxW=mCSB_draggerContainerW-10; + mCSB_dragger.css({"width":mCSB_draggerContainerMaxW}); + }else{ + mCSB_dragger.css({"width":draggerW}); + } + } + var mCSB_draggerW=mCSB_dragger.width(), + /*calculate and store scroll amount, add scrolling*/ + scrollAmount=(mCSB_containerW-mCustomScrollBoxW)/(mCSB_draggerContainerW-mCSB_draggerW); + $this.data("scrollAmount",scrollAmount).mCustomScrollbar("scrolling",mCustomScrollBox,mCSB_container,mCSB_draggerContainer,mCSB_dragger,mCSB_buttonUp,mCSB_buttonDown,mCSB_buttonLeft,mCSB_buttonRight); + /*scroll*/ + var mCSB_containerP=Math.abs(Math.round(mCSB_container.position().left)); + $this.mCustomScrollbar("scrollTo",mCSB_containerP,{callback:false}); + }else{ /*content does not need scrolling*/ + /*unbind events, reset content position, hide scrollbars, remove classes*/ + mCustomScrollBox.unbind("mousewheel focusin"); + if($this.data("horizontalScroll")){ + mCSB_dragger.add(mCSB_container).css("left",0); + }else{ + mCSB_dragger.add(mCSB_container).css("top",0); + } + mCSB_scrollTools.css("display","none"); + mCSB_container.addClass("mCS_no_scrollbar"); + $this.data({"bindEvent_mousewheel":false,"bindEvent_focusin":false}); + } + }, + scrolling:function(mCustomScrollBox,mCSB_container,mCSB_draggerContainer,mCSB_dragger,mCSB_buttonUp,mCSB_buttonDown,mCSB_buttonLeft,mCSB_buttonRight){ + var $this=$(this); + /*while scrolling callback*/ + $this.mCustomScrollbar("callbacks","whileScrolling"); + /*drag scrolling*/ + if(!mCSB_dragger.hasClass("ui-draggable")){ /*apply drag function once*/ + if($this.data("horizontalScroll")){ + var draggableAxis="x"; + }else{ + var draggableAxis="y"; + } + mCSB_dragger.draggable({ + axis:draggableAxis, + containment:"parent", + drag:function(event,ui){ + $this.mCustomScrollbar("scroll"); + mCSB_dragger.addClass("mCSB_dragger_onDrag"); + }, + stop:function(event,ui){ + mCSB_dragger.removeClass("mCSB_dragger_onDrag"); + } + }); + } + if(!$this.data("bindEvent_scrollbar_click")){ /*bind once*/ + mCSB_draggerContainer.bind("click",function(e){ + if($this.data("horizontalScroll")){ + var mouseCoord=(e.pageX-mCSB_draggerContainer.offset().left); + if(mouseCoord(mCSB_dragger.position().left+mCSB_dragger.width())){ + var scrollToPos=mouseCoord; + if(scrollToPos>=mCSB_draggerContainer.width()-mCSB_dragger.width()){ /*max dragger position is bottom*/ + scrollToPos=mCSB_draggerContainer.width()-mCSB_dragger.width(); + } + mCSB_dragger.css("left",scrollToPos); + $this.mCustomScrollbar("scroll"); + } + }else{ + var mouseCoord=(e.pageY-mCSB_draggerContainer.offset().top); + if(mouseCoord(mCSB_dragger.position().top+mCSB_dragger.height())){ + var scrollToPos=mouseCoord; + if(scrollToPos>=mCSB_draggerContainer.height()-mCSB_dragger.height()){ /*max dragger position is bottom*/ + scrollToPos=mCSB_draggerContainer.height()-mCSB_dragger.height(); + } + mCSB_dragger.css("top",scrollToPos); + $this.mCustomScrollbar("scroll"); + } + } + }); + $this.data({"bindEvent_scrollbar_click":true}); + } + /*mousewheel scrolling*/ + if($this.data("mouseWheel")){ + var mousewheelVel=$this.data("mouseWheel"); + if($this.data("mouseWheel")==="auto"){ + mousewheelVel=8; /*default mousewheel velocity*/ + /*check for safari browser on mac osx to lower mousewheel velocity*/ + var os=navigator.userAgent; + if(os.indexOf("Mac")!=-1 && os.indexOf("Safari")!=-1 && os.indexOf("AppleWebKit")!=-1 && os.indexOf("Chrome")==-1){ + mousewheelVel=1; + } + } + if(!$this.data("bindEvent_mousewheel")){ /*bind once*/ + mCustomScrollBox.bind("mousewheel",function(event,delta){ + event.preventDefault(); + var vel=Math.abs(delta*mousewheelVel); + if($this.data("horizontalScroll")){ + if($this.data("mouseWheel")==="pixels"){ + if(delta<0){ + delta=-1; + }else{ + delta=1; + } + var scrollTo=Math.abs(Math.round(mCSB_container.position().left))-(delta*$this.data("mouseWheelPixels")); + $this.mCustomScrollbar("scrollTo",scrollTo); + }else{ + var posX=mCSB_dragger.position().left-(delta*vel); + mCSB_dragger.css("left",posX); + if(mCSB_dragger.position().left<0){ + mCSB_dragger.css("left",0); + } + var mCSB_draggerContainerW=mCSB_draggerContainer.width(), + mCSB_draggerW=mCSB_dragger.width(); + if(mCSB_dragger.position().left>mCSB_draggerContainerW-mCSB_draggerW){ + mCSB_dragger.css("left",mCSB_draggerContainerW-mCSB_draggerW); + } + $this.mCustomScrollbar("scroll"); + } + }else{ + if($this.data("mouseWheel")==="pixels"){ + if(delta<0){ + delta=-1; + }else{ + delta=1; + } + var scrollTo=Math.abs(Math.round(mCSB_container.position().top))-(delta*$this.data("mouseWheelPixels")); + $this.mCustomScrollbar("scrollTo",scrollTo); + }else{ + var posY=mCSB_dragger.position().top-(delta*vel); + mCSB_dragger.css("top",posY); + if(mCSB_dragger.position().top<0){ + mCSB_dragger.css("top",0); + } + var mCSB_draggerContainerH=mCSB_draggerContainer.height(), + mCSB_draggerH=mCSB_dragger.height(); + if(mCSB_dragger.position().top>mCSB_draggerContainerH-mCSB_draggerH){ + mCSB_dragger.css("top",mCSB_draggerContainerH-mCSB_draggerH); + } + $this.mCustomScrollbar("scroll"); + } + } + }); + $this.data({"bindEvent_mousewheel":true}); + } + } + /*buttons scrolling*/ + if($this.data("scrollButtons_enable")){ + if($this.data("scrollButtons_scrollType")==="pixels"){ /*scroll by pixels*/ + var pixelsScrollTo; + if($.browser.msie && parseInt($.browser.version)<9){ /*stupid ie8*/ + $this.data("scrollInertia",0); + } + if($this.data("horizontalScroll")){ + mCSB_buttonRight.add(mCSB_buttonLeft).unbind("mousedown touchstart onmsgesturestart mouseup mouseout touchend onmsgestureend",mCSB_buttonRight_stop,mCSB_buttonLeft_stop); + $this.data({"bindEvent_buttonsContinuous_x":false}); + if(!$this.data("bindEvent_buttonsPixels_x")){ /*bind once*/ + /*scroll right*/ + mCSB_buttonRight.bind("click",function(e){ + e.preventDefault(); + if(!mCSB_container.is(":animated")){ + pixelsScrollTo=Math.abs(mCSB_container.position().left)+$this.data("scrollButtons_scrollAmount"); + $this.mCustomScrollbar("scrollTo",pixelsScrollTo); + } + }); + /*scroll left*/ + mCSB_buttonLeft.bind("click",function(e){ + e.preventDefault(); + if(!mCSB_container.is(":animated")){ + pixelsScrollTo=Math.abs(mCSB_container.position().left)-$this.data("scrollButtons_scrollAmount"); + if(mCSB_container.position().left>=-$this.data("scrollButtons_scrollAmount")){ + pixelsScrollTo="left"; + } + $this.mCustomScrollbar("scrollTo",pixelsScrollTo); + } + }); + $this.data({"bindEvent_buttonsPixels_x":true}); + } + }else{ + mCSB_buttonDown.add(mCSB_buttonUp).unbind("mousedown touchstart onmsgesturestart mouseup mouseout touchend onmsgestureend",mCSB_buttonRight_stop,mCSB_buttonLeft_stop); + $this.data({"bindEvent_buttonsContinuous_y":false}); + if(!$this.data("bindEvent_buttonsPixels_y")){ /*bind once*/ + /*scroll down*/ + mCSB_buttonDown.bind("click",function(e){ + e.preventDefault(); + if(!mCSB_container.is(":animated")){ + pixelsScrollTo=Math.abs(mCSB_container.position().top)+$this.data("scrollButtons_scrollAmount"); + $this.mCustomScrollbar("scrollTo",pixelsScrollTo); + } + }); + /*scroll up*/ + mCSB_buttonUp.bind("click",function(e){ + e.preventDefault(); + if(!mCSB_container.is(":animated")){ + pixelsScrollTo=Math.abs(mCSB_container.position().top)-$this.data("scrollButtons_scrollAmount"); + if(mCSB_container.position().top>=-$this.data("scrollButtons_scrollAmount")){ + pixelsScrollTo="top"; + } + $this.mCustomScrollbar("scrollTo",pixelsScrollTo); + } + }); + $this.data({"bindEvent_buttonsPixels_y":true}); + } + } + }else{ /*continuous scrolling*/ + if($this.data("horizontalScroll")){ + mCSB_buttonRight.add(mCSB_buttonLeft).unbind("click"); + $this.data({"bindEvent_buttonsPixels_x":false}); + if(!$this.data("bindEvent_buttonsContinuous_x")){ /*bind once*/ + /*scroll right*/ + mCSB_buttonRight.bind("mousedown touchstart onmsgesturestart",function(e){ + e.preventDefault(); + e.stopPropagation(); + $this.data({"mCSB_buttonScrollRight":setInterval(function(){ + var scrollTo=Math.round((Math.abs(Math.round(mCSB_container.position().left))+$this.data("scrollButtons_scrollSpeed"))/$this.data("scrollAmount")); + $this.mCustomScrollbar("scrollTo",scrollTo,{moveDragger:true}); + },30)}); + }); + var mCSB_buttonRight_stop=function(e){ + e.preventDefault(); + e.stopPropagation(); + clearInterval($this.data("mCSB_buttonScrollRight")); + } + mCSB_buttonRight.bind("mouseup touchend onmsgestureend mouseout",mCSB_buttonRight_stop); + /*scroll left*/ + mCSB_buttonLeft.bind("mousedown touchstart onmsgesturestart",function(e){ + e.preventDefault(); + e.stopPropagation(); + $this.data({"mCSB_buttonScrollLeft":setInterval(function(){ + var scrollTo=Math.round((Math.abs(Math.round(mCSB_container.position().left))-$this.data("scrollButtons_scrollSpeed"))/$this.data("scrollAmount")); + $this.mCustomScrollbar("scrollTo",scrollTo,{moveDragger:true}); + },30)}); + }); + var mCSB_buttonLeft_stop=function(e){ + e.preventDefault(); + e.stopPropagation(); + clearInterval($this.data("mCSB_buttonScrollLeft")); + } + mCSB_buttonLeft.bind("mouseup touchend onmsgestureend mouseout",mCSB_buttonLeft_stop); + $this.data({"bindEvent_buttonsContinuous_x":true}); + } + }else{ + mCSB_buttonDown.add(mCSB_buttonUp).unbind("click"); + $this.data({"bindEvent_buttonsPixels_y":false}); + if(!$this.data("bindEvent_buttonsContinuous_y")){ /*bind once*/ + /*scroll down*/ + mCSB_buttonDown.bind("mousedown touchstart onmsgesturestart",function(e){ + e.preventDefault(); + e.stopPropagation(); + $this.data({"mCSB_buttonScrollDown":setInterval(function(){ + var scrollTo=Math.round((Math.abs(Math.round(mCSB_container.position().top))+$this.data("scrollButtons_scrollSpeed"))/$this.data("scrollAmount")); + $this.mCustomScrollbar("scrollTo",scrollTo,{moveDragger:true}); + },30)}); + }); + var mCSB_buttonDown_stop=function(e){ + e.preventDefault(); + e.stopPropagation(); + clearInterval($this.data("mCSB_buttonScrollDown")); + } + mCSB_buttonDown.bind("mouseup touchend onmsgestureend mouseout",mCSB_buttonDown_stop); + /*scroll up*/ + mCSB_buttonUp.bind("mousedown touchstart onmsgesturestart",function(e){ + e.preventDefault(); + e.stopPropagation(); + $this.data({"mCSB_buttonScrollUp":setInterval(function(){ + var scrollTo=Math.round((Math.abs(Math.round(mCSB_container.position().top))-$this.data("scrollButtons_scrollSpeed"))/$this.data("scrollAmount")); + $this.mCustomScrollbar("scrollTo",scrollTo,{moveDragger:true}); + },30)}); + }); + var mCSB_buttonUp_stop=function(e){ + e.preventDefault(); + e.stopPropagation(); + clearInterval($this.data("mCSB_buttonScrollUp")); + } + mCSB_buttonUp.bind("mouseup touchend onmsgestureend mouseout",mCSB_buttonUp_stop); + $this.data({"bindEvent_buttonsContinuous_y":true}); + } + } + } + } + /*scrolling on element focus (e.g. via TAB key)*/ + if($this.data("autoScrollOnFocus")){ + if(!$this.data("bindEvent_focusin")){ /*bind once*/ + mCustomScrollBox.bind("focusin",function(){ + mCustomScrollBox.scrollTop(0).scrollLeft(0); + var focusedElem=$(document.activeElement); + if(focusedElem.is("input,textarea,select,button,a[tabindex],area,object")){ + if($this.data("horizontalScroll")){ + var mCSB_containerX=mCSB_container.position().left, + focusedElemX=focusedElem.position().left, + mCustomScrollBoxW=mCustomScrollBox.width(), + focusedElemW=focusedElem.outerWidth(); + if(mCSB_containerX+focusedElemX>=0 && mCSB_containerX+focusedElemX<=mCustomScrollBoxW-focusedElemW){ + /*just focus...*/ + }else{ /*scroll, then focus*/ + var moveDragger=focusedElemX/$this.data("scrollAmount"); + if(moveDragger>=mCSB_draggerContainer.width()-mCSB_dragger.width()){ /*max dragger position is bottom*/ + moveDragger=mCSB_draggerContainer.width()-mCSB_dragger.width(); + } + mCSB_dragger.css("left",moveDragger); + $this.mCustomScrollbar("scroll"); + } + }else{ + var mCSB_containerY=mCSB_container.position().top, + focusedElemY=focusedElem.position().top, + mCustomScrollBoxH=mCustomScrollBox.height(), + focusedElemH=focusedElem.outerHeight(); + if(mCSB_containerY+focusedElemY>=0 && mCSB_containerY+focusedElemY<=mCustomScrollBoxH-focusedElemH){ + /*just focus...*/ + }else{ /*scroll, then focus*/ + var moveDragger=focusedElemY/$this.data("scrollAmount"); + if(moveDragger>=mCSB_draggerContainer.height()-mCSB_dragger.height()){ /*max dragger position is bottom*/ + moveDragger=mCSB_draggerContainer.height()-mCSB_dragger.height(); + } + mCSB_dragger.css("top",moveDragger); + $this.mCustomScrollbar("scroll"); + } + } + } + }); + $this.data({"bindEvent_focusin":true}); + } + } + /*touch events*/ + if($(document).data("mCS-is-touch-device")){ + /*scrollbar touch-drag*/ + if(!$this.data("bindEvent_scrollbar_touch")){ /*bind once*/ + var mCSB_draggerTouchY, + mCSB_draggerTouchX; + mCSB_dragger.bind("touchstart onmsgesturestart",function(e){ + e.preventDefault(); + e.stopPropagation(); + var touch=e.originalEvent.touches[0] || e.originalEvent.changedTouches[0], + elem=$(this), + elemOffset=elem.offset(), + x=touch.pageX-elemOffset.left, + y=touch.pageY-elemOffset.top; + if(x0 && y0){ + mCSB_draggerTouchY=y; + mCSB_draggerTouchX=x; + } + }); + mCSB_dragger.bind("touchmove onmsgesturechange",function(e){ + e.preventDefault(); + e.stopPropagation(); + var touch=e.originalEvent.touches[0] || e.originalEvent.changedTouches[0], + elem=$(this), + elemOffset=elem.offset(), + x=touch.pageX-elemOffset.left, + y=touch.pageY-elemOffset.top; + if($this.data("horizontalScroll")){ + $this.mCustomScrollbar("scrollTo",(mCSB_dragger.position().left-(mCSB_draggerTouchX))+x,{moveDragger:true}); + }else{ + $this.mCustomScrollbar("scrollTo",(mCSB_dragger.position().top-(mCSB_draggerTouchY))+y,{moveDragger:true}); + } + }); + $this.data({"bindEvent_scrollbar_touch":true}); + } + /*content touch-drag*/ + if(!$this.data("bindEvent_content_touch")){ /*bind once*/ + var touch, + elem, + elemOffset, + x, + y, + mCSB_containerTouchY, + mCSB_containerTouchX; + mCSB_container.bind("touchstart onmsgesturestart",function(e){ + touch=e.originalEvent.touches[0] || e.originalEvent.changedTouches[0]; + elem=$(this); + elemOffset=elem.offset(); + x=touch.pageX-elemOffset.left; + y=touch.pageY-elemOffset.top; + mCSB_containerTouchY=y; + mCSB_containerTouchX=x; + }); + mCSB_container.bind("touchmove onmsgesturechange",function(e){ + e.preventDefault(); + e.stopPropagation(); + touch=e.originalEvent.touches[0] || e.originalEvent.changedTouches[0]; + elem=$(this).parent(); + elemOffset=elem.offset(); + x=touch.pageX-elemOffset.left; + y=touch.pageY-elemOffset.top; + if($this.data("horizontalScroll")){ + $this.mCustomScrollbar("scrollTo",mCSB_containerTouchX-x); + }else{ + $this.mCustomScrollbar("scrollTo",mCSB_containerTouchY-y); + } + }); + $this.data({"bindEvent_content_touch":true}); + } + } + }, + scroll:function(bypassCallbacks){ + var $this=$(this), + mCSB_dragger=$this.find(".mCSB_dragger"), + mCSB_container=$this.find(".mCSB_container"), + mCustomScrollBox=$this.find(".mCustomScrollBox"); + if($this.data("horizontalScroll")){ + var draggerX=mCSB_dragger.position().left, + targX=-draggerX*$this.data("scrollAmount"), + thisX=mCSB_container.position().left, + posX=Math.round(thisX-targX); + }else{ + var draggerY=mCSB_dragger.position().top, + targY=-draggerY*$this.data("scrollAmount"), + thisY=mCSB_container.position().top, + posY=Math.round(thisY-targY); + } + if($.browser.webkit){ /*fix webkit zoom and jquery animate*/ + var screenCssPixelRatio=(window.outerWidth-8)/window.innerWidth, + isZoomed=(screenCssPixelRatio<.98 || screenCssPixelRatio>1.02); + } + if($this.data("scrollInertia")===0 || isZoomed){ + if(!bypassCallbacks){ + $this.mCustomScrollbar("callbacks","onScrollStart"); /*user custom callback functions*/ + } + if($this.data("horizontalScroll")){ + mCSB_container.css("left",targX); + }else{ + mCSB_container.css("top",targY); + } + if(!bypassCallbacks){ + /*user custom callback functions*/ + if($this.data("whileScrolling")){ + $this.data("whileScrolling_Callback").call(); + } + $this.mCustomScrollbar("callbacks","onScroll"); + } + $this.data({"mCS_Init":false}); + }else{ + if(!bypassCallbacks){ + $this.mCustomScrollbar("callbacks","onScrollStart"); /*user custom callback functions*/ + } + if($this.data("horizontalScroll")){ + mCSB_container.stop().animate({left:"-="+posX},$this.data("scrollInertia"),$this.data("scrollEasing"),function(){ + if(!bypassCallbacks){ + $this.mCustomScrollbar("callbacks","onScroll"); /*user custom callback functions*/ + } + $this.data({"mCS_Init":false}); + }); + }else{ + mCSB_container.stop().animate({top:"-="+posY},$this.data("scrollInertia"),$this.data("scrollEasing"),function(){ + if(!bypassCallbacks){ + $this.mCustomScrollbar("callbacks","onScroll"); /*user custom callback functions*/ + } + $this.data({"mCS_Init":false}); + }); + } + } + }, + scrollTo:function(scrollTo,options){ + var defaults={ + moveDragger:false, + callback:true + }, + options=$.extend(defaults,options), + $this=$(this), + scrollToPos, + mCustomScrollBox=$this.find(".mCustomScrollBox"), + mCSB_container=mCustomScrollBox.children(".mCSB_container"), + mCSB_draggerContainer=$this.find(".mCSB_draggerContainer"), + mCSB_dragger=mCSB_draggerContainer.children(".mCSB_dragger"), + targetPos; + if(scrollTo || scrollTo===0){ + if(typeof(scrollTo)==="number"){ /*if integer, scroll by number of pixels*/ + if(options.moveDragger){ /*scroll dragger*/ + scrollToPos=scrollTo; + }else{ /*scroll content by default*/ + targetPos=scrollTo; + scrollToPos=Math.round(targetPos/$this.data("scrollAmount")); + } + }else if(typeof(scrollTo)==="string"){ /*if string, scroll by element position*/ + var target; + if(scrollTo==="top"){ /*scroll to top*/ + target=0; + }else if(scrollTo==="bottom" && !$this.data("horizontalScroll")){ /*scroll to bottom*/ + target=mCSB_container.outerHeight()-mCustomScrollBox.height(); + }else if(scrollTo==="left"){ /*scroll to left*/ + target=0; + }else if(scrollTo==="right" && $this.data("horizontalScroll")){ /*scroll to right*/ + target=mCSB_container.outerWidth()-mCustomScrollBox.width(); + }else if(scrollTo==="first"){ /*scroll to first element position*/ + target=$this.find(".mCSB_container").find(":first"); + }else if(scrollTo==="last"){ /*scroll to last element position*/ + target=$this.find(".mCSB_container").find(":last"); + }else{ /*scroll to element position*/ + target=$this.find(scrollTo); + } + if(target.length===1){ /*if such unique element exists, scroll to it*/ + if($this.data("horizontalScroll")){ + targetPos=target.position().left; + }else{ + targetPos=target.position().top; + } + scrollToPos=Math.ceil(targetPos/$this.data("scrollAmount")); + }else{ + scrollToPos=target; + } + } + /*scroll to*/ + if(scrollToPos<0){ + scrollToPos=0; + } + if($this.data("horizontalScroll")){ + if(scrollToPos>=mCSB_draggerContainer.width()-mCSB_dragger.width()){ /*max dragger position is bottom*/ + scrollToPos=mCSB_draggerContainer.width()-mCSB_dragger.width(); + } + mCSB_dragger.css("left",scrollToPos); + }else{ + if(scrollToPos>=mCSB_draggerContainer.height()-mCSB_dragger.height()){ /*max dragger position is bottom*/ + scrollToPos=mCSB_draggerContainer.height()-mCSB_dragger.height(); + } + mCSB_dragger.css("top",scrollToPos); + } + if(options.callback){ + $this.mCustomScrollbar("scroll",false); + }else{ + $this.mCustomScrollbar("scroll",true); + } + } + }, + callbacks:function(callback){ + var $this=$(this), + mCustomScrollBox=$this.find(".mCustomScrollBox"), + mCSB_container=$this.find(".mCSB_container"); + switch(callback){ + /*start scrolling callback*/ + case "onScrollStart": + if(!mCSB_container.is(":animated")){ + $this.data("onScrollStart_Callback").call(); + } + break; + /*end scrolling callback*/ + case "onScroll": + if($this.data("horizontalScroll")){ + var mCSB_containerX=Math.round(mCSB_container.position().left); + if(mCSB_containerX<0 && mCSB_containerX<=mCustomScrollBox.width()-mCSB_container.outerWidth()+$this.data("onTotalScroll_Offset")){ + $this.data("onTotalScroll_Callback").call(); + }else if(mCSB_containerX>=-$this.data("onTotalScroll_Offset")){ + $this.data("onTotalScrollBack_Callback").call(); + }else{ + $this.data("onScroll_Callback").call(); + } + }else{ + var mCSB_containerY=Math.round(mCSB_container.position().top); + if(mCSB_containerY<0 && mCSB_containerY<=mCustomScrollBox.height()-mCSB_container.outerHeight()+$this.data("onTotalScroll_Offset")){ + $this.data("onTotalScroll_Callback").call(); + }else if(mCSB_containerY>=-$this.data("onTotalScroll_Offset")){ + $this.data("onTotalScrollBack_Callback").call(); + }else{ + $this.data("onScroll_Callback").call(); + } + } + break; + /*while scrolling callback*/ + case "whileScrolling": + if($this.data("whileScrolling_Callback") && !$this.data("whileScrolling")){ + $this.data({"whileScrolling":setInterval(function(){ + if(mCSB_container.is(":animated") && !$this.data("mCS_Init")){ + $this.data("whileScrolling_Callback").call(); + } + },$this.data("whileScrolling_Interval"))}); + } + break; + } + }, + disable:function(resetScroll){ + var $this=$(this), + mCustomScrollBox=$this.children(".mCustomScrollBox"), + mCSB_container=mCustomScrollBox.children(".mCSB_container"), + mCSB_scrollTools=mCustomScrollBox.children(".mCSB_scrollTools"), + mCSB_dragger=mCSB_scrollTools.find(".mCSB_dragger"); + mCustomScrollBox.unbind("mousewheel focusin"); + if(resetScroll){ + if($this.data("horizontalScroll")){ + mCSB_dragger.add(mCSB_container).css("left",0); + }else{ + mCSB_dragger.add(mCSB_container).css("top",0); + } + } + mCSB_scrollTools.css("display","none"); + mCSB_container.addClass("mCS_no_scrollbar"); + $this.data({"bindEvent_mousewheel":false,"bindEvent_focusin":false}).addClass("mCS_disabled"); + }, + destroy:function(){ + var $this=$(this), + content=$this.find(".mCSB_container").html(); + $this.find(".mCustomScrollBox").remove(); + $this.html(content).removeClass("mCustomScrollbar _mCS_"+$(document).data("mCustomScrollbar-index")).addClass("mCS_destroyed"); + } + } + $.fn.mCustomScrollbar=function(method){ + if(methods[method]){ + return methods[method].apply(this,Array.prototype.slice.call(arguments,1)); + }else if(typeof method==="object" || !method){ + return methods.init.apply(this,arguments); + }else{ + $.error("Method "+method+" does not exist"); + } + }; +})(jQuery); +/*iOS 6 bug fix + iOS 6 suffers from a bug that kills timers that are created while a page is scrolling. + The following fixes that problem by recreating timers after scrolling finishes (with interval correction).*/ +var iOSVersion=iOSVersion(); +if(iOSVersion>=6){ + (function(h){var a={};var d={};var e=h.setTimeout;var f=h.setInterval;var i=h.clearTimeout;var c=h.clearInterval;if(!h.addEventListener){return false}function j(q,n,l){var p,k=l[0],m=(q===f);function o(){if(k){k.apply(h,arguments);if(!m){delete n[p];k=null}}}l[0]=o;p=q.apply(h,l);n[p]={args:l,created:Date.now(),cb:k,id:p};return p}function b(q,o,k,r,t){var l=k[r];if(!l){return}var m=(q===f);o(l.id);if(!m){var n=l.args[1];var p=Date.now()-l.created;if(p<0){p=0}n-=p;if(n<0){n=0}l.args[1]=n}function s(){if(l.cb){l.cb.apply(h,arguments);if(!m){delete k[r];l.cb=null}}}l.args[0]=s;l.created=Date.now();l.id=q.apply(h,l.args)}h.setTimeout=function(){return j(e,a,arguments)};h.setInterval=function(){return j(f,d,arguments)};h.clearTimeout=function(l){var k=a[l];if(k){delete a[l];i(k.id)}};h.clearInterval=function(l){var k=d[l];if(k){delete d[l];c(k.id)}};var g=h;while(g.location!=g.parent.location){g=g.parent}g.addEventListener("scroll",function(){var k;for(k in a){b(e,i,a,k)}for(k in d){b(f,c,d,k)}})}(window)); +} +function iOSVersion(){ + var agent=window.navigator.userAgent, + start=agent.indexOf('OS '); + if((agent.indexOf('iPhone')>-1 || agent.indexOf('iPad')>-1) && start>-1){ + return window.Number(agent.substr(start+3,3).replace('_','.')); + } + return 0; +} \ No newline at end of file diff --git a/lib/dashboard/js/jquery.mousewheel.min.js b/lib/dashboard/js/jquery.mousewheel.min.js new file mode 100644 index 0000000..3390202 --- /dev/null +++ b/lib/dashboard/js/jquery.mousewheel.min.js @@ -0,0 +1,12 @@ +/*! Copyright (c) 2011 Brandon Aaron (http://brandonaaron.net) + * Licensed under the MIT License (LICENSE.txt). + * + * Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers. + * Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix. + * Thanks to: Seamus Leahy for adding deltaX and deltaY + * + * Version: 3.0.6 + * + * Requires: 1.2.2+ + */ +(function(a){function d(b){var c=b||window.event,d=[].slice.call(arguments,1),e=0,f=!0,g=0,h=0;return b=a.event.fix(c),b.type="mousewheel",c.wheelDelta&&(e=c.wheelDelta/120),c.detail&&(e=-c.detail/3),h=e,c.axis!==undefined&&c.axis===c.HORIZONTAL_AXIS&&(h=0,g=-1*e),c.wheelDeltaY!==undefined&&(h=c.wheelDeltaY/120),c.wheelDeltaX!==undefined&&(g=-1*c.wheelDeltaX/120),d.unshift(b,e,g,h),(a.event.dispatch||a.event.handle).apply(this,d)}var b=["DOMMouseScroll","mousewheel"];if(a.event.fixHooks)for(var c=b.length;c;)a.event.fixHooks[b[--c]]=a.event.mouseHooks;a.event.special.mousewheel={setup:function(){if(this.addEventListener)for(var a=b.length;a;)this.addEventListener(b[--a],d,!1);else this.onmousewheel=d},teardown:function(){if(this.removeEventListener)for(var a=b.length;a;)this.removeEventListener(b[--a],d,!1);else this.onmousewheel=null}},a.fn.extend({mousewheel:function(a){return a?this.bind("mousewheel",a):this.trigger("mousewheel")},unmousewheel:function(a){return this.unbind("mousewheel",a)}})})(jQuery) diff --git a/lib/dashboard/webfonts/22AE77_0_0.eot b/lib/dashboard/webfonts/22AE77_0_0.eot new file mode 100644 index 0000000..07591ea Binary files /dev/null and b/lib/dashboard/webfonts/22AE77_0_0.eot differ diff --git a/lib/dashboard/webfonts/22AE77_0_0.ttf b/lib/dashboard/webfonts/22AE77_0_0.ttf new file mode 100644 index 0000000..a3d4698 --- /dev/null +++ b/lib/dashboard/webfonts/22AE77_0_0.ttf @@ -0,0 +1,3 @@ + + +
      Ö°Í°/°.Ͳ.
      +³@ +° 2±2+±° \ No newline at end of file diff --git a/lib/dashboard/webfonts/22AE77_0_0.woff b/lib/dashboard/webfonts/22AE77_0_0.woff new file mode 100644 index 0000000..bb671f4 --- /dev/null +++ b/lib/dashboard/webfonts/22AE77_0_0.woff @@ -0,0 +1,3 @@ + + +
      wOFF \ No newline at end of file diff --git a/lib/dashboard/webfonts/scoolar_tfb.eot b/lib/dashboard/webfonts/scoolar_tfb.eot new file mode 100644 index 0000000..59af250 Binary files /dev/null and b/lib/dashboard/webfonts/scoolar_tfb.eot differ diff --git a/lib/dashboard/webfonts/scoolar_tfb.ttf b/lib/dashboard/webfonts/scoolar_tfb.ttf new file mode 100644 index 0000000..1360641 Binary files /dev/null and b/lib/dashboard/webfonts/scoolar_tfb.ttf differ diff --git a/lib/initialize.js b/lib/initialize.js new file mode 100644 index 0000000..6286ed7 --- /dev/null +++ b/lib/initialize.js @@ -0,0 +1,59 @@ +var file = require('fs'); + +//create logs directory +file.exists('./../logs', function (exists) { + if (!exists) { + file.mkdir('./../logs'); + } +}); + +process.on('uncaughtException', function(err) { + file.appendFile('./../logs/error-log.txt', err.message + '\r\n' + err.stack + '\r\n', function() {}); +}); + +exports.initialize = function(options) { + var config = options || {}, db, irc, commands, dashboard, currency; + +//-------- Setup ------- + irc = require('./irc.js')({ + name : config.twitch.bot.name, + pass : config.twitch.bot.password, + channel : '#' + config.twitch.channel + }); + db = require('./mysql.js')({ + host : config.currency.host, + user : config.currency.user, + password : config.currency.password, + database : config.currency.database + }); + commands = require('./commands.js')(irc, db, { + bot_name : config.twitch.bot.name, + currency : config.currency.name + }); + currency = require('./currency.js')(irc, db, { + currency : config.currency.name, + payrate : config.currency.payrate, + subscribers : config.twitch.subscribers, + website : config.currency.website + }); + +//-------- Start ------- + irc.start(); + db.start(); + currency.start(); + if (config.commands === true) commands.start(); + + irc.on('data', function (data) { + console.log(data); + irc.realtime(data); + }); + + irc.on('command', function (data) { + currency.commands(data); + if (config.commands === true) commands.commands(data); + }); + + irc.on('message', function (msg) { + irc.queue(msg); + }); +}; \ No newline at end of file diff --git a/lib/irc.js b/lib/irc.js new file mode 100644 index 0000000..0b6e9dc --- /dev/null +++ b/lib/irc.js @@ -0,0 +1,368 @@ +/** + * api: + * IRC([required options]) + * required options - {name, pass, channel} + * + * IRC.connect() + * connects to the twitch irc server + * + * IRC.on('command', callback) + * allows custom commands + * + * IRC.on('data', callback) + * event when data is recieved / sent + * + * IRC.msg(message, [options]) + * options - {caller, auth[0/1]} + * + * IRC.caller(data[0]) + * parse out user name from socket data, + * mainly for plugin use when working with commands + * + * example: + * require: + * var irc = require('./lib/core/irc.js')({ + * name : 'TwitchBot', + * pass : 'twitch!twitch!twitch!', + * channel : '#awesomebroadcaster' + * }); + * + * connect: + * irc.connect(); + * + * custom commands: + * irc.on('command' function (data) { + * if (data[3] == ':!command') { + * // do something + * } + * }); + * + * irc data: + * irc.on('data', function (data) { + * //do something with data + * }); + * + * irc logging: + * irc.on('data', function (data) { + * irc.realtime(data); + * } + * + * send a message to chat: + * irc.msg('Hi chat!'); + * irc.msg('Hi chat!', {caller: 'SupremoRTD', auth: 1}); + * + * get user name: + * irc.caller(data[0]); + */ + +var net = require('net'), + events = require('events'), + file = require('fs'), + https = require('https'), + utils = require('./utils.js'); + +//-------- Construct --------- +function IRC(options) { + var __self = this; + + __self.options = options || {pass: 'irc_bot', name: 'irc_bot', channel: 'irc_bot'}; + + __self.config = { + // twitch bot info + pass : __self.options.pass, + name : __self.options.name, + nick : 'irc_bot', + broadcaster : __self.options.channel.slice(1).charAt(0).toUpperCase() + __self.options.channel.slice(2).toLowerCase(), + // twitch server + addr : '199.9.250.229', //__self.options.name.toLowerCase() + '.jtvirc.com', + port : 6667, + channel : __self.options.channel.toLowerCase(), + encoding : 'ascii' + }; + + __self.mods = []; + __self.buffer = []; + + // message queue + __self.queue_timer = 2000; + __self.queue_messages = []; + __self.previous_message = ''; + + // irc logging + __self.check_streaming = 4;//minutes + __self.streaming = false; + __self.new_file = false; + __self.log = ''; +} + +IRC.prototype = new events.EventEmitter(); + +//-------- Methods -------- +IRC.prototype.start = function () { + var __self = this; + + // check stream status + function stream_status() { + var time = utils.make_interval(__self.check_streaming); + if (time === 0) { + https.get('https://api.twitch.tv/kraken/streams/' + __self.config.channel.slice(1), function (response) { + var body = ''; + + // put together response + response.on('data', function (chunk) { + body += chunk; + }); + + // log file creation + response.on('end', function () { + var json = JSON.parse(body); + __self.streaming = json.stream !== null; + if (__self.streaming && !__self.new_file) { + // prevent another file from being created while streaming + __self.new_file = true; + + // set stream time for file + var date = new Date(), + hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours(), + min = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes(), + sec = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds(), + streaming_time = ''; + + // create start time string + streaming_time += (date.getMonth().toString() + 1) + date.getDate().toString() + date.getFullYear().toString(); + streaming_time += hours.toString() + min.toString() + sec.toString(); + + // create new log file + __self.log = './../logs/' + __self.config.channel.slice(1) + '_' + streaming_time.toString() + '.txt'; + file.open(__self.log, 'w'); + } else if (!__self.streaming) { + __self.new_file = false; + } + setTimeout(stream_status, 1000); + }); + }); + } else { + setTimeout(stream_status, time); + } + } + + stream_status(); + __self.connect(); + __self.monitor_queue(); +}; + +IRC.prototype.realtime = function (data){ + var __self = this; + + // only log irc data if streaming + if (__self.streaming) { + // check if file exists, if it does append + file.exists(__self.log, function (exists) { + if (exists) { + file.appendFile(__self.log, data + '\r\n', 'utf-8', function (err) { + if (err) { + throw err; + } + }); + } + }); + } +}; + +IRC.prototype.connect = function () { + var __self = this; + + // create new socket + __self.socket = new net.Socket(); + __self.socket.setEncoding(__self.config.encoding); + __self.socket.setNoDelay(); + __self.socket.connect(__self.config.port, __self.config.addr); + + // connect to twitch irc via socket + __self.socket.on('connect', function () { + __self.emit('data', 'RECV - Established connection to Twitch IRC, registering user...'); + __self.raw('PASS ' + __self.config.pass, true); + __self.raw('NICK ' + __self.config.name); + __self.raw('USER ' + __self.config.nick + ' ' + __self.config.nick + '.com ' + __self.config.nick + ' :' + __self.config.name); + }); + + // handle incoming socket data + __self.socket.on('data', function (data) { + var prepack, lines, line, word; + prepack = data.replace('\r\n', '\n'); + __self.buffer += prepack; + lines = __self.buffer.split('\n'); + __self.buffer = ''; + + if (lines[lines.length - 1] !== '') { + __self.buffer = lines[lines.length - 1]; + } + + lines = lines.splice(0, lines.length - 1); + for (var i = 0; i < lines.length; i++) { + line = lines[i].replace('\r',''); + word = line.replace('\r', '').split(' '); + __self.emit('data', 'RECV - ' + line); + __self.join(word); + __self.pingpong(word); + __self.moderators(word); + if (word[3] !== undefined) { + __self.emit('command', word); + } + } + }); + + __self.socket.on('error', function(){ + __self.reconnect(); + }); +}; + +// reconnect to socket +IRC.prototype.reconnect = function () { + var __self = this; + + // send quit to server, destroy socket connection, + // clear socket variable and then reconnect + __self.socket.end('QUIT\r\n'); + __self.socket.destroy(); + __self.socket = null; + __self.connect(); +}; + +// join channel +IRC.prototype.join = function (data) { + var __self = this; + + if (data[3] === ':End') { + __self.raw('JOIN ' + __self.config.channel); + } +}; + +// ping / pong +IRC.prototype.pingpong = function (data) { + var __self = this; + + if (data[0] === 'PING') { + __self.raw('PONG ' + data[1]); + } +}; + +// store / remove mods +IRC.prototype.moderators = function (data) { + var __self = this; + + if (data[1] === 'MODE') { + if (data[4] !== undefined) { + var user = data[4].charAt(0).toUpperCase() + data[4].slice(1); + switch (data[3]) { + case '+o': + if (__self.mods.indexOf(user) < 0) { + __self.mods.push(user); + } + break; + case '-o': + if (__self.mods.indexOf(user) >= 0) { + __self.mods.splice(__self.mods.indexOf(user), 1); + } + break; + } + } + } +}; + +// output to socket / console +IRC.prototype.raw = function (data, hide) { + var __self = this; + + __self.socket.write(data + '\r\n', __self.config.encoding, function (){ + if (!hide) { + // monitor commands sent by the bot + // and push them to command action + var parse = data.split(' '); + if (parse[0] === 'PRIVMSG') { + parse = __self.options.name + ' ' + data; + __self.emit('command', parse.split(' ')); + __self.emit('data', 'SENT - ' + __self.options.name + ' ' + data); + } else { + // output response + __self.emit('data', 'SENT - ' + data); + } + } + }); +}; + +// who sent message +IRC.prototype.caller = function (data) { + var caller = data.split('!'); + + return caller[0].charAt(1).toUpperCase() + caller[0].slice(2); +}; + +// send message to twitch chat +IRC.prototype.msg = function (msg, options) { + var __self = this, opts = options || {caller:null, auth:0}; + + switch (opts.auth) { + case 0: + __self.raw('PRIVMSG ' + __self.config.channel + ' :' + msg); + break; + case 1: + if (__self.mods.indexOf(opts.caller) >= 0) { + __self.raw('PRIVMSG ' + __self.config.channel + ' :' + msg); + } + break; + } +}; + +// message queue +IRC.prototype.queue = function(msg) { + var __self = this; + __self.queue_messages.push(msg); +}; + +IRC.prototype.monitor_queue = function() { + var __self = this, prepend_text = ['>', '+']; + + // handle messages in queue + function handle_queue() { + if (__self.queue_messages.length > 0) { + var message = __self.queue_messages[0].message, + options = __self.queue_messages[0].options, + timer = __self.queue_messages[0].timer || __self.queue_timer; + + // change message if it's the same as the previous message + if (message === __self.previous_message) { + for (var i = 0; i < prepend_text.length; i++) { + if (prepend_text[i] !== message.charAt(0)) { + message = prepend_text[i] + message.slice(1); + __self.previous_message = message; + break; + } + } + } else { + __self.previous_message = __self.queue_messages[0].message; + } + + // remove message from queue + __self.queue_messages.splice(0, 1); + + // output message to chat + setTimeout(function() { + if (options === null) { + __self.msg(message); + } else { + __self.msg(message, options); + } + // recheck the queue + setTimeout(handle_queue, 500); + }, timer); + } else { + setTimeout(handle_queue, 500); + } + } + handle_queue(); +}; + +module.exports = function (options) { + return new IRC(options); +}; \ No newline at end of file diff --git a/lib/mysql.js b/lib/mysql.js new file mode 100644 index 0000000..fdc3377 --- /dev/null +++ b/lib/mysql.js @@ -0,0 +1,81 @@ +/** + * api: + * DB([required options]) + * required options - {host, user, password, database} + * + * example: + * var db = require('./lib/plugins/db.js')({ + * host : 'localhost', + * user : 'user', + * password : 'password', + * database : 'database', + * }); + */ + +var mysql = require('mysql'), + file = require('fs'); + +//-------- Construct --------- +function DB(options) { + var __self = this; + + // config + __self.host = options.host || ''; + __self.user = options.user || ''; + __self.password = options.password || ''; + __self.database = options.database || ''; +} + +//-------- Methods --------- +DB.prototype.start = function() { + var __self = this, commands ='', viewers = ''; + + // table structure for table commands + commands += 'CREATE TABLE IF NOT EXISTS `commands` ('; + commands += '`id` int(11) NOT NULL AUTO_INCREMENT,'; + commands += '`command` text COLLATE utf8_unicode_ci NOT NULL,'; + commands += '`text` longtext COLLATE utf8_unicode_ci NOT NULL,'; + commands += '`auth` int(11) NOT NULL DEFAULT \'1\','; + commands += 'PRIMARY KEY (`id`)'; + commands += ') ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1'; + + // table structure for table viewers + viewers += 'CREATE TABLE IF NOT EXISTS `viewers` ('; + viewers += '`user` varchar(64) COLLATE utf8_unicode_ci NOT NULL,'; + viewers += '`points` int(11) NOT NULL,'; + viewers += 'PRIMARY KEY (`user`)'; + viewers += ') ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;'; + + // execute sql, create tables if they don't exist + __self.execute(commands + '; ' + viewers, function(){}); +}; + +DB.prototype.execute = function(sql, callback) { + var __self = this, + connection = mysql.createConnection({ + host : __self.host, + user : __self.user, + password : __self.password, + database : __self.database, + multipleStatements : true + }); + + // execute query + connection.query(sql, function (err, rows, fields) { + // error handling + if (err) { + file.appendFile('./../logs/error-log.txt', err.message + '\r\n' + err.stack + '\r\n', function() {}); + return; + } + + // close connection + connection.end(); + + // return results + callback(rows, fields); + }); +}; + +module.exports = function (options) { + return new DB(options); +}; \ No newline at end of file diff --git a/lib/utils.js b/lib/utils.js new file mode 100644 index 0000000..0c6b80a --- /dev/null +++ b/lib/utils.js @@ -0,0 +1,15 @@ +exports.selectRandomArrayItem = function(array){ + return array[Math.floor(Math.random() * array.length)]; +}; + +exports.max = function (value) { + return Math.max.apply(Math, value.map(function (o) {return o.bid;})); +}; + +// adjusts setTimeout time so it's in sync with actual time intervals +// e.g. 5 minutes intervals will happen at 10:05/10:10/10:15 regardless of start time +// TODO: add the ability to set the interval in seconds / minutes / hours +exports.make_interval = function (interval) { + var d = new Date(), min = d.getMinutes(), sec = d.getSeconds(); + return min % interval === 0 && sec === 0 ? 0 : ((60 * (interval - (min % interval))) - sec) * 1000; +}; \ No newline at end of file diff --git a/node_modules/mustache/.jshintrc b/node_modules/mustache/.jshintrc new file mode 100644 index 0000000..28dff71 --- /dev/null +++ b/node_modules/mustache/.jshintrc @@ -0,0 +1,5 @@ +{ + "eqnull": true, + "evil": true +} + diff --git a/node_modules/mustache/.npmignore b/node_modules/mustache/.npmignore new file mode 100644 index 0000000..9000776 --- /dev/null +++ b/node_modules/mustache/.npmignore @@ -0,0 +1,10 @@ +.DS_Store +.rvmrc +node_modules +runner.js +jquery.mustache.js +qooxdoo.mustache.js +dojox +yui3 +requirejs.mustache.js + diff --git a/node_modules/mustache/.travis.yml b/node_modules/mustache/.travis.yml new file mode 100644 index 0000000..3d839b0 --- /dev/null +++ b/node_modules/mustache/.travis.yml @@ -0,0 +1,4 @@ +language: node_js +node_js: + - 0.6 + diff --git a/node_modules/mustache/CHANGES b/node_modules/mustache/CHANGES new file mode 100644 index 0000000..b5b04f4 --- /dev/null +++ b/node_modules/mustache/CHANGES @@ -0,0 +1,34 @@ += 0.7.2 / 27 Dec 2012 + + * Fixed a rendering bug (#274) when using nested higher-order sections. + * Converted tests to use mocha instead of vows. + += 0.7.1 / 6 Dec 2012 + + * Handle empty templates gracefully. Fixes #265, #267, and #270. + * Cache partials by template, not by name. Fixes #257. + * Added Mustache.compileTokens to compile the output of Mustache.parse. Fixes + #258. + += 0.7.0 / 10 Sep 2012 + + * Rename Renderer => Writer. + * Allow partials to be loaded dynamically using a callback (thanks + @TiddoLangerak for the suggestion). + * Fixed a bug with higher-order sections that prevented them from being + passed the raw text of the section from the original template. + * More concise token format. Tokens also include start/end indices in the + original template. + * High-level API is consistent with the Writer API. + * Allow partials to be passed to the pre-compiled function (thanks + @fallenice). + * Don't use eval (thanks @cweider). + += 0.6.0 / 31 Aug 2012 + + * Use JavaScript's definition of falsy when determining whether to render an + inverted section or not. Issue #186. + * Use Mustache.escape to escape values inside {{}}. This function may be + reassigned to alter the default escaping behavior. Issue #244. + * Fixed a bug that clashed with QUnit (thanks @kannix). + * Added volo support (thanks @guybedford). diff --git a/node_modules/mustache/LICENSE b/node_modules/mustache/LICENSE new file mode 100644 index 0000000..6626848 --- /dev/null +++ b/node_modules/mustache/LICENSE @@ -0,0 +1,10 @@ +The MIT License + +Copyright (c) 2009 Chris Wanstrath (Ruby) +Copyright (c) 2010 Jan Lehnardt (JavaScript) + +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. diff --git a/node_modules/mustache/README.md b/node_modules/mustache/README.md new file mode 100644 index 0000000..d4b6ad0 --- /dev/null +++ b/node_modules/mustache/README.md @@ -0,0 +1,400 @@ +# mustache.js - Logic-less {{mustache}} templates with JavaScript + +> What could be more logical awesome than no logic at all? + +[mustache.js](http://github.com/janl/mustache.js) is an implementation of the [mustache](http://mustache.github.com/) template system in JavaScript. + +[Mustache](http://mustache.github.com/) is a logic-less template syntax. It can be used for HTML, config files, source code - anything. It works by expanding tags in a template using values provided in a hash or object. + +We call it "logic-less" because there are no if statements, else clauses, or for loops. Instead there are only tags. Some tags are replaced with a value, some nothing, and others a series of values. + +For a language-agnostic overview of mustache's template syntax, see the `mustache(5)` [manpage](http://mustache.github.com/mustache.5.html). + +## Where to use mustache.js? + +You can use mustache.js to render mustache templates anywhere you can use JavaScript. This includes web browsers, server-side environments such as [node](http://nodejs.org/), and [CouchDB](http://couchdb.apache.org/) views. + +mustache.js ships with support for both the [CommonJS](http://www.commonjs.org/) module API and the [Asynchronous Module Definition](https://github.com/amdjs/amdjs-api/wiki/AMD) API, or AMD. + +## Who uses mustache.js? + +An updated list of mustache.js users is kept [on the Github wiki](http://wiki.github.com/janl/mustache.js/beard-competition). Add yourself or your company if you use mustache.js! + +## Usage + +Below is quick example how to use mustache.js: + + var view = { + title: "Joe", + calc: function () { + return 2 + 4; + } + }; + + var output = Mustache.render("{{title}} spends {{calc}}", view); + +In this example, the `Mustache.render` function takes two parameters: 1) the [mustache](http://mustache.github.com/) template and 2) a `view` object that contains the data and code needed to render the template. + +## Templates + +A [mustache](http://mustache.github.com/) template is a string that contains any number of mustache tags. Tags are indicated by the double mustaches that surround them. `{{person}}` is a tag, as is `{{#person}}`. In both examples we refer to `person` as the tag's key. + +There are several types of tags available in mustache.js. + +### Variables + +The most basic tag type is a simple variable. A `{{name}}` tag renders the value of the `name` key in the current context. If there is no such key, nothing is rendered. + +All variables are HTML-escaped by default. If you want to render unescaped HTML, use the triple mustache: `{{{name}}}`. You can also use `&` to unescape a variable. + +View: + + { + "name": "Chris", + "company": "GitHub" + } + +Template: + + * {{name}} + * {{age}} + * {{company}} + * {{{company}}} + * {{&company}} + +Output: + + * Chris + * + * <b>GitHub</b> + * GitHub + * GitHub + +JavaScript's dot notation may be used to access keys that are properties of objects in a view. + +View: + + { + "name": { + "first": "Michael", + "last": "Jackson" + }, + "age": "RIP" + } + +Template: + + * {{name.first}} {{name.last}} + * {{age}} + +Output: + + * Michael Jackson + * RIP + +### Sections + +Sections render blocks of text one or more times, depending on the value of the key in the current context. + +A section begins with a pound and ends with a slash. That is, `{{#person}}` begins a `person` section, while `{{/person}}` ends it. The text between the two tags is referred to as that section's "block". + +The behavior of the section is determined by the value of the key. + +#### False Values or Empty Lists + +If the `person` key does not exist, or exists and has a value of `null`, `undefined`, or `false`, or is an empty list, the block will not be rendered. + +View: + + { + "person": false + } + +Template: + + Shown. + {{#person}} + Never shown! + {{/person}} + +Output: + + Shown. + +#### Non-Empty Lists + +If the `person` key exists and is not `null`, `undefined`, or `false`, and is not an empty list the block will be rendered one or more times. + +When the value is a list, the block is rendered once for each item in the list. The context of the block is set to the current item in the list for each iteration. In this way we can loop over collections. + +View: + + { + "stooges": [ + { "name": "Moe" }, + { "name": "Larry" }, + { "name": "Curly" } + ] + } + +Template: + + {{#stooges}} + {{name}} + {{/stooges}} + +Output: + + Moe + Larry + Curly + +When looping over an array of strings, a `.` can be used to refer to the current item in the list. + +View: + + { + "musketeers": ["Athos", "Aramis", "Porthos", "D'Artagnan"] + } + +Template: + + {{#musketeers}} + * {{.}} + {{/musketeers}} + +Output: + + * Athos + * Aramis + * Porthos + * D'Artagnan + +If the value of a section variable is a function, it will be called in the context of the current item in the list on each iteration. + +View: + + { + "beatles": [ + { "firstName": "John", "lastName": "Lennon" }, + { "firstName": "Paul", "lastName": "McCartney" }, + { "firstName": "George", "lastName": "Harrison" }, + { "firstName": "Ringo", "lastName": "Starr" } + ], + "name": function () { + return this.firstName + " " + this.lastName; + } + } + +Template: + + {{#beatles}} + * {{name}} + {{/beatles}} + +Output: + + * John Lennon + * Paul McCartney + * George Harrison + * Ringo Starr + +#### Functions + +If the value of a section key is a function, it is called with the section's literal block of text, un-rendered, as its first argument. The second argument is a special rendering function that uses the current view as its view argument. It is called in the context of the current view object. + +View: + + { + "name": "Tater", + "bold": function () { + return function (text, render) { + return "" + render(text) + ""; + } + } + } + +Template: + + {{#bold}}Hi {{name}}.{{/bold}} + +Output: + + Hi Tater. + +### Inverted Sections + +An inverted section opens with `{{^section}}` instead of `{{#section}}`. The block of an inverted section is rendered only if the value of that section's tag is `null`, `undefined`, `false`, or an empty list. + +View: + + { + "repos": [] + } + +Template: + + {{#repos}}{{name}}{{/repos}} + {{^repos}}No repos :({{/repos}} + +Output: + + No repos :( + +### Comments + +Comments begin with a bang and are ignored. The following template: + +

      Today{{! ignore me }}.

      + +Will render as follows: + +

      Today.

      + +Comments may contain newlines. + +### Partials + +Partials begin with a greater than sign, like {{> box}}. + +Partials are rendered at runtime (as opposed to compile time), so recursive partials are possible. Just avoid infinite loops. + +They also inherit the calling context. Whereas in ERB you may have this: + + <%= partial :next_more, :start => start, :size => size %> + +Mustache requires only this: + + {{> next_more}} + +Why? Because the `next_more.mustache` file will inherit the `size` and `start` variables from the calling context. In this way you may want to think of partials as includes, or template expansion, even though it's not literally true. + +For example, this template and partial: + + base.mustache: +

      Names

      + {{#names}} + {{> user}} + {{/names}} + + user.mustache: + {{name}} + +Can be thought of as a single, expanded template: + +

      Names

      + {{#names}} + {{name}} + {{/names}} + +In mustache.js an object of partials may be passed as the third argument to `Mustache.render`. The object should be keyed by the name of the partial, and its value should be the partial text. + +### Set Delimiter + +Set Delimiter tags start with an equals sign and change the tag delimiters from `{{` and `}}` to custom strings. + +Consider the following contrived example: + + * {{ default_tags }} + {{=<% %>=}} + * <% erb_style_tags %> + <%={{ }}=%> + * {{ default_tags_again }} + +Here we have a list with three items. The first item uses the default tag style, the second uses ERB style as defined by the Set Delimiter tag, and the third returns to the default style after yet another Set Delimiter declaration. + +According to [ctemplates](http://google-ctemplate.googlecode.com/svn/trunk/doc/howto.html), this "is useful for languages like TeX, where double-braces may occur in the text and are awkward to use for markup." + +Custom delimiters may not contain whitespace or the equals sign. + +### Compiled Templates + +Mustache templates can be compiled into JavaScript functions using `Mustache.compile` for improved rendering performance. + +If you have template views that are rendered multiple times, compiling your template into a JavaScript function will minimise the amount of work required for each re-render. + +Pre-compiled templates can also be generated server-side, for delivery to the browser as ready to use JavaScript functions, further reducing the amount of client side processing required for initialising templates. + +**Mustache.compile** + +Use `Mustache.compile` to compile standard Mustache string templates into reusable Mustache template functions. + + var compiledTemplate = Mustache.compile(stringTemplate); + +The function returned from `Mustache.compile` can then be called directly, passing in the template data as an argument (with an object of partials as an optional second parameter), to generate the final output. + + var templateOutput = compiledTemplate(templateData); + +**Mustache.compilePartial** + +Template partials can also be compiled using the `Mustache.compilePartial` function. The first parameter of this function, is the name of the partial as it appears within parent templates. + + Mustache.compilePartial('partial-name', stringTemplate); + +Compiled partials are then available to both `Mustache.render` and `Mustache.compile`. + +## Plugins for JavaScript Libraries + +mustache.js may be built specifically for several different client libraries, including the following: + + - [jQuery](http://jquery.com/) + - [MooTools](http://mootools.net/) + - [Dojo](http://www.dojotoolkit.org/) + - [YUI](http://developer.yahoo.com/yui/) + - [qooxdoo](http://qooxdoo.org/) + +These may be built using [Rake](http://rake.rubyforge.org/) and one of the following commands: + + $ rake jquery + $ rake mootools + $ rake dojo + $ rake yui + $ rake qooxdoo + +## Testing + +The mustache.js test suite uses the [vows](http://vowsjs.org/) testing framework. In order to run the tests you'll need to install [node](http://nodejs.org/). Once that's done you can install vows using [npm](http://npmjs.org/). + + $ npm install -g vows + +Then run the tests. + + $ vows --spec + +The test suite consists of both unit and integration tests. If a template isn't rendering correctly for you, you can make a test for it by doing the following: + + 1. Create a template file named `mytest.mustache` in the `test/_files` + directory. Replace `mytest` with the name of your test. + 2. Create a corresponding view file named `mytest.js` in the same directory. + This file should contain a JavaScript object literal enclosed in + parentheses. See any of the other view files for an example. + 3. Create a file with the expected output in `mytest.txt` in the same + directory. + +Then, you can run the test with: + + $ TEST=mytest vows test/render_test.js + +## Thanks + +mustache.js wouldn't kick ass if it weren't for these fine souls: + + * Chris Wanstrath / defunkt + * Alexander Lang / langalex + * Sebastian Cohnen / tisba + * J Chris Anderson / jchris + * Tom Robinson / tlrobinson + * Aaron Quint / quirkey + * Douglas Crockford + * Nikita Vasilyev / NV + * Elise Wood / glytch + * Damien Mathieu / dmathieu + * Jakub Kuźma / qoobaa + * Will Leinweber / will + * dpree + * Jason Smith / jhs + * Aaron Gibralter / agibralter + * Ross Boucher / boucher + * Matt Sanford / mzsanford + * Ben Cherry / bcherry + * Michael Jackson / mjijackson diff --git a/node_modules/mustache/Rakefile b/node_modules/mustache/Rakefile new file mode 100644 index 0000000..bc32175 --- /dev/null +++ b/node_modules/mustache/Rakefile @@ -0,0 +1,68 @@ +require 'rake' +require 'rake/clean' + +task :default => :test + +ROOT = File.expand_path('..', __FILE__) +MUSTACHE_JS = File.read(File.join(ROOT, 'mustache.js')) + +def mustache_version + match = MUSTACHE_JS.match(/exports\.version = "([^"]+)";/) + match[1] +end + +def minified_file + ENV['FILE'] || 'mustache.min.js' +end + +desc "Run all tests, requires vows (see http://vowsjs.org)" +task :test do + sh "vows --spec" +end + +desc "Minify to #{minified_file}, requires UglifyJS (see http://marijnhaverbeke.nl/uglifyjs)" +task :minify do + sh "uglifyjs mustache.js > #{minified_file}" +end + +desc "Run JSHint, requires jshint (see http://www.jshint.com)" +task :lint do + sh "jshint mustache.js" +end + +# Creates a task that uses the various template wrappers to make a wrapped +# output file. There is some extra complexity because Dojo and YUI use +# different final locations. +def templated_build(name, opts={}) + short = name.downcase + source = File.join("wrappers", short) + dependencies = ["mustache.js"] + Dir.glob("#{source}/*.tpl.*") + target_js = opts[:location] ? "mustache.js" : "#{short}.mustache.js" + + CLEAN.include(opts[:location] ? opts[:location] : target_js) + + desc "Package for #{name}" + task short.to_sym => dependencies do + puts "Packaging for #{name}" + + mkdir_p opts[:location] if opts[:location] + + files = [ + "#{source}/mustache.js.pre", + 'mustache.js', + "#{source}/mustache.js.post" + ] + + open("#{opts[:location] || '.'}/#{target_js}", 'w') do |f| + files.each {|file| f << File.read(file) } + end + + puts "Done, see #{opts[:location] || '.'}/#{target_js}" + end +end + +templated_build "jQuery" +templated_build "MooTools" +templated_build "Dojo", :location => "dojox/string" +templated_build "YUI3", :location => "yui3/mustache" +templated_build "qooxdoo" diff --git a/node_modules/mustache/mustache.js b/node_modules/mustache/mustache.js new file mode 100644 index 0000000..932052b --- /dev/null +++ b/node_modules/mustache/mustache.js @@ -0,0 +1,610 @@ +/*! + * mustache.js - Logic-less {{mustache}} templates with JavaScript + * http://github.com/janl/mustache.js + */ + +/*global define: false*/ + +(function (root, factory) { + if (typeof exports === "object" && exports) { + module.exports = factory; // CommonJS + } else if (typeof define === "function" && define.amd) { + define(factory); // AMD + } else { + root.Mustache = factory; // diff --git a/node_modules/mustache/test/_files/backslashes.txt b/node_modules/mustache/test/_files/backslashes.txt new file mode 100644 index 0000000..038dd37 --- /dev/null +++ b/node_modules/mustache/test/_files/backslashes.txt @@ -0,0 +1,7 @@ +* \abc +* \abc +* \abc + diff --git a/node_modules/mustache/test/_files/bug_11_eating_whitespace.js b/node_modules/mustache/test/_files/bug_11_eating_whitespace.js new file mode 100644 index 0000000..e41ccd1 --- /dev/null +++ b/node_modules/mustache/test/_files/bug_11_eating_whitespace.js @@ -0,0 +1,3 @@ +({ + tag: "yo" +}) diff --git a/node_modules/mustache/test/_files/bug_11_eating_whitespace.mustache b/node_modules/mustache/test/_files/bug_11_eating_whitespace.mustache new file mode 100644 index 0000000..8d5cd92 --- /dev/null +++ b/node_modules/mustache/test/_files/bug_11_eating_whitespace.mustache @@ -0,0 +1 @@ +{{tag}} foo diff --git a/node_modules/mustache/test/_files/bug_11_eating_whitespace.txt b/node_modules/mustache/test/_files/bug_11_eating_whitespace.txt new file mode 100644 index 0000000..f5bbc85 --- /dev/null +++ b/node_modules/mustache/test/_files/bug_11_eating_whitespace.txt @@ -0,0 +1 @@ +yo foo diff --git a/node_modules/mustache/test/_files/changing_delimiters.js b/node_modules/mustache/test/_files/changing_delimiters.js new file mode 100644 index 0000000..b808f4c --- /dev/null +++ b/node_modules/mustache/test/_files/changing_delimiters.js @@ -0,0 +1,4 @@ +({ + "foo": "foooooooooooooo", + "bar": "bar!" +}) diff --git a/node_modules/mustache/test/_files/changing_delimiters.mustache b/node_modules/mustache/test/_files/changing_delimiters.mustache new file mode 100644 index 0000000..0cd044c --- /dev/null +++ b/node_modules/mustache/test/_files/changing_delimiters.mustache @@ -0,0 +1 @@ +{{=<% %>=}}<% foo %> {{foo}} <%{bar}%> {{{bar}}} diff --git a/node_modules/mustache/test/_files/changing_delimiters.txt b/node_modules/mustache/test/_files/changing_delimiters.txt new file mode 100644 index 0000000..1b1510d --- /dev/null +++ b/node_modules/mustache/test/_files/changing_delimiters.txt @@ -0,0 +1 @@ +foooooooooooooo {{foo}} bar! {{{bar}}} diff --git a/node_modules/mustache/test/_files/check_falsy.js b/node_modules/mustache/test/_files/check_falsy.js new file mode 100644 index 0000000..5a599ca --- /dev/null +++ b/node_modules/mustache/test/_files/check_falsy.js @@ -0,0 +1,7 @@ +({ + number: function(text, render) { + return function(text, render) { + return +render(text); + } + } +}) diff --git a/node_modules/mustache/test/_files/check_falsy.mustache b/node_modules/mustache/test/_files/check_falsy.mustache new file mode 100644 index 0000000..30e2547 --- /dev/null +++ b/node_modules/mustache/test/_files/check_falsy.mustache @@ -0,0 +1 @@ +

      {{#number}}0{{/number}}

      diff --git a/node_modules/mustache/test/_files/check_falsy.txt b/node_modules/mustache/test/_files/check_falsy.txt new file mode 100644 index 0000000..3bb2f51 --- /dev/null +++ b/node_modules/mustache/test/_files/check_falsy.txt @@ -0,0 +1 @@ +

      0

      diff --git a/node_modules/mustache/test/_files/comments.js b/node_modules/mustache/test/_files/comments.js new file mode 100644 index 0000000..f20b8b1 --- /dev/null +++ b/node_modules/mustache/test/_files/comments.js @@ -0,0 +1,5 @@ +({ + title: function () { + return "A Comedy of Errors"; + } +}) diff --git a/node_modules/mustache/test/_files/comments.mustache b/node_modules/mustache/test/_files/comments.mustache new file mode 100644 index 0000000..5036801 --- /dev/null +++ b/node_modules/mustache/test/_files/comments.mustache @@ -0,0 +1 @@ +

      {{title}}{{! just something interesting... or not... }}

      diff --git a/node_modules/mustache/test/_files/comments.txt b/node_modules/mustache/test/_files/comments.txt new file mode 100644 index 0000000..0133517 --- /dev/null +++ b/node_modules/mustache/test/_files/comments.txt @@ -0,0 +1 @@ +

      A Comedy of Errors

      diff --git a/node_modules/mustache/test/_files/complex.js b/node_modules/mustache/test/_files/complex.js new file mode 100644 index 0000000..68a4809 --- /dev/null +++ b/node_modules/mustache/test/_files/complex.js @@ -0,0 +1,19 @@ +({ + header: function () { + return "Colors"; + }, + item: [ + {name: "red", current: true, url: "#Red"}, + {name: "green", current: false, url: "#Green"}, + {name: "blue", current: false, url: "#Blue"} + ], + link: function () { + return this["current"] !== true; + }, + list: function () { + return this.item.length !== 0; + }, + empty: function () { + return this.item.length === 0; + } +}) diff --git a/node_modules/mustache/test/_files/complex.mustache b/node_modules/mustache/test/_files/complex.mustache new file mode 100644 index 0000000..869a4f0 --- /dev/null +++ b/node_modules/mustache/test/_files/complex.mustache @@ -0,0 +1,16 @@ +

      {{header}}

      +{{#list}} +
        + {{#item}} + {{#current}} +
      • {{name}}
      • + {{/current}} + {{#link}} +
      • {{name}}
      • + {{/link}} + {{/item}} +
      +{{/list}} +{{#empty}} +

      The list is empty.

      +{{/empty}} diff --git a/node_modules/mustache/test/_files/complex.txt b/node_modules/mustache/test/_files/complex.txt new file mode 100644 index 0000000..596d3f6 --- /dev/null +++ b/node_modules/mustache/test/_files/complex.txt @@ -0,0 +1,6 @@ +

      Colors

      + diff --git a/node_modules/mustache/test/_files/context_lookup.js b/node_modules/mustache/test/_files/context_lookup.js new file mode 100644 index 0000000..8ce6299 --- /dev/null +++ b/node_modules/mustache/test/_files/context_lookup.js @@ -0,0 +1,8 @@ +({ + "outer": { + "id": 1, + "second": { + "nothing": 2 + } + } +}) diff --git a/node_modules/mustache/test/_files/context_lookup.mustache b/node_modules/mustache/test/_files/context_lookup.mustache new file mode 100644 index 0000000..3c7b767 --- /dev/null +++ b/node_modules/mustache/test/_files/context_lookup.mustache @@ -0,0 +1 @@ +{{#outer}}{{#second}}{{id}}{{/second}}{{/outer}} diff --git a/node_modules/mustache/test/_files/context_lookup.txt b/node_modules/mustache/test/_files/context_lookup.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/node_modules/mustache/test/_files/context_lookup.txt @@ -0,0 +1 @@ +1 diff --git a/node_modules/mustache/test/_files/delimiters.js b/node_modules/mustache/test/_files/delimiters.js new file mode 100644 index 0000000..365d01e --- /dev/null +++ b/node_modules/mustache/test/_files/delimiters.js @@ -0,0 +1,6 @@ +({ + first: "It worked the first time.", + second: "And it worked the second time.", + third: "Then, surprisingly, it worked the third time.", + fourth: "Fourth time also fine!." +}) diff --git a/node_modules/mustache/test/_files/delimiters.mustache b/node_modules/mustache/test/_files/delimiters.mustache new file mode 100644 index 0000000..7fac846 --- /dev/null +++ b/node_modules/mustache/test/_files/delimiters.mustache @@ -0,0 +1,7 @@ +{{=<% %>=}}* +<% first %> +* <% second %> +<%=| |=%> +* | third | +|={{ }}=| +* {{ fourth }} diff --git a/node_modules/mustache/test/_files/delimiters.txt b/node_modules/mustache/test/_files/delimiters.txt new file mode 100644 index 0000000..698a6bb --- /dev/null +++ b/node_modules/mustache/test/_files/delimiters.txt @@ -0,0 +1,5 @@ +* +It worked the first time. +* And it worked the second time. +* Then, surprisingly, it worked the third time. +* Fourth time also fine!. diff --git a/node_modules/mustache/test/_files/disappearing_whitespace.js b/node_modules/mustache/test/_files/disappearing_whitespace.js new file mode 100644 index 0000000..973dd1c --- /dev/null +++ b/node_modules/mustache/test/_files/disappearing_whitespace.js @@ -0,0 +1,4 @@ +({ + bedrooms: true, + total: 1 +}) diff --git a/node_modules/mustache/test/_files/disappearing_whitespace.mustache b/node_modules/mustache/test/_files/disappearing_whitespace.mustache new file mode 100644 index 0000000..16c16e0 --- /dev/null +++ b/node_modules/mustache/test/_files/disappearing_whitespace.mustache @@ -0,0 +1 @@ +{{#bedrooms}}{{total}}{{/bedrooms}} BED diff --git a/node_modules/mustache/test/_files/disappearing_whitespace.txt b/node_modules/mustache/test/_files/disappearing_whitespace.txt new file mode 100644 index 0000000..66e98ef --- /dev/null +++ b/node_modules/mustache/test/_files/disappearing_whitespace.txt @@ -0,0 +1 @@ +1 BED diff --git a/node_modules/mustache/test/_files/dot_notation.js b/node_modules/mustache/test/_files/dot_notation.js new file mode 100644 index 0000000..de06a03 --- /dev/null +++ b/node_modules/mustache/test/_files/dot_notation.js @@ -0,0 +1,23 @@ +({ + name: "A Book", + authors: ["John Power", "Jamie Walsh"], + price: { + value: 200, + vat: function () { + return this.value * 0.2; + }, + currency: { + symbol: '$', + name: 'USD' + } + }, + availability: { + status: true, + text: "In Stock" + }, + // And now, some truthy false values + truthy: { + zero: 0, + notTrue: false + } +}) diff --git a/node_modules/mustache/test/_files/dot_notation.mustache b/node_modules/mustache/test/_files/dot_notation.mustache new file mode 100644 index 0000000..f89d70b --- /dev/null +++ b/node_modules/mustache/test/_files/dot_notation.mustache @@ -0,0 +1,9 @@ + +

      {{name}}

      +

      Authors:

        {{#authors}}
      • {{.}}
      • {{/authors}}

      +

      Price: {{{price.currency.symbol}}}{{price.value}} {{#price.currency}}{{name}} {{availability.text}}{{/price.currency}}

      +

      VAT: {{{price.currency.symbol}}}{{#price}}{{vat}}{{/price}}

      + +

      Test truthy false values:

      +

      Zero: {{truthy.zero}}

      +

      False: {{truthy.notTrue}}

      diff --git a/node_modules/mustache/test/_files/dot_notation.txt b/node_modules/mustache/test/_files/dot_notation.txt new file mode 100644 index 0000000..08afa05 --- /dev/null +++ b/node_modules/mustache/test/_files/dot_notation.txt @@ -0,0 +1,9 @@ + +

      A Book

      +

      Authors:

      • John Power
      • Jamie Walsh

      +

      Price: $200 USD In Stock

      +

      VAT: $40

      + +

      Test truthy false values:

      +

      Zero: 0

      +

      False: false

      diff --git a/node_modules/mustache/test/_files/double_render.js b/node_modules/mustache/test/_files/double_render.js new file mode 100644 index 0000000..28acb2c --- /dev/null +++ b/node_modules/mustache/test/_files/double_render.js @@ -0,0 +1,5 @@ +({ + foo: true, + bar: "{{win}}", + win: "FAIL" +}) diff --git a/node_modules/mustache/test/_files/double_render.mustache b/node_modules/mustache/test/_files/double_render.mustache new file mode 100644 index 0000000..4500fd7 --- /dev/null +++ b/node_modules/mustache/test/_files/double_render.mustache @@ -0,0 +1 @@ +{{#foo}}{{bar}}{{/foo}} diff --git a/node_modules/mustache/test/_files/double_render.txt b/node_modules/mustache/test/_files/double_render.txt new file mode 100644 index 0000000..b6e652d --- /dev/null +++ b/node_modules/mustache/test/_files/double_render.txt @@ -0,0 +1 @@ +{{win}} diff --git a/node_modules/mustache/test/_files/empty_list.js b/node_modules/mustache/test/_files/empty_list.js new file mode 100644 index 0000000..c0e1159 --- /dev/null +++ b/node_modules/mustache/test/_files/empty_list.js @@ -0,0 +1,3 @@ +({ + jobs: [] +}) diff --git a/node_modules/mustache/test/_files/empty_list.mustache b/node_modules/mustache/test/_files/empty_list.mustache new file mode 100644 index 0000000..4fdf13d --- /dev/null +++ b/node_modules/mustache/test/_files/empty_list.mustache @@ -0,0 +1,4 @@ +These are the jobs: +{{#jobs}} +{{.}} +{{/jobs}} diff --git a/node_modules/mustache/test/_files/empty_list.txt b/node_modules/mustache/test/_files/empty_list.txt new file mode 100644 index 0000000..d9b4a67 --- /dev/null +++ b/node_modules/mustache/test/_files/empty_list.txt @@ -0,0 +1 @@ +These are the jobs: diff --git a/node_modules/mustache/test/_files/empty_sections.js b/node_modules/mustache/test/_files/empty_sections.js new file mode 100644 index 0000000..b4100a5 --- /dev/null +++ b/node_modules/mustache/test/_files/empty_sections.js @@ -0,0 +1 @@ +({}) diff --git a/node_modules/mustache/test/_files/empty_sections.mustache b/node_modules/mustache/test/_files/empty_sections.mustache new file mode 100644 index 0000000..b6065db --- /dev/null +++ b/node_modules/mustache/test/_files/empty_sections.mustache @@ -0,0 +1 @@ +{{#foo}}{{/foo}}foo{{#bar}}{{/bar}} diff --git a/node_modules/mustache/test/_files/empty_sections.txt b/node_modules/mustache/test/_files/empty_sections.txt new file mode 100644 index 0000000..257cc56 --- /dev/null +++ b/node_modules/mustache/test/_files/empty_sections.txt @@ -0,0 +1 @@ +foo diff --git a/node_modules/mustache/test/_files/empty_string.js b/node_modules/mustache/test/_files/empty_string.js new file mode 100644 index 0000000..be6e058 --- /dev/null +++ b/node_modules/mustache/test/_files/empty_string.js @@ -0,0 +1,6 @@ +({ + description: "That is all!", + child: { + description: "" + } +}) diff --git a/node_modules/mustache/test/_files/empty_string.mustache b/node_modules/mustache/test/_files/empty_string.mustache new file mode 100644 index 0000000..f568441 --- /dev/null +++ b/node_modules/mustache/test/_files/empty_string.mustache @@ -0,0 +1 @@ +{{description}}{{#child}}{{description}}{{/child}} diff --git a/node_modules/mustache/test/_files/empty_string.txt b/node_modules/mustache/test/_files/empty_string.txt new file mode 100644 index 0000000..22e2a6e --- /dev/null +++ b/node_modules/mustache/test/_files/empty_string.txt @@ -0,0 +1 @@ +That is all! diff --git a/node_modules/mustache/test/_files/empty_template.js b/node_modules/mustache/test/_files/empty_template.js new file mode 100644 index 0000000..b4100a5 --- /dev/null +++ b/node_modules/mustache/test/_files/empty_template.js @@ -0,0 +1 @@ +({}) diff --git a/node_modules/mustache/test/_files/empty_template.mustache b/node_modules/mustache/test/_files/empty_template.mustache new file mode 100644 index 0000000..bb2367a --- /dev/null +++ b/node_modules/mustache/test/_files/empty_template.mustache @@ -0,0 +1 @@ +

      Test

      \ No newline at end of file diff --git a/node_modules/mustache/test/_files/empty_template.txt b/node_modules/mustache/test/_files/empty_template.txt new file mode 100644 index 0000000..bb2367a --- /dev/null +++ b/node_modules/mustache/test/_files/empty_template.txt @@ -0,0 +1 @@ +

      Test

      \ No newline at end of file diff --git a/node_modules/mustache/test/_files/error_not_found.js b/node_modules/mustache/test/_files/error_not_found.js new file mode 100644 index 0000000..10e4709 --- /dev/null +++ b/node_modules/mustache/test/_files/error_not_found.js @@ -0,0 +1,3 @@ +({ + bar: 2 +}) diff --git a/node_modules/mustache/test/_files/error_not_found.mustache b/node_modules/mustache/test/_files/error_not_found.mustache new file mode 100644 index 0000000..24369f7 --- /dev/null +++ b/node_modules/mustache/test/_files/error_not_found.mustache @@ -0,0 +1 @@ +{{foo}} \ No newline at end of file diff --git a/node_modules/mustache/test/_files/error_not_found.txt b/node_modules/mustache/test/_files/error_not_found.txt new file mode 100644 index 0000000..e69de29 diff --git a/node_modules/mustache/test/_files/escaped.js b/node_modules/mustache/test/_files/escaped.js new file mode 100644 index 0000000..cd77c1f --- /dev/null +++ b/node_modules/mustache/test/_files/escaped.js @@ -0,0 +1,6 @@ +({ + title: function () { + return "Bear > Shark"; + }, + entities: "" \"'<>/" +}) diff --git a/node_modules/mustache/test/_files/escaped.mustache b/node_modules/mustache/test/_files/escaped.mustache new file mode 100644 index 0000000..93e800b --- /dev/null +++ b/node_modules/mustache/test/_files/escaped.mustache @@ -0,0 +1,2 @@ +

      {{title}}

      +And even {{entities}}, but not {{{entities}}}. diff --git a/node_modules/mustache/test/_files/escaped.txt b/node_modules/mustache/test/_files/escaped.txt new file mode 100644 index 0000000..c1527d5 --- /dev/null +++ b/node_modules/mustache/test/_files/escaped.txt @@ -0,0 +1,2 @@ +

      Bear > Shark

      +And even &quot; "'<>/, but not " "'<>/. diff --git a/node_modules/mustache/test/_files/falsy.js b/node_modules/mustache/test/_files/falsy.js new file mode 100644 index 0000000..ae9b9bf --- /dev/null +++ b/node_modules/mustache/test/_files/falsy.js @@ -0,0 +1,8 @@ +({ + "emptyString": "", + "emptyArray": [], + "zero": 0, + "null": null, + "undefined": undefined, + "NaN": 0/0 +}) \ No newline at end of file diff --git a/node_modules/mustache/test/_files/falsy.mustache b/node_modules/mustache/test/_files/falsy.mustache new file mode 100644 index 0000000..f3698da --- /dev/null +++ b/node_modules/mustache/test/_files/falsy.mustache @@ -0,0 +1,12 @@ +{{#emptyString}}empty string{{/emptyString}} +{{^emptyString}}inverted empty string{{/emptyString}} +{{#emptyArray}}empty array{{/emptyArray}} +{{^emptyArray}}inverted empty array{{/emptyArray}} +{{#zero}}zero{{/zero}} +{{^zero}}inverted zero{{/zero}} +{{#null}}null{{/null}} +{{^null}}inverted null{{/null}} +{{#undefined}}undefined{{/undefined}} +{{^undefined}}inverted undefined{{/undefined}} +{{#NaN}}NaN{{/NaN}} +{{^NaN}}inverted NaN{{/NaN}} diff --git a/node_modules/mustache/test/_files/falsy.txt b/node_modules/mustache/test/_files/falsy.txt new file mode 100644 index 0000000..9b7cde3 --- /dev/null +++ b/node_modules/mustache/test/_files/falsy.txt @@ -0,0 +1,12 @@ + +inverted empty string + +inverted empty array + +inverted zero + +inverted null + +inverted undefined + +inverted NaN diff --git a/node_modules/mustache/test/_files/grandparent_context.js b/node_modules/mustache/test/_files/grandparent_context.js new file mode 100644 index 0000000..97dbfd3 --- /dev/null +++ b/node_modules/mustache/test/_files/grandparent_context.js @@ -0,0 +1,19 @@ +({ + grand_parent_id: 'grand_parent1', + parent_contexts: [ + { + parent_id: 'parent1', + child_contexts: [ + { child_id: 'parent1-child1' }, + { child_id: 'parent1-child2' } + ] + }, + { + parent_id: 'parent2', + child_contexts: [ + { child_id: 'parent2-child1' }, + { child_id: 'parent2-child2' } + ] + } + ] +}) diff --git a/node_modules/mustache/test/_files/grandparent_context.mustache b/node_modules/mustache/test/_files/grandparent_context.mustache new file mode 100644 index 0000000..e6c07a2 --- /dev/null +++ b/node_modules/mustache/test/_files/grandparent_context.mustache @@ -0,0 +1,10 @@ +{{grand_parent_id}} +{{#parent_contexts}} +{{grand_parent_id}} +{{parent_id}} +{{#child_contexts}} +{{grand_parent_id}} +{{parent_id}} +{{child_id}} +{{/child_contexts}} +{{/parent_contexts}} diff --git a/node_modules/mustache/test/_files/grandparent_context.txt b/node_modules/mustache/test/_files/grandparent_context.txt new file mode 100644 index 0000000..64996ad --- /dev/null +++ b/node_modules/mustache/test/_files/grandparent_context.txt @@ -0,0 +1,17 @@ +grand_parent1 +grand_parent1 +parent1 +grand_parent1 +parent1 +parent1-child1 +grand_parent1 +parent1 +parent1-child2 +grand_parent1 +parent2 +grand_parent1 +parent2 +parent2-child1 +grand_parent1 +parent2 +parent2-child2 diff --git a/node_modules/mustache/test/_files/higher_order_sections.js b/node_modules/mustache/test/_files/higher_order_sections.js new file mode 100644 index 0000000..bacb0a4 --- /dev/null +++ b/node_modules/mustache/test/_files/higher_order_sections.js @@ -0,0 +1,9 @@ +({ + name: "Tater", + helper: "To tinker?", + bolder: function () { + return function (text, render) { + return text + ' => ' + render(text) + ' ' + this.helper; + } + } +}) diff --git a/node_modules/mustache/test/_files/higher_order_sections.mustache b/node_modules/mustache/test/_files/higher_order_sections.mustache new file mode 100644 index 0000000..04f5318 --- /dev/null +++ b/node_modules/mustache/test/_files/higher_order_sections.mustache @@ -0,0 +1 @@ +{{#bolder}}Hi {{name}}.{{/bolder}} diff --git a/node_modules/mustache/test/_files/higher_order_sections.txt b/node_modules/mustache/test/_files/higher_order_sections.txt new file mode 100644 index 0000000..be50ad7 --- /dev/null +++ b/node_modules/mustache/test/_files/higher_order_sections.txt @@ -0,0 +1 @@ +Hi {{name}}. => Hi Tater. To tinker? diff --git a/node_modules/mustache/test/_files/included_tag.js b/node_modules/mustache/test/_files/included_tag.js new file mode 100644 index 0000000..eb032a4 --- /dev/null +++ b/node_modules/mustache/test/_files/included_tag.js @@ -0,0 +1,3 @@ +({ + html: "I like {{mustache}}" +}) diff --git a/node_modules/mustache/test/_files/included_tag.mustache b/node_modules/mustache/test/_files/included_tag.mustache new file mode 100644 index 0000000..70631c2 --- /dev/null +++ b/node_modules/mustache/test/_files/included_tag.mustache @@ -0,0 +1 @@ +You said "{{{html}}}" today diff --git a/node_modules/mustache/test/_files/included_tag.txt b/node_modules/mustache/test/_files/included_tag.txt new file mode 100644 index 0000000..1af4556 --- /dev/null +++ b/node_modules/mustache/test/_files/included_tag.txt @@ -0,0 +1 @@ +You said "I like {{mustache}}" today diff --git a/node_modules/mustache/test/_files/inverted_section.js b/node_modules/mustache/test/_files/inverted_section.js new file mode 100644 index 0000000..f8f08fd --- /dev/null +++ b/node_modules/mustache/test/_files/inverted_section.js @@ -0,0 +1,3 @@ +({ + "repos": [] +}) diff --git a/node_modules/mustache/test/_files/inverted_section.mustache b/node_modules/mustache/test/_files/inverted_section.mustache new file mode 100644 index 0000000..b0a183b --- /dev/null +++ b/node_modules/mustache/test/_files/inverted_section.mustache @@ -0,0 +1,3 @@ +{{#repos}}{{name}}{{/repos}} +{{^repos}}No repos :({{/repos}} +{{^nothin}}Hello!{{/nothin}} diff --git a/node_modules/mustache/test/_files/inverted_section.txt b/node_modules/mustache/test/_files/inverted_section.txt new file mode 100644 index 0000000..b421582 --- /dev/null +++ b/node_modules/mustache/test/_files/inverted_section.txt @@ -0,0 +1,3 @@ + +No repos :( +Hello! diff --git a/node_modules/mustache/test/_files/keys_with_questionmarks.js b/node_modules/mustache/test/_files/keys_with_questionmarks.js new file mode 100644 index 0000000..becd631 --- /dev/null +++ b/node_modules/mustache/test/_files/keys_with_questionmarks.js @@ -0,0 +1,5 @@ +({ + "person?": { + name: "Jon" + } +}) diff --git a/node_modules/mustache/test/_files/keys_with_questionmarks.mustache b/node_modules/mustache/test/_files/keys_with_questionmarks.mustache new file mode 100644 index 0000000..417f17f --- /dev/null +++ b/node_modules/mustache/test/_files/keys_with_questionmarks.mustache @@ -0,0 +1,3 @@ +{{#person?}} + Hi {{name}}! +{{/person?}} diff --git a/node_modules/mustache/test/_files/keys_with_questionmarks.txt b/node_modules/mustache/test/_files/keys_with_questionmarks.txt new file mode 100644 index 0000000..0f69b94 --- /dev/null +++ b/node_modules/mustache/test/_files/keys_with_questionmarks.txt @@ -0,0 +1 @@ + Hi Jon! diff --git a/node_modules/mustache/test/_files/malicious_template.js b/node_modules/mustache/test/_files/malicious_template.js new file mode 100644 index 0000000..b4100a5 --- /dev/null +++ b/node_modules/mustache/test/_files/malicious_template.js @@ -0,0 +1 @@ +({}) diff --git a/node_modules/mustache/test/_files/malicious_template.mustache b/node_modules/mustache/test/_files/malicious_template.mustache new file mode 100644 index 0000000..b956867 --- /dev/null +++ b/node_modules/mustache/test/_files/malicious_template.mustache @@ -0,0 +1,5 @@ +{{"+(function () {throw "evil"})()+"}} +{{{"+(function () {throw "evil"})()+"}}} +{{> "+(function () {throw "evil"})()+"}} +{{# "+(function () {throw "evil"})()+"}} +{{/ "+(function () {throw "evil"})()+"}} diff --git a/node_modules/mustache/test/_files/malicious_template.txt b/node_modules/mustache/test/_files/malicious_template.txt new file mode 100644 index 0000000..139597f --- /dev/null +++ b/node_modules/mustache/test/_files/malicious_template.txt @@ -0,0 +1,2 @@ + + diff --git a/node_modules/mustache/test/_files/multiline_comment.js b/node_modules/mustache/test/_files/multiline_comment.js new file mode 100644 index 0000000..b4100a5 --- /dev/null +++ b/node_modules/mustache/test/_files/multiline_comment.js @@ -0,0 +1 @@ +({}) diff --git a/node_modules/mustache/test/_files/multiline_comment.mustache b/node_modules/mustache/test/_files/multiline_comment.mustache new file mode 100644 index 0000000..dff0893 --- /dev/null +++ b/node_modules/mustache/test/_files/multiline_comment.mustache @@ -0,0 +1,6 @@ +{{! + +This is a multi-line comment. + +}} +Hello world! diff --git a/node_modules/mustache/test/_files/multiline_comment.txt b/node_modules/mustache/test/_files/multiline_comment.txt new file mode 100644 index 0000000..cd08755 --- /dev/null +++ b/node_modules/mustache/test/_files/multiline_comment.txt @@ -0,0 +1 @@ +Hello world! diff --git a/node_modules/mustache/test/_files/nested_higher_order_sections.js b/node_modules/mustache/test/_files/nested_higher_order_sections.js new file mode 100644 index 0000000..3ccf4d3 --- /dev/null +++ b/node_modules/mustache/test/_files/nested_higher_order_sections.js @@ -0,0 +1,8 @@ +({ + bold: function () { + return function (text, render) { + return '' + render(text) + ''; + }; + }, + person: { name: 'Jonas' } +}); diff --git a/node_modules/mustache/test/_files/nested_higher_order_sections.mustache b/node_modules/mustache/test/_files/nested_higher_order_sections.mustache new file mode 100644 index 0000000..e312fe7 --- /dev/null +++ b/node_modules/mustache/test/_files/nested_higher_order_sections.mustache @@ -0,0 +1 @@ +{{#bold}}{{#person}}My name is {{name}}!{{/person}}{{/bold}} diff --git a/node_modules/mustache/test/_files/nested_higher_order_sections.txt b/node_modules/mustache/test/_files/nested_higher_order_sections.txt new file mode 100644 index 0000000..0ee6a40 --- /dev/null +++ b/node_modules/mustache/test/_files/nested_higher_order_sections.txt @@ -0,0 +1 @@ +My name is Jonas! diff --git a/node_modules/mustache/test/_files/nested_iterating.js b/node_modules/mustache/test/_files/nested_iterating.js new file mode 100644 index 0000000..2708b2d --- /dev/null +++ b/node_modules/mustache/test/_files/nested_iterating.js @@ -0,0 +1,8 @@ +({ + inner: [{ + foo: 'foo', + inner: [{ + bar: 'bar' + }] + }] +}) diff --git a/node_modules/mustache/test/_files/nested_iterating.mustache b/node_modules/mustache/test/_files/nested_iterating.mustache new file mode 100644 index 0000000..1a3bb1a --- /dev/null +++ b/node_modules/mustache/test/_files/nested_iterating.mustache @@ -0,0 +1 @@ +{{#inner}}{{foo}}{{#inner}}{{bar}}{{/inner}}{{/inner}} diff --git a/node_modules/mustache/test/_files/nested_iterating.txt b/node_modules/mustache/test/_files/nested_iterating.txt new file mode 100644 index 0000000..323fae0 --- /dev/null +++ b/node_modules/mustache/test/_files/nested_iterating.txt @@ -0,0 +1 @@ +foobar diff --git a/node_modules/mustache/test/_files/nesting.js b/node_modules/mustache/test/_files/nesting.js new file mode 100644 index 0000000..264cc2f --- /dev/null +++ b/node_modules/mustache/test/_files/nesting.js @@ -0,0 +1,7 @@ +({ + foo: [ + {a: {b: 1}}, + {a: {b: 2}}, + {a: {b: 3}} + ] +}) diff --git a/node_modules/mustache/test/_files/nesting.mustache b/node_modules/mustache/test/_files/nesting.mustache new file mode 100644 index 0000000..551366d --- /dev/null +++ b/node_modules/mustache/test/_files/nesting.mustache @@ -0,0 +1,5 @@ +{{#foo}} + {{#a}} + {{b}} + {{/a}} +{{/foo}} diff --git a/node_modules/mustache/test/_files/nesting.txt b/node_modules/mustache/test/_files/nesting.txt new file mode 100644 index 0000000..7db34b1 --- /dev/null +++ b/node_modules/mustache/test/_files/nesting.txt @@ -0,0 +1,3 @@ + 1 + 2 + 3 diff --git a/node_modules/mustache/test/_files/nesting_same_name.js b/node_modules/mustache/test/_files/nesting_same_name.js new file mode 100644 index 0000000..10a0c14 --- /dev/null +++ b/node_modules/mustache/test/_files/nesting_same_name.js @@ -0,0 +1,8 @@ +({ + items: [ + { + name: 'name', + items: [1, 2, 3, 4] + } + ] +}) diff --git a/node_modules/mustache/test/_files/nesting_same_name.mustache b/node_modules/mustache/test/_files/nesting_same_name.mustache new file mode 100644 index 0000000..777dbd6 --- /dev/null +++ b/node_modules/mustache/test/_files/nesting_same_name.mustache @@ -0,0 +1 @@ +{{#items}}{{name}}{{#items}}{{.}}{{/items}}{{/items}} diff --git a/node_modules/mustache/test/_files/nesting_same_name.txt b/node_modules/mustache/test/_files/nesting_same_name.txt new file mode 100644 index 0000000..34fcfd3 --- /dev/null +++ b/node_modules/mustache/test/_files/nesting_same_name.txt @@ -0,0 +1 @@ +name1234 diff --git a/node_modules/mustache/test/_files/null_string.js b/node_modules/mustache/test/_files/null_string.js new file mode 100644 index 0000000..984ee51 --- /dev/null +++ b/node_modules/mustache/test/_files/null_string.js @@ -0,0 +1,10 @@ +({ + name: "Elise", + glytch: true, + binary: false, + value: null, + undef: undefined, + numeric: function() { + return NaN; + } +}) diff --git a/node_modules/mustache/test/_files/null_string.mustache b/node_modules/mustache/test/_files/null_string.mustache new file mode 100644 index 0000000..a6f3300 --- /dev/null +++ b/node_modules/mustache/test/_files/null_string.mustache @@ -0,0 +1,6 @@ +Hello {{name}} +glytch {{glytch}} +binary {{binary}} +value {{value}} +undef {{undef}} +numeric {{numeric}} diff --git a/node_modules/mustache/test/_files/null_string.txt b/node_modules/mustache/test/_files/null_string.txt new file mode 100644 index 0000000..bcabe0a --- /dev/null +++ b/node_modules/mustache/test/_files/null_string.txt @@ -0,0 +1,6 @@ +Hello Elise +glytch true +binary false +value +undef +numeric NaN diff --git a/node_modules/mustache/test/_files/null_view.js b/node_modules/mustache/test/_files/null_view.js new file mode 100644 index 0000000..dbdae72 --- /dev/null +++ b/node_modules/mustache/test/_files/null_view.js @@ -0,0 +1,4 @@ +({ + name: 'Joe', + friends: null +}) diff --git a/node_modules/mustache/test/_files/null_view.mustache b/node_modules/mustache/test/_files/null_view.mustache new file mode 100644 index 0000000..115b376 --- /dev/null +++ b/node_modules/mustache/test/_files/null_view.mustache @@ -0,0 +1 @@ +{{name}}'s friends: {{#friends}}{{name}}, {{/friends}} \ No newline at end of file diff --git a/node_modules/mustache/test/_files/null_view.txt b/node_modules/mustache/test/_files/null_view.txt new file mode 100644 index 0000000..15ed2ab --- /dev/null +++ b/node_modules/mustache/test/_files/null_view.txt @@ -0,0 +1 @@ +Joe's friends: \ No newline at end of file diff --git a/node_modules/mustache/test/_files/partial_array.js b/node_modules/mustache/test/_files/partial_array.js new file mode 100644 index 0000000..2a6ddf1 --- /dev/null +++ b/node_modules/mustache/test/_files/partial_array.js @@ -0,0 +1,3 @@ +({ + array: ['1', '2', '3', '4'] +}) diff --git a/node_modules/mustache/test/_files/partial_array.mustache b/node_modules/mustache/test/_files/partial_array.mustache new file mode 100644 index 0000000..7a336fe --- /dev/null +++ b/node_modules/mustache/test/_files/partial_array.mustache @@ -0,0 +1 @@ +{{>partial}} \ No newline at end of file diff --git a/node_modules/mustache/test/_files/partial_array.partial b/node_modules/mustache/test/_files/partial_array.partial new file mode 100644 index 0000000..0ba652c --- /dev/null +++ b/node_modules/mustache/test/_files/partial_array.partial @@ -0,0 +1,4 @@ +Here's a non-sense array of values +{{#array}} + {{.}} +{{/array}} diff --git a/node_modules/mustache/test/_files/partial_array.txt b/node_modules/mustache/test/_files/partial_array.txt new file mode 100644 index 0000000..892837c --- /dev/null +++ b/node_modules/mustache/test/_files/partial_array.txt @@ -0,0 +1,5 @@ +Here's a non-sense array of values + 1 + 2 + 3 + 4 diff --git a/node_modules/mustache/test/_files/partial_array_of_partials.js b/node_modules/mustache/test/_files/partial_array_of_partials.js new file mode 100644 index 0000000..03f13c9 --- /dev/null +++ b/node_modules/mustache/test/_files/partial_array_of_partials.js @@ -0,0 +1,8 @@ +({ + numbers: [ + {i: '1'}, + {i: '2'}, + {i: '3'}, + {i: '4'} + ] +}) diff --git a/node_modules/mustache/test/_files/partial_array_of_partials.mustache b/node_modules/mustache/test/_files/partial_array_of_partials.mustache new file mode 100644 index 0000000..1af6d68 --- /dev/null +++ b/node_modules/mustache/test/_files/partial_array_of_partials.mustache @@ -0,0 +1,4 @@ +Here is some stuff! +{{#numbers}} +{{>partial}} +{{/numbers}} diff --git a/node_modules/mustache/test/_files/partial_array_of_partials.partial b/node_modules/mustache/test/_files/partial_array_of_partials.partial new file mode 100644 index 0000000..bdde77d --- /dev/null +++ b/node_modules/mustache/test/_files/partial_array_of_partials.partial @@ -0,0 +1 @@ +{{i}} diff --git a/node_modules/mustache/test/_files/partial_array_of_partials.txt b/node_modules/mustache/test/_files/partial_array_of_partials.txt new file mode 100644 index 0000000..f622375 --- /dev/null +++ b/node_modules/mustache/test/_files/partial_array_of_partials.txt @@ -0,0 +1,5 @@ +Here is some stuff! +1 +2 +3 +4 diff --git a/node_modules/mustache/test/_files/partial_array_of_partials_implicit.js b/node_modules/mustache/test/_files/partial_array_of_partials_implicit.js new file mode 100644 index 0000000..9ec0c00 --- /dev/null +++ b/node_modules/mustache/test/_files/partial_array_of_partials_implicit.js @@ -0,0 +1,3 @@ +({ + numbers: ['1', '2', '3', '4'] +}) diff --git a/node_modules/mustache/test/_files/partial_array_of_partials_implicit.mustache b/node_modules/mustache/test/_files/partial_array_of_partials_implicit.mustache new file mode 100644 index 0000000..1af6d68 --- /dev/null +++ b/node_modules/mustache/test/_files/partial_array_of_partials_implicit.mustache @@ -0,0 +1,4 @@ +Here is some stuff! +{{#numbers}} +{{>partial}} +{{/numbers}} diff --git a/node_modules/mustache/test/_files/partial_array_of_partials_implicit.partial b/node_modules/mustache/test/_files/partial_array_of_partials_implicit.partial new file mode 100644 index 0000000..12f7159 --- /dev/null +++ b/node_modules/mustache/test/_files/partial_array_of_partials_implicit.partial @@ -0,0 +1 @@ +{{.}} diff --git a/node_modules/mustache/test/_files/partial_array_of_partials_implicit.txt b/node_modules/mustache/test/_files/partial_array_of_partials_implicit.txt new file mode 100644 index 0000000..f622375 --- /dev/null +++ b/node_modules/mustache/test/_files/partial_array_of_partials_implicit.txt @@ -0,0 +1,5 @@ +Here is some stuff! +1 +2 +3 +4 diff --git a/node_modules/mustache/test/_files/partial_empty.js b/node_modules/mustache/test/_files/partial_empty.js new file mode 100644 index 0000000..82b8c22 --- /dev/null +++ b/node_modules/mustache/test/_files/partial_empty.js @@ -0,0 +1,3 @@ +({ + foo: 1 +}) diff --git a/node_modules/mustache/test/_files/partial_empty.mustache b/node_modules/mustache/test/_files/partial_empty.mustache new file mode 100644 index 0000000..a710047 --- /dev/null +++ b/node_modules/mustache/test/_files/partial_empty.mustache @@ -0,0 +1,2 @@ +hey {{foo}} +{{>partial}} diff --git a/node_modules/mustache/test/_files/partial_empty.partial b/node_modules/mustache/test/_files/partial_empty.partial new file mode 100644 index 0000000..e69de29 diff --git a/node_modules/mustache/test/_files/partial_empty.txt b/node_modules/mustache/test/_files/partial_empty.txt new file mode 100644 index 0000000..1a67907 --- /dev/null +++ b/node_modules/mustache/test/_files/partial_empty.txt @@ -0,0 +1 @@ +hey 1 diff --git a/node_modules/mustache/test/_files/partial_template.js b/node_modules/mustache/test/_files/partial_template.js new file mode 100644 index 0000000..a913f87 --- /dev/null +++ b/node_modules/mustache/test/_files/partial_template.js @@ -0,0 +1,6 @@ +({ + title: function () { + return "Welcome"; + }, + again: "Goodbye" +}) diff --git a/node_modules/mustache/test/_files/partial_template.mustache b/node_modules/mustache/test/_files/partial_template.mustache new file mode 100644 index 0000000..6a7492e --- /dev/null +++ b/node_modules/mustache/test/_files/partial_template.mustache @@ -0,0 +1,2 @@ +

      {{title}}

      +{{>partial}} diff --git a/node_modules/mustache/test/_files/partial_template.partial b/node_modules/mustache/test/_files/partial_template.partial new file mode 100644 index 0000000..a404529 --- /dev/null +++ b/node_modules/mustache/test/_files/partial_template.partial @@ -0,0 +1 @@ +Again, {{again}}! diff --git a/node_modules/mustache/test/_files/partial_template.txt b/node_modules/mustache/test/_files/partial_template.txt new file mode 100644 index 0000000..692698f --- /dev/null +++ b/node_modules/mustache/test/_files/partial_template.txt @@ -0,0 +1,2 @@ +

      Welcome

      +Again, Goodbye! diff --git a/node_modules/mustache/test/_files/partial_view.js b/node_modules/mustache/test/_files/partial_view.js new file mode 100644 index 0000000..3ad70d3 --- /dev/null +++ b/node_modules/mustache/test/_files/partial_view.js @@ -0,0 +1,14 @@ +({ + greeting: function () { + return "Welcome"; + }, + farewell: function () { + return "Fair enough, right?"; + }, + name: "Chris", + value: 10000, + taxed_value: function () { + return this.value - (this.value * 0.4); + }, + in_ca: true +}) diff --git a/node_modules/mustache/test/_files/partial_view.mustache b/node_modules/mustache/test/_files/partial_view.mustache new file mode 100644 index 0000000..f8f6a5b --- /dev/null +++ b/node_modules/mustache/test/_files/partial_view.mustache @@ -0,0 +1,3 @@ +

      {{greeting}}

      +{{>partial}} +

      {{farewell}}

      diff --git a/node_modules/mustache/test/_files/partial_view.partial b/node_modules/mustache/test/_files/partial_view.partial new file mode 100644 index 0000000..03df206 --- /dev/null +++ b/node_modules/mustache/test/_files/partial_view.partial @@ -0,0 +1,5 @@ +Hello {{name}} +You have just won ${{value}}! +{{#in_ca}} +Well, ${{ taxed_value }}, after taxes. +{{/in_ca}} \ No newline at end of file diff --git a/node_modules/mustache/test/_files/partial_view.txt b/node_modules/mustache/test/_files/partial_view.txt new file mode 100644 index 0000000..c09147c --- /dev/null +++ b/node_modules/mustache/test/_files/partial_view.txt @@ -0,0 +1,5 @@ +

      Welcome

      +Hello Chris +You have just won $10000! +Well, $6000, after taxes. +

      Fair enough, right?

      diff --git a/node_modules/mustache/test/_files/partial_whitespace.js b/node_modules/mustache/test/_files/partial_whitespace.js new file mode 100644 index 0000000..3ad70d3 --- /dev/null +++ b/node_modules/mustache/test/_files/partial_whitespace.js @@ -0,0 +1,14 @@ +({ + greeting: function () { + return "Welcome"; + }, + farewell: function () { + return "Fair enough, right?"; + }, + name: "Chris", + value: 10000, + taxed_value: function () { + return this.value - (this.value * 0.4); + }, + in_ca: true +}) diff --git a/node_modules/mustache/test/_files/partial_whitespace.mustache b/node_modules/mustache/test/_files/partial_whitespace.mustache new file mode 100644 index 0000000..48bd1ff --- /dev/null +++ b/node_modules/mustache/test/_files/partial_whitespace.mustache @@ -0,0 +1,3 @@ +

      {{ greeting }}

      +{{> partial }} +

      {{ farewell }}

      diff --git a/node_modules/mustache/test/_files/partial_whitespace.partial b/node_modules/mustache/test/_files/partial_whitespace.partial new file mode 100644 index 0000000..30de8f6 --- /dev/null +++ b/node_modules/mustache/test/_files/partial_whitespace.partial @@ -0,0 +1,5 @@ +Hello {{ name}} +You have just won ${{value }}! +{{# in_ca }} +Well, ${{ taxed_value }}, after taxes. +{{/ in_ca }} \ No newline at end of file diff --git a/node_modules/mustache/test/_files/partial_whitespace.txt b/node_modules/mustache/test/_files/partial_whitespace.txt new file mode 100644 index 0000000..c09147c --- /dev/null +++ b/node_modules/mustache/test/_files/partial_whitespace.txt @@ -0,0 +1,5 @@ +

      Welcome

      +Hello Chris +You have just won $10000! +Well, $6000, after taxes. +

      Fair enough, right?

      diff --git a/node_modules/mustache/test/_files/recursion_with_same_names.js b/node_modules/mustache/test/_files/recursion_with_same_names.js new file mode 100644 index 0000000..ce26502 --- /dev/null +++ b/node_modules/mustache/test/_files/recursion_with_same_names.js @@ -0,0 +1,8 @@ +({ + name: 'name', + description: 'desc', + terms: [ + {name: 't1', index: 0}, + {name: 't2', index: 1} + ] +}) diff --git a/node_modules/mustache/test/_files/recursion_with_same_names.mustache b/node_modules/mustache/test/_files/recursion_with_same_names.mustache new file mode 100644 index 0000000..c331d04 --- /dev/null +++ b/node_modules/mustache/test/_files/recursion_with_same_names.mustache @@ -0,0 +1,7 @@ +{{ name }} +{{ description }} + +{{#terms}} + {{name}} + {{index}} +{{/terms}} diff --git a/node_modules/mustache/test/_files/recursion_with_same_names.txt b/node_modules/mustache/test/_files/recursion_with_same_names.txt new file mode 100644 index 0000000..cb15d75 --- /dev/null +++ b/node_modules/mustache/test/_files/recursion_with_same_names.txt @@ -0,0 +1,7 @@ +name +desc + + t1 + 0 + t2 + 1 diff --git a/node_modules/mustache/test/_files/reuse_of_enumerables.js b/node_modules/mustache/test/_files/reuse_of_enumerables.js new file mode 100644 index 0000000..4368b57 --- /dev/null +++ b/node_modules/mustache/test/_files/reuse_of_enumerables.js @@ -0,0 +1,6 @@ +({ + terms: [ + {name: 't1', index: 0}, + {name: 't2', index: 1} + ] +}) diff --git a/node_modules/mustache/test/_files/reuse_of_enumerables.mustache b/node_modules/mustache/test/_files/reuse_of_enumerables.mustache new file mode 100644 index 0000000..cc0cb7a --- /dev/null +++ b/node_modules/mustache/test/_files/reuse_of_enumerables.mustache @@ -0,0 +1,8 @@ +{{#terms}} + {{name}} + {{index}} +{{/terms}} +{{#terms}} + {{name}} + {{index}} +{{/terms}} diff --git a/node_modules/mustache/test/_files/reuse_of_enumerables.txt b/node_modules/mustache/test/_files/reuse_of_enumerables.txt new file mode 100644 index 0000000..6d05d96 --- /dev/null +++ b/node_modules/mustache/test/_files/reuse_of_enumerables.txt @@ -0,0 +1,8 @@ + t1 + 0 + t2 + 1 + t1 + 0 + t2 + 1 diff --git a/node_modules/mustache/test/_files/section_as_context.js b/node_modules/mustache/test/_files/section_as_context.js new file mode 100644 index 0000000..425b29c --- /dev/null +++ b/node_modules/mustache/test/_files/section_as_context.js @@ -0,0 +1,10 @@ +({ + a_object: { + title: 'this is an object', + description: 'one of its attributes is a list', + a_list: [ + {label: 'listitem1'}, + {label: 'listitem2'} + ] + } +}) diff --git a/node_modules/mustache/test/_files/section_as_context.mustache b/node_modules/mustache/test/_files/section_as_context.mustache new file mode 100644 index 0000000..59990f6 --- /dev/null +++ b/node_modules/mustache/test/_files/section_as_context.mustache @@ -0,0 +1,9 @@ +{{#a_object}} +

      {{title}}

      +

      {{description}}

      +
        + {{#a_list}} +
      • {{label}}
      • + {{/a_list}} +
      +{{/a_object}} diff --git a/node_modules/mustache/test/_files/section_as_context.txt b/node_modules/mustache/test/_files/section_as_context.txt new file mode 100644 index 0000000..d834e80 --- /dev/null +++ b/node_modules/mustache/test/_files/section_as_context.txt @@ -0,0 +1,6 @@ +

      this is an object

      +

      one of its attributes is a list

      +
        +
      • listitem1
      • +
      • listitem2
      • +
      diff --git a/node_modules/mustache/test/_files/simple.js b/node_modules/mustache/test/_files/simple.js new file mode 100644 index 0000000..1d8d6f4 --- /dev/null +++ b/node_modules/mustache/test/_files/simple.js @@ -0,0 +1,8 @@ +({ + name: "Chris", + value: 10000, + taxed_value: function () { + return this.value - (this.value * 0.4); + }, + in_ca: true +}) diff --git a/node_modules/mustache/test/_files/simple.mustache b/node_modules/mustache/test/_files/simple.mustache new file mode 100644 index 0000000..2fea632 --- /dev/null +++ b/node_modules/mustache/test/_files/simple.mustache @@ -0,0 +1,5 @@ +Hello {{name}} +You have just won ${{value}}! +{{#in_ca}} +Well, ${{ taxed_value }}, after taxes. +{{/in_ca}} diff --git a/node_modules/mustache/test/_files/simple.txt b/node_modules/mustache/test/_files/simple.txt new file mode 100644 index 0000000..5d75d65 --- /dev/null +++ b/node_modules/mustache/test/_files/simple.txt @@ -0,0 +1,3 @@ +Hello Chris +You have just won $10000! +Well, $6000, after taxes. diff --git a/node_modules/mustache/test/_files/string_as_context.js b/node_modules/mustache/test/_files/string_as_context.js new file mode 100644 index 0000000..e8bb4da --- /dev/null +++ b/node_modules/mustache/test/_files/string_as_context.js @@ -0,0 +1,4 @@ +({ + a_string: 'aa', + a_list: ['a','b','c'] +}) diff --git a/node_modules/mustache/test/_files/string_as_context.mustache b/node_modules/mustache/test/_files/string_as_context.mustache new file mode 100644 index 0000000..c6aa11a --- /dev/null +++ b/node_modules/mustache/test/_files/string_as_context.mustache @@ -0,0 +1,5 @@ +
        +{{#a_list}} +
      • {{.}}
      • +{{/a_list}} +
      \ No newline at end of file diff --git a/node_modules/mustache/test/_files/string_as_context.txt b/node_modules/mustache/test/_files/string_as_context.txt new file mode 100644 index 0000000..35e6306 --- /dev/null +++ b/node_modules/mustache/test/_files/string_as_context.txt @@ -0,0 +1,5 @@ + \ No newline at end of file diff --git a/node_modules/mustache/test/_files/two_in_a_row.js b/node_modules/mustache/test/_files/two_in_a_row.js new file mode 100644 index 0000000..9c17c11 --- /dev/null +++ b/node_modules/mustache/test/_files/two_in_a_row.js @@ -0,0 +1,4 @@ +({ + name: "Joe", + greeting: "Welcome" +}) diff --git a/node_modules/mustache/test/_files/two_in_a_row.mustache b/node_modules/mustache/test/_files/two_in_a_row.mustache new file mode 100644 index 0000000..b23f29e --- /dev/null +++ b/node_modules/mustache/test/_files/two_in_a_row.mustache @@ -0,0 +1 @@ +{{greeting}}, {{name}}! diff --git a/node_modules/mustache/test/_files/two_in_a_row.txt b/node_modules/mustache/test/_files/two_in_a_row.txt new file mode 100644 index 0000000..c6d6a9b --- /dev/null +++ b/node_modules/mustache/test/_files/two_in_a_row.txt @@ -0,0 +1 @@ +Welcome, Joe! diff --git a/node_modules/mustache/test/_files/two_sections.js b/node_modules/mustache/test/_files/two_sections.js new file mode 100644 index 0000000..b4100a5 --- /dev/null +++ b/node_modules/mustache/test/_files/two_sections.js @@ -0,0 +1 @@ +({}) diff --git a/node_modules/mustache/test/_files/two_sections.mustache b/node_modules/mustache/test/_files/two_sections.mustache new file mode 100644 index 0000000..a4b9f2a --- /dev/null +++ b/node_modules/mustache/test/_files/two_sections.mustache @@ -0,0 +1,4 @@ +{{#foo}} +{{/foo}} +{{#bar}} +{{/bar}} diff --git a/node_modules/mustache/test/_files/two_sections.txt b/node_modules/mustache/test/_files/two_sections.txt new file mode 100644 index 0000000..e69de29 diff --git a/node_modules/mustache/test/_files/unescaped.js b/node_modules/mustache/test/_files/unescaped.js new file mode 100644 index 0000000..b6d064f --- /dev/null +++ b/node_modules/mustache/test/_files/unescaped.js @@ -0,0 +1,5 @@ +({ + title: function () { + return "Bear > Shark"; + } +}) diff --git a/node_modules/mustache/test/_files/unescaped.mustache b/node_modules/mustache/test/_files/unescaped.mustache new file mode 100644 index 0000000..6b07d7b --- /dev/null +++ b/node_modules/mustache/test/_files/unescaped.mustache @@ -0,0 +1 @@ +

      {{{title}}}

      diff --git a/node_modules/mustache/test/_files/unescaped.txt b/node_modules/mustache/test/_files/unescaped.txt new file mode 100644 index 0000000..089ad79 --- /dev/null +++ b/node_modules/mustache/test/_files/unescaped.txt @@ -0,0 +1 @@ +

      Bear > Shark

      diff --git a/node_modules/mustache/test/_files/whitespace.js b/node_modules/mustache/test/_files/whitespace.js new file mode 100644 index 0000000..f41cb56 --- /dev/null +++ b/node_modules/mustache/test/_files/whitespace.js @@ -0,0 +1,4 @@ +({ + tag1: "Hello", + tag2: "World" +}) diff --git a/node_modules/mustache/test/_files/whitespace.mustache b/node_modules/mustache/test/_files/whitespace.mustache new file mode 100644 index 0000000..aa76e08 --- /dev/null +++ b/node_modules/mustache/test/_files/whitespace.mustache @@ -0,0 +1,4 @@ +{{tag1}} + + +{{tag2}}. diff --git a/node_modules/mustache/test/_files/whitespace.txt b/node_modules/mustache/test/_files/whitespace.txt new file mode 100644 index 0000000..851fa74 --- /dev/null +++ b/node_modules/mustache/test/_files/whitespace.txt @@ -0,0 +1,4 @@ +Hello + + +World. diff --git a/node_modules/mustache/test/context-test.js b/node_modules/mustache/test/context-test.js new file mode 100644 index 0000000..752f74b --- /dev/null +++ b/node_modules/mustache/test/context-test.js @@ -0,0 +1,51 @@ +require('./helper'); +var Context = Mustache.Context; + +describe('A new Mustache.Context', function () { + var context; + beforeEach(function () { + context = new Context({ name: 'parent', message: 'hi', a: { b: 'b' } }); + }); + + it('is able to lookup properties of its own view', function () { + assert.equal(context.lookup('name'), 'parent'); + }); + + it('is able to lookup nested properties of its own view', function () { + assert.equal(context.lookup('a.b'), 'b'); + }); + + describe('when pushed', function () { + beforeEach(function () { + context = context.push({ name: 'child', c: { d: 'd' } }); + }); + + it('returns the child context', function () { + assert.equal(context.view.name, 'child'); + assert.equal(context.parent.view.name, 'parent'); + }); + + it('is able to lookup properties of its own view', function () { + assert.equal(context.lookup('name'), 'child'); + }); + + it("is able to lookup properties of the parent context's view", function () { + assert.equal(context.lookup('message'), 'hi'); + }); + + it('is able to lookup nested properties of its own view', function () { + assert.equal(context.lookup('c.d'), 'd'); + }); + + it('is able to lookup nested properties of its parent view', function () { + assert.equal(context.lookup('a.b'), 'b'); + }); + }); +}); + +describe('Mustache.Context.make', function () { + it('returns the same object when given a Context', function () { + var context = new Context; + assert.strictEqual(Context.make(context), context); + }); +}); diff --git a/node_modules/mustache/test/helper.js b/node_modules/mustache/test/helper.js new file mode 100644 index 0000000..a91fe49 --- /dev/null +++ b/node_modules/mustache/test/helper.js @@ -0,0 +1,2 @@ +assert = require('assert'); +Mustache = require('../mustache'); diff --git a/node_modules/mustache/test/parse-test.js b/node_modules/mustache/test/parse-test.js new file mode 100644 index 0000000..40d23a4 --- /dev/null +++ b/node_modules/mustache/test/parse-test.js @@ -0,0 +1,106 @@ +require('./helper'); + +// A map of templates to their expected token output. Tokens are in the format: +// [type, value, startIndex, endIndex, subTokens]. +var expectations = { + '' : [], + '{{hi}}' : [ [ 'name', 'hi', 0, 6 ] ], + '{{hi.world}}' : [ [ 'name', 'hi.world', 0, 12 ] ], + '{{hi . world}}' : [ [ 'name', 'hi . world', 0, 14 ] ], + '{{ hi}}' : [ [ 'name', 'hi', 0, 7 ] ], + '{{hi }}' : [ [ 'name', 'hi', 0, 7 ] ], + '{{ hi }}' : [ [ 'name', 'hi', 0, 8 ] ], + '{{{hi}}}' : [ [ '&', 'hi', 0, 8 ] ], + '{{!hi}}' : [ [ '!', 'hi', 0, 7 ] ], + '{{! hi}}' : [ [ '!', 'hi', 0, 8 ] ], + '{{! hi }}' : [ [ '!', 'hi', 0, 9 ] ], + '{{ !hi}}' : [ [ '!', 'hi', 0, 8 ] ], + '{{ ! hi}}' : [ [ '!', 'hi', 0, 9 ] ], + '{{ ! hi }}' : [ [ '!', 'hi', 0, 10 ] ], + 'a\n b' : [ [ 'text', 'a\n b', 0, 4 ] ], + 'a{{hi}}' : [ [ 'text', 'a', 0, 1 ], [ 'name', 'hi', 1, 7 ] ], + 'a {{hi}}' : [ [ 'text', 'a ', 0, 2 ], [ 'name', 'hi', 2, 8 ] ], + ' a{{hi}}' : [ [ 'text', ' a', 0, 2 ], [ 'name', 'hi', 2, 8 ] ], + ' a {{hi}}' : [ [ 'text', ' a ', 0, 3 ], [ 'name', 'hi', 3, 9 ] ], + 'a{{hi}}b' : [ [ 'text', 'a', 0, 1 ], [ 'name', 'hi', 1, 7 ], [ 'text', 'b', 7, 8 ] ], + 'a{{hi}} b' : [ [ 'text', 'a', 0, 1 ], [ 'name', 'hi', 1, 7 ], [ 'text', ' b', 7, 9 ] ], + 'a{{hi}}b ' : [ [ 'text', 'a', 0, 1 ], [ 'name', 'hi', 1, 7 ], [ 'text', 'b ', 7, 9 ] ], + 'a\n{{hi}} b \n' : [ [ 'text', 'a\n', 0, 2 ], [ 'name', 'hi', 2, 8 ], [ 'text', ' b \n', 8, 12 ] ], + 'a\n {{hi}} \nb' : [ [ 'text', 'a\n ', 0, 3 ], [ 'name', 'hi', 3, 9 ], [ 'text', ' \nb', 9, 12 ] ], + 'a\n {{!hi}} \nb' : [ [ 'text', 'a\n', 0, 2 ], [ '!', 'hi', 3, 10 ], [ 'text', 'b', 12, 13 ] ], + 'a\n{{#a}}{{/a}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 2, 8, [], 8 ], [ 'text', 'b', 15, 16 ] ], + 'a\n {{#a}}{{/a}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [], 9 ], [ 'text', 'b', 16, 17 ] ], + 'a\n {{#a}}{{/a}} \nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [], 9 ], [ 'text', 'b', 17, 18 ] ], + 'a\n{{#a}}\n{{/a}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 2, 8, [], 9 ], [ 'text', 'b', 16, 17 ] ], + 'a\n {{#a}}\n{{/a}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [], 10 ], [ 'text', 'b', 17, 18 ] ], + 'a\n {{#a}}\n{{/a}} \nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [], 10 ], [ 'text', 'b', 18, 19 ] ], + 'a\n{{#a}}\n{{/a}}\n{{#b}}\n{{/b}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 2, 8, [], 9 ], [ '#', 'b', 16, 22, [], 23 ], [ 'text', 'b', 30, 31 ] ], + 'a\n {{#a}}\n{{/a}}\n{{#b}}\n{{/b}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [], 10 ], [ '#', 'b', 17, 23, [], 24 ], [ 'text', 'b', 31, 32 ] ], + 'a\n {{#a}}\n{{/a}}\n{{#b}}\n{{/b}} \nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [], 10 ], [ '#', 'b', 17, 23, [], 24 ], [ 'text', 'b', 32, 33 ] ], + 'a\n{{#a}}\n{{#b}}\n{{/b}}\n{{/a}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 2, 8, [ [ '#', 'b', 9, 15, [], 16 ] ], 23 ], [ 'text', 'b', 30, 31 ] ], + 'a\n {{#a}}\n{{#b}}\n{{/b}}\n{{/a}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [ [ '#', 'b', 10, 16, [], 17 ] ], 24 ], [ 'text', 'b', 31, 32 ] ], + 'a\n {{#a}}\n{{#b}}\n{{/b}}\n{{/a}} \nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [ [ '#', 'b', 10, 16, [], 17 ] ], 24 ], [ 'text', 'b', 32, 33 ] ], + '{{>abc}}' : [ [ '>', 'abc', 0, 8 ] ], + '{{> abc }}' : [ [ '>', 'abc', 0, 10 ] ], + '{{ > abc }}' : [ [ '>', 'abc', 0, 11 ] ], + '{{=<% %>=}}' : [ [ '=', '<% %>', 0, 11 ] ], + '{{= <% %> =}}' : [ [ '=', '<% %>', 0, 13 ] ], + '{{=<% %>=}}<%={{ }}=%>' : [ [ '=', '<% %>', 0, 11 ], [ '=', '{{ }}', 11, 22 ] ], + '{{=<% %>=}}<%hi%>' : [ [ '=', '<% %>', 0, 11 ], [ 'name', 'hi', 11, 17 ] ], + '{{#a}}{{/a}}hi{{#b}}{{/b}}\n' : [ [ '#', 'a', 0, 6, [], 6 ], [ 'text', 'hi', 12, 14 ], [ '#', 'b', 14, 20, [], 20 ], [ 'text', '\n', 26, 27 ] ], + '{{a}}\n{{b}}\n\n{{#c}}\n{{/c}}\n' : [ [ 'name', 'a', 0, 5 ], [ 'text', '\n', 5, 6 ], [ 'name', 'b', 6, 11 ], [ 'text', '\n\n', 11, 13 ], [ '#', 'c', 13, 19, [], 20 ] ], + '{{#foo}}\n {{#a}}\n {{b}}\n {{/a}}\n{{/foo}}\n' + : [ [ '#', 'foo', 0, 8, [ [ '#', 'a', 11, 17, [ [ 'text', ' ', 18, 22 ], [ 'name', 'b', 22, 27 ], [ 'text', '\n', 27, 28 ] ], 30 ] ], 37 ] ] +}; + +describe('Mustache.parse', function () { + + for (var template in expectations) { + (function (template, tokens) { + it('knows how to parse ' + JSON.stringify(template), function () { + assert.deepEqual(Mustache.parse(template), tokens); + }); + })(template, expectations[template]); + } + + describe('when there is an unclosed tag', function () { + it('throws an error', function () { + assert.throws(function () { + Mustache.parse('My name is {{name'); + }, /unclosed tag at 17/i); + }); + }); + + describe('when there is an unclosed section', function () { + it('throws an error', function () { + assert.throws(function () { + Mustache.parse('A list: {{#people}}{{name}}'); + }, /unclosed section "people" at 27/i); + }); + }); + + describe('when there is an unopened section', function () { + it('throws an error', function () { + assert.throws(function () { + Mustache.parse('The end of the list! {{/people}}'); + }, /unopened section "people" at 21/i); + }); + }); + + describe('when invalid tags are given as an argument', function () { + it('throws an error', function () { + assert.throws(function () { + Mustache.parse('A template <% name %>', [ '<%' ]); + }, /invalid tags/i); + }); + }); + + describe('when the template contains invalid tags', function () { + it('throws an error', function () { + assert.throws(function () { + Mustache.parse('A template {{=<%=}}'); + }, /invalid tags at 11/i); + }); + }); + +}); diff --git a/node_modules/mustache/test/render-test.js b/node_modules/mustache/test/render-test.js new file mode 100644 index 0000000..acec47f --- /dev/null +++ b/node_modules/mustache/test/render-test.js @@ -0,0 +1,68 @@ +require('./helper'); + +var fs = require('fs'); +var path = require('path'); +var _files = path.join(__dirname, '_files'); + +function getContents(testName, ext) { + return fs.readFileSync(path.join(_files, testName + '.' + ext), 'utf8'); +} + +function getView(testName) { + var view = getContents(testName, 'js'); + if (!view) throw new Error('Cannot find view for test "' + testName + '"'); + return eval(view); +} + +function getPartial(testName) { + try { + return getContents(testName, 'partial'); + } catch (e) { + // No big deal. Not all tests need to test partial support. + } +} + +function getTest(testName) { + var test = {}; + test.view = getView(testName); + test.template = getContents(testName, 'mustache'); + test.partial = getPartial(testName); + test.expect = getContents(testName, 'txt'); + return test; +} + +// You can put the name of a specific test to run in the TEST environment +// variable (e.g. TEST=backslashes vows test/render-test.js) +var testToRun = process.env.TEST; + +var testNames; +if (testToRun) { + testNames = [testToRun]; +} else { + testNames = fs.readdirSync(_files).filter(function (file) { + return (/\.js$/).test(file); + }).map(function (file) { + return path.basename(file).replace(/\.js$/, ''); + }); +} + +describe('Mustache.render', function () { + beforeEach(function () { + Mustache.clearCache(); + }); + + testNames.forEach(function (testName) { + var test = getTest(testName); + + it('knows how to render ' + testName, function () { + var output; + if (test.partial) { + output = Mustache.render(test.template, test.view, { partial: test.partial }); + } else { + output = Mustache.render(test.template, test.view); + } + + assert.equal(output, test.expect); + }); + }); +}); diff --git a/node_modules/mustache/test/scanner-test.js b/node_modules/mustache/test/scanner-test.js new file mode 100644 index 0000000..9c97664 --- /dev/null +++ b/node_modules/mustache/test/scanner-test.js @@ -0,0 +1,78 @@ +require('./helper'); +var Scanner = Mustache.Scanner; + +describe('A new Mustache.Scanner', function () { + describe('for an empty string', function () { + it('is at the end', function () { + var scanner = new Scanner(''); + assert(scanner.eos()); + }); + }); + + describe('for a non-empty string', function () { + var scanner; + beforeEach(function () { + scanner = new Scanner('a b c'); + }); + + describe('scan', function () { + describe('when the RegExp matches the entire string', function () { + it('returns the entire string', function () { + var match = scanner.scan(/a b c/); + assert.equal(match, scanner.string); + assert(scanner.eos()); + }); + }); + + describe('when the RegExp matches at index 0', function () { + it('returns the portion of the string that matched', function () { + var match = scanner.scan(/a/); + assert.equal(match, 'a'); + assert.equal(scanner.pos, 1); + }); + }); + + describe('when the RegExp matches at some index other than 0', function () { + it('returns the empty string', function () { + var match = scanner.scan(/b/); + assert.equal(match, ''); + assert.equal(scanner.pos, 0); + }); + }); + + describe('when the RegExp does not match', function () { + it('returns the empty string', function () { + var match = scanner.scan(/z/); + assert.equal(match, ''); + assert.equal(scanner.pos, 0); + }); + }); + }); // scan + + describe('scanUntil', function () { + describe('when the RegExp matches at index 0', function () { + it('returns the empty string', function () { + var match = scanner.scanUntil(/a/); + assert.equal(match, ''); + assert.equal(scanner.pos, 0); + }); + }); + + describe('when the RegExp matches at some index other than 0', function () { + it('returns the string up to that index', function () { + var match = scanner.scanUntil(/b/); + assert.equal(match, 'a '); + assert.equal(scanner.pos, 2); + }); + }); + + describe('when the RegExp does not match', function () { + it('returns the entire string', function () { + var match = scanner.scanUntil(/z/); + assert.equal(match, scanner.string); + assert(scanner.eos()); + }); + }); + }); // scanUntil + }); // for a non-empty string +}); diff --git a/node_modules/mustache/test/writer-test.js b/node_modules/mustache/test/writer-test.js new file mode 100644 index 0000000..db2813a --- /dev/null +++ b/node_modules/mustache/test/writer-test.js @@ -0,0 +1,43 @@ +require('./helper'); +var Writer = Mustache.Writer; + +describe('A new Mustache.Writer', function () { + var writer; + beforeEach(function () { + writer = new Writer; + }); + + it('loads partials correctly', function () { + var partial = 'The content of the partial.'; + var result = writer.render('{{>partial}}', {}, function (name) { + assert.equal(name, 'partial'); + return partial; + }); + + assert.equal(result, partial); + }); + + it('caches partials by content, not name', function () { + var result = writer.render('{{>partial}}', {}, { + partial: 'partial one' + }); + + assert.equal(result, 'partial one'); + + result = writer.render('{{>partial}}', {}, { + partial: 'partial two' + }); + + assert.equal(result, 'partial two'); + }); + + it('can compile an array of tokens', function () { + var template = 'Hello {{name}}!'; + var tokens = Mustache.parse(template); + var render = writer.compileTokens(tokens, template); + + var result = render({ name: 'Michael' }); + + assert.equal(result, 'Hello Michael!'); + }); +}); diff --git a/node_modules/mustache/wrappers/dojo/mustache.js.post b/node_modules/mustache/wrappers/dojo/mustache.js.post new file mode 100644 index 0000000..eeeb4b7 --- /dev/null +++ b/node_modules/mustache/wrappers/dojo/mustache.js.post @@ -0,0 +1,4 @@ + + dojox.mustache = dojo.hitch(Mustache, "render"); + +})(); \ No newline at end of file diff --git a/node_modules/mustache/wrappers/dojo/mustache.js.pre b/node_modules/mustache/wrappers/dojo/mustache.js.pre new file mode 100644 index 0000000..f87f3cd --- /dev/null +++ b/node_modules/mustache/wrappers/dojo/mustache.js.pre @@ -0,0 +1,9 @@ +/* +Shameless port of a shameless port +@defunkt => @janl => @aq => @voodootikigod + +See http://github.com/defunkt/mustache for more info. +*/ + +dojo.provide("dojox.mustache._base"); +(function(){ diff --git a/node_modules/mustache/wrappers/jquery/mustache.js.post b/node_modules/mustache/wrappers/jquery/mustache.js.post new file mode 100644 index 0000000..d27d730 --- /dev/null +++ b/node_modules/mustache/wrappers/jquery/mustache.js.post @@ -0,0 +1,14 @@ + + $.mustache = function (template, view, partials) { + return Mustache.render(template, view, partials); + }; + + $.fn.mustache = function (view, partials) { + return $(this).map(function (i, elm) { + var template = $(elm).html().trim(); + var output = $.mustache(template, view, partials); + return $(output).get(); + }); + }; + +})(jQuery); diff --git a/node_modules/mustache/wrappers/jquery/mustache.js.pre b/node_modules/mustache/wrappers/jquery/mustache.js.pre new file mode 100644 index 0000000..b4d8af5 --- /dev/null +++ b/node_modules/mustache/wrappers/jquery/mustache.js.pre @@ -0,0 +1,9 @@ +/* +Shameless port of a shameless port +@defunkt => @janl => @aq + +See http://github.com/defunkt/mustache for more info. +*/ + +;(function($) { + diff --git a/node_modules/mustache/wrappers/mootools/mustache.js.post b/node_modules/mustache/wrappers/mootools/mustache.js.post new file mode 100644 index 0000000..aa9b8fa --- /dev/null +++ b/node_modules/mustache/wrappers/mootools/mustache.js.post @@ -0,0 +1,5 @@ + + Object.implement('mustache', function(view, partials){ + return Mustache.render(view, this, partials); + }); +})(); diff --git a/node_modules/mustache/wrappers/mootools/mustache.js.pre b/node_modules/mustache/wrappers/mootools/mustache.js.pre new file mode 100644 index 0000000..9839f99 --- /dev/null +++ b/node_modules/mustache/wrappers/mootools/mustache.js.pre @@ -0,0 +1,2 @@ +(function(){ + diff --git a/node_modules/mustache/wrappers/qooxdoo/mustache.js.post b/node_modules/mustache/wrappers/qooxdoo/mustache.js.post new file mode 100644 index 0000000..aba7af6 --- /dev/null +++ b/node_modules/mustache/wrappers/qooxdoo/mustache.js.post @@ -0,0 +1,9 @@ +/** + * Above is the original mustache code. + */ + +// EXPOSE qooxdoo variant +qx.bom.Template.version = Mustache.version; +qx.bom.Template.render = Mustache.render; + +})(); diff --git a/node_modules/mustache/wrappers/qooxdoo/mustache.js.pre b/node_modules/mustache/wrappers/qooxdoo/mustache.js.pre new file mode 100644 index 0000000..b51ccf9 --- /dev/null +++ b/node_modules/mustache/wrappers/qooxdoo/mustache.js.pre @@ -0,0 +1,134 @@ +/* ************************************************************************ + + qooxdoo - the new era of web development + + http://qooxdoo.org + + Copyright: + 2004-2012 1&1 Internet AG, Germany, http://www.1und1.de + + License: + LGPL: http://www.gnu.org/licenses/lgpl.html + EPL: http://www.eclipse.org/org/documents/epl-v10.php + See the LICENSE file in the project's top-level directory for details. + + Authors: + * Martin Wittemann (martinwittemann) + + ====================================================================== + + This class contains code based on the following work: + + * Mustache.js version 0.5.1-dev + + Code: + https://github.com/janl/mustache.js + + Copyright: + (c) 2009 Chris Wanstrath (Ruby) + (c) 2010 Jan Lehnardt (JavaScript) + + License: + MIT: http://www.opensource.org/licenses/mit-license.php + + ---------------------------------------------------------------------- + + Copyright (c) 2009 Chris Wanstrath (Ruby) + Copyright (c) 2010 Jan Lehnardt (JavaScript) + + 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. + +************************************************************************ */ + +/** + * The is a template class which can be used for HTML templating. In fact, + * this is a wrapper for mustache.js which is a "framework-agnostic way to + * render logic-free views". + * + * Here is a basic example how to use it: + * Template: + *
      + * var template = "Hi, my name is {{name}}!";
      + * var view = {name: "qooxdoo"};
      + * qx.bom.Template.toHtml(template, view);
      + * // return "Hi, my name is qooxdoo!"
      + * 
      + * + * For further details, please visit the mustache.js documentation here: + * https://github.com/janl/mustache.js/blob/master/README.md + */ +qx.Bootstrap.define("qx.bom.Template", { + statics : { + /** Contains the mustache.js version. */ + version: null, + + /** + * Original and only template method of mustache.js. For further + * documentation, please visit https://github.com/janl/mustache.js + * + * @signature function(template, view, partials) + * @param template {String} The String containing the template. + * @param view {Object} The object holding the data to render. + * @param partials {Object} Object holding parts of a template. + * @return {String} The parsed template. + */ + render: null, + + + /** + * Helper method which provides you with a direct access to templates + * stored as HTML in the DOM. The DOM node with the given ID will be used + * as a template, parsed and a new DOM node will be returned containing the + * parsed data. Keep in mind to have only one root DOM element in the the + * template. + * + * @param id {String} The id of the HTML template in the DOM. + * @param view {Object} The object holding the data to render. + * @param partials {Object} Object holding parts of a template. + * @return {DomNode} A DOM element holding the parsed template data. + */ + get : function(id, view, partials) { + // get the content stored in the DOM + var template = document.getElementById(id); + var inner = template.innerHTML; + + // apply the view + inner = this.toHtml(inner, view, partials); + + // special case for text only conversion + if (inner.search(/<|>/) === -1) { + return inner; + } + + // create a helper to convert the string into DOM nodes + var helper = qx.bom.Element.create("div"); + helper.innerHTML = inner; + + return helper.children[0]; + } + } +}); + +(function() { + +/** + * Below is the original mustache.js code. Snapshot date is mentioned in + * the head of this file. + */ diff --git a/node_modules/mysql/.npmignore b/node_modules/mysql/.npmignore new file mode 100644 index 0000000..b68bceb --- /dev/null +++ b/node_modules/mysql/.npmignore @@ -0,0 +1,5 @@ +*.un~ + +/node_modules + +*.sublime-* diff --git a/node_modules/mysql/.travis.yml b/node_modules/mysql/.travis.yml new file mode 100644 index 0000000..2a9b9e6 --- /dev/null +++ b/node_modules/mysql/.travis.yml @@ -0,0 +1,5 @@ +language: node_js +node_js: + - 0.4 + - 0.6 + - 0.8 diff --git a/node_modules/mysql/Changes.md b/node_modules/mysql/Changes.md new file mode 100644 index 0000000..325560b --- /dev/null +++ b/node_modules/mysql/Changes.md @@ -0,0 +1,193 @@ +# 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-alpha5 (2012-12-03) + +* Add mysql.escapeId to escape identifiers (closes #342) +* Allow custom escaping mode (config.queryFormat) +* Convert DATE columns to configured timezone instead of UTC (#332) +* Convert LONGLONG and NEWDECIMAL to numbers (#333) +* Fix Connection.escape() (fixes #330) +* Changed Readme ambiguity about custom type cast fallback +* Change typeCast to receive Connection instead of Connection.config.timezone +* Fix drain event having useless err parameter +* Add Connection.statistics() back from v0.9 +* Add Connection.ping() back from v0.9 + +## v2.0.0-alpha4 (2012-10-03) + +* Fix some OOB errors on resume() +* Fix quick pause() / resume() usage +* Properly parse host denied / similar errors +* Add Connection.ChangeUser functionality +* Make sure changeUser errors are fatal +* Enable formatting nested arrays for bulk inserts +* Add Connection.escape functionality +* Renamed 'close' to 'end' event +* Return parsed object instead of Buffer for GEOMETRY types +* Allow nestTables inline (using a string instead of a boolean) +* Check for ZEROFILL_FLAG and format number accordingly +* Add timezone support (default: local) +* Add custom typeCast functionality +* Export mysql column types +* Add connection flags functionality (#237) +* Exports drain event when queue finishes processing (#272, #271, #306) + +## 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) diff --git a/node_modules/mysql/License b/node_modules/mysql/License new file mode 100644 index 0000000..c7ff12a --- /dev/null +++ b/node_modules/mysql/License @@ -0,0 +1,19 @@ +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. diff --git a/node_modules/mysql/Makefile b/node_modules/mysql/Makefile new file mode 100644 index 0000000..1eab4b9 --- /dev/null +++ b/node_modules/mysql/Makefile @@ -0,0 +1,4 @@ +test: + node test/run.js + +.PHONY: test diff --git a/node_modules/mysql/Readme.md b/node_modules/mysql/Readme.md new file mode 100644 index 0000000..3ca62ba --- /dev/null +++ b/node_modules/mysql/Readme.md @@ -0,0 +1,759 @@ +# node-mysql + +[![Build Status](https://secure.travis-ci.org/felixge/node-mysql.png)](http://travis-ci.org/felixge/node-mysql) + +## Install + +```bash +npm install mysql@2.0.0-alpha5 +``` + +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 + 1 AS solution', function(err, rows, fields) { + if (err) throw err; + + console.log('The solution is: ', rows[0].solution); +}); + +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 + +If you'd like to discuss this module, or ask questions about it, please use one +of the following: + +* **Mailing list**: https://groups.google.com/forum/#!forum/node-mysql +* **IRC Channel**: #node.js (on freenode.net, I pay attention to any message + including the term `mysql`) + +## 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 password 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'`) +* `timezone`: The timezone used to store local dates. (Default: `'local'`) +* `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`) +* `queryFormat`: A custom query format function. See [Custom format](#custom-format). +* `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) +* `flags`: List of connection flags to use other than the default ones. It is + also possible to blacklist default ones. For more information, check [Connection Flags](#connection-flags). + +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&timezone=-0700'); +``` + +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 underlying 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. + +## Switching users / altering connection state + +MySQL offers a changeUser command that allows you to alter the current user and +other aspects of the connection without shutting down the underlying socket: + +```js +connection.changeUser({user : 'john'}, function(err) { + if (err) throw err; +}); +``` + +The available options for this feature are: + +* `user`: The name of the new user (defaults to the previous one). +* `password`: The password of the new user (defaults to the previous one). +* `charset`: The new charset (defaults to the previous one). +* `database`: The new database (defaults to the previous one). + +A sometimes useful side effect of this functionality is that this function also +resets any connection state (variables, transactions, etc.). + +Errors encountered during this operation are treated as fatal connection errors +by this module. + +## Server disconnects + +You may lose 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 handle such unexpected disconnects is shown below: + +```js +function handleDisconnect(connection) { + connection.on('error', function(err) { + if (!err.fatal) { + return; + } + + if (err.code !== 'PROTOCOL_CONNECTION_LOST') { + throw err; + } + + console.log('Re-connecting lost connection: ' + err.stack); + + connection = mysql.createConnection(connection.config); + handleDisconnect(connection); + connection.connect(); + }); +} + +handleDisconnect(connection); +``` + +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. + +This logic will also be part of connection pool support once I add that to this +library. + +## 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'` +* Nested arrays are turned into grouped lists (for bulk inserts), e.g. `[['a', + 'b'], ['c', 'd']]` turns into `('a', 'b'), ('c', 'd')` +* 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' + +``` + +If you feel the need to escape queries by yourself, you can also use the escaping +function directly: + +```js +var query = "SELECT * FROM posts WHERE title=" + mysql.escape("Hello MySQL"); + +console.log(query); // SELECT * FROM posts WHERE title='Hello MySQL' +``` + +## Escaping query identifiers + +If you can't trust an SQL identifier (database / table / column name) because it is +provided by a user, you should escape it with `mysql.escapeId(identifier)` like this: + +```js +var sorter = 'date'; +var query = 'SELECT * FROM posts ORDER BY ' + mysql.escapeId(sorter); + +console.log(query); // SELECT * FROM posts ORDER BY `date` +``` + +It also supports adding qualified identifiers. It will escape both parts. + +```js +var sorter = 'date'; +var query = 'SELECT * FROM posts ORDER BY ' + mysql.escapeId('posts.' + sorter); + +console.log(query); // SELECT * FROM posts ORDER BY `posts`.`date` +``` + +When you pass an Object to `.escape()` or `.query()`, `.escapeId()` is used to avoid SQL +injection in object keys. + +### Custom format + +If you prefer to have another type of query escape format, there's a connection configuration option you can use to define a custom format function. You can access the connection object if you want to use the built-in `.escape()` or any other connection function. + +Here's an example of how to implement another format: + +```js +connection.config.queryFormat = function (query, values) { + if (!values) return query; + return query.replace(/\:(\w+)/g, function (txt, key) { + if (values.hasOwnProperty(key)) { + return this.escape(values[key]); + } + return txt; + }.bind(this)); +}; + +connection.query("UPDATE posts SET title = :title", { 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 underlying 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 contributions 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: '...', + }, + }, ...] + */ +}); +``` + +Or use a string separator to have your results merged. + +```js +var options = {sql: '...', nestTables: '_'}; +connection.query(options, function(err, results) { + /* results will be an array like this now: + [{ + table1_fieldA: '...', + table1_fieldB: '...', + table2_fieldA: '...', + table2_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_CONNECTION_LOST'`). +* `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 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 Norris: +connection.on('error', function() {}); +``` + +## Exception Safety + +This module is exception safe. That means you can continue to use it, even if +one of your callback functions throws an error which you're catching using +'uncaughtException' or a domain. + +## 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 necessary) + +### 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) { + +}): +``` + +You can also pass a function and handle type casting yourself. You're given some +column information like database, table and name and also type and length. If you +just want to apply a custom type casting to a specific type you can do it and then +fallback to the default. Here's an example of converting `TINYINT(1)` to boolean: + +```js +connection.query({ + sql: '...', + typeCast: function (field, next) { + if (field.type == 'TINY' && field.length == 1) { + return (field.string() == '1'); // 1 = true, 0 = false + } + return next(); + } +}) +``` + +If you need a buffer there's also a `.buffer()` function and also a `.geometry()` one +both used by the default type cast that you can use. + +## Connection Flags + +If, for any reason, you would like to change the default connection flags, you +can use the connection option `flags`. Pass a string with a comma separated list +of items to add to the default flags. If you don't want a default flag to be used +prepend the flag with a minus sign. To add a flag that is not in the default list, don't prepend it with a plus sign, just write the flag name (case insensitive). + +**Please note that some available flags that are not default are still not supported +(e.g.: SSL, Compression). Use at your own risk.** + +### Example + +The next example blacklists FOUND_ROWS flag from default connection flags. + +```js +var connection = mysql.createConnection("mysql://localhost/test?flags=-FOUND_ROWS") +``` + +### Default Flags + +- LONG_PASSWORD +- FOUND_ROWS +- LONG_FLAG +- CONNECT_WITH_DB +- ODBC +- LOCAL_FILES +- IGNORE_SPACE +- PROTOCOL_41 +- IGNORE_SIGPIPE +- TRANSACTIONS +- RESERVED +- SECURE_CONNECTION +- MULTI_RESULTS +- MULTI_STATEMENTS (used if `multipleStatements` option is activated) + +### Other Available Flags + +- NO_SCHEMA +- COMPRESS +- INTERACTIVE +- SSL +- PS_MULTI_RESULTS +- PLUGIN_AUTH +- SSL_VERIFY_SERVER_CERT +- REMEMBER_OPTIONS + +## 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) diff --git a/node_modules/mysql/benchmark/analyze.js b/node_modules/mysql/benchmark/analyze.js new file mode 100644 index 0000000..d27ae6b --- /dev/null +++ b/node_modules/mysql/benchmark/analyze.js @@ -0,0 +1,96 @@ +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 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); +} diff --git a/node_modules/mysql/benchmark/select-100k-blog-rows.js b/node_modules/mysql/benchmark/select-100k-blog-rows.js new file mode 100644 index 0000000..d2673a0 --- /dev/null +++ b/node_modules/mysql/benchmark/select-100k-blog-rows.js @@ -0,0 +1,42 @@ +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(); + }); +}; diff --git a/node_modules/mysql/index.js b/node_modules/mysql/index.js new file mode 100644 index 0000000..6883f48 --- /dev/null +++ b/node_modules/mysql/index.js @@ -0,0 +1,12 @@ +var Connection = require('./lib/Connection'); +var ConnectionConfig = require('./lib/ConnectionConfig'); +var Types = require('./lib/protocol/constants/types'); +var SqlString = require('./lib/protocol/SqlString'); + +exports.createConnection = function(config) { + return new Connection({config: new ConnectionConfig(config)}); +}; + +exports.Types = Types; +exports.escape = SqlString.escape; +exports.escapeId = SqlString.escapeId; diff --git a/node_modules/mysql/lib/Connection.js b/node_modules/mysql/lib/Connection.js new file mode 100644 index 0000000..755cac6 --- /dev/null +++ b/node_modules/mysql/lib/Connection.js @@ -0,0 +1,155 @@ +var Net = require('net'); +var ConnectionConfig = require('./ConnectionConfig'); +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 = options.config; + + this._socket = options.socket; + this._protocol = new Protocol({config: this.config, connection: this}); + this._connectCalled = false; +} + +Connection.prototype.connect = function(cb) { + if (!this._connectCalled) { + this._connectCalled = true; + + 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('drain', this._handleProtocolDrain.bind(this)); + this._protocol.on('end', this._handleProtocolEnd.bind(this)); + } + + this._protocol.handshake(cb); +}; + +Connection.prototype.changeUser = function(options, cb){ + this._implyConnect(); + + if (typeof options === 'function') { + cb = options; + options = {}; + } + + var charsetNumber = (options.charset) + ? Config.getCharsetNumber(options.charset) + : this.config.charsetNumber; + + return this._protocol.changeUser({ + user : options.user || this.config.user, + password : options.password || this.config.password, + database : options.database || this.config.database, + charsetNumber : charsetNumber, + currentConfig : this.config + }, 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.ping = function(cb) { + this._implyConnect(); + this._protocol.ping(cb); +}; + +Connection.prototype.statistics = function(cb) { + this._implyConnect(); + this._protocol.stats(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, false, this.config.timezone); +}; + +Connection.prototype.format = function(sql, values) { + if (typeof this.config.queryFormat == "function") { + return this.config.queryFormat.call(this, sql, values, this.config.timezone); + } + return SqlString.format(sql, values, this.config.timezone); +}; + +Connection.prototype._handleNetworkError = function(err) { + this._protocol.handleNetworkError(err); +}; + +Connection.prototype._handleProtocolError = function(err) { + this.emit('error', err); +}; + +Connection.prototype._handleProtocolDrain = function() { + this.emit('drain'); +}; + +Connection.prototype._handleProtocolEnd = function(err) { + this.emit('end', err); +}; + +Connection.prototype._implyConnect = function() { + if (!this._connectCalled) { + this.connect(); + } +}; diff --git a/node_modules/mysql/lib/ConnectionConfig.js b/node_modules/mysql/lib/ConnectionConfig.js new file mode 100644 index 0000000..b328617 --- /dev/null +++ b/node_modules/mysql/lib/ConnectionConfig.js @@ -0,0 +1,111 @@ +var urlParse = require('url').parse; +var ClientConstants = require('./protocol/constants/client'); +var Charsets = require('./protocol/constants/charsets'); + +module.exports = ConnectionConfig; +function ConnectionConfig(options) { + if (typeof options === 'string') { + options = ConnectionConfig.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.timezone = options.timezone || 'local'; + this.flags = options.flags || ''; + this.queryFormat = options.queryFormat; + this.typeCast = (options.typeCast === undefined) + ? true + : options.typeCast; + + if (this.timezone[0] == " ") { + // "+" is a url encoded char for space so it + // gets translated to space when giving a + // connection string.. + this.timezone = "+" + this.timezone.substr(1); + } + + this.maxPacketSize = 0; + this.charsetNumber = (options.charset) + ? ConnectionConfig.getCharsetNumber(options.charset) + : Charsets.UTF8_GENERAL_CI; + + this.clientFlags = ConnectionConfig.mergeFlags(ConnectionConfig.getDefaultFlags(options), + options.flags || ''); +} + +ConnectionConfig.mergeFlags = function(default_flags, user_flags) { + var flags = 0x0, i; + + user_flags = (user_flags || '').toUpperCase().split(/\s*,+\s*/); + + // add default flags unless "blacklisted" + for (i in default_flags) { + if (user_flags.indexOf("-" + default_flags[i]) >= 0) continue; + + flags |= ClientConstants["CLIENT_" + default_flags[i]] || 0x0; + } + // add user flags unless already already added + for (i in user_flags) { + if (user_flags[i][0] == "-") continue; + if (default_flags.indexOf(user_flags[i]) >= 0) continue; + + flags |= ClientConstants["CLIENT_" + user_flags[i]] || 0x0; + } + + return flags; +}; + +ConnectionConfig.getDefaultFlags = function(options) { + var defaultFlags = [ "LONG_PASSWORD", "FOUND_ROWS", "LONG_FLAG", + "CONNECT_WITH_DB", "ODBC", "LOCAL_FILES", + "IGNORE_SPACE", "PROTOCOL_41", "IGNORE_SIGPIPE", + "TRANSACTIONS", "RESERVED", "SECURE_CONNECTION", + "MULTI_RESULTS" ]; + if (options && options.multipleStatements) { + defaultFlags.push("MULTI_STATEMENTS"); + } + + return defaultFlags; +}; + +ConnectionConfig.getCharsetNumber = function(charset) { + return Charsets[charset]; +}; + +ConnectionConfig.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; +}; diff --git a/node_modules/mysql/lib/protocol/Auth.js b/node_modules/mysql/lib/protocol/Auth.js new file mode 100644 index 0000000..f8738c5 --- /dev/null +++ b/node_modules/mysql/lib/protocol/Auth.js @@ -0,0 +1,165 @@ +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]); +}; diff --git a/node_modules/mysql/lib/protocol/PacketHeader.js b/node_modules/mysql/lib/protocol/PacketHeader.js new file mode 100644 index 0000000..1bb282e --- /dev/null +++ b/node_modules/mysql/lib/protocol/PacketHeader.js @@ -0,0 +1,5 @@ +module.exports = PacketHeader; +function PacketHeader(length, number) { + this.length = length; + this.number = number; +} diff --git a/node_modules/mysql/lib/protocol/PacketWriter.js b/node_modules/mysql/lib/protocol/PacketWriter.js new file mode 100644 index 0000000..7de4e5a --- /dev/null +++ b/node_modules/mysql/lib/protocol/PacketWriter.js @@ -0,0 +1,197 @@ +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); +}; diff --git a/node_modules/mysql/lib/protocol/Parser.js b/node_modules/mysql/lib/protocol/Parser.js new file mode 100644 index 0000000..906f35e --- /dev/null +++ b/node_modules/mysql/lib/protocol/Parser.js @@ -0,0 +1,366 @@ +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 = new Buffer(0); + this._longPacketBuffers = []; + this._offset = 0; + this._packetEnd = null; + this._packetHeader = null; + this._onPacket = options.onPacket || function() {}; + this._nextPacketNumber = 0; + this._encoding = 'utf-8'; + this._paused = false; +} + +Parser.prototype.write = function(buffer) { + this.append(buffer); + + while (true) { + if (this._paused) { + return; + } + + if (!this._packetHeader) { + if (this._bytesRemaining() < 4) { + break; + } + + this._packetHeader = new PacketHeader( + this.parseUnsignedNumber(3), + this.parseUnsignedNumber(1) + ); + + this._trackAndVerifyPacketNumber(this._packetHeader.number); + } + + if (this._bytesRemaining() < this._packetHeader.length) { + break; + } + + this._packetEnd = this._offset + this._packetHeader.length; + + if (this._packetHeader.length === MAX_PACKET_LENGTH) { + this._longPacketBuffers.push(this._buffer.slice(this._offset, this._packetEnd)); + + this._advanceToNextPacket(); + continue; + } + + this._combineLongPacketBuffers(); + + // Try...finally to ensure exception safety. Unfortunately this is costing + // us up to ~10% performance in some benchmarks. + var hadException = true; + try { + this._onPacket(this._packetHeader); + hadException = false; + } finally { + this._advanceToNextPacket(); + + // If we had an exception, the parser while loop will be broken out + // of after the finally block. So we need to make sure to re-enter it + // to continue parsing any bytes that may already have been received. + if (hadException) { + process.nextTick(this.write.bind(this)); + } + } + } +}; + +Parser.prototype.append = function(newBuffer) { + // If resume() is called, we don't pass a buffer to write() + if (!newBuffer) { + return; + } + + var oldBuffer = this._buffer; + var bytesRemaining = this._bytesRemaining(); + var newLength = bytesRemaining + newBuffer.length; + + var combinedBuffer = (this._offset > newLength) + ? oldBuffer.slice(0, newLength) + : new Buffer(newLength); + + oldBuffer.copy(combinedBuffer, 0, this._offset); + newBuffer.copy(combinedBuffer, bytesRemaining); + + this._buffer = combinedBuffer; + this._offset = 0; +}; + +Parser.prototype.pause = function() { + this._paused = true; +}; + +Parser.prototype.resume = function() { + this._paused = false; + + // nextTick() to avoid entering write() multiple times within the same stack + // which would cause problems as write manipulates the state of the object. + process.nextTick(this.write.bind(this)); +}; + +Parser.prototype.peak = function() { + return this._buffer[this._offset]; +}; + +Parser.prototype.parseUnsignedNumber = function(bytes) { + var bytesRead = 0; + var value = 0; + + while (bytesRead < bytes) { + var byte = this._buffer[this._offset++]; + + value += byte * Math.pow(256, bytesRead); + + bytesRead++; + } + + return value; +}; + +Parser.prototype.parseLengthCodedString = function() { + var length = this.parseLengthCodedNumber(); + + if (length === null) { + return null; + } + + return this.parseString(length); +}; + +Parser.prototype.parseLengthCodedBuffer = function() { + var length = this.parseLengthCodedNumber(); + + if (length === null) { + return null; + } + + return this.parseBuffer(length); +}; + +Parser.prototype.parseLengthCodedNumber = function() { + var byte = this._buffer[this._offset++]; + + if (byte <= 251) { + return (byte === 251) + ? null + : byte; + } + + var length; + if (byte === 252) { + length = 2; + } else if (byte === 253) { + length = 3; + } else if (byte === 254) { + length = 8; + } else { + throw new Error('parseLengthCodedNumber: Unexpected first byte: ' + byte); + } + + var value = 0; + for (var bytesRead = 0; bytesRead < length; bytesRead++) { + var byte = this._buffer[this._offset++]; + value += Math.pow(256, bytesRead) * byte; + } + + if (value >= IEEE_754_BINARY_64_PRECISION) { + throw new Error( + 'parseLengthCodedNumber: JS precision range exceeded, ' + + 'number is >= 53 bit: "' + value + '"' + ); + } + + return value; +}; + +Parser.prototype.parseFiller = function(length) { + return this.parseBuffer(length); +}; + +Parser.prototype.parseNullTerminatedBuffer = function() { + var end = this._nullByteOffset(); + var value = this._buffer.slice(this._offset, end); + this._offset = end + 1; + + return value; +}; + +Parser.prototype.parseNullTerminatedString = function() { + var end = this._nullByteOffset(); + var value = this._buffer.toString(this._encoding, this._offset, end) + this._offset = end + 1; + + return value; +}; + +Parser.prototype._nullByteOffset = function() { + var offset = this._offset; + + while (this._buffer[offset] !== 0x00) { + offset++; + + if (offset >= this._buffer.length) { + throw new Error('Offset of null terminated string not found.'); + } + } + + return offset; +}; + +Parser.prototype.parsePacketTerminatedString = function() { + var length = this._packetEnd - this._offset; + return this.parseString(length); +}; + +Parser.prototype.parseBuffer = function(length) { + var response = new Buffer(length); + this._buffer.copy(response, 0, this._offset, this._offset + length); + + this._offset += length; + return response; +}; + +Parser.prototype.parseString = function(length) { + var offset = this._offset; + var end = offset + length; + var value = this._buffer.toString(this._encoding, offset, end); + + this._offset = end; + return value; +}; + +Parser.prototype.parseGeometryValue = function() { + var buffer = this.parseLengthCodedBuffer(); + var offset = 4; + + if (buffer === null) { + return null; + } + + function parseGeometry() { + var result = null; + var byteOrder = buffer.readUInt8(offset); offset += 1; + var wkbType = byteOrder? buffer.readUInt32LE(offset) : buffer.readUInt32BE(offset); offset += 4; + switch(wkbType) { + case 1: // WKBPoint + var x = byteOrder? buffer.readDoubleLE(offset) : buffer.readDoubleBE(offset); offset += 8; + var y = byteOrder? buffer.readDoubleLE(offset) : buffer.readDoubleBE(offset); offset += 8; + result = {x: x, y: y}; + break; + case 2: // WKBLineString + var numPoints = byteOrder? buffer.readUInt32LE(offset) : buffer.readUInt32BE(offset); offset += 4; + result = []; + for(var i=numPoints;i>0;i--) { + var x = byteOrder? buffer.readDoubleLE(offset) : buffer.readDoubleBE(offset); offset += 8; + var y = byteOrder? buffer.readDoubleLE(offset) : buffer.readDoubleBE(offset); offset += 8; + result.push({x: x, y: y}); + } + break; + case 3: // WKBPolygon + var numRings = byteOrder? buffer.readUInt32LE(offset) : buffer.readUInt32BE(offset); offset += 4; + result = []; + for(var i=numRings;i>0;i--) { + var numPoints = byteOrder? buffer.readUInt32LE(offset) : buffer.readUInt32BE(offset); offset += 4; + var line = []; + for(var j=numPoints;j>0;j--) { + var x = byteOrder? buffer.readDoubleLE(offset) : buffer.readDoubleBE(offset); offset += 8; + var y = byteOrder? buffer.readDoubleLE(offset) : buffer.readDoubleBE(offset); offset += 8; + line.push({x: x, y: y}); + } + result.push(line); + } + break; + case 4: // WKBMultiPoint + case 5: // WKBMultiLineString + case 6: // WKBMultiPolygon + case 7: // WKBGeometryCollection + var num = byteOrder? buffer.readUInt32LE(offset) : buffer.readUInt32BE(offset); offset += 4; + var result = []; + for(var i=num;i>0;i--) { + result.push(parseGeometry()); + } + break; + } + return result; + } + return parseGeometry(); +} + +Parser.prototype.reachedPacketEnd = function() { + return this._offset === this._packetEnd; +}; + +Parser.prototype._bytesRemaining = function() { + return this._buffer.length - this._offset; +}; + +Parser.prototype._trackAndVerifyPacketNumber = function(number) { + if (number !== this._nextPacketNumber) { + var err = new Error( + 'Packets out of order. Got: ' + number + ' ' + + 'Expected: ' + this._nextPacketNumber + ); + + err.code = 'PROTOCOL_PACKETS_OUT_OF_ORDER'; + + throw err; + } + + this.incrementPacketNumber(); +}; + +Parser.prototype.incrementPacketNumber = function() { + var currentPacketNumber = this._nextPacketNumber; + this._nextPacketNumber = (this._nextPacketNumber + 1) % 256; + + return currentPacketNumber; +}; + +Parser.prototype.resetPacketNumber = function() { + this._nextPacketNumber = 0; +}; + +Parser.prototype.packetLength = function() { + return this._longPacketBuffers.reduce(function(length, buffer) { + return length + buffer.length; + }, this._packetHeader.length); +}; + +Parser.prototype._combineLongPacketBuffers = function() { + if (!this._longPacketBuffers.length) { + return; + } + + var trailingPacketBytes = this._buffer.length - this._packetEnd; + + var length = this._longPacketBuffers.reduce(function(length, buffer) { + return length + buffer.length; + }, this._bytesRemaining()); + + var combinedBuffer = new Buffer(length); + + var offset = this._longPacketBuffers.reduce(function(offset, buffer) { + buffer.copy(combinedBuffer, offset); + return offset + buffer.length; + }, 0); + + this._buffer.copy(combinedBuffer, offset, this._offset); + + this._buffer = combinedBuffer; + this._longPacketBuffers = []; + this._offset = 0; + this._packetEnd = this._buffer.length - trailingPacketBytes; +}; + +Parser.prototype._advanceToNextPacket = function() { + this._offset = this._packetEnd; + this._packetHeader = null; + this._packetEnd = null; +}; diff --git a/node_modules/mysql/lib/protocol/Protocol.js b/node_modules/mysql/lib/protocol/Protocol.js new file mode 100644 index 0000000..12062d1 --- /dev/null +++ b/node_modules/mysql/lib/protocol/Protocol.js @@ -0,0 +1,292 @@ +var Parser = require('./Parser'); +var Sequences = require('./sequences'); +var Packets = require('./packets'); +var Auth = require('./Auth'); +var Stream = require('stream').Stream; +var Util = require('util'); +var PacketWriter = require('./PacketWriter'); + +module.exports = Protocol; +Util.inherits(Protocol, Stream); +function Protocol(options) { + Stream.call(this); + + options = options || {}; + + this.readable = true; + this.writable = true; + + this._parser = new Parser({onPacket: this._parsePacket.bind(this)}); + this._config = options.config || {}; + this._connection = options.connection; + this._callback = null; + this._fatalError = null; + this._quitSequence = null; + this._handshakeSequence = null; + this._destroyed = false; + this._queue = []; + this._handshakeInitializationPacket = null; +} + +Protocol.prototype.write = function(buffer) { + this._parser.write(buffer); + return true; +}; + +Protocol.prototype.handshake = function(cb) { + return this._handshakeSequence = this._enqueue(new Sequences.Handshake(this._config, cb)); +}; + +Protocol.prototype.query = function(options, cb) { + return this._enqueue(new Sequences.Query(options, cb)); +}; + +Protocol.prototype.changeUser = function(options, cb) { + return this._enqueue(new Sequences.ChangeUser(options, cb)); +}; + +Protocol.prototype.ping = function(cb) { + return this._enqueue(new Sequences.Ping(cb)); +}; + +Protocol.prototype.stats = function(cb) { + return this._enqueue(new Sequences.Statistics(cb)); +}; + +Protocol.prototype.quit = function(cb) { + return this._quitSequence = this._enqueue(new Sequences.Quit(cb)); +}; + +Protocol.prototype.end = function() { + var expected = (this._quitSequence && this._queue[0] === this._quitSequence); + if (expected) { + this._quitSequence.end(); + this.emit('end'); + return; + } + + var err = new Error('Connection lost: The server closed the connection.'); + err.fatal = true; + err.code = 'PROTOCOL_CONNECTION_LOST'; + + this._delegateError(err); +}; + +Protocol.prototype.pause = function() { + this._parser.pause(); +}; + +Protocol.prototype.resume = function() { + this._parser.resume(); +}; + +Protocol.prototype._enqueue = function(sequence) { + if (!this._validateEnqueue(sequence)) { + return sequence; + } + + this._queue.push(sequence); + + var self = this; + sequence + .on('error', function(err) { + self._delegateError(err, sequence); + }) + .on('packet', function(packet) { + self._emitPacket(packet); + }) + .on('end', function() { + self._dequeue(); + }); + + if (this._queue.length === 1) { + this._parser.resetPacketNumber(); + sequence.start(); + } + + return sequence; +}; + +Protocol.prototype._validateEnqueue = function(sequence) { + var err; + var prefix = 'Cannot enqueue ' + sequence.constructor.name + ' after '; + + if (this._quitSequence) { + err = new Error(prefix + 'invoking quit.'); + err.code = 'PROTOCOL_ENQUEUE_AFTER_QUIT'; + } else if (this._destroyed) { + err = new Error(prefix + 'being destroyed.'); + err.code = 'PROTOCOL_ENQUEUE_AFTER_DESTROY'; + } else if (this._handshakeSequence && sequence.constructor === Sequences.Handshake) { + err = new Error(prefix + 'already enqueuing a Handshake.'); + err.code = 'PROTOCOL_ENQUEUE_HANDSHAKE_TWICE'; + } else { + return true; + } + + var self = this; + err.fatal = false; + + sequence + .on('error', function(err) { + self._delegateError(err, sequence); + }) + .end(err); + + return false; +}; + +Protocol.prototype._parsePacket = function() { + var sequence = this._queue[0]; + var Packet = this._determinePacket(sequence); + var packet = new Packet(); + + // Special case: Faster dispatch, and parsing done inside sequence + if (Packet === Packets.RowDataPacket) { + sequence.RowDataPacket(packet, this._parser, this._connection); + + if (this._config.debug) { + this._debugPacket(true, packet); + } + + return; + } + + packet.parse(this._parser); + + if (this._config.debug) { + this._debugPacket(true, packet); + } + + if (Packet === Packets.HandshakeInitializationPacket) { + this._handshakeInitializationPacket = packet; + } + + sequence[Packet.name](packet); +}; + +Protocol.prototype._emitPacket = function(packet) { + var packetWriter = new PacketWriter(); + packet.write(packetWriter); + this.emit('data', packetWriter.toBuffer(this._parser)); + + if (this._config.debug) { + this._debugPacket(false, packet) + } +}; + +Protocol.prototype._determinePacket = function(sequence) { + var firstByte = this._parser.peak(); + + if (sequence.determinePacket) { + var Packet = sequence.determinePacket(firstByte, this._parser); + if (Packet) { + return Packet; + } + } + + switch (firstByte) { + case 0x00: return Packets.OkPacket; + case 0xfe: return Packets.EofPacket; + case 0xff: return Packets.ErrorPacket; + } + + throw new Error('Could not determine packet, firstByte = ' + firstByte); +}; + +Protocol.prototype._dequeue = function() { + // No point in advancing the queue, we are dead + if (this._fatalError) { + return; + } + + this._queue.shift(); + + var sequence = this._queue[0]; + if (!sequence) { + this.emit('drain'); + return; + } + + this._parser.resetPacketNumber(); + + if (sequence.constructor == Sequences.ChangeUser) { + sequence.start(this._handshakeInitializationPacket); + return; + } + + sequence.start(); +}; + +Protocol.prototype.handleNetworkError = function(err) { + err.fatal = true; + + var sequence = this._queue[0]; + if (sequence) { + sequence.end(err) + } else { + this._delegateError(err); + } +}; + +Protocol.prototype._delegateError = function(err, sequence) { + // Stop delegating errors after the first fatal error + if (this._fatalError) { + return; + } + + if (err.fatal) { + this._fatalError = err; + } + + if (this._shouldErrorBubbleUp(err, sequence)) { + // Can't use regular 'error' event here as that always destroys the pipe + // between socket and protocol which is not what we want (unless the + // exception was fatal). + this.emit('unhandledError', err); + } else if (err.fatal) { + this._queue.forEach(function(sequence) { + sequence.end(err); + }); + } + + // Make sure the stream we are piping to is getting closed + if (err.fatal) { + this.emit('end', err); + } +}; + +Protocol.prototype._shouldErrorBubbleUp = function(err, sequence) { + if (sequence) { + if (sequence.hasErrorHandler()) { + return false; + } else if (!err.fatal) { + return true; + } + } + + return (err.fatal && !this._hasPendingErrorHandlers()); +}; + +Protocol.prototype._hasPendingErrorHandlers = function() { + return this._queue.some(function(sequence) { + return sequence.hasErrorHandler(); + }); +}; + +Protocol.prototype.destroy = function() { + this._destroyed = true; + this._parser.pause(); +}; + +Protocol.prototype._debugPacket = function(incoming, packet) { + var headline = (incoming) + ? '<-- ' + : '--> '; + + headline = headline + packet.constructor.name; + + console.log(headline); + console.log(packet); + console.log(''); +}; diff --git a/node_modules/mysql/lib/protocol/ResultSet.js b/node_modules/mysql/lib/protocol/ResultSet.js new file mode 100644 index 0000000..f58d74f --- /dev/null +++ b/node_modules/mysql/lib/protocol/ResultSet.js @@ -0,0 +1,7 @@ +module.exports = ResultSet; +function ResultSet(resultSetHeaderPacket) { + this.resultSetHeaderPacket = resultSetHeaderPacket; + this.fieldPackets = []; + this.eofPackets = []; + this.rows = []; +} diff --git a/node_modules/mysql/lib/protocol/SqlString.js b/node_modules/mysql/lib/protocol/SqlString.js new file mode 100644 index 0000000..a4e778f --- /dev/null +++ b/node_modules/mysql/lib/protocol/SqlString.js @@ -0,0 +1,136 @@ +var SqlString = exports; + +SqlString.escapeId = function (val, forbidQualified) { + if (forbidQualified) { + return '`' + val.replace(/`/g, '``') + '`'; + } + return '`' + val.replace(/`/g, '``').replace(/\./g, '`.`') + '`'; +}; + +SqlString.escape = function(val, stringifyObjects, timeZone) { + if (val === undefined || val === null) { + return 'NULL'; + } + + switch (typeof val) { + case 'boolean': return (val) ? 'true' : 'false'; + case 'number': return val+''; + } + + if (val instanceof Date) { + val = SqlString.dateToString(val, timeZone || "Z"); + } + + if (Buffer.isBuffer(val)) { + return SqlString.bufferToString(val); + } + + if (Array.isArray(val)) { + return SqlString.arrayToList(val, timeZone); + } + + if (typeof val === 'object') { + if (stringifyObjects) { + val = val.toString(); + } else { + return SqlString.objectToValues(val, timeZone); + } + } + + val = val.replace(/[\0\n\r\b\t\\\'\"\x1a]/g, function(s) { + switch(s) { + case "\0": return "\\0"; + case "\n": return "\\n"; + case "\r": return "\\r"; + case "\b": return "\\b"; + case "\t": return "\\t"; + case "\x1a": return "\\Z"; + default: return "\\"+s; + } + }); + return "'"+val+"'"; +}; + +SqlString.arrayToList = function(array, timeZone) { + return array.map(function(v) { + if (Array.isArray(v)) return '(' + SqlString.arrayToList(v) + ')'; + return SqlString.escape(v, true, timeZone); + }).join(', '); +}; + +SqlString.format = function(sql, values, timeZone) { + values = [].concat(values); + + return sql.replace(/\?/g, function(match) { + if (!values.length) { + return match; + } + + return SqlString.escape(values.shift(), false, timeZone); + }); +}; + +SqlString.dateToString = function(date, timeZone) { + var dt = new Date(date); + + if (timeZone != 'local') { + tz = convertTimezone(timeZone); + + dt.setTime(dt.getTime() + (dt.getTimezoneOffset() * 60000)); + if (tz !== false) { + dt.setTime(dt.getTime() + (tz * 60000)); + } + } + + var year = dt.getFullYear(); + var month = zeroPad(dt.getMonth() + 1); + var day = zeroPad(dt.getDate()); + var hour = zeroPad(dt.getHours()); + var minute = zeroPad(dt.getMinutes()); + var second = zeroPad(dt.getSeconds()); + + return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second; +}; + +SqlString.bufferToString = function(buffer) { + var hex = ''; + try { + hex = buffer.toString('hex'); + } catch (err) { + // node v0.4.x does not support hex / throws unknown encoding error + for (var i = 0; i < buffer.length; i++) { + var byte = buffer[i]; + hex += zeroPad(byte.toString(16)); + } + } + + return "X'" + hex+ "'"; +}; + +SqlString.objectToValues = function(object, timeZone) { + var values = []; + for (var key in object) { + var value = object[key]; + if(typeof value === 'function') { + continue; + } + + values.push(this.escapeId(key) + ' = ' + SqlString.escape(value, true, timeZone)); + } + + return values.join(', '); +}; + +function zeroPad(number) { + return (number < 10) ? '0' + number : number; +} + +function convertTimezone(tz) { + if (tz == "Z") return 0; + + var m = tz.match(/([\+\-\s])(\d\d):?(\d\d)?/); + if (m) { + return (m[1] == '-' ? -1 : 1) * (parseInt(m[2], 10) + ((m[3] ? parseInt(m[3], 10) : 0) / 60)) * 60; + } + return false; +} diff --git a/node_modules/mysql/lib/protocol/constants/charsets.js b/node_modules/mysql/lib/protocol/constants/charsets.js new file mode 100644 index 0000000..6aefda7 --- /dev/null +++ b/node_modules/mysql/lib/protocol/constants/charsets.js @@ -0,0 +1,128 @@ +// not sure where I got this from, will need to add a generation script for it +exports.BIG5_CHINESE_CI = 1; +exports.LATIN2_CZECH_CS = 2; +exports.DEC8_SWEDISH_CI = 3; +exports.CP850_GENERAL_CI = 4; +exports.LATIN1_GERMAN1_CI = 5; +exports.HP8_ENGLISH_CI = 6; +exports.KOI8R_GENERAL_CI = 7; +exports.LATIN1_SWEDISH_CI = 8; +exports.LATIN2_GENERAL_CI = 9; +exports.SWE7_SWEDISH_CI = 10; +exports.ASCII_GENERAL_CI = 11; +exports.UJIS_JAPANESE_CI = 12; +exports.SJIS_JAPANESE_CI = 13; +exports.CP1251_BULGARIAN_CI = 14; +exports.LATIN1_DANISH_CI = 15; +exports.HEBREW_GENERAL_CI = 16; +exports.TIS620_THAI_CI = 18; +exports.EUCKR_KOREAN_CI = 19; +exports.LATIN7_ESTONIAN_CS = 20; +exports.LATIN2_HUNGARIAN_CI = 21; +exports.KOI8U_GENERAL_CI = 22; +exports.CP1251_UKRAINIAN_CI = 23; +exports.GB2312_CHINESE_CI = 24; +exports.GREEK_GENERAL_CI = 25; +exports.CP1250_GENERAL_CI = 26; +exports.LATIN2_CROATIAN_CI = 27; +exports.GBK_CHINESE_CI = 28; +exports.CP1257_LITHUANIAN_CI = 29; +exports.LATIN5_TURKISH_CI = 30; +exports.LATIN1_GERMAN2_CI = 31; +exports.ARMSCII8_GENERAL_CI = 32; +exports.UTF8_GENERAL_CI = 33; +exports.CP1250_CZECH_CS = 34; +exports.UCS2_GENERAL_CI = 35; +exports.CP866_GENERAL_CI = 36; +exports.KEYBCS2_GENERAL_CI = 37; +exports.MACCE_GENERAL_CI = 38; +exports.MACROMAN_GENERAL_CI = 39; +exports.CP852_GENERAL_CI = 40; +exports.LATIN7_GENERAL_CI = 41; +exports.LATIN7_GENERAL_CS = 42; +exports.MACCE_BIN = 43; +exports.CP1250_CROATIAN_CI = 44; +exports.LATIN1_BIN = 47; +exports.LATIN1_GENERAL_CI = 48; +exports.LATIN1_GENERAL_CS = 49; +exports.CP1251_BIN = 50; +exports.CP1251_GENERAL_CI = 51; +exports.CP1251_GENERAL_CS = 52; +exports.MACROMAN_BIN = 53; +exports.CP1256_GENERAL_CI = 57; +exports.CP1257_BIN = 58; +exports.CP1257_GENERAL_CI = 59; +exports.BINARY = 63; +exports.ARMSCII8_BIN = 64; +exports.ASCII_BIN = 65; +exports.CP1250_BIN = 66; +exports.CP1256_BIN = 67; +exports.CP866_BIN = 68; +exports.DEC8_BIN = 69; +exports.GREEK_BIN = 70; +exports.HEBREW_BIN = 71; +exports.HP8_BIN = 72; +exports.KEYBCS2_BIN = 73; +exports.KOI8R_BIN = 74; +exports.KOI8U_BIN = 75; +exports.LATIN2_BIN = 77; +exports.LATIN5_BIN = 78; +exports.LATIN7_BIN = 79; +exports.CP850_BIN = 80; +exports.CP852_BIN = 81; +exports.SWE7_BIN = 82; +exports.UTF8_BIN = 83; +exports.BIG5_BIN = 84; +exports.EUCKR_BIN = 85; +exports.GB2312_BIN = 86; +exports.GBK_BIN = 87; +exports.SJIS_BIN = 88; +exports.TIS620_BIN = 89; +exports.UCS2_BIN = 90; +exports.UJIS_BIN = 91; +exports.GEOSTD8_GENERAL_CI = 92; +exports.GEOSTD8_BIN = 93; +exports.LATIN1_SPANISH_CI = 94; +exports.CP932_JAPANESE_CI = 95; +exports.CP932_BIN = 96; +exports.EUCJPMS_JAPANESE_CI = 97; +exports.EUCJPMS_BIN = 98; +exports.CP1250_POLISH_CI = 99; +exports.UCS2_UNICODE_CI = 128; +exports.UCS2_ICELANDIC_CI = 129; +exports.UCS2_LATVIAN_CI = 130; +exports.UCS2_ROMANIAN_CI = 131; +exports.UCS2_SLOVENIAN_CI = 132; +exports.UCS2_POLISH_CI = 133; +exports.UCS2_ESTONIAN_CI = 134; +exports.UCS2_SPANISH_CI = 135; +exports.UCS2_SWEDISH_CI = 136; +exports.UCS2_TURKISH_CI = 137; +exports.UCS2_CZECH_CI = 138; +exports.UCS2_DANISH_CI = 139; +exports.UCS2_LITHUANIAN_CI = 140; +exports.UCS2_SLOVAK_CI = 141; +exports.UCS2_SPANISH2_CI = 142; +exports.UCS2_ROMAN_CI = 143; +exports.UCS2_PERSIAN_CI = 144; +exports.UCS2_ESPERANTO_CI = 145; +exports.UCS2_HUNGARIAN_CI = 146; +exports.UTF8_UNICODE_CI = 192; +exports.UTF8_ICELANDIC_CI = 193; +exports.UTF8_LATVIAN_CI = 194; +exports.UTF8_ROMANIAN_CI = 195; +exports.UTF8_SLOVENIAN_CI = 196; +exports.UTF8_POLISH_CI = 197; +exports.UTF8_ESTONIAN_CI = 198; +exports.UTF8_SPANISH_CI = 199; +exports.UTF8_SWEDISH_CI = 200; +exports.UTF8_TURKISH_CI = 201; +exports.UTF8_CZECH_CI = 202; +exports.UTF8_DANISH_CI = 203; +exports.UTF8_LITHUANIAN_CI = 204; +exports.UTF8_SLOVAK_CI = 205; +exports.UTF8_SPANISH2_CI = 206; +exports.UTF8_ROMAN_CI = 207; +exports.UTF8_PERSIAN_CI = 208; +exports.UTF8_ESPERANTO_CI = 209; +exports.UTF8_HUNGARIAN_CI = 210; diff --git a/node_modules/mysql/lib/protocol/constants/client.js b/node_modules/mysql/lib/protocol/constants/client.js new file mode 100644 index 0000000..59aadc6 --- /dev/null +++ b/node_modules/mysql/lib/protocol/constants/client.js @@ -0,0 +1,26 @@ +// Manually extracted from mysql-5.5.23/include/mysql_com.h +exports.CLIENT_LONG_PASSWORD = 1; /* new more secure passwords */ +exports.CLIENT_FOUND_ROWS = 2; /* Found instead of affected rows */ +exports.CLIENT_LONG_FLAG = 4; /* Get all column flags */ +exports.CLIENT_CONNECT_WITH_DB = 8; /* One can specify db on connect */ +exports.CLIENT_NO_SCHEMA = 16; /* Don't allow database.table.column */ +exports.CLIENT_COMPRESS = 32; /* Can use compression protocol */ +exports.CLIENT_ODBC = 64; /* Odbc client */ +exports.CLIENT_LOCAL_FILES = 128; /* Can use LOAD DATA LOCAL */ +exports.CLIENT_IGNORE_SPACE = 256; /* Ignore spaces before '(' */ +exports.CLIENT_PROTOCOL_41 = 512; /* New 4.1 protocol */ +exports.CLIENT_INTERACTIVE = 1024; /* This is an interactive client */ +exports.CLIENT_SSL = 2048; /* Switch to SSL after handshake */ +exports.CLIENT_IGNORE_SIGPIPE = 4096; /* IGNORE sigpipes */ +exports.CLIENT_TRANSACTIONS = 8192; /* Client knows about transactions */ +exports.CLIENT_RESERVED = 16384; /* Old flag for 4.1 protocol */ +exports.CLIENT_SECURE_CONNECTION = 32768; /* New 4.1 authentication */ + +exports.CLIENT_MULTI_STATEMENTS = 65536; /* Enable/disable multi-stmt support */ +exports.CLIENT_MULTI_RESULTS = 131072; /* Enable/disable multi-results */ +exports.CLIENT_PS_MULTI_RESULTS = 262144; /* Multi-results in PS-protocol */ + +exports.CLIENT_PLUGIN_AUTH = 524288; /* Client supports plugin authentication */ + +exports.CLIENT_SSL_VERIFY_SERVER_CERT = 1073741824; +exports.CLIENT_REMEMBER_OPTIONS = 2147483648; diff --git a/node_modules/mysql/lib/protocol/constants/errors.js b/node_modules/mysql/lib/protocol/constants/errors.js new file mode 100644 index 0000000..68ed9c5 --- /dev/null +++ b/node_modules/mysql/lib/protocol/constants/errors.js @@ -0,0 +1,725 @@ +// Generated by generate-error-constants.js, do not modify by hand +exports[1000] = 'ER_HASHCHK'; +exports[1001] = 'ER_NISAMCHK'; +exports[1002] = 'ER_NO'; +exports[1003] = 'ER_YES'; +exports[1004] = 'ER_CANT_CREATE_FILE'; +exports[1005] = 'ER_CANT_CREATE_TABLE'; +exports[1006] = 'ER_CANT_CREATE_DB'; +exports[1007] = 'ER_DB_CREATE_EXISTS'; +exports[1008] = 'ER_DB_DROP_EXISTS'; +exports[1009] = 'ER_DB_DROP_DELETE'; +exports[1010] = 'ER_DB_DROP_RMDIR'; +exports[1011] = 'ER_CANT_DELETE_FILE'; +exports[1012] = 'ER_CANT_FIND_SYSTEM_REC'; +exports[1013] = 'ER_CANT_GET_STAT'; +exports[1014] = 'ER_CANT_GET_WD'; +exports[1015] = 'ER_CANT_LOCK'; +exports[1016] = 'ER_CANT_OPEN_FILE'; +exports[1017] = 'ER_FILE_NOT_FOUND'; +exports[1018] = 'ER_CANT_READ_DIR'; +exports[1019] = 'ER_CANT_SET_WD'; +exports[1020] = 'ER_CHECKREAD'; +exports[1021] = 'ER_DISK_FULL'; +exports[1022] = 'ER_DUP_KEY'; +exports[1023] = 'ER_ERROR_ON_CLOSE'; +exports[1024] = 'ER_ERROR_ON_READ'; +exports[1025] = 'ER_ERROR_ON_RENAME'; +exports[1026] = 'ER_ERROR_ON_WRITE'; +exports[1027] = 'ER_FILE_USED'; +exports[1028] = 'ER_FILSORT_ABORT'; +exports[1029] = 'ER_FORM_NOT_FOUND'; +exports[1030] = 'ER_GET_ERRNO'; +exports[1031] = 'ER_ILLEGAL_HA'; +exports[1032] = 'ER_KEY_NOT_FOUND'; +exports[1033] = 'ER_NOT_FORM_FILE'; +exports[1034] = 'ER_NOT_KEYFILE'; +exports[1035] = 'ER_OLD_KEYFILE'; +exports[1036] = 'ER_OPEN_AS_READONLY'; +exports[1037] = 'ER_OUTOFMEMORY'; +exports[1038] = 'ER_OUT_OF_SORTMEMORY'; +exports[1039] = 'ER_UNEXPECTED_EOF'; +exports[1040] = 'ER_CON_COUNT_ERROR'; +exports[1041] = 'ER_OUT_OF_RESOURCES'; +exports[1042] = 'ER_BAD_HOST_ERROR'; +exports[1043] = 'ER_HANDSHAKE_ERROR'; +exports[1044] = 'ER_DBACCESS_DENIED_ERROR'; +exports[1045] = 'ER_ACCESS_DENIED_ERROR'; +exports[1046] = 'ER_NO_DB_ERROR'; +exports[1047] = 'ER_UNKNOWN_COM_ERROR'; +exports[1048] = 'ER_BAD_NULL_ERROR'; +exports[1049] = 'ER_BAD_DB_ERROR'; +exports[1050] = 'ER_TABLE_EXISTS_ERROR'; +exports[1051] = 'ER_BAD_TABLE_ERROR'; +exports[1052] = 'ER_NON_UNIQ_ERROR'; +exports[1053] = 'ER_SERVER_SHUTDOWN'; +exports[1054] = 'ER_BAD_FIELD_ERROR'; +exports[1055] = 'ER_WRONG_FIELD_WITH_GROUP'; +exports[1056] = 'ER_WRONG_GROUP_FIELD'; +exports[1057] = 'ER_WRONG_SUM_SELECT'; +exports[1058] = 'ER_WRONG_VALUE_COUNT'; +exports[1059] = 'ER_TOO_LONG_IDENT'; +exports[1060] = 'ER_DUP_FIELDNAME'; +exports[1061] = 'ER_DUP_KEYNAME'; +exports[1062] = 'ER_DUP_ENTRY'; +exports[1063] = 'ER_WRONG_FIELD_SPEC'; +exports[1064] = 'ER_PARSE_ERROR'; +exports[1065] = 'ER_EMPTY_QUERY'; +exports[1066] = 'ER_NONUNIQ_TABLE'; +exports[1067] = 'ER_INVALID_DEFAULT'; +exports[1068] = 'ER_MULTIPLE_PRI_KEY'; +exports[1069] = 'ER_TOO_MANY_KEYS'; +exports[1070] = 'ER_TOO_MANY_KEY_PARTS'; +exports[1071] = 'ER_TOO_LONG_KEY'; +exports[1072] = 'ER_KEY_COLUMN_DOES_NOT_EXITS'; +exports[1073] = 'ER_BLOB_USED_AS_KEY'; +exports[1074] = 'ER_TOO_BIG_FIELDLENGTH'; +exports[1075] = 'ER_WRONG_AUTO_KEY'; +exports[1076] = 'ER_READY'; +exports[1077] = 'ER_NORMAL_SHUTDOWN'; +exports[1078] = 'ER_GOT_SIGNAL'; +exports[1079] = 'ER_SHUTDOWN_COMPLETE'; +exports[1080] = 'ER_FORCING_CLOSE'; +exports[1081] = 'ER_IPSOCK_ERROR'; +exports[1082] = 'ER_NO_SUCH_INDEX'; +exports[1083] = 'ER_WRONG_FIELD_TERMINATORS'; +exports[1084] = 'ER_BLOBS_AND_NO_TERMINATED'; +exports[1085] = 'ER_TEXTFILE_NOT_READABLE'; +exports[1086] = 'ER_FILE_EXISTS_ERROR'; +exports[1087] = 'ER_LOAD_INFO'; +exports[1088] = 'ER_ALTER_INFO'; +exports[1089] = 'ER_WRONG_SUB_KEY'; +exports[1090] = 'ER_CANT_REMOVE_ALL_FIELDS'; +exports[1091] = 'ER_CANT_DROP_FIELD_OR_KEY'; +exports[1092] = 'ER_INSERT_INFO'; +exports[1093] = 'ER_UPDATE_TABLE_USED'; +exports[1094] = 'ER_NO_SUCH_THREAD'; +exports[1095] = 'ER_KILL_DENIED_ERROR'; +exports[1096] = 'ER_NO_TABLES_USED'; +exports[1097] = 'ER_TOO_BIG_SET'; +exports[1098] = 'ER_NO_UNIQUE_LOGFILE'; +exports[1099] = 'ER_TABLE_NOT_LOCKED_FOR_WRITE'; +exports[1100] = 'ER_TABLE_NOT_LOCKED'; +exports[1101] = 'ER_BLOB_CANT_HAVE_DEFAULT'; +exports[1102] = 'ER_WRONG_DB_NAME'; +exports[1103] = 'ER_WRONG_TABLE_NAME'; +exports[1104] = 'ER_TOO_BIG_SELECT'; +exports[1105] = 'ER_UNKNOWN_ERROR'; +exports[1106] = 'ER_UNKNOWN_PROCEDURE'; +exports[1107] = 'ER_WRONG_PARAMCOUNT_TO_PROCEDURE'; +exports[1108] = 'ER_WRONG_PARAMETERS_TO_PROCEDURE'; +exports[1109] = 'ER_UNKNOWN_TABLE'; +exports[1110] = 'ER_FIELD_SPECIFIED_TWICE'; +exports[1111] = 'ER_INVALID_GROUP_FUNC_USE'; +exports[1112] = 'ER_UNSUPPORTED_EXTENSION'; +exports[1113] = 'ER_TABLE_MUST_HAVE_COLUMNS'; +exports[1114] = 'ER_RECORD_FILE_FULL'; +exports[1115] = 'ER_UNKNOWN_CHARACTER_SET'; +exports[1116] = 'ER_TOO_MANY_TABLES'; +exports[1117] = 'ER_TOO_MANY_FIELDS'; +exports[1118] = 'ER_TOO_BIG_ROWSIZE'; +exports[1119] = 'ER_STACK_OVERRUN'; +exports[1120] = 'ER_WRONG_OUTER_JOIN'; +exports[1121] = 'ER_NULL_COLUMN_IN_INDEX'; +exports[1122] = 'ER_CANT_FIND_UDF'; +exports[1123] = 'ER_CANT_INITIALIZE_UDF'; +exports[1124] = 'ER_UDF_NO_PATHS'; +exports[1125] = 'ER_UDF_EXISTS'; +exports[1126] = 'ER_CANT_OPEN_LIBRARY'; +exports[1127] = 'ER_CANT_FIND_DL_ENTRY'; +exports[1128] = 'ER_FUNCTION_NOT_DEFINED'; +exports[1129] = 'ER_HOST_IS_BLOCKED'; +exports[1130] = 'ER_HOST_NOT_PRIVILEGED'; +exports[1131] = 'ER_PASSWORD_ANONYMOUS_USER'; +exports[1132] = 'ER_PASSWORD_NOT_ALLOWED'; +exports[1133] = 'ER_PASSWORD_NO_MATCH'; +exports[1134] = 'ER_UPDATE_INFO'; +exports[1135] = 'ER_CANT_CREATE_THREAD'; +exports[1136] = 'ER_WRONG_VALUE_COUNT_ON_ROW'; +exports[1137] = 'ER_CANT_REOPEN_TABLE'; +exports[1138] = 'ER_INVALID_USE_OF_NULL'; +exports[1139] = 'ER_REGEXP_ERROR'; +exports[1140] = 'ER_MIX_OF_GROUP_FUNC_AND_FIELDS'; +exports[1141] = 'ER_NONEXISTING_GRANT'; +exports[1142] = 'ER_TABLEACCESS_DENIED_ERROR'; +exports[1143] = 'ER_COLUMNACCESS_DENIED_ERROR'; +exports[1144] = 'ER_ILLEGAL_GRANT_FOR_TABLE'; +exports[1145] = 'ER_GRANT_WRONG_HOST_OR_USER'; +exports[1146] = 'ER_NO_SUCH_TABLE'; +exports[1147] = 'ER_NONEXISTING_TABLE_GRANT'; +exports[1148] = 'ER_NOT_ALLOWED_COMMAND'; +exports[1149] = 'ER_SYNTAX_ERROR'; +exports[1150] = 'ER_DELAYED_CANT_CHANGE_LOCK'; +exports[1151] = 'ER_TOO_MANY_DELAYED_THREADS'; +exports[1152] = 'ER_ABORTING_CONNECTION'; +exports[1153] = 'ER_NET_PACKET_TOO_LARGE'; +exports[1154] = 'ER_NET_READ_ERROR_FROM_PIPE'; +exports[1155] = 'ER_NET_FCNTL_ERROR'; +exports[1156] = 'ER_NET_PACKETS_OUT_OF_ORDER'; +exports[1157] = 'ER_NET_UNCOMPRESS_ERROR'; +exports[1158] = 'ER_NET_READ_ERROR'; +exports[1159] = 'ER_NET_READ_INTERRUPTED'; +exports[1160] = 'ER_NET_ERROR_ON_WRITE'; +exports[1161] = 'ER_NET_WRITE_INTERRUPTED'; +exports[1162] = 'ER_TOO_LONG_STRING'; +exports[1163] = 'ER_TABLE_CANT_HANDLE_BLOB'; +exports[1164] = 'ER_TABLE_CANT_HANDLE_AUTO_INCREMENT'; +exports[1165] = 'ER_DELAYED_INSERT_TABLE_LOCKED'; +exports[1166] = 'ER_WRONG_COLUMN_NAME'; +exports[1167] = 'ER_WRONG_KEY_COLUMN'; +exports[1168] = 'ER_WRONG_MRG_TABLE'; +exports[1169] = 'ER_DUP_UNIQUE'; +exports[1170] = 'ER_BLOB_KEY_WITHOUT_LENGTH'; +exports[1171] = 'ER_PRIMARY_CANT_HAVE_NULL'; +exports[1172] = 'ER_TOO_MANY_ROWS'; +exports[1173] = 'ER_REQUIRES_PRIMARY_KEY'; +exports[1174] = 'ER_NO_RAID_COMPILED'; +exports[1175] = 'ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE'; +exports[1176] = 'ER_KEY_DOES_NOT_EXITS'; +exports[1177] = 'ER_CHECK_NO_SUCH_TABLE'; +exports[1178] = 'ER_CHECK_NOT_IMPLEMENTED'; +exports[1179] = 'ER_CANT_DO_THIS_DURING_AN_TRANSACTION'; +exports[1180] = 'ER_ERROR_DURING_COMMIT'; +exports[1181] = 'ER_ERROR_DURING_ROLLBACK'; +exports[1182] = 'ER_ERROR_DURING_FLUSH_LOGS'; +exports[1183] = 'ER_ERROR_DURING_CHECKPOINT'; +exports[1184] = 'ER_NEW_ABORTING_CONNECTION'; +exports[1185] = 'ER_DUMP_NOT_IMPLEMENTED'; +exports[1186] = 'ER_FLUSH_MASTER_BINLOG_CLOSED'; +exports[1187] = 'ER_INDEX_REBUILD'; +exports[1188] = 'ER_MASTER'; +exports[1189] = 'ER_MASTER_NET_READ'; +exports[1190] = 'ER_MASTER_NET_WRITE'; +exports[1191] = 'ER_FT_MATCHING_KEY_NOT_FOUND'; +exports[1192] = 'ER_LOCK_OR_ACTIVE_TRANSACTION'; +exports[1193] = 'ER_UNKNOWN_SYSTEM_VARIABLE'; +exports[1194] = 'ER_CRASHED_ON_USAGE'; +exports[1195] = 'ER_CRASHED_ON_REPAIR'; +exports[1196] = 'ER_WARNING_NOT_COMPLETE_ROLLBACK'; +exports[1197] = 'ER_TRANS_CACHE_FULL'; +exports[1198] = 'ER_SLAVE_MUST_STOP'; +exports[1199] = 'ER_SLAVE_NOT_RUNNING'; +exports[1200] = 'ER_BAD_SLAVE'; +exports[1201] = 'ER_MASTER_INFO'; +exports[1202] = 'ER_SLAVE_THREAD'; +exports[1203] = 'ER_TOO_MANY_USER_CONNECTIONS'; +exports[1204] = 'ER_SET_CONSTANTS_ONLY'; +exports[1205] = 'ER_LOCK_WAIT_TIMEOUT'; +exports[1206] = 'ER_LOCK_TABLE_FULL'; +exports[1207] = 'ER_READ_ONLY_TRANSACTION'; +exports[1208] = 'ER_DROP_DB_WITH_READ_LOCK'; +exports[1209] = 'ER_CREATE_DB_WITH_READ_LOCK'; +exports[1210] = 'ER_WRONG_ARGUMENTS'; +exports[1211] = 'ER_NO_PERMISSION_TO_CREATE_USER'; +exports[1212] = 'ER_UNION_TABLES_IN_DIFFERENT_DIR'; +exports[1213] = 'ER_LOCK_DEADLOCK'; +exports[1214] = 'ER_TABLE_CANT_HANDLE_FT'; +exports[1215] = 'ER_CANNOT_ADD_FOREIGN'; +exports[1216] = 'ER_NO_REFERENCED_ROW'; +exports[1217] = 'ER_ROW_IS_REFERENCED'; +exports[1218] = 'ER_CONNECT_TO_MASTER'; +exports[1219] = 'ER_QUERY_ON_MASTER'; +exports[1220] = 'ER_ERROR_WHEN_EXECUTING_COMMAND'; +exports[1221] = 'ER_WRONG_USAGE'; +exports[1222] = 'ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT'; +exports[1223] = 'ER_CANT_UPDATE_WITH_READLOCK'; +exports[1224] = 'ER_MIXING_NOT_ALLOWED'; +exports[1225] = 'ER_DUP_ARGUMENT'; +exports[1226] = 'ER_USER_LIMIT_REACHED'; +exports[1227] = 'ER_SPECIFIC_ACCESS_DENIED_ERROR'; +exports[1228] = 'ER_LOCAL_VARIABLE'; +exports[1229] = 'ER_GLOBAL_VARIABLE'; +exports[1230] = 'ER_NO_DEFAULT'; +exports[1231] = 'ER_WRONG_VALUE_FOR_VAR'; +exports[1232] = 'ER_WRONG_TYPE_FOR_VAR'; +exports[1233] = 'ER_VAR_CANT_BE_READ'; +exports[1234] = 'ER_CANT_USE_OPTION_HERE'; +exports[1235] = 'ER_NOT_SUPPORTED_YET'; +exports[1236] = 'ER_MASTER_FATAL_ERROR_READING_BINLOG'; +exports[1237] = 'ER_SLAVE_IGNORED_TABLE'; +exports[1238] = 'ER_INCORRECT_GLOBAL_LOCAL_VAR'; +exports[1239] = 'ER_WRONG_FK_DEF'; +exports[1240] = 'ER_KEY_REF_DO_NOT_MATCH_TABLE_REF'; +exports[1241] = 'ER_OPERAND_COLUMNS'; +exports[1242] = 'ER_SUBQUERY_NO_'; +exports[1243] = 'ER_UNKNOWN_STMT_HANDLER'; +exports[1244] = 'ER_CORRUPT_HELP_DB'; +exports[1245] = 'ER_CYCLIC_REFERENCE'; +exports[1246] = 'ER_AUTO_CONVERT'; +exports[1247] = 'ER_ILLEGAL_REFERENCE'; +exports[1248] = 'ER_DERIVED_MUST_HAVE_ALIAS'; +exports[1249] = 'ER_SELECT_REDUCED'; +exports[1250] = 'ER_TABLENAME_NOT_ALLOWED_HERE'; +exports[1251] = 'ER_NOT_SUPPORTED_AUTH_MODE'; +exports[1252] = 'ER_SPATIAL_CANT_HAVE_NULL'; +exports[1253] = 'ER_COLLATION_CHARSET_MISMATCH'; +exports[1254] = 'ER_SLAVE_WAS_RUNNING'; +exports[1255] = 'ER_SLAVE_WAS_NOT_RUNNING'; +exports[1256] = 'ER_TOO_BIG_FOR_UNCOMPRESS'; +exports[1257] = 'ER_ZLIB_Z_MEM_ERROR'; +exports[1258] = 'ER_ZLIB_Z_BUF_ERROR'; +exports[1259] = 'ER_ZLIB_Z_DATA_ERROR'; +exports[1260] = 'ER_CUT_VALUE_GROUP_CONCAT'; +exports[1261] = 'ER_WARN_TOO_FEW_RECORDS'; +exports[1262] = 'ER_WARN_TOO_MANY_RECORDS'; +exports[1263] = 'ER_WARN_NULL_TO_NOTNULL'; +exports[1264] = 'ER_WARN_DATA_OUT_OF_RANGE'; +exports[1265] = 'WARN_DATA_TRUNCATED'; +exports[1266] = 'ER_WARN_USING_OTHER_HANDLER'; +exports[1267] = 'ER_CANT_AGGREGATE_'; +exports[1268] = 'ER_DROP_USER'; +exports[1269] = 'ER_REVOKE_GRANTS'; +exports[1270] = 'ER_CANT_AGGREGATE_'; +exports[1271] = 'ER_CANT_AGGREGATE_NCOLLATIONS'; +exports[1272] = 'ER_VARIABLE_IS_NOT_STRUCT'; +exports[1273] = 'ER_UNKNOWN_COLLATION'; +exports[1274] = 'ER_SLAVE_IGNORED_SSL_PARAMS'; +exports[1275] = 'ER_SERVER_IS_IN_SECURE_AUTH_MODE'; +exports[1276] = 'ER_WARN_FIELD_RESOLVED'; +exports[1277] = 'ER_BAD_SLAVE_UNTIL_COND'; +exports[1278] = 'ER_MISSING_SKIP_SLAVE'; +exports[1279] = 'ER_UNTIL_COND_IGNORED'; +exports[1280] = 'ER_WRONG_NAME_FOR_INDEX'; +exports[1281] = 'ER_WRONG_NAME_FOR_CATALOG'; +exports[1282] = 'ER_WARN_QC_RESIZE'; +exports[1283] = 'ER_BAD_FT_COLUMN'; +exports[1284] = 'ER_UNKNOWN_KEY_CACHE'; +exports[1285] = 'ER_WARN_HOSTNAME_WONT_WORK'; +exports[1286] = 'ER_UNKNOWN_STORAGE_ENGINE'; +exports[1287] = 'ER_WARN_DEPRECATED_SYNTAX'; +exports[1288] = 'ER_NON_UPDATABLE_TABLE'; +exports[1289] = 'ER_FEATURE_DISABLED'; +exports[1290] = 'ER_OPTION_PREVENTS_STATEMENT'; +exports[1291] = 'ER_DUPLICATED_VALUE_IN_TYPE'; +exports[1292] = 'ER_TRUNCATED_WRONG_VALUE'; +exports[1293] = 'ER_TOO_MUCH_AUTO_TIMESTAMP_COLS'; +exports[1294] = 'ER_INVALID_ON_UPDATE'; +exports[1295] = 'ER_UNSUPPORTED_PS'; +exports[1296] = 'ER_GET_ERRMSG'; +exports[1297] = 'ER_GET_TEMPORARY_ERRMSG'; +exports[1298] = 'ER_UNKNOWN_TIME_ZONE'; +exports[1299] = 'ER_WARN_INVALID_TIMESTAMP'; +exports[1300] = 'ER_INVALID_CHARACTER_STRING'; +exports[1301] = 'ER_WARN_ALLOWED_PACKET_OVERFLOWED'; +exports[1302] = 'ER_CONFLICTING_DECLARATIONS'; +exports[1303] = 'ER_SP_NO_RECURSIVE_CREATE'; +exports[1304] = 'ER_SP_ALREADY_EXISTS'; +exports[1305] = 'ER_SP_DOES_NOT_EXIST'; +exports[1306] = 'ER_SP_DROP_FAILED'; +exports[1307] = 'ER_SP_STORE_FAILED'; +exports[1308] = 'ER_SP_LILABEL_MISMATCH'; +exports[1309] = 'ER_SP_LABEL_REDEFINE'; +exports[1310] = 'ER_SP_LABEL_MISMATCH'; +exports[1311] = 'ER_SP_UNINIT_VAR'; +exports[1312] = 'ER_SP_BADSELECT'; +exports[1313] = 'ER_SP_BADRETURN'; +exports[1314] = 'ER_SP_BADSTATEMENT'; +exports[1315] = 'ER_UPDATE_LOG_DEPRECATED_IGNORED'; +exports[1316] = 'ER_UPDATE_LOG_DEPRECATED_TRANSLATED'; +exports[1317] = 'ER_QUERY_INTERRUPTED'; +exports[1318] = 'ER_SP_WRONG_NO_OF_ARGS'; +exports[1319] = 'ER_SP_COND_MISMATCH'; +exports[1320] = 'ER_SP_NORETURN'; +exports[1321] = 'ER_SP_NORETURNEND'; +exports[1322] = 'ER_SP_BAD_CURSOR_QUERY'; +exports[1323] = 'ER_SP_BAD_CURSOR_SELECT'; +exports[1324] = 'ER_SP_CURSOR_MISMATCH'; +exports[1325] = 'ER_SP_CURSOR_ALREADY_OPEN'; +exports[1326] = 'ER_SP_CURSOR_NOT_OPEN'; +exports[1327] = 'ER_SP_UNDECLARED_VAR'; +exports[1328] = 'ER_SP_WRONG_NO_OF_FETCH_ARGS'; +exports[1329] = 'ER_SP_FETCH_NO_DATA'; +exports[1330] = 'ER_SP_DUP_PARAM'; +exports[1331] = 'ER_SP_DUP_VAR'; +exports[1332] = 'ER_SP_DUP_COND'; +exports[1333] = 'ER_SP_DUP_CURS'; +exports[1334] = 'ER_SP_CANT_ALTER'; +exports[1335] = 'ER_SP_SUBSELECT_NYI'; +exports[1336] = 'ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG'; +exports[1337] = 'ER_SP_VARCOND_AFTER_CURSHNDLR'; +exports[1338] = 'ER_SP_CURSOR_AFTER_HANDLER'; +exports[1339] = 'ER_SP_CASE_NOT_FOUND'; +exports[1340] = 'ER_FPARSER_TOO_BIG_FILE'; +exports[1341] = 'ER_FPARSER_BAD_HEADER'; +exports[1342] = 'ER_FPARSER_EOF_IN_COMMENT'; +exports[1343] = 'ER_FPARSER_ERROR_IN_PARAMETER'; +exports[1344] = 'ER_FPARSER_EOF_IN_UNKNOWN_PARAMETER'; +exports[1345] = 'ER_VIEW_NO_EXPLAIN'; +exports[1346] = 'ER_FRM_UNKNOWN_TYPE'; +exports[1347] = 'ER_WRONG_OBJECT'; +exports[1348] = 'ER_NONUPDATEABLE_COLUMN'; +exports[1349] = 'ER_VIEW_SELECT_DERIVED'; +exports[1350] = 'ER_VIEW_SELECT_CLAUSE'; +exports[1351] = 'ER_VIEW_SELECT_VARIABLE'; +exports[1352] = 'ER_VIEW_SELECT_TMPTABLE'; +exports[1353] = 'ER_VIEW_WRONG_LIST'; +exports[1354] = 'ER_WARN_VIEW_MERGE'; +exports[1355] = 'ER_WARN_VIEW_WITHOUT_KEY'; +exports[1356] = 'ER_VIEW_INVALID'; +exports[1357] = 'ER_SP_NO_DROP_SP'; +exports[1358] = 'ER_SP_GOTO_IN_HNDLR'; +exports[1359] = 'ER_TRG_ALREADY_EXISTS'; +exports[1360] = 'ER_TRG_DOES_NOT_EXIST'; +exports[1361] = 'ER_TRG_ON_VIEW_OR_TEMP_TABLE'; +exports[1362] = 'ER_TRG_CANT_CHANGE_ROW'; +exports[1363] = 'ER_TRG_NO_SUCH_ROW_IN_TRG'; +exports[1364] = 'ER_NO_DEFAULT_FOR_FIELD'; +exports[1365] = 'ER_DIVISION_BY_ZERO'; +exports[1366] = 'ER_TRUNCATED_WRONG_VALUE_FOR_FIELD'; +exports[1367] = 'ER_ILLEGAL_VALUE_FOR_TYPE'; +exports[1368] = 'ER_VIEW_NONUPD_CHECK'; +exports[1369] = 'ER_VIEW_CHECK_FAILED'; +exports[1370] = 'ER_PROCACCESS_DENIED_ERROR'; +exports[1371] = 'ER_RELAY_LOG_FAIL'; +exports[1372] = 'ER_PASSWD_LENGTH'; +exports[1373] = 'ER_UNKNOWN_TARGET_BINLOG'; +exports[1374] = 'ER_IO_ERR_LOG_INDEX_READ'; +exports[1375] = 'ER_BINLOG_PURGE_PROHIBITED'; +exports[1376] = 'ER_FSEEK_FAIL'; +exports[1377] = 'ER_BINLOG_PURGE_FATAL_ERR'; +exports[1378] = 'ER_LOG_IN_USE'; +exports[1379] = 'ER_LOG_PURGE_UNKNOWN_ERR'; +exports[1380] = 'ER_RELAY_LOG_INIT'; +exports[1381] = 'ER_NO_BINARY_LOGGING'; +exports[1382] = 'ER_RESERVED_SYNTAX'; +exports[1383] = 'ER_WSAS_FAILED'; +exports[1384] = 'ER_DIFF_GROUPS_PROC'; +exports[1385] = 'ER_NO_GROUP_FOR_PROC'; +exports[1386] = 'ER_ORDER_WITH_PROC'; +exports[1387] = 'ER_LOGGING_PROHIBIT_CHANGING_OF'; +exports[1388] = 'ER_NO_FILE_MAPPING'; +exports[1389] = 'ER_WRONG_MAGIC'; +exports[1390] = 'ER_PS_MANY_PARAM'; +exports[1391] = 'ER_KEY_PART_'; +exports[1392] = 'ER_VIEW_CHECKSUM'; +exports[1393] = 'ER_VIEW_MULTIUPDATE'; +exports[1394] = 'ER_VIEW_NO_INSERT_FIELD_LIST'; +exports[1395] = 'ER_VIEW_DELETE_MERGE_VIEW'; +exports[1396] = 'ER_CANNOT_USER'; +exports[1397] = 'ER_XAER_NOTA'; +exports[1398] = 'ER_XAER_INVAL'; +exports[1399] = 'ER_XAER_RMFAIL'; +exports[1400] = 'ER_XAER_OUTSIDE'; +exports[1401] = 'ER_XAER_RMERR'; +exports[1402] = 'ER_XA_RBROLLBACK'; +exports[1403] = 'ER_NONEXISTING_PROC_GRANT'; +exports[1404] = 'ER_PROC_AUTO_GRANT_FAIL'; +exports[1405] = 'ER_PROC_AUTO_REVOKE_FAIL'; +exports[1406] = 'ER_DATA_TOO_LONG'; +exports[1407] = 'ER_SP_BAD_SQLSTATE'; +exports[1408] = 'ER_STARTUP'; +exports[1409] = 'ER_LOAD_FROM_FIXED_SIZE_ROWS_TO_VAR'; +exports[1410] = 'ER_CANT_CREATE_USER_WITH_GRANT'; +exports[1411] = 'ER_WRONG_VALUE_FOR_TYPE'; +exports[1412] = 'ER_TABLE_DEF_CHANGED'; +exports[1413] = 'ER_SP_DUP_HANDLER'; +exports[1414] = 'ER_SP_NOT_VAR_ARG'; +exports[1415] = 'ER_SP_NO_RETSET'; +exports[1416] = 'ER_CANT_CREATE_GEOMETRY_OBJECT'; +exports[1417] = 'ER_FAILED_ROUTINE_BREAK_BINLOG'; +exports[1418] = 'ER_BINLOG_UNSAFE_ROUTINE'; +exports[1419] = 'ER_BINLOG_CREATE_ROUTINE_NEED_SUPER'; +exports[1420] = 'ER_EXEC_STMT_WITH_OPEN_CURSOR'; +exports[1421] = 'ER_STMT_HAS_NO_OPEN_CURSOR'; +exports[1422] = 'ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG'; +exports[1423] = 'ER_NO_DEFAULT_FOR_VIEW_FIELD'; +exports[1424] = 'ER_SP_NO_RECURSION'; +exports[1425] = 'ER_TOO_BIG_SCALE'; +exports[1426] = 'ER_TOO_BIG_PRECISION'; +exports[1427] = 'ER_M_BIGGER_THAN_D'; +exports[1428] = 'ER_WRONG_LOCK_OF_SYSTEM_TABLE'; +exports[1429] = 'ER_CONNECT_TO_FOREIGN_DATA_SOURCE'; +exports[1430] = 'ER_QUERY_ON_FOREIGN_DATA_SOURCE'; +exports[1431] = 'ER_FOREIGN_DATA_SOURCE_DOESNT_EXIST'; +exports[1432] = 'ER_FOREIGN_DATA_STRING_INVALID_CANT_CREATE'; +exports[1433] = 'ER_FOREIGN_DATA_STRING_INVALID'; +exports[1434] = 'ER_CANT_CREATE_FEDERATED_TABLE'; +exports[1435] = 'ER_TRG_IN_WRONG_SCHEMA'; +exports[1436] = 'ER_STACK_OVERRUN_NEED_MORE'; +exports[1437] = 'ER_TOO_LONG_BODY'; +exports[1438] = 'ER_WARN_CANT_DROP_DEFAULT_KEYCACHE'; +exports[1439] = 'ER_TOO_BIG_DISPLAYWIDTH'; +exports[1440] = 'ER_XAER_DUPID'; +exports[1441] = 'ER_DATETIME_FUNCTION_OVERFLOW'; +exports[1442] = 'ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG'; +exports[1443] = 'ER_VIEW_PREVENT_UPDATE'; +exports[1444] = 'ER_PS_NO_RECURSION'; +exports[1445] = 'ER_SP_CANT_SET_AUTOCOMMIT'; +exports[1446] = 'ER_MALFORMED_DEFINER'; +exports[1447] = 'ER_VIEW_FRM_NO_USER'; +exports[1448] = 'ER_VIEW_OTHER_USER'; +exports[1449] = 'ER_NO_SUCH_USER'; +exports[1450] = 'ER_FORBID_SCHEMA_CHANGE'; +exports[1451] = 'ER_ROW_IS_REFERENCED_'; +exports[1452] = 'ER_NO_REFERENCED_ROW_'; +exports[1453] = 'ER_SP_BAD_VAR_SHADOW'; +exports[1454] = 'ER_TRG_NO_DEFINER'; +exports[1455] = 'ER_OLD_FILE_FORMAT'; +exports[1456] = 'ER_SP_RECURSION_LIMIT'; +exports[1457] = 'ER_SP_PROC_TABLE_CORRUPT'; +exports[1458] = 'ER_SP_WRONG_NAME'; +exports[1459] = 'ER_TABLE_NEEDS_UPGRADE'; +exports[1460] = 'ER_SP_NO_AGGREGATE'; +exports[1461] = 'ER_MAX_PREPARED_STMT_COUNT_REACHED'; +exports[1462] = 'ER_VIEW_RECURSIVE'; +exports[1463] = 'ER_NON_GROUPING_FIELD_USED'; +exports[1464] = 'ER_TABLE_CANT_HANDLE_SPKEYS'; +exports[1465] = 'ER_NO_TRIGGERS_ON_SYSTEM_SCHEMA'; +exports[1466] = 'ER_REMOVED_SPACES'; +exports[1467] = 'ER_AUTOINC_READ_FAILED'; +exports[1468] = 'ER_USERNAME'; +exports[1469] = 'ER_HOSTNAME'; +exports[1470] = 'ER_WRONG_STRING_LENGTH'; +exports[1471] = 'ER_NON_INSERTABLE_TABLE'; +exports[1472] = 'ER_ADMIN_WRONG_MRG_TABLE'; +exports[1473] = 'ER_TOO_HIGH_LEVEL_OF_NESTING_FOR_SELECT'; +exports[1474] = 'ER_NAME_BECOMES_EMPTY'; +exports[1475] = 'ER_AMBIGUOUS_FIELD_TERM'; +exports[1476] = 'ER_FOREIGN_SERVER_EXISTS'; +exports[1477] = 'ER_FOREIGN_SERVER_DOESNT_EXIST'; +exports[1478] = 'ER_ILLEGAL_HA_CREATE_OPTION'; +exports[1479] = 'ER_PARTITION_REQUIRES_VALUES_ERROR'; +exports[1480] = 'ER_PARTITION_WRONG_VALUES_ERROR'; +exports[1481] = 'ER_PARTITION_MAXVALUE_ERROR'; +exports[1482] = 'ER_PARTITION_SUBPARTITION_ERROR'; +exports[1483] = 'ER_PARTITION_SUBPART_MIX_ERROR'; +exports[1484] = 'ER_PARTITION_WRONG_NO_PART_ERROR'; +exports[1485] = 'ER_PARTITION_WRONG_NO_SUBPART_ERROR'; +exports[1486] = 'ER_WRONG_EXPR_IN_PARTITION_FUNC_ERROR'; +exports[1487] = 'ER_NO_CONST_EXPR_IN_RANGE_OR_LIST_ERROR'; +exports[1488] = 'ER_FIELD_NOT_FOUND_PART_ERROR'; +exports[1489] = 'ER_LIST_OF_FIELDS_ONLY_IN_HASH_ERROR'; +exports[1490] = 'ER_INCONSISTENT_PARTITION_INFO_ERROR'; +exports[1491] = 'ER_PARTITION_FUNC_NOT_ALLOWED_ERROR'; +exports[1492] = 'ER_PARTITIONS_MUST_BE_DEFINED_ERROR'; +exports[1493] = 'ER_RANGE_NOT_INCREASING_ERROR'; +exports[1494] = 'ER_INCONSISTENT_TYPE_OF_FUNCTIONS_ERROR'; +exports[1495] = 'ER_MULTIPLE_DEF_CONST_IN_LIST_PART_ERROR'; +exports[1496] = 'ER_PARTITION_ENTRY_ERROR'; +exports[1497] = 'ER_MIX_HANDLER_ERROR'; +exports[1498] = 'ER_PARTITION_NOT_DEFINED_ERROR'; +exports[1499] = 'ER_TOO_MANY_PARTITIONS_ERROR'; +exports[1500] = 'ER_SUBPARTITION_ERROR'; +exports[1501] = 'ER_CANT_CREATE_HANDLER_FILE'; +exports[1502] = 'ER_BLOB_FIELD_IN_PART_FUNC_ERROR'; +exports[1503] = 'ER_UNIQUE_KEY_NEED_ALL_FIELDS_IN_PF'; +exports[1504] = 'ER_NO_PARTS_ERROR'; +exports[1505] = 'ER_PARTITION_MGMT_ON_NONPARTITIONED'; +exports[1506] = 'ER_FOREIGN_KEY_ON_PARTITIONED'; +exports[1507] = 'ER_DROP_PARTITION_NON_EXISTENT'; +exports[1508] = 'ER_DROP_LAST_PARTITION'; +exports[1509] = 'ER_COALESCE_ONLY_ON_HASH_PARTITION'; +exports[1510] = 'ER_REORG_HASH_ONLY_ON_SAME_NO'; +exports[1511] = 'ER_REORG_NO_PARAM_ERROR'; +exports[1512] = 'ER_ONLY_ON_RANGE_LIST_PARTITION'; +exports[1513] = 'ER_ADD_PARTITION_SUBPART_ERROR'; +exports[1514] = 'ER_ADD_PARTITION_NO_NEW_PARTITION'; +exports[1515] = 'ER_COALESCE_PARTITION_NO_PARTITION'; +exports[1516] = 'ER_REORG_PARTITION_NOT_EXIST'; +exports[1517] = 'ER_SAME_NAME_PARTITION'; +exports[1518] = 'ER_NO_BINLOG_ERROR'; +exports[1519] = 'ER_CONSECUTIVE_REORG_PARTITIONS'; +exports[1520] = 'ER_REORG_OUTSIDE_RANGE'; +exports[1521] = 'ER_PARTITION_FUNCTION_FAILURE'; +exports[1522] = 'ER_PART_STATE_ERROR'; +exports[1523] = 'ER_LIMITED_PART_RANGE'; +exports[1524] = 'ER_PLUGIN_IS_NOT_LOADED'; +exports[1525] = 'ER_WRONG_VALUE'; +exports[1526] = 'ER_NO_PARTITION_FOR_GIVEN_VALUE'; +exports[1527] = 'ER_FILEGROUP_OPTION_ONLY_ONCE'; +exports[1528] = 'ER_CREATE_FILEGROUP_FAILED'; +exports[1529] = 'ER_DROP_FILEGROUP_FAILED'; +exports[1530] = 'ER_TABLESPACE_AUTO_EXTEND_ERROR'; +exports[1531] = 'ER_WRONG_SIZE_NUMBER'; +exports[1532] = 'ER_SIZE_OVERFLOW_ERROR'; +exports[1533] = 'ER_ALTER_FILEGROUP_FAILED'; +exports[1534] = 'ER_BINLOG_ROW_LOGGING_FAILED'; +exports[1535] = 'ER_BINLOG_ROW_WRONG_TABLE_DEF'; +exports[1536] = 'ER_BINLOG_ROW_RBR_TO_SBR'; +exports[1537] = 'ER_EVENT_ALREADY_EXISTS'; +exports[1538] = 'ER_EVENT_STORE_FAILED'; +exports[1539] = 'ER_EVENT_DOES_NOT_EXIST'; +exports[1540] = 'ER_EVENT_CANT_ALTER'; +exports[1541] = 'ER_EVENT_DROP_FAILED'; +exports[1542] = 'ER_EVENT_INTERVAL_NOT_POSITIVE_OR_TOO_BIG'; +exports[1543] = 'ER_EVENT_ENDS_BEFORE_STARTS'; +exports[1544] = 'ER_EVENT_EXEC_TIME_IN_THE_PAST'; +exports[1545] = 'ER_EVENT_OPEN_TABLE_FAILED'; +exports[1546] = 'ER_EVENT_NEITHER_M_EXPR_NOR_M_AT'; +exports[1547] = 'ER_COL_COUNT_DOESNT_MATCH_CORRUPTED'; +exports[1548] = 'ER_CANNOT_LOAD_FROM_TABLE'; +exports[1549] = 'ER_EVENT_CANNOT_DELETE'; +exports[1550] = 'ER_EVENT_COMPILE_ERROR'; +exports[1551] = 'ER_EVENT_SAME_NAME'; +exports[1552] = 'ER_EVENT_DATA_TOO_LONG'; +exports[1553] = 'ER_DROP_INDEX_FK'; +exports[1554] = 'ER_WARN_DEPRECATED_SYNTAX_WITH_VER'; +exports[1555] = 'ER_CANT_WRITE_LOCK_LOG_TABLE'; +exports[1556] = 'ER_CANT_LOCK_LOG_TABLE'; +exports[1557] = 'ER_FOREIGN_DUPLICATE_KEY'; +exports[1558] = 'ER_COL_COUNT_DOESNT_MATCH_PLEASE_UPDATE'; +exports[1559] = 'ER_TEMP_TABLE_PREVENTS_SWITCH_OUT_OF_RBR'; +exports[1560] = 'ER_STORED_FUNCTION_PREVENTS_SWITCH_BINLOG_FORMAT'; +exports[1561] = 'ER_NDB_CANT_SWITCH_BINLOG_FORMAT'; +exports[1562] = 'ER_PARTITION_NO_TEMPORARY'; +exports[1563] = 'ER_PARTITION_CONST_DOMAIN_ERROR'; +exports[1564] = 'ER_PARTITION_FUNCTION_IS_NOT_ALLOWED'; +exports[1565] = 'ER_DDL_LOG_ERROR'; +exports[1566] = 'ER_NULL_IN_VALUES_LESS_THAN'; +exports[1567] = 'ER_WRONG_PARTITION_NAME'; +exports[1568] = 'ER_CANT_CHANGE_TX_ISOLATION'; +exports[1569] = 'ER_DUP_ENTRY_AUTOINCREMENT_CASE'; +exports[1570] = 'ER_EVENT_MODIFY_QUEUE_ERROR'; +exports[1571] = 'ER_EVENT_SET_VAR_ERROR'; +exports[1572] = 'ER_PARTITION_MERGE_ERROR'; +exports[1573] = 'ER_CANT_ACTIVATE_LOG'; +exports[1574] = 'ER_RBR_NOT_AVAILABLE'; +exports[1575] = 'ER_BASE'; +exports[1576] = 'ER_EVENT_RECURSION_FORBIDDEN'; +exports[1577] = 'ER_EVENTS_DB_ERROR'; +exports[1578] = 'ER_ONLY_INTEGERS_ALLOWED'; +exports[1579] = 'ER_UNSUPORTED_LOG_ENGINE'; +exports[1580] = 'ER_BAD_LOG_STATEMENT'; +exports[1581] = 'ER_CANT_RENAME_LOG_TABLE'; +exports[1582] = 'ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT'; +exports[1583] = 'ER_WRONG_PARAMETERS_TO_NATIVE_FCT'; +exports[1584] = 'ER_WRONG_PARAMETERS_TO_STORED_FCT'; +exports[1585] = 'ER_NATIVE_FCT_NAME_COLLISION'; +exports[1586] = 'ER_DUP_ENTRY_WITH_KEY_NAME'; +exports[1587] = 'ER_BINLOG_PURGE_EMFILE'; +exports[1588] = 'ER_EVENT_CANNOT_CREATE_IN_THE_PAST'; +exports[1589] = 'ER_EVENT_CANNOT_ALTER_IN_THE_PAST'; +exports[1590] = 'ER_SLAVE_INCIDENT'; +exports[1591] = 'ER_NO_PARTITION_FOR_GIVEN_VALUE_SILENT'; +exports[1592] = 'ER_BINLOG_UNSAFE_STATEMENT'; +exports[1593] = 'ER_SLAVE_FATAL_ERROR'; +exports[1594] = 'ER_SLAVE_RELAY_LOG_READ_FAILURE'; +exports[1595] = 'ER_SLAVE_RELAY_LOG_WRITE_FAILURE'; +exports[1596] = 'ER_SLAVE_CREATE_EVENT_FAILURE'; +exports[1597] = 'ER_SLAVE_MASTER_COM_FAILURE'; +exports[1598] = 'ER_BINLOG_LOGGING_IMPOSSIBLE'; +exports[1599] = 'ER_VIEW_NO_CREATION_CTX'; +exports[1600] = 'ER_VIEW_INVALID_CREATION_CTX'; +exports[1601] = 'ER_SR_INVALID_CREATION_CTX'; +exports[1602] = 'ER_TRG_CORRUPTED_FILE'; +exports[1603] = 'ER_TRG_NO_CREATION_CTX'; +exports[1604] = 'ER_TRG_INVALID_CREATION_CTX'; +exports[1605] = 'ER_EVENT_INVALID_CREATION_CTX'; +exports[1606] = 'ER_TRG_CANT_OPEN_TABLE'; +exports[1607] = 'ER_CANT_CREATE_SROUTINE'; +exports[1608] = 'ER_NEVER_USED'; +exports[1609] = 'ER_NO_FORMAT_DESCRIPTION_EVENT_BEFORE_BINLOG_STATEMENT'; +exports[1610] = 'ER_SLAVE_CORRUPT_EVENT'; +exports[1611] = 'ER_LOAD_DATA_INVALID_COLUMN'; +exports[1612] = 'ER_LOG_PURGE_NO_FILE'; +exports[1613] = 'ER_XA_RBTIMEOUT'; +exports[1614] = 'ER_XA_RBDEADLOCK'; +exports[1615] = 'ER_NEED_REPREPARE'; +exports[1616] = 'ER_DELAYED_NOT_SUPPORTED'; +exports[1617] = 'WARN_NO_MASTER_INFO'; +exports[1618] = 'WARN_OPTION_IGNORED'; +exports[1619] = 'WARN_PLUGIN_DELETE_BUILTIN'; +exports[1620] = 'WARN_PLUGIN_BUSY'; +exports[1621] = 'ER_VARIABLE_IS_READONLY'; +exports[1622] = 'ER_WARN_ENGINE_TRANSACTION_ROLLBACK'; +exports[1623] = 'ER_SLAVE_HEARTBEAT_FAILURE'; +exports[1624] = 'ER_SLAVE_HEARTBEAT_VALUE_OUT_OF_RANGE'; +exports[1625] = 'ER_NDB_REPLICATION_SCHEMA_ERROR'; +exports[1626] = 'ER_CONFLICT_FN_PARSE_ERROR'; +exports[1627] = 'ER_EXCEPTIONS_WRITE_ERROR'; +exports[1628] = 'ER_TOO_LONG_TABLE_COMMENT'; +exports[1629] = 'ER_TOO_LONG_FIELD_COMMENT'; +exports[1630] = 'ER_FUNC_INEXISTENT_NAME_COLLISION'; +exports[1631] = 'ER_DATABASE_NAME'; +exports[1632] = 'ER_TABLE_NAME'; +exports[1633] = 'ER_PARTITION_NAME'; +exports[1634] = 'ER_SUBPARTITION_NAME'; +exports[1635] = 'ER_TEMPORARY_NAME'; +exports[1636] = 'ER_RENAMED_NAME'; +exports[1637] = 'ER_TOO_MANY_CONCURRENT_TRXS'; +exports[1638] = 'WARN_NON_ASCII_SEPARATOR_NOT_IMPLEMENTED'; +exports[1639] = 'ER_DEBUG_SYNC_TIMEOUT'; +exports[1640] = 'ER_DEBUG_SYNC_HIT_LIMIT'; +exports[1641] = 'ER_DUP_SIGNAL_SET'; +exports[1642] = 'ER_SIGNAL_WARN'; +exports[1643] = 'ER_SIGNAL_NOT_FOUND'; +exports[1644] = 'ER_SIGNAL_EXCEPTION'; +exports[1645] = 'ER_RESIGNAL_WITHOUT_ACTIVE_HANDLER'; +exports[1646] = 'ER_SIGNAL_BAD_CONDITION_TYPE'; +exports[1647] = 'WARN_COND_ITEM_TRUNCATED'; +exports[1648] = 'ER_COND_ITEM_TOO_LONG'; +exports[1649] = 'ER_UNKNOWN_LOCALE'; +exports[1650] = 'ER_SLAVE_IGNORE_SERVER_IDS'; +exports[1651] = 'ER_QUERY_CACHE_DISABLED'; +exports[1652] = 'ER_SAME_NAME_PARTITION_FIELD'; +exports[1653] = 'ER_PARTITION_COLUMN_LIST_ERROR'; +exports[1654] = 'ER_WRONG_TYPE_COLUMN_VALUE_ERROR'; +exports[1655] = 'ER_TOO_MANY_PARTITION_FUNC_FIELDS_ERROR'; +exports[1656] = 'ER_MAXVALUE_IN_VALUES_IN'; +exports[1657] = 'ER_TOO_MANY_VALUES_ERROR'; +exports[1658] = 'ER_ROW_SINGLE_PARTITION_FIELD_ERROR'; +exports[1659] = 'ER_FIELD_TYPE_NOT_ALLOWED_AS_PARTITION_FIELD'; +exports[1660] = 'ER_PARTITION_FIELDS_TOO_LONG'; +exports[1661] = 'ER_BINLOG_ROW_ENGINE_AND_STMT_ENGINE'; +exports[1662] = 'ER_BINLOG_ROW_MODE_AND_STMT_ENGINE'; +exports[1663] = 'ER_BINLOG_UNSAFE_AND_STMT_ENGINE'; +exports[1664] = 'ER_BINLOG_ROW_INJECTION_AND_STMT_ENGINE'; +exports[1665] = 'ER_BINLOG_STMT_MODE_AND_ROW_ENGINE'; +exports[1666] = 'ER_BINLOG_ROW_INJECTION_AND_STMT_MODE'; +exports[1667] = 'ER_BINLOG_MULTIPLE_ENGINES_AND_SELF_LOGGING_ENGINE'; +exports[1668] = 'ER_BINLOG_UNSAFE_LIMIT'; +exports[1669] = 'ER_BINLOG_UNSAFE_INSERT_DELAYED'; +exports[1670] = 'ER_BINLOG_UNSAFE_SYSTEM_TABLE'; +exports[1671] = 'ER_BINLOG_UNSAFE_AUTOINC_COLUMNS'; +exports[1672] = 'ER_BINLOG_UNSAFE_UDF'; +exports[1673] = 'ER_BINLOG_UNSAFE_SYSTEM_VARIABLE'; +exports[1674] = 'ER_BINLOG_UNSAFE_SYSTEM_FUNCTION'; +exports[1675] = 'ER_BINLOG_UNSAFE_NONTRANS_AFTER_TRANS'; +exports[1676] = 'ER_MESSAGE_AND_STATEMENT'; +exports[1677] = 'ER_SLAVE_CONVERSION_FAILED'; +exports[1678] = 'ER_SLAVE_CANT_CREATE_CONVERSION'; +exports[1679] = 'ER_INSIDE_TRANSACTION_PREVENTS_SWITCH_BINLOG_FORMAT'; +exports[1680] = 'ER_PATH_LENGTH'; +exports[1681] = 'ER_WARN_DEPRECATED_SYNTAX_NO_REPLACEMENT'; +exports[1682] = 'ER_WRONG_NATIVE_TABLE_STRUCTURE'; +exports[1683] = 'ER_WRONG_PERFSCHEMA_USAGE'; +exports[1684] = 'ER_WARN_I_S_SKIPPED_TABLE'; +exports[1685] = 'ER_INSIDE_TRANSACTION_PREVENTS_SWITCH_BINLOG_DIRECT'; +exports[1686] = 'ER_STORED_FUNCTION_PREVENTS_SWITCH_BINLOG_DIRECT'; +exports[1687] = 'ER_SPATIAL_MUST_HAVE_GEOM_COL'; +exports[1688] = 'ER_TOO_LONG_INDEX_COMMENT'; +exports[1689] = 'ER_LOCK_ABORTED'; +exports[1690] = 'ER_DATA_OUT_OF_RANGE'; +exports[1691] = 'ER_WRONG_SPVAR_TYPE_IN_LIMIT'; +exports[1692] = 'ER_BINLOG_UNSAFE_MULTIPLE_ENGINES_AND_SELF_LOGGING_ENGINE'; +exports[1693] = 'ER_BINLOG_UNSAFE_MIXED_STATEMENT'; +exports[1694] = 'ER_INSIDE_TRANSACTION_PREVENTS_SWITCH_SQL_LOG_BIN'; +exports[1695] = 'ER_STORED_FUNCTION_PREVENTS_SWITCH_SQL_LOG_BIN'; +exports[1696] = 'ER_FAILED_READ_FROM_PAR_FILE'; +exports[1697] = 'ER_VALUES_IS_NOT_INT_TYPE_ERROR'; +exports[1698] = 'ER_ACCESS_DENIED_NO_PASSWORD_ERROR'; +exports[1699] = 'ER_SET_PASSWORD_AUTH_PLUGIN'; +exports[1700] = 'ER_GRANT_PLUGIN_USER_EXISTS'; +exports[1701] = 'ER_TRUNCATE_ILLEGAL_FK'; +exports[1702] = 'ER_PLUGIN_IS_PERMANENT'; +exports[1703] = 'ER_SLAVE_HEARTBEAT_VALUE_OUT_OF_RANGE_MIN'; +exports[1704] = 'ER_SLAVE_HEARTBEAT_VALUE_OUT_OF_RANGE_MAX'; +exports[1705] = 'ER_STMT_CACHE_FULL'; +exports[1706] = 'ER_MULTI_UPDATE_KEY_CONFLICT'; +exports[1707] = 'ER_TABLE_NEEDS_REBUILD'; +exports[1708] = 'WARN_OPTION_BELOW_LIMIT'; +exports[1709] = 'ER_INDEX_COLUMN_TOO_LONG'; +exports[1710] = 'ER_ERROR_IN_TRIGGER_BODY'; +exports[1711] = 'ER_ERROR_IN_UNKNOWN_TRIGGER_BODY'; +exports[1712] = 'ER_INDEX_CORRUPT'; +exports[1713] = 'ER_UNDO_RECORD_TOO_BIG'; +exports[1714] = 'ER_BINLOG_UNSAFE_INSERT_IGNORE_SELECT'; +exports[1715] = 'ER_BINLOG_UNSAFE_INSERT_SELECT_UPDATE'; +exports[1716] = 'ER_BINLOG_UNSAFE_REPLACE_SELECT'; +exports[1717] = 'ER_BINLOG_UNSAFE_CREATE_IGNORE_SELECT'; +exports[1718] = 'ER_BINLOG_UNSAFE_CREATE_REPLACE_SELECT'; +exports[1719] = 'ER_BINLOG_UNSAFE_UPDATE_IGNORE'; +exports[1720] = 'ER_PLUGIN_NO_UNINSTALL'; +exports[1721] = 'ER_PLUGIN_NO_INSTALL'; +exports[1722] = 'ER_BINLOG_UNSAFE_WRITE_AUTOINC_SELECT'; +exports[1723] = 'ER_BINLOG_UNSAFE_CREATE_SELECT_AUTOINC'; \ No newline at end of file diff --git a/node_modules/mysql/lib/protocol/constants/field_flags.js b/node_modules/mysql/lib/protocol/constants/field_flags.js new file mode 100644 index 0000000..c698da5 --- /dev/null +++ b/node_modules/mysql/lib/protocol/constants/field_flags.js @@ -0,0 +1,18 @@ +// Manually extracted from mysql-5.5.23/include/mysql_com.h +exports.NOT_NULL_FLAG = 1; /* Field can't be NULL */ +exports.PRI_KEY_FLAG = 2; /* Field is part of a primary key */ +exports.UNIQUE_KEY_FLAG = 4; /* Field is part of a unique key */ +exports.MULTIPLE_KEY_FLAG = 8; /* Field is part of a key */ +exports.BLOB_FLAG = 16; /* Field is a blob */ +exports.UNSIGNED_FLAG = 32; /* Field is unsigned */ +exports.ZEROFILL_FLAG = 64; /* Field is zerofill */ +exports.BINARY_FLAG = 128; /* Field is binary */ + +/* The following are only sent to new clients */ +exports.ENUM_FLAG = 256; /* field is an enum */ +exports.AUTO_INCREMENT_FLAG = 512; /* field is a autoincrement field */ +exports.TIMESTAMP_FLAG = 1024; /* Field is a timestamp */ +exports.SET_FLAG = 2048; /* field is a set */ +exports.NO_DEFAULT_VALUE_FLAG = 4096; /* Field doesn't have default value */ +exports.ON_UPDATE_NOW_FLAG = 8192; /* Field is set to NOW on UPDATE */ +exports.NUM_FLAG = 32768; /* Field is num (for clients) */ diff --git a/node_modules/mysql/lib/protocol/constants/server_status.js b/node_modules/mysql/lib/protocol/constants/server_status.js new file mode 100644 index 0000000..5537ee5 --- /dev/null +++ b/node_modules/mysql/lib/protocol/constants/server_status.js @@ -0,0 +1,39 @@ +// Manually extracted from mysql-5.5.23/include/mysql_com.h + +/** + Is raised when a multi-statement transaction + has been started, either explicitly, by means + of BEGIN or COMMIT AND CHAIN, or + implicitly, by the first transactional + statement, when autocommit=off. +*/ +exports.SERVER_STATUS_IN_TRANS = 1; +exports.SERVER_STATUS_AUTOCOMMIT = 2; /* Server in auto_commit mode */ +exports.SERVER_MORE_RESULTS_EXISTS = 8; /* Multi query - next query exists */ +exports.SERVER_QUERY_NO_GOOD_INDEX_USED = 16; +exports.SERVER_QUERY_NO_INDEX_USED = 32; +/** + The server was able to fulfill the clients request and opened a + read-only non-scrollable cursor for a query. This flag comes + in reply to COM_STMT_EXECUTE and COM_STMT_FETCH commands. +*/ +exports.SERVER_STATUS_CURSOR_EXISTS = 64; +/** + This flag is sent when a read-only cursor is exhausted, in reply to + COM_STMT_FETCH command. +*/ +exports.SERVER_STATUS_LAST_ROW_SENT = 128; +exports.SERVER_STATUS_DB_DROPPED = 256; /* A database was dropped */ +exports.SERVER_STATUS_NO_BACKSLASH_ESCAPES = 512; +/** + Sent to the client if after a prepared statement reprepare + we discovered that the new statement returns a different + number of result set columns. +*/ +exports.SERVER_STATUS_METADATA_CHANGED = 1024; +exports.SERVER_QUERY_WAS_SLOW = 2048; + +/** + To mark ResultSet containing output parameter values. +*/ +exports.SERVER_PS_OUT_PARAMS = 4096; diff --git a/node_modules/mysql/lib/protocol/constants/types.js b/node_modules/mysql/lib/protocol/constants/types.js new file mode 100644 index 0000000..21c4000 --- /dev/null +++ b/node_modules/mysql/lib/protocol/constants/types.js @@ -0,0 +1,29 @@ +// Manually extracted from mysql-5.5.23/include/mysql_com.h +// some more info here: http://dev.mysql.com/doc/refman/5.5/en/c-api-prepared-statement-type-codes.html +exports.DECIMAL = 0x00; // aka DECIMAL (http://dev.mysql.com/doc/refman/5.0/en/precision-math-decimal-changes.html) +exports.TINY = 0x01; // aka TINYINT, 1 byte +exports.SHORT = 0x02; // aka SMALLINT, 2 bytes +exports.LONG = 0x03; // aka INT, 4 bytes +exports.FLOAT = 0x04; // aka FLOAT, 4-8 bytes +exports.DOUBLE = 0x05; // aka DOUBLE, 8 bytes +exports.NULL = 0x06; // NULL (used for prepared statements, I think) +exports.TIMESTAMP = 0x07; // aka TIMESTAMP +exports.LONGLONG = 0x08; // aka BIGINT, 8 bytes +exports.INT24 = 0x09; // aka MEDIUMINT, 3 bytes +exports.DATE = 0x0a; // aka DATE +exports.TIME = 0x0b; // aka TIME +exports.DATETIME = 0x0c; // aka DATETIME +exports.YEAR = 0x0d; // aka YEAR, 1 byte (don't ask) +exports.NEWDATE = 0x0e; // aka ? +exports.VARCHAR = 0x0f; // aka VARCHAR (?) +exports.BIT = 0x10; // aka BIT, 1-8 byte +exports.NEWDECIMAL = 0xf6; // aka DECIMAL +exports.ENUM = 0xf7; // aka ENUM +exports.SET = 0xf8; // aka SET +exports.TINY_BLOB = 0xf9; // aka TINYBLOB, TINYTEXT +exports.MEDIUM_BLOB = 0xfa; // aka MEDIUMBLOB, MEDIUMTEXT +exports.LONG_BLOB = 0xfb; // aka LONGBLOG, LONGTEXT +exports.BLOB = 0xfc; // aka BLOB, TEXT +exports.VAR_STRING = 0xfd; // aka VARCHAR, VARBINARY +exports.STRING = 0xfe; // aka CHAR, BINARY +exports.GEOMETRY = 0xff; // aka GEOMETRY diff --git a/node_modules/mysql/lib/protocol/packets/ClientAuthenticationPacket.js b/node_modules/mysql/lib/protocol/packets/ClientAuthenticationPacket.js new file mode 100644 index 0000000..19e1539 --- /dev/null +++ b/node_modules/mysql/lib/protocol/packets/ClientAuthenticationPacket.js @@ -0,0 +1,32 @@ +module.exports = ClientAuthenticationPacket; +function ClientAuthenticationPacket(options) { + options = options || {}; + + this.clientFlags = options.clientFlags; + this.maxPacketSize = options.maxPacketSize; + this.charsetNumber = options.charsetNumber; + this.filler = undefined; + this.user = options.user; + this.scrambleBuff = options.scrambleBuff; + this.database = options.database; +} + +ClientAuthenticationPacket.prototype.parse = function(parser) { + this.clientFlags = parser.parseUnsignedNumber(4); + this.maxPacketSize = parser.parseUnsignedNumber(4); + this.charsetNumber = parser.parseUnsignedNumber(1); + this.filler = parser.parseFiller(23); + this.user = parser.parseNullTerminatedString(); + this.scrambleBuff = parser.parseLengthCodedBuffer(); + this.database = parser.parseNullTerminatedString(); +}; + +ClientAuthenticationPacket.prototype.write = function(writer) { + writer.writeUnsignedNumber(4, this.clientFlags); + writer.writeUnsignedNumber(4, this.maxPacketSize); + writer.writeUnsignedNumber(1, this.charsetNumber); + writer.writeFiller(23); + writer.writeNullTerminatedString(this.user); + writer.writeLengthCodedBuffer(this.scrambleBuff); + writer.writeNullTerminatedString(this.database); +}; diff --git a/node_modules/mysql/lib/protocol/packets/ComChangeUserPacket.js b/node_modules/mysql/lib/protocol/packets/ComChangeUserPacket.js new file mode 100644 index 0000000..d8279b0 --- /dev/null +++ b/node_modules/mysql/lib/protocol/packets/ComChangeUserPacket.js @@ -0,0 +1,25 @@ +module.exports = ComChangeUserPacket; +function ComChangeUserPacket(options) { + options = options || {}; + + this.command = 0x11; + this.user = options.user; + this.scrambleBuff = options.scrambleBuff; + this.database = options.database; + this.charsetNumber = options.charsetNumber; +} + +ComChangeUserPacket.prototype.parse = function(parser) { + this.user = parser.parseNullTerminatedString(); + this.scrambleBuff = parser.parseLengthCodedBuffer(); + this.database = parser.parseNullTerminatedString(); + this.charsetNumber = parser.parseUnsignedNumber(1); +}; + +ComChangeUserPacket.prototype.write = function(writer) { + writer.writeUnsignedNumber(1, this.command); + writer.writeNullTerminatedString(this.user); + writer.writeLengthCodedBuffer(this.scrambleBuff); + writer.writeNullTerminatedString(this.database); + writer.writeUnsignedNumber(1, this.charsetNumber); +}; diff --git a/node_modules/mysql/lib/protocol/packets/ComPingPacket.js b/node_modules/mysql/lib/protocol/packets/ComPingPacket.js new file mode 100644 index 0000000..359eef5 --- /dev/null +++ b/node_modules/mysql/lib/protocol/packets/ComPingPacket.js @@ -0,0 +1,12 @@ +module.exports = ComPingPacket; +function ComPingPacket(sql) { + this.command = 0x0e; +} + +ComPingPacket.prototype.write = function(writer) { + writer.writeUnsignedNumber(1, this.command); +}; + +ComPingPacket.prototype.parse = function(parser) { + this.command = parser.parseUnsignedNumber(1); +}; diff --git a/node_modules/mysql/lib/protocol/packets/ComQueryPacket.js b/node_modules/mysql/lib/protocol/packets/ComQueryPacket.js new file mode 100644 index 0000000..7ac191f --- /dev/null +++ b/node_modules/mysql/lib/protocol/packets/ComQueryPacket.js @@ -0,0 +1,15 @@ +module.exports = ComQueryPacket; +function ComQueryPacket(sql) { + this.command = 0x03; + this.sql = sql; +} + +ComQueryPacket.prototype.write = function(writer) { + writer.writeUnsignedNumber(1, this.command); + writer.writeString(this.sql); +}; + +ComQueryPacket.prototype.parse = function(parser) { + this.command = parser.parseUnsignedNumber(1); + this.sql = parser.parsePacketTerminatedString(); +}; diff --git a/node_modules/mysql/lib/protocol/packets/ComQuitPacket.js b/node_modules/mysql/lib/protocol/packets/ComQuitPacket.js new file mode 100644 index 0000000..d4e2805 --- /dev/null +++ b/node_modules/mysql/lib/protocol/packets/ComQuitPacket.js @@ -0,0 +1,7 @@ +module.exports = ComQuitPacket; +function ComQuitPacket(sql) { +} + +ComQuitPacket.prototype.write = function(writer) { + writer.writeUnsignedNumber(1, 0x01); +}; diff --git a/node_modules/mysql/lib/protocol/packets/ComStatisticsPacket.js b/node_modules/mysql/lib/protocol/packets/ComStatisticsPacket.js new file mode 100644 index 0000000..fe11d18 --- /dev/null +++ b/node_modules/mysql/lib/protocol/packets/ComStatisticsPacket.js @@ -0,0 +1,12 @@ +module.exports = ComStatisticsPacket; +function ComStatisticsPacket(sql) { + this.command = 0x09; +} + +ComStatisticsPacket.prototype.write = function(writer) { + writer.writeUnsignedNumber(1, this.command); +}; + +ComStatisticsPacket.prototype.parse = function(parser) { + this.command = parser.parseUnsignedNumber(1); +}; diff --git a/node_modules/mysql/lib/protocol/packets/EmptyPacket.js b/node_modules/mysql/lib/protocol/packets/EmptyPacket.js new file mode 100644 index 0000000..f02189c --- /dev/null +++ b/node_modules/mysql/lib/protocol/packets/EmptyPacket.js @@ -0,0 +1,6 @@ +module.exports = EmptyPacket; +function EmptyPacket() { +} + +EmptyPacket.prototype.write = function(writer) { +}; diff --git a/node_modules/mysql/lib/protocol/packets/EofPacket.js b/node_modules/mysql/lib/protocol/packets/EofPacket.js new file mode 100644 index 0000000..116f861 --- /dev/null +++ b/node_modules/mysql/lib/protocol/packets/EofPacket.js @@ -0,0 +1,20 @@ +module.exports = EofPacket; +function EofPacket(options) { + options = options || {}; + + this.fieldCount = undefined; + this.warningCount = options.warningCount; + this.serverStatus = options.serverStatus; +} + +EofPacket.prototype.parse = function(parser) { + this.fieldCount = parser.parseUnsignedNumber(1); + this.warningCount = parser.parseUnsignedNumber(2); + this.serverStatus = parser.parseUnsignedNumber(2); +}; + +EofPacket.prototype.write = function(writer) { + writer.writeUnsignedNumber(1, 0xfe); + writer.writeUnsignedNumber(2, this.warningCount); + writer.writeUnsignedNumber(2, this.serverStatus); +}; diff --git a/node_modules/mysql/lib/protocol/packets/ErrorPacket.js b/node_modules/mysql/lib/protocol/packets/ErrorPacket.js new file mode 100644 index 0000000..e03de00 --- /dev/null +++ b/node_modules/mysql/lib/protocol/packets/ErrorPacket.js @@ -0,0 +1,35 @@ +module.exports = ErrorPacket; +function ErrorPacket(options) { + options = options || {}; + + this.fieldCount = options.fieldCount; + this.errno = options.errno; + this.sqlStateMarker = options.sqlStateMarker; + this.sqlState = options.sqlState; + this.message = options.message; +} + +ErrorPacket.prototype.parse = function(parser) { + this.fieldCount = parser.parseUnsignedNumber(1); + this.errno = parser.parseUnsignedNumber(2); + + // sqlStateMarker ('#' = 0x23) indicates error packet format + if (parser.peak() === 0x23) { + this.sqlStateMarker = parser.parseString(1); + this.sqlState = parser.parseString(5); + } + + this.message = parser.parsePacketTerminatedString(); +}; + +ErrorPacket.prototype.write = function(writer) { + writer.writeUnsignedNumber(1, 0xff); + writer.writeUnsignedNumber(2, this.errno); + + if (this.sqlStateMarker) { + writer.writeString(this.sqlStateMarker); + writer.writeString(this.sqlState); + } + + writer.writeString(this.message); +}; diff --git a/node_modules/mysql/lib/protocol/packets/Field.js b/node_modules/mysql/lib/protocol/packets/Field.js new file mode 100644 index 0000000..9b48c14 --- /dev/null +++ b/node_modules/mysql/lib/protocol/packets/Field.js @@ -0,0 +1,31 @@ +var Types = require('../constants/types'); + +module.exports = Field; +function Field(options) { + options = options || {}; + + this.parser = options.parser; + this.db = options.packet.db; + this.table = options.packet.table; + this.name = options.packet.name; + this.type = typeToString(options.packet.type); + this.length = options.packet.length; +} + +Field.prototype.string = function () { + return this.parser.parseLengthCodedString(); +}; + +Field.prototype.buffer = function () { + return this.parser.parseLengthCodedBuffer(); +}; + +Field.prototype.geometry = function () { + return this.parser.parseGeometryValue(); +}; + +function typeToString(t) { + for (var k in Types) { + if (Types[k] == t) return k; + } +} diff --git a/node_modules/mysql/lib/protocol/packets/FieldPacket.js b/node_modules/mysql/lib/protocol/packets/FieldPacket.js new file mode 100644 index 0000000..b7a8819 --- /dev/null +++ b/node_modules/mysql/lib/protocol/packets/FieldPacket.js @@ -0,0 +1,65 @@ +module.exports = FieldPacket; +function FieldPacket(options) { + options = options || {}; + + this.catalog = options.catalog; + this.db = options.db; + this.table = options.table; + this.orgTable = options.orgTable; + this.name = options.name; + this.orgName = options.orgName; + this.filler1 = undefined; + this.charsetNr = options.charsetNr; + this.length = options.length; + this.type = options.type; + this.flags = options.flags; + this.decimals = options.decimals; + this.filler2 = undefined; + this.default = options.default; + this.zeroFill = options.zeroFill; +} + +FieldPacket.prototype.parse = function(parser) { + this.catalog = parser.parseLengthCodedString(); + this.db = parser.parseLengthCodedString(); + this.table = parser.parseLengthCodedString(); + this.orgTable = parser.parseLengthCodedString(); + this.name = parser.parseLengthCodedString(); + this.orgName = parser.parseLengthCodedString(); + this.filler1 = parser.parseFiller(1); + this.charsetNr = parser.parseUnsignedNumber(2); + this.fieldLength = parser.parseUnsignedNumber(4); + this.type = parser.parseUnsignedNumber(1); + this.flags = parser.parseUnsignedNumber(2); + this.decimals = parser.parseUnsignedNumber(1); + this.filler2 = parser.parseFiller(2); + + // parsed flags + this.zeroFill = (this.flags & 0x0040 ? true : false); + + if (parser.reachedPacketEnd()) { + return; + } + + this.default = parser.parseLengthCodedNumber(); +}; + +FieldPacket.prototype.write = function(writer) { + writer.writeLengthCodedString(this.catalog); + writer.writeLengthCodedString(this.db); + writer.writeLengthCodedString(this.table); + writer.writeLengthCodedString(this.orgTable); + writer.writeLengthCodedString(this.name); + writer.writeLengthCodedString(this.orgName); + writer.writeFiller(1); + writer.writeUnsignedNumber(2, this.charsetNr || 0); + writer.writeUnsignedNumber(4, this.fieldLength || 0); + writer.writeUnsignedNumber(1, this.type) || 0; + writer.writeUnsignedNumber(2, this.flags || 0); + writer.writeUnsignedNumber(1, this.decimals || 0); + writer.writeFiller(2); + + if (this.default !== undefined) { + writer.writeLengthCodedString(this.default); + } +}; diff --git a/node_modules/mysql/lib/protocol/packets/HandshakeInitializationPacket.js b/node_modules/mysql/lib/protocol/packets/HandshakeInitializationPacket.js new file mode 100644 index 0000000..a5b9def --- /dev/null +++ b/node_modules/mysql/lib/protocol/packets/HandshakeInitializationPacket.js @@ -0,0 +1,81 @@ +module.exports = HandshakeInitializationPacket; +function HandshakeInitializationPacket(options) { + options = options || {}; + + this.protocolVersion = options.protocolVersion; + this.serverVersion = options.serverVersion; + this.threadId = options.threadId; + this.scrambleBuff1 = options.scrambleBuff1; + this.filler1 = options.filler1; + this.serverCapabilities1 = options.serverCapabilities1; + this.serverLanguage = options.serverLanguage; + this.serverStatus = options.serverStatus; + this.serverCapabilities2 = options.serverCapabilities2; + this.scrambleLength = options.scrambleLength; + this.filler2 = options.filler2; + this.scrambleBuff2 = options.scrambleBuff2; + this.filler3 = options.filler3; + this.pluginData = options.pluginData; +} + +HandshakeInitializationPacket.prototype.parse = function(parser) { + this.protocolVersion = parser.parseUnsignedNumber(1); + this.serverVersion = parser.parseNullTerminatedString(); + this.threadId = parser.parseUnsignedNumber(4); + this.scrambleBuff1 = parser.parseBuffer(8); + this.filler1 = parser.parseFiller(1); + this.serverCapabilities1 = parser.parseUnsignedNumber(2); + this.serverLanguage = parser.parseUnsignedNumber(1); + this.serverStatus = parser.parseUnsignedNumber(2); + this.serverCapabilities2 = parser.parseUnsignedNumber(2); + this.scrambleLength = parser.parseUnsignedNumber(1); + this.filler2 = parser.parseFiller(10); + + // scrambleBuff2 should be 0x00 terminated, but sphinx does not do this + // so we assume scrambleBuff2 to be 12 byte and treat the next byte as a + // filler byte. + this.scrambleBuff2 = parser.parseBuffer(12); + this.filler3 = parser.parseFiller(1); + + if (parser.reachedPacketEnd()) { + return; + } + + // According to the docs this should be 0x00 terminated, but MariaDB does + // not do this, so we assume this string to be packet terminated. + this.pluginData = parser.parsePacketTerminatedString(); + + // However, if there is a trailing '\0', strip it + var lastChar = this.pluginData.length - 1; + if (this.pluginData[lastChar] === '\0') { + this.pluginData = this.pluginData.substr(0, lastChar); + } +}; + +HandshakeInitializationPacket.prototype.write = function(writer) { + writer.writeUnsignedNumber(1, this.protocolVersion); + writer.writeNullTerminatedString(this.serverVersion); + writer.writeUnsignedNumber(4, this.threadId); + writer.writeBuffer(this.scrambleBuff1); + writer.writeFiller(1); + writer.writeUnsignedNumber(2, this.serverCapabilities1); + writer.writeUnsignedNumber(1, this.serverLanguage); + writer.writeUnsignedNumber(2, this.serverStatus); + writer.writeUnsignedNumber(2, this.serverCapabilities2); + writer.writeUnsignedNumber(1, this.scrambleLength); + writer.writeFiller(10); + writer.writeNullTerminatedBuffer(this.scrambleBuff2); + + if (this.pluginData !== undefined) { + writer.writeNullTerminatedString(this.pluginData); + } +}; + +HandshakeInitializationPacket.prototype.scrambleBuff = function() { + var buffer = new Buffer(this.scrambleBuff1.length + this.scrambleBuff2.length); + + this.scrambleBuff1.copy(buffer); + this.scrambleBuff2.copy(buffer, this.scrambleBuff1.length); + + return buffer; +}; diff --git a/node_modules/mysql/lib/protocol/packets/LocalDataFilePacket.js b/node_modules/mysql/lib/protocol/packets/LocalDataFilePacket.js new file mode 100644 index 0000000..c38a88a --- /dev/null +++ b/node_modules/mysql/lib/protocol/packets/LocalDataFilePacket.js @@ -0,0 +1,8 @@ +module.exports = LocalDataFilePacket; +function LocalDataFilePacket(data) { + this.data = data; +} + +LocalDataFilePacket.prototype.write = function(writer) { + writer.writeString(this.data); +}; diff --git a/node_modules/mysql/lib/protocol/packets/OkPacket.js b/node_modules/mysql/lib/protocol/packets/OkPacket.js new file mode 100644 index 0000000..fb25e2e --- /dev/null +++ b/node_modules/mysql/lib/protocol/packets/OkPacket.js @@ -0,0 +1,34 @@ +module.exports = OkPacket; +function OkPacket() { + this.fieldCount = undefined; + this.affectedRows = undefined; + this.insertId = undefined; + this.serverStatus = undefined; + this.warningCount = undefined; + this.message = undefined; +} + +OkPacket.prototype.parse = function(parser) { + this.fieldCount = parser.parseUnsignedNumber(1); + this.affectedRows = parser.parseLengthCodedNumber(); + this.insertId = parser.parseLengthCodedNumber(); + this.serverStatus = parser.parseUnsignedNumber(2); + this.warningCount = parser.parseUnsignedNumber(2); + this.message = parser.parsePacketTerminatedString(); + this.changedRows = 0; + + var m = this.message.match(/\schanged:\s*(\d+)/i); + + if (m !== null) { + this.changedRows = parseInt(m[1], 10); + } +}; + +OkPacket.prototype.write = function(writer) { + writer.writeUnsignedNumber(1, 0x00); + writer.writeLengthCodedNumber(this.affectedRows || 0); + writer.writeLengthCodedNumber(this.insertId || 0); + writer.writeUnsignedNumber(2, this.serverStatus || 0); + writer.writeUnsignedNumber(2, this.warningCount || 0); + writer.writeString(this.message); +}; diff --git a/node_modules/mysql/lib/protocol/packets/OldPasswordPacket.js b/node_modules/mysql/lib/protocol/packets/OldPasswordPacket.js new file mode 100644 index 0000000..1ff78ec --- /dev/null +++ b/node_modules/mysql/lib/protocol/packets/OldPasswordPacket.js @@ -0,0 +1,15 @@ +module.exports = OldPasswordPacket; +function OldPasswordPacket(options) { + options = options || {}; + + this.scrambleBuff = options.scrambleBuff; +} + +OldPasswordPacket.prototype.parse = function(parser) { + this.scrambleBuff = parser.parseNullTerminatedBuffer(); +}; + +OldPasswordPacket.prototype.write = function(writer) { + writer.writeBuffer(this.scrambleBuff); + writer.writeFiller(1); +}; diff --git a/node_modules/mysql/lib/protocol/packets/ResultSetHeaderPacket.js b/node_modules/mysql/lib/protocol/packets/ResultSetHeaderPacket.js new file mode 100644 index 0000000..25b8002 --- /dev/null +++ b/node_modules/mysql/lib/protocol/packets/ResultSetHeaderPacket.js @@ -0,0 +1,25 @@ +module.exports = ResultSetHeaderPacket; +function ResultSetHeaderPacket(options) { + options = options || {}; + + this.fieldCount = options.fieldCount; + this.extra = options.extra; +} + +ResultSetHeaderPacket.prototype.parse = function(parser) { + this.fieldCount = parser.parseLengthCodedNumber(); + + if (parser.reachedPacketEnd()) return; + + this.extra = (this.fieldCount === null) + ? parser.parsePacketTerminatedString() + : parser.parseLengthCodedNumber(); +}; + +ResultSetHeaderPacket.prototype.write = function(writer) { + writer.writeLengthCodedNumber(this.fieldCount); + + if (this.extra !== undefined) { + writer.writeLengthCodedNumber(this.extra); + } +}; diff --git a/node_modules/mysql/lib/protocol/packets/RowDataPacket.js b/node_modules/mysql/lib/protocol/packets/RowDataPacket.js new file mode 100644 index 0000000..47d1437 --- /dev/null +++ b/node_modules/mysql/lib/protocol/packets/RowDataPacket.js @@ -0,0 +1,89 @@ +var Types = require('../constants/types'); +var Charsets = require('../constants/charsets'); +var Field = require('./Field'); + +module.exports = RowDataPacket; +function RowDataPacket() { +} + +RowDataPacket.prototype.parse = function(parser, fieldPackets, typeCast, nestTables, connection) { + var self = this; + var next = function () { + return self._typeCast(fieldPacket, parser, connection.config.timezone); + }; + + for (var i = 0; i < fieldPackets.length; i++) { + var fieldPacket = fieldPackets[i]; + var value; + + if (typeof typeCast == "function") { + value = typeCast.apply(connection, [ new Field({ packet: fieldPacket, parser: parser }), next ]); + } else { + value = (typeCast) + ? this._typeCast(fieldPacket, parser, connection.config.timezone) + : ( (fieldPacket.charsetNr === Charsets.BINARY) + ? parser.parseLengthCodedBuffer() + : parser.parseLengthCodedString() ); + } + + if (typeof nestTables == "string" && nestTables.length) { + this[fieldPacket.table + nestTables + fieldPacket.name] = value; + } else if (nestTables) { + this[fieldPacket.table] = this[fieldPacket.table] || {}; + this[fieldPacket.table][fieldPacket.name] = value; + } else { + this[fieldPacket.name] = value; + } + } +}; + +RowDataPacket.prototype._typeCast = function(field, parser, timeZone) { + switch (field.type) { + case Types.TIMESTAMP: + case Types.DATE: + case Types.DATETIME: + case Types.NEWDATE: + var dateString = parser.parseLengthCodedString(); + if (dateString === null) { + return null; + } + + if (timeZone != 'local') { + if (field.type === Types.DATE) { + dateString += ' 00:00:00 ' + timeZone; + } else { + dateString += timeZone; + } + } + + return new Date(dateString); + case Types.TINY: + case Types.SHORT: + case Types.LONG: + case Types.INT24: + case Types.YEAR: + case Types.FLOAT: + case Types.DOUBLE: + case Types.LONGLONG: + case Types.NEWDECIMAL: + var numberString = parser.parseLengthCodedString(); + return (numberString === null || (field.zeroFill && numberString[0] == "0")) + ? numberString + : Number(numberString); + case Types.BIT: + return parser.parseLengthCodedBuffer(); + case Types.STRING: + case Types.VAR_STRING: + case Types.TINY_BLOB: + case Types.MEDIUM_BLOB: + case Types.LONG_BLOB: + case Types.BLOB: + return (field.charsetNr === Charsets.BINARY) + ? parser.parseLengthCodedBuffer() + : parser.parseLengthCodedString(); + case Types.GEOMETRY: + return parser.parseGeometryValue(); + default: + return parser.parseLengthCodedString(); + } +}; diff --git a/node_modules/mysql/lib/protocol/packets/StatisticsPacket.js b/node_modules/mysql/lib/protocol/packets/StatisticsPacket.js new file mode 100644 index 0000000..5f70b3b --- /dev/null +++ b/node_modules/mysql/lib/protocol/packets/StatisticsPacket.js @@ -0,0 +1,20 @@ +module.exports = StatisticsPacket; +function StatisticsPacket() { + this.message = undefined; +} + +StatisticsPacket.prototype.parse = function(parser) { + this.message = parser.parsePacketTerminatedString(); + + var items = this.message.split(/\s\s/); + for (var i = 0; i < items.length; i++) { + var m = items[i].match(/^(.+)\:\s+(.+)$/); + if (m !== null) { + this[m[1].toLowerCase().replace(/\s/g, '_')] = Number(m[2]); + } + } +}; + +StatisticsPacket.prototype.write = function(writer) { + writer.writeString(this.message); +}; diff --git a/node_modules/mysql/lib/protocol/packets/UseOldPasswordPacket.js b/node_modules/mysql/lib/protocol/packets/UseOldPasswordPacket.js new file mode 100644 index 0000000..d73bf44 --- /dev/null +++ b/node_modules/mysql/lib/protocol/packets/UseOldPasswordPacket.js @@ -0,0 +1,14 @@ +module.exports = UseOldPasswordPacket; +function UseOldPasswordPacket(options) { + options = options || {}; + + this.firstByte = options.firstByte || 0xfe; +} + +UseOldPasswordPacket.prototype.parse = function(parser) { + this.firstByte = parser.parseUnsignedNumber(1); +}; + +UseOldPasswordPacket.prototype.write = function(writer) { + writer.writeUnsignedNumber(1, this.firstByte); +}; diff --git a/node_modules/mysql/lib/protocol/packets/index.js b/node_modules/mysql/lib/protocol/packets/index.js new file mode 100644 index 0000000..a13d469 --- /dev/null +++ b/node_modules/mysql/lib/protocol/packets/index.js @@ -0,0 +1,4 @@ +var Elements = module.exports = require('require-all')({ + dirname : __dirname, + filter : /([A-Z].+)\.js$/, +}); diff --git a/node_modules/mysql/lib/protocol/sequences/ChangeUser.js b/node_modules/mysql/lib/protocol/sequences/ChangeUser.js new file mode 100644 index 0000000..6169eda --- /dev/null +++ b/node_modules/mysql/lib/protocol/sequences/ChangeUser.js @@ -0,0 +1,41 @@ +var Sequence = require('./Sequence'); +var Util = require('util'); +var Packets = require('../packets'); +var Auth = require('../Auth'); + +module.exports = ChangeUser; +Util.inherits(ChangeUser, Sequence); +function ChangeUser(options, callback) { + Sequence.call(this, callback); + + this._user = options.user; + this._password = options.password; + this._database = options.database; + this._charsetNumber = options.charsetNumber; + this._currentConfig = options.currentConfig; +} + +ChangeUser.prototype.start = function(handshakeInitializationPacket) { + var scrambleBuff = handshakeInitializationPacket.scrambleBuff(); + scrambleBuff = Auth.token(this._password, scrambleBuff); + + var packet = new Packets.ComChangeUserPacket({ + user : this._user, + scrambleBuff : scrambleBuff, + database : this._database, + charsetNumber : this._charsetNumber, + }); + + this._currentConfig.user = this._user; + this._currentConfig.password = this._password; + this._currentConfig.database = this._database; + this._currentConfig.charsetNumber = this._charsetNumber; + + this.emit('packet', packet); +}; + +ChangeUser.prototype['ErrorPacket'] = function(packet) { + var err = this._packetToError(packet); + err.fatal = true; + this.end(err); +}; diff --git a/node_modules/mysql/lib/protocol/sequences/Handshake.js b/node_modules/mysql/lib/protocol/sequences/Handshake.js new file mode 100644 index 0000000..c63ba5c --- /dev/null +++ b/node_modules/mysql/lib/protocol/sequences/Handshake.js @@ -0,0 +1,65 @@ +var Sequence = require('./Sequence'); +var Util = require('util'); +var Packets = require('../packets'); +var Auth = require('../Auth'); + +module.exports = Handshake; +Util.inherits(Handshake, Sequence); +function Handshake(config, callback) { + Sequence.call(this, callback); + + this._config = config; + this._handshakeInitializationPacket = null; +} + +Handshake.prototype.determinePacket = function(firstByte) { + if (firstByte === 0xff) { + return Packets.ErrorPacket; + } + + if (!this._handshakeInitializationPacket) { + return Packets.HandshakeInitializationPacket; + } + + if (firstByte === 0xfe) { + return Packets.UseOldPasswordPacket; + } +}; + +Handshake.prototype['HandshakeInitializationPacket'] = function(packet) { + this._handshakeInitializationPacket = packet; + + this.emit('packet', new Packets.ClientAuthenticationPacket({ + clientFlags : this._config.clientFlags, + maxPacketSize : this._config.maxPacketSize, + charsetNumber : this._config.charsetNumber, + user : this._config.user, + scrambleBuff : Auth.token(this._config.password, packet.scrambleBuff()), + database : this._config.database, + })); +}; + +Handshake.prototype['UseOldPasswordPacket'] = function(packet) { + if (!this._config.insecureAuth) { + var err = new Error( + 'MySQL server is requesting the old and insecure pre-4.1 auth mechanism.' + + 'Upgrade the user password or use the {insecureAuth: true} option.' + ); + + err.code = 'HANDSHAKE_INSECURE_AUTH'; + err.fatal = true; + + this.end(err); + return; + } + + this.emit('packet', new Packets.OldPasswordPacket({ + scrambleBuff : Auth.scramble323(this._handshakeInitializationPacket.scrambleBuff(), this._config.password), + })); +}; + +Handshake.prototype['ErrorPacket'] = function(packet) { + var err = this._packetToError(packet, true); + err.fatal = true; + this.end(err); +}; diff --git a/node_modules/mysql/lib/protocol/sequences/Ping.js b/node_modules/mysql/lib/protocol/sequences/Ping.js new file mode 100644 index 0000000..0a5aba9 --- /dev/null +++ b/node_modules/mysql/lib/protocol/sequences/Ping.js @@ -0,0 +1,14 @@ +var Sequence = require('./Sequence'); +var Util = require('util'); +var Packets = require('../packets'); + +module.exports = Ping; +Util.inherits(Ping, Sequence); + +function Ping(callback) { + Sequence.call(this, callback); +} + +Ping.prototype.start = function() { + this.emit('packet', new Packets.ComPingPacket); +}; diff --git a/node_modules/mysql/lib/protocol/sequences/Query.js b/node_modules/mysql/lib/protocol/sequences/Query.js new file mode 100644 index 0000000..6ff7d6c --- /dev/null +++ b/node_modules/mysql/lib/protocol/sequences/Query.js @@ -0,0 +1,162 @@ +var Sequence = require('./Sequence'); +var Util = require('util'); +var Packets = require('../packets'); +var ResultSet = require('../ResultSet'); +var ServerStatus = require('../constants/server_status'); +var fs = require('fs'); + +module.exports = Query; +Util.inherits(Query, Sequence); +function Query(options, callback) { + Sequence.call(this, callback); + + this.sql = options.sql; + this.typeCast = (options.typeCast === undefined) + ? true + : options.typeCast; + this.nestTables = options.nestTables || false; + + this._resultSet = null; + this._results = []; + this._fields = []; + this._index = 0; + this._loadError = null; +} + +Query.prototype.start = function() { + this.emit('packet', new Packets.ComQueryPacket(this.sql)); +}; + +Query.prototype.determinePacket = function(firstByte, parser) { + if (firstByte === 0) { + // If we have a resultSet and got one eofPacket + if (this._resultSet && this._resultSet.eofPackets.length === 1) { + // Then this is a RowDataPacket with an empty string in the first column. + // See: https://github.com/felixge/node-mysql/issues/222 + } else { + return; + } + } + + if (firstByte === 255) { + return; + } + + // EofPacket's are 5 bytes in mysql >= 4.1 + // This is the only / best way to differentiate their firstByte from a 9 + // byte length coded binary. + if (firstByte === 0xfe && parser.packetLength() < 9) { + return Packets.EofPacket; + } + + if (!this._resultSet) { + return Packets.ResultSetHeaderPacket; + } + + return (this._resultSet.eofPackets.length === 0) + ? Packets.FieldPacket + : Packets.RowDataPacket; +}; + +Query.prototype['OkPacket'] = function(packet) { + // try...finally for exception safety + try { + if (!this._callback) { + this.emit('result', packet, this._index); + } else { + this._results.push(packet); + this._fields.push(undefined); + } + } finally { + this._index++; + this._handleFinalResultPacket(packet); + } +}; + +Query.prototype['ErrorPacket'] = function(packet) { + var err = this._packetToError(packet); + + var results = (this._results.length > 0) + ? this._results + : undefined; + + var fields = (this._fields.length > 0) + ? this._fields + : undefined; + + err.index = this._index; + this.end(err, results, fields); +}; + +Query.prototype['ResultSetHeaderPacket'] = function(packet) { + this._resultSet = new ResultSet(packet); + + // used by LOAD DATA LOCAL INFILE queries + if (packet.fieldCount === null) { + this._sendLocalDataFile(packet.extra); + } +}; + +Query.prototype['FieldPacket'] = function(packet) { + this._resultSet.fieldPackets.push(packet); +}; + +Query.prototype['EofPacket'] = function(packet) { + this._resultSet.eofPackets.push(packet); + + if (this._resultSet.eofPackets.length === 1 && !this._callback) { + this.emit('fields', this._resultSet.fieldPackets, this._index); + } + + if (this._resultSet.eofPackets.length !== 2) { + return; + } + + if (this._callback) { + this._results.push(this._resultSet.rows); + this._fields.push(this._resultSet.fieldPackets); + } + + this._index++; + this._resultSet = null; + this._handleFinalResultPacket(packet); +}; + +Query.prototype._handleFinalResultPacket = function(packet) { + if (packet.serverStatus & ServerStatus.SERVER_MORE_RESULTS_EXISTS) { + return; + } + + var results = (this._results.length > 1) + ? this._results + : this._results[0]; + + var fields = (this._fields.length > 1) + ? this._fields + : this._fields[0]; + + this.end(this._loadError, results, fields); +}; + +Query.prototype['RowDataPacket'] = function(packet, parser, connection) { + packet.parse(parser, this._resultSet.fieldPackets, this.typeCast, this.nestTables, connection); + + if (this._callback) { + this._resultSet.rows.push(packet); + } else { + this.emit('result', packet, this._index); + } +}; + +Query.prototype._sendLocalDataFile = function(path) { + var self = this; + fs.readFile(path, 'utf-8', function(err, data) { + if (err) { + self._loadError = err; + } else { + self.emit('packet', new Packets.LocalDataFilePacket(data)); + } + + self.emit('packet', new Packets.EmptyPacket()); + }); +}; diff --git a/node_modules/mysql/lib/protocol/sequences/Quit.js b/node_modules/mysql/lib/protocol/sequences/Quit.js new file mode 100644 index 0000000..d75ea51 --- /dev/null +++ b/node_modules/mysql/lib/protocol/sequences/Quit.js @@ -0,0 +1,13 @@ +var Sequence = require('./Sequence'); +var Util = require('util'); +var Packets = require('../packets'); + +module.exports = Quit; +Util.inherits(Quit, Sequence); +function Quit(callback) { + Sequence.call(this, callback); +} + +Quit.prototype.start = function() { + this.emit('packet', new Packets.ComQuitPacket); +}; diff --git a/node_modules/mysql/lib/protocol/sequences/Sequence.js b/node_modules/mysql/lib/protocol/sequences/Sequence.js new file mode 100644 index 0000000..d635799 --- /dev/null +++ b/node_modules/mysql/lib/protocol/sequences/Sequence.js @@ -0,0 +1,83 @@ +var Util = require('util'); +var EventEmitter = require('events').EventEmitter; +var Packets = require('../packets'); +var ErrorConstants = require('../constants/errors'); + +module.exports = Sequence; +Util.inherits(Sequence, EventEmitter); +function Sequence(callback) { + EventEmitter.call(this); + + this._callback = callback; + this._ended = false; + + // Experimental: Long stack trace support + this._callSite = (new Error).stack.replace(/.+\n/, ''); +} + +Sequence.determinePacket = function(byte) { + switch (byte) { + case 0x00: return Packets.OkPacket; + case 0xfe: return Packets.EofPacket; + case 0xff: return Packets.ErrorPacket; + } +}; + +Sequence.prototype.hasErrorHandler = function() { + return this._callback || this.listeners('error').length > 1; +}; + +Sequence.prototype._packetToError = function(packet) { + var code = ErrorConstants[packet.errno] || 'UNKNOWN_CODE_PLEASE_REPORT'; + var err = new Error(code + ': ' + packet.message); + err.code = code; + + return err; +}; + +Sequence.prototype._addLongStackTrace = function(err) { + var delimiter = '\n --------------------\n' ; + if (err.stack.indexOf(delimiter) > -1) { + return; + } + + err.stack += delimiter + this._callSite; +}; + +Sequence.prototype.end = function(err) { + if (this._ended) { + return; + } + + this._ended = true; + + if (err) { + this._addLongStackTrace(err); + } + + // try...finally for exception safety + try { + if (err) { + this.emit('error', err); + } + } finally { + try { + if (this._callback) { + this._callback.apply(this, arguments); + } + } finally { + this.emit('end'); + } + } +}; + +Sequence.prototype['OkPacket'] = function(packet) { + this.end(null, packet); +}; + +Sequence.prototype['ErrorPacket'] = function(packet) { + this.end(this._packetToError(packet)); +}; + +// Implemented by child classes +Sequence.prototype.start = function() {}; diff --git a/node_modules/mysql/lib/protocol/sequences/Statistics.js b/node_modules/mysql/lib/protocol/sequences/Statistics.js new file mode 100644 index 0000000..2a11cc1 --- /dev/null +++ b/node_modules/mysql/lib/protocol/sequences/Statistics.js @@ -0,0 +1,23 @@ +var Sequence = require('./Sequence'); +var Util = require('util'); +var Packets = require('../packets'); + +module.exports = Statistics; +Util.inherits(Statistics, Sequence); +function Statistics(callback) { + Sequence.call(this, callback); +} + +Statistics.prototype.start = function() { + this.emit('packet', new Packets.ComStatisticsPacket); +}; + +Statistics.prototype['StatisticsPacket'] = function (packet) { + this.end(null, packet); +}; + +Statistics.prototype.determinePacket = function(firstByte, parser) { + if (firstByte === 0x55) { + return Packets.StatisticsPacket; + } +}; diff --git a/node_modules/mysql/lib/protocol/sequences/index.js b/node_modules/mysql/lib/protocol/sequences/index.js new file mode 100644 index 0000000..a13d469 --- /dev/null +++ b/node_modules/mysql/lib/protocol/sequences/index.js @@ -0,0 +1,4 @@ +var Elements = module.exports = require('require-all')({ + dirname : __dirname, + filter : /([A-Z].+)\.js$/, +}); diff --git a/node_modules/mysql/node_modules/require-all/.Readme.md.un~ b/node_modules/mysql/node_modules/require-all/.Readme.md.un~ new file mode 100644 index 0000000..2b299d8 Binary files /dev/null and b/node_modules/mysql/node_modules/require-all/.Readme.md.un~ differ diff --git a/node_modules/mysql/node_modules/require-all/.index.js.un~ b/node_modules/mysql/node_modules/require-all/.index.js.un~ new file mode 100644 index 0000000..8d1fbd9 Binary files /dev/null and b/node_modules/mysql/node_modules/require-all/.index.js.un~ differ diff --git a/node_modules/mysql/node_modules/require-all/Readme.md b/node_modules/mysql/node_modules/require-all/Readme.md new file mode 100644 index 0000000..60a7a5c --- /dev/null +++ b/node_modules/mysql/node_modules/require-all/Readme.md @@ -0,0 +1,16 @@ +# require-all + +An easy way to require all files within a directory. + +## Usage + +```js +var controllers = require('require-all')({ + dirname: __dirname + '/controllers', + filter: /(.+Controller)\.js$/, +}); + +// controllers now is an object with references to all modules matching the filter +// for example: +// { HomeController: function HomeController() {...}, ...} +``` diff --git a/node_modules/mysql/node_modules/require-all/index.js b/node_modules/mysql/node_modules/require-all/index.js new file mode 100644 index 0000000..71084ba --- /dev/null +++ b/node_modules/mysql/node_modules/require-all/index.js @@ -0,0 +1,25 @@ +var fs = require('fs'); + +module.exports = function requireAll(options) { + var files = fs.readdirSync(options.dirname); + var modules = {}; + + files.forEach(function(file) { + var filepath = options.dirname + '/' + file; + if (fs.statSync(filepath).isDirectory()) { + modules[file] = requireAll({ + dirname: filepath, + filter: options.filter + }); + + } else { + var match = file.match(options.filter); + if (!match) return; + + modules[match[1]] = require(filepath); + } + }); + + return modules; +}; + diff --git a/node_modules/mysql/node_modules/require-all/package.json b/node_modules/mysql/node_modules/require-all/package.json new file mode 100644 index 0000000..7018d6b --- /dev/null +++ b/node_modules/mysql/node_modules/require-all/package.json @@ -0,0 +1,23 @@ +{ + "author": { + "name": "Felix Geisendörfer", + "email": "felix@debuggable.com", + "url": "http://debuggable.com/" + }, + "name": "require-all", + "description": "An easy way to require all files within a directory.", + "version": "0.0.3", + "repository": { + "url": "" + }, + "main": "./index", + "engines": { + "node": "*" + }, + "dependencies": {}, + "devDependencies": {}, + "readme": "# require-all\n\nAn easy way to require all files within a directory.\n\n## Usage\n\n```js\nvar controllers = require('require-all')({\n dirname: __dirname + '/controllers',\n filter: /(.+Controller)\\.js$/,\n});\n\n// controllers now is an object with references to all modules matching the filter\n// for example:\n// { HomeController: function HomeController() {...}, ...}\n```\n", + "readmeFilename": "Readme.md", + "_id": "require-all@0.0.3", + "_from": "require-all@0.0.3" +} diff --git a/node_modules/mysql/node_modules/require-all/test/.test.js.un~ b/node_modules/mysql/node_modules/require-all/test/.test.js.un~ new file mode 100644 index 0000000..51d49cd Binary files /dev/null and b/node_modules/mysql/node_modules/require-all/test/.test.js.un~ differ diff --git a/node_modules/mysql/node_modules/require-all/test/controllers/main-Controller.js b/node_modules/mysql/node_modules/require-all/test/controllers/main-Controller.js new file mode 100644 index 0000000..355ce40 --- /dev/null +++ b/node_modules/mysql/node_modules/require-all/test/controllers/main-Controller.js @@ -0,0 +1,4 @@ +exports.index = 1; +exports.show = 2; +exports.add = 3; +exports.edit = 4; diff --git a/node_modules/mysql/node_modules/require-all/test/controllers/notthis.js b/node_modules/mysql/node_modules/require-all/test/controllers/notthis.js new file mode 100644 index 0000000..8ac88c5 --- /dev/null +++ b/node_modules/mysql/node_modules/require-all/test/controllers/notthis.js @@ -0,0 +1 @@ +exports.yes = 'no'; diff --git a/node_modules/mysql/node_modules/require-all/test/controllers/other-Controller.js b/node_modules/mysql/node_modules/require-all/test/controllers/other-Controller.js new file mode 100644 index 0000000..aec8f24 --- /dev/null +++ b/node_modules/mysql/node_modules/require-all/test/controllers/other-Controller.js @@ -0,0 +1,2 @@ +exports.index = 1; +exports.show = 'nothing' diff --git a/node_modules/mysql/node_modules/require-all/test/mydir/foo.js b/node_modules/mysql/node_modules/require-all/test/mydir/foo.js new file mode 100644 index 0000000..cb1c2c0 --- /dev/null +++ b/node_modules/mysql/node_modules/require-all/test/mydir/foo.js @@ -0,0 +1 @@ +module.exports = 'bar'; diff --git a/node_modules/mysql/node_modules/require-all/test/mydir/hello.js b/node_modules/mysql/node_modules/require-all/test/mydir/hello.js new file mode 100644 index 0000000..b1d17c1 --- /dev/null +++ b/node_modules/mysql/node_modules/require-all/test/mydir/hello.js @@ -0,0 +1,2 @@ +exports.world = true; +exports.universe = 42; diff --git a/node_modules/mysql/node_modules/require-all/test/mydir/sub/.config.json.un~ b/node_modules/mysql/node_modules/require-all/test/mydir/sub/.config.json.un~ new file mode 100644 index 0000000..0029e64 Binary files /dev/null and b/node_modules/mysql/node_modules/require-all/test/mydir/sub/.config.json.un~ differ diff --git a/node_modules/mysql/node_modules/require-all/test/mydir/sub/config.json b/node_modules/mysql/node_modules/require-all/test/mydir/sub/config.json new file mode 100644 index 0000000..d2b5939 --- /dev/null +++ b/node_modules/mysql/node_modules/require-all/test/mydir/sub/config.json @@ -0,0 +1,4 @@ +{ + "settingA": "A", + "settingB": "B" +} diff --git a/node_modules/mysql/node_modules/require-all/test/mydir/sub/no.2js b/node_modules/mysql/node_modules/require-all/test/mydir/sub/no.2js new file mode 100644 index 0000000..ec01c2c --- /dev/null +++ b/node_modules/mysql/node_modules/require-all/test/mydir/sub/no.2js @@ -0,0 +1 @@ +module.exports = true; diff --git a/node_modules/mysql/node_modules/require-all/test/mydir/sub/yes.js b/node_modules/mysql/node_modules/require-all/test/mydir/sub/yes.js new file mode 100644 index 0000000..ec01c2c --- /dev/null +++ b/node_modules/mysql/node_modules/require-all/test/mydir/sub/yes.js @@ -0,0 +1 @@ +module.exports = true; diff --git a/node_modules/mysql/node_modules/require-all/test/test.js b/node_modules/mysql/node_modules/require-all/test/test.js new file mode 100644 index 0000000..025af0d --- /dev/null +++ b/node_modules/mysql/node_modules/require-all/test/test.js @@ -0,0 +1,38 @@ +var assert = require('assert'); +var requireAll = require('..'); + +var controllers = requireAll({ + dirname: __dirname + '/controllers', + filter: /(.+Controller)\.js$/ +}); + +assert.deepEqual(controllers, { + 'main-Controller': { + index: 1, + show: 2, + add: 3, + edit: 4 + }, + + 'other-Controller': { + index: 1, + show: 'nothing' + } +}); + + +if (process.version > 'v0.6.0') { + var mydir = requireAll({ + dirname: __dirname + '/mydir', + filter: /(.+)\.(js|json)$/ + }); + + assert.deepEqual(mydir, { + foo: 'bar', + hello: { world: true, universe: 42 }, + sub: { + config: { settingA: 'A', settingB: 'B' }, + yes: true + } + }); +} diff --git a/node_modules/mysql/package.json b/node_modules/mysql/package.json new file mode 100644 index 0000000..8a8c62e --- /dev/null +++ b/node_modules/mysql/package.json @@ -0,0 +1,33 @@ +{ + "author": { + "name": "Felix Geisendörfer", + "email": "felix@debuggable.com", + "url": "http://debuggable.com/" + }, + "name": "mysql", + "description": "A node.js driver for mysql. It is written in JavaScript, does not require compiling, and is 100% MIT licensed.", + "version": "2.0.0-alpha5", + "repository": { + "url": "" + }, + "main": "./index", + "scripts": { + "test": "make test" + }, + "engines": { + "node": "*" + }, + "dependencies": { + "require-all": "0.0.3" + }, + "devDependencies": { + "utest": "0.0.6", + "urun": "0.0.6", + "underscore": "1.3.1" + }, + "optionalDependencies": {}, + "readme": "# node-mysql\n\n[![Build Status](https://secure.travis-ci.org/felixge/node-mysql.png)](http://travis-ci.org/felixge/node-mysql)\n\n## Install\n\n```bash\nnpm install mysql@2.0.0-alpha5\n```\n\nDespite the alpha tag, this is the recommended version for new applications.\nFor information about the previous 0.9.x releases, visit the [v0.9 branch][].\n\nSometimes I may also ask you to install the latest version from Github to check\nif a bugfix is working. In this case, please do:\n\n```\nnpm install git://github.com/felixge/node-mysql.git\n```\n\n[v0.9 branch]: https://github.com/felixge/node-mysql/tree/v0.9\n\n## Introduction\n\nThis is a node.js driver for mysql. It is written in JavaScript, does not\nrequire compiling, and is 100% MIT licensed.\n\nHere is an example on how to use it:\n\n```js\nvar mysql = require('mysql');\nvar connection = mysql.createConnection({\n host : 'localhost',\n user : 'me',\n password : 'secret',\n});\n\nconnection.connect();\n\nconnection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {\n if (err) throw err;\n\n console.log('The solution is: ', rows[0].solution);\n});\n\nconnection.end();\n```\n\nFrom this example, you can learn the following:\n\n* Every method you invoke on a connection is queued and executed in sequence.\n* Closing the connection is done using `end()` which makes sure all remaining\n queries are executed before sending a quit packet to the mysql server.\n\n## Contributors\n\nThanks goes to the people who have contributed code to this module, see the\n[GitHub Contributors page][].\n\n[GitHub Contributors page]: https://github.com/felixge/node-mysql/graphs/contributors\n\nAdditionally I'd like to thank the following people:\n\n* [Andrey Hristov][] (Oracle) - for helping me with protocol questions.\n* [Ulf Wendel][] (Oracle) - for helping me with protocol questions.\n\n[Ulf Wendel]: http://blog.ulf-wendel.de/\n[Andrey Hristov]: http://andrey.hristov.com/\n\n## Sponsors\n\nThe following companies have supported this project financially, allowing me to\nspend more time on it (ordered by time of contribution):\n\n* [Transloadit](http://transloadit.com) (my startup, we do file uploading &\n video encoding as a service, check it out)\n* [Joyent](http://www.joyent.com/)\n* [pinkbike.com](http://pinkbike.com/)\n* [Holiday Extras](http://www.holidayextras.co.uk/) (they are [hiring](http://join.holidayextras.co.uk/vacancy/senior-web-technologist/))\n* [Newscope](http://newscope.com/) (they are [hiring](http://www.newscope.com/stellenangebote))\n\nIf you are interested in sponsoring a day or more of my time, please\n[get in touch][].\n\n[get in touch]: http://felixge.de/consulting\n\n## Community\n\nIf you'd like to discuss this module, or ask questions about it, please use one\nof the following:\n\n* **Mailing list**: https://groups.google.com/forum/#!forum/node-mysql\n* **IRC Channel**: #node.js (on freenode.net, I pay attention to any message\n including the term `mysql`)\n\n## Establishing connections\n\nThe recommended way to establish a connection is this:\n\n```js\nvar mysql = require('mysql');\nvar connection = mysql.createConnection({\n host : 'example.org',\n user : 'bob',\n password : 'secret',\n});\n\nconnection.connect(function(err) {\n // connected! (unless `err` is set)\n});\n```\n\nHowever, a connection can also be implicitly established by invoking a query:\n\n```js\nvar mysql = require('mysql');\nvar connection = mysql.createConnection(...);\n\nconnection.query('SELECT 1', function(err, rows) {\n // connected! (unless `err` is set)\n});\n```\n\nDepending on how you like to handle your errors, either method may be\nappropriate. Any type of connection error (handshake or network) is considered\na fatal error, see the [Error Handling](#error-handling) section for more\ninformation.\n\n## Connection options\n\nWhen establishing a connection, you can set the following options:\n\n* `host`: The hostname of the database you are connecting to. (Default:\n `localhost`)\n* `port`: The port number to connect to. (Default: `3306`)\n* `socketPath`: The path to a unix domain socket to connect to. When used `host`\n and `port` are ignored.\n* `user`: The MySQL user to authenticate as.\n* `password`: The password of that MySQL user.\n* `database`: Name of the database to use for this connection (Optional).\n* `charset`: The charset for the connection. (Default: `'UTF8_GENERAL_CI'`)\n* `timezone`: The timezone used to store local dates. (Default: `'local'`)\n* `insecureAuth`: Allow connecting to MySQL instances that ask for the old\n (insecure) authentication method. (Default: `false`)\n* `typeCast`: Determines if column values should be converted to native\n JavaScript types. (Default: `true`)\n* `queryFormat`: A custom query format function. See [Custom format](#custom-format).\n* `debug`: Prints protocol details to stdout. (Default: `false`)\n* `multipleStatements`: Allow multiple mysql statements per query. Be careful\n with this, it exposes you to SQL injection attacks. (Default: `false)\n* `flags`: List of connection flags to use other than the default ones. It is\n also possible to blacklist default ones. For more information, check [Connection Flags](#connection-flags).\n\nIn addition to passing these options as an object, you can also use a url\nstring. For example:\n\n```js\nvar connection = mysql.createConnection('mysql://user:pass@host/db?debug=true&charset=BIG5_CHINESE_CI&timezone=-0700');\n```\n\nNote: The query values are first attempted to be parsed as JSON, and if that\nfails assumed to be plaintext strings.\n\n## Terminating connections\n\nThere are two ways to end a connection. Terminating a connection gracefully is\ndone by calling the `end()` method:\n\n```js\nconnection.end(function(err) {\n // The connection is terminated now\n});\n```\n\nThis will make sure all previously enqueued queries are still before sending a\n`COM_QUIT` packet to the MySQL server. If a fatal error occurs before the\n`COM_QUIT` packet can be sent, an `err` argument will be provided to the\ncallback, but the connection will be terminated regardless of that.\n\nAn alternative way to end the connection is to call the `destroy()` method.\nThis will cause an immediate termination of the underlying socket.\nAdditionally `destroy()` guarantees that no more events or callbacks will be\ntriggered for the connection.\n\n```js\nconnection.destroy();\n```\n\nUnlike `end()` the `destroy()` method does not take a callback argument.\n\n## Switching users / altering connection state\n\nMySQL offers a changeUser command that allows you to alter the current user and\nother aspects of the connection without shutting down the underlying socket:\n\n```js\nconnection.changeUser({user : 'john'}, function(err) {\n if (err) throw err;\n});\n```\n\nThe available options for this feature are:\n\n* `user`: The name of the new user (defaults to the previous one).\n* `password`: The password of the new user (defaults to the previous one).\n* `charset`: The new charset (defaults to the previous one).\n* `database`: The new database (defaults to the previous one).\n\nA sometimes useful side effect of this functionality is that this function also\nresets any connection state (variables, transactions, etc.).\n\nErrors encountered during this operation are treated as fatal connection errors\nby this module.\n\n## Server disconnects\n\nYou may lose the connection to a MySQL server due to network problems, the\nserver timing you out, or the server crashing. All of these events are\nconsidered fatal errors, and will have the `err.code =\n'PROTOCOL_CONNECTION_LOST'`. See the [Error Handling](#error-handling) section\nfor more information.\n\nThe best way to handle such unexpected disconnects is shown below:\n\n```js\nfunction handleDisconnect(connection) {\n connection.on('error', function(err) {\n if (!err.fatal) {\n return;\n }\n\n if (err.code !== 'PROTOCOL_CONNECTION_LOST') {\n throw err;\n }\n\n console.log('Re-connecting lost connection: ' + err.stack);\n\n connection = mysql.createConnection(connection.config);\n handleDisconnect(connection);\n connection.connect();\n });\n}\n\nhandleDisconnect(connection);\n```\n\nAs you can see in the example above, re-connecting a connection is done by\nestablishing a new connection. Once terminated, an existing connection object\ncannot be re-connected by design.\n\nThis logic will also be part of connection pool support once I add that to this\nlibrary.\n\n## Escaping query values\n\nIn order to avoid SQL Injection attacks, you should always escape any user\nprovided data before using it inside a SQL query. You can do so using the\n`connection.escape()` method:\n\n```js\nvar userId = 'some user provided value';\nvar sql = 'SELECT * FROM users WHERE id = ' + connection.escape(userId);\nconnection.query(sql, function(err, results) {\n // ...\n});\n```\n\nAlternatively, you can use `?` characters as placeholders for values you would\nlike to have escaped like this:\n\n```js\nconnection.query('SELECT * FROM users WHERE id = ?', [userId], function(err, results) {\n // ...\n});\n```\n\nThis looks similar to prepared statements in MySQL, however it really just uses\nthe same `connection.escape()` method internally.\n\nDifferent value types are escaped differently, here is how:\n\n* Numbers are left untouched\n* Booleans are converted to `true` / `false` strings\n* Date objects are converted to `'YYYY-mm-dd HH:ii:ss'` strings\n* Buffers are converted to hex strings, e.g. `X'0fa5'`\n* Strings are safely escaped\n* Arrays are turned into list, e.g. `['a', 'b']` turns into `'a', 'b'`\n* Nested arrays are turned into grouped lists (for bulk inserts), e.g. `[['a',\n 'b'], ['c', 'd']]` turns into `('a', 'b'), ('c', 'd')`\n* Objects are turned into `key = 'val'` pairs. Nested objects are cast to\n strings.\n* `undefined` / `null` are converted to `NULL`\n* `NaN` / `Infinity` are left as-is. MySQL does not support these, and trying\n to insert them as values will trigger MySQL errors until they implement\n support.\n\nIf you paid attention, you may have noticed that this escaping allows you\nto do neat things like this:\n\n```js\nvar post = {id: 1, title: 'Hello MySQL'};\nvar query = connection.query('INSERT INTO posts SET ?', post, function(err, result) {\n // Neat!\n});\nconsole.log(query.sql); // INSERT INTO posts SET `id` = 1, `title` = 'Hello MySQL'\n\n```\n\nIf you feel the need to escape queries by yourself, you can also use the escaping\nfunction directly:\n\n```js\nvar query = \"SELECT * FROM posts WHERE title=\" + mysql.escape(\"Hello MySQL\");\n\nconsole.log(query); // SELECT * FROM posts WHERE title='Hello MySQL'\n```\n\n## Escaping query identifiers\n\nIf you can't trust an SQL identifier (database / table / column name) because it is\nprovided by a user, you should escape it with `mysql.escapeId(identifier)` like this:\n\n```js\nvar sorter = 'date';\nvar query = 'SELECT * FROM posts ORDER BY ' + mysql.escapeId(sorter);\n\nconsole.log(query); // SELECT * FROM posts ORDER BY `date`\n```\n\nIt also supports adding qualified identifiers. It will escape both parts.\n\n```js\nvar sorter = 'date';\nvar query = 'SELECT * FROM posts ORDER BY ' + mysql.escapeId('posts.' + sorter);\n\nconsole.log(query); // SELECT * FROM posts ORDER BY `posts`.`date`\n```\n\nWhen you pass an Object to `.escape()` or `.query()`, `.escapeId()` is used to avoid SQL\ninjection in object keys.\n\n### Custom format\n\nIf you prefer to have another type of query escape format, there's a connection configuration option you can use to define a custom format function. You can access the connection object if you want to use the built-in `.escape()` or any other connection function.\n\nHere's an example of how to implement another format:\n\n```js\nconnection.config.queryFormat = function (query, values) {\n if (!values) return query;\n return query.replace(/\\:(\\w+)/g, function (txt, key) {\n if (values.hasOwnProperty(key)) {\n return this.escape(values[key]);\n }\n return txt;\n }.bind(this));\n};\n\nconnection.query(\"UPDATE posts SET title = :title\", { title: \"Hello MySQL\" });\n```\n\n## Getting the id of an inserted row\n\nIf you are inserting a row into a table with an auto increment primary key, you\ncan retrieve the insert id like this:\n\n```js\nconnection.query('INSERT INTO posts SET ?', {title: 'test'}, function(err, result) {\n if (err) throw err;\n\n console.log(result.insertId);\n});\n```\n\n## Executing queries in parallel\n\nThe MySQL protocol is sequential, this means that you need multiple connections\nto execute queries in parallel. Future version of this module may ship with a\nconnection pool implementation, but for now you have to figure out how to\nmanage multiple connections yourself if you want to execute queries in\nparallel.\n\nOne simple approach is to create one connection per incoming http request.\n\n## Streaming query rows\n\nSometimes you may want to select large quantities of rows and process each of\nthem as they are received. This can be done like this:\n\n```js\nvar query = connection.query('SELECT * FROM posts');\nquery\n .on('error', function(err) {\n // Handle error, an 'end' event will be emitted after this as well\n })\n .on('fields', function(fields) {\n // the field packets for the rows to follow\n })\n .on('result', function(row) {\n // Pausing the connnection is useful if your processing involves I/O\n connection.pause();\n\n processRow(row, function() {\n connection.resume();\n });\n })\n .on('end', function() {\n // all rows have been received\n });\n```\n\nPlease note a few things about the example above:\n\n* Usually you will want to receive a certain amount of rows before starting to\n throttle the connection using `pause()`. This number will depend on the\n amount and size of your rows.\n* `pause()` / `resume()` operate on the underlying socket and parser. You are\n guaranteed that no more `'result'` events will fire after calling `pause()`.\n* You MUST NOT provide a callback to the `query()` method when streaming rows.\n* The `'result'` event will fire for both rows as well as OK packets\n confirming the success of a INSERT/UPDATE query.\n\nAdditionally you may be interested to know that it is currently not possible to\nstream individual row columns, they will always be buffered up entirely. If you\nhave a good use case for streaming large fields to and from MySQL, I'd love to\nget your thoughts and contributions on this.\n\n## Multiple statement queries\n\nSupport for multiple statements is disabled for security reasons (it allows for\nSQL injection attacks if values are not properly escaped). To use this feature\nyou have to enable it for your connection:\n\n```js\nvar connection = mysql.createConnection({multipleStatements: true});\n```\n\nOnce enabled, you can execute multiple statement queries like any other query:\n\n```js\nconnection.query('SELECT 1; SELECT 2', function(err, results) {\n if (err) throw err;\n\n // `results` is an array with one element for every statement in the query:\n console.log(results[0]); // [{1: 1}]\n console.log(results[1]); // [{2: 2}]\n});\n```\n\nAdditionally you can also stream the results of multiple statement queries:\n\n```js\nvar query = connection.query('SELECT 1; SELECT 2');\n\nquery\n .on('fields', function(fields, index) {\n // the fields for the result rows that follow\n })\n .on('result', function(row, index) {\n // index refers to the statement this result belongs to (starts at 0)\n });\n```\n\nIf one of the statements in your query causes an error, the resulting Error\nobject contains a `err.index` property which tells you which statement caused\nit. MySQL will also stop executing any remaining statements when an error\noccurs.\n\nPlease note that the interface for streaming multiple statement queries is\nexperimental and I am looking forward to feedback on it.\n\n## Stored procedures\n\nYou can call stored procedures from your queries as with any other mysql driver.\nIf the stored procedure produces several result sets, they are exposed to you\nthe same way as the results for multiple statement queries.\n\n## Joins with overlapping column names\n\nWhen executing joins, you are likely to get result sets with overlapping column\nnames.\n\nBy default, node-mysql will overwrite colliding column names in the\norder the columns are received from MySQL, causing some of the received values\nto be unavailable.\n\nHowever, you can also specify that you want your columns to be nested below\nthe table name like this:\n\n```js\nvar options = {sql: '...', nestTables: true};\nconnection.query(options, function(err, results) {\n /* results will be an array like this now:\n [{\n table1: {\n fieldA: '...',\n fieldB: '...',\n },\n table2: {\n fieldA: '...',\n fieldB: '...',\n },\n }, ...]\n */\n});\n```\n\nOr use a string separator to have your results merged.\n\n```js\nvar options = {sql: '...', nestTables: '_'};\nconnection.query(options, function(err, results) {\n /* results will be an array like this now:\n [{\n table1_fieldA: '...',\n table1_fieldB: '...',\n table2_fieldA: '...',\n table2_fieldB: '...'\n }, ...]\n */\n});\n```\n\n## Error handling\n\nThis module comes with a consistent approach to error handling that you should\nreview carefully in order to write solid applications.\n\nAll errors created by this module are instances of the JavaScript [Error][]\nobject. Additionally they come with two properties:\n\n* `err.code`: Either a [MySQL server error][] (e.g.\n `'ER_ACCESS_DENIED_ERROR'`), a node.js error (e.g. `'ECONNREFUSED'`) or an\n internal error (e.g. `'PROTOCOL_CONNECTION_LOST'`).\n* `err.fatal`: Boolean, indicating if this error is terminal to the connection\n object.\n\n[Error]: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error\n[MySQL server error]: http://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html\n\nFatal errors are propagated to *all* pending callbacks. In the example below, a\nfatal error is triggered by trying to connect to an invalid port. Therefore the\nerror object is propagated to both pending callbacks:\n\n```js\nvar connection = require('mysql').createConnection({\n port: 84943, // WRONG PORT\n});\n\nconnection.connect(function(err) {\n console.log(err.code); // 'ECONNREFUSED'\n console.log(err.fatal); // true\n});\n\nconnection.query('SELECT 1', function(err) {\n console.log(err.code); // 'ECONNREFUSED'\n console.log(err.fatal); // true\n});\n```\n\nNormal errors however are only delegated to the callback they belong to. So in\nthe example below, only the first callback receives an error, the second query\nworks as expected:\n\n```js\nconnection.query('USE name_of_db_that_does_not_exist', function(err, rows) {\n console.log(err.code); // 'ER_BAD_DB_ERROR'\n});\n\nconnection.query('SELECT 1', function(err, rows) {\n console.log(err); // null\n console.log(rows.length); // 1\n});\n```\n\nLast but not least: If a fatal errors occurs and there are no pending\ncallbacks, or a normal error occurs which has no callback belonging to it, the\nerror is emitted as an `'error'` event on the connection object. This is\ndemonstrated in the example below:\n\n```js\nconnection.on('error', function(err) {\n console.log(err.code); // 'ER_BAD_DB_ERROR'\n});\n\nconnection.query('USE name_of_db_that_does_not_exist');\n```\n\nNote: `'error'` are special in node. If they occur without an attached\nlistener, a stack trace is printed and your process is killed.\n\n**tl;dr:** This module does not want you to deal with silent failures. You\nshould always provide callbacks to your method calls. If you want to ignore\nthis advice and suppress unhandled errors, you can do this:\n\n```js\n// I am Chuck Norris:\nconnection.on('error', function() {});\n```\n\n## Exception Safety\n\nThis module is exception safe. That means you can continue to use it, even if\none of your callback functions throws an error which you're catching using\n'uncaughtException' or a domain.\n\n## Type casting\n\nFor your convenience, this driver will cast mysql types into native JavaScript\ntypes by default. The following mappings exist:\n\n### Number\n\n* TINYINT\n* SMALLINT\n* INT\n* MEDIUMINT\n* YEAR\n* FLOAT\n* DOUBLE\n\n### Date\n\n* TIMESTAMP\n* DATE\n* DATETIME\n\n### Buffer\n\n* TINYBLOB\n* MEDIUMBLOB\n* LONGBLOB\n* BLOB\n* BINARY\n* VARBINARY\n* BIT (last byte will be filled with 0 bits as necessary)\n\n### String\n\n* CHAR\n* VARCHAR\n* TINYTEXT\n* MEDIUMTEXT\n* LONGTEXT\n* TEXT\n* ENUM\n* SET\n* DECIMAL (may exceed float precision)\n* BIGINT (may exceed float precision)\n* TIME (could be mapped to Date, but what date would be set?)\n* GEOMETRY (never used those, get in touch if you do)\n\nIt is not recommended (and may go away / change in the future) to disable type\ncasting, but you can currently do so on either the connection:\n\n```js\nvar connection = require('mysql').createConnection({typeCast: false});\n```\n\nOr on the query level:\n\n```js\nvar options = {sql: '...', typeCast: false};\nvar query = connection.query(options, function(err, results) {\n\n}):\n```\n\nYou can also pass a function and handle type casting yourself. You're given some\ncolumn information like database, table and name and also type and length. If you\njust want to apply a custom type casting to a specific type you can do it and then\nfallback to the default. Here's an example of converting `TINYINT(1)` to boolean:\n\n```js\nconnection.query({\n sql: '...',\n typeCast: function (field, next) {\n if (field.type == 'TINY' && field.length == 1) {\n return (field.string() == '1'); // 1 = true, 0 = false\n }\n return next();\n }\n})\n```\n\nIf you need a buffer there's also a `.buffer()` function and also a `.geometry()` one\nboth used by the default type cast that you can use.\n\n## Connection Flags\n\nIf, for any reason, you would like to change the default connection flags, you\ncan use the connection option `flags`. Pass a string with a comma separated list\nof items to add to the default flags. If you don't want a default flag to be used\nprepend the flag with a minus sign. To add a flag that is not in the default list, don't prepend it with a plus sign, just write the flag name (case insensitive).\n\n**Please note that some available flags that are not default are still not supported\n(e.g.: SSL, Compression). Use at your own risk.**\n\n### Example\n\nThe next example blacklists FOUND_ROWS flag from default connection flags.\n\n```js\nvar connection = mysql.createConnection(\"mysql://localhost/test?flags=-FOUND_ROWS\")\n```\n\n### Default Flags\n\n- LONG_PASSWORD\n- FOUND_ROWS\n- LONG_FLAG\n- CONNECT_WITH_DB\n- ODBC\n- LOCAL_FILES\n- IGNORE_SPACE\n- PROTOCOL_41\n- IGNORE_SIGPIPE\n- TRANSACTIONS\n- RESERVED\n- SECURE_CONNECTION\n- MULTI_RESULTS\n- MULTI_STATEMENTS (used if `multipleStatements` option is activated)\n\n### Other Available Flags\n\n- NO_SCHEMA\n- COMPRESS\n- INTERACTIVE\n- SSL\n- PS_MULTI_RESULTS\n- PLUGIN_AUTH\n- SSL_VERIFY_SERVER_CERT\n- REMEMBER_OPTIONS\n\n## Debugging and reporting problems\n\nIf you are running into problems, one thing that may help is enabling the\n`debug` mode for the connection:\n\n```js\nvar connection = mysql.createConnection({debug: true});\n```\n\nThis will print all incoming and outgoing packets on stdout.\n\nIf that does not help, feel free to open a GitHub issue. A good GitHub issue\nwill have:\n\n* The minimal amount of code required to reproduce the problem (if possible)\n* As much debugging output and information about your environment (mysql\n version, node version, os, etc.) as you can gather.\n\n## Todo\n\n* Prepared statements\n* setTimeout() for Connection / Query\n* connection pooling\n* Support for encodings other than UTF-8 / ASCII\n* API support for transactions, similar to [php](http://www.php.net/manual/en/mysqli.quickstart.transactions.php)\n", + "readmeFilename": "Readme.md", + "_id": "mysql@2.0.0-alpha5", + "_from": "mysql@2.0.0-alpha5" +} diff --git a/node_modules/mysql/test/FakeServer.js b/node_modules/mysql/test/FakeServer.js new file mode 100644 index 0000000..c99ef74 --- /dev/null +++ b/node_modules/mysql/test/FakeServer.js @@ -0,0 +1,147 @@ +// An experimental fake MySQL server for tricky integration tests. Expanded +// as needed. + +var Net = require('net'); +var Packets = require('../lib/protocol/packets'); +var PacketWriter = require('../lib/protocol/PacketWriter'); +var Parser = require('../lib/protocol/Parser'); +var Auth = require('../lib/protocol/Auth'); +var EventEmitter = require('events').EventEmitter; +var Util = require('util'); + +module.exports = FakeServer; +Util.inherits(FakeServer, EventEmitter); +function FakeServer(options) { + EventEmitter.call(this); + + this._server = null; + this._connections = []; +} + +FakeServer.prototype.listen = function(port, cb) { + this._server = Net.createServer(this._handleConnection.bind(this)); + this._server.listen(port, cb); +}; + +FakeServer.prototype._handleConnection = function(socket) { + var connection = new FakeConnection(socket); + this.emit('connection', connection); + this._connections.push(connection); +}; + +FakeServer.prototype.destroy = function() { + this._server.close(); + this._connections.forEach(function(connection) { + connection.destroy(); + }); +}; + +Util.inherits(FakeConnection, EventEmitter); +function FakeConnection(socket) { + EventEmitter.call(this); + + this._socket = socket; + this._parser = new Parser({onPacket: this._parsePacket.bind(this)}); + + this._handshakeInitializationPacket = null; + this._clientAuthenticationPacket = null; + this._oldPasswordPacket = null; + this._handshakeOptions = {}; + + socket.on('data', this._handleData.bind(this)); +} + +FakeConnection.prototype.handshake = function(options) { + this._handshakeOptions = options || {}; + + this._handshakeInitializationPacket = new Packets.HandshakeInitializationPacket({ + scrambleBuff1: new Buffer(8), + scrambleBuff2: new Buffer(12), + }); + + this._sendPacket(this._handshakeInitializationPacket); +}; + +FakeConnection.prototype.deny = function(message, errno) { + this._sendPacket(new Packets.ErrorPacket({ + message: message, + errno: errno, + })); +}; + +FakeConnection.prototype._sendPacket = function(packet) { + var writer = new PacketWriter(); + packet.write(writer); + this._socket.write(writer.toBuffer(this._parser)); +}; + +FakeConnection.prototype._handleData = function(buffer) { + this._parser.write(buffer); +}; + +FakeConnection.prototype._parsePacket = function(header) { + var Packet = this._determinePacket(header); + var packet = new Packet(); + + packet.parse(this._parser); + + switch (Packet) { + case Packets.ClientAuthenticationPacket: + this._clientAuthenticationPacket = packet; + + if (this._handshakeOptions.oldPassword) { + this._sendPacket(new Packets.UseOldPasswordPacket()); + } else { + if (this._handshakeOptions.user || this._handshakeOptions.password) { + throw new Error('not implemented'); + } + + this._sendPacket(new Packets.OkPacket()); + this._parser.resetPacketNumber(); + } + break; + case Packets.OldPasswordPacket: + this._oldPasswordPacket = packet; + + var expected = Auth.scramble323(this._handshakeInitializationPacket.scrambleBuff(), this._handshakeOptions.password); + var got = packet.scrambleBuff; + + var toString = function(buffer) { + return Array.prototype.slice.call(buffer).join(','); + }; + + if (toString(expected) === toString(got)) { + this._sendPacket(new Packets.OkPacket()); + } else { + this._sendPacket(new Packets.ErrorPacket()); + } + + this._parser.resetPacketNumber(); + break; + case Packets.ComQueryPacket: + this.emit('query', packet); + break; + default: + throw new Error('Unexpected packet: ' + Packet.name) + } +}; + +FakeConnection.prototype._determinePacket = function() { + if (!this._clientAuthenticationPacket) { + return Packets.ClientAuthenticationPacket; + } else if (this._handshakeOptions.oldPassword && !this._oldPasswordPacket) { + return Packets.OldPasswordPacket; + } + + var firstByte = this._parser.peak(); + switch (firstByte) { + case 0x03: return Packets.ComQueryPacket; + default: + throw new Error('Unknown packet, first byte: ' + firstByte); + break; + } +}; + +FakeConnection.prototype.destroy = function() { + this._socket.destroy(); +}; diff --git a/node_modules/mysql/test/common.js b/node_modules/mysql/test/common.js new file mode 100644 index 0000000..ad9c6c5 --- /dev/null +++ b/node_modules/mysql/test/common.js @@ -0,0 +1,55 @@ +var common = exports; +var path = require('path'); +var _ = require('underscore'); +var FakeServer = require('./FakeServer'); + +common.lib = path.join(__dirname, '../lib'); +common.fixtures = path.join(__dirname, 'fixtures'); + +// Useful for triggering ECONNREFUSED errors on connect() +common.bogusPort = 47378; +// Useful for triggering ER_ACCESS_DENIED_ERROR errors on connect() +common.bogusPassword = 'INVALID PASSWORD'; + +// Used for simulating a fake mysql server +common.fakeServerPort = 32893; +// Used for simulating a fake mysql server +common.fakeServerSocket = __dirname + '/fake_server.sock'; + +common.testDatabase = process.env.MYSQL_DATABASE; + +var Mysql = require('../'); + +common.isTravis = function() { + return Boolean(process.env.CI); +}; + +common.createConnection = function(config) { + if (common.isTravis()) { + // see: http://about.travis-ci.org/docs/user/database-setup/ + config = _.extend({ + user: 'root' + }, config); + } else { + config = _.extend({ + host : process.env.MYSQL_HOST, + port : process.env.MYSQL_PORT, + user : process.env.MYSQL_USER, + password : process.env.MYSQL_PASSWORD + }, config); + } + + return Mysql.createConnection(config); +}; + +common.createFakeServer = function(options) { + return new FakeServer(_.extend({}, options)); +}; + +common.useTestDb = function(connection) { + var query = connection.query('CREATE DATABASE ' + common.testDatabase, function(err) { + if (err && err.code !== 'ER_DB_CREATE_EXISTS') throw err; + }); + + connection.query('USE ' + common.testDatabase); +} diff --git a/node_modules/mysql/test/fixtures/data.csv b/node_modules/mysql/test/fixtures/data.csv new file mode 100644 index 0000000..6fac03d --- /dev/null +++ b/node_modules/mysql/test/fixtures/data.csv @@ -0,0 +1,3 @@ +1,Hello World +2,This is a test +3,For loading data from a file diff --git a/node_modules/mysql/test/integration/connection/test-bad-credentials.js b/node_modules/mysql/test/integration/connection/test-bad-credentials.js new file mode 100644 index 0000000..83d2b6e --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-bad-credentials.js @@ -0,0 +1,27 @@ +var common = require('../../common'); +var connection = common.createConnection({password: 'INVALID PASSWORD'}); +var assert = require('assert'); + +var endErr; +connection.on('end', function(err) { + assert.equal(endErr, undefined); + endErr = err; +}); + +var connectErr; +connection.connect(function(err) { + assert.equal(connectErr, undefined); + connectErr = err; + + connection.end(); +}); + +process.on('exit', function() { + if (process.env.NO_GRANT == '1' && typeof endErr == 'undefined') return; + + assert.equal(endErr.code, 'ER_ACCESS_DENIED_ERROR'); + assert.ok(/access denied/i.test(endErr.message)); + + assert.strictEqual(endErr, connectErr); +}); + diff --git a/node_modules/mysql/test/integration/connection/test-callback-errors-are-not-caught.js b/node_modules/mysql/test/integration/connection/test-callback-errors-are-not-caught.js new file mode 100644 index 0000000..17d893d --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-callback-errors-are-not-caught.js @@ -0,0 +1,20 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +var err = new Error('uncaught exception'); + +connection.connect(function() { + throw err; +}); + +var caughtErr; +process.on('uncaughtException', function(err) { + caughtErr = err; + process.exit(0); +}); + +process.on('exit', function() { + assert.strictEqual(caughtErr, err); +}); + diff --git a/node_modules/mysql/test/integration/connection/test-change-user-fatal-error.js b/node_modules/mysql/test/integration/connection/test-change-user-fatal-error.js new file mode 100644 index 0000000..9bd0549 --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-change-user-fatal-error.js @@ -0,0 +1,24 @@ +// This test verifies that changeUser errors are treated as fatal errors. The +// rationale for that is that a failure to execute a changeUser sequence may +// cause unexpected behavior for queries that were enqueued under the +// assumption of changeUser to succeed. + +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +if (common.isTravis()) { + return console.log('skipping - travis mysql does not support this test'); +} + +var err; +connection.changeUser({user: 'does-not-exist'}, function(_err) { + err = _err; + connection.end(); +}); + +process.on('exit', function() { + if (process.env.NO_GRANT == '1' && err === null) return; + assert.equal(err.code, 'ER_ACCESS_DENIED_ERROR'); + assert.equal(err.fatal, true); +}); diff --git a/node_modules/mysql/test/integration/connection/test-change-user.js b/node_modules/mysql/test/integration/connection/test-change-user.js new file mode 100644 index 0000000..b49ef41 --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-change-user.js @@ -0,0 +1,36 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +if (common.isTravis()) { + return console.log('skipping - travis mysql does not support this test'); +} + +connection.query('CREATE DATABASE ' + common.testDatabase, function(err) { + if (err && err.code !== 'ER_DB_CREATE_EXISTS') throw err; +}); + +var initialDb; +connection.query('select database() as db', function(err, results) { + if (err) throw err; + + initialDb = results[0].db; + assert.equal(connection.config.database, null); +}); + +connection.changeUser({database: common.testDatabase}); + +var finalDb; +connection.query('select database() as db', function(err, results){ + if (err) throw err; + + finalDb = results[0].db; + assert.equal(connection.config.database, finalDb); +}); + +connection.end(); + +process.on('exit', function() { + assert.equal(initialDb, null); + assert.equal(finalDb, common.testDatabase); +}); diff --git a/node_modules/mysql/test/integration/connection/test-connection-config-flags-affected-rows.js b/node_modules/mysql/test/integration/connection/test-connection-config-flags-affected-rows.js new file mode 100644 index 0000000..3029a7f --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-connection-config-flags-affected-rows.js @@ -0,0 +1,39 @@ +// Based on: +// https://github.com/ichernev/node-mysql/blob/on-duplicate-key-update/test/integration/connection/test-on-duplicate-key-update.js +// (but with CLIENT_FOUND_ROWS connection flag blacklisted) + +var common = require('../../common'); +var connection = common.createConnection({ flags: "-FOUND_ROWS" }); +var assert = require('assert'); + +common.useTestDb(connection); + +var table = 'on_duplicate_key_test'; +connection.query('DROP TABLE IF EXISTS `' + table + '`'); +connection.query([ + 'CREATE TABLE `' + table + '` (', + '`a` int(11) unsigned NOT NULL AUTO_INCREMENT,', + '`b` int(11),', + '`c` int(11),', + 'PRIMARY KEY (`a`)', + ') ENGINE=InnoDB DEFAULT CHARSET=utf8' +].join('\n')); + +connection.query('INSERT INTO `' + table + '` SET ?', {a: 1, b: 1, c: 1}); + +connection.query('INSERT INTO `' + table + '` (a, b, c) VALUES (1, 2, 3) ON DUPLICATE KEY UPDATE c = 1', function(err, info) { + assert.strictEqual(null, err); + assert.strictEqual(0, info.affectedRows, 'both primary key and updated key are the same so nothing is affected (expected 0, got ' + info.affectedRows + ' affectedRows)'); +}); + +connection.query('INSERT INTO `' + table + '` (a, b, c) VALUES (2, 3, 4) ON DUPLICATE KEY UPDATE c = 1', function(err, info) { + assert.strictEqual(null, err); + assert.strictEqual(1, info.affectedRows, 'primary key differs, so new row is inserted (expected 1, got ' + info.affectedRows + ' affectedRows)'); +}); + +connection.query('INSERT INTO `' + table + '` (a, b, c) VALUES (1, 2, 3) ON DUPLICATE KEY UPDATE c = 2', function(err, info) { + assert.strictEqual(null, err); + assert.strictEqual(2, info.affectedRows, 'primary key is the same, row is updated (expected 2, got ' + info.affectedRows + ' affectedRows)'); +}); + +connection.end(); diff --git a/node_modules/mysql/test/integration/connection/test-connection-config-flags.js b/node_modules/mysql/test/integration/connection/test-connection-config-flags.js new file mode 100644 index 0000000..4a2d6ea --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-connection-config-flags.js @@ -0,0 +1,53 @@ +var ConnectionConfig = require('../../../lib/ConnectionConfig'); +var ClientConstants = require('../../../lib/protocol/constants/client'); +var assert = require('assert'); + +var testFlags = [{ + 'default' : [ '' ], + 'user' : 'LONG_PASSWORD', + 'expected': ClientConstants.CLIENT_LONG_PASSWORD +}, { + 'default' : [ '' ], + 'user' : '-LONG_PASSWORD', + 'expected': 0x0 +}, { + 'default' : [ 'LONG_PASSWORD', 'FOUND_ROWS' ], + 'user' : '-LONG_PASSWORD', + 'expected': ClientConstants.CLIENT_FOUND_ROWS +}, { + 'default' : [ 'LONG_PASSWORD', 'FOUND_ROWS' ], + 'user' : '-FOUND_ROWS', + 'expected': ClientConstants.CLIENT_LONG_PASSWORD +}, { + 'default' : [ 'LONG_PASSWORD', 'FOUND_ROWS' ], + 'user' : '-LONG_FLAG', + 'expected': ClientConstants.CLIENT_LONG_PASSWORD | + ClientConstants.CLIENT_FOUND_ROWS +}, { + 'default' : [ 'LONG_PASSWORD', 'FOUND_ROWS' ], + 'user' : 'LONG_FLAG', + 'expected': ClientConstants.CLIENT_LONG_PASSWORD | + ClientConstants.CLIENT_FOUND_ROWS | + ClientConstants.CLIENT_LONG_FLAG +}, { + 'default' : [ 'LONG_PASSWORD', 'FOUND_ROWS' ], + 'user' : 'UNDEFINED_CONSTANT', + 'expected': ClientConstants.CLIENT_LONG_PASSWORD | + ClientConstants.CLIENT_FOUND_ROWS +}, { + 'default' : [ 'LONG_PASSWORD', 'FOUND_ROWS' ], + 'user' : '-UNDEFINED_CONSTANT', + 'expected': ClientConstants.CLIENT_LONG_PASSWORD | + ClientConstants.CLIENT_FOUND_ROWS +}, { + 'default' : [ 'LONG_PASSWORD', 'FOUND_ROWS' ], + 'user' : '-UNDEFINED_CONSTANT,, -found_ROWS', + 'expected': ClientConstants.CLIENT_LONG_PASSWORD +}]; + +for (var i = 0; i < testFlags.length; i++) { + // console.log("expected: %s got: %s", testFlags[i]['expected'], + // ConnectionConfig.mergeFlags(testFlags[i]['default'], testFlags[i]['user'])); + assert.strictEqual(testFlags[i]['expected'], + ConnectionConfig.mergeFlags(testFlags[i]['default'], testFlags[i]['user'])); +} diff --git a/node_modules/mysql/test/integration/connection/test-connection-destroy.js b/node_modules/mysql/test/integration/connection/test-connection-destroy.js new file mode 100644 index 0000000..5d80fda --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-connection-destroy.js @@ -0,0 +1,9 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +connection.connect(function(err) { + if (err) throw err; + + connection.destroy(); +}); diff --git a/node_modules/mysql/test/integration/connection/test-custom-query-format.js b/node_modules/mysql/test/integration/connection/test-custom-query-format.js new file mode 100644 index 0000000..6309433 --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-custom-query-format.js @@ -0,0 +1,18 @@ +var Mysql = require('../../../'); +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +connection.config.queryFormat = function (query, values, tz) { + if (!values) return query; + return query.replace(/\:(\w+)/g, function (txt, key) { + if (values.hasOwnProperty(key)) { + return this.escape(values[key]); + } + return txt; + }.bind(this)); +}; + +assert.equal(connection.format("SELECT :a1, :a2", { a1: 1, a2: 'two' }), "SELECT 1, 'two'"); +assert.equal(connection.format("SELECT :a1", []), "SELECT :a1"); +assert.equal(connection.format("SELECT :a1"), "SELECT :a1"); diff --git a/node_modules/mysql/test/integration/connection/test-custom-typecast.js b/node_modules/mysql/test/integration/connection/test-custom-typecast.js new file mode 100644 index 0000000..bf82198 --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-custom-typecast.js @@ -0,0 +1,46 @@ +var Mysql = require('../../../'); +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +common.useTestDb(connection); + +var table = 'custom_typecast_test'; +connection.query([ + 'CREATE TEMPORARY TABLE `' + table + '`(', + '`id` int(5),', + '`val` tinyint(1)', + ') ENGINE=InnoDB DEFAULT CHARSET=utf8' +].join('\n')); + +var results; + +connection.query("INSERT INTO " + table + " VALUES (1, 0), (2, 1), (3, NULL)"); +connection.query({ + sql: "SELECT * FROM " + table, + typeCast: function (field, next) { + if (field.type != 'TINY') { + return next(); + } + + var val = field.string(); + + if (val === null) { + return null; + } + + return (Number(val) > 0); + } +}, function (err, _results) { + if (err) throw err; + + results = _results; +}); +connection.end(); + +process.on('exit', function() { + assert.equal(results.length, 3); + assert.deepEqual(results[0], {id: 1, val: false}); + assert.deepEqual(results[1], {id: 2, val: true}); + assert.deepEqual(results[2], {id: 3, val: null}); +}); diff --git a/node_modules/mysql/test/integration/connection/test-destroy-while-streaming-rows.js b/node_modules/mysql/test/integration/connection/test-destroy-while-streaming-rows.js new file mode 100644 index 0000000..5832695 --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-destroy-while-streaming-rows.js @@ -0,0 +1,45 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +common.useTestDb(connection); + +var table = 'stream_test'; +connection.query([ + 'CREATE TEMPORARY TABLE `' + table + '` (', + '`id` int(11) unsigned NOT NULL AUTO_INCREMENT,', + '`title` varchar(255),', + 'PRIMARY KEY (`id`)', + ') ENGINE=InnoDB DEFAULT CHARSET=utf8' +].join('\n')); + +var rowCount = 10; +for (var i = 1; i <= rowCount; i++) { + var row = { + id: i, + title: 'Row #' + i, + }; + + connection.query('INSERT INTO ' + table + ' SET ?', row); +} + +var destroyed = false; +var hadEnd = false; +var query = connection.query('SELECT * FROM ' + table); + +query + .on('result', function(row) { + assert.equal(destroyed, false); + + destroyed = true; + connection.destroy(); + }) + .on('end', function() { + hadEnd = true; + }); + +connection.end(); + +process.on('exit', function() { + assert.strictEqual(hadEnd, false); +}); diff --git a/node_modules/mysql/test/integration/connection/test-double-connect.js b/node_modules/mysql/test/integration/connection/test-double-connect.js new file mode 100644 index 0000000..99c51ca --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-double-connect.js @@ -0,0 +1,24 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +var didConnect = false; +connection.connect(function(err) { + if (err) throw err; + + assert.equal(didConnect, false); + didConnect = true; +}); + +var err; +connection.connect(function(_err) { + err = _err; +}); + +connection.end(); + +process.on('exit', function() { + assert.equal(didConnect, true); + assert.equal(err.fatal, false); + assert.equal(err.code, 'PROTOCOL_ENQUEUE_HANDSHAKE_TWICE'); +}); diff --git a/node_modules/mysql/test/integration/connection/test-drain-event.js b/node_modules/mysql/test/integration/connection/test-drain-event.js new file mode 100644 index 0000000..b075abf --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-drain-event.js @@ -0,0 +1,20 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +connection.connect(); + +var got_drain = false; + +connection.on('drain', function() { + got_drain = true; +}); + +connection.query("SELECT 1", function(err) { + assert.equal(got_drain, false); + assert.ok(!err); + process.nextTick(function() { + assert.equal(got_drain, true); + connection.end(); + }); +}); diff --git a/node_modules/mysql/test/integration/connection/test-end-callback.js b/node_modules/mysql/test/integration/connection/test-end-callback.js new file mode 100644 index 0000000..021022c --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-end-callback.js @@ -0,0 +1,27 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +connection.connect(); + +var gotEnd = false; +connection.on('end', function(err) { + assert.equal(gotEnd, false); + assert.ok(!err); + + gotEnd = true; +}); + +var gotCallback = false; +connection.end(function(err) { + if (err) throw err; + + assert.equal(gotCallback, false); + gotCallback = true; +}); + +process.on('exit', function() { + assert.equal(gotCallback, true); + assert.equal(gotEnd, true); +}); + diff --git a/node_modules/mysql/test/integration/connection/test-escape-id.js b/node_modules/mysql/test/integration/connection/test-escape-id.js new file mode 100644 index 0000000..743bd53 --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-escape-id.js @@ -0,0 +1,7 @@ +var SqlString = require('../../../lib/protocol/SqlString'); +var assert = require('assert'); + +assert.equal('`id`', SqlString.escapeId('id')); +assert.equal('`i``d`', SqlString.escapeId('i`d')); +assert.equal('`id1`.`id2`', SqlString.escapeId('id1.id2')); +assert.equal('`id``1`.`i``d2`', SqlString.escapeId('id`1.i`d2')); diff --git a/node_modules/mysql/test/integration/connection/test-exception-safety.js b/node_modules/mysql/test/integration/connection/test-exception-safety.js new file mode 100644 index 0000000..dd78684 --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-exception-safety.js @@ -0,0 +1,66 @@ +// This test covers all event / callback interfaces offered by node-mysql and +// throws an exception in them. Exception safety means that each of those +// exceptions can be caught by an 'uncaughtException' / Domain handler without +// the connection instance ending up in a bad state where it doesn't work +// properly or doesn't execute the next sequence anymore. + +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + + +var errors = []; +process.on('uncaughtException', function(err) { + console.log(err.stack); + errors.push(err); +}); + +// Normal callback +connection.connect(function(err) { + throw err || new Error('1'); +}); + +// Normal callback (same code path as connect, but in here should that +// implementation detail change at some point). +connection.query('SELECT 1', function(err) { + throw err || new Error('2'); +}); + +// Row streaming events +connection.query('SELECT 1') + .on('fields', function() { + throw new Error('3'); + }) + .on('result', function() { + throw new Error('4'); + }); + +// Normal callback with error +connection.query('INVALID SQL', function(err) { + assert.equal(err.code, 'ER_PARSE_ERROR'); + + throw new Error('5'); +}); + +// Row streaming 'result' event triggered by Ok Packet (special code path) +connection.query('USE ' + common.testDatabase) + .on('result', function() { + throw new Error('6'); + }); + +// Normal callback (same code path as connect, but in here should that +// implementation detail change at some point). +connection.end(function(err) { + throw err || new Error('7'); +}); + +process.on('exit', function() { + process.removeAllListeners(); + + var expectedErrors = 7; + for (var i = 0; i < expectedErrors - 1; i++) { + var error = errors[i]; + assert.equal(error.message, String(i + 1)); + assert.equal(error.code, undefined); + } +}); diff --git a/node_modules/mysql/test/integration/connection/test-fatal-auth-error-going-to-all-callbacks.js b/node_modules/mysql/test/integration/connection/test-fatal-auth-error-going-to-all-callbacks.js new file mode 100644 index 0000000..88d3a2e --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-fatal-auth-error-going-to-all-callbacks.js @@ -0,0 +1,25 @@ +var common = require('../../common'); +var connection = common.createConnection({password: common.bogusPassword}); +var assert = require('assert'); + +var errors = {}; + +connection.connect(function(err) { + assert.equal(errors.a, undefined); + errors.a = err; +}); + +connection.query('SELECT 1', function(err) { + assert.equal(errors.b, undefined); + errors.b = err; + + connection.end(); +}); + +process.on('exit', function() { + if (process.env.NO_GRANT == '1' && errors.a === null) return; + + assert.equal(errors.a.code, 'ER_ACCESS_DENIED_ERROR'); + assert.equal(errors.a.fatal, true); + assert.strictEqual(errors.a, errors.b); +}); diff --git a/node_modules/mysql/test/integration/connection/test-fatal-auth-error-without-handlers.js b/node_modules/mysql/test/integration/connection/test-fatal-auth-error-without-handlers.js new file mode 100644 index 0000000..1c7c88f --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-fatal-auth-error-without-handlers.js @@ -0,0 +1,20 @@ +var common = require('../../common'); +var connection = common.createConnection({password: common.bogusPassword}); +var assert = require('assert'); + +connection.connect(); +connection.query('SELECT 1'); + +var err; +connection.on('error', function(_err) { + assert.equal(err, undefined); + err = _err; +}); +connection.end(); + +process.on('exit', function() { + if (process.env.NO_GRANT == '1' && typeof err == 'undefined') return; + + assert.equal(err.code, 'ER_ACCESS_DENIED_ERROR'); + assert.equal(err.fatal, true); +}); diff --git a/node_modules/mysql/test/integration/connection/test-fatal-network-error-going-to-all-callbacks.js b/node_modules/mysql/test/integration/connection/test-fatal-network-error-going-to-all-callbacks.js new file mode 100644 index 0000000..20c6e27 --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-fatal-network-error-going-to-all-callbacks.js @@ -0,0 +1,21 @@ +var common = require('../../common'); +var connection = common.createConnection({port: common.bogusPort}); +var assert = require('assert'); + +var errors = {}; + +connection.connect(function(err) { + assert.equal(errors.a, undefined); + errors.a = err; +}); + +connection.query('SELECT 1', function(err) { + assert.equal(errors.b, undefined); + errors.b = err; +}); + +process.on('exit', function() { + assert.equal(errors.a.code, 'ECONNREFUSED'); + assert.equal(errors.a.fatal, true); + assert.strictEqual(errors.a, errors.b); +}); diff --git a/node_modules/mysql/test/integration/connection/test-fatal-network-error-without-handlers.js b/node_modules/mysql/test/integration/connection/test-fatal-network-error-without-handlers.js new file mode 100644 index 0000000..6575be6 --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-fatal-network-error-without-handlers.js @@ -0,0 +1,17 @@ +var common = require('../../common'); +var connection = common.createConnection({port: common.bogusPort}); +var assert = require('assert'); + +connection.connect(); +connection.query('SELECT 1'); + +var err; +connection.on('error', function(_err) { + assert.equal(err, undefined); + err = _err; +}); + +process.on('exit', function() { + assert.equal(err.code, 'ECONNREFUSED'); + assert.equal(err.fatal, true); +}); diff --git a/node_modules/mysql/test/integration/connection/test-fatal-query-error-without-callback.js b/node_modules/mysql/test/integration/connection/test-fatal-query-error-without-callback.js new file mode 100644 index 0000000..3c26147 --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-fatal-query-error-without-callback.js @@ -0,0 +1,19 @@ +var common = require('../../common'); +var connection = common.createConnection({port: common.bogusPort}); +var assert = require('assert'); + +connection.connect(); +var query = connection.query('SELECT 1'); + +var err; +query.on('error', function(_err) { + assert.equal(err, undefined); + err = _err; +}); + +connection.end(); + +process.on('exit', function() { + assert.equal(err.code, 'ECONNREFUSED'); + assert.equal(err.fatal, true); +}); diff --git a/node_modules/mysql/test/integration/connection/test-host-denied-error.js b/node_modules/mysql/test/integration/connection/test-host-denied-error.js new file mode 100644 index 0000000..f831628 --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-host-denied-error.js @@ -0,0 +1,26 @@ +var common = require('../../common'); +var connection = common.createConnection({port: common.fakeServerPort}); +var assert = require('assert'); + +var server = common.createFakeServer(); + +var connectErr; +server.listen(common.fakeServerPort, function(err) { + if (err) throw err; + + connection.connect(function(err) { + connectErr = err; + server.destroy(); + }); +}); + +server.on('connection', function(incomingConnection) { + var errno = 1130; // ER_HOST_NOT_PRIVILEGED + incomingConnection.deny('You suck.', errno); +}); + +process.on('exit', function() { + assert.equal(connectErr.code, 'ER_HOST_NOT_PRIVILEGED'); + assert.ok(/You suck/.test(connectErr.message)); + assert.equal(connectErr.fatal, true); +}); diff --git a/node_modules/mysql/test/integration/connection/test-implicit-connect.js b/node_modules/mysql/test/integration/connection/test-implicit-connect.js new file mode 100644 index 0000000..5db2e9d --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-implicit-connect.js @@ -0,0 +1,16 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +var rows = undefined; +connection.query('SELECT 1', function(err, _rows) { + if (err) throw err; + + rows = _rows; +}); + +connection.end(); + +process.on('exit', function() { + assert.deepEqual(rows, [{1: 1}]); +}); diff --git a/node_modules/mysql/test/integration/connection/test-insert-results.js b/node_modules/mysql/test/integration/connection/test-insert-results.js new file mode 100644 index 0000000..b00071e --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-insert-results.js @@ -0,0 +1,26 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +common.useTestDb(connection); + +var table = 'insert_test'; +connection.query([ + 'CREATE TEMPORARY TABLE `' + table + '` (', + '`id` int(11) unsigned NOT NULL AUTO_INCREMENT,', + '`title` varchar(255),', + 'PRIMARY KEY (`id`)', + ') ENGINE=InnoDB DEFAULT CHARSET=utf8' +].join('\n')); + +var result; +connection.query('INSERT INTO ' + table + ' SET ?', {title: 'test'}, function(err, _result) { + if (err) throw err; + + result = _result; +}); +connection.end(); + +process.on('exit', function() { + assert.strictEqual(result.insertId, 1); +}); diff --git a/node_modules/mysql/test/integration/connection/test-load-data-infile.js b/node_modules/mysql/test/integration/connection/test-load-data-infile.js new file mode 100644 index 0000000..397819a --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-load-data-infile.js @@ -0,0 +1,55 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +common.useTestDb(connection); + +var table = 'load_data_test'; +connection.query([ + 'CREATE TEMPORARY TABLE `' + table + '` (', + '`id` int(11) unsigned NOT NULL AUTO_INCREMENT,', + '`title` varchar(255),', + 'PRIMARY KEY (`id`)', + ') ENGINE=InnoDB DEFAULT CHARSET=utf8' +].join('\n')); + +var path = common.fixtures + '/data.csv'; +var sql = + 'LOAD DATA LOCAL INFILE ? INTO TABLE ' + table + ' ' + + 'FIELDS TERMINATED BY ? (id, title)'; + +var ok; +connection.query(sql, [path, ','], function(err, _ok) { + if (err) throw err; + + ok = _ok; +}); + +var rows; +connection.query('SELECT * FROM ' + table, function(err, _rows) { + if (err) throw err; + + rows = _rows; +}); + +// Try to load a file that does not exist to see if we handle this properly +var loadErr; +var loadResult; +var badPath = common.fixtures + '/does_not_exist.csv'; + +connection.query(sql, [badPath, ','], function(err, result) { + loadErr = err; + loadResult = result; +}); + +connection.end(); + +process.on('exit', function() { + assert.equal(ok.affectedRows, 3); + assert.equal(rows.length, 3); + assert.equal(rows[0].id, 1); + assert.equal(rows[0].title, 'Hello World'); + + assert.equal(loadErr.code, 'ENOENT'); + assert.equal(loadResult.affectedRows, 0); +}); diff --git a/node_modules/mysql/test/integration/connection/test-long-stack-traces-for-connection-errors.js b/node_modules/mysql/test/integration/connection/test-long-stack-traces-for-connection-errors.js new file mode 100644 index 0000000..9b95598 --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-long-stack-traces-for-connection-errors.js @@ -0,0 +1,12 @@ +var common = require('../../common'); +var connection = common.createConnection({port: common.bogusPort}); +var assert = require('assert'); + +var err; +connection.connect(function(_err) { + err = _err; +}); + +process.on('exit', function() { + assert.ok(err.stack.indexOf(__filename) > 0); +}); diff --git a/node_modules/mysql/test/integration/connection/test-long-stack-traces.js b/node_modules/mysql/test/integration/connection/test-long-stack-traces.js new file mode 100644 index 0000000..def47fc --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-long-stack-traces.js @@ -0,0 +1,16 @@ +// Experimental: https://github.com/felixge/node-mysql/issues/198 + +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +var err; +connection.query('invalid sql', function(_err) { + err = _err; +}); + +connection.end(); + +process.on('exit', function() { + assert.ok(err.stack.indexOf(__filename) > 0); +}); diff --git a/node_modules/mysql/test/integration/connection/test-multiple-statements-streaming.js b/node_modules/mysql/test/integration/connection/test-multiple-statements-streaming.js new file mode 100644 index 0000000..b80817f --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-multiple-statements-streaming.js @@ -0,0 +1,55 @@ +var common = require('../../common'); +var connection = common.createConnection({multipleStatements: true}); +var assert = require('assert'); + +var sql = [ + 'SELECT 1', + 'USE ' + common.testDatabase, + 'SELECT 2', + 'invalid sql', + 'SELECT 3', +].join('; '); + +var results = []; +var fields = []; +var hadErr = false; + +var query = connection.query(sql); +query + .on('error', function(err) { + assert.equal(hadErr, false); + hadErr = true; + + assert.equal(err.code, 'ER_PARSE_ERROR'); + assert.equal(err.index, 3); + }) + .on('fields', function(_fields, index) { + fields.push({fields: _fields, index: index}); + }) + .on('result', function(result, index) { + results.push({result: result, index: index}); + }); + +connection.end(); + +process.on('exit', function() { + assert.ok(hadErr); + + assert.equal(results.length, 3); + + assert.deepEqual(results[0].result, {1: 1}); + assert.equal(results[0].index, 0); + + assert.equal(results[1].result.constructor.name, 'OkPacket'); + assert.equal(results[1].index, 1); + + assert.deepEqual(results[2].result, {2: 2}); + assert.equal(results[2].index, 2); + + assert.equal(fields.length, 2); + assert.equal(fields[0].fields[0].name, '1'); + assert.equal(fields[1].fields[0].name, '2'); + + assert.equal(fields[0].index, 0); + assert.equal(fields[1].index, 2); +}); diff --git a/node_modules/mysql/test/integration/connection/test-multiple-statements-with-error.js b/node_modules/mysql/test/integration/connection/test-multiple-statements-with-error.js new file mode 100644 index 0000000..11056cf --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-multiple-statements-with-error.js @@ -0,0 +1,36 @@ +var common = require('../../common'); +var connection = common.createConnection({multipleStatements: true}); +var assert = require('assert'); + +var sql = [ + 'SELECT 1', + 'invalid sql', + 'SELECT 2', +].join('; '); + +var finishedQueryOne = false; +connection.query(sql, function(err, results, fields) { + assert.equal(finishedQueryOne, false); + finishedQueryOne = true; + + assert.equal(err.code, 'ER_PARSE_ERROR'); + assert.deepEqual(results, [[{1: 1}]]); + + assert.equal(fields.length, 1); + assert.equal(fields[0][0].name, '1'); +}); + +var finishedQueryTwo = false; +connection.query('SELECT 3', function(err, results) { + assert.equal(finishedQueryTwo, false); + finishedQueryTwo = true; + + assert.deepEqual(results, [{3: 3}]); +}); + +connection.end(); + +process.on('exit', function() { + assert.equal(finishedQueryOne, true); + assert.equal(finishedQueryTwo, true); +}); diff --git a/node_modules/mysql/test/integration/connection/test-multiple-statements.js b/node_modules/mysql/test/integration/connection/test-multiple-statements.js new file mode 100644 index 0000000..f60d3e4 --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-multiple-statements.js @@ -0,0 +1,31 @@ +var common = require('../../common'); +var connection = common.createConnection({multipleStatements: true}); +var assert = require('assert'); + +var sql = [ + 'SELECT 1', + 'USE ' + common.testDatabase, + 'SELECT 2', +].join('; '); + +var results; +var fields; +connection.query(sql, function(err, _results, _fields) { + if (err) throw err; + + results = _results; + fields = _fields; +}); + +connection.end(); + +process.on('exit', function() { + assert.equal(results.length, 3); + assert.deepEqual(results[0], [{1: 1}]); + assert.strictEqual(results[1].constructor.name, 'OkPacket'); + assert.deepEqual(results[2], [{2: 2}]); + + assert.equal(fields[0][0].name, '1'); + assert.equal(fields[1], undefined); + assert.equal(fields[2][0].name, '2'); +}); diff --git a/node_modules/mysql/test/integration/connection/test-nested-tables-query.js b/node_modules/mysql/test/integration/connection/test-nested-tables-query.js new file mode 100644 index 0000000..be6f3e5 --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-nested-tables-query.js @@ -0,0 +1,48 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +common.useTestDb(connection); + +var table = 'nested_test'; +connection.query([ + 'CREATE TEMPORARY TABLE `' + table + '` (', + '`id` int(11) unsigned NOT NULL AUTO_INCREMENT,', + '`title` varchar(255),', + 'PRIMARY KEY (`id`)', + ') ENGINE=InnoDB DEFAULT CHARSET=utf8' +].join('\n')); + +connection.query('INSERT INTO ' + table + ' SET ?', {title: 'test'}); + +var options1 = { + nestTables: true, + sql: 'SELECT * FROM ' + table +}; +var options2 = { + nestTables: '_', + sql: 'SELECT * FROM ' + table +}; +var rows1, rows2; + +connection.query(options1, function(err, _rows) { + if (err) throw err; + + rows1 = _rows; +}); +connection.query(options2, function(err, _rows) { + if (err) throw err; + + rows2 = _rows; +}); + +connection.end(); + +process.on('exit', function() { + assert.equal(rows1.length, 1); + assert.equal(rows1[0].nested_test.id, 1); + assert.equal(rows1[0].nested_test.title, 'test'); + assert.equal(rows2.length, 1); + assert.equal(rows2[0].nested_test_id, 1); + assert.equal(rows2[0].nested_test_title, 'test'); +}); diff --git a/node_modules/mysql/test/integration/connection/test-normal-error-without-handler.js b/node_modules/mysql/test/integration/connection/test-normal-error-without-handler.js new file mode 100644 index 0000000..384559b --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-normal-error-without-handler.js @@ -0,0 +1,19 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +connection.connect(); +connection.query('INVALID SQL'); + +var err; +connection.on('error', function(_err) { + assert.equal(err, undefined); + err = _err; +}); + +connection.end(); + +process.on('exit', function() { + assert.equal(err.code, 'ER_PARSE_ERROR'); + assert.equal(Boolean(err.fatal), false); +}); diff --git a/node_modules/mysql/test/integration/connection/test-normal-query-error-without-callback.js b/node_modules/mysql/test/integration/connection/test-normal-query-error-without-callback.js new file mode 100644 index 0000000..414b391 --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-normal-query-error-without-callback.js @@ -0,0 +1,19 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +connection.connect(); +var query = connection.query('INVALID SQL'); + +var err; +query.on('error', function(_err) { + assert.equal(err, undefined); + err = _err; +}); + +connection.end(); + +process.on('exit', function() { + assert.equal(err.code, 'ER_PARSE_ERROR'); + assert.equal(Boolean(err.fatal), false); +}); diff --git a/node_modules/mysql/test/integration/connection/test-old-password.js b/node_modules/mysql/test/integration/connection/test-old-password.js new file mode 100644 index 0000000..3662510 --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-old-password.js @@ -0,0 +1,35 @@ +var common = require('../../common'); +var connection = common.createConnection({ + port : common.fakeServerPort, + password : 'oldpw', + insecureAuth : true, +}); +var assert = require('assert'); + +var server = common.createFakeServer(); + +var connected; +server.listen(common.fakeServerPort, function(err) { + if (err) throw err; + + connection.connect(function(err, result) { + if (err) throw err; + + connected = result; + + connection.destroy(); + server.destroy(); + }); +}); + +server.on('connection', function(incomingConnection) { + incomingConnection.handshake({ + user : connection.config.user, + password : connection.config.password, + oldPassword : true, + }); +}); + +process.on('exit', function() { + assert.equal(connected.fieldCount, 0); +}); diff --git a/node_modules/mysql/test/integration/connection/test-ping.js b/node_modules/mysql/test/integration/connection/test-ping.js new file mode 100644 index 0000000..25c3135 --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-ping.js @@ -0,0 +1,15 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +var pingErr; + +connection.ping(function(err) { + pingErr = err; +}); + +connection.end(); + +process.on('exit', function() { + assert.equal(pingErr, null); +}); diff --git a/node_modules/mysql/test/integration/connection/test-procedure-with-multiple-selects.js b/node_modules/mysql/test/integration/connection/test-procedure-with-multiple-selects.js new file mode 100644 index 0000000..fc5338d --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-procedure-with-multiple-selects.js @@ -0,0 +1,40 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +common.useTestDb(connection); + +var procedureName = 'multipleSelectProcedure'; +var input0 = 1; +var input1 = 1000; +var fieldName0 = 'param0'; +var fieldName1 = 'param1'; +var result = undefined; + +connection.query([ + 'CREATE DEFINER=root@localhost PROCEDURE '+procedureName+'(IN '+fieldName0+' INT, IN '+fieldName1+' INT)', + 'BEGIN', + 'SELECT '+fieldName0+';', + 'SELECT '+fieldName1+';', + 'END' +].join('\n')); + +connection.query('CALL '+procedureName+'(?,?)', [input0,input1], function(err, _result) { + if (err) throw err; + + _result.pop(); // drop metadata + result = _result; +}); + +connection.query('DROP PROCEDURE '+procedureName); + +connection.end(); + +process.on('exit', function() { + var result0Expected = {}; + result0Expected[fieldName0] = input0; + var result1Expected = {}; + result1Expected[fieldName1] = input1; + + assert.deepEqual(result, [[result0Expected], [result1Expected]]); +}); diff --git a/node_modules/mysql/test/integration/connection/test-procedure-with-single-select.js b/node_modules/mysql/test/integration/connection/test-procedure-with-single-select.js new file mode 100644 index 0000000..72ac750 --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-procedure-with-single-select.js @@ -0,0 +1,33 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +common.useTestDb(connection); + +var procedureName = 'singleSelectProcedure'; +var input = 1; +var fieldName = 'param'; +var result = undefined; + +connection.query([ + 'CREATE DEFINER=root@localhost PROCEDURE '+procedureName+'(IN '+fieldName+' INT)', + 'BEGIN', + 'SELECT '+fieldName+';', + 'END' +].join('\n')); + +connection.query('CALL '+procedureName+'(?)', [input], function(err, _result) { + if (err) throw err; + _result.pop(); // drop metadata + result = _result; +}); + +connection.query('DROP PROCEDURE '+procedureName); + +connection.end(); + +process.on('exit', function() { + var expected = {}; + expected[fieldName] = input; + assert.deepEqual(result, [[expected]]); +}); diff --git a/node_modules/mysql/test/integration/connection/test-query-after-destroy.js b/node_modules/mysql/test/integration/connection/test-query-after-destroy.js new file mode 100644 index 0000000..22cb678 --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-query-after-destroy.js @@ -0,0 +1,17 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +var err; +connection.connect(function() { + connection.destroy(); + + connection.query('SELECT 1', function(_err) { + err = _err; + }); +}); + +process.on('exit', function() { + assert.equal(err.fatal, false); + assert.equal(err.code, 'PROTOCOL_ENQUEUE_AFTER_DESTROY'); +}); diff --git a/node_modules/mysql/test/integration/connection/test-query-after-end-without-callback.js b/node_modules/mysql/test/integration/connection/test-query-after-end-without-callback.js new file mode 100644 index 0000000..302d958 --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-query-after-end-without-callback.js @@ -0,0 +1,23 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +var didEnd = false; +connection.connect(); +connection.end(function(err) { + if (err) throw err; + + didEnd = true; +}); + +var err; +connection.on('error', function(_err) { + err = _err; +}); +connection.query('SELECT 1'); + +process.on('exit', function() { + assert.equal(didEnd, true); + assert.equal(err.code, 'PROTOCOL_ENQUEUE_AFTER_QUIT'); + assert.equal(err.fatal, false); +}); diff --git a/node_modules/mysql/test/integration/connection/test-query-after-end.js b/node_modules/mysql/test/integration/connection/test-query-after-end.js new file mode 100644 index 0000000..e907b48 --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-query-after-end.js @@ -0,0 +1,23 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +var didEnd = false; +connection.connect(); +connection.end(function(err) { + if (err) throw err; + + didEnd = true; +}); + +var err; +connection.query('SELECT 1', function(_err) { + assert.equal(didEnd, false); + err = _err; +}); + +process.on('exit', function() { + assert.equal(didEnd, true); + assert.equal(err.code, 'PROTOCOL_ENQUEUE_AFTER_QUIT'); + assert.equal(err.fatal, false); +}); diff --git a/node_modules/mysql/test/integration/connection/test-query-escaping.js b/node_modules/mysql/test/integration/connection/test-query-escaping.js new file mode 100644 index 0000000..145733f --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-query-escaping.js @@ -0,0 +1,36 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +common.useTestDb(connection); + +var table = 'escape_test'; +connection.query([ + 'CREATE TEMPORARY TABLE `' + table + '` (', + '`id` int(11) unsigned NOT NULL AUTO_INCREMENT,', + '`example` varchar(255),', + 'PRIMARY KEY (`id`)', + ') ENGINE=InnoDB DEFAULT CHARSET=utf8' +].join('\n')); + +connection.query('INSERT INTO ' + table + ' SET id = ?, example = ?', [1, 'array escape']); +connection.query('INSERT INTO ' + table + ' SET ?', { + id: 2, + example: 'object escape' +}); + +var rows; +connection.query('SELECT * FROM escape_test', function(err, _rows) { + if (err) throw err; + + rows = _rows; +}); + +connection.end(); + + +process.on('exit', function() { + assert.equal(rows.length, 2); + assert.deepEqual(rows[0], {id: 1, example: 'array escape'}); + assert.deepEqual(rows[1], {id: 2, example: 'object escape'}); +}); diff --git a/node_modules/mysql/test/integration/connection/test-select-1.js b/node_modules/mysql/test/integration/connection/test-select-1.js new file mode 100644 index 0000000..64fa83a --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-select-1.js @@ -0,0 +1,21 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +connection.connect(); + +var rows = undefined; +var fields = undefined; +connection.query('SELECT 1', function(err, _rows, _fields) { + if (err) throw err; + + rows = _rows; + fields = _fields; +}); + +connection.end(); + +process.on('exit', function() { + assert.deepEqual(rows, [{1: 1}]); + assert.equal(fields[0].name, '1'); +}); diff --git a/node_modules/mysql/test/integration/connection/test-select-empty-string.js b/node_modules/mysql/test/integration/connection/test-select-empty-string.js new file mode 100644 index 0000000..8e10ce7 --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-select-empty-string.js @@ -0,0 +1,16 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +var rows; +connection.query('SELECT ""', function(err, _rows) { + if (err) throw err; + + rows = _rows; +}); + +connection.end(); + +process.on('exit', function() { + assert.deepEqual(rows, [{'': ''}]); +}); diff --git a/node_modules/mysql/test/integration/connection/test-send-and-receive-large-packets.js b/node_modules/mysql/test/integration/connection/test-send-and-receive-large-packets.js new file mode 100644 index 0000000..3509af7 --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-send-and-receive-large-packets.js @@ -0,0 +1,81 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +connection.connect(function(err) { + if (err) throw err; + + getMaxAllowedPacket(); +}); + + +var oldMaxAllowedPacket; +function getMaxAllowedPacket() { + connection.query('SHOW VARIABLES WHERE Variable_name = ?', ['max_allowed_packet'], function(err, rows) { + if (err) throw err; + + oldMaxAllowedPacket = Number(rows[0].Value); + + increaseMaxAllowedPacketIfNeeded(); + }); +} + +function increaseMaxAllowedPacketIfNeeded() { + // Our test generates a SQL query a few bytes larger than 16 MB, but lets + // leave a little margin: + var minMaxAllowedPacket = 20 * 1024 * 1024; + + var newMaxAllowedPacket = (oldMaxAllowedPacket < minMaxAllowedPacket) + ? minMaxAllowedPacket + : oldMaxAllowedPacket; + + connection.query('SET GLOBAL max_allowed_packet = ?', [newMaxAllowedPacket], function(err, rows) { + if (err) throw err; + + + // We need to re-connect for this change to take effect, bah + connection.end(); + connection = common.createConnection(); + + // We need to wait for the re-connect to happen before starting the actual + // test. That's because our buffer to hex shim in 0.4.x takes ~12 sec on + // TravisCI, causing a MySQL connection timeout otherwise. + connection.connect(function(err) { + if (err) throw err; + + triggerLargeQueryAndResponsePackets(); + }); + }); +} + +var rows = []; +var length = Math.pow(256, 3) / 2; // Half, because of hex encoding +var buffer = new Buffer(length); +var sql = 'SELECT ? as bigField'; + +function triggerLargeQueryAndResponsePackets() { + connection.query(sql, [buffer], function(err, _rows) { + if (err) throw err; + + rows = _rows; + + resetMaxAllowedPacket(); + }); +} + +function resetMaxAllowedPacket() { + connection.query('SET GLOBAL max_allowed_packet = ?', [oldMaxAllowedPacket], function(err, rows) { + if (err) { + err.message = 'Could not reset max_allowed_packet size, please check your server settings: ' + err.message; + throw err; + } + }); + + connection.end(); +} + + +process.on('exit', function() { + assert.equal(rows.length, 1); + assert.equal(rows[0].bigField.length, length); +}); diff --git a/node_modules/mysql/test/integration/connection/test-server-timeout-disconnect.js b/node_modules/mysql/test/integration/connection/test-server-timeout-disconnect.js new file mode 100644 index 0000000..b4ec403 --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-server-timeout-disconnect.js @@ -0,0 +1,24 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +connection.query('SET wait_timeout = 1'); + +var errorErr; +var endErr; +connection + .on('end', function(err) { + assert.ok(!endErr); + endErr = err; + }) + .on('error', function(err) { + assert.ok(!errorErr); + errorErr = err; + }); + +process.on('exit', function() { + assert.strictEqual(errorErr.code, 'PROTOCOL_CONNECTION_LOST'); + assert.strictEqual(errorErr.fatal, true); + + assert.strictEqual(endErr, errorErr); +}); diff --git a/node_modules/mysql/test/integration/connection/test-server-unexpected-disconnect.js b/node_modules/mysql/test/integration/connection/test-server-unexpected-disconnect.js new file mode 100644 index 0000000..6bb5957 --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-server-unexpected-disconnect.js @@ -0,0 +1,36 @@ +var common = require('../../common'); +var connection = common.createConnection({port: common.fakeServerPort}); +var assert = require('assert'); + +var endErr; +connection.on('end', function(err) { + assert.ok(!endErr); + endErr = err; +}); + +var queryErr; + +var server = common.createFakeServer(); +server.listen(common.fakeServerPort, function(err) { + if (err) throw err; + + connection.query('SELECT 1', function(err) { + assert.ok(!queryErr); + queryErr = err; + }); +}); + +server.on('connection', function(connection) { + connection.handshake(); + + connection.on('query', function(packet) { + server.destroy(); + }); +}); + +process.on('exit', function() { + assert.strictEqual(queryErr.code, 'PROTOCOL_CONNECTION_LOST'); + assert.strictEqual(queryErr.fatal, true); + + assert.strictEqual(endErr, queryErr); +}); diff --git a/node_modules/mysql/test/integration/connection/test-statistics.js b/node_modules/mysql/test/integration/connection/test-statistics.js new file mode 100644 index 0000000..5fc9865 --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-statistics.js @@ -0,0 +1,26 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +var statsErr, statsData; + +connection.statistics(function(err, data) { + statsErr = err; + statsData = data; +}); + +connection.end(); + +process.on('exit', function() { + assert.equal(statsErr, null); + assert.strictEqual(typeof statsData, "object"); + assert.strictEqual(statsData.hasOwnProperty("message"), true); + assert.strictEqual(statsData.hasOwnProperty("uptime"), true); + assert.strictEqual(statsData.hasOwnProperty("threads"), true); + assert.strictEqual(statsData.hasOwnProperty("questions"), true); + assert.strictEqual(statsData.hasOwnProperty("slow_queries"), true); + assert.strictEqual(statsData.hasOwnProperty("opens"), true); + assert.strictEqual(statsData.hasOwnProperty("flush_tables"), true); + assert.strictEqual(statsData.hasOwnProperty("open_tables"), true); + assert.strictEqual(statsData.hasOwnProperty("queries_per_second_avg"), true); +}); diff --git a/node_modules/mysql/test/integration/connection/test-streaming-rows-quick-pause-resume.js b/node_modules/mysql/test/integration/connection/test-streaming-rows-quick-pause-resume.js new file mode 100644 index 0000000..48a80b2 --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-streaming-rows-quick-pause-resume.js @@ -0,0 +1,46 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +common.useTestDb(connection); + +var table = 'stream_test'; +connection.query([ + 'CREATE TEMPORARY TABLE `' + table + '` (', + '`id` int(11) unsigned NOT NULL AUTO_INCREMENT,', + '`title` varchar(255),', + 'PRIMARY KEY (`id`)', + ') ENGINE=InnoDB DEFAULT CHARSET=utf8' +].join('\n')); + +var rowCount = 10; +for (var i = 1; i <= rowCount; i++) { + var row = { + id: i, + title: 'Row #' + i, + }; + + connection.query('INSERT INTO ' + table + ' SET ?', row); +} + +var query = connection.query('SELECT * FROM ' + table); + +var hadEnd = false; +var rows = []; +var fields = undefined; +query.on('result', function(row) { + connection.pause(); + connection.resume(); + + rows.push(row); +}) +.on('end', function() { + hadEnd = true; +}); + +connection.end(); + +process.on('exit', function() { + assert.equal(rows.length, rowCount); + assert.equal(hadEnd, true); +}); diff --git a/node_modules/mysql/test/integration/connection/test-streaming-rows.js b/node_modules/mysql/test/integration/connection/test-streaming-rows.js new file mode 100644 index 0000000..8ea45da --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-streaming-rows.js @@ -0,0 +1,65 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +common.useTestDb(connection); + +var table = 'stream_test'; +connection.query([ + 'CREATE TEMPORARY TABLE `' + table + '` (', + '`id` int(11) unsigned NOT NULL AUTO_INCREMENT,', + '`title` varchar(255),', + 'PRIMARY KEY (`id`)', + ') ENGINE=InnoDB DEFAULT CHARSET=utf8' +].join('\n')); + +var rowCount = 10; +for (var i = 1; i <= rowCount; i++) { + var row = { + id: i, + title: 'Row #' + i, + }; + + connection.query('INSERT INTO ' + table + ' SET ?', row); +} + +var paused = false; +var query = connection.query('SELECT * FROM ' + table); + +var hadEnd = 0; +var rows = []; +var fields = undefined; +query + .on('fields', function(_fields, index) { + assert.equal(index, 0); + assert.ok(!fields); + + fields = _fields; + }) + .on('result', function(row) { + // Make sure we never receive a row while being paused + assert.equal(paused, false); + + paused = true; + connection.pause(); + + setTimeout(function() { + paused = false; + connection.resume(); + + rows.push(row); + }, 10); + }) + .on('end', function() { + hadEnd = true; + }); + +connection.end(); + +process.on('exit', function() { + assert.equal(rows.length, 10); + assert.equal(hadEnd, true); + + assert.equal(fields[0].name, 'id'); + assert.equal(fields[1].name, 'title'); +}); diff --git a/node_modules/mysql/test/integration/connection/test-timezones.js b/node_modules/mysql/test/integration/connection/test-timezones.js new file mode 100644 index 0000000..dd59de4 --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-timezones.js @@ -0,0 +1,67 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +common.useTestDb(connection); + +var table = 'timezone_test'; +connection.query([ + 'CREATE TEMPORARY TABLE `' + table + '`(', + '`offset` varchar(10),', + '`dt` datetime', + ') ENGINE=InnoDB DEFAULT CHARSET=utf8' +].join('\n')); + +var tests = [ 0, 1, 5, 12, 26, -1, -5, -20, 'Z', 'local' ]; + +connection.query('DELETE FROM ' + table); + +testNextDate(); + +function testNextDate() { + if (tests.length === 0) { + return connection.end(); + } + + var test = tests.pop(); + + testDate(test, function () { + testNextDate(); + }); +} + +function testDate(offset, cb) { + var dt = new Date(); + + if (offset == 'Z' || offset == 'local') { + connection.config.timezone = offset; + } else { + connection.config.timezone = (offset < 0 ? "-" : "+") + pad2(Math.abs(offset)) + ":00"; + } + connection.query('INSERT INTO ' + table + ' SET ?', { offset: offset, dt: dt }); + + if (offset == 'Z') { + dt.setTime(dt.getTime() + (dt.getTimezoneOffset() * 60000)); + } else if (offset != 'local') { + dt.setTime(dt.getTime() + (dt.getTimezoneOffset() * 60000) + (offset * 3600000)); + } + + connection.query({ + sql: 'SELECT * FROM ' + table + ' WHERE offset = \'' + offset + '\'', + typeCast: function (field, next) { + if (field.type != 'DATETIME') return next(); + + return new Date(field.string()); + } + }, function (err, result) { + if (err) throw err; + + assert.strictEqual(dt.toString(), result[0].dt.toString()); + + return cb(); + }); +} + +function pad2(v) { + return (v < 10 ? "0" : "") + v; +} diff --git a/node_modules/mysql/test/integration/connection/test-transaction-commit.js b/node_modules/mysql/test/integration/connection/test-transaction-commit.js new file mode 100644 index 0000000..86015a6 --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-transaction-commit.js @@ -0,0 +1,41 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +common.useTestDb(connection); + +var table = 'transaction_test'; +connection.query([ + 'CREATE TEMPORARY TABLE `' + table + '` (', + '`id` int(11) unsigned NOT NULL AUTO_INCREMENT,', + '`title` varchar(255),', + 'PRIMARY KEY (`id`)', + ') ENGINE=InnoDB DEFAULT CHARSET=utf8' +].join('\n')); + +connection.query('START TRANSACTION'); + +var rowCount = 10; +for (var i = 1; i <= rowCount; i++) { + var row = { + id: i, + title: 'Row #' + i, + }; + + connection.query('INSERT INTO ' + table + ' SET ?', row); +} + +connection.query('COMMIT'); + +var rows = []; +var query = connection.query('SELECT * FROM ' + table, function(err, _rows) { + if (err) throw err; + + rows = _rows; +}); + +connection.end(); + +process.on('exit', function() { + assert.equal(rows.length, rowCount); +}); diff --git a/node_modules/mysql/test/integration/connection/test-transaction-rollback.js b/node_modules/mysql/test/integration/connection/test-transaction-rollback.js new file mode 100644 index 0000000..f06bee5 --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-transaction-rollback.js @@ -0,0 +1,41 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +common.useTestDb(connection); + +var table = 'transaction_test'; +connection.query([ + 'CREATE TEMPORARY TABLE `' + table + '` (', + '`id` int(11) unsigned NOT NULL AUTO_INCREMENT,', + '`title` varchar(255),', + 'PRIMARY KEY (`id`)', + ') ENGINE=InnoDB DEFAULT CHARSET=utf8' +].join('\n')); + +connection.query('START TRANSACTION'); + +var rowCount = 10; +for (var i = 1; i <= rowCount; i++) { + var row = { + id: i, + title: 'Row #' + i, + }; + + connection.query('INSERT INTO ' + table + ' SET ?', row); +} + +connection.query('ROLLBACK'); + +var rows; +var query = connection.query('SELECT * FROM ' + table, function(err, _rows) { + if (err) throw err; + + rows = _rows; +}); + +connection.end(); + +process.on('exit', function() { + assert.equal(rows.length, 0); +}); diff --git a/node_modules/mysql/test/integration/connection/test-type-cast-null-fields.js b/node_modules/mysql/test/integration/connection/test-type-cast-null-fields.js new file mode 100644 index 0000000..f5718f4 --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-type-cast-null-fields.js @@ -0,0 +1,34 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +common.useTestDb(connection); + +var table = 'insert_test'; +connection.query([ + 'CREATE TEMPORARY TABLE `' + table + '` (', + '`id` int(11) unsigned NOT NULL AUTO_INCREMENT,', + '`date` DATETIME NULL,', + '`number` INT NULL,', + 'PRIMARY KEY (`id`)', + ') ENGINE=InnoDB DEFAULT CHARSET=utf8' +].join('\n')); + +connection.query('INSERT INTO ' + table + ' SET ?', { + date : null, + number : null, +}); + +var results; +connection.query('SELECT * FROM ' + table, function(err, _results) { + if (err) throw err; + + results = _results; +}); + +connection.end(); + +process.on('exit', function() { + assert.strictEqual(results[0].date, null); + assert.strictEqual(results[0].number, null); +}); diff --git a/node_modules/mysql/test/integration/connection/test-type-cast-query.js b/node_modules/mysql/test/integration/connection/test-type-cast-query.js new file mode 100644 index 0000000..0a6dda5 --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-type-cast-query.js @@ -0,0 +1,37 @@ +var common = require('../../common'); +var connection = common.createConnection({typeCast: true}); +var assert = require('assert'); +var util = require('util'); + +connection.connect(); + +var options = { + sql : "SELECT NOW() as date, POINT(1.2,-3.4) as point", + typeCast : false +}; + +var rows; +var query = connection.query(options, function(err, _rows) { + if (err) throw err; + + rows = _rows; +}); + +connection.end(); + +process.on('exit', function() { + var point = rows[0].point; + var byteOrder = point.readUInt8(4); + var wkbType = byteOrder? point.readUInt32LE(5) : point.readUInt32BE(5); + var x = byteOrder? point.readDoubleLE(9) : point.readDoubleBE(9); + var y = byteOrder? point.readDoubleLE(17) : point.readDoubleBE(17); + + assert.strictEqual(typeof rows[0].date, 'object'); + assert.equal(Buffer.isBuffer(rows[0].date), true); + assert.strictEqual(typeof point, 'object'); + assert.equal(Buffer.isBuffer(point), true); + assert.equal(point.readUInt32LE(0), 0); // unknown + assert.equal(wkbType, 1); // WKBPoint + assert.equal(x, 1.2); + assert.equal(y, -3.4); +}); diff --git a/node_modules/mysql/test/integration/connection/test-type-casting.js b/node_modules/mysql/test/integration/connection/test-type-casting.js new file mode 100644 index 0000000..781ce6f --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-type-casting.js @@ -0,0 +1,118 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +common.useTestDb(connection); + +var tests = [ + {type: 'decimal(3,3)', insert: '0.330', expect: 0.33 }, + {type: 'decimal(3,3)', insert: 0.33}, + {type: 'tinyint', insert: 1}, + {type: 'smallint', insert: 2}, + {type: 'int', insert: 3}, + {type: 'float', insert: 4.5}, + {type: 'double', insert: 5.5}, + {type: 'bigint', insert: '6', expect: 6}, + {type: 'bigint', insert: 6}, + {type: 'mediumint', insert: 7}, + {type: 'year', insert: 2012}, + {type: 'timestamp', insert: new Date('2012-05-12 11:00:23')}, + {type: 'datetime', insert: new Date('2012-05-12 12:00:23')}, + {type: 'date', insert: new Date('2012-05-12 00:00:00 Z')}, + {type: 'time', insert: '13:13:23'}, + {type: 'binary(4)', insert: new Buffer([0, 1, 254, 255])}, + {type: 'varbinary(4)', insert: new Buffer([0, 1, 254, 255])}, + {type: 'tinyblob', insert: new Buffer([0, 1, 254, 255])}, + {type: 'mediumblob', insert: new Buffer([0, 1, 254, 255])}, + {type: 'longblob', insert: new Buffer([0, 1, 254, 255])}, + {type: 'blob', insert: new Buffer([0, 1, 254, 255])}, + {type: 'bit(32)', insert: new Buffer([0, 1, 254, 255])}, + {type: 'char(5)', insert: 'Hello'}, + {type: 'varchar(5)', insert: 'Hello'}, + {type: 'varchar(3) character set utf8 collate utf8_bin', insert: 'bin'}, + {type: 'tinytext', insert: 'Hello World'}, + {type: 'mediumtext', insert: 'Hello World'}, + {type: 'longtext', insert: 'Hello World'}, + {type: 'text', insert: 'Hello World'}, + {type: 'point', insertRaw: 'POINT(1.2,-3.4)', expect: {x:1.2, y:-3.4}, deep: true}, + {type: 'point', insertRaw: (function() { + var buffer = new Buffer(21); + buffer.writeUInt8(1, 0); + buffer.writeUInt32LE(1, 1); + buffer.writeDoubleLE(-5.6, 5); + buffer.writeDoubleLE(10.23, 13); + return 'GeomFromWKB(' + connection.escape(buffer) + ')'; + })(), expect: {x:-5.6, y:10.23}, deep: true}, + {type: 'linestring', insertRaw: 'LINESTRING(POINT(1.2,-3.4),POINT(-5.6,10.23),POINT(0.2,0.7))', expect: [{x:1.2, y:-3.4}, {x:-5.6, y:10.23}, {x:0.2, y:0.7}], deep: true}, + {type: 'polygon', insertRaw: "GeomFromText('POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7, 5 5))')", expect: [[{x:0,y:0},{x:10,y:0},{x:10,y:10},{x:0,y:10},{x:0,y:0}],[{x:5,y:5},{x:7,y:5},{x:7,y:7},{x:5,y:7},{x:5,y:5}]], deep: true}, + {type: 'geometry', insertRaw: 'POINT(1.2,-3.4)', expect: {x:1.2, y:-3.4}, deep: true}, + {type: 'multipoint', insertRaw: "GeomFromText('MULTIPOINT(0 0, 20 20, 60 60)')", expect: [{x:0, y:0}, {x:20, y:20}, {x:60, y:60}], deep: true}, + {type: 'multilinestring', insertRaw: "GeomFromText('MULTILINESTRING((10 10, 20 20), (15 15, 30 15))')", expect: [[{x:10,y:10},{x:20,y:20}],[{x:15,y:15},{x:30,y:15}]], deep: true}, + {type: 'multipolygon', insertRaw: "GeomFromText('MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0)),((5 5,7 5,7 7,5 7, 5 5)))')", expect: [[[{x:0,y:0},{x:10,y:0},{x:10,y:10},{x:0,y:10},{x:0,y:0}]],[[{x:5,y:5},{x:7,y:5},{x:7,y:7},{x:5,y:7},{x:5,y:5}]]], deep: true}, + {type: 'geometrycollection', insertRaw: "GeomFromText('GEOMETRYCOLLECTION(POINT(10 10), POINT(30 30), LINESTRING(15 15, 20 20))')", expect: [{x:10,y:10},{x:30,y:30},[{x:15,y:15},{x:20,y:20}]], deep: true} +]; + +var table = 'type_casting'; + +var schema = []; +var inserts = []; + +tests.forEach(function(test, index) { + var escaped = test.insertRaw || connection.escape(test.insert); + + test.columnName = test.type + '_' + index; + + schema.push('`' + test.columnName + '` ' + test.type + ','); + inserts.push('`' + test.columnName + '` = ' + escaped); +}); + +var createTable = [ + 'CREATE TEMPORARY TABLE `' + table + '` (', + '`id` int(11) unsigned NOT NULL AUTO_INCREMENT,' + ].concat(schema).concat([ + 'PRIMARY KEY (`id`)', + ') ENGINE=InnoDB DEFAULT CHARSET=utf8' +]).join('\n'); + +connection.query(createTable); + +connection.query('INSERT INTO ' + table + ' SET' + inserts.join(',\n')); + +var row; +connection.query('SELECT * FROM type_casting', function(err, rows) { + if (err) throw err; + + row = rows[0]; +}); + +connection.end(); + +process.on('exit', function() { + tests.forEach(function(test) { + var expected = test.expect || test.insert; + var got = row[test.columnName]; + var message; + + if (expected instanceof Date) { + assert.equal(got instanceof Date, true, test.type); + + expected = String(expected); + got = String(got); + } else if (Buffer.isBuffer(expected)) { + assert.equal(Buffer.isBuffer(got), true, test.type); + + expected = String(Array.prototype.slice.call(expected)); + got = String(Array.prototype.slice.call(got)); + } + + if (test.deep) { + message = 'got: "' + JSON.stringify(got) + '" expected: "' + JSON.stringify(expected) + + '" test: ' + test.type + ''; + assert.deepEqual(expected, got, message); + } else { + message = 'got: "' + got + '" (' + (typeof got) + ') expected: "' + expected + + '" (' + (typeof expected) + ') test: ' + test.type + ''; + assert.strictEqual(expected, got, message); + } + }); +}); diff --git a/node_modules/mysql/test/integration/connection/test-unix-domain-socket.js b/node_modules/mysql/test/integration/connection/test-unix-domain-socket.js new file mode 100644 index 0000000..229395e --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-unix-domain-socket.js @@ -0,0 +1,32 @@ +var common = require('../../common'); +var connection = common.createConnection({socketPath: common.fakeServerSocket}); +var assert = require('assert'); + +var server = common.createFakeServer(); +var didConnect = false; +server.listen(common.fakeServerSocket, function(err) { + if (err) throw err; + + connection.connect(function(err) { + if (err) throw err; + + assert.equal(didConnect, false); + didConnect = true; + + connection.destroy(); + server.destroy(); + }); +}); + +var hadConnection = false; +server.on('connection', function(connection) { + connection.handshake(); + + assert.equal(hadConnection, false); + hadConnection = true; +}); + +process.on('exit', function() { + assert.equal(didConnect, true); + assert.equal(hadConnection, true); +}); diff --git a/node_modules/mysql/test/integration/connection/test-zerofill-results.js b/node_modules/mysql/test/integration/connection/test-zerofill-results.js new file mode 100644 index 0000000..49176da --- /dev/null +++ b/node_modules/mysql/test/integration/connection/test-zerofill-results.js @@ -0,0 +1,39 @@ +var common = require('../../common'); +var connection = common.createConnection(); +var assert = require('assert'); + +common.useTestDb(connection); + +var table = 'zerofill_results_test'; +connection.query([ + 'CREATE TEMPORARY TABLE `' + table + '` (', + '`id` int(11) unsigned NOT NULL AUTO_INCREMENT,', + '`num` int(5) UNSIGNED ZEROFILL,', + 'PRIMARY KEY (`id`)', + ') ENGINE=InnoDB DEFAULT CHARSET=utf8' +].join('\n')); + +var test_numbers = [ "00000", "00001", "00012", "00123", "01234", "12345", null ]; +var results = {}; + +for (var i = 0; i < test_numbers.length; i++) { + connection.query('INSERT INTO ' + table + ' SET ?', { + id: (i + 1), + num: (test_numbers[i] !== null ? parseInt(test_numbers[i], 10) : null) + }, function (err, _result) { + if (err) throw err; + }); +} +connection.query('SELECT * FROM ' + table, function (err, _results) { + if (err) throw err; + + results = _results; +}); +connection.end(); + +process.on('exit', function() { + assert.strictEqual(results.length, test_numbers.length); + for (var i = 0; i < results.length; i++) { + assert.strictEqual(test_numbers[results[i].id - 1], (results[i].num !== null ? "" + results[i].num : null)); + } +}); diff --git a/node_modules/mysql/test/run.js b/node_modules/mysql/test/run.js new file mode 100644 index 0000000..a58d5ce --- /dev/null +++ b/node_modules/mysql/test/run.js @@ -0,0 +1,7 @@ +var options = {}; + +if (process.env.FILTER) { + options.include = new RegExp(process.env.FILTER + '.*\\.js$'); +} + +require('urun')(__dirname, options); diff --git a/node_modules/mysql/test/unit/protocol/test-Parser.js b/node_modules/mysql/test/unit/protocol/test-Parser.js new file mode 100644 index 0000000..fef7ae8 --- /dev/null +++ b/node_modules/mysql/test/unit/protocol/test-Parser.js @@ -0,0 +1,125 @@ +var common = require('../../common'); +var test = require('utest'); +var assert = require('assert'); +var Parser = require(common.lib + '/protocol/Parser'); + +function packet(bytes) { + var buffer = new Buffer(bytes); + var parser = new Parser(); + + parser.append(buffer); + + return parser; +} + +test('Parser', { + "parseBuffer: buffer won\'t change after appending another one": function() { + var startBuffer = new Buffer(5); + startBuffer.fill('a'); + + var parser = new Parser(); + parser.append(startBuffer); + + var value = parser.parseBuffer(4); + + assert.equal(value.toString(), 'aaaa'); + + parser.append(new Buffer('b')); + + assert.equal(value.toString(), 'aaaa'); + }, + + 'parseUnsignedNumber: 1 byte': function() { + var value = packet([5]).parseUnsignedNumber(1); + assert.equal(value, 5); + }, + + 'parseUnsignedNumber: 2 bytes': function() { + var value = packet([1, 1]).parseUnsignedNumber(2); + assert.equal(value, 256 + 1); + }, + + 'parseUnsignedNumber: honors offsets': function() { + var parser = packet([1, 2]); + assert.equal(parser.parseUnsignedNumber(1), 1); + assert.equal(parser.parseUnsignedNumber(1), 2); + }, + + 'parseLengthCodedNumber: 1 byte': function() { + var parser = packet([250]); + assert.strictEqual(parser.parseLengthCodedNumber(), 250); + }, + + 'parseLengthCodedNumber: 251 = null': function() { + var parser = packet([251]); + assert.strictEqual(parser.parseLengthCodedNumber(), null); + }, + + 'parseLengthCodedNumber: 252 = 16 bit': function() { + var parser = packet([252, 2, 1]); + var expected = + 2 * Math.pow(256, 0) + + 1 * Math.pow(256, 1); + assert.strictEqual(parser.parseLengthCodedNumber(), expected); + }, + + 'parseLengthCodedNumber: 253 = 24 bit': function() { + var parser = packet([253, 3, 2, 1]); + var expected = + 3 * Math.pow(256, 0) + + 2 * Math.pow(256, 1) + + 1 * Math.pow(256, 2); + + assert.strictEqual(parser.parseLengthCodedNumber(), expected); + }, + + 'parseLengthCodedNumber: 254 = 64 bit': function() { + var parser = packet([254, 8, 7, 6, 5, 4, 3, 2, 0]); + var expected = + 8 * Math.pow(256, 0) + + 7 * Math.pow(256, 1) + + 6 * Math.pow(256, 2) + + 5 * Math.pow(256, 3) + + 4 * Math.pow(256, 4) + + 3 * Math.pow(256, 5) + + 2 * Math.pow(256, 6) + + 0 * Math.pow(256, 7); + + assert.strictEqual(parser.parseLengthCodedNumber(), expected); + }, + + 'parseLengthCodedNumber: < 53 bit = no problemo': function() { + var parser = packet([254, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00]); + assert.strictEqual(parser.parseLengthCodedNumber(), Math.pow(2, 53) - 1); + }, + + 'parseLengthCodedNumber: 53 bit = Error': function() { + var parser = packet([254, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00]); + assert.throws(function() { + parser.parseLengthCodedNumber(); + }, /precision/i); + }, + + 'parseLengthCodedNumber: 255 = Error': function() { + var parser = packet([255]); + assert.throws(function() { + parser.parseLengthCodedNumber(); + }, /unexpected/i); + }, + + 'parsePacketTerminatedString: regular case': function() { + var parser = packet([0x48, 0x69]); + parser._packetEnd = 2; + + var str = parser.parsePacketTerminatedString(); + assert.equal(str, 'Hi'); + }, + + 'parsePacketTerminatedString: 0x00 terminated': function() { + var parser = packet([0x48, 0x69, 0x00]); + parser._packetEnd = 2; + + var str = parser.parsePacketTerminatedString(); + assert.equal(str, 'Hi'); + }, +}); diff --git a/node_modules/mysql/test/unit/protocol/test-SqlString.js b/node_modules/mysql/test/unit/protocol/test-SqlString.js new file mode 100644 index 0000000..ed156be --- /dev/null +++ b/node_modules/mysql/test/unit/protocol/test-SqlString.js @@ -0,0 +1,132 @@ +var common = require('../../common'); +var test = require('utest'); +var assert = require('assert'); +var SqlString = require(common.lib + '/protocol/SqlString'); + +test('SqlString.escape', { + 'undefined -> NULL': function() { + assert.equal(SqlString.escape(undefined), 'NULL'); + }, + + 'null -> NULL': function() { + assert.equal(SqlString.escape(null), 'NULL'); + }, + + 'booleans convert to strings': function() { + assert.equal(SqlString.escape(false), 'false'); + assert.equal(SqlString.escape(true), 'true'); + }, + + 'numbers convert to strings': function() { + assert.equal(SqlString.escape(5), '5'); + }, + + 'objects are turned into key value pairs': function() { + assert.equal(SqlString.escape({a: 'b', c: 'd'}), "`a` = 'b', `c` = 'd'"); + }, + + 'objects function properties are ignored': function() { + assert.equal(SqlString.escape({a: 'b', c: function() {}}), "`a` = 'b'"); + }, + + 'nested objects are cast to strings': function() { + assert.equal(SqlString.escape({a: {nested: true}}), "`a` = '[object Object]'"); + }, + + 'arrays are turned into lists': function() { + assert.equal(SqlString.escape([1, 2, 'c']), "1, 2, 'c'"); + }, + + 'nested arrays are turned into grouped lists': function() { + assert.equal(SqlString.escape([[1,2,3], [4,5,6], ['a', 'b', {nested: true}]]), "(1, 2, 3), (4, 5, 6), ('a', 'b', '[object Object]')"); + }, + + 'nested objects inside arrays are cast to strings': function() { + assert.equal(SqlString.escape([1, {nested: true}, 2]), "1, '[object Object]', 2"); + }, + + 'strings are quoted': function() { + assert.equal(SqlString.escape('Super'), "'Super'"); + }, + + '\0 gets escaped': function() { + assert.equal(SqlString.escape('Sup\0er'), "'Sup\\0er'"); + }, + + '\b gets escaped': function() { + assert.equal(SqlString.escape('Sup\ber'), "'Sup\\ber'"); + }, + + '\n gets escaped': function() { + assert.equal(SqlString.escape('Sup\ner'), "'Sup\\ner'"); + }, + + '\r gets escaped': function() { + assert.equal(SqlString.escape('Sup\rer'), "'Sup\\rer'"); + }, + + '\t gets escaped': function() { + assert.equal(SqlString.escape('Sup\ter'), "'Sup\\ter'"); + }, + + '\\ gets escaped': function() { + assert.equal(SqlString.escape('Sup\\er'), "'Sup\\\\er'"); + }, + + '\u001a (ascii 26) gets replaced with \\Z': function() { + assert.equal(SqlString.escape('Sup\u001aer'), "'Sup\\Zer'"); + }, + + 'single quotes get escaped': function() { + assert.equal(SqlString.escape('Sup\'er'), "'Sup\\'er'"); + }, + + 'double quotes get escaped': function() { + assert.equal(SqlString.escape('Sup"er'), "'Sup\\\"er'"); + }, + + 'dates are converted to YYYY-MM-DD HH:II:SS': function() { + var expected = '2012-05-07 11:42:03'; + var date = new Date(Date.UTC(2012, 4, 7, 11, 42, 3)); + var string = SqlString.escape(date); + + assert.strictEqual(string, "'" + expected + "'"); + }, + + 'buffers are converted to hex': function() { + var buffer = new Buffer([0, 1, 254, 255]); + var string = SqlString.escape(buffer); + + assert.strictEqual(string, "X'0001feff'"); + }, + + 'NaN -> NaN': function() { + assert.equal(SqlString.escape(NaN), 'NaN'); + }, + + 'Infinity -> Infinity': function() { + assert.equal(SqlString.escape(Infinity), 'Infinity'); + } +}); + +test('SqlString.format', { + 'question marks are replaced with escaped array values': function() { + var sql = SqlString.format('? and ?', ['a', 'b']); + assert.equal(sql, "'a' and 'b'"); + }, + + 'extra question marks are left untouched': function() { + var sql = SqlString.format('? and ?', ['a']); + assert.equal(sql, "'a' and ?"); + }, + + 'extra arguments are not used': function() { + var sql = SqlString.format('? and ?', ['a', 'b', 'c']); + assert.equal(sql, "'a' and 'b'"); + }, + + 'question marks within values do not cause issues': function() { + var sql = SqlString.format('? and ?', ['hello?', 'b']); + assert.equal(sql, "'hello?' and 'b'"); + }, +}); diff --git a/node_modules/mysql/test/unit/test-ConnectionConfig.js b/node_modules/mysql/test/unit/test-ConnectionConfig.js new file mode 100644 index 0000000..6102246 --- /dev/null +++ b/node_modules/mysql/test/unit/test-ConnectionConfig.js @@ -0,0 +1,29 @@ +var common = require('../common'); +var test = require('utest'); +var assert = require('assert'); +var Charsets = require(common.lib + '/protocol/constants/charsets'); +var ConnectionConfig = require(common.lib + '/ConnectionConfig'); + +test('ConnectionConfig#Constructor', { + 'takes user,pw,host,port,db from url string': function() { + var url = 'mysql://myuser:mypass@myhost:3333/mydb'; + var config = new ConnectionConfig(url); + + assert.equal(config.host, 'myhost'); + assert.equal(config.port, 3333); + assert.equal(config.user, 'myuser'); + assert.equal(config.password, 'mypass'); + assert.equal(config.database, 'mydb'); + }, + + 'allows additional options via url query': function() { + var url = 'mysql://myhost/mydb?debug=true&charset=BIG5_CHINESE_CI'; + var config = new ConnectionConfig(url); + + assert.equal(config.host, 'myhost'); + assert.equal(config.port, 3306); + assert.equal(config.database, 'mydb'); + assert.equal(config.debug, true); + assert.equal(config.charsetNumber, Charsets.BIG5_CHINESE_CI); + }, +}); diff --git a/node_modules/mysql/tool/generate-error-constants.js b/node_modules/mysql/tool/generate-error-constants.js new file mode 100644 index 0000000..b781134 --- /dev/null +++ b/node_modules/mysql/tool/generate-error-constants.js @@ -0,0 +1,28 @@ +#!/usr/bin/env node +var path = require('path'); +var script = path.basename(__filename); + +var errorFile = process.argv[2]; +if (!errorFile) { + console.error('Usage: ./' + script + ' path/to/errmsg-utf8.txt'); + process.exit(1); +} + +var fs = require('fs'); +var errors = fs.readFileSync(errorFile, 'utf-8'); + +var number = Number(errors.match(/start-error-number (\d+)/)[1]); +var codes = errors.match(/^([A-Z_]+)/mg) + +var source = + '// Generated by ' + script + ', do not modify by hand\n' + + codes + .map(function(code) { + return 'exports[' + (number++) + '] = \'' + code + '\';'; + }) + .join('\n'); + +var targetFile = path.join(__dirname, '../lib/protocol/constants/errors.js'); +fs.writeFileSync(targetFile, source, 'utf-8'); + +console.log('Wrote constants to ' + targetFile);