var fs = require('fs') , Path = require('path') , crypto = require('crypto') , assert = require('assert') , fs_storage = require('./local-fs') , UError = require('./error').UserError , utils = require('./utils') , mystreams = require('./streams') , Logger = require('./logger') , info_file = 'package.json' , localList = require('./local-list') , targz = require('tar.gz') , search = require('./search'); // // Implements Storage interface // (same for storage.js, local-storage.js, up-storage.js) // function Storage(config) { if (!(this instanceof Storage)) return new Storage(config) this.config = config this.logger = Logger.logger.child({sub: 'fs'}) return this } // returns the minimal package file function get_boilerplate(name) { return { // standard things name: name, versions: {}, 'dist-tags': {}, // our own object '_distfiles': {}, '_attachments': {}, '_uplinks': {}, } } Storage.prototype._internal_error = function(err, file, msg) { this.logger.error( {err: err, file: file} , msg + ' @{file}: @{!err.message}' ) return new UError({ status: 500, msg: 'internal server error' }) } Storage.prototype.add_package = function(name, package, callback) { this.storage(name).create_json(info_file, get_boilerplate(name), function(err) { if (err && err.code === 'EEXISTS') { return callback(new UError({ status: 409, msg: 'this package is already present' })) } callback() }) console.log(package); search.add(package.versions[package['dist-tags'].latest]); localList.add(name); } Storage.prototype.remove_package = function(name, callback) { var self = this self.logger.info({name: name}, 'unpublishing @{name} (all)') self.storage(name).read_json(info_file, function(err, data) { if (err) { if (err.code === 'ENOENT') { return callback(new UError({ status: 404, msg: 'no such package available', })) } else { return callback(err) } } self._normalize_package(data) self.storage(name).unlink(info_file, function(err) { if (err) return callback(err) var files = Object.keys(data._attachments) function unlinkNext(cb) { if (files.length === 0) return cb() var file = files.shift() self.storage(name).unlink(file, function() { unlinkNext(cb) }) } unlinkNext(function() { // try to unlink the directory, but ignore errors because it can fail self.storage(name).rmdir('.', function(err) { callback(err) }); }); }); }); localList.remove(name); } Storage.prototype._read_create_package = function(name, callback) { var self = this self.storage(name).read_json(info_file, function(err, data) { // TODO: race condition if (err) { if (err.code === 'ENOENT') { // if package doesn't exist, we create it here data = get_boilerplate(name) } else { return callback(self._internal_error(err, info_file, 'error reading')) } } self._normalize_package(data) callback(null, data) }) } // synchronize remote package info with the local one // TODO: readfile called twice Storage.prototype.update_versions = function(name, newdata, callback) { var self = this self._read_create_package(name, function(err, data) { if (err) return callback(err) var change = false for (var ver in newdata.versions) { if (data.versions[ver] == null) { var verdata = newdata.versions[ver] // why does anyone need to keep that in database? delete verdata.readme change = true data.versions[ver] = verdata if (verdata.dist && verdata.dist.tarball) { var url = utils.parse_tarball_url( /*verdata.dist.__sinopia_orig_tarball ||*/ verdata.dist.tarball ) // we do NOT overwrite any existing records if (url != null && data._distfiles[url.filename] == null) { data._distfiles[url.filename] = { url: /*verdata.dist.__sinopia_orig_tarball ||*/ verdata.dist.tarball, sha: verdata.dist.shasum, } } } } } for (var tag in newdata['dist-tags']) { if (!Array.isArray(data['dist-tags'][tag]) || data['dist-tags'][tag].length != newdata['dist-tags'][tag].length) { // backward compat var need_change = true } else { for (var i=0; i