Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,31 +443,33 @@ module.exports = function(/*options, callback*/) {
function (callback) {
let repository = options.repositories[repositoryIndex];
let url = repository.url + urlPath;
let req_options = { url: url };
// Without responseType axios decodes the body as UTF-8 text, which corrupts every jar.
let req_options = { url: url, responseType: 'arraybuffer' };
if (repository.hasOwnProperty('credentials')) {
let username = repository.credentials.username;
let password = repository.credentials.password;
req_options = {
url: url,
auth: {
username,
password
}
req_options.auth = {
username,
password
};
}
debug('downloading ' + url);
axios.get(url, req_options).catch(err => {
error = err
return callback();
}).then(res => {
fs.promises.writeFile(destinationFile, res.data).then(() => {
foundUrl = url;
return callback()
}).catch(() => {
return callback();
})
})
repositoryIndex++;
axios.get(url, req_options)
.then(
(res) => fs.promises.writeFile(destinationFile, res.data).then(() => {
foundUrl = url;
}),
(err) => {
const status = err.response ? err.response.status : err.code;
error = new Error('download failed for ' + url + (reason ? ' (' + reason + ')' : '') + ' [status: ' + status + ']');
}
)
.catch((err) => {
error = err;
})
// Outside the promise chain, so a throw in the next step is not swallowed as an unhandled rejection.
.then(() => process.nextTick(callback));
},
function () {
if (foundUrl) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cubejs-backend/node-java-maven",
"version": "0.1.3",
"version": "0.1.4",
"description": "Utility for Node's java module to load mvn dependencies.",
"main": "index.js",
"bin": {
Expand Down
11 changes: 11 additions & 0 deletions test/missing-artifact.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"java": {
"dependencies": [
{
"groupId": "com.example.doesnotexist",
"artifactId": "missing-artifact",
"version": "1.0.0"
}
]
}
}
40 changes: 40 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
var assert = require('assert');
var crypto = require('crypto');
var fs = require('fs-extra');
var path = require('path');
var axios = require('axios').default;
var mvn = require('../index');
var REMOVE_M2_DIR_AFTER_EACH_TEST = true;

Expand All @@ -16,6 +19,43 @@ describe('maven', function() {
testJson('org.springframework_spring_2.0.6.json');
testJson('com.googlecode.netlib-java.netlib-java.json');

it('should download jars byte-for-byte as published', function(done) {
this.timeout(120 * 1000);
var opts = {
localRepository: localRepository,
packageJsonPath: path.join(__dirname, 'com.j256.ormlite_ormlite-jdbc_4.48.json')
};
mvn(opts, function(err, mvnResults) {
if (err) {
return done(err);
}
assert.ok(mvnResults.classpath.length > 0, 'expected jars on the classpath');
Promise.all(mvnResults.classpath.map(function(jarPath) {
var relativePath = path.relative(localRepository, jarPath).split(path.sep).join('/');
return axios.get('https://repo1.maven.org/maven2/' + relativePath + '.sha1', { responseType: 'text' })
.then(function(res) {
var expected = res.data.trim().split(/\s+/)[0];
var actual = crypto.createHash('sha1').update(fs.readFileSync(jarPath)).digest('hex');
assert.strictEqual(actual, expected, relativePath);
});
})).then(function() { done(); }, done);
});
});

it('should report a missing artifact through the callback', function(done) {
this.timeout(60 * 1000);
var opts = {
localRepository: localRepository,
packageJsonPath: path.join(__dirname, 'missing-artifact.json')
};
mvn(opts, function(err) {
// Resolution errors come back as an array, one entry per failed dependency.
assert.ok(Array.isArray(err) && err.length === 1, 'expected one error for a missing artifact');
assert.match(err[0].message, /download failed for .*missing-artifact.* \[status: 404\]/);
done();
});
});

function testJson(jsonPath) {
it('should pull the maven dependencies for ' + jsonPath, function(done) {
this.timeout(120 * 1000);
Expand Down
Loading