Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cuddle

Package Overview
Dependencies
Maintainers
1
Versions
81
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cuddle - npm Package Compare versions

Comparing version 0.0.51 to 0.0.52

725

index.js

@@ -10,471 +10,458 @@ 'use strict';

var https = require('https'),
http = require('http'),
zlib = require('zlib'),
url = require('url'),
http = require('http'),
url = require('url'),
logger = {log: function () {}},
logger = {
log: function () {}
},
grapher;
grapher,
stringify = function (obj) {
var ret = [],
key;
function stringify (obj) {
var ret = [],
key;
for (key in obj) {
ret.push(
obj[key] === null ? encodeURIComponent(key) : encodeURIComponent(key) + '=' + encodeURIComponent(
obj[key])
);
}
for (key in obj) {
ret.push(
obj[key] === null ? encodeURIComponent(key) : encodeURIComponent(key) + '=' + encodeURIComponent(
obj[key])
);
}
return ret.join('&');
},
return ret.join('&');
}
Request = function (method) {
this.method = method;
this.secure = false;
this.started = false;
this.follow = false;
this.request_opts = {};
this.headers = {};
this.retries = 0;
this.max_retry = 3;
this.callbacks = {};
this.encoding = 'utf8';
this.log = true;
this.to_string = function () {
return [
this.method,
' ',
'http',
this.secured ? 's' : '',
'://',
this.host,
':',
this.port,
this.path,
'\nPayload:\t',
JSON.stringify(this.data),
'\nHeaders:\t',
JSON.stringify(this.headers)
].join('');
};
function Request (method) {
this.raw = function () {
console.log('\tcudl.raw() is deprecated');
return this;
};
this.method = method;
this.secure = false;
this.started = false;
this.follow = false;
this.request_opts = {};
this.headers = {};
this.retries = 0;
this.max_retry = 3;
this.callbacks = {};
this.encoding = 'utf8';
this.log = true;
this.to = function (host, port, path) {
this.to_string = function () {
return [
this.method,
' ',
'http',
this.secured ? 's' : '',
'://',
this.host,
':',
this.port,
this.path,
'\nPayload:\t',
JSON.stringify(this.data),
'\nHeaders:\t',
JSON.stringify(this.headers)
].join('');
};
if (!port && !path) {
host = url.parse(host);
this.host = host.hostname;
this.path = host.path;
this.port = host.port ? host.port : 80;
this.raw = function () {
console.log('\tcudl.raw() is deprecated');
return this;
};
if (host.protocol === 'https:') {
this.port = 443;
this.secure = true;
}
}
else {
this.path = path;
this.host = host;
this.port = port;
}
this.to = function (host, port, path) {
this.request_opts = {
host: this.host,
port: this.port
};
if (!port && !path) {
host = url.parse(host);
this.host = host.hostname;
this.path = host.path;
this.port = host.port ? host.port : 80;
return this;
};
if (host.protocol === 'https:') {
this.port = 443;
this.secure = true;
}
}
else {
this.path = path;
this.host = host;
this.port = port;
}
this.set_max_retry = function (max) {
this.max_retry = max;
return this;
};
this.request_opts = {
host: this.host,
port: this.port
this.secured = function () {
this.secure = true;
return this;
};
return this;
};
this.add_header = function (key, value) {
this.headers[key] = value;
return this;
};
this.set_max_retry = function (max) {
this.max_retry = max;
return this;
};
this.add_opts = function (key, value) {
this.request_opts[key] = value;
return this;
};
this.secured = function () {
this.secure = true;
return this;
};
this.then = function (cb) {
if (!this.real_cb) {
this.real_cb = cb;
}
this.add_header = function (key, value) {
this.headers[key] = value;
return this;
};
if (!this.started) {
this.send();
}
return this;
};
this.add_opts = function (key, value) {
this.request_opts[key] = value;
return this;
};
this.cb = function () {
if (grapher) {
grapher(this.start_date, +new Date());
}
this.then = function (cb) {
if (!this.real_cb) {
this.real_cb = cb;
}
if (this.real_cb) {
this.real_cb.apply(this, arguments);
}
};
if (!this.started) {
this.send();
}
return this;
};
this.args = function () {
this.additional_arguments = arguments;
return this;
};
this.cb = function () {
if (grapher) {
grapher(this.start_date, +new Date());
}
this.set_before_json = function (fn) {
this.before_json = fn;
return this;
};
if (this.real_cb) {
this.real_cb.apply(this, arguments);
}
};
this.set_encoding = function (encoding) {
this.encoding = encoding;
return this;
};
this.args = function () {
this.additional_arguments = arguments;
return this;
};
this.follow_redirects = function (max_redirects) {
this.max_redirects = +max_redirects || 3;
this.follow = true;
return this;
};
this.set_before_json = function (fn) {
this.before_json = fn;
return this;
};
this.logging = function (log) {
this.log = log;
return this;
};
this.set_encoding = function (encoding) {
this.encoding = encoding;
return this;
};
this.req_log = function () {
if (this.log) {
logger.log.apply(logger, arguments);
}
};
this.follow_redirects = function (max_redirects) {
this.max_redirects = +max_redirects || 3;
this.follow = true;
return this;
};
this.stringify = stringify;
this.logging = function (log) {
this.log = log;
return this;
};
this.retry = function () {
this.retries++;
if (this.retries > this.max_retry) {
this.req_log('error', 'Reached max retries');
this.cb({
message: 'Reached max retries',
url: this.host + ':' + this.port + this.path
},
null,
this,
this.additional_arguments
);
return this;
}
this.req_log('warn', 'Retrying request');
return this.send(this.data);
};
this.req_log = function () {
if (this.log) {
logger.log.apply(logger, arguments);
}
};
this.pipe = function (stream) {
this.req_log('verbose', 'Piping file..');
this._stream = stream;
return this;
};
this.stringify = stringify;
this.on = function (event_type, next) {
this.retry = function () {
if (this.callbacks[event_type]) {
this.callbacks[event_type] = this.callbacks[event_type].push(next);
}
this.retries++;
this.callbacks[event_type] = [next];
if (this.retries > this.max_retry) {
this.req_log('error', 'Reached max retries');
this.cb({
message: 'Reached max retries',
url: this.host + ':' + this.port + this.path
},
null,
this,
this.additional_arguments
);
return this;
}
};
this.req_log('warn', 'Retrying request');
this.emit = function (event_type, err, response) {
if (this.callbacks[event_type]) {
this.callbacks[event_type].forEach(function (callback) {
callback(err, response);
});
}
return this.send(this.data);
};
return this;
};
this.pipe = function (stream) {
this.req_log('verbose', 'Piping file..');
this._stream = stream;
return this;
};
this.send = function (data) {
var new_path = this.path,
self = this,
protocol,
payload,
req;
this.on = function (event_type, next) {
this.started = true;
this.data = data || '';
if (this.callbacks[event_type]) {
this.callbacks[event_type] = this.callbacks[event_type].push(next);
}
this.callbacks[event_type] = [next];
return this;
};
this.emit = function (event_type, err, response) {
if (this.callbacks[event_type]) {
this.callbacks[event_type].forEach(function (callback) {
callback(err, response);
});
}
return this;
};
this.send = function (data) {
var new_path = this.path,
self = this,
protocol,
payload,
req;
this.started = true;
this.data = data || '';
if (data && this.method === 'GET') {
new_path += '?' + this.stringify(data);
}
else {
if (!this.headers['Content-Type']) {
payload = this.stringify(data);
this.headers['Content-Type'] = 'application/x-www-form-urlencoded';
this.headers['Content-Length'] = payload.length;
if (data && this.method === 'GET') {
new_path += '?' + this.stringify(data);
}
else {
payload = JSON.stringify(data);
if (!this.headers['Content-Type']) {
payload = this.stringify(data);
this.headers['Content-Type'] = 'application/x-www-form-urlencoded';
this.headers['Content-Length'] = payload.length;
}
else {
payload = JSON.stringify(data);
}
}
}
this.headers.Accept = 'application/json';
this.headers.Accept = 'application/json';
this.req_log('verbose', this.method, this.host + ':' + this.port + new_path);
this.req_log('verbose', this.method, this.host + ':' + this.port + new_path);
if (payload) {
this.req_log('debug', 'data\n', payload);
}
if (payload) {
this.req_log('debug', 'data\n', payload);
}
this.req_log('debug', 'headers\n', this.headers);
this.req_log('debug', 'headers\n', this.headers);
protocol = this.secure ? https : http;
protocol = this.secure ? https : http;
this.request_opts.path = new_path;
this.request_opts.method = this.method;
this.request_opts.headers = this.headers;
this.request_opts.path = new_path;
this.request_opts.method = this.method;
this.request_opts.headers = this.headers;
req = protocol.request(this.request_opts);
try {
req = protocol.request(this.request_opts);
req.on('response', response_callback);
req.on('response', function (response) {
var s = '';
req.on('error', function (err) {
var retryable_errors = [
'ECONNREFUSED',
'ECONNRESET',
'ENOTFOUND',
'EADDRINFO',
'ETIMEDOUT',
'ESRCH'
];
response.setEncoding(self.encoding);
self.req_log('error', 'Request error', err, self.host + ':' + self.port +
self.path);
response.on('data', function (chunk) {
s += chunk;
if (~retryable_errors.indexOf(err.code)) {
if (self.retries < self.max_retry) {
return self.retry();
}
err.message = 'OMG. Server on ' + self.host + ':' + self.port +
' seems dead';
}
self.emit('data', null, chunk);
self.cb(err, null, self, self.additional_arguments);
});
});
req.on('socket', function () {
self.start_date = +new Date();
});
response.on('close', function () {
self.req_log('error', 'request closed');
self.emit('close', 'request closed');
self.retry();
});
req.on('continue', function () {
self.req_log('error', 'continue event emitted');
self.retry();
});
response.on('error', function (err) {
self.req_log('error', 'Response error', err);
self.emit('error', err);
self.retry();
});
req.on('upgrade', function () {
self.req_log('error', 'upgrade event emitted');
self.retry();
});
response.on('end', function () {
var redir,
temp;
req.on('connect', function () {
self.req_log('error', 'connect event emitted');
self.retry();
});
self.response_headers = response.headers;
self.target_location = response.headers.location;
if (this.method !== 'GET') {
req.write(payload);
}
if (self.follow && self.response_headers.location) {
if (!self.max_redirects) {
self.cb({
message: 'Too many redirects'
}, s, self, self.additional_arguments);
return;
}
req.end();
return this;
};
}
temp = self.response_headers.location.split('/');
redir = new Request('GET')
.to(self.response_headers.location)
.follow_redirects(self.max_redirects - 1);
function response_callback (response) {
var self = this,
chunks = [];
if (temp[0] === 'https:') {
redir = redir.secured();
}
response.setEncoding(self.encoding);
for (temp in self.headers) {
redir = redir.add_header(temp, self.headers[temp]);
}
response.on('data', function (chunk) {
chunks.push(chunk);
if (typeof (self._stream) !== 'undefined') {
redir.pipe(self._stream);
}
self.emit('data', null, chunk);
if (typeof (self.callbacks) !== 'undefined') {
redir.callbacks = self.callbacks;
}
});
redir.target_location = self.target_location;
redir.set_encoding(self.encoding);
response.on('close', function () {
self.req_log('error', 'request closed');
self.emit('close', 'request closed');
self.retry();
});
redir.then(self.real_cb);
}
else {
self.req_log('verbose', 'Response', response.statusCode);
self.req_log('silly', s);
response.on('error', function (err) {
self.req_log('error', 'Response error', err);
self.emit('error', err);
self.retry();
});
if (response.headers['content-type'] && response.headers[
'content-type'].split(';')[0] ===
'application/json') {
if (self.before_json) {
s = self.before_json(s);
}
try {
s = JSON.parse(s);
}
catch (e) {
self.req_log('error', 'JSON is invalid');
self.req_log('error', e);
e.status_code = response.statusCode;
return self.cb(e, s, self, self.additional_arguments);
}
}
response.on('end', function () {
var buffer = Buffer.concat(chunks),
encoding = response.headers['content-encoding'],
redir,
temp;
self.response_headers = response.headers;
self.target_location = response.headers.location;
if (response.statusCode >= 200 && response.statusCode < 300) {
if (self.follow && self.response_headers.location) {
if (!self.max_redirects) {
self.cb({
message: 'Too many redirects'
}, chunks.join(''), self, self.additional_arguments);
return;
}
if (typeof (self._stream) !== 'undefined') {
response.pipe(self._stream);
}
temp = self.response_headers.location.split('/');
self.cb(null, s, self, self.additional_arguments);
redir = new Request('GET')
.to(self.response_headers.location)
.follow_redirects(self.max_redirects - 1);
}
else {
if (temp[0] === 'https:') {
redir = redir.secured();
}
self.cb({
response: s,
status_code: response.statusCode
}, null, self, self.additional_arguments);
}
}
});
});
for (temp in self.headers) {
redir = redir.add_header(temp, self.headers[temp]);
}
req.on('error', function (err) {
var retryable_errors = [
'ECONNREFUSED',
'ECONNRESET',
'ENOTFOUND',
'EADDRINFO',
'ETIMEDOUT',
'ESRCH'
];
if (typeof (self._stream) !== 'undefined') {
redir.pipe(self._stream);
}
self.req_log('error', 'Request error', err, self.host + ':' + self.port +
self.path);
if (typeof (self.callbacks) !== 'undefined') {
redir.callbacks = self.callbacks;
}
if (~retryable_errors.indexOf(err.code)) {
if (self.retries < self.max_retry) {
return self.retry();
}
err.message = 'OMG. Server on ' + self.host + ':' + self.port +
' seems dead';
}
redir.target_location = self.target_location;
redir.set_encoding(self.encoding);
self.cb(err, null, self, self.additional_arguments);
});
redir.then(self.real_cb);
}
else {
self.req_log('verbose', 'Response', response.statusCode);
self.req_log('silly', chunks.join(''));
req.on('socket', function () {
self.start_date = +new Date();
});
if (encoding === 'gzip') {
zlib.gunzip(buffer, function (err, decoded) {
req.on('continue', function () {
self.req_log('error', 'continue event emitted');
self.retry();
});
req.on('upgrade', function () {
self.req_log('error', 'upgrade event emitted');
self.retry();
});
}
if (response.headers['content-type'] && response.headers[
'content-type'].split(';')[0] ===
'application/json') {
if (self.before_json) {
s = self.before_json(s);
}
try {
s = JSON.parse(s);
}
catch (e) {
self.req_log('error', 'JSON is invalid');
self.req_log('error', e);
e.status_code = response.statusCode;
return self.cb(e, s, self, self.additional_arguments);
}
}
req.on('connect', function () {
self.req_log('error', 'connect event emitted');
self.retry();
});
if (response.statusCode >= 200 && response.statusCode < 300) {
if (typeof (self._stream) !== 'undefined') {
response.pipe(self._stream);
if (this.method !== 'GET') {
req.write(payload);
}
self.cb(null, s, self, self.additional_arguments);
req.end();
}
catch (e) {
self.req_log('error', e.toString() || e);
self.retry();
}
return this;
};
},
attach = function (object) {
object.get = {
to: function (host, port, path) {
return new Request('GET').to(host, port, path);
}
else {
};
self.cb({
response: s,
status_code: response.statusCode
}, null, self, self.additional_arguments);
object.post = {
to: function (host, port, path) {
return new Request('POST').to(host, port, path);
}
}
});
}
};
function attach (object) {
object.get = {
to: function (host, port, path) {
return new Request('GET').to(host, port, path);
}
};
object.put = {
to: function (host, port, path) {
return new Request('PUT').to(host, port, path);
}
};
object.post = {
to: function (host, port, path) {
return new Request('POST').to(host, port, path);
}
};
object.delete = {
to: function (host, port, path) {
return new Request('DELETE').to(host, port, path);
}
};
object.put = {
to: function (host, port, path) {
return new Request('PUT').to(host, port, path);
}
};
object.request = function (method) {
this.to = function (host, port, path) {
return new Request(method).to(host, port, path);
};
return this;
};
object.delete = {
to: function (host, port, path) {
return new Request('DELETE').to(host, port, path);
}
};
object.request = function (method) {
this.to = function (host, port, path) {
return new Request(method).to(host, port, path);
object.graph = function (cb) {
grapher = cb;
return this;
};
return this;
};
object.graph = function (cb) {
grapher = cb;
return this;
object.stringify = stringify;
return object;
};
object.stringify = stringify;
return object;
}
module.exports = function (_logger) {

@@ -481,0 +468,0 @@ logger = _logger || logger;

{
"name": "cuddle",
"version": "0.0.51",
"version": "0.0.52",
"description": "",

@@ -5,0 +5,0 @@ "main": "index.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc