1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | 2x 1x 2x 3x 1x 4x 6x 1x 6x 6x 3x 3x 3x 3x 7x 7x 1x 2x 8x 8x 3x 3x 2x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 1x 2x 1x 2x 1x 2x 2x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 4x 1x 3x 2x 2x 3x 3x 3x 4x 3x 1x 6x 4x 4x 1x 4x 2x 2x 2x 2x 4x 14x 14x 2x 6x 4x 2x 2x 3x 2x 1x 1x 1x 3x 11x 11x 11x 2x 3x 3x 3x 1x 2x 2x 2x 1x 1x 1x 1x | // @flow import fs from 'fs'; import path from 'path'; import _ from 'lodash'; import mkdirp from 'mkdirp'; import createError from 'http-errors'; import type {HttpError} from 'http-errors'; import {UploadTarball, ReadTarball} from '@verdaccio/streams'; import {unlockFile, readFile} from '@verdaccio/file-locking'; import type {ILocalFS, Callback, Logger} from '@verdaccio/types'; const fileExist: string = 'EEXISTS'; const noSuchFile: string = 'ENOENT'; const fSError = function(message: string): HttpError { const err: HttpError = createError(409, message); return err; }; const tempFile = function(str) { return `${str}.tmp${String(Math.random()).substr(2)}`; }; const renameTmp = function(src, dst, _cb) { const cb = (err) => { Iif (err) { fs.unlink(src, function() {}); } _cb(err); }; Eif (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, () => {}); } }); }; class LocalFS implements ILocalFS { path: string; logger: Logger; constructor(path: string, logger: Logger) { this.path = path; this.logger = logger; } deleteJSON(fileName: string, callback: Callback) { return fs.unlink(this._getStorage(fileName), callback); } removePackage(dirPath: string, callback: Callback): void { fs.rmdir(this._getStorage(dirPath), callback); } createJSON(name: string, value: any, cb: Function) { this._createFile(this._getStorage(name), this._convertToString(value), cb); } writeJSON(name: string, value: any, cb: Function) { this._writeFile(this._getStorage(name), this._convertToString(value), cb); } lockAndReadJSON(name: string, cb: Function) { const fileName: string = this._getStorage(name); readFile(fileName, { lock: true, parse: true, }, function(err, res) { Eif (err) { return cb(err); } return cb(null, res); }); } unlock_file(name: string, cb: Function) { unlockFile(this._getStorage(name), cb); } readJSON(name: string, cb: Function) { this._readStorageFile(this._getStorage(name)).then(function(res) { try { const data: any = JSON.parse(res.toString('utf8')); cb(null, data); } catch (err) { cb(err); } }, function(err) { return cb(err); }); } createWriteStream(name: string): UploadTarball { const uploadStream = new UploadTarball(); let _ended = 0; uploadStream.on('end', function() { _ended = 1; }); const pathName: string = this._getStorage(name); fs.exists(pathName, function(exists) { Iif (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, pathName, function(err) { Iif (err) { uploadStream.emit('error', err); } else { uploadStream.emit('success'); } }); }); file.end(); }; Iif (_ended) { onend(); } else { uploadStream.on('end', onend); } }; uploadStream.abort = function() { if (opened) { opened = false; file.on('close', function() { fs.unlink(temporalName, function() {}); }); } file.end(); }; 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; } createReadStream(name: string, readTarballStream: any, callback: Function = () => {}) { const pathName: string = this._getStorage(name); const readStream = fs.createReadStream(pathName); readStream.on('error', function(err) { readTarballStream.emit('error', err); }); readStream.on('open', function(fd) { fs.fstat(fd, function(err, stats) { Iif (_.isNil(err) === false) { return readTarballStream.emit('error', err); } readTarballStream.emit('content-length', stats.size); readTarballStream.emit('open'); readStream.pipe(readTarballStream); }); }); readTarballStream = new ReadTarball(); readTarballStream.abort = function() { readStream.close(); }; return readTarballStream; } _createFile(name: string, contents: any, callback: Function) { fs.exists(name, (exists) => { Iif (exists) { return callback( fSError(fileExist) ); } this._writeFile(name, contents, callback); }); } _readStorageFile(name: string): Promise<any> { return new Promise((resolve, reject) => { fs.readFile(name, (err, data) => { if (err) { reject(err); } else { resolve(data); } }); }); } _convertToString(value: string): string { return JSON.stringify(value, null, '\t'); } _getStorage(name: string = '') { const storagePath: string = path.join(this.path, name); console.log('_getStorage', storagePath); return storagePath; } _writeFile(dest: string, data: string, cb: Function) { const createTempFile = (cb) => { const tempFilePath = tempFile(dest); fs.writeFile(tempFilePath, data, (err) => { if (err) { return cb(err); } renameTmp(tempFilePath, dest, cb); }); }; createTempFile((err) => { if (err && err.code === noSuchFile) { mkdirp(path.dirname(dest), function(err) { Iif (err) { return cb(err); } createTempFile(cb); }); } else { cb(err); } }); } } export default LocalFS; |