From 112cee789eaad3177ba32e34339a35298b6a79b6 Mon Sep 17 00:00:00 2001 From: "Juan Picado @jotadeveloper" <juanpicado19@gmail.com> Date: Sun, 13 Aug 2017 17:37:40 +0200 Subject: [PATCH] refactor: remove local-storage in favor of the new dependency --- src/lib/storage.js | 2 +- src/lib/storage/local/local-fs.js | 256 ---------------------------- src/lib/{storage => }/up-storage.js | 4 +- test/unit/no_proxy.js | 2 +- 4 files changed, 4 insertions(+), 260 deletions(-) delete mode 100644 src/lib/storage/local/local-fs.js rename src/lib/{storage => }/up-storage.js (99%) diff --git a/src/lib/storage.js b/src/lib/storage.js index 18e0e4461..5f87eb0f0 100644 --- a/src/lib/storage.js +++ b/src/lib/storage.js @@ -11,7 +11,7 @@ const Search = require('./search'); const Logger = require('./logger'); const LocalStorage = require('@verdaccio/local-storage').LocalStorage; const MyStreams = require('@verdaccio/streams'); -const Proxy = require('./storage/up-storage'); +const Proxy = require('./up-storage'); const Utils = require('./utils'); const WHITELIST = ['_rev', 'name', 'versions', 'dist-tags', 'readme', 'time']; diff --git a/src/lib/storage/local/local-fs.js b/src/lib/storage/local/local-fs.js deleted file mode 100644 index ca319d209..000000000 --- a/src/lib/storage/local/local-fs.js +++ /dev/null @@ -1,256 +0,0 @@ -/* eslint prefer-spread: "off" */ - -'use strict'; - -const fs = require('fs'); -const path = require('path'); -const createError = require('http-errors'); -const mkdirp = require('mkdirp'); -const MyStream = require('@verdaccio/streams'); -const locker = require('@verdaccio/file-locking'); -const fileExist = 'EEXISTS'; -const noSuchFile = 'ENOENT'; - -const fSError = function(code) { - const err = createError(code); - err.code = code; - return err; -}; - -const readFile = function(name) { - return new Promise((resolve, reject) => { - fs.readFile(name, (err, data) => { - if (err) { - reject(err); - } else { - resolve(data); - } - }); - }); -}; - -const tempFile = function(str) { - return `${str}.tmp${String(Math.random()).substr(2)}`; -}; - -const renameTmp = function(src, dst, _cb) { - const cb = function(err) { - if (err) { - fs.unlink(src, function() {}); - } - _cb(err); - }; - - if (process.platform !== 'win32') { - return fs.rename(src, dst, cb); - } - - // windows can't remove opened file, - // but it seem to be able to rename it - const tmp = tempFile(dst); - fs.rename(dst, tmp, function(err) { - fs.rename(src, dst, cb); - if (!err) { - fs.unlink(tmp, () => {}); - } - }); -}; - -const writeFile = function(dest, data, cb) { - const createTempFile = function(cb) { - const tempFilePath = tempFile(dest); - fs.writeFile(tempFilePath, data, function(err) { - if (err) { - return cb(err); - } - renameTmp(tempFilePath, dest, cb); - }); - }; - - createTempFile(function(err) { - if (err && err.code === noSuchFile) { - mkdirp(path.dirname(dest), function(err) { - if (err) { - return cb(err); - } - createTempFile(cb); - }); - } else { - cb(err); - } - }); -}; - -const createWriteStream = function(name) { - const uploadStream = new MyStream.UploadTarball(); - let _ended = 0; - uploadStream.on('end', function() { - _ended = 1; - }); - - fs.exists(name, function(exists) { - if (exists) { - return uploadStream.emit('error', fSError(fileExist)); - } - - const temporalName = `${name}.tmp-${String(Math.random()).replace(/^0\./, '')}`; - const file = fs.createWriteStream(temporalName); - let opened = false; - uploadStream.pipe(file); - - uploadStream.done = function() { - const onend = function() { - file.on('close', function() { - renameTmp(temporalName, name, function(err) { - if (err) { - uploadStream.emit('error', err); - } else { - uploadStream.emit('success'); - } - }); - }); - file.destroySoon(); - }; - if (_ended) { - onend(); - } else { - uploadStream.on('end', onend); - } - }; - uploadStream.abort = function() { - if (opened) { - opened = false; - file.on('close', function() { - fs.unlink(temporalName, function() {}); - }); - } - file.destroySoon(); - }; - file.on('open', function() { - opened = true; - // re-emitting open because it's handled in storage.js - uploadStream.emit('open'); - }); - file.on('error', function(err) { - uploadStream.emit('error', err); - }); - }); - return uploadStream; -}; - -const createReadStream = function(name, readTarballStream, callback) { - let readStream = fs.createReadStream(name); - readStream.on('error', function(err) { - readTarballStream.emit('error', err); - }); - readStream.on('open', function(fd) { - fs.fstat(fd, function(err, stats) { - if (err) return readTarballStream.emit('error', err); - readTarballStream.emit('content-length', stats.size); - readTarballStream.emit('open'); - readStream.pipe(readTarballStream); - }); - }); - - readTarballStream = new MyStream.ReadTarball(); - readTarballStream.abort = function() { - readStream.close(); - }; - return readTarballStream; -}; - -const createFile = function(name, contents, callback) { - fs.exists(name, function(exists) { - if (exists) { - return callback( fSError(fileExist) ); - } - writeFile(name, contents, callback); - }); -}; - -const updateFile = function(name, contents, callback) { - fs.exists(name, function(exists) { - if (!exists) { - return callback( fSError(noSuchFile) ); - } - writeFile(name, contents, callback); - }); -}; - -const readJSON = function(name, cb) { - readFile(name).then(function(res) { - let args = []; - try { - args = [null, JSON.parse(res.toString('utf8'))]; - } catch(err) { - args = [err]; - } - cb.apply(null, args); - }, function(err) { - return cb(err); - }); -}; - -const lock_and_read = function(name, cb) { - locker.readFile(name, {lock: true}, function(err, res) { - if (err) { - return cb(err); - } - return cb(null, res); - }); -}; - -const lockAndReadJSON = function(name, cb) { - locker.readFile(name, { - lock: true, - parse: true, - }, function(err, res) { - if (err) { - return cb(err); - } - return cb(null, res); - }); -}; - -const unlock_file = function(name, cb) { - locker.unlockFile(name, cb); -}; - -const createJSON = function(name, value, cb) { - createFile(name, JSON.stringify(value, null, '\t'), cb); -}; - - -const updateJSON = function(name, value, cb) { - updateFile(name, JSON.stringify(value, null, '\t'), cb); -}; - - -const writeJSON = function(name, value, cb) { - writeFile(name, JSON.stringify(value, null, '\t'), cb); -}; - -// fs -module.exports.unlink = fs.unlink; -module.exports.rmdir = fs.rmdir; - -// streams -module.exports.createWriteStream = createWriteStream; -module.exports.createReadStream = createReadStream; - -// io -module.exports.read = readFile; -module.exports.write = writeFile; -module.exports.update = updateFile; -module.exports.create = createFile; - -// json -module.exports.readJSON = readJSON; -module.exports.lockAndReadJSON = lockAndReadJSON; -module.exports.writeJSON = writeJSON; -module.exports.updateJSON = updateJSON; -module.exports.createJSON = createJSON; - -// lock -module.exports.unlock_file = unlock_file; -module.exports.lock_and_read = lock_and_read; diff --git a/src/lib/storage/up-storage.js b/src/lib/up-storage.js similarity index 99% rename from src/lib/storage/up-storage.js rename to src/lib/up-storage.js index e8c5b41ea..19c19d264 100644 --- a/src/lib/storage/up-storage.js +++ b/src/lib/up-storage.js @@ -6,9 +6,9 @@ const _ = require('lodash'); const request = require('request'); const Stream = require('stream'); const URL = require('url'); -const Logger = require('../logger'); +const Logger = require('./logger'); const MyStreams = require('@verdaccio/streams'); -const Utils = require('../utils'); +const Utils = require('./utils'); const zlib = require('zlib'); const encode = function(thing) { diff --git a/test/unit/no_proxy.js b/test/unit/no_proxy.js index 39fe300ec..e117013c3 100644 --- a/test/unit/no_proxy.js +++ b/test/unit/no_proxy.js @@ -1,7 +1,7 @@ 'use strict'; let assert = require('assert'); -let Storage = require('../../src/lib/storage/up-storage'); +let Storage = require('../../src/lib/up-storage'); require('../../src/lib/logger').setup([]);