Comparing version 1.0.0 to 2.0.0
@@ -58,3 +58,23 @@ "use strict"; | ||
var buffer = new Uint8Array(this.buffer.consume_bytes(len)); | ||
return String.fromCharCode.apply(null, buffer); | ||
// decode utf-8 string without using TextDecoder | ||
// first get all bytes to single byte code points | ||
var codePoints = []; | ||
for (var i = 0; i < len; ++i) { | ||
var byte = buffer[i]; | ||
if (byte < 0x80) { | ||
codePoints.push(byte); | ||
} | ||
else if (byte < 0xE0) { | ||
codePoints.push(((byte & 0x1F) << 6) | (buffer[++i] & 0x3F)); | ||
} | ||
else if (byte < 0xF0) { | ||
codePoints.push(((byte & 0x0F) << 12) | ((buffer[++i] & 0x3F) << 6) | (buffer[++i] & 0x3F)); | ||
} | ||
else { | ||
var codePoint = ((byte & 0x07) << 18) | ((buffer[++i] & 0x3F) << 12) | ((buffer[++i] & 0x3F) << 6) | (buffer[++i] & 0x3F); | ||
codePoints.push(codePoint); | ||
} | ||
} | ||
// then decode code points to utf-8 | ||
return String.fromCodePoint.apply(String, codePoints); | ||
}; | ||
@@ -61,0 +81,0 @@ BorshDeserializer.prototype.decode_boolean = function () { |
@@ -87,8 +87,24 @@ "use strict"; | ||
var _value = value; | ||
// 4 bytes for length | ||
this.encoded.store_value(_value.length, 'u32'); | ||
// string bytes | ||
// encode to utf8 bytes without using TextEncoder | ||
var utf8Bytes = []; | ||
for (var i = 0; i < _value.length; i++) { | ||
this.encoded.store_value(_value.charCodeAt(i), 'u8'); | ||
var charCode = _value.charCodeAt(i); | ||
if (charCode < 0x80) { | ||
utf8Bytes.push(charCode); | ||
} | ||
else if (charCode < 0x800) { | ||
utf8Bytes.push(0xc0 | (charCode >> 6), 0x80 | (charCode & 0x3f)); | ||
} | ||
else if (charCode < 0xd800 || charCode >= 0xe000) { | ||
utf8Bytes.push(0xe0 | (charCode >> 12), 0x80 | ((charCode >> 6) & 0x3f), 0x80 | (charCode & 0x3f)); | ||
} | ||
else { | ||
i++; | ||
charCode = 0x10000 + (((charCode & 0x3ff) << 10) | (_value.charCodeAt(i) & 0x3ff)); | ||
utf8Bytes.push(0xf0 | (charCode >> 18), 0x80 | ((charCode >> 12) & 0x3f), 0x80 | ((charCode >> 6) & 0x3f), 0x80 | (charCode & 0x3f)); | ||
} | ||
} | ||
// 4 bytes for length + string bytes | ||
this.encoded.store_value(utf8Bytes.length, 'u32'); | ||
this.encoded.store_bytes(new Uint8Array(utf8Bytes)); | ||
}; | ||
@@ -95,0 +111,0 @@ BorshSerializer.prototype.encode_boolean = function (value) { |
@@ -55,3 +55,23 @@ import { integers } from './types.js'; | ||
var buffer = new Uint8Array(this.buffer.consume_bytes(len)); | ||
return String.fromCharCode.apply(null, buffer); | ||
// decode utf-8 string without using TextDecoder | ||
// first get all bytes to single byte code points | ||
var codePoints = []; | ||
for (var i = 0; i < len; ++i) { | ||
var byte = buffer[i]; | ||
if (byte < 0x80) { | ||
codePoints.push(byte); | ||
} | ||
else if (byte < 0xE0) { | ||
codePoints.push(((byte & 0x1F) << 6) | (buffer[++i] & 0x3F)); | ||
} | ||
else if (byte < 0xF0) { | ||
codePoints.push(((byte & 0x0F) << 12) | ((buffer[++i] & 0x3F) << 6) | (buffer[++i] & 0x3F)); | ||
} | ||
else { | ||
var codePoint = ((byte & 0x07) << 18) | ((buffer[++i] & 0x3F) << 12) | ((buffer[++i] & 0x3F) << 6) | (buffer[++i] & 0x3F); | ||
codePoints.push(codePoint); | ||
} | ||
} | ||
// then decode code points to utf-8 | ||
return String.fromCodePoint.apply(String, codePoints); | ||
}; | ||
@@ -58,0 +78,0 @@ BorshDeserializer.prototype.decode_boolean = function () { |
@@ -61,8 +61,24 @@ import { integers } from './types.js'; | ||
var _value = value; | ||
// 4 bytes for length | ||
this.encoded.store_value(_value.length, 'u32'); | ||
// string bytes | ||
// encode to utf8 bytes without using TextEncoder | ||
var utf8Bytes = []; | ||
for (var i = 0; i < _value.length; i++) { | ||
this.encoded.store_value(_value.charCodeAt(i), 'u8'); | ||
var charCode = _value.charCodeAt(i); | ||
if (charCode < 0x80) { | ||
utf8Bytes.push(charCode); | ||
} | ||
else if (charCode < 0x800) { | ||
utf8Bytes.push(0xc0 | (charCode >> 6), 0x80 | (charCode & 0x3f)); | ||
} | ||
else if (charCode < 0xd800 || charCode >= 0xe000) { | ||
utf8Bytes.push(0xe0 | (charCode >> 12), 0x80 | ((charCode >> 6) & 0x3f), 0x80 | (charCode & 0x3f)); | ||
} | ||
else { | ||
i++; | ||
charCode = 0x10000 + (((charCode & 0x3ff) << 10) | (_value.charCodeAt(i) & 0x3ff)); | ||
utf8Bytes.push(0xf0 | (charCode >> 18), 0x80 | ((charCode >> 12) & 0x3f), 0x80 | ((charCode >> 6) & 0x3f), 0x80 | (charCode & 0x3f)); | ||
} | ||
} | ||
// 4 bytes for length + string bytes | ||
this.encoded.store_value(utf8Bytes.length, 'u32'); | ||
this.encoded.store_bytes(new Uint8Array(utf8Bytes)); | ||
}; | ||
@@ -69,0 +85,0 @@ BorshSerializer.prototype.encode_boolean = function (value) { |
{ | ||
"name": "borsh", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Binary Object Representation Serializer for Hashing", | ||
@@ -5,0 +5,0 @@ "main": "./lib/cjs/index.js", |
@@ -42,4 +42,4 @@ # Borsh JS | ||
The package exposes the following functions: | ||
- `serialize(schema: Schema, obj: any): Uint8Array` - serializes an object `obj` according to the schema `schema`. | ||
- `deserialize(schema: Schema, buffer: Uint8Array, class?: Class): any` - deserializes an object according to the schema `schema` from the buffer `buffer`. If the optional parameter `class` is present, the deserialized object will be an of `class`. | ||
- `serialize(schema: Schema, obj: any, validate: boolean = true): Uint8Array` - serializes an object `obj` according to the schema `schema`. Setting `validate` to false will skip the validation of the `schema`. | ||
- `deserialize(schema: Schema, buffer: Uint8Array, validate: boolean = true): any` - deserializes an object according to the schema `schema` from the buffer `buffer`. Setting `validate` to false will skip the validation of the `schema`. | ||
@@ -66,3 +66,3 @@ ## Schemas | ||
- `{ set: Schema }` - a set. The type of the elements is described by the `type` field. | ||
- `{ enum: [{ className1: { struct: {...} } }, { className2: { struct: {...} } }, ... ] }` - an enum. The variants of the enum are described by the `className1`, `className2`, etc. fields. The variants are structs. | ||
- `{ enum: [ { struct: { className1: structSchema1 } }, { struct: { className2: structSchema2 } }, ... ] }` - an enum. The variants of the enum are described by the `className1`, `className2`, etc. fields. The variants are structs. | ||
- `{ struct: { field1: Schema1, field2: Schema2, ... } }` - a struct. The fields of the struct are described by the `field1`, `field2`, etc. fields. | ||
@@ -124,2 +124,2 @@ | ||
[Borsh]: https://borsh.io | ||
[Borsh]: https://borsh.io |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
80119
1436
123
0