Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use native http module instead of request dependency #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
49 changes: 41 additions & 8 deletions lib/images.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,16 @@ module.exports = function (dolphin) {
// TODO: registry auth.
// TODO: default registries, etc.
//
var request = require('request');
images.manifest = function (name, tag) {
return new Promise(function (resolve, reject) {
var splitted = name.split('/');
if (splitted.length > 1) {
var registry = splitted[0];
var repo = splitted[1];
var opts = {
url: 'https://' + registry + '/v2/' + repo + '/manifests/' + tag,
method: 'GET',
json: true,
}

request(opts, function (err, response, body) {
getJSON('https://' + registry + '/v2/' + repo + '/manifests/' + tag, function (err, data) {
if (err) return reject(err);
resolve(body);
resolve(data);
});
} else {
resolve();
Expand All @@ -125,3 +119,42 @@ module.exports = function (dolphin) {

return images;
}

var http = require('http');
function getJSON(url, callback) {
http.get(url, function (res) {
var statusCode = res.statusCode;
var contentType = res.headers['content-type'];

var error;
if (statusCode !== 200) {
error = new Error('Request Failed.\n' +
`Status Code: ${statusCode}`);

} else if (!/^application\/json/.test(contentType)) {
error = new Error('Invalid content-type.\n' +
`Expected application/json but received ${contentType}`);
}

if (error) {
// Consume response data to free up memory
res.resume();
callback(error.message);
return;
}

res.setEncoding('utf8');
var rawData = '';
res.on('data', (chunk) => { rawData += chunk; });
res.on('end', () => {
try {
callback(undefined, JSON.parse(rawData));

} catch (e) {
callback(e.message);
}
});
}).on('error', (e) => {
callback(e.message);
});
}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"homepage": "https://github.com/OptimalBits/dolphin",
"dependencies": {
"bluebird": "^2.9.24",
"lodash": "^4.15.0",
"request": "^2.65.0"
"lodash": "^4.15.0"
}
}