Skip to content

Commit 59c78bc

Browse files
authored
Drop node dependencies (#48)
1 parent 8e6d90a commit 59c78bc

37 files changed

+946
-1425
lines changed

.github/workflows/node.js.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212

1313
strategy:
1414
matrix:
15-
node-version: [12.x, 14.x, 16.x]
15+
node-version: [14.x, 16.x, 18.x]
1616

1717
steps:
1818
- uses: actions/checkout@v2

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
coverage.html
33
coverage/
44
node_modules/
5+
.parcel-cache
6+
dist

README.md

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,30 @@ This is just a small example of what Restructure can do. Check out the API docum
1717
below for more information.
1818

1919
```javascript
20-
var r = require('restructure');
20+
import * as r from 'restructure';
2121

22-
var Person = new r.Struct({
22+
let Person = new r.Struct({
2323
name: new r.String(r.uint8, 'utf8'),
2424
age: r.uint8
2525
});
2626

2727
// decode a person from a buffer
28-
var stream = new r.DecodeStream(buffer);
29-
Person.decode(stream); // returns an object with the fields defined above
28+
let value = Person.fromBuffer(new Uint8Array([/* ... */])); // returns an object with the fields defined above
3029

3130
// encode a person from an object
32-
// pipe the stream to a destination, such as a file
33-
var stream = new r.EncodeStream();
34-
stream.pipe(fs.createWriteStream('out.bin'));
35-
36-
Person.encode(stream, {
31+
let buffer = Person.toBuffer({
3732
name: 'Devon',
3833
age: 21
3934
});
40-
41-
stream.end();
4235
```
4336

44-
4537
## API
4638

4739
All of the following types support three standard methods:
4840

49-
* `decode(stream)` - decodes an instance of the type from the given DecodeStream
41+
* `fromBuffer(buffer)` - decodes an instance of the type from the given Uint8Array
5042
* `size(value)` - returns the amount of space the value would take if encoded
51-
* `encode(stream, value)` - encodes the given value into the given EncodeStream
43+
* `toBuffer(value)` - encodes the given value into a Uint8Array
5244

5345
Restructure supports a wide variety of types, but if you need to write your own for
5446
some custom use that cannot be represented by them, you can do so by just implementing
@@ -143,7 +135,7 @@ bitfield.encode(stream, result);
143135

144136
### Buffer
145137

146-
Extracts a slice of the buffer to a Node `Buffer`. The length can be a constant, or taken from
138+
Extracts a slice of the buffer to a `Uint8Array`. The length can be a constant, or taken from
147139
a previous field in the parent structure.
148140

149141
```javascript
@@ -162,8 +154,9 @@ var struct = new r.Struct({
162154
A `String` maps a JavaScript string to and from binary encodings. The length can be a constant, taken
163155
from a previous field in the parent structure, or encoded using a number type immediately before the string.
164156

165-
Supported encodings include `'ascii'`, `'utf8'`, `'ucs2'`, `'utf16le'`, `'utf16be'`, and if you also install
166-
[iconv-lite](https://github.com/ashtuchkin/iconv-lite), many other legacy codecs.
157+
Fully supported encodings include `'ascii'`, `'utf8'`, `'ucs2'`, `'utf16le'`, `'utf16be'`. Decoding is also possible
158+
with any encoding supported by [TextDecoder](https://developer.mozilla.org/en-US/docs/Web/API/Encoding_API/Encodings),
159+
however encoding these is not supported.
167160

168161
```javascript
169162
// fixed length, ascii encoding by default

index.js

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
exports.EncodeStream = require('./src/EncodeStream');
2-
exports.DecodeStream = require('./src/DecodeStream');
3-
exports.Array = require('./src/Array');
4-
exports.LazyArray = require('./src/LazyArray');
5-
exports.Bitfield = require('./src/Bitfield');
6-
exports.Boolean = require('./src/Boolean');
7-
exports.Buffer = require('./src/Buffer');
8-
exports.Enum = require('./src/Enum');
9-
exports.Optional = require('./src/Optional');
10-
exports.Reserved = require('./src/Reserved');
11-
exports.String = require('./src/String');
12-
exports.Struct = require('./src/Struct');
13-
exports.VersionedStruct = require('./src/VersionedStruct');
1+
export {EncodeStream} from './src/EncodeStream.js';
2+
export {DecodeStream} from './src/DecodeStream.js';
3+
export {Array} from './src/Array.js';
4+
export {LazyArray} from './src/LazyArray.js';
5+
export {Bitfield} from './src/Bitfield.js';
6+
export {Boolean} from './src/Boolean.js';
7+
export {Buffer} from './src/Buffer.js';
8+
export {Enum} from './src/Enum.js';
9+
export {Optional} from './src/Optional.js';
10+
export {Reserved} from './src/Reserved.js';
11+
export {String} from './src/String.js';
12+
export {Struct} from './src/Struct.js';
13+
export {VersionedStruct} from './src/VersionedStruct.js';
1414

15-
const utils = require('./src/utils');
16-
const NumberT = require('./src/Number');
17-
const Pointer = require('./src/Pointer');
18-
19-
Object.assign(exports, utils, NumberT, Pointer);
15+
export * from './src/utils.js';
16+
export * from './src/Number.js';
17+
export * from './src/Pointer.js';

package.json

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,25 @@
22
"name": "restructure",
33
"version": "2.0.1",
44
"description": "Declaratively encode and decode binary data",
5-
"main": "index.js",
5+
"type": "module",
6+
"main": "./dist/main.cjs",
7+
"module": "./index.js",
8+
"source": "./index.js",
9+
"exports": {
10+
"import": "./index.js",
11+
"require": "./dist/main.cjs"
12+
},
13+
"targets": {
14+
"module": false
15+
},
616
"devDependencies": {
7-
"chai": "~4.2.0",
8-
"concat-stream": "~2.0.0",
9-
"coveralls": "^3.0.11",
10-
"iconv-lite": "^0.5.1",
11-
"mocha": "~7.1.1",
12-
"nyc": "^15.0.1"
17+
"mocha": "^10.0.0",
18+
"parcel": "^2.6.1"
1319
},
1420
"scripts": {
15-
"test": "mocha --reporter spec",
16-
"cover": "nyc --reporter=html --reporter=text mocha",
17-
"coveralls": "nyc report --reporter=text-lcov | coveralls"
21+
"test": "mocha",
22+
"build": "parcel build",
23+
"prepublishOnly": "parcel build"
1824
},
1925
"repository": {
2026
"type": "git",

src/Array.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
const {Number:NumberT} = require('./Number');
2-
const utils = require('./utils');
1+
import {Base} from './Base.js';
2+
import {Number as NumberT} from './Number.js';
3+
import * as utils from './utils.js';
34

4-
class ArrayT {
5+
class ArrayT extends Base {
56
constructor(type, length, lengthType = 'count') {
7+
super();
68
this.type = type;
79
this.length = length;
810
this.lengthType = lengthType;
@@ -52,21 +54,25 @@ class ArrayT {
5254
return res;
5355
}
5456

55-
size(array, ctx) {
57+
size(array, ctx, includePointers = true) {
5658
if (!array) {
5759
return this.type.size(null, ctx) * utils.resolveLength(this.length, null, ctx);
5860
}
5961

6062
let size = 0;
6163
if (this.length instanceof NumberT) {
6264
size += this.length.size();
63-
ctx = {parent: ctx};
65+
ctx = {parent: ctx, pointerSize: 0};
6466
}
6567

6668
for (let item of array) {
6769
size += this.type.size(item, ctx);
6870
}
6971

72+
if (ctx && includePointers && this.length instanceof NumberT) {
73+
size += ctx.pointerSize;
74+
}
75+
7076
return size;
7177
}
7278

@@ -79,7 +85,7 @@ class ArrayT {
7985
parent
8086
};
8187

82-
ctx.pointerOffset = stream.pos + this.size(array, ctx);
88+
ctx.pointerOffset = stream.pos + this.size(array, ctx, false);
8389
this.length.encode(stream, array.length);
8490
}
8591

@@ -91,11 +97,10 @@ class ArrayT {
9197
let i = 0;
9298
while (i < ctx.pointers.length) {
9399
const ptr = ctx.pointers[i++];
94-
ptr.type.encode(stream, ptr.val);
100+
ptr.type.encode(stream, ptr.val, ptr.parent);
95101
}
96102
}
97-
98103
}
99104
}
100105

101-
module.exports = ArrayT;
106+
export {ArrayT as Array};

src/Base.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {DecodeStream} from './DecodeStream.js';
2+
import {EncodeStream} from './EncodeStream.js';
3+
4+
export class Base {
5+
fromBuffer(buffer) {
6+
let stream = new DecodeStream(buffer);
7+
return this.decode(stream);
8+
}
9+
10+
toBuffer(value) {
11+
let size = this.size(value);
12+
let buffer = new Uint8Array(size);
13+
let stream = new EncodeStream(buffer);
14+
this.encode(stream, value);
15+
return buffer;
16+
}
17+
}

src/Bitfield.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
class Bitfield {
1+
import {Base} from './Base.js';
2+
3+
export class Bitfield extends Base {
24
constructor(type, flags = []) {
5+
super();
36
this.type = type;
47
this.flags = flags;
58
}
9+
610
decode(stream) {
711
const val = this.type.decode(stream);
812

@@ -33,5 +37,3 @@ class Bitfield {
3337
return this.type.encode(stream, val);
3438
}
3539
}
36-
37-
module.exports = Bitfield;

src/Boolean.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
class BooleanT {
1+
import {Base} from './Base.js';
2+
3+
export class BooleanT extends Base {
24
constructor(type) {
5+
super();
36
this.type = type;
47
}
58

@@ -16,4 +19,4 @@ class BooleanT {
1619
}
1720
}
1821

19-
module.exports = BooleanT;
22+
export {BooleanT as Boolean};

src/Buffer.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
const utils = require('./utils');
2-
const {Number:NumberT} = require('./Number');
1+
import {Base} from './Base.js';
2+
import {Number as NumberT} from './Number.js';
3+
import * as utils from './utils.js';
34

4-
class BufferT {
5+
export class BufferT extends Base {
56
constructor(length) {
7+
super();
68
this.length = length;
79
}
10+
811
decode(stream, parent) {
912
const length = utils.resolveLength(this.length, stream, parent);
1013
return stream.readBuffer(length);
@@ -15,7 +18,12 @@ class BufferT {
1518
return utils.resolveLength(this.length, null, parent);
1619
}
1720

18-
return val.length;
21+
let len = val.length;
22+
if (this.length instanceof NumberT) {
23+
len += this.length.size();
24+
}
25+
26+
return len;
1927
}
2028

2129
encode(stream, buf, parent) {
@@ -27,4 +35,4 @@ class BufferT {
2735
}
2836
}
2937

30-
module.exports = BufferT;
38+
export {BufferT as Buffer};

0 commit comments

Comments
 (0)