socket 0.0.1 → 0.14.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,29 +0,0 @@
1
- // (The MIT License)
2
- //
3
- // Copyright (c) 2012 Richard S Allinson <rsa@mountainmansoftware.com>
4
- //
5
- // Permission is hereby granted, free of charge, to any person obtaining
6
- // a copy of this software and associated documentation files (the
7
- // 'Software'), to deal in the Software without restriction, including
8
- // without limitation the rights to use, copy, modify, merge, publish,
9
- // distribute, sublicense, and/or sell copies of the Software, and to
10
- // permit persons to whom the Software is furnished to do so, subject to
11
- // the following conditions:
12
- //
13
- // The above copyright notice and this permission notice shall be
14
- // included in all copies or substantial portions of the Software.
15
- //
16
- // THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
- // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
- // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
- // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
- // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
-
24
- var socket = require('../lib');
25
-
26
- socket.createServer(
27
- socket.profiler(),
28
- socket.echo()
29
- ).listen(8000);
@@ -1,76 +0,0 @@
1
- // (The MIT License)
2
- //
3
- // Copyright (c) 2012 Richard S Allinson <rsa@mountainmansoftware.com>
4
- //
5
- // Permission is hereby granted, free of charge, to any person obtaining
6
- // a copy of this software and associated documentation files (the
7
- // 'Software'), to deal in the Software without restriction, including
8
- // without limitation the rights to use, copy, modify, merge, publish,
9
- // distribute, sublicense, and/or sell copies of the Software, and to
10
- // permit persons to whom the Software is furnished to do so, subject to
11
- // the following conditions:
12
- //
13
- // The above copyright notice and this permission notice shall be
14
- // included in all copies or substantial portions of the Software.
15
- //
16
- // THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
- // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
- // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
- // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
- // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
-
24
- 'use strict';
25
-
26
- var socket = require('../lib'),
27
- net = require('net'),
28
- microtime = require('microtime'),
29
- port = 8080,
30
- app,
31
- repeat = 1000,
32
- count = repeat,
33
- start = microtime.now(),
34
- i;
35
-
36
- app = socket.createServer(
37
- socket.profiler(),
38
- socket.echo()
39
- ).listen(port);
40
-
41
- console.log('Knot Server Started');
42
-
43
- function test(port, ip, obj, callback) {
44
- var buffer = '',
45
- client;
46
-
47
- client = net.connect(port, ip, function () {
48
- client.write(JSON.stringify(obj) + app.TERM);
49
- });
50
- client.on('data', function (data) {
51
- buffer += data;
52
- });
53
- client.on('end', function () {
54
- var data;
55
- try {
56
- data = JSON.parse(buffer);
57
- } catch (e) {
58
- data = {error: 'bad json parse'};
59
- }
60
- callback(data);
61
- });
62
- }
63
-
64
- function log(msg) {
65
- count = count - 1;
66
- if (!count) {
67
- console.log('Total: ' + ((microtime.now() - start) / 1000000) + 'sec (' + repeat + ' requests)');
68
- process.exit();
69
- } else {
70
- console.log(msg);
71
- }
72
- }
73
-
74
- for (i = 0; i < count; i = i + 1) {
75
- test(port, 'localhost', {num: i}, log);
76
- }
@@ -1,40 +0,0 @@
1
- // (The MIT License)
2
- //
3
- // Copyright (c) 2012 Richard S Allinson <rsa@mountainmansoftware.com>
4
- //
5
- // Permission is hereby granted, free of charge, to any person obtaining
6
- // a copy of this software and associated documentation files (the
7
- // 'Software'), to deal in the Software without restriction, including
8
- // without limitation the rights to use, copy, modify, merge, publish,
9
- // distribute, sublicense, and/or sell copies of the Software, and to
10
- // permit persons to whom the Software is furnished to do so, subject to
11
- // the following conditions:
12
- //
13
- // The above copyright notice and this permission notice shall be
14
- // included in all copies or substantial portions of the Software.
15
- //
16
- // THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
- // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
- // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
- // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
- // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
-
24
- 'use strict';
25
-
26
- var socket = require('../lib');
27
-
28
- function simpleTimeServer() {
29
- console.log('Simple Time Server started\r\n');
30
- return function (req, res) {
31
- var time = new Date().getTime();
32
- res.end(time.toString() + '\r\n');
33
- };
34
- }
35
-
36
- socket.createServer(
37
- {term: ''},
38
- socket.profiler(),
39
- simpleTimeServer()
40
- ).listen(1024);
package/index.js DELETED
@@ -1,24 +0,0 @@
1
- // (The MIT License)
2
- //
3
- // Copyright (c) 2012 Richard S Allinson <rsa@mountainmansoftware.com>
4
- //
5
- // Permission is hereby granted, free of charge, to any person obtaining
6
- // a copy of this software and associated documentation files (the
7
- // 'Software'), to deal in the Software without restriction, including
8
- // without limitation the rights to use, copy, modify, merge, publish,
9
- // distribute, sublicense, and/or sell copies of the Software, and to
10
- // permit persons to whom the Software is furnished to do so, subject to
11
- // the following conditions:
12
- //
13
- // The above copyright notice and this permission notice shall be
14
- // included in all copies or substantial portions of the Software.
15
- //
16
- // THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
- // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
- // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
- // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
- // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
-
24
- module.exports = require('./lib/index');
package/lib/index.js DELETED
@@ -1,110 +0,0 @@
1
- // (The MIT License)
2
- //
3
- // Copyright (c) 2012 Richard S Allinson <rsa@mountainmansoftware.com>
4
- //
5
- // Permission is hereby granted, free of charge, to any person obtaining
6
- // a copy of this software and associated documentation files (the
7
- // 'Software'), to deal in the Software without restriction, including
8
- // without limitation the rights to use, copy, modify, merge, publish,
9
- // distribute, sublicense, and/or sell copies of the Software, and to
10
- // permit persons to whom the Software is furnished to do so, subject to
11
- // the following conditions:
12
- //
13
- // The above copyright notice and this permission notice shall be
14
- // included in all copies or substantial portions of the Software.
15
- //
16
- // THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
- // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
- // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
- // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
- // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
-
24
- /*jslint stupid: true, nomen: true*/
25
-
26
- 'use strict';
27
-
28
- /**
29
- * Module dependencies.
30
- */
31
-
32
- var proto = require('./proto'),
33
- utils = require('./utils'),
34
- path = require('path'),
35
- basename = path.basename,
36
- fs = require('fs');
37
-
38
- /**
39
- * Create a new knot server.
40
- *
41
- * @return {Function}
42
- * @api public
43
- */
44
-
45
- function createServer() {
46
- var i, opt;
47
- function app(req, res) {
48
- app.handle(req, res);
49
- }
50
- utils.merge(app, proto);
51
- app.route = '/';
52
- app.stack = [];
53
- app.TERM = '\r\n';
54
- app.ENCODING = 'utf8';
55
-
56
- // I have no idea how Connect does this?
57
- for (i = 0; i < arguments.length; i = i + 1) {
58
- if (typeof arguments[i] === 'function') {
59
- app.use(arguments[i]);
60
- } else if (typeof arguments[i] === 'object' && !opt) {
61
- opt = arguments[i];
62
- }
63
- }
64
- // If we were given options, use them.
65
- if (opt) {
66
- app.TERM = opt.term !== undefined ? opt.term : '\r\n';
67
- app.ENCODING = opt.encoding || 'utf8';
68
- }
69
-
70
- return app;
71
- }
72
-
73
- // expose createServer() as the module
74
-
75
- module.exports = createServer;
76
-
77
- /**
78
- * Framework version.
79
- */
80
-
81
- module.exports.version = '0.1.0';
82
-
83
- /**
84
- * Expose createServer
85
- */
86
-
87
- module.exports.createServer = createServer;
88
-
89
- /**
90
- * Expose createServer
91
- */
92
-
93
- module.exports.dispatch = proto.dispatch;
94
-
95
- /**
96
- * Auto-load bundled middleware with getters.
97
- */
98
-
99
- fs.readdirSync(__dirname + '/middleware').forEach(function (filename) {
100
- var name;
101
-
102
- if (!/\.js$/.test(filename)) {
103
- return;
104
- }
105
- name = basename(filename, '.js');
106
- function load() {
107
- return require('./middleware/' + name);
108
- }
109
- module.exports.__defineGetter__(name, load);
110
- });
@@ -1,30 +0,0 @@
1
- // (The MIT License)
2
- //
3
- // Copyright (c) 2012 Richard S Allinson <rsa@mountainmansoftware.com>
4
- //
5
- // Permission is hereby granted, free of charge, to any person obtaining
6
- // a copy of this software and associated documentation files (the
7
- // 'Software'), to deal in the Software without restriction, including
8
- // without limitation the rights to use, copy, modify, merge, publish,
9
- // distribute, sublicense, and/or sell copies of the Software, and to
10
- // permit persons to whom the Software is furnished to do so, subject to
11
- // the following conditions:
12
- //
13
- // The above copyright notice and this permission notice shall be
14
- // included in all copies or substantial portions of the Software.
15
- //
16
- // THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
- // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
- // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
- // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
- // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
-
24
- 'use strict';
25
-
26
- module.exports = function () {
27
- return function (req, res) {
28
- res.end(req.data);
29
- };
30
- };
@@ -1,57 +0,0 @@
1
- // (The MIT License)
2
- //
3
- // Copyright (c) 2012 Richard S Allinson <rsa@mountainmansoftware.com>
4
- //
5
- // Permission is hereby granted, free of charge, to any person obtaining
6
- // a copy of this software and associated documentation files (the
7
- // 'Software'), to deal in the Software without restriction, including
8
- // without limitation the rights to use, copy, modify, merge, publish,
9
- // distribute, sublicense, and/or sell copies of the Software, and to
10
- // permit persons to whom the Software is furnished to do so, subject to
11
- // the following conditions:
12
- //
13
- // The above copyright notice and this permission notice shall be
14
- // included in all copies or substantial portions of the Software.
15
- //
16
- // THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
- // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
- // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
- // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
- // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
-
24
- 'use strict';
25
-
26
- /**
27
- * Development error handler, providing stack traces
28
- * and error message responses for requests accepting text, html,
29
- * or json.
30
- *
31
- * Will respond with an object in the form of `{ "error": error }`.
32
- *
33
- * @return {Function}
34
- * @api public
35
- */
36
-
37
- module.exports = function errorHandler() {
38
- return function (err, req, res, next) {
39
-
40
- var error,
41
- prop,
42
- json;
43
-
44
- console.error(err.stack);
45
- error = {
46
- message: err.message,
47
- stack: err.stack
48
- };
49
- for (prop in err) {
50
- if (err.hasOwnProperty(prop)) {
51
- error[prop] = err[prop];
52
- }
53
- }
54
- json = JSON.stringify({error: error});
55
- res.end(json);
56
- };
57
- };
@@ -1,42 +0,0 @@
1
- // (The MIT License)
2
- //
3
- // Copyright (c) 2012 Richard S Allinson <rsa@mountainmansoftware.com>
4
- //
5
- // Permission is hereby granted, free of charge, to any person obtaining
6
- // a copy of this software and associated documentation files (the
7
- // 'Software'), to deal in the Software without restriction, including
8
- // without limitation the rights to use, copy, modify, merge, publish,
9
- // distribute, sublicense, and/or sell copies of the Software, and to
10
- // permit persons to whom the Software is furnished to do so, subject to
11
- // the following conditions:
12
- //
13
- // The above copyright notice and this permission notice shall be
14
- // included in all copies or substantial portions of the Software.
15
- //
16
- // THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
- // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
- // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
- // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
- // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
-
24
- 'use strict';
25
-
26
- module.exports = function json() {
27
- return function (req, res, next) {
28
- var end = res.end;
29
- try {
30
- req.data = JSON.parse(req.data);
31
- } catch (e) {
32
- res.end('{error:"bad json request"}');
33
- }
34
- // proxy res.end()
35
- res.end = function (data, encoding) {
36
- data = JSON.stringify(data);
37
- res.end = end;
38
- res.end(data, encoding);
39
- };
40
- next();
41
- };
42
- };
@@ -1,129 +0,0 @@
1
- // (The MIT License)
2
- //
3
- // Copyright (c) 2012 Richard S Allinson <rsa@mountainmansoftware.com>
4
- //
5
- // Permission is hereby granted, free of charge, to any person obtaining
6
- // a copy of this software and associated documentation files (the
7
- // 'Software'), to deal in the Software without restriction, including
8
- // without limitation the rights to use, copy, modify, merge, publish,
9
- // distribute, sublicense, and/or sell copies of the Software, and to
10
- // permit persons to whom the Software is furnished to do so, subject to
11
- // the following conditions:
12
- //
13
- // The above copyright notice and this permission notice shall be
14
- // included in all copies or substantial portions of the Software.
15
- //
16
- // THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
- // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
- // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
- // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
- // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
-
24
- 'use strict';
25
-
26
- /**
27
- * Profile the duration of a request.
28
- *
29
- * Typically this middleware should be utilized
30
- * _above_ all others, as it proxies the `res.end()`
31
- * method, being first allows it to encapsulate all
32
- * other middleware.
33
- *
34
- * Example Output:
35
- *
36
- * request
37
- * response time 2ms
38
- * memory rss 52.00kb
39
- * memory vsize 2.07mb
40
- * heap before 3.76mb / 8.15mb
41
- * heap after 3.80mb / 8.15mb
42
- *
43
- * @api public
44
- */
45
-
46
- var microtime = require('microtime');
47
-
48
- /**
49
- * Row helper
50
- *
51
- * @param {String} key
52
- * @param {String} val
53
- * @api private
54
- */
55
-
56
- function row(key, val) {
57
- console.log(' %s %s ', key, val);
58
- }
59
-
60
- /**
61
- * Format byte-size.
62
- *
63
- * @param {Number} bytes
64
- * @return {String}
65
- * @api private
66
- */
67
-
68
- function formatBytes(bytes) {
69
- var kb = 1024,
70
- mb = 1024 * kb,
71
- gb = 1024 * mb;
72
-
73
- if (bytes < kb) {
74
- return bytes + 'b';
75
- }
76
- if (bytes < mb) {
77
- return (bytes / kb).toFixed(2) + 'kb';
78
- }
79
- if (bytes < gb) {
80
- return (bytes / mb).toFixed(2) + 'mb';
81
- }
82
- return (bytes / gb).toFixed(2) + 'gb';
83
- }
84
-
85
- /**
86
- * Compare `start` / `end` snapshots.
87
- *
88
- * @param {IncomingRequest} req
89
- * @param {Object} start
90
- * @param {Object} end
91
- * @api private
92
- */
93
-
94
- function compare(req, start, end) {
95
- console.log();
96
- row('request:', typeof req.data === 'string' ? req.data : JSON.stringify(req.data));
97
- row('response time:', ((end.time - start.time) / 1000) + 'ms');
98
- row('memory rss:', formatBytes(end.mem.rss - start.mem.rss));
99
- row('heap before:', formatBytes(start.mem.heapUsed) + ' / ' + formatBytes(start.mem.heapTotal));
100
- row('heap after:', formatBytes(end.mem.heapUsed) + ' / ' + formatBytes(end.mem.heapTotal));
101
- console.log();
102
- }
103
-
104
- module.exports = function profiler() {
105
- return function (req, res, next) {
106
- var end,
107
- start;
108
-
109
- // state snapshot
110
- function snapshot() {
111
- return {
112
- mem: process.memoryUsage(),
113
- time: microtime.now() //new Date
114
- };
115
- }
116
-
117
- end = res.end;
118
- start = snapshot();
119
-
120
- // proxy res.end()
121
- res.end = function (data, encoding) {
122
- res.end = end;
123
- res.end(data, encoding);
124
- compare(req, start, snapshot());
125
- };
126
-
127
- next();
128
- };
129
- };