Skip to content

Commit

Permalink
chore: collapse getStream into openStream
Browse files Browse the repository at this point in the history
  • Loading branch information
fallenoak committed Nov 25, 2023
1 parent 24c91ce commit 856b208
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 27 deletions.
4 changes: 3 additions & 1 deletion src/lib/stream/util.mts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ const openStream = (
source: IoSource,
endianness = Endianness.Little,
): IoStream => {
if (typeof source === 'string' || typeof source === 'number') {
if (isStream(source)) {
return source as IoStream;
} else if (typeof source === 'string' || typeof source === 'number') {
return new FsStream(source, endianness);
} else if (ArrayBuffer.isView(source)) {
return new ArrayBufferStream(
Expand Down
7 changes: 4 additions & 3 deletions src/lib/type/ArrayIo.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getStream, validateType } from '../util.mjs';
import { validateType } from '../util.mjs';
import { openStream } from '../stream/util.mjs';

type ArrayOptions = {
size?: number;
Expand Down Expand Up @@ -30,7 +31,7 @@ class ArrayIo implements IoType {
}

read(source: IoSource, context: IoContext = {}) {
const stream = getStream(source);
const stream = openStream(source);
const value = [];
const size = this.#options.size;

Expand All @@ -51,7 +52,7 @@ class ArrayIo implements IoType {
}

write(source: IoSource, value: any[], context: IoContext = {}) {
const stream = getStream(source);
const stream = openStream(source);
const size = value.length;
const type = this.#type;

Expand Down
7 changes: 4 additions & 3 deletions src/lib/type/StringIo.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getStream, resolveValue } from '../util.mjs';
import { resolveValue } from '../util.mjs';
import { openStream } from '../stream/util.mjs';

const STRING_TERMINATOR = 0x00;
const DEFAULT_ENCODING = 'utf-8';
Expand Down Expand Up @@ -47,7 +48,7 @@ class StringIo implements IoType {
}

read(source: IoSource, context: IoContext = {}) {
const stream = getStream(source);
const stream = openStream(source);
const size = resolveValue(this.#options.size, context.local, context.root);

const rawBytes = this.#readRawBytes(stream, size);
Expand All @@ -57,7 +58,7 @@ class StringIo implements IoType {
}

write(source: IoSource, value: string, context: IoContext = {}) {
const stream = getStream(source);
const stream = openStream(source);

const finalValue = this.#options.reverse
? value.split('').reverse().join('')
Expand Down
7 changes: 4 additions & 3 deletions src/lib/type/StructIo.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Endianness, getStream, validateType } from '../util.mjs';
import { Endianness, validateType } from '../util.mjs';
import { openStream } from '../stream/util.mjs';

type StructFields = Record<string, IoType>;

Expand Down Expand Up @@ -42,7 +43,7 @@ class StructIo implements IoType {
}

read(source: IoSource, context: IoContext = {}) {
const stream = getStream(source, this.#options.endianness);
const stream = openStream(source, this.#options.endianness);
const value = {};

context.local = value;
Expand All @@ -56,7 +57,7 @@ class StructIo implements IoType {
}

write(source: IoSource, value: object, context: IoContext = {}) {
const stream = getStream(source, this.#options.endianness);
const stream = openStream(source, this.#options.endianness);

context.local = value;
context.root = context.root ?? value;
Expand Down
9 changes: 5 additions & 4 deletions src/lib/type/TlvIo.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Endianness, getStream } from '../util.mjs';
import { Endianness } from '../util.mjs';
import { openStream } from '../stream/util.mjs';

type TlvValueCallback = (
type: string | number,
Expand Down Expand Up @@ -56,7 +57,7 @@ class TlvIo implements IoType {
}

read(source: IoSource, context: IoContext = {}): Tlv {
const stream = getStream(source, this.#options.endianness);
const stream = openStream(source, this.#options.endianness);

context.local = null;
context.root = context.root ?? null;
Expand All @@ -69,7 +70,7 @@ class TlvIo implements IoType {

let valueValue = valueBytes;
if (valueType && valueType.read) {
const valueStream = getStream(valueBytes, this.#options.endianness);
const valueStream = openStream(valueBytes, this.#options.endianness);
valueValue = valueType.read(valueStream, context);
}

Expand All @@ -81,7 +82,7 @@ class TlvIo implements IoType {
}

write(source: IoSource, value: Tlv, context: IoContext = {}) {
const stream = getStream(source, this.#options.endianness);
const stream = openStream(source, this.#options.endianness);

context.local = null;
context.root = context.root ?? null;
Expand Down
10 changes: 1 addition & 9 deletions src/lib/util.mts
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import { isStream, openStream } from './stream/util.mjs';

enum Endianness {
Little = 1,
Big = 2,
}

const getStream = (
source: IoSource,
endianness = Endianness.Little,
): IoStream =>
isStream(source) ? (source as IoStream) : openStream(source, endianness);

const validateType = (type: IoType) => {
if (typeof type.getSize !== 'function') {
throw new Error('Missing required function: getSize');
Expand All @@ -34,4 +26,4 @@ const resolveValue = (ref: number | string, ...objects: object[]) => {
}
};

export { Endianness, getStream, validateType, resolveValue };
export { Endianness, validateType, resolveValue };
8 changes: 4 additions & 4 deletions src/spec/util.spec.mts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { describe, expect, test } from 'vitest';
import { getStream } from '../lib/util.mts';
import FsStream from '../lib/stream/FsStream.mts';
import ArrayBufferStream from '../lib/stream/ArrayBufferStream.mts';
import { openStream } from '../lib/stream/util.mjs';

describe('getStream', () => {
test('returns FsStream object when given path to file', () => {
const stream = getStream('./fixture/fixture.bmp');
const stream = openStream('./fixture/fixture.bmp');
expect(stream).toBeInstanceOf(FsStream);
});

test('returns ArrayBufferStream object when given Uint8Array', () => {
const view = new Uint8Array(10);
const stream = getStream(view);
const stream = openStream(view);
expect(stream).toBeInstanceOf(ArrayBufferStream);
});

test('returns ArrayBufferStream object when given ArrayBuffer', () => {
const buffer = new ArrayBuffer(10);
const stream = getStream(buffer);
const stream = openStream(buffer);
expect(stream).toBeInstanceOf(ArrayBufferStream);
});
});

0 comments on commit 856b208

Please sign in to comment.