Skip to content

Commit d65ad07

Browse files
committed
fix(ddn-node-sdk): fix a few bugs about var not being defined
1 parent b743538 commit d65ad07

15 files changed

Lines changed: 94 additions & 257 deletions

File tree

packages/ddn-asset-base/src/asset-base.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*---------------------------------------------------------------------------------------------
22
* Created by imfly on Sun May 06 2017 11:39:6
33
*
4-
* Copyright (c) 2018 DDN.link. All rights reserved.
4+
* Copyright (c) 2018 DDN FOUNDATION. All rights reserved.
55
* Licensed under the MIT License. See License.txt in the project root for license information.
66
*--------------------------------------------------------------------------------------------*/
77
/**
@@ -11,11 +11,11 @@
1111
* create,getBytes,calculateFee,verify,objectNormalize,dbRead,apply,undo,applyUnconfirmed,
1212
* undoUnconfirmed,ready,process
1313
*/
14-
const AssetUtils = require('./asset-utils');
15-
const ByteBuffer = require('bytebuffer');
16-
const CommonUtils = require('./common-utils');
17-
const { Bignum } = require('@ddn/ddn-utils');
18-
const _ = require('lodash');
14+
import AssetUtils from './asset-utils';
15+
import ByteBuffer from 'bytebuffer';
16+
import CommonUtils from './common-utils';
17+
import { Bignum } from '@ddn/ddn-utils';
18+
import _ from 'lodash';
1919

2020
/**
2121
* 定义字段相应规则

packages/ddn-dapp/src/dapp.js

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class Dapp extends AssetBase {
7070
trs.amount = "0";
7171

7272
const assetJsonName = await this.getAssetJsonName(trs.type);
73+
// eslint-disable-next-line require-atomic-updates
7374
trs.asset[assetJsonName] = data[assetJsonName];
7475

7576
// {
@@ -86,11 +87,11 @@ class Dapp extends AssetBase {
8687
return trs;
8788
}
8889

89-
async calculateFee(trs, sender) {
90+
async calculateFee() {
9091
return Bignum.multiply(100, this.tokenSetting.fixedPoint);
9192
}
9293

93-
async verify(trs, sender) {
94+
async verify(trs) {
9495
const dapp = await this.getAssetObject(trs);
9596
if (trs.recipient_id) {
9697
throw new Error("Invalid recipient");
@@ -174,7 +175,7 @@ class Dapp extends AssetBase {
174175

175176
tags = tags.map(tag => tag.trim()).sort();
176177

177-
for (var i = 0; i < tags.length - 1; i++) {
178+
for (let i = 0; i < tags.length - 1; i++) {
178179
if (tags[i + 1] == tags[i]) {
179180
throw new Error(`Encountered duplicate tags: ${tags[i]}`)
180181
}
@@ -250,22 +251,22 @@ class Dapp extends AssetBase {
250251
return buf;
251252
}
252253

253-
async apply(trs, block, sender, dbTrans) {
254+
async apply(trs) {
254255
const assetObj = await this.getAssetObject(trs);
255256
if (assetObj.name === WITNESS_CLUB_DAPP_NAME) {
256257
global.state.clubInfo = assetObj
257258
global.state.clubInfo.transactionId = trs.id
258259
}
259260
}
260261

261-
async undo(trs, block, sender, dbTrans) {
262+
async undo(trs) {
262263
const assetObj = await this.getAssetObject(trs);
263264
if (assetObj.name === WITNESS_CLUB_DAPP_NAME) {
264265
global.state.clubInfo = null
265266
}
266267
}
267268

268-
async applyUnconfirmed(trs, sender, dbTrans) {
269+
async applyUnconfirmed(trs) {
269270
const assetObj = await this.getAssetObject(trs);
270271

271272
//Bignum update if (privated.unconfirmedNames[trs.asset.dapp.name]) {
@@ -275,7 +276,7 @@ class Dapp extends AssetBase {
275276

276277
//Bignum update if (trs.asset.dapp.link && privated.unconfirmedLinks[trs.asset.dapp.link]) {
277278
if (assetObj.link && this.oneoff.has(assetObj.link.toLowerCase())) {
278-
throw new Error(cb, "Dapp link already exists");
279+
throw new Error("Dapp link already exists");
279280
}
280281

281282
//Bignum update privated.unconfirmedNames[trs.asset.dapp.name] = true;
@@ -284,7 +285,7 @@ class Dapp extends AssetBase {
284285
this.oneoff.set(assetObj.link.toLowerCase(), true);
285286
}
286287

287-
async undoUnconfirmed(trs, sender, dbTrans) {
288+
async undoUnconfirmed(trs) {
288289
const assetObj = await this.getAssetObject(trs);
289290
//Bignum update delete privated.unconfirmedNames[trs.asset.dapp.name];
290291
this.oneoff.delete(assetObj.name.toLowerCase());
@@ -320,7 +321,7 @@ class Dapp extends AssetBase {
320321
async attachApi(router) {
321322
router.put("/", async (req, res) => {
322323
try {
323-
const result = await this.putDapp(req, res);
324+
const result = await this.putDapp(req);
324325
res.json(result);
325326
} catch (err) {
326327
res.json({ success: false, error: err.message || err.toString() });
@@ -338,7 +339,7 @@ class Dapp extends AssetBase {
338339

339340
router.get("/get", async (req, res) => {
340341
try {
341-
const result = await this.getDappById(req, res);
342+
const result = await this.getDappById(req);
342343
res.json(result);
343344
} catch (err) {
344345
res.json({ success: false, error: err.message || err.toString() });
@@ -356,7 +357,7 @@ class Dapp extends AssetBase {
356357

357358
router.get("/installed", async (req, res) => {
358359
try {
359-
const result = await this.getInstalled(req, res);
360+
const result = await this.getInstalled();
360361
res.json(result);
361362
} catch (err) {
362363
res.json({ success: false, error: err.message || err.toString() });
@@ -457,7 +458,7 @@ class Dapp extends AssetBase {
457458

458459
router.get("/balances/:dappid", async (req, res) => {
459460
try {
460-
const result = await this.getDappBalances(req, res);
461+
const result = await this.getDappBalances(req);
461462
res.json(result);
462463
} catch (err) {
463464
res.json({ success: false, error: err.message || err.toString() });
@@ -466,15 +467,15 @@ class Dapp extends AssetBase {
466467

467468
router.get("/balances/:dappid/:currency", async (req, res) => {
468469
try {
469-
const result = await this.getDappBalance(req, res);
470+
const result = await this.getDappBalance(req);
470471
res.json(result);
471472
} catch (err) {
472473
res.json({ success: false, error: err.message || err.toString() });
473474
}
474475
});
475476
}
476477

477-
async getDappBalances(req, res) {
478+
async getDappBalances(req) {
478479
const dappId = req.params.dappid;
479480
const limit = req.query.limit || 100;
480481
const offset = req.query.offset || 0;
@@ -491,7 +492,7 @@ class Dapp extends AssetBase {
491492
});
492493
}
493494

494-
async getDappBalance(req, res) {
495+
async getDappBalance(req) {
495496
const dappId = req.params.dappid;
496497
const currency = req.params.currency;
497498

@@ -507,7 +508,7 @@ class Dapp extends AssetBase {
507508
});
508509
}
509510

510-
async getLaunchDappLastError(req, res) {
511+
async getLaunchDappLastError(req) {
511512
const query = req.query;
512513

513514
const validateErrors = await this.ddnSchema.validate({
@@ -540,7 +541,7 @@ class Dapp extends AssetBase {
540541
}
541542
}
542543

543-
async postLaunchDapp(req, res) {
544+
async postLaunchDapp(req) {
544545
const body = req.body;
545546

546547
const validateErrors = await this.ddnSchema.validate({
@@ -633,7 +634,7 @@ class Dapp extends AssetBase {
633634
try {
634635
await this.stopDapp(dapp);
635636
} catch (err) {
636-
637+
throw new Error(err);
637638
}
638639
}
639640

@@ -644,6 +645,7 @@ class Dapp extends AssetBase {
644645

645646
sandbox.run(args);
646647

648+
// eslint-disable-next-line require-atomic-updates
647649
_dappLaunched[id] = sandbox;
648650

649651
await this._attachDappApi(id);
@@ -753,7 +755,7 @@ class Dapp extends AssetBase {
753755
}
754756
}
755757

756-
async postStopDapp(req, res) {
758+
async postStopDapp(req) {
757759
const body = req.body;
758760

759761
const validateErrors = await this.ddnSchema.validate({
@@ -814,7 +816,7 @@ class Dapp extends AssetBase {
814816
throw new Error("DApp not found: " + trsId);
815817
}
816818

817-
async getDappList(req, res) {
819+
async getDappList(req) {
818820
var query = req.query;
819821

820822
const validateErrors = await this.ddnSchema.validate({
@@ -905,7 +907,7 @@ class Dapp extends AssetBase {
905907
return { success: true, result };
906908
}
907909

908-
async getDappById(req, res) {
910+
async getDappById(req) {
909911
const query = req.query;
910912

911913
const validateErrors = await this.ddnSchema.validate({
@@ -934,13 +936,15 @@ class Dapp extends AssetBase {
934936
}
935937

936938
delDir(path) {
937-
var files = [];
939+
let files = [];
940+
let self = this;
941+
938942
if (fs.existsSync(path)) {
939943
files = fs.readdirSync(path);
940-
files.forEach(function (file, index) {
944+
files.forEach(function (file) {
941945
var curPath = path + "/" + file;
942946
if (fs.statSync(curPath).isDirectory()) { // recurse
943-
delDir(curPath);
947+
self.delDir(curPath);
944948
} else { // delete file
945949
fs.unlinkSync(curPath);
946950
}
@@ -970,7 +974,7 @@ class Dapp extends AssetBase {
970974
});
971975
}
972976

973-
async getInstalled(req, res) {
977+
async getInstalled() {
974978
const ids = await this.getInstalledDappIds();
975979
if (ids && ids.length) {
976980
const dapps = await this.queryAsset({ trs_id: { "$in": ids } }, null, false, 1, ids.length);
@@ -981,7 +985,7 @@ class Dapp extends AssetBase {
981985

982986
async downloadDapp(source, target) {
983987
const downloadErr = await new Promise((resolve, reject) => {
984-
request(source, (err, res, body) => {
988+
request(source, (err) => {
985989
if (err) {
986990
return reject(err);
987991
}
@@ -1016,7 +1020,7 @@ class Dapp extends AssetBase {
10161020
if (fs.existsSync(target)) {
10171021
fs.unlinkSync(target);
10181022
}
1019-
return reject(err);
1023+
return reject(downloadErr);
10201024
}
10211025
resolve();
10221026
});
@@ -1030,7 +1034,7 @@ class Dapp extends AssetBase {
10301034
return reject(`Failed to decompress zip file: ${err}`);
10311035
});
10321036

1033-
unzipper.on("extract", log => {
1037+
unzipper.on("extract", () => {
10341038
resolve();
10351039
});
10361040

@@ -1128,6 +1132,7 @@ class Dapp extends AssetBase {
11281132

11291133
if (_dappLaunched[body.id]) {
11301134
await this.stopDapp(dapp);
1135+
// eslint-disable-next-line require-atomic-updates
11311136
_dappLaunched[body.id] = false;
11321137
}
11331138

@@ -1137,6 +1142,7 @@ class Dapp extends AssetBase {
11371142
await this.runtime.socketio.emit('dapps/change', {});
11381143
return res.json({ success: true });
11391144
} finally {
1145+
// eslint-disable-next-line require-atomic-updates
11401146
_dappRemoving[body.id] = false;
11411147
}
11421148
}
@@ -1181,21 +1187,22 @@ class Dapp extends AssetBase {
11811187
const dapp = await this.getDappByTransactionId(body.id);
11821188
const dappPath = await this.installDApp(dapp);
11831189

1184-
if (dapp.type == 0) {
1185-
// no need to install node dependencies
1186-
} else {
1187-
}
1190+
// if (dapp.type == 0) {
1191+
// // no need to install node dependencies
1192+
// } else {
1193+
// }
11881194

11891195
await this._removeLaunchedMarkFile(dappPath);
11901196

11911197
await this.runtime.socketio.emit('dapps/change', {});
11921198
return res.json({ success: true, path: dappPath });
11931199
} finally {
1200+
// eslint-disable-next-line require-atomic-updates
11941201
_dappInstalling[body.id] = false;
11951202
}
11961203
}
11971204

1198-
async putDapp(req, res) {
1205+
async putDapp(req) {
11991206
const body = req.body;
12001207

12011208
const validateErrors = await this.ddnSchema.validate({
@@ -1334,7 +1341,7 @@ class Dapp extends AssetBase {
13341341
}
13351342
}
13361343

1337-
async onNewBlock(block, votes) {
1344+
async onNewBlock(block) {
13381345
Object.keys(_dappLaunched).forEach(async(dappId) => {
13391346
const sandbox = _dappLaunched[dappId];
13401347
if (sandbox) {

0 commit comments

Comments
 (0)