Skip to content

Commit cff637d

Browse files
committed
Fixed #50
1 parent 4710bba commit cff637d

File tree

241 files changed

+5816
-3007
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

241 files changed

+5816
-3007
lines changed

README.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ const tribeType = new sb.StructType({
8383
money: new sb.MapType(personType, new sb.FloatType)
8484
});
8585
````
86-
### Converting types and values to `Buffer`s
86+
### Converting types and values to binary data
8787
````javascript
8888
const sb = require('structure-bytes');
8989

@@ -97,9 +97,9 @@ const tribeType = new sb.StructType({
9797
members: new sb.SetType(personType),
9898
money: new sb.MapType(personType, new sb.FloatType)
9999
});
100-
let typeBuffer = tribeType.toBuffer(); //ArrayBuffer { byteLength: 49 }
100+
let typeBuffer = tribeType.toBuffer(); //ArrayBuffer { byteLength: 47 }
101101
console.log(Buffer.from(typeBuffer));
102-
//<Buffer 51 03 06 6c 65 61 64 65 72 51 03 03 64 6f 62 15 02 69 64 12 04 6e 61 6d 65 41 07 6d 65 6d 62 65 72 73 53 ff 00 1b 05 6d 6f 6e 65 79 54 ff 00 25 20>
102+
//<Buffer 51 03 06 6c 65 61 64 65 72 51 03 03 64 6f 62 1a 02 69 64 12 04 6e 61 6d 65 41 07 6d 65 6d 62 65 72 73 53 ff 1b 05 6d 6f 6e 65 79 54 ff 24 20>
103103

104104
let louis = {
105105
dob: new Date(1437592284193),
@@ -120,16 +120,16 @@ let value = {
120120
members: new Set().add(louis).add(garfield),
121121
money: new Map().set(louis, 23.05).set(garfield, -10.07)
122122
};
123-
let valueBuffer = tribeType.valueBuffer(value); //ArrayBuffer { byteLength: 100 }
123+
let valueBuffer = tribeType.valueBuffer(value); //ArrayBuffer { byteLength: 94 }
124124
console.log(Buffer.from(valueBuffer));
125-
//<Buffer 00 00 01 4e b7 2d 6c 20 00 0a 4a 6f 65 00 00 00 00 02 00 00 01 4e b7 2d 6c 21 00 09 4c 6f 75 69 73 00 00 00 01 4e b7 2d 6c 22 00 11 47 61 72 66 69 65 ... >
125+
//<Buffer 00 00 01 4e b7 2d 6c 20 00 0a 4a 6f 65 00 02 00 00 01 4e b7 2d 6c 21 00 09 4c 6f 75 69 73 00 00 00 01 4e b7 2d 6c 22 00 11 47 61 72 66 69 65 6c 64 00 ... >
126126
````
127-
### Reading from type and value `Buffer`s
127+
### Reading from type and value buffers
128128
````javascript
129129
const sb = require('structure-bytes');
130130

131131
//Buffer obtained somehow
132-
let tribeBuffer = new Uint8Array([0x51, 0x03, 0x06, 0x6c, 0x65, 0x61, 0x64, 0x65, 0x72, 0x51, 0x03, 0x03, 0x64, 0x6f, 0x62, 0x15, 0x02, 0x69, 0x64, 0x12, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x41, 0x07, 0x6d, 0x65, 0x6d, 0x62, 0x65, 0x72, 0x73, 0x53, 0xff, 0x00, 0x1b, 0x05, 0x6d, 0x6f, 0x6e, 0x65, 0x79, 0x54, 0xff, 0x00, 0x25, 0x20]);
132+
let tribeBuffer = new Uint8Array([0x51, 0x03, 0x06, 0x6c, 0x65, 0x61, 0x64, 0x65, 0x72, 0x51, 0x03, 0x03, 0x64, 0x6f, 0x62, 0x1a, 0x02, 0x69, 0x64, 0x12, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x41, 0x07, 0x6d, 0x65, 0x6d, 0x62, 0x65, 0x72, 0x73, 0x53, 0xff, 0x1b, 0x05, 0x6d, 0x6f, 0x6e, 0x65, 0x79, 0x54, 0xff, 0x24, 0x20]);
133133
let type = sb.r.type(tribeBuffer.buffer);
134134
console.log(type);
135135
/*
@@ -141,8 +141,8 @@ StructType {
141141
*/
142142

143143
//Buffer obtained somehow
144-
let valueBuffer = new Uint8Array([0x00, 0x00, 0x01, 0x4e, 0xb7, 0x2d, 0x6c, 0x20, 0x00, 0x0a, 0x4a, 0x6f, 0x65, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x01, 0x4e, 0xb7, 0x2d, 0x6c, 0x21, 0x00, 0x09, 0x4c, 0x6f, 0x75, 0x69, 0x73, 0x00, 0x00, 0x00, 0x01, 0x4e, 0xb7, 0x2d, 0x6c, 0x22, 0x00, 0x11, 0x47, 0x61, 0x72, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x01, 0x4e, 0xb7, 0x2d, 0x6c, 0x21, 0x00, 0x09, 0x4c, 0x6f, 0x75, 0x69, 0x73, 0x00, 0x41, 0xb8, 0x66, 0x66, 0x00, 0x00, 0x01, 0x4e, 0xb7, 0x2d, 0x6c, 0x22, 0x00, 0x11, 0x47, 0x61, 0x72, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x00, 0xc1, 0x21, 0x1e, 0xb8]);
145-
console.log(sb.r.value({type, buffer: valueBuffer.buffer}));
144+
let valueBuffer = new Uint8Array([0x00, 0x00, 0x01, 0x4e, 0xb7, 0x2d, 0x6c, 0x20, 0x00, 0x0a, 0x4a, 0x6f, 0x65, 0x00, 0x02, 0x00, 0x00, 0x01, 0x4e, 0xb7, 0x2d, 0x6c, 0x21, 0x00, 0x09, 0x4c, 0x6f, 0x75, 0x69, 0x73, 0x00, 0x00, 0x00, 0x01, 0x4e, 0xb7, 0x2d, 0x6c, 0x22, 0x00, 0x11, 0x47, 0x61, 0x72, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x00, 0x02, 0x00, 0x00, 0x01, 0x4e, 0xb7, 0x2d, 0x6c, 0x21, 0x00, 0x09, 0x4c, 0x6f, 0x75, 0x69, 0x73, 0x00, 0x41, 0xb8, 0x66, 0x66, 0x00, 0x00, 0x01, 0x4e, 0xb7, 0x2d, 0x6c, 0x22, 0x00, 0x11, 0x47, 0x61, 0x72, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x00, 0xc1, 0x21, 0x1e, 0xb8]);
145+
console.log(type.readValue(valueBuffer.buffer));
146146
/*
147147
{ leader: { dob: 2015-07-22T19:11:24.192Z, id: 10, name: 'Joe' },
148148
members:
@@ -287,6 +287,11 @@ that it requires the value to be of type:
287287
*/
288288
````
289289
You can even add explicit `VALUE` generics to make the compiler check that your `Type` serializes the correct types of values.
290+
Many non-primitive types (e.g. `ArrayType`, `MapType`, `StructType`) can take either one or two generics.
291+
The first, `VALUE`, specifies the type of values the type can write.
292+
The second, `READ_VALUE`, specifies the type of values the type will read from written values and defaults to `VALUE` if omitted.
293+
Generally the two generics are the same, except in the case of numeric types (which write `number | string` but read `number`) and `OptionalType` (which writes `E | null | undefined` but reads `E | null`).
294+
`READ_VALUE` must be a subset of `VALUE`, so in most cases where you specify the generic it makes sense to restrict `VALUE` to the type of read values and let `READ_VALUE` default to `VALUE`.
290295

291296
With `StructType`:
292297
````typescript
@@ -328,10 +333,11 @@ let structType = new sb.StructType<SerializedCar>({
328333

329334
With `ChoiceType` (and similarly for `NamedChoiceType`), you can let the type be inferred automatically if each of the possible types writes the same type of values:
330335
````javascript
331-
let type = new sb.ChoiceType([ //Type<number | string>
336+
let type = new sb.ChoiceType([ //Type<number | string, number>
332337
new sb.ByteType,
333338
new sb.ShortType,
334-
new sb.IntType
339+
new sb.IntType,
340+
new sb.DoubleType
335341
])
336342
````
337343
However, if the value types are not the same, TypeScript will complain about them not matching, and you should express the union type explicitly:

client-side/download.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export function download({name, url, options}: DownloadOptions): Promise<any> {
6565
if (typeInCache && typeInCache.sig === sig) {
6666
return response.arrayBuffer()
6767
.then(buffer => {
68-
const value = r.value({buffer, type: typeInCache.type})
68+
const value = typeInCache.type.readValue(buffer)
6969
return Promise.resolve(value)
7070
})
7171
}
@@ -74,7 +74,7 @@ export function download({name, url, options}: DownloadOptions): Promise<any> {
7474
.then(buffer => {
7575
const readType = r._consumeType(buffer, 0)
7676
const type = readType.value
77-
const value = r.value({buffer, offset: readType.length, type})
77+
const value = type.readValue(buffer, readType.length)
7878
typeCache[name] = {sig, type}
7979
saveTypeCache()
8080
return Promise.resolve(value)

0 commit comments

Comments
 (0)