REDROOM
PHP 8.2.30
Path:
Logout
Edit File
Size: 3.45 KB
Close
/home/byroehnu/.trash/node_modules11/restructure/test/Struct.js
Text
Base64
import assert from 'assert'; import {Struct, String as StringT, Pointer, uint8, DecodeStream, EncodeStream} from 'restructure'; describe('Struct', function() { describe('decode', function() { it('should decode into an object', function() { const struct = new Struct({ name: new StringT(uint8), age: uint8 }); assert.deepEqual(struct.fromBuffer(Buffer.from('\x05devon\x15')), { name: 'devon', age: 21 }); }); it('should support process hook', function() { const struct = new Struct({ name: new StringT(uint8), age: uint8 }); struct.process = function() { return this.canDrink = this.age >= 21; }; assert.deepEqual(struct.fromBuffer(Buffer.from('\x05devon\x20')), { name: 'devon', age: 32, canDrink: true }); }); it('should support function keys', function() { const struct = new Struct({ name: new StringT(uint8), age: uint8, canDrink() { return this.age >= 21; } }); assert.deepEqual(struct.fromBuffer(Buffer.from('\x05devon\x20')), { name: 'devon', age: 32, canDrink: true }); }); }); describe('size', function() { it('should compute the correct size', function() { const struct = new Struct({ name: new StringT(uint8), age: uint8 }); assert.equal(struct.size({name: 'devon', age: 21}), 7); }); it('should compute the correct size with pointers', function() { const struct = new Struct({ name: new StringT(uint8), age: uint8, ptr: new Pointer(uint8, new StringT(uint8)) }); const size = struct.size({ name: 'devon', age: 21, ptr: 'hello' }); assert.equal(size, 14); }); it('should get the correct size when no value is given', function() { const struct = new Struct({ name: new StringT(4), age: uint8 }); assert.equal(struct.size(), 5); }); it('should throw when getting non-fixed length size and no value is given', function() { const struct = new Struct({ name: new StringT(uint8), age: uint8 }); assert.throws(() => struct.size(), /not a fixed size/i); }); }); describe('encode', function() { it('should encode objects to buffers', function() { const struct = new Struct({ name: new StringT(uint8), age: uint8 }); const buf = struct.toBuffer({ name: 'devon', age: 21 }); assert.deepEqual(buf, Buffer.from('\x05devon\x15')); }); it('should support preEncode hook', function() { const struct = new Struct({ nameLength: uint8, name: new StringT('nameLength'), age: uint8 }); struct.preEncode = function() { return this.nameLength = this.name.length; }; const buf = struct.toBuffer({ name: 'devon', age: 21 }); assert.deepEqual(buf, Buffer.from('\x05devon\x15')); }); it('should encode pointer data after structure', function() { const struct = new Struct({ name: new StringT(uint8), age: uint8, ptr: new Pointer(uint8, new StringT(uint8)) }); const buf = struct.toBuffer({ name: 'devon', age: 21, ptr: 'hello' }); assert.deepEqual(buf, Buffer.from('\x05devon\x15\x08\x05hello')); }); }); });
Save
Close
Exit & Reset
Text mode: syntax highlighting auto-detects file type.
Directory Contents
Dirs: 0 × Files: 15
Delete Selected
Select All
Select None
Sort:
Name
Size
Modified
Enable drag-to-move
Name
Size
Perms
Modified
Actions
Array.js
3.97 KB
lrw-r--r--
2026-02-14 19:19:14
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
Bitfield.js
1.04 KB
lrw-r--r--
2026-02-14 19:19:14
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
Boolean.js
1.15 KB
lrw-r--r--
2026-02-14 19:19:16
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
Buffer.js
1.53 KB
lrw-r--r--
2026-02-14 19:19:16
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
DecodeStream.js
3.53 KB
lrw-r--r--
2026-02-14 19:19:16
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
EncodeStream.js
3.74 KB
lrw-r--r--
2026-02-14 19:19:16
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
Enum.js
831 B
lrw-r--r--
2026-02-14 19:19:16
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
LazyArray.js
1.87 KB
lrw-r--r--
2026-02-14 19:19:18
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
Number.js
11.03 KB
lrw-r--r--
2026-02-14 19:19:18
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
Optional.js
3.83 KB
lrw-r--r--
2026-02-14 19:19:18
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
Pointer.js
9.48 KB
lrw-r--r--
2026-02-14 19:19:18
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
Reserved.js
820 B
lrw-r--r--
2026-02-14 19:19:18
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
String.js
5.61 KB
lrw-r--r--
2026-02-14 19:19:18
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
Struct.js
3.45 KB
lrw-r--r--
2026-02-14 19:19:18
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
VersionedStruct.js
11.37 KB
lrw-r--r--
2026-02-14 19:19:18
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
Zip Selected
If ZipArchive is unavailable, a
.tar
will be created (no compression).