New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

bitbucket-v2

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bitbucket-v2 - npm Package Compare versions

Comparing version 0.1.3 to 0.1.4

4

bitbucket/index.js

@@ -10,3 +10,3 @@ const Request = require('./request').Request;

class Bitbucket {
constructor(proxy) {
constructor({ proxy, useXhr }) {
/**

@@ -20,3 +20,3 @@ * Define HTTP proxy in format localhost:3128

this.request = new Request({ 'proxy_host': this.$proxy_host, 'proxy_port': this.$proxy_port });
this.request = new Request({ 'proxy_host': this.$proxy_host, 'proxy_port': this.$proxy_port, 'use_xhr': useXhr });

@@ -23,0 +23,0 @@ this.constants = {

@@ -50,3 +50,2 @@ const _ = require('lodash');

*/
//TODO confirm this works
getBranches(username, repoSlug, callback) {

@@ -61,2 +60,16 @@ this.$api.get(

/**
* Get a single commit
* @param {String} repo owner
* @param {String} slug (name) of the repo.
* @param {String} the sha of the commit
*/
getCommit(username, repoSlug, sha, callback) {
this.$api.get(
'repositories/' + encodeURI(username) + '/' + encodeURI(repoSlug) + '/commit/' + sha,
null, null,
this.$createListener(callback)
);
}
/**
* Get the pull requests for a single repo

@@ -78,3 +91,3 @@ *

const hasInvalidState = _.find(state, (stateElement) => !_.contains(constants.pullRequest.states, stateElement));
const hasInvalidState = _.find(state, (stateElement) => !_.includes(constants.pullRequest.states, stateElement));
if (hasInvalidState) {

@@ -81,0 +94,0 @@ stateArray = [constants.pullRequest.states.OPEN];

@@ -0,4 +1,6 @@

const _ = require('lodash');
const https = require('https');
const querystring = require('querystring');
const https = require('https');
const url = require('url');
const xhr = require('xhr');

@@ -27,11 +29,8 @@ /**

proxy_host: null,
proxy_port: null
proxy_port: null,
use_xhr: false
};
this.configure = function configure(options = {}) {
this.$options = {};
for (const key in this.$defaults) {
this.$options[key] = options[key] !== undefined ? options[key] : this.$defaults[key];
}
this.$options = _.defaults({}, options, this.$defaults);
return this;

@@ -105,6 +104,6 @@ };

const response = this.decodeResponse(_response);
const response = this.$options.use_xhr ? _response : this.decodeResponse(_response);
if (initialOptions) {
this.options = initialOptions;
this.$options = initialOptions;
}

@@ -124,22 +123,4 @@ if (callback) {

this.doPrebuiltSend = function doPrebuiltSend(prebuiltURL, callback) {
const port = this.$options.proxy_host ? this.$options.proxy_port || 3128 : this.$options.http_port || 443;
const { headers, port } = this.prepRequest(this.$options);
const headers = {
'Host': 'api.bitbucket.org',
'User-Agent': 'NodeJS HTTP Client',
'Content-Length': '0',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Bearer ' + this.$options.oauth_access_token
};
const parsedUrl = url.parse(prebuiltURL);
const getOptions = {
headers,
hostname: parsedUrl.hostname,
method: 'GET',
path: parsedUrl.path,
post: port
};
let called = false;

@@ -155,43 +136,23 @@ function done(err, body) {

const request = https.request(getOptions, (response) => {
response.setEncoding('utf8');
if (this.$options.use_xhr) {
const xhrOptions = {
url: prebuiltURL,
responseType: 'json',
timeout: this.$options.timeout * 1000
};
const body = [];
response.addListener('data', (chunk) => {
body.push(chunk);
});
response.addListener('end', () => {
let msg = body.join('');
this.sendXhrRequest(xhrOptions, done);
return;
}
if (response.statusCode > 204) {
if (response.headers['content-type'].includes('application/json')) {
msg = JSON.parse(body);
}
else {
msg = body;
}
const { hostname, path } = url.parse(prebuiltURL);
const httpsOptions = {
headers,
hostname,
method: 'GET',
path,
post: port
};
done({ status: response.statusCode, msg });
return;
}
if (response.statusCode === 204) {
msg = '{}';
}
done(null, this.decodeResponse(msg));
});
response.addListener('error', (e) => {
done(e);
});
response.addListener('timeout', () => {
done(new Error('Request timed out'));
});
});
request.on('error', (e) => {
done(e);
});
request.end();
this.sendHttpsRequest(httpsOptions, undefined, done);
};

@@ -207,20 +168,13 @@

this.doSend = function doSend(apiPath, parameters, _httpMethod, callback) {
const httpMethod = _httpMethod.toUpperCase();
const host = this.$options.proxy_host ? this.$options.proxy_host : this.$options.hostname;
const port = this.$options.proxy_host ? this.$options.proxy_port || 3128 : this.$options.http_port || 443;
const method = _httpMethod.toUpperCase();
const { headers, hostname, port } = this.prepRequest(this.$options);
const headers = {
'Host': 'api.bitbucket.org',
'User-Agent': 'NodeJS HTTP Client',
'Content-Length': '0',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Bearer ' + this.$options.oauth_access_token
};
let query;
let path = this.$options.path + '/' + apiPath.replace(/\/*$/, '');
if (httpMethod === 'POST') {
if (method === 'POST') {
query = JSON.stringify(parameters);
headers['Content-Type'] = 'application/json';
headers['Content-Length'] = query.length;
if (!this.$options.use_xhr) {
headers['Content-Type'] = 'application/json';
headers['Content-Length'] = query.length;
}
}

@@ -232,10 +186,2 @@ else {

const getOptions = {
host,
post: port,
path,
method: httpMethod,
headers
};
let called = false;

@@ -251,6 +197,72 @@ function done(err, body) {

const request = https.request(getOptions, (response) => {
if (this.$options.use_xhr) {
const xhrOptions = {
method,
headers,
url: `https://${hostname}${path}`,
responseType: 'json',
timeout: this.$options.timeout * 1000
};
if (method === 'POST') {
xhrOptions.json = parameters;
}
this.sendXhrRequest(xhrOptions, done);
return;
}
const httpsOptions = {
headers,
hostname,
method,
path,
post: port
};
this.sendHttpsRequest(httpsOptions, query, done);
};
/**
* Get a JSON response and transform to JSON
*/
this.decodeResponse = function decodeResponse(response) {
if (this.$options.format === 'text') {
return response;
}
else if (this.$options.format === 'json') {
return JSON.parse(response);
}
};
this.prepRequest = function prepRequest(options) {
const {
hostname: _hostname,
http_port: httpPort,
oauth_access_token: oauthAccessToken,
proxy_host: proxyHost,
proxy_port: proxyPort,
use_xhr: useXhr
} = options;
const hostname = !useXhr && proxyHost ? proxyHost : _hostname;
const port = !useXhr && proxyHost ? proxyPort || 3128 : httpPort || 443;
const headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Bearer ' + oauthAccessToken
};
if (!useXhr) {
headers['Host'] = 'api.bitbucket.org'; // eslint-disable-line dot-notation
headers['User-Agent'] = 'NodeJS HTTP Client';
headers['Content-Lengthf'] = '0';
}
return { headers, hostname, port };
};
this.sendHttpsRequest = function sendHttpsRequest(httpsOptions, query, done) {
const request = https.request(httpsOptions, (response) => {
response.setEncoding('utf8');
let body = [];
const body = [];
response.addListener('data', (chunk) => {

@@ -260,12 +272,8 @@ body.push(chunk);

response.addListener('end', () => {
let msg;
body = body.join('');
let msg = body.join('');
if (response.statusCode > 204) {
if (response.headers['content-type'].includes('application/json')) {
msg = JSON.parse(body);
msg = JSON.parse(msg);
}
else {
msg = body;
}
done({ status: response.statusCode, msg });

@@ -275,6 +283,9 @@ return;

if (response.statusCode === 204) {
body = '{}';
msg = {};
}
else {
msg = this.decodeResponse(msg);
}
done(null, body);
done(null, msg);
});

@@ -295,21 +306,28 @@

if (httpMethod === 'POST') {
if (httpsOptions.method === 'POST') {
request.write(query);
}
request.end();
};
/**
* Get a JSON response and transform to JSON
*/
this.decodeResponse = function decodeResponse(response) {
if (this.$options.format === 'text') {
return response;
}
else if (this.$options.format === 'json') {
return JSON.parse(response);
}
this.sendXhrRequest = function sendXhrRequest(xhrOptions, done) {
xhr(xhrOptions, (error, response) => {
if (error) {
done(error);
return;
}
let msg = response.body;
if (response.statusCode > 204) {
done({ status: response.statusCode, msg });
return;
}
if (response.statusCode === 204) {
msg = {};
}
done(null, msg);
});
};
}).call(Request.prototype);

@@ -16,3 +16,6 @@ 'use strict';

var Bitbucket = function () {
function Bitbucket(proxy) {
function Bitbucket(_ref) {
var proxy = _ref.proxy;
var useXhr = _ref.useXhr;
_classCallCheck(this, Bitbucket);

@@ -28,3 +31,3 @@

this.request = new Request({ 'proxy_host': this.$proxy_host, 'proxy_port': this.$proxy_port });
this.request = new Request({ 'proxy_host': this.$proxy_host, 'proxy_port': this.$proxy_port, 'use_xhr': useXhr });

@@ -52,2 +55,3 @@ this.constants = {

_createClass(Bitbucket, [{

@@ -54,0 +58,0 @@ key: 'authenticateOAuth2',

@@ -36,2 +36,3 @@ 'use strict';

_createClass(RepositoriesApi, [{

@@ -65,3 +66,2 @@ key: 'createPullRequest',

*/
//TODO confirm this works

@@ -75,2 +75,15 @@ }, {

/**
* Get a single commit
* @param {String} repo owner
* @param {String} slug (name) of the repo.
* @param {String} the sha of the commit
*/
}, {
key: 'getCommit',
value: function getCommit(username, repoSlug, sha, callback) {
this.$api.get('repositories/' + encodeURI(username) + '/' + encodeURI(repoSlug) + '/commit/' + sha, null, null, this.$createListener(callback));
}
/**
* Get the pull requests for a single repo

@@ -96,3 +109,3 @@ *

var hasInvalidState = _.find(state, function (stateElement) {
return !_.contains(constants.pullRequest.states, stateElement);
return !_.includes(constants.pullRequest.states, stateElement);
});

@@ -99,0 +112,0 @@ if (hasInvalidState) {

'use strict';
var _ = require('lodash');
var https = require('https');
var querystring = require('querystring');
var https = require('https');
var url = require('url');
var xhr = require('xhr');

@@ -29,3 +31,4 @@ /**

proxy_host: null,
proxy_port: null
proxy_port: null,
use_xhr: false
};

@@ -36,7 +39,3 @@

this.$options = {};
for (var key in this.$defaults) {
this.$options[key] = options[key] !== undefined ? options[key] : this.$defaults[key];
}
this.$options = _.defaults({}, options, this.$defaults);
return this;

@@ -103,3 +102,3 @@ };

var initialOptions = undefined;
var initialOptions = void 0;
if (options) {

@@ -118,6 +117,6 @@ initialOptions = this.$options;

var response = _this.decodeResponse(_response);
var response = _this.$options.use_xhr ? _response : _this.decodeResponse(_response);
if (initialOptions) {
_this.options = initialOptions;
_this.$options = initialOptions;
}

@@ -136,24 +135,8 @@ if (callback) {

this.doPrebuiltSend = function doPrebuiltSend(prebuiltURL, callback) {
var _this2 = this;
var _prepRequest = this.prepRequest(this.$options);
var port = this.$options.proxy_host ? this.$options.proxy_port || 3128 : this.$options.http_port || 443;
var headers = _prepRequest.headers;
var port = _prepRequest.port;
var headers = {
'Host': 'api.bitbucket.org',
'User-Agent': 'NodeJS HTTP Client',
'Content-Length': '0',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Bearer ' + this.$options.oauth_access_token
};
var parsedUrl = url.parse(prebuiltURL);
var getOptions = {
headers: headers,
hostname: parsedUrl.hostname,
method: 'GET',
path: parsedUrl.path,
post: port
};
var called = false;

@@ -169,42 +152,27 @@ function done(err, body) {

var request = https.request(getOptions, function (response) {
response.setEncoding('utf8');
if (this.$options.use_xhr) {
var xhrOptions = {
url: prebuiltURL,
responseType: 'json',
timeout: this.$options.timeout * 1000
};
var body = [];
response.addListener('data', function (chunk) {
body.push(chunk);
});
response.addListener('end', function () {
var msg = body.join('');
this.sendXhrRequest(xhrOptions, done);
return;
}
if (response.statusCode > 204) {
if (response.headers['content-type'].includes('application/json')) {
msg = JSON.parse(body);
} else {
msg = body;
}
var _url$parse = url.parse(prebuiltURL);
done({ status: response.statusCode, msg: msg });
return;
}
if (response.statusCode === 204) {
msg = '{}';
}
done(null, _this2.decodeResponse(msg));
});
var hostname = _url$parse.hostname;
var path = _url$parse.path;
response.addListener('error', function (e) {
done(e);
});
var httpsOptions = {
headers: headers,
hostname: hostname,
method: 'GET',
path: path,
post: port
};
response.addListener('timeout', function () {
done(new Error('Request timed out'));
});
});
request.on('error', function (e) {
done(e);
});
request.end();
this.sendHttpsRequest(httpsOptions, undefined, done);
};

@@ -220,20 +188,19 @@

this.doSend = function doSend(apiPath, parameters, _httpMethod, callback) {
var httpMethod = _httpMethod.toUpperCase();
var host = this.$options.proxy_host ? this.$options.proxy_host : this.$options.hostname;
var port = this.$options.proxy_host ? this.$options.proxy_port || 3128 : this.$options.http_port || 443;
var method = _httpMethod.toUpperCase();
var headers = {
'Host': 'api.bitbucket.org',
'User-Agent': 'NodeJS HTTP Client',
'Content-Length': '0',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Bearer ' + this.$options.oauth_access_token
};
var _prepRequest2 = this.prepRequest(this.$options);
var query = undefined;
var headers = _prepRequest2.headers;
var hostname = _prepRequest2.hostname;
var port = _prepRequest2.port;
var query = void 0;
var path = this.$options.path + '/' + apiPath.replace(/\/*$/, '');
if (httpMethod === 'POST') {
if (method === 'POST') {
query = JSON.stringify(parameters);
headers['Content-Type'] = 'application/json';
headers['Content-Length'] = query.length;
if (!this.$options.use_xhr) {
headers['Content-Type'] = 'application/json';
headers['Content-Length'] = query.length;
}
} else {

@@ -244,10 +211,2 @@ query = querystring.stringify(parameters);

var getOptions = {
host: host,
post: port,
path: path,
method: httpMethod,
headers: headers
};
var called = false;

@@ -263,3 +222,69 @@ function done(err, body) {

var request = https.request(getOptions, function (response) {
if (this.$options.use_xhr) {
var xhrOptions = {
method: method,
headers: headers,
url: 'https://' + hostname + path,
responseType: 'json',
timeout: this.$options.timeout * 1000
};
if (method === 'POST') {
xhrOptions.json = parameters;
}
this.sendXhrRequest(xhrOptions, done);
return;
}
var httpsOptions = {
headers: headers,
hostname: hostname,
method: method,
path: path,
post: port
};
this.sendHttpsRequest(httpsOptions, query, done);
};
/**
* Get a JSON response and transform to JSON
*/
this.decodeResponse = function decodeResponse(response) {
if (this.$options.format === 'text') {
return response;
} else if (this.$options.format === 'json') {
return JSON.parse(response);
}
};
this.prepRequest = function prepRequest(options) {
var _hostname = options.hostname;
var httpPort = options.http_port;
var oauthAccessToken = options.oauth_access_token;
var proxyHost = options.proxy_host;
var proxyPort = options.proxy_port;
var useXhr = options.use_xhr;
var hostname = !useXhr && proxyHost ? proxyHost : _hostname;
var port = !useXhr && proxyHost ? proxyPort || 3128 : httpPort || 443;
var headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Bearer ' + oauthAccessToken
};
if (!useXhr) {
headers['Host'] = 'api.bitbucket.org'; // eslint-disable-line dot-notation
headers['User-Agent'] = 'NodeJS HTTP Client';
headers['Content-Lengthf'] = '0';
}
return { headers: headers, hostname: hostname, port: port };
};
this.sendHttpsRequest = function sendHttpsRequest(httpsOptions, query, done) {
var _this2 = this;
var request = https.request(httpsOptions, function (response) {
response.setEncoding('utf8');

@@ -272,10 +297,7 @@

response.addListener('end', function () {
var msg = undefined;
body = body.join('');
var msg = body.join('');
if (response.statusCode > 204) {
if (response.headers['content-type'].includes('application/json')) {
msg = JSON.parse(body);
} else {
msg = body;
msg = JSON.parse(msg);
}

@@ -286,6 +308,8 @@ done({ status: response.statusCode, msg: msg });

if (response.statusCode === 204) {
body = '{}';
msg = {};
} else {
msg = _this2.decodeResponse(msg);
}
done(null, body);
done(null, msg);
});

@@ -306,3 +330,3 @@

if (httpMethod === 'POST') {
if (httpsOptions.method === 'POST') {
request.write(query);

@@ -314,12 +338,21 @@ }

/**
* Get a JSON response and transform to JSON
*/
this.decodeResponse = function decodeResponse(response) {
if (this.$options.format === 'text') {
return response;
} else if (this.$options.format === 'json') {
return JSON.parse(response);
}
this.sendXhrRequest = function sendXhrRequest(xhrOptions, done) {
xhr(xhrOptions, function (error, response) {
if (error) {
done(error);
return;
}
var msg = response.body;
if (response.statusCode > 204) {
done({ status: response.statusCode, msg: msg });
return;
}
if (response.statusCode === 204) {
msg = {};
}
done(null, msg);
});
};
}).call(Request.prototype);

@@ -30,2 +30,3 @@ 'use strict';

_createClass(UserApi, [{

@@ -32,0 +33,0 @@ key: 'get',

{
"name": "bitbucket-v2",
"version": "0.1.3",
"version": "0.1.4",
"description": "Wrapper for the BitBucket API v2",

@@ -22,4 +22,5 @@ "author": "Jordan Wallet <jjwallet@gmail.com>",

"asyncjs": "~0.0.10",
"lodash": "^3.10.1",
"oauth": "~0.9.14"
"lodash": "^4.6.1",
"oauth": "~0.9.14",
"xhr": "^2.2.0"
},

@@ -33,8 +34,3 @@ "devDependencies": {

},
"licenses": [
{
"type": "The MIT License",
"url": "http://www.opensource.org/licenses/mit-license.php"
}
]
"license": "MIT"
}
# node-bitbucket-v2
node.js library to access the Bitbucket API v2
## usage
```
const Bitbucket = require('node-bitbucket-v2')
const bitbucketApi = new Bitbucket(); //or: new Bitbucket({useXhr: true})
bitbucketApi.authenticateOAuth2(someAccessToken);
bitbucketApi.user.get((response) => {
console.log(response.username);
});
```
For implemented methods, check `bitbucket/repositories.js` and `bitbucket/user.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