@qingfu/core-env 0.1.19 → 0.1.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/utils.js +24 -7
  2. package/package.json +1 -1
package/lib/utils.js CHANGED
@@ -1,10 +1,12 @@
1
- const _ = require( 'lodash' );
2
- const crypto = require( 'crypto' );
3
- const xml2js = require( 'xml2js' );
4
- const xlsx = require( 'xlsx' );
5
- const pinyin = require( 'pinyin' );
6
- const qrcode = require( 'qr-image' );
7
- const utils = global.utils = module.exports = {};
1
+ const fs = require( 'fs' );
2
+ const _ = require( 'lodash' );
3
+ const crypto = require( 'crypto' );
4
+ const xml2js = require( 'xml2js' );
5
+ const xlsx = require( 'xlsx' );
6
+ const pinyin = require( 'pinyin' );
7
+ const qrcode = require( 'qr-image' );
8
+ const request = require( 'superagent' );
9
+ const utils = global.utils = module.exports = {};
8
10
 
9
11
  utils.xml2json = async xml => {
10
12
  let options = { explicitRoot: false, explicitArray: false };
@@ -222,3 +224,18 @@ utils.identityValidate = identity => {
222
224
  if ( parity[ sum % 11 ] != identity[ 17 ]) return false;
223
225
  else return true;
224
226
  };
227
+
228
+ utils.fetchBig = async ({ url, dest, retry = 0 }) => {
229
+ try {
230
+ return await new Promise(( resolve, reject ) => {
231
+ let writeStream = fs.createWriteStream( dest );
232
+ let readStream = request.get( url );
233
+ readStream.pipe( writeStream );
234
+ readStream.on( 'error', reject );
235
+ writeStream.on( 'finish', resolve );
236
+ });
237
+ } catch ( err ) {
238
+ if ( !retry-- ) throw err;
239
+ return utils.fetchBig({ url, dest, retry });
240
+ }
241
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@qingfu/core-env",
3
- "version": "0.1.19",
3
+ "version": "0.1.20",
4
4
  "readmeFilename": "README.md",
5
5
  "dependencies": {
6
6
  "@koa/cors": "3.1.0",