@dxos/codec-protobuf
Advanced tools
Comparing version 1.1.0-beta.2 to 1.1.0-beta.3
@@ -210,18 +210,8 @@ "use strict"; | ||
if (fieldType === 'google.protobuf.Any') { | ||
const encodeAny = any => { | ||
const { | ||
__type_url: typeUrl | ||
} = any; | ||
return { | ||
type_url: typeUrl, | ||
value: this.encodeByType(any, typeUrl) | ||
}; | ||
}; // NOTE: Each ANY is separately encoded so that it can be optionally decoded (e.g., if the type is not known). | ||
// 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 => encodeAny(value)); | ||
object[field] = value[field].map(value => this._encodeAny(value)); | ||
} else { | ||
object[field] = encodeAny(value[field]); | ||
object[field] = this._encodeAny(value[field]); | ||
} | ||
@@ -313,34 +303,7 @@ } | ||
if (fieldType === 'google.protobuf.Any' && options.recursive) { | ||
const decodeAny = any => { | ||
// Test if already decoded. | ||
if (any.__type_url) { | ||
return any; | ||
} // Check known type, otherwise leave decoded ANY object in place. | ||
const { | ||
type_url: typeUrl, | ||
value: buffer | ||
} = any; | ||
const type = this.getType(typeUrl); | ||
if (!type) { | ||
if (options.strict) { | ||
throw new Error(`Unknown type: ${typeUrl}`); | ||
} | ||
return any; | ||
} // Recursively decode the object. | ||
return Object.assign(this.decodeByType(buffer, typeUrl, options), { | ||
__type_url: typeUrl | ||
}); | ||
}; | ||
if (object[field] !== undefined) { | ||
if (repeated) { | ||
object[field] = object[field].map(any => decodeAny(any)); | ||
object[field] = object[field].map(any => this._decodeAny(any, options)); | ||
} else { | ||
object[field] = decodeAny(object[field]); | ||
object[field] = this._decodeAny(object[field], options); | ||
} | ||
@@ -354,2 +317,39 @@ } | ||
_encodeAny(any) { | ||
const { | ||
__type_url: typeUrl | ||
} = any; | ||
return { | ||
type_url: typeUrl, | ||
value: this.encodeByType(any, typeUrl) | ||
}; | ||
} | ||
_decodeAny(any, options) { | ||
// Test if already decoded. | ||
if (any.__type_url) { | ||
return any; | ||
} // Check known type, otherwise leave decoded ANY object in place. | ||
const { | ||
type_url: typeUrl, | ||
value: buffer | ||
} = any; | ||
const type = this.getType(typeUrl); | ||
if (!type) { | ||
if (options.strict) { | ||
throw new Error(`Unknown type: ${typeUrl}`); | ||
} | ||
return any; | ||
} // Recursively decode the object. | ||
return Object.assign(this.decodeByType(buffer, typeUrl, options), { | ||
__type_url: typeUrl | ||
}); | ||
} | ||
} | ||
@@ -356,0 +356,0 @@ |
{ | ||
"name": "@dxos/codec-protobuf", | ||
"version": "1.1.0-beta.2", | ||
"version": "1.1.0-beta.3", | ||
"description": "Value encoding for protobuf.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/dxos/codec-protobuf#readme", |
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
308
61987