
Research
Malicious npm Packages Impersonate Flashbots SDKs, Targeting Ethereum Wallet Credentials
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
一个小巧而强大的js流程框架
在日常程序的开发中,你是否时常有如下的困惑:
没关系,这些都可以用这个框架解决。
流程是什么?
流程显然是动作和业务规则的组合。在理想状态下,两者应该有明确清晰的界限。但是在现实开发中,往往是两者像面条一样的混杂在一起分不清楚。 在这里,我们不去纠结原因,只寻求一种解决方案。
这里我们人为的定义:
这里又分为 异常处理规则、前置规则和后置规则
异常处理规则: 在动作发生异常之后,进行后续的处理。返回一个流程表示进入子流程处理异常,子流程处理成功继续原流程
前置规则: 判断、加工动作的参数,决定动作能不能做,返回一个流程表示动作的前置流程,做完之后继续执行原流程;返回false表示打断动作的执行,返回其他值表示接着执行流程。
后置规则: 判断、加工动作的成功返回值,决定要不要执行后续流程。返回一个流程表示动作的后续流程,本流程做完之后会继续原后续流程;返回false表示打断后续流程;返回其他值表示接着执行流程。
这里优先考虑责任链模式, 引入aop。
框架命名为flow,框架应该有如下的表现形式:
1、预先单独定义各个操作函数。这里定义一个标准,函数都必须返回Promise对象,函数本身不含流程控制的任何内容。
function step1(arg){
return new Promise(function(resolve,reject){
setTimeout(function(){
console.log('step1 execute',arg);
resolve({step1:'step1 complete'})
},100);
});
}
var flowCtrl = 0;
function step2(arg){
console.log('stop2 arg',arg);
return new Promise(function(resolve,reject){
setTimeout(function(){
if(flowCtrl==0){
++flowCtrl;
console.log('step2 execute');
try{
//这里模拟一个http异常
throw {code:'http'}
}catch(e){
reject(e)
}
}else{
resolve({step2:'step2 data'});
}
},100);
});
}
function step3(arg){
return new Promise(function(resolve,reject){
setTimeout(function(){
console.log('step3 arg',arg);
resolve({step3:'step3 complete'})
},100);
});
}
为了演示方便,step2在第一次的时候出错,在第二次执行的时候正确。
//如果发生了http错误,则重启流程
function step2HttpError(error){
console.log('step2Error',error);
console.log('restart progress');
flow1.restart();
}
function step2Check(arg){
console.log('step2Check',arg);
}
function step2ResultHandler(arg){
console.log('step2ResultHandler',arg);
// throw new Error();
}
2、使用流程工具整合这些操作
var flow2 = new Flow().add("step3",step3);
var flow1 = new Flow().add("step1",step1)
.add("step2",step2,{catch:{http:step2HttpError},before:step2Check,after:step2ResultHandler})
.add("step3",flow2);
这里add的第三个参数为步骤配置,catch为异常处理函数,可以指定code=某个指定值的异常的处理。before为本步骤执行前的判断函数,如果返回false,表示中止流程。after为本步骤执行成功之后的判断函数,如果返回false,表示中止流程。
4、流程工具的执行,应该也是一个Promise,这个promise成功之后返回最后一个成功执行的流程步骤(可能被before和after打断)的结果
flow1.promise({step1:'step1 data'}).then(function(result){
console.log('complete',result)
}).catch(function(error){
console.warn(error);
})
5、执行结果
FAQs
A js promise flow control library
The npm package jzflow receives a total of 0 weekly downloads. As such, jzflow popularity was classified as not popular.
We found that jzflow demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
Security News
Ruby maintainers from Bundler and rbenv teams are building rv to bring Python uv's speed and unified tooling approach to Ruby development.
Security News
Following last week’s supply chain attack, Nx published findings on the GitHub Actions exploit and moved npm publishing to Trusted Publishers.