New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

construct-js

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

construct-js - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

2

package.json
{
"name": "construct-js",
"version": "0.1.0",
"version": "0.2.0",
"description": "",

@@ -5,0 +5,0 @@ "main": "src/index.js",

@@ -20,3 +20,5 @@ # construct-js

- [2.1.7 computeBufferSize](#computeBufferSize)
- [2.1.8 toBuffer](#toBuffer)
- [2.1.8 toArrayBuffer](#toArrayBuffer)
- [2.1.9 toBuffer](#toBuffer)
- [2.1.10 toBytes](#toBytes)
- [2.2 BitStruct](#bitstruct)

@@ -28,2 +30,4 @@ - [2.2.1 flag](#flag)

- [2.2.5 toBuffer](#toBuffer-1)
- [2.2.6 toArrayBuffer](#toArrayBuffer-1)
- [2.2.7 toArray](#toArray-1)
- [2.3 Fields](#fields)

@@ -66,4 +70,4 @@ - [2.3.1 U8](#U8)

RawString,
Word,
DoubleWord,
U16,
U32,
Struct,

@@ -76,15 +80,15 @@ } = require('construct-js');

const sharedHeaderInfo = Struct('sharedHeaderInfo')
.field('minVersion', Word(10))
.field('gpFlag', Word(0))
.field('compressionMethod', Word(0))
.field('lastModifiedTime', Word(0))
.field('lastModifiedDate', Word(0))
.field('crc32', DoubleWord(0))
.field('compressedSized', DoubleWord(data.byteLength))
.field('uncompressedSized', DoubleWord(data.byteLength))
.field('filenameSize', Word(filename.byteLength))
.field('extraFieldLength', Word(0));
.field('minVersion', U16(10))
.field('gpFlag', U16(0))
.field('compressionMethod', U16(0))
.field('lastModifiedTime', U16(0))
.field('lastModifiedDate', U16(0))
.field('crc32', U32(0))
.field('compressedSized', U32(data.byteLength))
.field('uncompressedSized', U32(data.byteLength))
.field('filenameSize', U16(filename.byteLength))
.field('extraFieldLength', U16(0));
const localHeader = Struct('localHeader')
.field('header', DoubleWord(0x04034b50))
.field('header', U32(0x04034b50))
.field('sharedHeaderInfo', sharedHeaderInfo)

@@ -94,21 +98,21 @@ .field('filename', filename);

const centralDirectory = Struct('centralDirectory')
.field('header', DoubleWord(0x02014b50))
.field('madeByVersion', Word(10))
.field('header', U32(0x02014b50))
.field('madeByVersion', U16(10))
.field('sharedHeaderInfo', sharedHeaderInfo)
.field('fileCommentSize', Word(0))
.field('diskNumber', Word(0))
.field('internalFileAttributes', Word(0))
.field('externalFileAttributes', DoubleWord(0))
.field('relativeOffset', DoubleWord(0))
.field('fileCommentSize', U16(0))
.field('diskNumber', U16(0))
.field('internalFileAttributes', U16(0))
.field('externalFileAttributes', U32(0))
.field('relativeOffset', U32(0))
.field('filename', filename);
const endOfCentralDirectory = Struct('endOfCentralDirectory')
.field('header', DoubleWord(0x06054b50))
.field('diskNumber', Word(0))
.field('centralDirDiskStart', Word(0))
.field('numberOfCentralDirsOnDisk', Word(1))
.field('totalNumberOfCentralDirs', Word(1))
.field('centralDirSize', DoubleWord(0))
.field('offsetToStart', DoubleWord(0))
.field('commentLength', Word(0));
.field('header', U32(0x06054b50))
.field('diskNumber', U16(0))
.field('centralDirDiskStart', U16(0))
.field('numberOfCentralDirsOnDisk', U16(1))
.field('totalNumberOfCentralDirs', U16(1))
.field('centralDirSize', U32(0))
.field('offsetToStart', U32(0))
.field('commentLength', U16(0));

@@ -192,2 +196,8 @@

#### toArrayBuffer
`.toArrayBuffer()`
Returns an `ArrayBuffer` representation of the Struct. You should use this if working in the browser.
#### toBuffer

@@ -199,2 +209,8 @@

#### toBytes
`.toBytes()`
Returns a regular `Array` representation of the Struct.
### BitStruct

@@ -238,2 +254,14 @@

#### toArrayBuffer
`.toArrayBuffer()`
Returns a byte-aligned `ArrayBuffer` representing the BitStruct.
#### toArray
`.toArray()`
Returns a byte-aligned `Array` representing the BitStruct.
### Fields

@@ -251,3 +279,3 @@

The rest of the properties should be considered private and not modifed directly.
The rest of the properties should be considered private and not modified directly.

@@ -349,54 +377,48 @@ #### U8

All fields contain some common properties and methods. These are:
#### Byte
`.set(value | values)`
`Byte(value)`
Which sets either the value or values of the field.
`.setIsLittleEndian(trueOrFalse)`
Manually sets this field to little or big endian.
The rest of the properties should be considered private and not modifed directly.
#### U8
`U8(value)`
A single 8-bit unsigned value.
#### U16
#### Word
`U16(value)`
`Word(value)`
A single 16-bit unsigned value.
#### U32
#### DoubleWord
`U32(value)`
`DoubleWord(value)`
A single 32-bit unsigned value.
#### I8
#### SignedByte
`I8(value)`
`SignedByte(value)`
A single 8-bit signed value.
#### I16
#### SignedWord
`I16(value)`
`SignedWord(value)`
A single 16-bit signed value.
#### I32
#### SignedDoubleWord
`I32(value)`
`SignedDoubleWord(value)`
A single 32-bit signed value.
#### U8s
#### RawString
`U8s(array | number)`
`RawString(string)`
A collection of 8-bit unsigned values, interpreted directly from the string provided. The size of the field is the **byte length** of the string (which is not always the `string.length` when considering unicode).
#### Bytes
`Bytes(array | number)`
A collection of 8-bit unsigned values.

@@ -406,5 +428,5 @@

#### U16s
#### Words
`U16s(array | number)`
`Words(array | number)`

@@ -415,5 +437,5 @@ A collection of 16-bit unsigned values.

#### U32s
#### DoubleWords
`U32s(array | number)`
`DoubleWords(array | number)`

@@ -424,5 +446,5 @@ A collection of 32-bit unsigned values.

#### I8s
#### SignedBytes
`I8s(array | number)`
`SignedBytes(array | number)`

@@ -433,5 +455,5 @@ A collection of 8-bit signed values.

#### I16s
#### SignedWords
`I16s(array | number)`
`SignedWords(array | number)`

@@ -442,5 +464,5 @@ A collection of 16-bit signed values.

#### I32s
#### SignedDoubleWords
`I32s(array | number)`
`SignedDoubleWords(array | number)`

@@ -447,0 +469,0 @@ A collection of 32-bit signed values.

@@ -20,5 +20,5 @@ class DataValue {

this.arrayBuffer = new ArrayBuffer(size * this.byteWidth);
this.byteLength = size * this.byteWidth;
this.arrayBuffer = new ArrayBuffer(this.byteLength);
this.dataView = new DataView(this.arrayBuffer);
this.buffer = Buffer.from(this.arrayBuffer);
this.littleEndian = littleEndian;

@@ -44,4 +44,3 @@

this.size = this.raw.length;
this.buffer = Buffer.from(this.arrayBuffer);
this.byteLength = this.buffer.byteLength;
this.byteLength = this.size * this.byteWidth;
return this;

@@ -57,2 +56,24 @@ }

}
toBuffer() {
return Buffer.from(this.arrayBuffer);
}
toBytes() {
const bytes = Array.from({length: this.byteLength});
for (let i = 0; i < this.byteLength; i++) {
bytes[i] = this.dataView.getUint8(i, this.littleEndian);
}
return bytes;
}
writeBytes(array, offset) {
for (let i = offset; i < offset + this.byteLength; i++) {
array[i] = this.dataView.getUint8(i - offset, this.littleEndian);
}
return offset + this.byteLength;
}
}

@@ -195,3 +216,3 @@

} else {
size += field.buffer.byteLength;
size += field.byteLength;
}

@@ -227,3 +248,3 @@ }

}
return acc + field.buffer.byteLength;
return acc + field.byteLength;
}, 0);

@@ -240,2 +261,33 @@ }

}
toBytes() {
const bytes = Array.from({length: this.computeBufferSize()});
let offset = 0;
this.fields.forEach(([_, field]) => {
offset = field.writeBytes(bytes, offset);
});
return bytes;
}
writeBytes(bytes, offset) {
let newOffset = offset;
this.fields.forEach(([_, field]) => {
newOffset = field.writeBytes(bytes, newOffset);
});
return newOffset;
}
toArrayBuffer() {
const bytes = this.toBytes();
const ab = new ArrayBuffer(bytes.length);
const view = new DataView(ab);
for (let i = 0; i < bytes.length; i++) {
view.setUint8(i, bytes[i]);
}
return ab;
}
}

@@ -317,2 +369,21 @@

}
toBytes() {
return bits.reduce((bytes, bit, i) => {
const byteIndex = Math.floor(i/8);
const bitIndex = i % 8;
bytes[byteIndex] += bit << bitIndex;
return bytes;
}, Array.from({length: this.computeBufferSize()}).fill(0));
}
writeBytes(array, offset) {
const bytes = this.toBytes();
for (let i = offset; i < offset + bytes.length; i++) {
array[i] = bytes[i - offset];
}
return offset + bytes.length;
}
}

@@ -319,0 +390,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc