@arcblock/forge-message
Advanced tools
Comparing version 0.20.0 to 0.20.1
@@ -12,4 +12,17 @@ /* eslint no-console:"off" */ | ||
const scalarTypes = [ | ||
'bytes', | ||
'string', | ||
'double', | ||
'float', | ||
'sint32', | ||
'uint32', | ||
'sfixed32', | ||
'sint64', | ||
'uint64', | ||
'sfixed64', | ||
]; | ||
// Utility map to generate random data when compose fake message | ||
const scalarTypes = { | ||
const fakeValues = { | ||
sint32: 1, | ||
@@ -44,4 +57,4 @@ uint32: 2, | ||
if (scalarTypes[type]) { | ||
return scalarTypes[type]; | ||
if (fakeValues[type]) { | ||
return fakeValues[type]; | ||
} | ||
@@ -69,3 +82,3 @@ | ||
result[key] = { | ||
[scalarTypes[keyType]]: fakeMessage(subType), | ||
[fakeValues[keyType]]: fakeMessage(subType), | ||
}; | ||
@@ -76,3 +89,3 @@ return; | ||
if (enumTypes.includes(subType)) { | ||
result[key] = scalarTypes.enums(subType); | ||
result[key] = fakeValues.enums(subType); | ||
return; | ||
@@ -86,4 +99,4 @@ } | ||
if (scalarTypes[subType]) { | ||
result[key] = scalarTypes[subType]; | ||
if (fakeValues[subType]) { | ||
result[key] = fakeValues[subType]; | ||
} | ||
@@ -98,3 +111,3 @@ | ||
result[key] = { | ||
type: 'fg:x:random_data', | ||
type: 'string', | ||
value: 'ABCD 1234', | ||
@@ -124,2 +137,6 @@ }; | ||
if (type === 'json') { | ||
return JSON.parse(Buffer.from(data, 'base64').toString('utf8')); | ||
} | ||
if (typeof data !== 'object') { | ||
@@ -129,16 +146,3 @@ return data; | ||
if ( | ||
[ | ||
'bytes', | ||
'string', | ||
'double', | ||
'float', | ||
'sint32', | ||
'uint32', | ||
'sfixed32', | ||
'sint64', | ||
'uint64', | ||
'sfixed64', | ||
].includes(type) | ||
) { | ||
if (scalarTypes.includes(type)) { | ||
return data; | ||
@@ -154,25 +158,2 @@ } | ||
// "fields": { | ||
// "address": { | ||
// "type": "string", | ||
// "id": 1 | ||
// }, | ||
// "consensus": { | ||
// "type": "ConsensusParams", | ||
// "id": 2 | ||
// }, | ||
// "stakeSummary": { | ||
// "keyType": "uint32", | ||
// "type": "StakeSummary", | ||
// "id": 4 | ||
// }, | ||
// "forgeAppHash": { | ||
// "type": "bytes", | ||
// "id": 7 | ||
// }, | ||
// "data": { | ||
// "type": "google.protobuf.Any", | ||
// "id": 15 | ||
// } | ||
// } | ||
Object.keys(fields).forEach(key => { | ||
@@ -238,4 +219,4 @@ const { type: subType, keyType, rule } = fields[key]; | ||
result[key] = { | ||
type: decoded.type, | ||
value: formatMessage(decoded.type, decoded.value), | ||
type: decoded.type || value.typeUrl, | ||
value: formatMessage(decoded.type || value.typeUrl, decoded.value), | ||
}; | ||
@@ -371,2 +352,6 @@ } | ||
const { typeUrl, value } = data; | ||
if (typeUrl === 'json') { | ||
return { type: typeUrl, value: JSON.parse(Buffer.from(value)) }; | ||
} | ||
const type = fromTypeUrl(typeUrl); | ||
@@ -399,3 +384,7 @@ const { fn: Message } = getMessageType(type); | ||
anyMessage.setTypeUrl(data.typeUrl); | ||
anyMessage.setValue(Buffer.from(data.value, 'base64')); | ||
if (data.typeUrl === 'json') { | ||
anyMessage.setValue(Uint8Array.from(Buffer.from(JSON.stringify(data.value)))); | ||
} else { | ||
anyMessage.setValue(Uint8Array.from(Buffer.from(data.value, 'base64'))); | ||
} | ||
} else { | ||
@@ -402,0 +391,0 @@ const { value: anyValue, type: anyType } = data; |
{ | ||
"name": "@arcblock/forge-message", | ||
"description": "Utility functions to encode and decode message that can send to forge", | ||
"version": "0.20.0", | ||
"version": "0.20.1", | ||
"author": "wangshijun <shijun@arcblock.io> (https://www.arcblock.io)", | ||
@@ -21,3 +21,3 @@ "engines": { | ||
"dependencies": { | ||
"@arcblock/forge-proto": "^0.20.0", | ||
"@arcblock/forge-proto": "^0.20.1", | ||
"@arcblock/forge-util": "^0.20.0", | ||
@@ -75,3 +75,3 @@ "camelcase": "^5.0.0", | ||
}, | ||
"gitHead": "1bcb6daa62e48486496c3e73518c23ba27489284" | ||
"gitHead": "081809219385c09c6198bde48f9200eac107412b" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
0
21737
615