Skip to content

Commit

Permalink
Remove Buffer to make the package smaller and run faster in the bro…
Browse files Browse the repository at this point in the history
…wser (#145)
  • Loading branch information
tachibana-shin authored Apr 5, 2024
1 parent be3b748 commit db8afd6
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ This section describes the structure of the object returned by `parse()` method.
| ----------------- | -------- | -------- | --------- | ------------- |
| `method` | string | Yes | N/A | See METHOD attribute in [EXT-X-KEY](https://tools.ietf.org/html/draft-pantos-http-live-streaming-23#section-4.3.2.4) |
| `uri` | string | No | undefined | See URI attribute in [EXT-X-KEY](https://tools.ietf.org/html/draft-pantos-http-live-streaming-23#section-4.3.2.4) |
| `iv` | `Buffer`(length=16) | No | undefined | See IV attribute in [EXT-X-KEY](https://tools.ietf.org/html/draft-pantos-http-live-streaming-23#section-4.3.2.4) |
| `iv` | `ArrayBuffer`(length=16) | No | undefined | See IV attribute in [EXT-X-KEY](https://tools.ietf.org/html/draft-pantos-http-live-streaming-23#section-4.3.2.4) |
| `format` | string | No | undefined | See KEYFORMAT attribute in [EXT-X-KEY](https://tools.ietf.org/html/draft-pantos-http-live-streaming-23#section-4.3.2.4) |
| `formatVersion` | string | No | undefined | See KEYFORMATVERSIONS attribute in [EXT-X-KEY](https://tools.ietf.org/html/draft-pantos-http-live-streaming-23#section-4.3.2.4) |

Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,13 @@
"import/extensions": 0,
"import/no-dynamic-require": 0,
"new-cap": 0,
"node/prefer-global/buffer": 0,
"no-bitwise": 0,
"no-cond-assign": 0,
"no-mixed-operators": 0,
"no-multi-assign": 0,
"no-use-extend-native/no-use-extend-native": 0,
"object-curly-newline": 0,
"operator-linebreak": 0,
"n/prefer-global/buffer": 0,
"padding-line-between-statements": 0,
"quotes": 0,
"unicorn/catch-error-name": 0,
Expand Down
8 changes: 4 additions & 4 deletions parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function parseAllowedCpc(str: string) {
return allowedCpcList;
}

function parseIV(str: string): Buffer {
function parseIV(str: string): Uint8Array {
const iv = utils.hexToByteSequence(str);
if (iv.length !== 16) {
utils.INVALIDPLAYLIST('IV must be a 128-bit unsigned integer');
Expand Down Expand Up @@ -373,10 +373,10 @@ function sameKey(key1: Key, key2: Key) {
if (!key2.iv) {
return false;
}
if (key1.iv.length !== key2.iv.length) {
if (key1.iv.byteLength !== key2.iv.byteLength) {
return false;
}
for (let i = 0; i < key1.iv.length; i++) {
for (let i = 0; i < key1.iv.byteLength; i++) {
if (key1.iv[i] !== key2.iv[i]) {
return false;
}
Expand Down Expand Up @@ -962,7 +962,7 @@ function lexicalParse(text: string, params: Record<string, any>) {
// V8 has garbage collection issues when cleaning up substrings split from strings greater
// than 13 characters so before we continue we need to safely copy over each line so that it
// doesn't hold any reference to the containing string.
const line = Buffer.from(l.trim()).toString();
const line = l.trim();
if (!line) {
// empty line
continue;
Expand Down
2 changes: 1 addition & 1 deletion stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function buildKey(key: Key, isSessionKey?: any) {
attrs.push(`URI="${key.uri}"`);
}
if (key.iv) {
if (key.iv.length !== 16) {
if (key.iv.byteLength !== 16) {
utils.INVALIDPLAYLIST('IV must be a 128-bit unsigned integer');
}
attrs.push(`IV=${utils.byteSequenceToHex(key.iv)}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ test('#EXT-X-DATERANGE_06', t => {
http://example.com/3
`);
t.is(playlist.segments[0].dateRange.attributes['X-STR'], 'abc');
t.deepEqual(playlist.segments[1].dateRange.attributes['X-BYTE'], Buffer.from([255, 254, 253, 252]));
t.deepEqual(playlist.segments[1].dateRange.attributes['X-BYTE'], new Uint8Array([255, 254, 253, 252]));
t.is(playlist.segments[2].dateRange.attributes['X-FLOAT'], 0.999);
});

Expand Down
12 changes: 6 additions & 6 deletions test/spec/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ test('utils.toNumber', t => {
});

test('utils.hexToByteSequence', t => {
t.deepEqual(utils.hexToByteSequence('0x000000'), Buffer.from([0, 0, 0]));
t.deepEqual(utils.hexToByteSequence('0xFFFFFF'), Buffer.from([255, 255, 255]));
t.deepEqual(utils.hexToByteSequence('FFFFFF'), Buffer.from([255, 255, 255]));
t.deepEqual(utils.hexToByteSequence('0x000000'), new Uint8Array([0, 0, 0]));
t.deepEqual(utils.hexToByteSequence('0xFFFFFF'), new Uint8Array([255, 255, 255]));
t.deepEqual(utils.hexToByteSequence('FFFFFF'), new Uint8Array([255, 255, 255]));
});

test('utils.byteSequenceToHex', t => {
t.is(utils.byteSequenceToHex(Buffer.from([0, 0, 0])), '0x000000');
t.is(utils.byteSequenceToHex(Buffer.from([255, 255, 255])), '0xFFFFFF');
t.is(utils.byteSequenceToHex(Buffer.from([255, 255, 256])), '0xFFFF00');
t.is(utils.byteSequenceToHex(new Uint8Array([0, 0, 0])), '0x000000');
t.is(utils.byteSequenceToHex(new Uint8Array([255, 255, 255])), '0xFFFFFF');
t.is(utils.byteSequenceToHex(new Uint8Array([255, 255, 256])), '0xFFFF00');
});

test('utils.tryCatch', t => {
Expand Down
2 changes: 1 addition & 1 deletion types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class SessionData {
class Key {
method: string;
uri?: string;
iv?: Buffer;
iv?: ArrayBuffer;
format?: string;
formatVersion?: string;

Expand Down
10 changes: 5 additions & 5 deletions utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,18 @@ function toNumber(str: string, radix = 10) {
return num;
}

function hexToByteSequence(str: string): Buffer {
function hexToByteSequence(str: string): Uint8Array {
if (str.startsWith('0x') || str.startsWith('0X')) {
str = str.slice(2);
}
const numArray: number[] = [];
const numArray = new Uint8Array(str.length / 2);
for (let i = 0; i < str.length; i += 2) {
numArray.push(toNumber(str.slice(i, i + 2), 16));
numArray[i / 2] = Number.parseInt(str.slice(i, i + 2), 16);
}
return Buffer.from(numArray);
return numArray;
}

function byteSequenceToHex(sequence: Buffer, start = 0, end = sequence.length) {
function byteSequenceToHex(sequence: ArrayBuffer, start = 0, end = sequence.byteLength) {
if (end <= start) {
THROW(new Error(`end must be larger than start : start=${start}, end=${end}`));
}
Expand Down

0 comments on commit db8afd6

Please sign in to comment.