Skip to content

Commit

Permalink
update package to modern standards
Browse files Browse the repository at this point in the history
  • Loading branch information
miraclx committed Jul 6, 2022
1 parent 00fbfee commit 5880bbd
Show file tree
Hide file tree
Showing 8 changed files with 1,777 additions and 217 deletions.
43 changes: 43 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"env": {
"browser": true,
"es6": true,
"node": true
},
"parserOptions": {
"sourceType": "module",
"ecmaVersion": "latest",
"ecmaFeatures": {
"modules": true
},
"requireConfigFile": false
},
"extends": [
"eslint:recommended"
],
"plugins": [
"prettier"
],
"rules": {
"prettier/prettier": [
1,
{
"trailingComma": "all",
"printWidth": 130,
"bracketSpacing": false,
"arrowParens": "avoid",
"singleQuote": true
}
],
"no-empty": 0,
"no-cond-assign": 0,
"no-sparse-arrays": 0,
"no-unused-vars": [
1,
{
"argsIgnorePattern": "^_",
"varsIgnorePattern": "^_"
}
]
}
}
14 changes: 2 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ This installs a CLI command accessible with the `xget` command.
```bash
# Check if the xget command has been installed and accessible on your path
$ xget -v
v0.8.2-1
v0.10.0
```

## Usage
Expand Down Expand Up @@ -52,17 +52,8 @@ Use `--help` to see full usage documentation.
### Programmatically

```javascript
// Node CommonJS
const xget = require("libxget");
// Or ES6 Modules
import xget from "libxget";
// Or Typescript
import * as xget from "libxget";
```

#### Examples

```javascript
xget("https://github.com/microsoft/TypeScript/archive/master.zip", {
chunks: 10,
retries: 10,
Expand Down Expand Up @@ -355,14 +346,13 @@ xget https://myserver.com/movie.mp4 | vlc - > /dev/null 2>&1

### Building

Feel free to clone, use in adherance to the [license](#license) and perhaps send pull requests
Feel free to clone, use in adherance to the [license](#license). Pull requests are very much welcome.

```bash
git clone https://github.com/miraclx/libxget-js.git
cd libxget-js
npm install
# hack on code
npm run build
```

## License
Expand Down
34 changes: 18 additions & 16 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
#!/usr/bin/env node
const fs = require('fs');
const url = require('url');
const tty = require('tty');
const path = require('path');
const util = require('util');
import fs from 'fs';
import url from 'url';
import tty from 'tty';
import path from 'path';
import util from 'util';

const xbytes = require('xbytes');
const mime = require('mime-types');
const commander = require('commander');
const xprogress = require('xprogress');
const cStringd = require('stringd-colors');
const contentType = require('content-type');
const contentDisposition = require('content-disposition');
import xbytes from 'xbytes';
import mime from 'mime-types';
import commander from 'commander';
import xprogress from 'xprogress';
import cStringd from 'stringd-colors';
import contentType from 'content-type';
import contentDisposition from 'content-disposition';

const xget = require('.');
const packageJson = require('./package.json');
const {XgetException} = require('./lib/xgetception');
import xget from './lib/index.js';
import {XgetException} from './lib/xgetception.js';

const __dirname = url.fileURLToPath(new URL('.', import.meta.url));

const [log, error] = [, ,].fill(
(function ninjaLoggers() {
Expand Down Expand Up @@ -64,7 +65,6 @@ function processArgs(_url, outputFile, options) {
error('\x1b[31m[i]\x1b[0m Please enter a valid URL'), process.exit(1);

function CHECK_FLAG_VAL(variable, flagref, untype) {
// eslint-disable-next-line valid-typeof
if (![null, undefined].includes(variable) && typeof variable !== untype)
if (!(parseFloat(variable).toString() === variable && parseFloat(variable) >= 0))
throw new XgetException(`\`${flagref}\` if specified, must be given a valid positive \`${untype}\` datatype`);
Expand Down Expand Up @@ -258,6 +258,8 @@ function processArgs(_url, outputFile, options) {
request.start();
}

let packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json')).toString());

const command = commander
.name('xget')
.usage('[options] <url> [outputFile]')
Expand Down
28 changes: 11 additions & 17 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
/* eslint-disable no-underscore-dangle */
/* eslint-disable max-classes-per-file */
const crypto = require('crypto');
const stream = require('stream');
import crypto from 'crypto';
import stream from 'stream';

const axios = require('axios').default;
const merge2 = require('merge2');
const httpRange = require('http-range');
const xresilient = require('xresilient');
const StreamCache = require('./streamCache');
import axios from 'axios';
import merge2 from 'merge2';
import httpRange from 'http-range';
import xresilient from 'xresilient';

require('stream.pipeline-shim/auto');

const {XgetException, XgetNetException, XGET_CHECK_VAR} = require('./xgetception');
import StreamCache from './streamCache.js';
import {XgetException, XgetNetException, XGET_CHECK_VAR} from './xgetception.js';

function parseSplitSpec(total, numberOfparts) {
const chunkSize = Math.floor(total / numberOfparts);
Expand Down Expand Up @@ -359,7 +355,7 @@ function xonstructor(url, opts) {
this.getErrContext = raw => ({raw, ...raw[this.constructor.errHandler]});
}

class XGETStream extends stream.Readable {
export class XGETStream extends stream.Readable {
static errHandler = Symbol('XgetErrHandle');

constructor(url, opts) {
Expand All @@ -368,9 +364,7 @@ class XGETStream extends stream.Readable {
}
}

module.exports = function xget(url, opts) {
export default function xget(url, opts) {
const xtr = new XGETStream(url, opts);
return xtr;
};

module.exports.XGETStream = XGETStream;
}
42 changes: 16 additions & 26 deletions lib/streamCache.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable max-classes-per-file */
const {Duplex} = require('stream');
const {totalmem} = require('os');
import {Duplex} from 'stream';
import {totalmem} from 'os';

const LinkedList = require('linkedlist');
import esMain from 'es-main';
import LinkedList from 'linkedlist';

class CachingStream extends Duplex {
#parent;
Expand All @@ -13,28 +13,24 @@ class CachingStream extends Duplex {
*/
constructor(parent) {
super();
// eslint-disable-next-line no-use-before-define
if (!(parent && parent instanceof StreamCache)) throw new TypeError('<parent> must be an instance of a StreamCache');
this.#parent = parent;
}

// eslint-disable-next-line no-underscore-dangle
_write(chunk, _encoding, callback) {
this.#parent.cacheBytesOn(this, chunk, callback);
}

// eslint-disable-next-line no-underscore-dangle
_read() {
this.#parent.readBytesOn(this);
}

// eslint-disable-next-line no-underscore-dangle
_final(cb) {
this.#parent.cacheBytesOn(this, null, cb);
}
}

class StreamCache {
export default class StreamCache {
#store = {
items: new WeakMap(),
length: 0,
Expand Down Expand Up @@ -112,7 +108,6 @@ class StreamCache {
if (this.#isFull()) {
// if cache is full, try freeing some space by resolving pending readers
if (
// eslint-disable-next-line no-loop-func
!this.#readOn(stream, stack, chunk, () => {
// reason for this:
// • items are pending to be cached
Expand Down Expand Up @@ -224,28 +219,23 @@ class StreamCache {
}
}

module.exports = StreamCache;
module.exports.CachingStream = CachingStream;
module.exports.generator = function buildSingleStreamCache(size) {
function buildSingleStreamCache(size) {
// you lose control with this option.
// cannot change capacity, get metrics or add new children.
if (size && typeof size !== 'number') throw new TypeError('<size>, if defined must be a valid number');
const parent = new StreamCache({size});
return parent.new();
};
}

export {CachingStream, buildSingleStreamCache as generator};

function test() {
// eslint-disable-next-line global-require
const fs = require('fs');
// eslint-disable-next-line global-require
const path = require('path');
// eslint-disable-next-line global-require
const {Readable} = require('stream');
async function test() {
const fs = await import('fs');
const path = await import('path');
const {Readable} = await import('stream');

// eslint-disable-next-line global-require
const xbytes = require('xbytes');
// eslint-disable-next-line global-require
const ProgressBar = require('xprogress');
const xbytes = await import('xbytes');
const ProgressBar = await import('xprogress');

let SIZE;
const [TESTCODE, FILE, size] = process.argv.slice(2);
Expand Down Expand Up @@ -408,4 +398,4 @@ function test() {
}
}

if (require.main === module) test();
if (esMain(import.meta)) test();
5 changes: 1 addition & 4 deletions lib/xgetception.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable max-classes-per-file */

class XgetException extends Error {}

class XgetNetException extends XgetException {
Expand All @@ -12,12 +10,11 @@ class XgetNetException extends XgetException {

function XGET_CHECK_VAR(val, varName, type, optional) {
if (![undefined, null].includes(val) || !optional) {
// eslint-disable-next-line valid-typeof
if (typeof val !== type) {
const er = new XgetException(`<${varName}>${optional ? ', if defined,' : ''} must be a valid type \`${type}\``);
throw er;
} else return true;
}
}

module.exports = {XgetException, XgetNetException, XGET_CHECK_VAR};
export {XgetException, XgetNetException, XGET_CHECK_VAR};
Loading

0 comments on commit 5880bbd

Please sign in to comment.