<commit_before>/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

/*
 * Copyright (c) 2014, Joyent, Inc.
 */

var assert = require('assert-plus');
var bunyan = require('bunyan');
var fmt = require('util').format;
var idxClient = require('./idx');
var restify = require('restify');
var VError = require('verror').VError;



// --- Globals



var REGISTRY_URL = 'https://registry-1.docker.io';



// --- Registry object



/**
 * Creates a new registry client
 */
function Registry(opts) {
    assert.object(opts, 'opts');
    assert.string(opts.repo, 'opts.repo');
    assert.string(opts.token, 'opts.token');
    assert.optionalObject(opts.log, 'opts.log');

    this.authHeader = 'Token ' + opts.token;
    this.log = opts.log || bunyan.createLogger({ name: 'registry' });
    this.repo = opts.repo;
    this.url = opts.url || REGISTRY_URL;
}


Registry.prototype.jsonClient = function jsonClient(opts) {
    return restify.createJsonClient({
        log: this.log,
        url: this.url
    });
};


/**
 * Gets the image's ancestry: all of the image layers that are required for
 * it to be functional
 */
Registry.prototype.getImageAncestry = function getImageAncestry(img, callback) {
    var client = this.jsonClient();
    client.get({
        path: fmt('/v1/images/%s/ancestry', img),
        headers: {
            Authorization: this.authHeader
        }
    }, function _afterImageAncestry(err, req, res, obj) {
        if (err) {
            return callback(err);
        }

        return callback(null, obj);
    });
};


/**
 * Gets the remote image's Metadata
 */
Registry.prototype.getImageMetadata =
    function getImageMetadata(img, callback) {
    var client = this.jsonClient();
    client.get({
        path: fmt('/v1/images/%s/json', img),
        headers: {
            Authorization: this.authHeader
        }
    }, function _afterRemoteImageJSON(err, req, res, obj) {
        if (err) {
            return callback(err);
        }

        return callback(null, obj);
    });
};


/**
 * Gets the remote image's JSON
 */
Registry.prototype.getRepoTags = function getRepoTags(callback) {
    var client = this.jsonClient();
    client.get({
        path: fmt('/v1/repositories/%s/tags', this.repo),
        headers: {
            Authorization: this.authHeader
        }
    }, function _afterRepoTags(err, req, res, obj) {
        if (err) {
            return callback(err);
        }

        return callback(null, obj);
    });
};<commit_msg>DOCKER-5 getImageMetadata() should return the image size, if possible<commit_after>/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

/*
 * Copyright (c) 2014, Joyent, Inc.
 */

var assert = require('assert-plus');
var bunyan = require('bunyan');
var fmt = require('util').format;
var idxClient = require('./idx');
var restify = require('restify');
var VError = require('verror').VError;



// --- Globals



var REGISTRY_URL = 'https://registry-1.docker.io';



// --- Registry object



/**
 * Creates a new registry client
 */
function Registry(opts) {
    assert.object(opts, 'opts');
    assert.string(opts.repo, 'opts.repo');
    assert.string(opts.token, 'opts.token');
    assert.optionalObject(opts.log, 'opts.log');

    this.authHeader = 'Token ' + opts.token;
    this.log = opts.log || bunyan.createLogger({ name: 'registry' });
    this.repo = opts.repo;
    this.url = opts.url || REGISTRY_URL;
}


Registry.prototype.jsonClient = function jsonClient(opts) {
    return restify.createJsonClient({
        log: this.log,
        url: this.url
    });
};


/**
 * Gets the image's ancestry: all of the image layers that are required for
 * it to be functional
 */
Registry.prototype.getImageAncestry = function getImageAncestry(img, callback) {
    var client = this.jsonClient();
    client.get({
        path: fmt('/v1/images/%s/ancestry', img),
        headers: {
            Authorization: this.authHeader
        }
    }, function _afterImageAncestry(err, req, res, obj) {
        if (err) {
            return callback(err);
        }

        return callback(null, obj);
    });
};


/**
 * Gets the remote image's Metadata
 */
Registry.prototype.getImageMetadata =
    function getImageMetadata(img, callback) {
    var client = this.jsonClient();
    client.get({
        path: fmt('/v1/images/%s/json', img),
        headers: {
            Authorization: this.authHeader
        }
    }, function _afterRemoteImageJSON(err, req, res, obj) {
        if (err) {
            return callback(err);
        }

        var size = res.headers['x-docker-size'];
        var imgData = {
            json: obj,
            size: size ? Number(size) : -1
        };

        return callback(null, imgData);
    });
};


/**
 * Gets the remote image's JSON
 */
Registry.prototype.getRepoTags = function getRepoTags(callback) {
    var client = this.jsonClient();
    client.get({
        path: fmt('/v1/repositories/%s/tags', this.repo),
        headers: {
            Authorization: this.authHeader
        }
    }, function _afterRepoTags(err, req, res, obj) {
        if (err) {
            return callback(err);
        }

        return callback(null, obj);
    });
};
{'fraction_non_alphanumeric': 0.14337899543378996, 'fraction_numerical': 0.004748858447488584, 'mean_word_length': 2.49234693877551, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6503724', 'n_tokens_mistral': 1726, 'n_tokens_neox': 1649, 'n_words': 429}