if (xwalk.utils.type.isStringArray(value)) {
return BundleValueType.STRING_ARRAY;
}
- if (xwalk.utils.type.isByteStream(value)) {
+ if (xwalk.utils.type.isByteStream(value) ||
+ xwalk.utils.type.isLegacyByteStream(value)) {
return BundleValueType.BYTES;
}
- if (xwalk.utils.type.isByteStreamArray(value)) {
+ if (xwalk.utils.type.isByteStreamArray(value) ||
+ xwalk.utils.type.isLegacyByteStreamArray(value)) {
return BundleValueType.BYTES_ARRAY;
}
}
exports.Bundle.prototype.constructor = exports.Bundle;
exports.Bundle.prototype.set = function(key, value) {
- if (undefined === getValueType(value)) {
- value = xwalk.utils.converter.toString(value);
- }
- this.data[key] = value;
+ this.data[key] = convertToBundleValue(value);
};
+function convertToBundleValue(value) {
+ var converted = value;
+ switch (getValueType(value)) {
+ case undefined:
+ converted = xwalk.utils.converter.toString(value);
+ break;
+ case BundleValueType.BYTES:
+ converted = new Uint8Array(value);
+ break;
+ case BundleValueType.BYTES_ARRAY:
+ converted = [];
+ value.forEach((function (stream) {
+ converted.push(new Uint8Array(stream));
+ }).bind(this));
+ break;
+ }
+ return converted;
+}
+
exports.Bundle.prototype.get = function(key) {
if (!this.data.hasOwnProperty(key)) {
throw new WebAPIException(WebAPIException.NOT_FOUND_ERR);
exports.Bundle.prototype.toJSON = function() {
var json = {};
this.forEach(function(key, value) {
- json[key] = value;
+ json[key] = value;
});
return json;
};
+// Conversion from Uint8Array to Array happens in toString instead of toJSON
+// because toJSON method should preserve bundle values types.
exports.Bundle.prototype.toString = function() {
- return JSON.stringify(this.data);
+ var json = {};
+ this.forEach(function(key, value, type) {
+ if (type == BundleValueType.BYTES) {
+ json[key] = [].slice.call(value);
+ }
+ else if (type == BundleValueType.BYTES_ARRAY) {
+ json[key] = [];
+ value.forEach(function(stream) {
+ json[key].push([].slice.call(stream));
+ });
+ }
+ else {
+ json[key] = value;
+ }
+ });
+ return JSON.stringify(json);
};