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 path = require('path');
|
||||||
|
|
||||||
|
const download = require('./js/download');
|
||||||
const rootPath = __dirname.replace(/\\/g, '/');
|
|
||||||
|
|
||||||
const nanInclude = path.dirname(require.resolve('nan')).replace(/\\/g, '/');
|
|
||||||
const thisInclude = `${rootPath}/include`;
|
|
||||||
|
|
||||||
|
|
||||||
const isWindows = process.platform === 'win32';
|
const NAMES = ['win32', 'win64', 'linux64', 'mac64'];
|
||||||
|
|
||||||
const names = ['win32', 'win64', 'linux32', 'linux64', 'mac64'];
|
|
||||||
|
|
||||||
const prefixName = name => `bin-${name}`;
|
const prefixName = name => `bin-${name}`;
|
||||||
|
|
||||||
const getPlatformDir = platform => {
|
const getPlatformDir = () => {
|
||||||
switch (platform) {
|
switch (process.platform) {
|
||||||
case 'win32' : return process.arch === 'x64' ? 'win64' : 'win32';
|
case 'win32' : return process.arch === 'x64' ? 'win64' : 'win32';
|
||||||
case 'linux' :
|
case 'linux' : return 'linux64';
|
||||||
if (process.arch === 'x32') {
|
case 'darwin' : return 'mac64';
|
||||||
throw new Error('Linux x32 not supported since 4.0.0.');
|
default : throw new Error(`Platform "${process.platform}" is not supported.`);
|
||||||
}
|
|
||||||
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.`);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const rootPath = __dirname.replace(/\\/g, '/');
|
||||||
const currentDir = prefixName(getPlatformDir(process.platform));
|
const nanInclude = path.dirname(require.resolve('nan')).replace(/\\/g, '/');
|
||||||
const remDirs = names.map(prefixName).filter(n => n !== currentDir);
|
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 => {
|
const paths = dir => {
|
||||||
|
|
||||||
|
@ -68,9 +56,9 @@ const paths = dir => {
|
||||||
const includePath = `${nanInclude} ${thisInclude}`;
|
const includePath = `${nanInclude} ${thisInclude}`;
|
||||||
const binPath = currentDir;
|
const binPath = currentDir;
|
||||||
|
|
||||||
const mkdirPath = isWindows ? `${rootPath}/_mkdir.bat` : 'mkdir';
|
const mkdirPath = isWindows ? `${rootPath}/bat/mkdir.bat` : 'mkdir';
|
||||||
const rmPath = isWindows ? `${rootPath}/_rm.bat` : 'rm';
|
const rmPath = isWindows ? `${rootPath}/bat/rm.bat` : 'rm';
|
||||||
const cpPath = isWindows ? `${rootPath}/_cp.bat` : 'cp';
|
const cpPath = isWindows ? `${rootPath}/bat/cp.bat` : 'cp';
|
||||||
|
|
||||||
|
|
||||||
module.exports = {
|
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