@dxos/codec-protobuf
Advanced tools
Comparing version 1.1.0-beta.3 to 1.1.0-beta.4
@@ -187,10 +187,2 @@ "use strict"; | ||
if (!typeUrl) { | ||
typeUrl = value.__type_url; | ||
if (!typeUrl) { | ||
throw new Error('Missing __type_url attribute'); | ||
} | ||
} | ||
const type = this.getType(typeUrl); | ||
@@ -202,22 +194,17 @@ | ||
const object = Object.assign({}, value); | ||
const object = this._iterate(value, value => { | ||
if (!value.__type_url) { | ||
return; | ||
} | ||
for (const field in type.fields) { | ||
const { | ||
type: fieldType, | ||
repeated | ||
} = type.fields[field]; | ||
__type_url: typeUrl, | ||
...formalValue | ||
} = value; | ||
return { | ||
type_url: typeUrl, | ||
value: this.encodeByType(formalValue, typeUrl) | ||
}; | ||
}); | ||
if (fieldType === 'google.protobuf.Any') { | ||
// NOTE: Each ANY is separately encoded so that it can be optionally decoded (e.g., if the type is not known). | ||
if (value[field]) { | ||
if (repeated) { | ||
object[field] = value[field].map(value => this._encodeAny(value)); | ||
} else { | ||
object[field] = this._encodeAny(value[field]); | ||
} | ||
} | ||
} | ||
} | ||
return type.encode(object).finish(); | ||
@@ -271,3 +258,3 @@ } | ||
* | ||
* @param {Object} object - JSON object to decode. | ||
* @param {Object} value - JSON object to decode. | ||
* @param {string} typeUrl | ||
@@ -278,3 +265,3 @@ * @param {Object} [options] | ||
decodeObject(object, typeUrl, options = { | ||
decodeObject(value, typeUrl, options = { | ||
recursive: true, | ||
@@ -293,63 +280,73 @@ strict: true | ||
} else { | ||
return object; | ||
return value; | ||
} | ||
} | ||
/* eslint guard-for-in: "off" */ | ||
const object = this._iterate(value, value => { | ||
// Test if already decoded. | ||
if (value.__type_url) { | ||
return value; | ||
} | ||
for (const field in type.fields) { | ||
if (!value.type_url) { | ||
return; | ||
} // Check known type, otherwise leave decoded ANY object in place. | ||
const { | ||
type: fieldType, | ||
repeated | ||
} = type.fields[field]; | ||
type_url: typeUrl, | ||
value: buffer | ||
} = value; | ||
const type = this.getType(typeUrl); | ||
if (fieldType === 'google.protobuf.Any' && options.recursive) { | ||
if (object[field] !== undefined) { | ||
if (repeated) { | ||
object[field] = object[field].map(any => this._decodeAny(any, options)); | ||
} else { | ||
object[field] = this._decodeAny(object[field], options); | ||
} | ||
if (!type) { | ||
if (options.strict) { | ||
throw new Error(`Unknown type: ${typeUrl}`); | ||
} | ||
} | ||
} | ||
return value; | ||
} // Recursively decode the object. | ||
return Object.assign(this.decodeByType(buffer, typeUrl, options), { | ||
__type_url: typeUrl | ||
}); | ||
}); | ||
return object; | ||
} | ||
_encodeAny(any) { | ||
const { | ||
__type_url: typeUrl | ||
} = any; | ||
return { | ||
type_url: typeUrl, | ||
value: this.encodeByType(any, typeUrl) | ||
}; | ||
} | ||
_iterate(value, callback) { | ||
if (typeof value !== 'object') return value; | ||
let tmp; | ||
let prop; | ||
const str = Object.prototype.toString.call(value); | ||
_decodeAny(any, options) { | ||
// Test if already decoded. | ||
if (any.__type_url) { | ||
return any; | ||
} // Check known type, otherwise leave decoded ANY object in place. | ||
if (str === '[object Object]') { | ||
const result = callback(value); | ||
if (result) { | ||
return result; | ||
} | ||
const { | ||
type_url: typeUrl, | ||
value: buffer | ||
} = any; | ||
const type = this.getType(typeUrl); | ||
tmp = {}; | ||
if (!type) { | ||
if (options.strict) { | ||
throw new Error(`Unknown type: ${typeUrl}`); | ||
for (prop in value) { | ||
tmp[prop] = this._iterate(value[prop], callback); | ||
} | ||
return any; | ||
} // Recursively decode the object. | ||
return tmp; | ||
} | ||
if (str === '[object Array]') { | ||
prop = value.length; | ||
return Object.assign(this.decodeByType(buffer, typeUrl, options), { | ||
__type_url: typeUrl | ||
}); | ||
for (tmp = new Array(prop); prop--;) { | ||
tmp[prop] = this._iterate(value[prop], callback); | ||
} | ||
return tmp; | ||
} | ||
return value; | ||
} | ||
@@ -356,0 +353,0 @@ |
{ | ||
"name": "@dxos/codec-protobuf", | ||
"version": "1.1.0-beta.3", | ||
"version": "1.1.0-beta.4", | ||
"description": "Value encoding for protobuf.", | ||
@@ -54,3 +54,2 @@ "homepage": "https://github.com/dxos/codec-protobuf#readme", | ||
"jest": "^24.5.0", | ||
"jsonpath-plus": "^2.0.0", | ||
"semistandard": "^14.2.0" | ||
@@ -57,0 +56,0 @@ }, |
Sorry, the diff of this file is not supported yet
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
13
61049
301