planet-client
Advanced tools
Comparing version 0.13.0 to 0.14.0
@@ -6,1 +6,2 @@ exports.auth = require('./auth'); | ||
exports.workspaces = require('./workspaces'); | ||
exports.aois = require('./aois'); |
@@ -15,2 +15,3 @@ /** | ||
var assign = require('./util').assign; | ||
var util = require('./util'); | ||
var authStore = require('./auth-store'); | ||
@@ -25,2 +26,4 @@ var errors = require('./errors'); | ||
var boundary = generateBoundary(); | ||
/** | ||
@@ -35,6 +38,9 @@ * Generate request options provided a config object. | ||
var base; | ||
if (config.url) { | ||
var resolved; | ||
if (typeof location !== 'undefined') { | ||
resolved = url.resolve(location.href, config.url); | ||
var currentLocation = util.currentLocation(); | ||
if (typeof currentLocation !== 'undefined') { | ||
resolved = url.resolve(currentLocation.href, config.url); | ||
} else { | ||
@@ -71,2 +77,6 @@ resolved = config.url; | ||
} | ||
if (config.file) { | ||
headers['content-type'] = 'multipart/form-data; boundary=' + boundary; | ||
headers['content-length'] = byteCount(toMultipartUpload(config.file)); | ||
} | ||
@@ -242,2 +252,5 @@ if (config.withCredentials !== false) { | ||
} | ||
if (config.file) { | ||
client.write(toMultipartUpload(config.file)); | ||
} | ||
client.end(); | ||
@@ -319,2 +332,49 @@ | ||
/** | ||
* Converts a file object to a multipart payload. The file is assumed to have | ||
* textual content and to conform to the | ||
* [File](https://developer.mozilla.org/en-US/docs/Web/API/File) interface. | ||
* | ||
* Note: this isn't binary-safe. | ||
* | ||
* @param {File} file A File-like object conforming to the HTML File api. | ||
* @return {String} A multipart request body for a file upload. | ||
*/ | ||
function toMultipartUpload(file) { | ||
return [ | ||
'--' + boundary, | ||
'\r\n', | ||
'Content-Type: application/json; charset=utf-8', | ||
'\r\n', | ||
'Content-Disposition: form-data; name="file"; filename="' + file.name + '"', | ||
'\r\n\r\n', | ||
file.contents, | ||
'\r\n', | ||
'--' + boundary + '--' | ||
].join(''); | ||
} | ||
/** | ||
* Returns the length in bytes of a string. | ||
* @param {String} source A string whose length we wish to count. | ||
* @return {Number} The byte-length of a string | ||
*/ | ||
function byteCount(source) { | ||
return encodeURI(source).split(/%..|./).length - 1; | ||
} | ||
/** | ||
* Returns a boundary, generating a new one and memoizing it if necessary. | ||
* | ||
* @return {String} A 24 character hex string string to use as a multipart | ||
* boundary. | ||
*/ | ||
function generateBoundary() { | ||
var newBoundary = []; | ||
for (var i = 0; i < 24; i++) { | ||
newBoundary.push(Math.floor(Math.random() * 16).toString(16)); | ||
} | ||
return newBoundary.join(''); | ||
} | ||
exports.get = get; | ||
@@ -321,0 +381,0 @@ exports.post = post; |
@@ -50,3 +50,4 @@ /** | ||
exports.login = rootUrl('auth', 'login'); | ||
exports.aois = rootUrl('aois', ''); | ||
exports.join = join; |
@@ -41,3 +41,20 @@ /** | ||
/** | ||
* Get the current location. More readily mocked than using the global | ||
* directly. | ||
* | ||
* @return {Location} The current location. | ||
*/ | ||
function currentLocation() { | ||
/* location is tricky to mock in the browser */ | ||
/* istanbul ignore if */ | ||
if (typeof location !== 'undefined') { | ||
return location; | ||
} else { | ||
return undefined; | ||
} | ||
} | ||
exports.addQueryParams = addQueryParams; | ||
exports.assign = assign; | ||
exports.currentLocation = currentLocation; |
{ | ||
"name": "planet-client", | ||
"version": "0.13.0", | ||
"version": "0.14.0", | ||
"description": "A client for Planet's imagery API", | ||
@@ -16,4 +16,6 @@ "repository": { | ||
"pretest": "eslint bin examples api cli test", | ||
"test": "nyc mocha --recursive test", | ||
"coverage": "nyc report --reporter=text-lcov | coveralls", | ||
"test-node": "nyc mocha --recursive test", | ||
"test-browser": "karma start test/karma.config.js --single-run", | ||
"test": "npm run test-node && npm run test-browser", | ||
"coverage": "nyc report --reporter=html", | ||
"test-debug": "mocha --debug-brk --recursive test", | ||
@@ -46,2 +48,7 @@ "start": "watchy --watch bin,examples,api,cli,test -- npm test", | ||
"jsdoc-json": "^2.0.0", | ||
"karma": "^0.13.9", | ||
"karma-browserify": "^4.3.0", | ||
"karma-chrome-launcher": "^0.2.0", | ||
"karma-mocha": "^0.2.0", | ||
"karma-sauce-launcher": "^0.2.14", | ||
"marked": "^0.3.5", | ||
@@ -54,2 +61,3 @@ "metalsmith": "^2.0.1", | ||
"nyc": "^3.1.0", | ||
"readable-stream": "^2.0.2", | ||
"sinon": "^1.15.3", | ||
@@ -56,0 +64,0 @@ "watchy": "^0.6.2" |
@@ -5,3 +5,3 @@ /* eslint-env mocha */ | ||
var https = require('https'); | ||
var stream = require('stream'); | ||
var stream = require('readable-stream'); | ||
@@ -8,0 +8,0 @@ var assert = require('chai').assert; |
/* eslint-env mocha */ | ||
var http = require('http'); | ||
var https = require('https'); | ||
var stream = require('stream'); | ||
var stream = require('readable-stream'); | ||
var url = require('url'); | ||
@@ -27,11 +27,23 @@ | ||
mockRequest = { | ||
_read: sinon.spy(), | ||
end: sinon.spy(), | ||
abort: sinon.spy() | ||
}; | ||
http.request = sinon.spy(function() { | ||
var httpRequestMock = http.request = sinon.spy(function() { | ||
return mockRequest; | ||
}); | ||
https.request = sinon.spy(function() { | ||
var httpsRequestMock = https.request = sinon.spy(function() { | ||
return mockRequest; | ||
}); | ||
// When doing browser testing via karma the http/s module required by | ||
// request.js doesn't invoke the mock we've defined above. | ||
httpRequestMock.get = http.get = sinon.spy(function() { | ||
arguments[0].method = 'GET'; | ||
return httpRequestMock.apply(this, arguments); | ||
}); | ||
httpsRequestMock.get = https.get = sinon.spy(function() { | ||
arguments[0].method = 'GET'; | ||
return httpsRequestMock.apply(this, arguments); | ||
}); | ||
}); | ||
@@ -453,10 +465,2 @@ | ||
var existingLocation; | ||
beforeEach(function() { | ||
existingLocation = global.location; | ||
}); | ||
afterEach(function() { | ||
global.location = existingLocation; | ||
}); | ||
it('generates request options from a URL', function() { | ||
@@ -478,42 +482,51 @@ var config = { | ||
it('resolves a relative URL if location is defined', function() { | ||
global.location = { | ||
href: 'http://example.com/foo/bar.html' | ||
}; | ||
describe('relative urls', function() { | ||
var originalLocation = util.currentLocation; | ||
var config = { | ||
url: './relative/path/to/data.json' | ||
}; | ||
beforeEach(function() { | ||
util.currentLocation = function() { | ||
return { | ||
href: 'http://example.com/foo/bar.html' | ||
}; | ||
}; | ||
}); | ||
var options = { | ||
protocol: 'http:', | ||
hostname: 'example.com', | ||
port: '80', | ||
method: 'GET', | ||
path: '/foo/relative/path/to/data.json', | ||
headers: defaultHeaders | ||
}; | ||
afterEach(function() { | ||
util.currentLocation = originalLocation; | ||
}); | ||
assert.deepEqual(parseConfig(config), options); | ||
}); | ||
it('resolves a relative URL if location is defined', function() { | ||
var config = { | ||
url: './relative/path/to/data.json' | ||
}; | ||
it('works for root relative URLs', function() { | ||
global.location = { | ||
href: 'http://example.com/foo/bar.html' | ||
}; | ||
var options = { | ||
protocol: 'http:', | ||
hostname: 'example.com', | ||
port: '80', | ||
method: 'GET', | ||
path: '/foo/relative/path/to/data.json', | ||
headers: defaultHeaders | ||
}; | ||
var config = { | ||
url: '/root/path/to/data.json' | ||
}; | ||
assert.deepEqual(parseConfig(config), options); | ||
}); | ||
var options = { | ||
protocol: 'http:', | ||
hostname: 'example.com', | ||
port: '80', | ||
method: 'GET', | ||
path: '/root/path/to/data.json', | ||
headers: defaultHeaders | ||
}; | ||
it('works for root relative URLs', function() { | ||
var config = { | ||
url: '/root/path/to/data.json' | ||
}; | ||
assert.deepEqual(parseConfig(config), options); | ||
var options = { | ||
protocol: 'http:', | ||
hostname: 'example.com', | ||
port: '80', | ||
method: 'GET', | ||
path: '/root/path/to/data.json', | ||
headers: defaultHeaders | ||
}; | ||
assert.deepEqual(parseConfig(config), options); | ||
}); | ||
}); | ||
@@ -520,0 +533,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 4 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
147288
64
4189
25
19
19