Add download
This commit is contained in:
parent
9c956b0081
commit
be5e80f2ec
44
index.js
44
index.js
|
@ -2,40 +2,28 @@
|
|||
|
||||
const path = require('path');
|
||||
|
||||
|
||||
const rootPath = __dirname.replace(/\\/g, '/');
|
||||
|
||||
const nanInclude = path.dirname(require.resolve('nan')).replace(/\\/g, '/');
|
||||
const thisInclude = `${rootPath}/include`;
|
||||
const download = require('./js/download');
|
||||
|
||||
|
||||
const isWindows = process.platform === 'win32';
|
||||
|
||||
const names = ['win32', 'win64', 'linux32', 'linux64', 'mac64'];
|
||||
const NAMES = ['win32', 'win64', 'linux64', 'mac64'];
|
||||
|
||||
const prefixName = name => `bin-${name}`;
|
||||
|
||||
const getPlatformDir = platform => {
|
||||
switch (platform) {
|
||||
const getPlatformDir = () => {
|
||||
switch (process.platform) {
|
||||
case 'win32' : return process.arch === 'x64' ? 'win64' : 'win32';
|
||||
case 'linux' :
|
||||
if (process.arch === 'x32') {
|
||||
throw new Error('Linux x32 not supported since 4.0.0.');
|
||||
}
|
||||
return 'linux64';
|
||||
case 'darwin' :
|
||||
if (process.arch === 'x32') {
|
||||
throw new Error('Mac x32 not supported.');
|
||||
}
|
||||
return 'mac64';
|
||||
default : throw new Error(`Platform "${platform}" is not supported.`);
|
||||
case 'linux' : return 'linux64';
|
||||
case 'darwin' : return 'mac64';
|
||||
default : throw new Error(`Platform "${process.platform}" is not supported.`);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const currentDir = prefixName(getPlatformDir(process.platform));
|
||||
const remDirs = names.map(prefixName).filter(n => n !== currentDir);
|
||||
|
||||
const rootPath = __dirname.replace(/\\/g, '/');
|
||||
const nanInclude = path.dirname(require.resolve('nan')).replace(/\\/g, '/');
|
||||
const thisInclude = `${rootPath}/include`;
|
||||
const isWindows = process.platform === 'win32';
|
||||
const currentDir = prefixName(getPlatformDir());
|
||||
const remDirs = NAMES.map(prefixName).filter(n => n !== currentDir);
|
||||
|
||||
const paths = dir => {
|
||||
|
||||
|
@ -68,9 +56,9 @@ const paths = dir => {
|
|||
const includePath = `${nanInclude} ${thisInclude}`;
|
||||
const binPath = currentDir;
|
||||
|
||||
const mkdirPath = isWindows ? `${rootPath}/_mkdir.bat` : 'mkdir';
|
||||
const rmPath = isWindows ? `${rootPath}/_rm.bat` : 'rm';
|
||||
const cpPath = isWindows ? `${rootPath}/_cp.bat` : 'cp';
|
||||
const mkdirPath = isWindows ? `${rootPath}/bat/mkdir.bat` : 'mkdir';
|
||||
const rmPath = isWindows ? `${rootPath}/bat/rm.bat` : 'rm';
|
||||
const cpPath = isWindows ? `${rootPath}/bat/cp.bat` : 'cp';
|
||||
|
||||
|
||||
module.exports = {
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
'use strict';
|
||||
|
||||
const https = require('https');
|
||||
const http = require('http');
|
||||
|
||||
const WritableBuffer = require('./writable-buffer');
|
||||
|
||||
|
||||
const protocols = { http, https };
|
||||
|
||||
|
||||
module.exports = url => new Promise(
|
||||
(res, rej) => {
|
||||
|
||||
url = url.toLowerCase();
|
||||
|
||||
const stream = new WritableBuffer();
|
||||
const proto = protocols[url.match(/^https?/)[0]];
|
||||
|
||||
proto.get(url, response => {
|
||||
|
||||
response.pipe(stream);
|
||||
|
||||
response.on('end', () => res(stream.get()));
|
||||
response.on('error', err => rej(err));
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
);
|
|
@ -0,0 +1,65 @@
|
|||
'use strict';
|
||||
|
||||
const { Writable } = require('stream');
|
||||
|
||||
const CHUNK_SIZE = 1024;
|
||||
const INITIAL_SIZE = 8 * CHUNK_SIZE;
|
||||
const INCREMENT_SIZE = 8 * CHUNK_SIZE;
|
||||
|
||||
|
||||
class WritableBuffer extends Writable {
|
||||
|
||||
constructor() {
|
||||
|
||||
super();
|
||||
|
||||
this._buffer = new Buffer(INITIAL_SIZE);
|
||||
this._size = 0;
|
||||
|
||||
}
|
||||
|
||||
get() {
|
||||
|
||||
if ( ! this._size ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const data = new Buffer(this._size);
|
||||
this._buffer.copy(data, 0, 0, this._size);
|
||||
|
||||
return data;
|
||||
|
||||
}
|
||||
|
||||
|
||||
_increaseAsNeeded(incomingSize) {
|
||||
|
||||
if ( (this._buffer.length - this._size) >= incomingSize ) {
|
||||
return;
|
||||
}
|
||||
|
||||
const freeSpace = this._buffer.length - this._size;
|
||||
const factor = Math.ceil((incomingSize - freeSpace) / INCREMENT_SIZE);
|
||||
|
||||
const newBuffer = new Buffer(this._buffer.length + (INCREMENT_SIZE * factor));
|
||||
this._buffer.copy(newBuffer, 0, 0, this._size);
|
||||
|
||||
this._buffer = newBuffer;
|
||||
|
||||
}
|
||||
|
||||
|
||||
_write(chunk, encoding, callback) {
|
||||
|
||||
this._increaseAsNeeded(chunk.length);
|
||||
|
||||
chunk.copy(this._buffer, this._size, 0);
|
||||
this._size += chunk.length;
|
||||
|
||||
callback();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = WritableBuffer;
|
Loading…
Reference in New Issue