diff --git a/main.js b/main.js index 3cb27b4..01e5166 100644 --- a/main.js +++ b/main.js @@ -4,6 +4,8 @@ const random_bytes = require("random-bytes"); const bsplit = require("buffer-split"); const node_rsa = require("node-rsa"); +const btoa = require("btoa"); +const atob = require("atob"); const net = require("net"); const fs = require("fs"); @@ -12,10 +14,121 @@ var settings = require("./settings.json"); console.log("Ready."); + +function toBuffer(bytes) +{ + // String + if(typeof(bytes) == 'string') + { + // Create a buffer + var buffer = new Buffer.alloc(bytes.length); + + // Loop over the bytes + for(var i=0;i= 4) + { + // Get the number + recieve_size = recieve_buffer.readUInt32BE(0); + + // Reset the buffer + recieve_buffer = new Buffer.alloc(0); + + // Set the get data mode to true + recieve_get = true; + } + } + + else + { + // Is the recieve buffer as big as the size + if(recieve_buffer.length == recieve_size) + { + // Call the callback + callback(recieve_buffer); + + // Reset the buffer and the size + recieve_buffer = new Buffer.alloc(0); + recieve_size = 0; + + // Set the get data mode to false + recieve_get = false; + } + } + } +} + +function send_ordered(data) +{ + // Convert the data into a buffer + data = toBuffer(data); + + // Get the size of the data + var size = data.length; + + // Turn it into a 32 bit buffer + var bytes = new Buffer.alloc(4); + bytes.writeUInt32BE(size, 0); + + // Return the buffer and the data + return new Buffer.concat([bytes, data]); +} + function string_decrypt(key, string) { // Convert the string to a buffer - var buff = new Buffer.from(string); + var buff = toBuffer(string); // Create some new data var decrypted = new Buffer.alloc(buff.length); @@ -46,7 +159,7 @@ function string_decrypt(key, string) function string_encrypt(key, string) { // Convert the string to a buffer - var buff = new Buffer.from(string); + var buff = toBuffer(string); // Create some new data var encrypted = new Buffer.alloc(buff.length); @@ -92,20 +205,7 @@ function make_encryption_key(string) function socket_write(socket, data) { // Send the data encrypted with JSON - socket.sock.write(string_encrypt(socket.key, JSON.stringify(data)+"\n")); -} - -function socket_ondata(socket, callback) -{ - // Wait for a response - socket.on('data', function(data) - { - // Decode the data - data = JSON.parse(data); - - // Call the callback - callback(data); - }); + socket.sock.write(send_ordered(string_encrypt(socket.key, JSON.stringify(data)+"\n"))); } function socket_init(socket, callback, ondata) @@ -120,63 +220,81 @@ function socket_init(socket, callback, ondata) // Wait for data sock.sock.on('data', function(data) { - // Is the socket new - if(sock.new) + // Recieve data in order + recieve_ordered(data, function(data) { - // Set sock new - sock.new = false; + console.log(data); - // Load the key - var key = new node_rsa(); - key.importKey(data.toString(), 'public'); - console.log("Loaded the RSA key"); - - // Get some random bytes - random_bytes(settings.encryption_key_size, function(error,string) + // Is the socket new + if(sock.new) { - // Throw an error if there is one - if(error) throw error; + // Set sock new + sock.new = false; - // Make the key - sock.key = make_encryption_key(string); - console.log("Created an encryption key") + // Convert from JSON + data = JSON.parse(data); - // Encrypt the key with RSA - var key_encrypted = key.encrypt(sock.key.str, 'base64'); - console.log("Encrypted the key"); + // Is this the public key + if(data.mode == 'pubkey') + { + // Load the key + var key = new node_rsa(); + key.importKey(data.key, 'public'); + console.log("Loaded the RSA key", data); - // Send the size of the key to the client - sock.sock.write(key_encrypted.length.toString()+"\n"); - console.log("Sent the size of the key:", key_encrypted.length) + // Get some random bytes + random_bytes(settings.encryption_key_size, function(error,string) + { + console.log("btoa(string):",btoa(string)); - // Send the key to the client - sock.sock.write(key_encrypted.toString()+"\n"); - console.log("Sent the key to the client"); + // Throw an error if there is one + if(error) throw error; - // Send the done signal - //sock.sock.write("done"+"\n"); + // Make the key + sock.key = make_encryption_key(string); + console.log("Created an encryption key") - // Call the callback - setTimeout(callback, 1000, sock); - }); - } + // Encrypt the key with RSA + var key_encrypted = key.encrypt(btoa(sock.key.str), 'base64'); + console.log("Encrypted the key"); - else - { - // Decrypt the data - data = string_decrypt(sock.key, data); + // Send the key to the client + sock.sock.write(send_ordered(JSON.stringify({ + key: toBytes(key_encrypted), + mode: "encryption_key" + }))); - // Split the data by newlines - data = bsplit(data, Buffer("\n")); - - // Iterate over them - for(var i=0;iascii data back **to** binary. + +```javascript +(function () { + "use strict"; + + var atob = require('atob'); + var b64 = "SGVsbG8sIFdvcmxkIQ=="; + var bin = atob(b64); + + console.log(bin); // "Hello, World!" +}()); +``` + +### Need Unicode and Binary Support in the Browser? + +Check out [unibabel.js](https://git.coolaj86.com/coolaj86/unibabel.js) + +Changelog +======= + + * v2.1.0 address a few issues and PRs, update URLs + * v2.0.0 provide browser version for ios web workers + * v1.2.0 provide (empty) browser version + * v1.1.3 add MIT license + * v1.1.2 node only + +LICENSE +======= + +Code copyright 2012-2018 AJ ONeal + +Dual-licensed MIT and Apache-2.0 + +Docs copyright 2012-2018 AJ ONeal + +Docs released under [Creative Commons](https://git.coolaj86.com/coolaj86/atob.js/blob/master/LICENSE.DOCS). diff --git a/node_modules/atob/bin/atob.js b/node_modules/atob/bin/atob.js new file mode 100644 index 0000000..a56ac2e --- /dev/null +++ b/node_modules/atob/bin/atob.js @@ -0,0 +1,6 @@ +#!/usr/bin/env node +'use strict'; + +var atob = require('../node-atob'); +var str = process.argv[2]; +console.log(atob(str)); diff --git a/node_modules/atob/bower.json b/node_modules/atob/bower.json new file mode 100644 index 0000000..e3ef66e --- /dev/null +++ b/node_modules/atob/bower.json @@ -0,0 +1,24 @@ +{ + "name": "atob", + "description": "atob for isomorphic environments", + "main": "browser-atob.js", + "authors": [ + "AJ ONeal (https://coolaj86.com)" + ], + "license": "(MIT OR Apache-2.0)", + "keywords": [ + "atob", + "browser" + ], + "homepage": "https://github.com/node-browser-compat/atob", + "moduleType": [ + "globals" + ], + "ignore": [ + "**/.*", + "node_modules", + "bower_components", + "test", + "tests" + ] +} diff --git a/node_modules/atob/browser-atob.js b/node_modules/atob/browser-atob.js new file mode 100644 index 0000000..af4f357 --- /dev/null +++ b/node_modules/atob/browser-atob.js @@ -0,0 +1,44 @@ +(function (w) { + "use strict"; + + function findBest(atobNative) { + // normal window + if ('function' === typeof atobNative) { return atobNative; } + + + // browserify (web worker) + if ('function' === typeof Buffer) { + return function atobBrowserify(a) { + //!! Deliberately using an API that's deprecated in node.js because + //!! this file is for browsers and we expect them to cope with it. + //!! Discussion: github.com/node-browser-compat/atob/pull/9 + return new Buffer(a, 'base64').toString('binary'); + }; + } + + // ios web worker with base64js + if ('object' === typeof w.base64js) { + // bufferToBinaryString + // https://git.coolaj86.com/coolaj86/unibabel.js/blob/master/index.js#L50 + return function atobWebWorker_iOS(a) { + var buf = w.base64js.b64ToByteArray(a); + return Array.prototype.map.call(buf, function (ch) { + return String.fromCharCode(ch); + }).join(''); + }; + } + + return function () { + // ios web worker without base64js + throw new Error("You're probably in an old browser or an iOS webworker." + + " It might help to include beatgammit's base64-js."); + }; + } + + var atobBest = findBest(w.atob); + w.atob = atobBest; + + if ((typeof module === 'object') && module && module.exports) { + module.exports = atobBest; + } +}(window)); diff --git a/node_modules/atob/node-atob.js b/node_modules/atob/node-atob.js new file mode 100644 index 0000000..d7305a3 --- /dev/null +++ b/node_modules/atob/node-atob.js @@ -0,0 +1,7 @@ +"use strict"; + +function atob(str) { + return Buffer.from(str, 'base64').toString('binary'); +} + +module.exports = atob.atob = atob; diff --git a/node_modules/atob/package.json b/node_modules/atob/package.json new file mode 100644 index 0000000..0465052 --- /dev/null +++ b/node_modules/atob/package.json @@ -0,0 +1,86 @@ +{ + "_args": [ + [ + "atob", + "/mnt/c/Users/Josua/Desktop/data/nodejs/electron/chat-project/chat-server" + ] + ], + "_from": "atob@latest", + "_id": "atob@2.1.2", + "_inCache": true, + "_installable": true, + "_location": "/atob", + "_nodeVersion": "10.6.0", + "_npmOperationalInternal": { + "host": "s3://npm-registry-packages", + "tmp": "tmp/atob_2.1.2_1534587539650_0.5963110548313084" + }, + "_npmUser": { + "email": "coolaj86@gmail.com", + "name": "coolaj86" + }, + "_npmVersion": "6.1.0", + "_phantomChildren": {}, + "_requested": { + "name": "atob", + "raw": "atob", + "rawSpec": "", + "scope": null, + "spec": "latest", + "type": "tag" + }, + "_requiredBy": [ + "#USER" + ], + "_resolved": "https://registry.npmjs.org/atob/-/atob-2.1.2.tgz", + "_shasum": "6d9517eb9e030d2436666651e86bd9f6f13533c9", + "_shrinkwrap": null, + "_spec": "atob", + "_where": "/mnt/c/Users/Josua/Desktop/data/nodejs/electron/chat-project/chat-server", + "author": { + "email": "coolaj86@gmail.com", + "name": "AJ ONeal", + "url": "https://coolaj86.com" + }, + "bin": { + "atob": "bin/atob.js" + }, + "browser": "browser-atob.js", + "dependencies": {}, + "description": "atob for Node.JS and Linux / Mac / Windows CLI (it's a one-liner)", + "devDependencies": {}, + "directories": {}, + "dist": { + "fileCount": 9, + "integrity": "sha512-Wm6ukoaOGJi/73p/cl2GvLjTI5JM1k/O14isD73YML8StrH/7/lRFgmg8nICZgD3bZZvjwCGxtMOD3wWNAu8cg==", + "npm-signature": "-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJbd/KUCRA9TVsSAnZWagAAWT8P/3+yl8XFjF0rp1EFhKXr\nwiBksq0aT2A9GHF0nWtytC3MAmn6r9qmY1lmHRoDgwUxQDdrxkYhvljvEFlV\nkzzCnbMOQnTtJubX2lTHG1OLQeUz4DoE7jLEEtnA9UCj5gAZo9eAI+9F4v97\nfRWKlFIJS+miXsLODPww7PAgJKMYBaOkEs6hLW8CIrssIlrdjHAT26ZTYZhY\nFCOvmpT2Mg4BbCE61k8arLNPCqGq7Joh4oJ7BOdosXWnd0snfK4JsXzPFMu8\nLEasrWWAcTJP23k9tDkCn4wtGzt2ZSqRwHXpT7/jEL3GnycctMtefqJdZ6qb\nmRV7/KUVMhGAUnhm5sb0v50NrYx5vO7CGYQqv6IYyNRVbuq3zKjESneyjHzk\nN4Up8SW5UQ8eN+WBRoNfQiOheAk9bdEc02RguuNURng/Pjd9S5xILvg42nfa\np154xFSlNfJASYmH0uDWAzqW+7XZDLxGZOxEuxC52B/aRB3+oZIH/dZOwEw4\nwcQfIzK6421MzIrmBxgU8rL7WCjov4d1spI91jS6bhV6OGsij564gKqbufJq\nHbQUnQeV4PAfU1tLOkUlLxdatt6JL/ekaz0MIE9m35pRf+FRUIbMacJkdHd8\nCftoLutPQ4NR6Ce36ylhZOgoW9tcKUsaTnrwq8M12Y1TAqzHSYKPaVnqMSXN\ngBBD\r\n=TkXF\r\n-----END PGP SIGNATURE-----\r\n", + "shasum": "6d9517eb9e030d2436666651e86bd9f6f13533c9", + "tarball": "https://registry.npmjs.org/atob/-/atob-2.1.2.tgz", + "unpackedSize": 36200 + }, + "engines": { + "node": ">= 4.5.0" + }, + "gitHead": "755cfea7899074a17e83a248c7cfc3960d956d82", + "homepage": "https://git.coolaj86.com/coolaj86/atob.js.git", + "keywords": [ + "atob", + "browser" + ], + "license": "(MIT OR Apache-2.0)", + "main": "node-atob.js", + "maintainers": [ + { + "name": "coolaj86", + "email": "coolaj86@gmail.com" + } + ], + "name": "atob", + "optionalDependencies": {}, + "readme": "ERROR: No README data found!", + "repository": { + "type": "git", + "url": "git://git.coolaj86.com/coolaj86/atob.js.git" + }, + "version": "2.1.2" +} diff --git a/node_modules/atob/test.js b/node_modules/atob/test.js new file mode 100644 index 0000000..bd80a4e --- /dev/null +++ b/node_modules/atob/test.js @@ -0,0 +1,18 @@ +(function () { + "use strict"; + + var atob = require('.'); + var encoded = "SGVsbG8sIFdvcmxkIQ==" + var unencoded = "Hello, World!"; + /* + , encoded = "SGVsbG8sIBZM" + , unencoded = "Hello, 世界" + */ + + if (unencoded !== atob(encoded)) { + console.log('[FAIL]', unencoded, atob(encoded)); + return; + } + + console.log('[PASS] all tests pass'); +}()); diff --git a/node_modules/btoa/LICENSE b/node_modules/btoa/LICENSE new file mode 100644 index 0000000..37ec93a --- /dev/null +++ b/node_modules/btoa/LICENSE @@ -0,0 +1,191 @@ +Apache License +Version 2.0, January 2004 +http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + +"License" shall mean the terms and conditions for use, reproduction, and +distribution as defined by Sections 1 through 9 of this document. + +"Licensor" shall mean the copyright owner or entity authorized by the copyright +owner that is granting the License. + +"Legal Entity" shall mean the union of the acting entity and all other entities +that control, are controlled by, or are under common control with that entity. +For the purposes of this definition, "control" means (i) the power, direct or +indirect, to cause the direction or management of such entity, whether by +contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the +outstanding shares, or (iii) beneficial ownership of such entity. + +"You" (or "Your") shall mean an individual or Legal Entity exercising +permissions granted by this License. + +"Source" form shall mean the preferred form for making modifications, including +but not limited to software source code, documentation source, and configuration +files. + +"Object" form shall mean any form resulting from mechanical transformation or +translation of a Source form, including but not limited to compiled object code, +generated documentation, and conversions to other media types. + +"Work" shall mean the work of authorship, whether in Source or Object form, made +available under the License, as indicated by a copyright notice that is included +in or attached to the work (an example is provided in the Appendix below). + +"Derivative Works" shall mean any work, whether in Source or Object form, that +is based on (or derived from) the Work and for which the editorial revisions, +annotations, elaborations, or other modifications represent, as a whole, an +original work of authorship. For the purposes of this License, Derivative Works +shall not include works that remain separable from, or merely link (or bind by +name) to the interfaces of, the Work and Derivative Works thereof. + +"Contribution" shall mean any work of authorship, including the original version +of the Work and any modifications or additions to that Work or Derivative Works +thereof, that is intentionally submitted to Licensor for inclusion in the Work +by the copyright owner or by an individual or Legal Entity authorized to submit +on behalf of the copyright owner. For the purposes of this definition, +"submitted" means any form of electronic, verbal, or written communication sent +to the Licensor or its representatives, including but not limited to +communication on electronic mailing lists, source code control systems, and +issue tracking systems that are managed by, or on behalf of, the Licensor for +the purpose of discussing and improving the Work, but excluding communication +that is conspicuously marked or otherwise designated in writing by the copyright +owner as "Not a Contribution." + +"Contributor" shall mean Licensor and any individual or Legal Entity on behalf +of whom a Contribution has been received by Licensor and subsequently +incorporated within the Work. + +2. Grant of Copyright License. + +Subject to the terms and conditions of this License, each Contributor hereby +grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, +irrevocable copyright license to reproduce, prepare Derivative Works of, +publicly display, publicly perform, sublicense, and distribute the Work and such +Derivative Works in Source or Object form. + +3. Grant of Patent License. + +Subject to the terms and conditions of this License, each Contributor hereby +grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, +irrevocable (except as stated in this section) patent license to make, have +made, use, offer to sell, sell, import, and otherwise transfer the Work, where +such license applies only to those patent claims licensable by such Contributor +that are necessarily infringed by their Contribution(s) alone or by combination +of their Contribution(s) with the Work to which such Contribution(s) was +submitted. If You institute patent litigation against any entity (including a +cross-claim or counterclaim in a lawsuit) alleging that the Work or a +Contribution incorporated within the Work constitutes direct or contributory +patent infringement, then any patent licenses granted to You under this License +for that Work shall terminate as of the date such litigation is filed. + +4. Redistribution. + +You may reproduce and distribute copies of the Work or Derivative Works thereof +in any medium, with or without modifications, and in Source or Object form, +provided that You meet the following conditions: + +You must give any other recipients of the Work or Derivative Works a copy of +this License; and +You must cause any modified files to carry prominent notices stating that You +changed the files; and +You must retain, in the Source form of any Derivative Works that You distribute, +all copyright, patent, trademark, and attribution notices from the Source form +of the Work, excluding those notices that do not pertain to any part of the +Derivative Works; and +If the Work includes a "NOTICE" text file as part of its distribution, then any +Derivative Works that You distribute must include a readable copy of the +attribution notices contained within such NOTICE file, excluding those notices +that do not pertain to any part of the Derivative Works, in at least one of the +following places: within a NOTICE text file distributed as part of the +Derivative Works; within the Source form or documentation, if provided along +with the Derivative Works; or, within a display generated by the Derivative +Works, if and wherever such third-party notices normally appear. The contents of +the NOTICE file are for informational purposes only and do not modify the +License. You may add Your own attribution notices within Derivative Works that +You distribute, alongside or as an addendum to the NOTICE text from the Work, +provided that such additional attribution notices cannot be construed as +modifying the License. +You may add Your own copyright statement to Your modifications and may provide +additional or different license terms and conditions for use, reproduction, or +distribution of Your modifications, or for any such Derivative Works as a whole, +provided Your use, reproduction, and distribution of the Work otherwise complies +with the conditions stated in this License. + +5. Submission of Contributions. + +Unless You explicitly state otherwise, any Contribution intentionally submitted +for inclusion in the Work by You to the Licensor shall be under the terms and +conditions of this License, without any additional terms or conditions. +Notwithstanding the above, nothing herein shall supersede or modify the terms of +any separate license agreement you may have executed with Licensor regarding +such Contributions. + +6. Trademarks. + +This License does not grant permission to use the trade names, trademarks, +service marks, or product names of the Licensor, except as required for +reasonable and customary use in describing the origin of the Work and +reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. + +Unless required by applicable law or agreed to in writing, Licensor provides the +Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, +including, without limitation, any warranties or conditions of TITLE, +NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are +solely responsible for determining the appropriateness of using or +redistributing the Work and assume any risks associated with Your exercise of +permissions under this License. + +8. Limitation of Liability. + +In no event and under no legal theory, whether in tort (including negligence), +contract, or otherwise, unless required by applicable law (such as deliberate +and grossly negligent acts) or agreed to in writing, shall any Contributor be +liable to You for damages, including any direct, indirect, special, incidental, +or consequential damages of any character arising as a result of this License or +out of the use or inability to use the Work (including but not limited to +damages for loss of goodwill, work stoppage, computer failure or malfunction, or +any and all other commercial damages or losses), even if such Contributor has +been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. + +While redistributing the Work or Derivative Works thereof, You may choose to +offer, and charge a fee for, acceptance of support, warranty, indemnity, or +other liability obligations and/or rights consistent with this License. However, +in accepting such obligations, You may act only on Your own behalf and on Your +sole responsibility, not on behalf of any other Contributor, and only if You +agree to indemnify, defend, and hold each Contributor harmless for any liability +incurred by, or claims asserted against, such Contributor by reason of your +accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work + +To apply the Apache License to your work, attach the following boilerplate +notice, with the fields enclosed by brackets "[]" replaced with your own +identifying information. (Don't include the brackets!) The text should be +enclosed in the appropriate comment syntax for the file format. We also +recommend that a file or class name and description of purpose be included on +the same "printed page" as the copyright notice for easier identification within +third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/node_modules/btoa/LICENSE.DOCS b/node_modules/btoa/LICENSE.DOCS new file mode 100644 index 0000000..1d658d6 --- /dev/null +++ b/node_modules/btoa/LICENSE.DOCS @@ -0,0 +1,319 @@ +Creative Commons Legal Code + +Attribution 3.0 Unported + + CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE + LEGAL SERVICES. DISTRIBUTION OF THIS LICENSE DOES NOT CREATE AN + ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS + INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES + REGARDING THE INFORMATION PROVIDED, AND DISCLAIMS LIABILITY FOR + DAMAGES RESULTING FROM ITS USE. + +License + +THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS CREATIVE +COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). THE WORK IS PROTECTED BY +COPYRIGHT AND/OR OTHER APPLICABLE LAW. ANY USE OF THE WORK OTHER THAN AS +AUTHORIZED UNDER THIS LICENSE OR COPYRIGHT LAW IS PROHIBITED. + +BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND AGREE +TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE MAY +BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS +CONTAINED HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND +CONDITIONS. + +1. Definitions + + a. "Adaptation" means a work based upon the Work, or upon the Work and + other pre-existing works, such as a translation, adaptation, + derivative work, arrangement of music or other alterations of a + literary or artistic work, or phonogram or performance and includes + cinematographic adaptations or any other form in which the Work may be + recast, transformed, or adapted including in any form recognizably + derived from the original, except that a work that constitutes a + Collection will not be considered an Adaptation for the purpose of + this License. For the avoidance of doubt, where the Work is a musical + work, performance or phonogram, the synchronization of the Work in + timed-relation with a moving image ("synching") will be considered an + Adaptation for the purpose of this License. + b. "Collection" means a collection of literary or artistic works, such as + encyclopedias and anthologies, or performances, phonograms or + broadcasts, or other works or subject matter other than works listed + in Section 1(f) below, which, by reason of the selection and + arrangement of their contents, constitute intellectual creations, in + which the Work is included in its entirety in unmodified form along + with one or more other contributions, each constituting separate and + independent works in themselves, which together are assembled into a + collective whole. A work that constitutes a Collection will not be + considered an Adaptation (as defined above) for the purposes of this + License. + c. "Distribute" means to make available to the public the original and + copies of the Work or Adaptation, as appropriate, through sale or + other transfer of ownership. + d. "Licensor" means the individual, individuals, entity or entities that + offer(s) the Work under the terms of this License. + e. "Original Author" means, in the case of a literary or artistic work, + the individual, individuals, entity or entities who created the Work + or if no individual or entity can be identified, the publisher; and in + addition (i) in the case of a performance the actors, singers, + musicians, dancers, and other persons who act, sing, deliver, declaim, + play in, interpret or otherwise perform literary or artistic works or + expressions of folklore; (ii) in the case of a phonogram the producer + being the person or legal entity who first fixes the sounds of a + performance or other sounds; and, (iii) in the case of broadcasts, the + organization that transmits the broadcast. + f. "Work" means the literary and/or artistic work offered under the terms + of this License including without limitation any production in the + literary, scientific and artistic domain, whatever may be the mode or + form of its expression including digital form, such as a book, + pamphlet and other writing; a lecture, address, sermon or other work + of the same nature; a dramatic or dramatico-musical work; a + choreographic work or entertainment in dumb show; a musical + composition with or without words; a cinematographic work to which are + assimilated works expressed by a process analogous to cinematography; + a work of drawing, painting, architecture, sculpture, engraving or + lithography; a photographic work to which are assimilated works + expressed by a process analogous to photography; a work of applied + art; an illustration, map, plan, sketch or three-dimensional work + relative to geography, topography, architecture or science; a + performance; a broadcast; a phonogram; a compilation of data to the + extent it is protected as a copyrightable work; or a work performed by + a variety or circus performer to the extent it is not otherwise + considered a literary or artistic work. + g. "You" means an individual or entity exercising rights under this + License who has not previously violated the terms of this License with + respect to the Work, or who has received express permission from the + Licensor to exercise rights under this License despite a previous + violation. + h. "Publicly Perform" means to perform public recitations of the Work and + to communicate to the public those public recitations, by any means or + process, including by wire or wireless means or public digital + performances; to make available to the public Works in such a way that + members of the public may access these Works from a place and at a + place individually chosen by them; to perform the Work to the public + by any means or process and the communication to the public of the + performances of the Work, including by public digital performance; to + broadcast and rebroadcast the Work by any means including signs, + sounds or images. + i. "Reproduce" means to make copies of the Work by any means including + without limitation by sound or visual recordings and the right of + fixation and reproducing fixations of the Work, including storage of a + protected performance or phonogram in digital form or other electronic + medium. + +2. Fair Dealing Rights. Nothing in this License is intended to reduce, +limit, or restrict any uses free from copyright or rights arising from +limitations or exceptions that are provided for in connection with the +copyright protection under copyright law or other applicable laws. + +3. License Grant. Subject to the terms and conditions of this License, +Licensor hereby grants You a worldwide, royalty-free, non-exclusive, +perpetual (for the duration of the applicable copyright) license to +exercise the rights in the Work as stated below: + + a. to Reproduce the Work, to incorporate the Work into one or more + Collections, and to Reproduce the Work as incorporated in the + Collections; + b. to create and Reproduce Adaptations provided that any such Adaptation, + including any translation in any medium, takes reasonable steps to + clearly label, demarcate or otherwise identify that changes were made + to the original Work. For example, a translation could be marked "The + original work was translated from English to Spanish," or a + modification could indicate "The original work has been modified."; + c. to Distribute and Publicly Perform the Work including as incorporated + in Collections; and, + d. to Distribute and Publicly Perform Adaptations. + e. For the avoidance of doubt: + + i. Non-waivable Compulsory License Schemes. In those jurisdictions in + which the right to collect royalties through any statutory or + compulsory licensing scheme cannot be waived, the Licensor + reserves the exclusive right to collect such royalties for any + exercise by You of the rights granted under this License; + ii. Waivable Compulsory License Schemes. In those jurisdictions in + which the right to collect royalties through any statutory or + compulsory licensing scheme can be waived, the Licensor waives the + exclusive right to collect such royalties for any exercise by You + of the rights granted under this License; and, + iii. Voluntary License Schemes. The Licensor waives the right to + collect royalties, whether individually or, in the event that the + Licensor is a member of a collecting society that administers + voluntary licensing schemes, via that society, from any exercise + by You of the rights granted under this License. + +The above rights may be exercised in all media and formats whether now +known or hereafter devised. The above rights include the right to make +such modifications as are technically necessary to exercise the rights in +other media and formats. Subject to Section 8(f), all rights not expressly +granted by Licensor are hereby reserved. + +4. Restrictions. The license granted in Section 3 above is expressly made +subject to and limited by the following restrictions: + + a. You may Distribute or Publicly Perform the Work only under the terms + of this License. You must include a copy of, or the Uniform Resource + Identifier (URI) for, this License with every copy of the Work You + Distribute or Publicly Perform. You may not offer or impose any terms + on the Work that restrict the terms of this License or the ability of + the recipient of the Work to exercise the rights granted to that + recipient under the terms of the License. You may not sublicense the + Work. You must keep intact all notices that refer to this License and + to the disclaimer of warranties with every copy of the Work You + Distribute or Publicly Perform. When You Distribute or Publicly + Perform the Work, You may not impose any effective technological + measures on the Work that restrict the ability of a recipient of the + Work from You to exercise the rights granted to that recipient under + the terms of the License. This Section 4(a) applies to the Work as + incorporated in a Collection, but this does not require the Collection + apart from the Work itself to be made subject to the terms of this + License. If You create a Collection, upon notice from any Licensor You + must, to the extent practicable, remove from the Collection any credit + as required by Section 4(b), as requested. If You create an + Adaptation, upon notice from any Licensor You must, to the extent + practicable, remove from the Adaptation any credit as required by + Section 4(b), as requested. + b. If You Distribute, or Publicly Perform the Work or any Adaptations or + Collections, You must, unless a request has been made pursuant to + Section 4(a), keep intact all copyright notices for the Work and + provide, reasonable to the medium or means You are utilizing: (i) the + name of the Original Author (or pseudonym, if applicable) if supplied, + and/or if the Original Author and/or Licensor designate another party + or parties (e.g., a sponsor institute, publishing entity, journal) for + attribution ("Attribution Parties") in Licensor's copyright notice, + terms of service or by other reasonable means, the name of such party + or parties; (ii) the title of the Work if supplied; (iii) to the + extent reasonably practicable, the URI, if any, that Licensor + specifies to be associated with the Work, unless such URI does not + refer to the copyright notice or licensing information for the Work; + and (iv) , consistent with Section 3(b), in the case of an Adaptation, + a credit identifying the use of the Work in the Adaptation (e.g., + "French translation of the Work by Original Author," or "Screenplay + based on original Work by Original Author"). The credit required by + this Section 4 (b) may be implemented in any reasonable manner; + provided, however, that in the case of a Adaptation or Collection, at + a minimum such credit will appear, if a credit for all contributing + authors of the Adaptation or Collection appears, then as part of these + credits and in a manner at least as prominent as the credits for the + other contributing authors. For the avoidance of doubt, You may only + use the credit required by this Section for the purpose of attribution + in the manner set out above and, by exercising Your rights under this + License, You may not implicitly or explicitly assert or imply any + connection with, sponsorship or endorsement by the Original Author, + Licensor and/or Attribution Parties, as appropriate, of You or Your + use of the Work, without the separate, express prior written + permission of the Original Author, Licensor and/or Attribution + Parties. + c. Except as otherwise agreed in writing by the Licensor or as may be + otherwise permitted by applicable law, if You Reproduce, Distribute or + Publicly Perform the Work either by itself or as part of any + Adaptations or Collections, You must not distort, mutilate, modify or + take other derogatory action in relation to the Work which would be + prejudicial to the Original Author's honor or reputation. Licensor + agrees that in those jurisdictions (e.g. Japan), in which any exercise + of the right granted in Section 3(b) of this License (the right to + make Adaptations) would be deemed to be a distortion, mutilation, + modification or other derogatory action prejudicial to the Original + Author's honor and reputation, the Licensor will waive or not assert, + as appropriate, this Section, to the fullest extent permitted by the + applicable national law, to enable You to reasonably exercise Your + right under Section 3(b) of this License (right to make Adaptations) + but not otherwise. + +5. Representations, Warranties and Disclaimer + +UNLESS OTHERWISE MUTUALLY AGREED TO BY THE PARTIES IN WRITING, LICENSOR +OFFERS THE WORK AS-IS AND MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY +KIND CONCERNING THE WORK, EXPRESS, IMPLIED, STATUTORY OR OTHERWISE, +INCLUDING, WITHOUT LIMITATION, WARRANTIES OF TITLE, MERCHANTIBILITY, +FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, OR THE ABSENCE OF +LATENT OR OTHER DEFECTS, ACCURACY, OR THE PRESENCE OF ABSENCE OF ERRORS, +WHETHER OR NOT DISCOVERABLE. SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION +OF IMPLIED WARRANTIES, SO SUCH EXCLUSION MAY NOT APPLY TO YOU. + +6. Limitation on Liability. EXCEPT TO THE EXTENT REQUIRED BY APPLICABLE +LAW, IN NO EVENT WILL LICENSOR BE LIABLE TO YOU ON ANY LEGAL THEORY FOR +ANY SPECIAL, INCIDENTAL, CONSEQUENTIAL, PUNITIVE OR EXEMPLARY DAMAGES +ARISING OUT OF THIS LICENSE OR THE USE OF THE WORK, EVEN IF LICENSOR HAS +BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + +7. Termination + + a. This License and the rights granted hereunder will terminate + automatically upon any breach by You of the terms of this License. + Individuals or entities who have received Adaptations or Collections + from You under this License, however, will not have their licenses + terminated provided such individuals or entities remain in full + compliance with those licenses. Sections 1, 2, 5, 6, 7, and 8 will + survive any termination of this License. + b. Subject to the above terms and conditions, the license granted here is + perpetual (for the duration of the applicable copyright in the Work). + Notwithstanding the above, Licensor reserves the right to release the + Work under different license terms or to stop distributing the Work at + any time; provided, however that any such election will not serve to + withdraw this License (or any other license that has been, or is + required to be, granted under the terms of this License), and this + License will continue in full force and effect unless terminated as + stated above. + +8. Miscellaneous + + a. Each time You Distribute or Publicly Perform the Work or a Collection, + the Licensor offers to the recipient a license to the Work on the same + terms and conditions as the license granted to You under this License. + b. Each time You Distribute or Publicly Perform an Adaptation, Licensor + offers to the recipient a license to the original Work on the same + terms and conditions as the license granted to You under this License. + c. If any provision of this License is invalid or unenforceable under + applicable law, it shall not affect the validity or enforceability of + the remainder of the terms of this License, and without further action + by the parties to this agreement, such provision shall be reformed to + the minimum extent necessary to make such provision valid and + enforceable. + d. No term or provision of this License shall be deemed waived and no + breach consented to unless such waiver or consent shall be in writing + and signed by the party to be charged with such waiver or consent. + e. This License constitutes the entire agreement between the parties with + respect to the Work licensed here. There are no understandings, + agreements or representations with respect to the Work not specified + here. Licensor shall not be bound by any additional provisions that + may appear in any communication from You. This License may not be + modified without the mutual written agreement of the Licensor and You. + f. The rights granted under, and the subject matter referenced, in this + License were drafted utilizing the terminology of the Berne Convention + for the Protection of Literary and Artistic Works (as amended on + September 28, 1979), the Rome Convention of 1961, the WIPO Copyright + Treaty of 1996, the WIPO Performances and Phonograms Treaty of 1996 + and the Universal Copyright Convention (as revised on July 24, 1971). + These rights and subject matter take effect in the relevant + jurisdiction in which the License terms are sought to be enforced + according to the corresponding provisions of the implementation of + those treaty provisions in the applicable national law. If the + standard suite of rights granted under applicable copyright law + includes additional rights not granted under this License, such + additional rights are deemed to be included in the License; this + License is not intended to restrict the license of any rights under + applicable law. + + +Creative Commons Notice + + Creative Commons is not a party to this License, and makes no warranty + whatsoever in connection with the Work. Creative Commons will not be + liable to You or any party on any legal theory for any damages + whatsoever, including without limitation any general, special, + incidental or consequential damages arising in connection to this + license. Notwithstanding the foregoing two (2) sentences, if Creative + Commons has expressly identified itself as the Licensor hereunder, it + shall have all rights and obligations of Licensor. + + Except for the limited purpose of indicating to the public that the + Work is licensed under the CCPL, Creative Commons does not authorize + the use by either party of the trademark "Creative Commons" or any + related trademark or logo of Creative Commons without the prior + written consent of Creative Commons. Any permitted use will be in + compliance with Creative Commons' then-current trademark usage + guidelines, as may be published on its website or otherwise made + available upon request from time to time. For the avoidance of doubt, + this trademark restriction does not form part of this License. + + Creative Commons may be contacted at http://creativecommons.org/. diff --git a/node_modules/btoa/README.md b/node_modules/btoa/README.md new file mode 100644 index 0000000..073691d --- /dev/null +++ b/node_modules/btoa/README.md @@ -0,0 +1,40 @@ +btoa +=== + +| [atob](https://git.coolaj86.com/coolaj86/atob.js) +| **btoa** +| [unibabel.js](https://git.coolaj86.com/coolaj86/unibabel.js) +| Sponsored by [ppl](https://ppl.family) + +A port of the browser's `btoa` function. + +Uses `Buffer` to emulate the exact functionality of the browser's btoa +(except that it supports some unicode that the browser may not). + +It turns binary data __to__ base64-encoded ascii. + +```js +(function () { + "use strict"; + + var btoa = require('btoa'); + var bin = "Hello, 世界"; + var b64 = btoa(bin); + + console.log(b64); // "SGVsbG8sIBZM" +}()); +``` + +**Note**: Unicode may or may not be handled incorrectly. +This module is intended to provide exact compatibility with the browser. + +Copyright and License +=== + +Code copyright 2012-2018 AJ ONeal + +Dual-licensed MIT and Apache-2.0 + +Docs copyright 2012-2018 AJ ONeal + +Docs released under [Creative Commons](https://git.coolaj86.com/coolaj86/btoa.js/blob/master/LICENSE.DOCS). diff --git a/node_modules/btoa/bin/btoa.js b/node_modules/btoa/bin/btoa.js new file mode 100644 index 0000000..a237ce2 --- /dev/null +++ b/node_modules/btoa/bin/btoa.js @@ -0,0 +1,8 @@ +#!/usr/bin/env node +(function () { + "use strict"; + + var btoa = require('../index'); + + console.log(btoa(process.argv[2])); +}()); diff --git a/node_modules/btoa/index.js b/node_modules/btoa/index.js new file mode 100644 index 0000000..34b8f15 --- /dev/null +++ b/node_modules/btoa/index.js @@ -0,0 +1,17 @@ +(function () { + "use strict"; + + function btoa(str) { + var buffer; + + if (str instanceof Buffer) { + buffer = str; + } else { + buffer = Buffer.from(str.toString(), 'binary'); + } + + return buffer.toString('base64'); + } + + module.exports = btoa; +}()); diff --git a/node_modules/btoa/package.json b/node_modules/btoa/package.json new file mode 100644 index 0000000..56e5bce --- /dev/null +++ b/node_modules/btoa/package.json @@ -0,0 +1,84 @@ +{ + "_args": [ + [ + "btoa", + "/mnt/c/Users/Josua/Desktop/data/nodejs/electron/chat-project/chat-server" + ] + ], + "_from": "btoa@latest", + "_id": "btoa@1.2.1", + "_inCache": true, + "_installable": true, + "_location": "/btoa", + "_nodeVersion": "8.9.4", + "_npmOperationalInternal": { + "host": "s3://npm-registry-packages", + "tmp": "tmp/btoa_1.2.1_1522212847456_0.8367382855709491" + }, + "_npmUser": { + "email": "coolaj86@gmail.com", + "name": "coolaj86" + }, + "_npmVersion": "5.6.0", + "_phantomChildren": {}, + "_requested": { + "name": "btoa", + "raw": "btoa", + "rawSpec": "", + "scope": null, + "spec": "latest", + "type": "tag" + }, + "_requiredBy": [ + "#USER" + ], + "_resolved": "https://registry.npmjs.org/btoa/-/btoa-1.2.1.tgz", + "_shasum": "01a9909f8b2c93f6bf680ba26131eb30f7fa3d73", + "_shrinkwrap": null, + "_spec": "btoa", + "_where": "/mnt/c/Users/Josua/Desktop/data/nodejs/electron/chat-project/chat-server", + "author": { + "email": "coolaj86@gmail.com", + "name": "AJ ONeal", + "url": "https://coolaj86.com" + }, + "bin": { + "btoa": "bin/btoa.js" + }, + "dependencies": {}, + "description": "btoa for Node.JS (it's a one-liner)", + "devDependencies": {}, + "directories": {}, + "dist": { + "fileCount": 7, + "integrity": "sha512-SB4/MIGlsiVkMcHmT+pSmIPoNDoHg+7cMzmt3Uxt628MTz2487DKSqK/fuhFBrkuqrYv5UCEnACpF4dTFNKc/g==", + "shasum": "01a9909f8b2c93f6bf680ba26131eb30f7fa3d73", + "tarball": "https://registry.npmjs.org/btoa/-/btoa-1.2.1.tgz", + "unpackedSize": 32050 + }, + "engines": { + "node": ">= 0.4.0" + }, + "gitHead": "19e0da65686fc1d726f1b5210930a47fe9fbd3a3", + "homepage": "https://git.coolaj86.com/coolaj86/btoa.js.git", + "keywords": [ + "browser", + "btoa" + ], + "license": "(MIT OR Apache-2.0)", + "main": "index", + "maintainers": [ + { + "name": "coolaj86", + "email": "coolaj86@gmail.com" + } + ], + "name": "btoa", + "optionalDependencies": {}, + "readme": "ERROR: No README data found!", + "repository": { + "type": "git", + "url": "git://git.coolaj86.com/coolaj86/btoa.js.git" + }, + "version": "1.2.1" +} diff --git a/node_modules/btoa/test.js b/node_modules/btoa/test.js new file mode 100644 index 0000000..d57f94e --- /dev/null +++ b/node_modules/btoa/test.js @@ -0,0 +1,16 @@ +/*jshint strict:true node:true es5:true onevar:true laxcomma:true laxbreak:true eqeqeq:true immed:true latedef:true*/ +(function () { + "use strict"; + + var btoa = require('./index') + , encoded = "SGVsbG8sIBZM" + , unencoded = "Hello, 世界" + ; + + if (encoded !== btoa(unencoded)) { + console.error('[FAIL]', encoded, btoa(unencoded)); + return; + } + + console.log('[PASS] all tests pass'); +}()); diff --git a/node_modules/errno/.jshintrc b/node_modules/errno/.jshintrc new file mode 100644 index 0000000..c8ef3ca --- /dev/null +++ b/node_modules/errno/.jshintrc @@ -0,0 +1,59 @@ +{ + "predef": [ ] + , "bitwise": false + , "camelcase": false + , "curly": false + , "eqeqeq": false + , "forin": false + , "immed": false + , "latedef": false + , "noarg": true + , "noempty": true + , "nonew": true + , "plusplus": false + , "quotmark": true + , "regexp": false + , "undef": true + , "unused": true + , "strict": false + , "trailing": true + , "maxlen": 120 + , "asi": true + , "boss": true + , "debug": true + , "eqnull": true + , "esnext": true + , "evil": true + , "expr": true + , "funcscope": false + , "globalstrict": false + , "iterator": false + , "lastsemic": true + , "laxbreak": true + , "laxcomma": true + , "loopfunc": true + , "multistr": false + , "onecase": false + , "proto": false + , "regexdash": false + , "scripturl": true + , "smarttabs": false + , "shadow": false + , "sub": true + , "supernew": false + , "validthis": true + , "browser": true + , "couch": false + , "devel": false + , "dojo": false + , "mootools": false + , "node": true + , "nonstandard": true + , "prototypejs": false + , "rhino": false + , "worker": true + , "wsh": false + , "nomen": false + , "onevar": false + , "passfail": false +} \ No newline at end of file diff --git a/node_modules/errno/.travis.yml b/node_modules/errno/.travis.yml new file mode 100644 index 0000000..f996821 --- /dev/null +++ b/node_modules/errno/.travis.yml @@ -0,0 +1,11 @@ +sudo: false + +language: node_js + +node_js: + - 9 + - 8 + - 7 + - 6 + - 5 + - 4 diff --git a/node_modules/errno/README.md b/node_modules/errno/README.md new file mode 100644 index 0000000..a4d0fb5 --- /dev/null +++ b/node_modules/errno/README.md @@ -0,0 +1,145 @@ +# node-errno + +> Better [libuv](https://github.com/libuv/libuv)/[Node.js](https://nodejs.org)/[io.js](https://iojs.org) error handling & reporting. Available in npm as *errno*. + +[![npm](https://img.shields.io/npm/v/errno.svg)](https://www.npmjs.com/package/errno) +[![Build Status](https://secure.travis-ci.org/rvagg/node-errno.png)](http://travis-ci.org/rvagg/node-errno) +[![npm](https://img.shields.io/npm/dm/errno.svg)](https://www.npmjs.com/package/errno) + +* [errno exposed](#errnoexposed) +* [Custom errors](#customerrors) + + +## errno exposed + +Ever find yourself needing more details about Node.js errors? Me too, so *node-errno* contains the errno mappings direct from libuv so you can use them in your code. + +**By errno:** + +```js +require('errno').errno[3] +// → { +// "errno": 3, +// "code": "EACCES", +// "description": "permission denied" +// } +``` + +**By code:** + +```js +require('errno').code.ENOTEMPTY +// → { +// "errno": 53, +// "code": "ENOTEMPTY", +// "description": "directory not empty" +// } +``` + +**Make your errors more descriptive:** + +```js +var errno = require('errno') + +function errmsg(err) { + var str = 'Error: ' + // if it's a libuv error then get the description from errno + if (errno.errno[err.errno]) + str += errno.errno[err.errno].description + else + str += err.message + + // if it's a `fs` error then it'll have a 'path' property + if (err.path) + str += ' [' + err.path + ']' + + return str +} + +var fs = require('fs') + +fs.readFile('thisisnotarealfile.txt', function (err, data) { + if (err) + console.log(errmsg(err)) +}) +``` + +**Use as a command line tool:** + +``` +~ $ errno 53 +{ + "errno": 53, + "code": "ENOTEMPTY", + "description": "directory not empty" +} +~ $ errno EROFS +{ + "errno": 56, + "code": "EROFS", + "description": "read-only file system" +} +~ $ errno foo +No such errno/code: "foo" +``` + +Supply no arguments for the full list. Error codes are processed case-insensitive. + +You will need to install with `npm install errno -g` if you want the `errno` command to be available without supplying a full path to the node_modules installation. + + +## Custom errors + +Use `errno.custom.createError()` to create custom `Error` objects to throw around in your Node.js library. Create error hierarchies so `instanceof` becomes a useful tool in tracking errors. Call-stack is correctly captured at the time you create an instance of the error object, plus a `cause` property will make available the original error object if you pass one in to the constructor. + +```js +var create = require('errno').custom.createError +var MyError = create('MyError') // inherits from Error +var SpecificError = create('SpecificError', MyError) // inherits from MyError +var OtherError = create('OtherError', MyError) + +// use them! +if (condition) throw new SpecificError('Eeek! Something bad happened') + +if (err) return callback(new OtherError(err)) +``` + +Also available is a `errno.custom.FilesystemError` with in-built access to errno properties: + +```js +fs.readFile('foo', function (err, data) { + if (err) return callback(new errno.custom.FilesystemError(err)) + // do something else +}) +``` + +The resulting error object passed through the callback will have the following properties: `code`, `errno`, `path` and `message` will contain a descriptive human-readable message. + +## Contributors + +* [bahamas10](https://github.com/bahamas10) (Dave Eddy) - Added CLI +* [ralphtheninja](https://github.com/ralphtheninja) (Lars-Magnus Skog) + +## Copyright & Licence + +*Copyright (c) 2012-2015 [Rod Vagg](https://github.com/rvagg) ([@rvagg](https://twitter.com/rvagg))* + +Made available under the MIT licence: + +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/errno/build.js b/node_modules/errno/build.js new file mode 100644 index 0000000..fce8926 --- /dev/null +++ b/node_modules/errno/build.js @@ -0,0 +1,43 @@ +#!/usr/bin/env node + +var request = require('request') + , fs = require('fs') + + , uvheadloc = 'https://raw.github.com/joyent/libuv/master/include/uv.h' + , defreg = /^\s*XX\(\s*([\-\d]+),\s*([A-Z]+),\s*"([^"]*)"\s*\)\s*\\?$/ + + +request(uvheadloc, function (err, response) { + if (err) + throw err + + var data, out + + data = response.body + .split('\n') + .map(function (line) { return line.match(defreg) }) + .filter(function (match) { return match }) + .map(function (match) { return { + errno: parseInt(match[1], 10) + , code: match[2] + , description: match[3] + }}) + + out = 'var all = module.exports.all = ' + JSON.stringify(data, 0, 1) + '\n\n' + + out += '\nmodule.exports.errno = {\n ' + + data.map(function (e, i) { + return '\'' + e.errno + '\': all[' + i + ']' + }).join('\n , ') + + '\n}\n\n' + + out += '\nmodule.exports.code = {\n ' + + data.map(function (e, i) { + return '\'' + e.code + '\': all[' + i + ']' + }).join('\n , ') + + '\n}\n\n' + + out += '\nmodule.exports.custom = require("./custom")(module.exports)\n' + + fs.writeFile('errno.js', out) +}) \ No newline at end of file diff --git a/node_modules/errno/cli.js b/node_modules/errno/cli.js new file mode 100644 index 0000000..61d179b --- /dev/null +++ b/node_modules/errno/cli.js @@ -0,0 +1,22 @@ +#!/usr/bin/env node + +var errno = require('./') + , arg = process.argv[2] + , data, code + +if (arg === undefined) { + console.log(JSON.stringify(errno.code, null, 2)) + process.exit(0) +} + +if ((code = +arg) == arg) + data = errno.errno[code] +else + data = errno.code[arg] || errno.code[arg.toUpperCase()] + +if (data) + console.log(JSON.stringify(data, null, 2)) +else { + console.error('No such errno/code: "' + arg + '"') + process.exit(1) +} diff --git a/node_modules/errno/custom.js b/node_modules/errno/custom.js new file mode 100644 index 0000000..ca8c1d8 --- /dev/null +++ b/node_modules/errno/custom.js @@ -0,0 +1,57 @@ +var prr = require('prr') + +function init (type, message, cause) { + if (!!message && typeof message != 'string') { + message = message.message || message.name + } + prr(this, { + type : type + , name : type + // can be passed just a 'cause' + , cause : typeof message != 'string' ? message : cause + , message : message + }, 'ewr') +} + +// generic prototype, not intended to be actually used - helpful for `instanceof` +function CustomError (message, cause) { + Error.call(this) + if (Error.captureStackTrace) + Error.captureStackTrace(this, this.constructor) + init.call(this, 'CustomError', message, cause) +} + +CustomError.prototype = new Error() + +function createError (errno, type, proto) { + var err = function (message, cause) { + init.call(this, type, message, cause) + //TODO: the specificity here is stupid, errno should be available everywhere + if (type == 'FilesystemError') { + this.code = this.cause.code + this.path = this.cause.path + this.errno = this.cause.errno + this.message = + (errno.errno[this.cause.errno] + ? errno.errno[this.cause.errno].description + : this.cause.message) + + (this.cause.path ? ' [' + this.cause.path + ']' : '') + } + Error.call(this) + if (Error.captureStackTrace) + Error.captureStackTrace(this, err) + } + err.prototype = !!proto ? new proto() : new CustomError() + return err +} + +module.exports = function (errno) { + var ce = function (type, proto) { + return createError(errno, type, proto) + } + return { + CustomError : CustomError + , FilesystemError : ce('FilesystemError') + , createError : ce + } +} diff --git a/node_modules/errno/errno.js b/node_modules/errno/errno.js new file mode 100644 index 0000000..efb79d4 --- /dev/null +++ b/node_modules/errno/errno.js @@ -0,0 +1,313 @@ +var all = module.exports.all = [ + { + errno: -2, + code: 'ENOENT', + description: 'no such file or directory' + }, + { + errno: -1, + code: 'UNKNOWN', + description: 'unknown error' + }, + { + errno: 0, + code: 'OK', + description: 'success' + }, + { + errno: 1, + code: 'EOF', + description: 'end of file' + }, + { + errno: 2, + code: 'EADDRINFO', + description: 'getaddrinfo error' + }, + { + errno: 3, + code: 'EACCES', + description: 'permission denied' + }, + { + errno: 4, + code: 'EAGAIN', + description: 'resource temporarily unavailable' + }, + { + errno: 5, + code: 'EADDRINUSE', + description: 'address already in use' + }, + { + errno: 6, + code: 'EADDRNOTAVAIL', + description: 'address not available' + }, + { + errno: 7, + code: 'EAFNOSUPPORT', + description: 'address family not supported' + }, + { + errno: 8, + code: 'EALREADY', + description: 'connection already in progress' + }, + { + errno: 9, + code: 'EBADF', + description: 'bad file descriptor' + }, + { + errno: 10, + code: 'EBUSY', + description: 'resource busy or locked' + }, + { + errno: 11, + code: 'ECONNABORTED', + description: 'software caused connection abort' + }, + { + errno: 12, + code: 'ECONNREFUSED', + description: 'connection refused' + }, + { + errno: 13, + code: 'ECONNRESET', + description: 'connection reset by peer' + }, + { + errno: 14, + code: 'EDESTADDRREQ', + description: 'destination address required' + }, + { + errno: 15, + code: 'EFAULT', + description: 'bad address in system call argument' + }, + { + errno: 16, + code: 'EHOSTUNREACH', + description: 'host is unreachable' + }, + { + errno: 17, + code: 'EINTR', + description: 'interrupted system call' + }, + { + errno: 18, + code: 'EINVAL', + description: 'invalid argument' + }, + { + errno: 19, + code: 'EISCONN', + description: 'socket is already connected' + }, + { + errno: 20, + code: 'EMFILE', + description: 'too many open files' + }, + { + errno: 21, + code: 'EMSGSIZE', + description: 'message too long' + }, + { + errno: 22, + code: 'ENETDOWN', + description: 'network is down' + }, + { + errno: 23, + code: 'ENETUNREACH', + description: 'network is unreachable' + }, + { + errno: 24, + code: 'ENFILE', + description: 'file table overflow' + }, + { + errno: 25, + code: 'ENOBUFS', + description: 'no buffer space available' + }, + { + errno: 26, + code: 'ENOMEM', + description: 'not enough memory' + }, + { + errno: 27, + code: 'ENOTDIR', + description: 'not a directory' + }, + { + errno: 28, + code: 'EISDIR', + description: 'illegal operation on a directory' + }, + { + errno: 29, + code: 'ENONET', + description: 'machine is not on the network' + }, + { + errno: 31, + code: 'ENOTCONN', + description: 'socket is not connected' + }, + { + errno: 32, + code: 'ENOTSOCK', + description: 'socket operation on non-socket' + }, + { + errno: 33, + code: 'ENOTSUP', + description: 'operation not supported on socket' + }, + { + errno: 34, + code: 'ENOENT', + description: 'no such file or directory' + }, + { + errno: 35, + code: 'ENOSYS', + description: 'function not implemented' + }, + { + errno: 36, + code: 'EPIPE', + description: 'broken pipe' + }, + { + errno: 37, + code: 'EPROTO', + description: 'protocol error' + }, + { + errno: 38, + code: 'EPROTONOSUPPORT', + description: 'protocol not supported' + }, + { + errno: 39, + code: 'EPROTOTYPE', + description: 'protocol wrong type for socket' + }, + { + errno: 40, + code: 'ETIMEDOUT', + description: 'connection timed out' + }, + { + errno: 41, + code: 'ECHARSET', + description: 'invalid Unicode character' + }, + { + errno: 42, + code: 'EAIFAMNOSUPPORT', + description: 'address family for hostname not supported' + }, + { + errno: 44, + code: 'EAISERVICE', + description: 'servname not supported for ai_socktype' + }, + { + errno: 45, + code: 'EAISOCKTYPE', + description: 'ai_socktype not supported' + }, + { + errno: 46, + code: 'ESHUTDOWN', + description: 'cannot send after transport endpoint shutdown' + }, + { + errno: 47, + code: 'EEXIST', + description: 'file already exists' + }, + { + errno: 48, + code: 'ESRCH', + description: 'no such process' + }, + { + errno: 49, + code: 'ENAMETOOLONG', + description: 'name too long' + }, + { + errno: 50, + code: 'EPERM', + description: 'operation not permitted' + }, + { + errno: 51, + code: 'ELOOP', + description: 'too many symbolic links encountered' + }, + { + errno: 52, + code: 'EXDEV', + description: 'cross-device link not permitted' + }, + { + errno: 53, + code: 'ENOTEMPTY', + description: 'directory not empty' + }, + { + errno: 54, + code: 'ENOSPC', + description: 'no space left on device' + }, + { + errno: 55, + code: 'EIO', + description: 'i/o error' + }, + { + errno: 56, + code: 'EROFS', + description: 'read-only file system' + }, + { + errno: 57, + code: 'ENODEV', + description: 'no such device' + }, + { + errno: 58, + code: 'ESPIPE', + description: 'invalid seek' + }, + { + errno: 59, + code: 'ECANCELED', + description: 'operation canceled' + } +] + +module.exports.errno = {} +module.exports.code = {} + +all.forEach(function (error) { + module.exports.errno[error.errno] = error + module.exports.code[error.code] = error +}) + +module.exports.custom = require('./custom')(module.exports) +module.exports.create = module.exports.custom.createError diff --git a/node_modules/errno/package.json b/node_modules/errno/package.json new file mode 100644 index 0000000..951373b --- /dev/null +++ b/node_modules/errno/package.json @@ -0,0 +1,96 @@ +{ + "_args": [ + [ + "errno@~0.1.7", + "/mnt/c/Users/Josua/Desktop/data/nodejs/electron/chat-project/chat-server/node_modules/worker-farm" + ] + ], + "_from": "errno@>=0.1.7 <0.2.0", + "_id": "errno@0.1.7", + "_inCache": true, + "_installable": true, + "_location": "/errno", + "_nodeVersion": "9.4.0", + "_npmOperationalInternal": { + "host": "s3://npm-registry-packages", + "tmp": "tmp/errno_0.1.7_1518663517573_0.2402603718742864" + }, + "_npmUser": { + "email": "ralphtheninja@riseup.net", + "name": "ralphtheninja" + }, + "_npmVersion": "5.6.0", + "_phantomChildren": {}, + "_requested": { + "name": "errno", + "raw": "errno@~0.1.7", + "rawSpec": "~0.1.7", + "scope": null, + "spec": ">=0.1.7 <0.2.0", + "type": "range" + }, + "_requiredBy": [ + "/worker-farm" + ], + "_resolved": "https://registry.npmjs.org/errno/-/errno-0.1.7.tgz", + "_shasum": "4684d71779ad39af177e3f007996f7c67c852618", + "_shrinkwrap": null, + "_spec": "errno@~0.1.7", + "_where": "/mnt/c/Users/Josua/Desktop/data/nodejs/electron/chat-project/chat-server/node_modules/worker-farm", + "authors": [ + "Rod Vagg @rvagg (https://github.com/rvagg)" + ], + "bin": { + "errno": "./cli.js" + }, + "bugs": { + "url": "https://github.com/rvagg/node-errno/issues" + }, + "dependencies": { + "prr": "~1.0.1" + }, + "description": "libuv errno details exposed", + "devDependencies": { + "error-stack-parser": "^2.0.1", + "inherits": "^2.0.3", + "tape": "~4.8.0" + }, + "directories": {}, + "dist": { + "fileCount": 9, + "integrity": "sha512-MfrRBDWzIWifgq6tJj60gkAwtLNb6sQPlcFrSOflcP1aFmmruKQ2wRnze/8V6kgyz7H3FF8Npzv78mZ7XLLflg==", + "shasum": "4684d71779ad39af177e3f007996f7c67c852618", + "tarball": "https://registry.npmjs.org/errno/-/errno-0.1.7.tgz", + "unpackedSize": 18048 + }, + "gitHead": "ffb10fcb9459bcef9dcf5cb5f4fa6c68bbab8f4e", + "homepage": "https://github.com/rvagg/node-errno#readme", + "keywords": [ + "errno", + "errors", + "libuv" + ], + "license": "MIT", + "main": "errno.js", + "maintainers": [ + { + "name": "ralphtheninja", + "email": "ralphtheninja@riseup.net" + }, + { + "name": "rvagg", + "email": "r@va.gg" + } + ], + "name": "errno", + "optionalDependencies": {}, + "readme": "ERROR: No README data found!", + "repository": { + "type": "git", + "url": "git+https://github.com/rvagg/node-errno.git" + }, + "scripts": { + "test": "node --use_strict test.js" + }, + "version": "0.1.7" +} diff --git a/node_modules/errno/test.js b/node_modules/errno/test.js new file mode 100644 index 0000000..1c04635 --- /dev/null +++ b/node_modules/errno/test.js @@ -0,0 +1,88 @@ +var test = require('tape') + , inherits = require('inherits') + , ErrorStackParser = require('error-stack-parser') + , errno = require('./') + +test('sanity checks', function (t) { + t.ok(errno.all, 'errno.all not found') + t.ok(errno.errno, 'errno.errno not found') + t.ok(errno.code, 'errno.code not found') + + t.equal(errno.all.length, 60, 'found ' + errno.all.length + ', expected 60') + t.equal(errno.errno['-1'], errno.all[1], 'errno -1 not second element') + + t.equal(errno.code['UNKNOWN'], errno.all[1], 'code UNKNOWN not second element') + + t.equal(errno.errno[1], errno.all[3], 'errno 1 not fourth element') + + t.equal(errno.code['EOF'], errno.all[3], 'code EOF not fourth element') + t.end() +}) + +test('custom errors', function (t) { + const Cust = errno.create('FooNotBarError') + const cust = new Cust('foo is not bar') + + t.equal(cust.name, 'FooNotBarError', 'correct custom name') + t.equal(cust.type, 'FooNotBarError', 'correct custom type') + t.equal(cust.message, 'foo is not bar', 'correct custom message') + t.notOk(cust.cause, 'no cause') + t.end() +}) + +test('callstack', function (t) { + const MyError = errno.create('MyError') + + function lastFunction (ErrorType, cb) { + process.nextTick(cb, new ErrorType('oh noes!')) + } + + function secondLastFunction (ErrorType, cb) { + lastFunction(ErrorType, cb) + } + + function testFrames (t) { + return function (err) { + const stack = ErrorStackParser.parse(err) + t.same(stack[0].functionName, 'lastFunction', 'last stack frame ok') + t.same(stack[1].functionName, 'secondLastFunction', 'second last stack frame ok') + t.end() + } + } + + t.test('custom error, default prototype', function (t) { + secondLastFunction(MyError, testFrames(t)) + }) + + t.test('custom error, custom prototype', function (t) { + const MyError2 = errno.create('MyError2', MyError) + secondLastFunction(MyError2, testFrames(t)) + }) + + t.test('custom error, using inheritance', function (t) { + const CustomError = errno.custom.CustomError + + function MyError3 (message, cause) { + CustomError.call(this, message, cause) + } + + inherits(MyError3, CustomError) + + secondLastFunction(MyError3, testFrames(t)) + }) +}) + +test('error without message', function (t) { + const Cust = errno.create('WriteError') + const cust = new Cust({ + code: 22, + message: '', + name: 'QuotaExceededError' + }) + + t.equal(cust.name, 'WriteError', 'correct custom name') + t.equal(cust.type, 'WriteError', 'correct custom type') + t.equal(cust.message, 'QuotaExceededError', 'message is the name') + t.notOk(cust.cause, 'no cause') + t.end() +}) diff --git a/node_modules/prr/.jshintrc b/node_modules/prr/.jshintrc new file mode 100644 index 0000000..6a7a956 --- /dev/null +++ b/node_modules/prr/.jshintrc @@ -0,0 +1,61 @@ +{ + "predef": [ ] + , "bitwise": false + , "camelcase": false + , "curly": false + , "eqeqeq": false + , "forin": false + , "immed": false + , "latedef": false + , "newcap": true + , "noarg": true + , "noempty": true + , "nonew": true + , "plusplus": false + , "quotmark": true + , "regexp": false + , "undef": true + , "unused": true + , "strict": false + , "trailing": true + , "maxlen": 120 + , "asi": true + , "boss": true + , "debug": true + , "eqnull": true + , "es5": true + , "esnext": true + , "evil": true + , "expr": true + , "funcscope": false + , "globalstrict": false + , "iterator": false + , "lastsemic": true + , "laxbreak": true + , "laxcomma": true + , "loopfunc": true + , "multistr": false + , "onecase": false + , "proto": false + , "regexdash": false + , "scripturl": true + , "smarttabs": false + , "shadow": false + , "sub": true + , "supernew": false + , "validthis": true + , "browser": true + , "couch": false + , "devel": false + , "dojo": false + , "mootools": false + , "node": true + , "nonstandard": true + , "prototypejs": false + , "rhino": false + , "worker": true + , "wsh": false + , "nomen": false + , "onevar": true + , "passfail": false +} \ No newline at end of file diff --git a/node_modules/prr/.npmignore b/node_modules/prr/.npmignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/node_modules/prr/.npmignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/node_modules/prr/.travis.yml b/node_modules/prr/.travis.yml new file mode 100644 index 0000000..33dcbc3 --- /dev/null +++ b/node_modules/prr/.travis.yml @@ -0,0 +1,10 @@ +language: node_js +node_js: + - 0.8 + - "0.10" +branches: + only: + - master +notifications: + email: + - rod@vagg.org \ No newline at end of file diff --git a/node_modules/prr/LICENSE.md b/node_modules/prr/LICENSE.md new file mode 100644 index 0000000..29b95e3 --- /dev/null +++ b/node_modules/prr/LICENSE.md @@ -0,0 +1,11 @@ +The MIT License (MIT) +===================== + +Copyright (c) 2014 Rod Vagg +--------------------------- + +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/prr/README.md b/node_modules/prr/README.md new file mode 100644 index 0000000..b934048 --- /dev/null +++ b/node_modules/prr/README.md @@ -0,0 +1,47 @@ +# prr [![Build Status](https://secure.travis-ci.org/rvagg/prr.png)](http://travis-ci.org/rvagg/prr) + +An sensible alternative to `Object.defineProperty()`. Available in npm and Ender as **prr**. + +## Usage + +Set the property `'foo'` (`obj.foo`) to have the value `'bar'` with default options (`'enumerable'`, `'configurable'` and `'writable'` are all `false`): + +```js +prr(obj, 'foo', 'bar') +``` + +Adjust the default options: + +```js +prr(obj, 'foo', 'bar', { enumerable: true, writable: true }) +``` + +Do the same operation for multiple properties: + +```js +prr(obj, { one: 'one', two: 'two' }) +// or with options: +prr(obj, { one: 'one', two: 'two' }, { enumerable: true, writable: true }) +``` + +### Simplify! + +But obviously, having to write out the full options object makes it nearly as bad as the original `Object.defineProperty()` so we can simplify. + +As an alternative method we can use an options string where each character represents a option: `'e'=='enumerable'`, `'c'=='configurable'` and `'w'=='writable'`: + +```js +prr(obj, 'foo', 'bar', 'ew') // enumerable and writable but not configurable +// muliple properties: +prr(obj, { one: 'one', two: 'two' }, 'ewc') // configurable too +``` + +## Where can I use it? + +Anywhere! For pre-ES5 environments *prr* will simply fall-back to an `object[property] = value` so you can get close to what you want. + +*prr* is Ender-compatible so you can include it in your Ender build and `$.prr(...)` or `var prr = require('prr'); prr(...)`. + +## Licence + +prr is Copyright (c) 2013 Rod Vagg [@rvagg](https://twitter.com/rvagg) and licensed under the MIT licence. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE.md file for more details. diff --git a/node_modules/prr/package.json b/node_modules/prr/package.json new file mode 100644 index 0000000..48320b5 --- /dev/null +++ b/node_modules/prr/package.json @@ -0,0 +1,80 @@ +{ + "_args": [ + [ + "prr@~1.0.1", + "/mnt/c/Users/Josua/Desktop/data/nodejs/electron/chat-project/chat-server/node_modules/errno" + ] + ], + "_from": "prr@>=1.0.1 <1.1.0", + "_id": "prr@1.0.1", + "_inCache": true, + "_installable": true, + "_location": "/prr", + "_npmUser": { + "email": "rod@vagg.org", + "name": "rvagg" + }, + "_npmVersion": "1.4.14", + "_phantomChildren": {}, + "_requested": { + "name": "prr", + "raw": "prr@~1.0.1", + "rawSpec": "~1.0.1", + "scope": null, + "spec": ">=1.0.1 <1.1.0", + "type": "range" + }, + "_requiredBy": [ + "/errno" + ], + "_resolved": "https://registry.npmjs.org/prr/-/prr-1.0.1.tgz", + "_shasum": "d3fc114ba06995a45ec6893f484ceb1d78f5f476", + "_shrinkwrap": null, + "_spec": "prr@~1.0.1", + "_where": "/mnt/c/Users/Josua/Desktop/data/nodejs/electron/chat-project/chat-server/node_modules/errno", + "author": { + "email": "rod@vagg.org", + "name": "Rod Vagg", + "url": "https://github.com/rvagg" + }, + "bugs": { + "url": "https://github.com/rvagg/prr/issues" + }, + "dependencies": {}, + "description": "A better Object.defineProperty()", + "devDependencies": { + "tap": "*" + }, + "directories": {}, + "dist": { + "shasum": "d3fc114ba06995a45ec6893f484ceb1d78f5f476", + "tarball": "https://registry.npmjs.org/prr/-/prr-1.0.1.tgz" + }, + "gitHead": "b69ba0edc7aacbda0c98d550579e452b8597c126", + "homepage": "https://github.com/rvagg/prr", + "keywords": [ + "defineProperty", + "ender", + "properties", + "property" + ], + "license": "MIT", + "main": "./prr.js", + "maintainers": [ + { + "name": "rvagg", + "email": "rod@vagg.org" + } + ], + "name": "prr", + "optionalDependencies": {}, + "readme": "ERROR: No README data found!", + "repository": { + "type": "git", + "url": "git+https://github.com/rvagg/prr.git" + }, + "scripts": { + "test": "node ./test.js" + }, + "version": "1.0.1" +} diff --git a/node_modules/prr/prr.js b/node_modules/prr/prr.js new file mode 100644 index 0000000..94f5862 --- /dev/null +++ b/node_modules/prr/prr.js @@ -0,0 +1,63 @@ +/*! + * prr + * (c) 2013 Rod Vagg + * https://github.com/rvagg/prr + * License: MIT + */ + +(function (name, context, definition) { + if (typeof module != 'undefined' && module.exports) + module.exports = definition() + else + context[name] = definition() +})('prr', this, function() { + + var setProperty = typeof Object.defineProperty == 'function' + ? function (obj, key, options) { + Object.defineProperty(obj, key, options) + return obj + } + : function (obj, key, options) { // < es5 + obj[key] = options.value + return obj + } + + , makeOptions = function (value, options) { + var oo = typeof options == 'object' + , os = !oo && typeof options == 'string' + , op = function (p) { + return oo + ? !!options[p] + : os + ? options.indexOf(p[0]) > -1 + : false + } + + return { + enumerable : op('enumerable') + , configurable : op('configurable') + , writable : op('writable') + , value : value + } + } + + , prr = function (obj, key, value, options) { + var k + + options = makeOptions(value, options) + + if (typeof key == 'object') { + for (k in key) { + if (Object.hasOwnProperty.call(key, k)) { + options.value = key[k] + setProperty(obj, k, options) + } + } + return obj + } + + return setProperty(obj, key, options) + } + + return prr +}) \ No newline at end of file diff --git a/node_modules/prr/test.js b/node_modules/prr/test.js new file mode 100644 index 0000000..5222e30 --- /dev/null +++ b/node_modules/prr/test.js @@ -0,0 +1,169 @@ +const test = require('tap').test + , prr = require('./') + +test('test prr(o, key, value) form', function (t) { + t.plan(2) + + var o = {} + prr(o, 'foo', 'bar') + t.equal(o.foo, 'bar', 'correct value') + t.deepEqual( + Object.getOwnPropertyDescriptor(o, 'foo') + , { + enumerable : false + , configurable : false + , writable : false + , value : 'bar' + } + , 'correct property descriptor' + ) + t.end() +}) + +test('test prr(o, { key: value }) form', function (t) { + t.plan(2) + + var o = {} + prr(o, { foo: 'bar' }) + + t.equal(o.foo, 'bar', 'correct value') + t.deepEqual( + Object.getOwnPropertyDescriptor(o, 'foo') + , { + enumerable : false + , configurable : false + , writable : false + , value : 'bar' + } + , 'correct property descriptor' + ) + t.end() +}) + +test('test multiple key:value pairs', function (t) { + var o = { foo: 'bar' } + + prr(o, { one: 'ONE', two: 'TWO', obj: { o: 'o' }}) + + t.deepEqual(o, { foo: 'bar' }, 'properties are not enumerable') + t.equal(o.one, 'ONE', 'correctly set property') + t.equal(o.two, 'TWO', 'correctly set property') + t.deepEqual(o.obj, { o: 'o' }, 'correctly set property') + + ;[ 'one', 'two', 'obj' ].forEach(function (p) { + t.deepEqual( + Object.getOwnPropertyDescriptor(o, p) + , { + enumerable : false + , configurable : false + , writable : false + , value : p == 'obj' ? { o: 'o' } : p.toUpperCase() + } + , 'correct property descriptor' + ) + }) + + t.end() +}) + +test('test descriptor options', function (t) { + var o = {} + + prr(o, 'foo', 'bar', { + enumerable : true + , configurable : false + }) + t.equal(o.foo, 'bar', 'correct value') + t.deepEqual( + Object.getOwnPropertyDescriptor(o, 'foo') + , { + enumerable : true + , configurable : false + , writable : false + , value : 'bar' + } + , 'correct property descriptor' + ) + + prr(o, 'foo2', 'bar2', { + enumerable : true + , configurable : true + , writable : false + }) + t.equal(o.foo2, 'bar2', 'correct value') + t.deepEqual( + Object.getOwnPropertyDescriptor(o, 'foo2') + , { + enumerable : true + , configurable : true + , writable : false + , value : 'bar2' + } + , 'correct property descriptor' + ) + + prr(o, 'foo3', 'bar3', { + enumerable : true + , configurable : true + , writable : true + }) + t.equal(o.foo3, 'bar3', 'correct value') + t.deepEqual( + Object.getOwnPropertyDescriptor(o, 'foo3') + , { + enumerable : true + , configurable : true + , writable : true + , value : 'bar3' + } + , 'correct property descriptor' + ) + + t.end() +}) + + +test('test descriptor options, string form', function (t) { + var o = {} + + prr(o, 'foo', 'bar', 'e') + t.equal(o.foo, 'bar', 'correct value') + t.deepEqual( + Object.getOwnPropertyDescriptor(o, 'foo') + , { + enumerable : true + , configurable : false + , writable : false + , value : 'bar' + } + , 'correct property descriptor' + ) + + prr(o, 'foo2', 'bar2', 'ec') + t.equal(o.foo2, 'bar2', 'correct value') + t.deepEqual( + Object.getOwnPropertyDescriptor(o, 'foo2') + , { + enumerable : true + , configurable : true + , writable : false + , value : 'bar2' + } + , 'correct property descriptor' + ) + + prr(o, 'foo3', 'bar3', 'ecw') + t.equal(o.foo3, 'bar3', 'correct value') + t.deepEqual( + Object.getOwnPropertyDescriptor(o, 'foo3') + , { + enumerable : true + , configurable : true + , writable : true + , value : 'bar3' + } + , 'correct property descriptor' + ) + + t.end() +}) diff --git a/node_modules/worker-farm/.editorconfig b/node_modules/worker-farm/.editorconfig new file mode 100644 index 0000000..feaebc2 --- /dev/null +++ b/node_modules/worker-farm/.editorconfig @@ -0,0 +1,16 @@ +# This file is for unifying the coding style for different editors and IDEs +# editorconfig.org + +root = true + +[*] +end_of_line = lf +charset = utf-8 +insert_final_newline = true +trim_trailing_whitespace = true +indent_style = space +indent_size = 2 + +[*.js] +max_line_length = 80 +View diff --git a/node_modules/worker-farm/.travis.yml b/node_modules/worker-farm/.travis.yml new file mode 100644 index 0000000..1c9e2b5 --- /dev/null +++ b/node_modules/worker-farm/.travis.yml @@ -0,0 +1,12 @@ +language: node_js +node_js: + - 4 + - 6 + - 8 + - 9 +branches: + only: + - master +notifications: + email: + - rod@vagg.org diff --git a/node_modules/worker-farm/LICENSE.md b/node_modules/worker-farm/LICENSE.md new file mode 100644 index 0000000..274c9b4 --- /dev/null +++ b/node_modules/worker-farm/LICENSE.md @@ -0,0 +1,13 @@ +The MIT License (MIT) +===================== + +Copyright (c) 2014 LevelUP contributors +--------------------------------------- + +*LevelUP contributors listed at * + +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/worker-farm/README.md b/node_modules/worker-farm/README.md new file mode 100644 index 0000000..982b37c --- /dev/null +++ b/node_modules/worker-farm/README.md @@ -0,0 +1,147 @@ +# Worker Farm [![Build Status](https://secure.travis-ci.org/rvagg/node-worker-farm.png)](http://travis-ci.org/rvagg/node-worker-farm) + +[![NPM](https://nodei.co/npm/worker-farm.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/worker-farm/) [![NPM](https://nodei.co/npm-dl/worker-farm.png?months=6&height=3)](https://nodei.co/npm/worker-farm/) + + +Distribute processing tasks to child processes with an über-simple API and baked-in durability & custom concurrency options. *Available in npm as worker-farm*. + +## Example + +Given a file, *child.js*: + +```js +module.exports = function (inp, callback) { + callback(null, inp + ' BAR (' + process.pid + ')') +} +``` + +And a main file: + +```js +var workerFarm = require('worker-farm') + , workers = workerFarm(require.resolve('./child')) + , ret = 0 + +for (var i = 0; i < 10; i++) { + workers('#' + i + ' FOO', function (err, outp) { + console.log(outp) + if (++ret == 10) + workerFarm.end(workers) + }) +} +``` + +We'll get an output something like the following: + +``` +#1 FOO BAR (8546) +#0 FOO BAR (8545) +#8 FOO BAR (8545) +#9 FOO BAR (8546) +#2 FOO BAR (8548) +#4 FOO BAR (8551) +#3 FOO BAR (8549) +#6 FOO BAR (8555) +#5 FOO BAR (8553) +#7 FOO BAR (8557) +``` + +This example is contained in the *[examples/basic](https://github.com/rvagg/node-worker-farm/tree/master/examples/basic/)* directory. + +### Example #1: Estimating π using child workers + +You will also find a more complex example in *[examples/pi](https://github.com/rvagg/node-worker-farm/tree/master/examples/pi/)* that estimates the value of **π** by using a Monte Carlo *area-under-the-curve* method and compares the speed of doing it all in-process vs using child workers to complete separate portions. + +Running `node examples/pi` will give you something like: + +``` +Doing it the slow (single-process) way... +π ≈ 3.1416269360000006 (0.0000342824102075312 away from actual!) +took 8341 milliseconds +Doing it the fast (multi-process) way... +π ≈ 3.1416233600000036 (0.00003070641021052367 away from actual!) +took 1985 milliseconds +``` + +## Durability + +An important feature of Worker Farm is **call durability**. If a child process dies for any reason during the execution of call(s), those calls will be re-queued and taken care of by other child processes. In this way, when you ask for something to be done, unless there is something *seriously* wrong with what you're doing, you should get a result on your callback function. + +## My use-case + +There are other libraries for managing worker processes available but my use-case was fairly specific: I need to make heavy use of the [node-java](https://github.com/nearinfinity/node-java) library to interact with JVM code. Unfortunately, because the JVM garbage collector is so difficult to interact with, it's prone to killing your Node process when the GC kicks under heavy load. For safety I needed a durable way to make calls so that (1) it wouldn't kill my main process and (2) any calls that weren't successful would be resubmitted for processing. + +Worker Farm allows me to spin up multiple JVMs to be controlled by Node, and have a single, uncomplicated API that acts the same way as an in-process API and the calls will be taken care of by a child process even if an error kills a child process while it is working as the call will simply be passed to a new child process. + +**But**, don't think that Worker Farm is specific to that use-case, it's designed to be very generic and simple to adapt to anything requiring the use of child Node processes. + +## API + +Worker Farm exports a main function and an `end()` method. The main function sets up a "farm" of coordinated child-process workers and it can be used to instantiate multiple farms, all operating independently. + +### workerFarm([options, ]pathToModule[, exportedMethods]) + +In its most basic form, you call `workerFarm()` with the path to a module file to be invoked by the child process. You should use an **absolute path** to the module file, the best way to obtain the path is with `require.resolve('./path/to/module')`, this function can be used in exactly the same way as `require('./path/to/module')` but it returns an absolute path. + +#### `exportedMethods` + +If your module exports a single function on `module.exports` then you should omit the final parameter. However, if you are exporting multiple functions on `module.exports` then you should list them in an Array of Strings: + +```js +var workers = workerFarm(require.resolve('./mod'), [ 'doSomething', 'doSomethingElse' ]) +workers.doSomething(function () {}) +workers.doSomethingElse(function () {}) +``` + +Listing the available methods will instruct Worker Farm what API to provide you with on the returned object. If you don't list a `exportedMethods` Array then you'll get a single callable function to use; but if you list the available methods then you'll get an object with callable functions by those names. + +**It is assumed that each function you call on your child module will take a `callback` function as the last argument.** + +#### `options` + +If you don't provide an `options` object then the following defaults will be used: + +```js +{ + workerOptions : {} + , maxCallsPerWorker : Infinity + , maxConcurrentWorkers : require('os').cpus().length + , maxConcurrentCallsPerWorker : 10 + , maxConcurrentCalls : Infinity + , maxCallTime : Infinity + , maxRetries : Infinity + , autoStart : false +} +``` + + * **workerOptions** allows you to customize all the parameters passed to child nodes. This object supports [all possible options of `child_process.fork`](https://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options). The default options passed are the parent `execArgv`, `cwd` and `env`. Any (or all) of them can be overridden, and others can be added as well. + + * **maxCallsPerWorker** allows you to control the lifespan of your child processes. A positive number will indicate that you only want each child to accept that many calls before it is terminated. This may be useful if you need to control memory leaks or similar in child processes. + + * **maxConcurrentWorkers** will set the number of child processes to maintain concurrently. By default it is set to the number of CPUs available on the current system, but it can be any reasonable number, including `1`. + + * **maxConcurrentCallsPerWorker** allows you to control the *concurrency* of individual child processes. Calls are placed into a queue and farmed out to child processes according to the number of calls they are allowed to handle concurrently. It is arbitrarily set to 10 by default so that calls are shared relatively evenly across workers, however if your calls predictably take a similar amount of time then you could set it to `Infinity` and Worker Farm won't queue any calls but spread them evenly across child processes and let them go at it. If your calls aren't I/O bound then it won't matter what value you use here as the individual workers won't be able to execute more than a single call at a time. + + * **maxConcurrentCalls** allows you to control the maximum number of calls in the queue—either actively being processed or waiting for a worker to be processed. `Infinity` indicates no limit but if you have conditions that may endlessly queue jobs and you need to set a limit then provide a `>0` value and any calls that push the limit will return on their callback with a `MaxConcurrentCallsError` error (check `err.type == 'MaxConcurrentCallsError'`). + + * **maxCallTime** *(use with caution, understand what this does before you use it!)* when `!== Infinity`, will cap a time, in milliseconds, that *any single call* can take to execute in a worker. If this time limit is exceeded by just a single call then the worker running that call will be killed and any calls running on that worker will have their callbacks returned with a `TimeoutError` (check `err.type == 'TimeoutError'`). If you are running with `maxConcurrentCallsPerWorker` value greater than `1` then **all calls currently executing** will fail and will be automatically resubmitted uless you've changed the `maxRetries` option. Use this if you have jobs that may potentially end in infinite loops that you can't programatically end with your child code. Preferably run this with a `maxConcurrentCallsPerWorker` so you don't interrupt other calls when you have a timeout. This timeout operates on a per-call basis but will interrupt a whole worker. + + * **maxRetries** allows you to control the max number of call requeues after worker termination (unexpected or timeout). By default this option is set to `Infinity` which means that each call of each terminated worker will always be auto requeued. When the number of retries exceeds `maxRetries` value, the job callback will be executed with a `ProcessTerminatedError`. Note that if you are running with finite `maxCallTime` and `maxConcurrentCallsPerWorkers` greater than `1` then any `TimeoutError` will increase the retries counter *for each* concurrent call of the terminated worker. + + * **autoStart** when set to `true` will start the workers as early as possible. Use this when your workers have to do expensive initialization. That way they'll be ready when the first request comes through. + +### workerFarm.end(farm) + +Child processes stay alive waiting for jobs indefinitely and your farm manager will stay alive managing its workers, so if you need it to stop then you have to do so explicitly. If you send your farm API to `workerFarm.end()` then it'll cleanly end your worker processes. Note though that it's a *soft* ending so it'll wait for child processes to finish what they are working on before asking them to die. + +Any calls that are queued and not yet being handled by a child process will be discarded. `end()` only waits for those currently in progress. + +Once you end a farm, it won't handle any more calls, so don't even try! + +## Related + +* [farm-cli](https://github.com/Kikobeats/farm-cli) – Launch a farm of workers from CLI. + +## License + +Worker Farm is Copyright (c) 2014 Rod Vagg [@rvagg](https://twitter.com/rvagg) and licensed under the MIT license. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE.md file for more details. diff --git a/node_modules/worker-farm/examples/basic/child.js b/node_modules/worker-farm/examples/basic/child.js new file mode 100644 index 0000000..0f0900b --- /dev/null +++ b/node_modules/worker-farm/examples/basic/child.js @@ -0,0 +1,5 @@ +'use strict' + +module.exports = function (inp, callback) { + callback(null, inp + ' BAR (' + process.pid + ')') +} diff --git a/node_modules/worker-farm/examples/basic/index.js b/node_modules/worker-farm/examples/basic/index.js new file mode 100644 index 0000000..452e7b1 --- /dev/null +++ b/node_modules/worker-farm/examples/basic/index.js @@ -0,0 +1,13 @@ +'use strict' + +let workerFarm = require('../../') + , workers = workerFarm(require.resolve('./child')) + , ret = 0 + +for (let i = 0; i < 10; i++) { + workers('#' + i + ' FOO', function (err, outp) { + console.log(outp) + if (++ret == 10) + workerFarm.end(workers) + }) +} diff --git a/node_modules/worker-farm/examples/pi/calc.js b/node_modules/worker-farm/examples/pi/calc.js new file mode 100644 index 0000000..df9e5ba --- /dev/null +++ b/node_modules/worker-farm/examples/pi/calc.js @@ -0,0 +1,22 @@ +'use strict' + +/* A simple π estimation function using a Monte Carlo method + * For 0 to `points`, take 2 random numbers < 1, square and add them to + * find the area under that point in a 1x1 square. If that area is <= 1 + * then it's *within* a quarter-circle, otherwise it's outside. + * Take the number of points <= 1 and multiply it by 4 and you have an + * estimate! + * Do this across multiple processes and average the results to + * increase accuracy. + */ + +module.exports = function (points, callback) { + let inside = 0 + , i = points + + while (i--) + if (Math.pow(Math.random(), 2) + Math.pow(Math.random(), 2) <= 1) + inside++ + + callback(null, (inside / points) * 4) +} diff --git a/node_modules/worker-farm/examples/pi/index.js b/node_modules/worker-farm/examples/pi/index.js new file mode 100644 index 0000000..b7b2683 --- /dev/null +++ b/node_modules/worker-farm/examples/pi/index.js @@ -0,0 +1,41 @@ +'use strict' + +const CHILDREN = 500 + , POINTS_PER_CHILD = 1000000 + , FARM_OPTIONS = { + maxConcurrentWorkers : require('os').cpus().length + , maxCallsPerWorker : Infinity + , maxConcurrentCallsPerWorker : 1 + } + +let workerFarm = require('../../') + , calcDirect = require('./calc') + , calcWorker = workerFarm(FARM_OPTIONS, require.resolve('./calc')) + + , ret + , start + + , tally = function (finish, err, avg) { + ret.push(avg) + if (ret.length == CHILDREN) { + let pi = ret.reduce(function (a, b) { return a + b }) / ret.length + , end = +new Date() + console.log('π ≈', pi, '\t(' + Math.abs(pi - Math.PI), 'away from actual!)') + console.log('took', end - start, 'milliseconds') + if (finish) + finish() + } + } + + , calc = function (method, callback) { + ret = [] + start = +new Date() + for (let i = 0; i < CHILDREN; i++) + method(POINTS_PER_CHILD, tally.bind(null, callback)) + } + +console.log('Doing it the slow (single-process) way...') +calc(calcDirect, function () { + console.log('Doing it the fast (multi-process) way...') + calc(calcWorker, process.exit) +}) diff --git a/node_modules/worker-farm/index.d.ts b/node_modules/worker-farm/index.d.ts new file mode 100644 index 0000000..682c21f --- /dev/null +++ b/node_modules/worker-farm/index.d.ts @@ -0,0 +1,44 @@ +interface Workers { + (callback: WorkerCallback): void; + (arg1: any, callback: WorkerCallback): void; + (arg1: any, arg2: any, callback: WorkerCallback): void; + (arg1: any, arg2: any, arg3: any, callback: WorkerCallback): void; + (arg1: any, arg2: any, arg3: any, arg4: any, callback: WorkerCallback): void; +} + +type WorkerCallback = + | WorkerCallback0 + | WorkerCallback1 + | WorkerCallback2 + | WorkerCallback3 + | WorkerCallback4; + +type WorkerCallback0 = () => void; +type WorkerCallback1 = (arg1: any) => void; +type WorkerCallback2 = (arg1: any, arg2: any) => void; +type WorkerCallback3 = (arg1: any, arg2: any, arg3: any) => void; +type WorkerCallback4 = (arg1: any, arg2: any, arg3: any, arg4: any) => void; + +interface FarmOptions { + maxCallsPerWorker?: number + maxConcurrentWorkers?: number + maxConcurrentCallsPerWorker?: number + maxConcurrentCalls?: number + maxCallTime?: number + maxRetries?: number + autoStart?: boolean +} + +interface WorkerFarm { + (name: string): Workers; + (name: string, exportedMethods: string[]): Workers; + (options: FarmOptions, name: string): Workers; + (options: FarmOptions, name: string, exportedMethods: string[]): Workers; + + end: (workers: Workers) => void; +} + +declare module "worker-farm" { + const workerFarm: WorkerFarm; + export = workerFarm; +} diff --git a/node_modules/worker-farm/lib/child/index.js b/node_modules/worker-farm/lib/child/index.js new file mode 100644 index 0000000..f91e084 --- /dev/null +++ b/node_modules/worker-farm/lib/child/index.js @@ -0,0 +1,52 @@ +'use strict' + +let $module + +/* + let contextProto = this.context; + while (contextProto = Object.getPrototypeOf(contextProto)) { + completionGroups.push(Object.getOwnPropertyNames(contextProto)); + } +*/ + + +function handle (data) { + let idx = data.idx + , child = data.child + , method = data.method + , args = data.args + , callback = function () { + let _args = Array.prototype.slice.call(arguments) + if (_args[0] instanceof Error) { + let e = _args[0] + _args[0] = { + '$error' : '$error' + , 'type' : e.constructor.name + , 'message' : e.message + , 'stack' : e.stack + } + Object.keys(e).forEach(function(key) { + _args[0][key] = e[key] + }) + } + process.send({ idx: idx, child: child, args: _args }) + } + , exec + + if (method == null && typeof $module == 'function') + exec = $module + else if (typeof $module[method] == 'function') + exec = $module[method] + + if (!exec) + return console.error('NO SUCH METHOD:', method) + + exec.apply(null, args.concat([ callback ])) +} + + +process.on('message', function (data) { + if (!$module) return $module = require(data.module) + if (data == 'die') return process.exit(0) + handle(data) +}) diff --git a/node_modules/worker-farm/lib/farm.js b/node_modules/worker-farm/lib/farm.js new file mode 100644 index 0000000..ef0ab0e --- /dev/null +++ b/node_modules/worker-farm/lib/farm.js @@ -0,0 +1,339 @@ +'use strict' + +const DEFAULT_OPTIONS = { + workerOptions : {} + , maxCallsPerWorker : Infinity + , maxConcurrentWorkers : (require('os').cpus() || { length: 1 }).length + , maxConcurrentCallsPerWorker : 10 + , maxConcurrentCalls : Infinity + , maxCallTime : Infinity // exceed this and the whole worker is terminated + , maxRetries : Infinity + , forcedKillTime : 100 + , autoStart : false + } + +const fork = require('./fork') + , TimeoutError = require('errno').create('TimeoutError') + , ProcessTerminatedError = require('errno').create('ProcessTerminatedError') + , MaxConcurrentCallsError = require('errno').create('MaxConcurrentCallsError') + + +function Farm (options, path) { + this.options = Object.assign({}, DEFAULT_OPTIONS, options) + this.path = path + this.activeCalls = 0 +} + + +// make a handle to pass back in the form of an external API +Farm.prototype.mkhandle = function (method) { + return function () { + let args = Array.prototype.slice.call(arguments) + if (this.activeCalls >= this.options.maxConcurrentCalls) { + let err = new MaxConcurrentCallsError('Too many concurrent calls (' + this.activeCalls + ')') + if (typeof args[args.length - 1] == 'function') + return process.nextTick(args[args.length - 1].bind(null, err)) + throw err + } + this.addCall({ + method : method + , callback : args.pop() + , args : args + , retries : 0 + }) + }.bind(this) +} + + +// a constructor of sorts +Farm.prototype.setup = function (methods) { + let iface + if (!methods) { // single-function export + iface = this.mkhandle() + } else { // multiple functions on the export + iface = {} + methods.forEach(function (m) { + iface[m] = this.mkhandle(m) + }.bind(this)) + } + + this.searchStart = -1 + this.childId = -1 + this.children = {} + this.activeChildren = 0 + this.callQueue = [] + + if (this.options.autoStart) { + while (this.activeChildren < this.options.maxConcurrentWorkers) + this.startChild() + } + + return iface +} + + +// when a child exits, check if there are any outstanding jobs and requeue them +Farm.prototype.onExit = function (childId) { + // delay this to give any sends a chance to finish + setTimeout(function () { + let doQueue = false + if (this.children[childId] && this.children[childId].activeCalls) { + this.children[childId].calls.forEach(function (call, i) { + if (!call) return + else if (call.retries >= this.options.maxRetries) { + this.receive({ + idx : i + , child : childId + , args : [ new ProcessTerminatedError('cancel after ' + call.retries + ' retries!') ] + }) + } else { + call.retries++ + this.callQueue.unshift(call) + doQueue = true + } + }.bind(this)) + } + this.stopChild(childId) + doQueue && this.processQueue() + }.bind(this), 10) +} + + +// start a new worker +Farm.prototype.startChild = function () { + this.childId++ + + let forked = fork(this.path, this.options.workerOptions) + , id = this.childId + , c = { + send : forked.send + , child : forked.child + , calls : [] + , activeCalls : 0 + , exitCode : null + } + + forked.child.on('message', this.receive.bind(this)) + forked.child.once('exit', function (code) { + c.exitCode = code + this.onExit(id) + }.bind(this)) + + this.activeChildren++ + this.children[id] = c +} + + +// stop a worker, identified by id +Farm.prototype.stopChild = function (childId) { + let child = this.children[childId] + if (child) { + child.send('die') + setTimeout(function () { + if (child.exitCode === null) + child.child.kill('SIGKILL') + }, this.options.forcedKillTime).unref() + ;delete this.children[childId] + this.activeChildren-- + } +} + + +// called from a child process, the data contains information needed to +// look up the child and the original call so we can invoke the callback +Farm.prototype.receive = function (data) { + let idx = data.idx + , childId = data.child + , args = data.args + , child = this.children[childId] + , call + + if (!child) { + return console.error( + 'Worker Farm: Received message for unknown child. ' + + 'This is likely as a result of premature child death, ' + + 'the operation will have been re-queued.' + ) + } + + call = child.calls[idx] + if (!call) { + return console.error( + 'Worker Farm: Received message for unknown index for existing child. ' + + 'This should not happen!' + ) + } + + if (this.options.maxCallTime !== Infinity) + clearTimeout(call.timer) + + if (args[0] && args[0].$error == '$error') { + let e = args[0] + switch (e.type) { + case 'TypeError': args[0] = new TypeError(e.message); break + case 'RangeError': args[0] = new RangeError(e.message); break + case 'EvalError': args[0] = new EvalError(e.message); break + case 'ReferenceError': args[0] = new ReferenceError(e.message); break + case 'SyntaxError': args[0] = new SyntaxError(e.message); break + case 'URIError': args[0] = new URIError(e.message); break + default: args[0] = new Error(e.message) + } + args[0].type = e.type + args[0].stack = e.stack + + // Copy any custom properties to pass it on. + Object.keys(e).forEach(function(key) { + args[0][key] = e[key]; + }); + } + + process.nextTick(function () { + call.callback.apply(null, args) + }) + + ;delete child.calls[idx] + child.activeCalls-- + this.activeCalls-- + + if (child.calls.length >= this.options.maxCallsPerWorker + && !Object.keys(child.calls).length) { + // this child has finished its run, kill it + this.stopChild(childId) + } + + // allow any outstanding calls to be processed + this.processQueue() +} + + +Farm.prototype.childTimeout = function (childId) { + let child = this.children[childId] + , i + + if (!child) + return + + for (i in child.calls) { + this.receive({ + idx : i + , child : childId + , args : [ new TimeoutError('worker call timed out!') ] + }) + } + this.stopChild(childId) +} + + +// send a call to a worker, identified by id +Farm.prototype.send = function (childId, call) { + let child = this.children[childId] + , idx = child.calls.length + + child.calls.push(call) + child.activeCalls++ + this.activeCalls++ + + child.send({ + idx : idx + , child : childId + , method : call.method + , args : call.args + }) + + if (this.options.maxCallTime !== Infinity) { + call.timer = + setTimeout(this.childTimeout.bind(this, childId), this.options.maxCallTime) + } +} + + +// a list of active worker ids, in order, but the starting offset is +// shifted each time this method is called, so we work our way through +// all workers when handing out jobs +Farm.prototype.childKeys = function () { + let cka = Object.keys(this.children) + , cks + + if (this.searchStart >= cka.length - 1) + this.searchStart = 0 + else + this.searchStart++ + + cks = cka.splice(0, this.searchStart) + + return cka.concat(cks) +} + + +// Calls are added to a queue, this processes the queue and is called +// whenever there might be a chance to send more calls to the workers. +// The various options all impact on when we're able to send calls, +// they may need to be kept in a queue until a worker is ready. +Farm.prototype.processQueue = function () { + let cka, i = 0, childId + + if (!this.callQueue.length) + return this.ending && this.end() + + if (this.activeChildren < this.options.maxConcurrentWorkers) + this.startChild() + + for (cka = this.childKeys(); i < cka.length; i++) { + childId = +cka[i] + if (this.children[childId].activeCalls < this.options.maxConcurrentCallsPerWorker + && this.children[childId].calls.length < this.options.maxCallsPerWorker) { + + this.send(childId, this.callQueue.shift()) + if (!this.callQueue.length) + return this.ending && this.end() + } /*else { + console.log( + , this.children[childId].activeCalls < this.options.maxConcurrentCallsPerWorker + , this.children[childId].calls.length < this.options.maxCallsPerWorker + , this.children[childId].calls.length , this.options.maxCallsPerWorker) + }*/ + } + + if (this.ending) + this.end() +} + + +// add a new call to the call queue, then trigger a process of the queue +Farm.prototype.addCall = function (call) { + if (this.ending) + return this.end() // don't add anything new to the queue + this.callQueue.push(call) + this.processQueue() +} + + +// kills child workers when they're all done +Farm.prototype.end = function (callback) { + let complete = true + if (this.ending === false) + return + if (callback) + this.ending = callback + else if (this.ending == null) + this.ending = true + Object.keys(this.children).forEach(function (child) { + if (!this.children[child]) + return + if (!this.children[child].activeCalls) + this.stopChild(child) + else + complete = false + }.bind(this)) + + if (complete && typeof this.ending == 'function') { + process.nextTick(function () { + this.ending() + this.ending = false + }.bind(this)) + } +} + + +module.exports = Farm +module.exports.TimeoutError = TimeoutError diff --git a/node_modules/worker-farm/lib/fork.js b/node_modules/worker-farm/lib/fork.js new file mode 100644 index 0000000..2843df4 --- /dev/null +++ b/node_modules/worker-farm/lib/fork.js @@ -0,0 +1,33 @@ +'use strict' + +const childProcess = require('child_process') + , childModule = require.resolve('./child/index') + + +function fork (forkModule, workerOptions) { + // suppress --debug / --inspect flags while preserving others (like --harmony) + let filteredArgs = process.execArgv.filter(function (v) { + return !(/^--(debug|inspect)/).test(v) + }) + , options = Object.assign({ + execArgv : filteredArgs + , env : process.env + , cwd : process.cwd() + }, workerOptions) + , child = childProcess.fork(childModule, process.argv, options) + + child.on('error', function() { + // this *should* be picked up by onExit and the operation requeued + }) + + child.send({ module: forkModule }) + + // return a send() function for this child + return { + send : child.send.bind(child) + , child : child + } +} + + +module.exports = fork diff --git a/node_modules/worker-farm/lib/index.js b/node_modules/worker-farm/lib/index.js new file mode 100644 index 0000000..fe574e5 --- /dev/null +++ b/node_modules/worker-farm/lib/index.js @@ -0,0 +1,34 @@ +'use strict' + +const Farm = require('./farm') + +let farms = [] // keep record of farms so we can end() them if required + + +function farm (options, path, methods) { + if (typeof options == 'string') { + methods = path + path = options + options = {} + } + + let f = new Farm(options, path) + , api = f.setup(methods) + + farms.push({ farm: f, api: api }) + + // return the public API + return api +} + + +function end (api, callback) { + for (let i = 0; i < farms.length; i++) + if (farms[i] && farms[i].api === api) + return farms[i].farm.end(callback) + process.nextTick(callback.bind(null, 'Worker farm not found!')) +} + + +module.exports = farm +module.exports.end = end diff --git a/node_modules/worker-farm/package.json b/node_modules/worker-farm/package.json new file mode 100644 index 0000000..552e17c --- /dev/null +++ b/node_modules/worker-farm/package.json @@ -0,0 +1,93 @@ +{ + "_args": [ + [ + "worker-farm", + "/mnt/c/Users/Josua/Desktop/data/nodejs/electron/chat-project/chat-server" + ] + ], + "_from": "worker-farm@latest", + "_id": "worker-farm@1.6.0", + "_inCache": true, + "_installable": true, + "_location": "/worker-farm", + "_nodeVersion": "8.10.0", + "_npmOperationalInternal": { + "host": "s3://npm-registry-packages", + "tmp": "tmp/worker-farm_1.6.0_1520565271783_0.5530085286716173" + }, + "_npmUser": { + "email": "r@va.gg", + "name": "rvagg" + }, + "_npmVersion": "5.6.0", + "_phantomChildren": {}, + "_requested": { + "name": "worker-farm", + "raw": "worker-farm", + "rawSpec": "", + "scope": null, + "spec": "latest", + "type": "tag" + }, + "_requiredBy": [ + "#USER" + ], + "_resolved": "https://registry.npmjs.org/worker-farm/-/worker-farm-1.6.0.tgz", + "_shasum": "aecc405976fab5a95526180846f0dba288f3a4a0", + "_shrinkwrap": null, + "_spec": "worker-farm", + "_where": "/mnt/c/Users/Josua/Desktop/data/nodejs/electron/chat-project/chat-server", + "authors": [ + "Rod Vagg @rvagg (https://github.com/rvagg)" + ], + "bugs": { + "url": "https://github.com/rvagg/node-worker-farm/issues" + }, + "dependencies": { + "errno": "~0.1.7" + }, + "description": "Distribute processing tasks to child processes with an über-simple API and baked-in durability & custom concurrency options.", + "devDependencies": { + "tape": "~4.9.0" + }, + "directories": {}, + "dist": { + "fileCount": 17, + "integrity": "sha512-6w+3tHbM87WnSWnENBUvA2pxJPLhQUg5LKwUQHq3r+XPhIM+Gh2R5ycbwPCyuGbNg+lPgdcnQUhuC02kJCvffQ==", + "shasum": "aecc405976fab5a95526180846f0dba288f3a4a0", + "tarball": "https://registry.npmjs.org/worker-farm/-/worker-farm-1.6.0.tgz", + "unpackedSize": 47168 + }, + "gitHead": "f61372e0ad99338d472b8fd09b4900a4cbff5166", + "homepage": "https://github.com/rvagg/node-worker-farm", + "keywords": [ + "child", + "farm", + "processing", + "worker" + ], + "license": "MIT", + "main": "./lib/index.js", + "maintainers": [ + { + "name": "amasad", + "email": "amjad.masad@gmail.com" + }, + { + "name": "rvagg", + "email": "rod@vagg.org" + } + ], + "name": "worker-farm", + "optionalDependencies": {}, + "readme": "ERROR: No README data found!", + "repository": { + "type": "git", + "url": "git+https://github.com/rvagg/node-worker-farm.git" + }, + "scripts": { + "test": "node ./tests/" + }, + "types": "./index.d.ts", + "version": "1.6.0" +} diff --git a/node_modules/worker-farm/tests/child.js b/node_modules/worker-farm/tests/child.js new file mode 100644 index 0000000..71cb728 --- /dev/null +++ b/node_modules/worker-farm/tests/child.js @@ -0,0 +1,87 @@ +'use strict' + +const fs = require('fs') +const started = Date.now() + + +module.exports = function (timeout, callback) { + callback = callback.bind(null, null, process.pid, Math.random(), timeout) + if (timeout) + return setTimeout(callback, timeout) + callback() +} + + +module.exports.args = function (callback) { + callback(null, { + argv : process.argv + , cwd : process.cwd() + , execArgv : process.execArgv + }) +} + + +module.exports.run0 = function (callback) { + module.exports(0, callback) +} + + +module.exports.killable = function (id, callback) { + if (Math.random() < 0.5) + return process.exit(-1) + callback(null, id, process.pid) +} + + +module.exports.err = function (type, message, data, callback) { + if (typeof data == 'function') { + callback = data + data = null + } else { + let err = new Error(message) + Object.keys(data).forEach(function(key) { + err[key] = data[key] + }) + callback(err) + return + } + + if (type == 'TypeError') + return callback(new TypeError(message)) + callback(new Error(message)) +} + + +module.exports.block = function () { + while (true); +} + + +// use provided file path to save retries count among terminated workers +module.exports.stubborn = function (path, callback) { + function isOutdated(path) { + return ((new Date).getTime() - fs.statSync(path).mtime.getTime()) > 2000 + } + + // file may not be properly deleted, check if modified no earler than two seconds ago + if (!fs.existsSync(path) || isOutdated(path)) { + fs.writeFileSync(path, '1') + process.exit(-1) + } + + let retry = parseInt(fs.readFileSync(path, 'utf8')) + if (Number.isNaN(retry)) + return callback(new Error('file contents is not a number')) + + if (retry > 4) { + callback(null, 12) + } else { + fs.writeFileSync(path, String(retry + 1)) + process.exit(-1) + } +} + + +module.exports.uptime = function (callback) { + callback(null, Date.now() - started) +} diff --git a/node_modules/worker-farm/tests/debug.js b/node_modules/worker-farm/tests/debug.js new file mode 100644 index 0000000..4d2b803 --- /dev/null +++ b/node_modules/worker-farm/tests/debug.js @@ -0,0 +1,12 @@ +'use strict' + +const workerFarm = require('../') + , workers = workerFarm(require.resolve('./child'), ['args']) + + +workers.args(function(err, result) { + console.log(result); + workerFarm.end(workers) + console.log('FINISHED') + process.exit(0) +}) diff --git a/node_modules/worker-farm/tests/index.js b/node_modules/worker-farm/tests/index.js new file mode 100644 index 0000000..ec9deab --- /dev/null +++ b/node_modules/worker-farm/tests/index.js @@ -0,0 +1,564 @@ +'use strict' + +const tape = require('tape') + , child_process = require('child_process') + , workerFarm = require('../') + , childPath = require.resolve('./child') + , fs = require('fs') + , os = require('os') + +function uniq (ar) { + let a = [], i, j + o: for (i = 0; i < ar.length; ++i) { + for (j = 0; j < a.length; ++j) if (a[j] == ar[i]) continue o + a[a.length] = ar[i] + } + return a +} + + +// a child where module.exports = function ... +tape('simple, exports=function test', function (t) { + t.plan(4) + + let child = workerFarm(childPath) + child(0, function (err, pid, rnd) { + t.ok(pid > process.pid, 'pid makes sense') + t.ok(pid < process.pid + 750, 'pid makes sense') + t.ok(rnd >= 0 && rnd < 1, 'rnd result makes sense') + }) + + workerFarm.end(child, function () { + t.ok(true, 'workerFarm ended') + }) +}) + + +// a child where we have module.exports.fn = function ... +tape('simple, exports.fn test', function (t) { + t.plan(4) + + let child = workerFarm(childPath, [ 'run0' ]) + child.run0(function (err, pid, rnd) { + t.ok(pid > process.pid, 'pid makes sense') + t.ok(pid < process.pid + 750, 'pid makes sense') + t.ok(rnd >= 0 && rnd < 1, 'rnd result makes sense') + }) + + workerFarm.end(child, function () { + t.ok(true, 'workerFarm ended') + }) +}) + + +// use the returned pids to check that we're using a single child process +// when maxConcurrentWorkers = 1 +tape('single worker', function (t) { + t.plan(2) + + let child = workerFarm({ maxConcurrentWorkers: 1 }, childPath) + , pids = [] + , i = 10 + + while (i--) { + child(0, function (err, pid) { + pids.push(pid) + if (pids.length == 10) { + t.equal(1, uniq(pids).length, 'only a single process (by pid)') + } else if (pids.length > 10) + t.fail('too many callbacks!') + }) + } + + workerFarm.end(child, function () { + t.ok(true, 'workerFarm ended') + }) +}) + + +// use the returned pids to check that we're using two child processes +// when maxConcurrentWorkers = 2 +tape('two workers', function (t) { + t.plan(2) + + let child = workerFarm({ maxConcurrentWorkers: 2 }, childPath) + , pids = [] + , i = 10 + + while (i--) { + child(0, function (err, pid) { + pids.push(pid) + if (pids.length == 10) { + t.equal(2, uniq(pids).length, 'only two child processes (by pid)') + } else if (pids.length > 10) + t.fail('too many callbacks!') + }) + } + + workerFarm.end(child, function () { + t.ok(true, 'workerFarm ended') + }) +}) + + +// use the returned pids to check that we're using a child process per +// call when maxConcurrentWorkers = 10 +tape('many workers', function (t) { + t.plan(2) + + let child = workerFarm({ maxConcurrentWorkers: 10 }, childPath) + , pids = [] + , i = 10 + + while (i--) { + child(1, function (err, pid) { + pids.push(pid) + if (pids.length == 10) { + t.equal(10, uniq(pids).length, 'pids are all the same (by pid)') + } else if (pids.length > 10) + t.fail('too many callbacks!') + }) + } + + workerFarm.end(child, function () { + t.ok(true, 'workerFarm ended') + }) +}) + + +tape('auto start workers', function (t) { + let child = workerFarm({ maxConcurrentWorkers: 3, autoStart: true }, childPath, ['uptime']) + , pids = [] + , count = 5 + , i = count + , delay = 250 + + t.plan(count + 1) + + setTimeout(function() { + while (i--) + child.uptime(function (err, uptime) { + t.ok(uptime > 10, 'child has been up before the request (' + uptime + 'ms)') + }) + + workerFarm.end(child, function () { + t.ok(true, 'workerFarm ended') + }) + }, delay) +}) + + +// use the returned pids to check that we're using a child process per +// call when we set maxCallsPerWorker = 1 even when we have maxConcurrentWorkers = 1 +tape('single call per worker', function (t) { + t.plan(2) + + let child = workerFarm({ + maxConcurrentWorkers: 1 + , maxConcurrentCallsPerWorker: Infinity + , maxCallsPerWorker: 1 + , autoStart: true + }, childPath) + , pids = [] + , count = 25 + , i = count + + while (i--) { + child(0, function (err, pid) { + pids.push(pid) + if (pids.length == count) { + t.equal(count, uniq(pids).length, 'one process for each call (by pid)') + workerFarm.end(child, function () { + t.ok(true, 'workerFarm ended') + }) + } else if (pids.length > count) + t.fail('too many callbacks!') + }) + } +}) + + +// use the returned pids to check that we're using a child process per +// two-calls when we set maxCallsPerWorker = 2 even when we have maxConcurrentWorkers = 1 +tape('two calls per worker', function (t) { + t.plan(2) + + let child = workerFarm({ + maxConcurrentWorkers: 1 + , maxConcurrentCallsPerWorker: Infinity + , maxCallsPerWorker: 2 + , autoStart: true + }, childPath) + , pids = [] + , count = 20 + , i = count + + while (i--) { + child(0, function (err, pid) { + pids.push(pid) + if (pids.length == count) { + t.equal(count / 2, uniq(pids).length, 'one process for each call (by pid)') + workerFarm.end(child, function () { + t.ok(true, 'workerFarm ended') + }) + } else if (pids.length > count) + t.fail('too many callbacks!') + }) + } +}) + + +// use timing to confirm that one worker will process calls sequentially +tape('many concurrent calls', function (t) { + t.plan(2) + + let child = workerFarm({ + maxConcurrentWorkers: 1 + , maxConcurrentCallsPerWorker: Infinity + , maxCallsPerWorker: Infinity + , autoStart: true + }, childPath) + , defer = 200 + , count = 200 + , i = count + , cbc = 0 + + setTimeout(function () { + let start = Date.now() + + while (i--) { + child(defer, function () { + if (++cbc == count) { + let time = Date.now() - start + // upper-limit not tied to `count` at all + t.ok(time > defer && time < (defer * 2.5), 'processed tasks concurrently (' + time + 'ms)') + workerFarm.end(child, function () { + t.ok(true, 'workerFarm ended') + }) + } else if (cbc > count) + t.fail('too many callbacks!') + }) + } + }, 250) +}) + + +// use timing to confirm that one child processes calls sequentially with +// maxConcurrentCallsPerWorker = 1 +tape('single concurrent call', function (t) { + t.plan(2) + + let child = workerFarm({ + maxConcurrentWorkers: 1 + , maxConcurrentCallsPerWorker: 1 + , maxCallsPerWorker: Infinity + , autoStart: true + }, childPath) + , defer = 20 + , count = 100 + , i = count + , cbc = 0 + + setTimeout(function () { + let start = Date.now() + + while (i--) { + child(defer, function () { + if (++cbc == count) { + let time = Date.now() - start + // upper-limit tied closely to `count`, 1.3 is generous but accounts for all the timers + // coming back at the same time and the IPC overhead + t.ok(time > (defer * count) && time < (defer * count * 1.3), 'processed tasks sequentially (' + time + ')') + workerFarm.end(child, function () { + t.ok(true, 'workerFarm ended') + }) + } else if (cbc > count) + t.fail('too many callbacks!') + }) + } + }, 250) +}) + + +// use timing to confirm that one child processes *only* 5 calls concurrently +tape('multiple concurrent calls', function (t) { + t.plan(2) + + let callsPerWorker = 5 + , child = workerFarm({ + maxConcurrentWorkers: 1 + , maxConcurrentCallsPerWorker: callsPerWorker + , maxCallsPerWorker: Infinity + , autoStart: true + }, childPath) + , defer = 100 + , count = 100 + , i = count + , cbc = 0 + + setTimeout(function () { + let start = Date.now() + + while (i--) { + child(defer, function () { + if (++cbc == count) { + let time = Date.now() - start + // (defer * (count / callsPerWorker + 1)) - if precise it'd be count/callsPerWorker + // but accounting for IPC and other overhead, we need to give it a bit of extra time, + // hence the +1 + t.ok(time > (defer * 1.5) && time < (defer * (count / callsPerWorker + 1)), 'processed tasks concurrently (' + time + 'ms)') + workerFarm.end(child, function () { + t.ok(true, 'workerFarm ended') + }) + } else if (cbc > count) + t.fail('too many callbacks!') + }) + } + }, 250) +}) + + +// call a method that will die with a probability of 0.5 but expect that +// we'll get results for each of our calls anyway +tape('durability', function (t) { + t.plan(3) + + let child = workerFarm({ maxConcurrentWorkers: 2 }, childPath, [ 'killable' ]) + , ids = [] + , pids = [] + , count = 20 + , i = count + + while (i--) { + child.killable(i, function (err, id, pid) { + ids.push(id) + pids.push(pid) + if (ids.length == count) { + t.ok(uniq(pids).length > 2, 'processed by many (' + uniq(pids).length + ') workers, but got there in the end!') + t.ok(uniq(ids).length == count, 'received a single result for each unique call') + workerFarm.end(child, function () { + t.ok(true, 'workerFarm ended') + }) + } else if (ids.length > count) + t.fail('too many callbacks!') + }) + } +}) + + +// a callback provided to .end() can and will be called (uses "simple, exports=function test" to create a child) +tape('simple, end callback', function (t) { + t.plan(4) + + let child = workerFarm(childPath) + child(0, function (err, pid, rnd) { + t.ok(pid > process.pid, 'pid makes sense ' + pid + ' vs ' + process.pid) + t.ok(pid < process.pid + 750, 'pid makes sense ' + pid + ' vs ' + process.pid) + t.ok(rnd >= 0 && rnd < 1, 'rnd result makes sense') + }) + + workerFarm.end(child, function() { + t.pass('an .end() callback was successfully called') + }) +}) + + +tape('call timeout test', function (t) { + t.plan(3 + 3 + 4 + 4 + 4 + 3 + 1) + + let child = workerFarm({ maxCallTime: 250, maxConcurrentWorkers: 1 }, childPath) + + // should come back ok + child(50, function (err, pid, rnd) { + t.ok(pid > process.pid, 'pid makes sense ' + pid + ' vs ' + process.pid) + t.ok(pid < process.pid + 750, 'pid makes sense ' + pid + ' vs ' + process.pid) + t.ok(rnd > 0 && rnd < 1, 'rnd result makes sense ' + rnd) + }) + + // should come back ok + child(50, function (err, pid, rnd) { + t.ok(pid > process.pid, 'pid makes sense ' + pid + ' vs ' + process.pid) + t.ok(pid < process.pid + 750, 'pid makes sense ' + pid + ' vs ' + process.pid) + t.ok(rnd > 0 && rnd < 1, 'rnd result makes sense ' + rnd) + }) + + // should die + child(500, function (err, pid, rnd) { + t.ok(err, 'got an error') + t.equal(err.type, 'TimeoutError', 'correct error type') + t.ok(pid === undefined, 'no pid') + t.ok(rnd === undefined, 'no rnd') + }) + + // should die + child(1000, function (err, pid, rnd) { + t.ok(err, 'got an error') + t.equal(err.type, 'TimeoutError', 'correct error type') + t.ok(pid === undefined, 'no pid') + t.ok(rnd === undefined, 'no rnd') + }) + + // should die even though it is only a 100ms task, it'll get caught up + // in a dying worker + setTimeout(function () { + child(100, function (err, pid, rnd) { + t.ok(err, 'got an error') + t.equal(err.type, 'TimeoutError', 'correct error type') + t.ok(pid === undefined, 'no pid') + t.ok(rnd === undefined, 'no rnd') + }) + }, 200) + + // should be ok, new worker + setTimeout(function () { + child(50, function (err, pid, rnd) { + t.ok(pid > process.pid, 'pid makes sense ' + pid + ' vs ' + process.pid) + t.ok(pid < process.pid + 750, 'pid makes sense ' + pid + ' vs ' + process.pid) + t.ok(rnd > 0 && rnd < 1, 'rnd result makes sense ' + rnd) + }) + workerFarm.end(child, function () { + t.ok(true, 'workerFarm ended') + }) + }, 400) +}) + + +tape('test error passing', function (t) { + t.plan(10) + + let child = workerFarm(childPath, [ 'err' ]) + child.err('Error', 'this is an Error', function (err) { + t.ok(err instanceof Error, 'is an Error object') + t.equal('Error', err.type, 'correct type') + t.equal('this is an Error', err.message, 'correct message') + }) + child.err('TypeError', 'this is a TypeError', function (err) { + t.ok(err instanceof Error, 'is a TypeError object') + t.equal('TypeError', err.type, 'correct type') + t.equal('this is a TypeError', err.message, 'correct message') + }) + child.err('Error', 'this is an Error with custom props', {foo: 'bar', 'baz': 1}, function (err) { + t.ok(err instanceof Error, 'is an Error object') + t.equal(err.foo, 'bar', 'passes data') + t.equal(err.baz, 1, 'passes data') + }) + + workerFarm.end(child, function () { + t.ok(true, 'workerFarm ended') + }) +}) + + +tape('test maxConcurrentCalls', function (t) { + t.plan(10) + + let child = workerFarm({ maxConcurrentCalls: 5 }, childPath) + + child(50, function (err) { t.notOk(err, 'no error') }) + child(50, function (err) { t.notOk(err, 'no error') }) + child(50, function (err) { t.notOk(err, 'no error') }) + child(50, function (err) { t.notOk(err, 'no error') }) + child(50, function (err) { t.notOk(err, 'no error') }) + child(50, function (err) { + t.ok(err) + t.equal(err.type, 'MaxConcurrentCallsError', 'correct error type') + }) + child(50, function (err) { + t.ok(err) + t.equal(err.type, 'MaxConcurrentCallsError', 'correct error type') + }) + + workerFarm.end(child, function () { + t.ok(true, 'workerFarm ended') + }) +}) + + +// this test should not keep the process running! if the test process +// doesn't die then the problem is here +tape('test timeout kill', function (t) { + t.plan(3) + + let child = workerFarm({ maxCallTime: 250, maxConcurrentWorkers: 1 }, childPath, [ 'block' ]) + child.block(function (err) { + t.ok(err, 'got an error') + t.equal(err.type, 'TimeoutError', 'correct error type') + }) + + workerFarm.end(child, function () { + t.ok(true, 'workerFarm ended') + }) +}) + + +tape('test max retries after process terminate', function (t) { + t.plan(7) + + // temporary file is used to store the number of retries among terminating workers + let filepath1 = '.retries1' + let child1 = workerFarm({ maxConcurrentWorkers: 1, maxRetries: 5}, childPath, [ 'stubborn' ]) + child1.stubborn(filepath1, function (err, result) { + t.notOk(err, 'no error') + t.equal(result, 12, 'correct result') + }) + + workerFarm.end(child1, function () { + fs.unlinkSync(filepath1) + t.ok(true, 'workerFarm ended') + }) + + let filepath2 = '.retries2' + let child2 = workerFarm({ maxConcurrentWorkers: 1, maxRetries: 3}, childPath, [ 'stubborn' ]) + child2.stubborn(filepath2, function (err, result) { + t.ok(err, 'got an error') + t.equal(err.type, 'ProcessTerminatedError', 'correct error type') + t.equal(err.message, 'cancel after 3 retries!', 'correct message and number of retries') + }) + + workerFarm.end(child2, function () { + fs.unlinkSync(filepath2) + t.ok(true, 'workerFarm ended') + }) +}) + + +tape('custom arguments can be passed to "fork"', function (t) { + t.plan(3) + + // allocate a real, valid path, in any OS + let cwd = fs.realpathSync(os.tmpdir()) + , workerOptions = { + cwd : cwd + , execArgv : ['--no-warnings'] + } + , child = workerFarm({ maxConcurrentWorkers: 1, maxRetries: 5, workerOptions: workerOptions}, childPath, ['args']) + + child.args(function (err, result) { + t.equal(result.execArgv[0], '--no-warnings', 'flags passed (overridden default)') + t.equal(result.cwd, cwd, 'correct cwd folder') + }) + + workerFarm.end(child, function () { + t.ok(true, 'workerFarm ended') + }) +}) + + +tape('ensure --debug/--inspect not propagated to children', function (t) { + t.plan(3) + + let script = __dirname + '/debug.js' + , debugArg = process.version.replace(/^v(\d+)\..*$/, '$1') >= 8 ? '--inspect' : '--debug=8881' + , child = child_process.spawn(process.execPath, [ debugArg, script ]) + , stdout = '' + + child.stdout.on('data', function (data) { + stdout += data.toString() + }) + + child.on('close', function (code) { + t.equal(code, 0, 'exited without error (' + code + ')') + t.ok(stdout.indexOf('FINISHED') > -1, 'process finished') + t.ok(stdout.indexOf('--debug') === -1, 'child does not receive debug flag') + }) +}) diff --git a/settings.json b/settings.json index 98944b7..fa519ab 100644 --- a/settings.json +++ b/settings.json @@ -1,3 +1,3 @@ { - "encryption_key_size": 100000 + "encryption_key_size": 10 }