
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.
speike-proxy
Advanced tools
一个 Node.js 前后端中间层代理模块~
$ npm install speike-proxy --save
Node.js >= 6.0.0 required.
一个简单的例子,向 http://xxx.com/user
发送GET请求,获取数据并校验返回数据是否符合 rule
中配置的规则。
const Proxy = require('speike-proxy');
const proxy = new Proxy({
baseUrl: 'http://xxx.com',
api: {
test: {
options: {
url: '/user',
method: 'GET'
},
rule: {
'name': '@NAME',
'age|0-100': 1
}
}
}
});
proxy.test({...}).then(result => {
console.log(result.data);
});
在前后端分离项目中,Node.js通常作为 中间层
来向 服务端
发请求来获取数据,并将得到的数据根据不同的业务需求进行特殊处理之后发送给前端做渲染,本身并不直接对数据库进行操作
在此背景下 中间层
会面临几个问题
使用 Speike-proxy 可以完美解决以上所有问题,大大提升开发效率与开发体验。
一个经典案例
const Proxy = require('speike-proxy');
const proxy = new Proxy({
baseUrl: 'http://xxx.com',
api: {
// key为自定义请求方法
test: {
options: {
url: '/user',
method: 'GET'
},
rule: {
'name': '@NAME',
'age|0-100': 1
},
mock: true
}
},
// 是否开启Mock模式
mock: false,
// 服务端返回数据未通过检查时,触发该函数
alarm: function ({options, rule, data}) {}
});
// 调用自定义方法发送请求
proxy.test({...}).then(result => {
console.log(result.data);
});
new Proxy(options)
Key
- 将自定义的key
添加到实例的方法中,访问该方法发送对应的请求,请求选项和Mock规则在value
中定义
proxy[自定义的key](data, callback)
null
proxy.test({...}).then(({data, res}) => {
console.log('data: ${data}, res: ${res}');
});
or
proxy.test({...}, (err, data, res) => {
console.log('data: ${data}, res: ${res}');
});
url
地址Content-Type: application/json
请求头username:password
简单登录授权(Basic Authentication)参数,将以明文方式将登录信息以 Authorization 请求头发送出去username:password
摘要登录授权(Digest Authentication)参数,设置此参数会自动对 401 响应尝试生成 Authorization 请求头, 尝试以授权方式请求一次。result.res 是一个 ReadStream 对象
ca,rejectUnauthorized,pfx,key,cert,passphrase,ciphers,secureProtocol
这几个都是透传给 HTTPS 模块的参数,具体请查看 https.request(options, callback)
。request 部分基于 urllib 开发。了解更多请点我
Mock部分基于 Mock.js 开发。
Mock 规范包括两部分:
数据模板中的每个属性由 3 部分构成:属性名、生成规则、属性值:
// 属性名 name
// 生成规则 rule
// 属性值 value
'name|rule': value
注意:
'name|min-max': value
'name|count': value
'name|min-max.dmin-dmax': value
'name|min-max.dcount': value
'name|count.dmin-dmax': value
'name|count.dcount': value
'name|+step': value
生成规则和示例:
1. 属性值是字符串 String
'name|min-max': string
通过重复 string 生成一个字符串,重复次数大于等于 min,小于等于 max。'name|count': string
通过重复 string 生成一个字符串,重复次数等于 count。2. 属性值是数字 Number
'name|+1': number
属性值自动加 1,初始值为 number。'name|min-max': number
生成一个大于等于 min、小于等于 max 的整数,属性值 number 只是用来确定类型。'name|min-max.dmin-dmax': number
生成一个浮点数,整数部分大于等于 min、小于等于 max,小数部分保留 dmin 到 dmax 位。{
'number1|1-100.1-10': 1,
'number2|123.1-10': 1,
'number3|123.3': 1,
'number4|123.10': 1.123
}
// =>
{
"number1": 12.92,
"number2": 123.51,
"number3": 123.777,
"number4": 123.1231091814
}
3. 属性值是布尔型 Boolean
'name|1': boolean
随机生成一个布尔值,值为 true
的概率是 1/2,值为 false
的概率同样是 1/2
。'name|min-max': value
随机生成一个布尔值,值为 value
的概率是 min / (min + max)
,值为 !value
的概率是 max / (min + max)
。4. 属性值是对象 Object
'name|count': object
从属性值 object 中随机选取 count 个属性。'name|min-max': object
从属性值 object 中随机选取 min 到 max 个属性。5. 属性值是数组 Array
'name|1': array
从属性值 array 中随机选取 1 个元素,作为最终值。'name|+1': array
从属性值 array 中顺序选取 1 个元素,作为最终值。'name|min-max': array
通过重复属性值 array 生成一个新数组,重复次数大于等于 min,小于等于 max。'name|count': array
通过重复属性值 array 生成一个新数组,重复次数为 count。6. 属性值是函数 Function
'name': function
执行函数 function,取其返回值作为最终的属性值,函数的上下文为属性 'name' 所在的对象。7. 属性值是正则表达式 RegExp
'name': regexp
根据正则表达式 regexp 反向生成可以匹配它的字符串。用于生成自定义格式的字符串。{
'regexp1': /[a-z][A-Z][0-9]/,
'regexp2': /\w\W\s\S\d\D/,
'regexp3': /\d{5,10}/
}
// =>
{
"regexp1": "pJ7",
"regexp2": "F)\fp1G",
"regexp3": "561659409"
}
占位符 只是在属性值字符串中占个位置,并不出现在最终的属性值中。
占位符 的格式为:
@占位符
@占位符(参数 [, 参数])
注意:
{
name: {
first: '@FIRST',
middle: '@FIRST',
last: '@LAST',
full: '@first @middle @last'
}
}
// =>
{
"name": {
"first": "Charles",
"middle": "Brenda",
"last": "Lopez",
"full": "Charles Brenda Lopez"
}
}
FAQs
A proxy module for Node.js
The npm package speike-proxy receives a total of 0 weekly downloads. As such, speike-proxy popularity was classified as not popular.
We found that speike-proxy 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.