Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reader from #1

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
2 changes: 1 addition & 1 deletion dist/ckb-js-toolkit.esm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/ckb-js-toolkit.node.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/ckb-js-toolkit.node.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/ckb-js-toolkit.umd.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import JSBI from "jsbi";
export class Reader {
constructor(reader: string | ArrayBuffer | Reader);
static fromRawString(s: string): Reader;
static isReader(x: unknown): x is Reader;
static from(x: string | ArrayBuffer | Reader | Uint8Array): Reader;

length(): number;
indexAt(i: number): number;
Expand Down
1,151 changes: 567 additions & 584 deletions package-lock.json

Large diffs are not rendered by default.

42 changes: 38 additions & 4 deletions src/reader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
class ArrayBufferReader {
class BaseReader {
constructor() {
/**
* instanceof would be nice here, but when a user use multi version of Reader that may cause problem
* @example
* const { Reader } = require('ckb-js-toolkit'); // [email protected]
* const { readSomething } = require('other-serializer-lib'); // dependent on [email protected]
*
* readSomething() instanceof Reader; // false
*
* @type {boolean}
* @protected
*/
this.__isByteLikeReader__ = true;
}

static isReader(x) {
if (x == null) return false;
if (x instanceof BaseReader) return true;
return x.__isByteLikeReader__ === true;
}
}

class ArrayBufferReader extends BaseReader {
constructor(buffer) {
super();
this.view = new DataView(buffer);
}

Expand Down Expand Up @@ -27,8 +51,9 @@ class ArrayBufferReader {
}
}

class HexStringReader {
class HexStringReader extends BaseReader {
constructor(string) {
super();
this.string = string;
}

Expand All @@ -55,16 +80,17 @@ class HexStringReader {
}
}

export class Reader {
export class Reader extends BaseReader {
constructor(input) {
super();
if (
input instanceof HexStringReader ||
input instanceof ArrayBufferReader
) {
return input;
}
if (typeof input === "string") {
if (!input.startsWith("0x") || input.length % 2 != 0) {
if (!input.startsWith("0x") || input.length % 2 !== 0) {
throw new Error(
"Hex string must start with 0x, and has even numbered length!"
);
Expand All @@ -74,9 +100,17 @@ export class Reader {
if (input instanceof ArrayBuffer) {
return new ArrayBufferReader(input);
}

if (input instanceof Uint8Array) {
return new ArrayBufferReader(Uint8Array.from(input).buffer);
}
throw new Error("Reader can only accept hex string or ArrayBuffer!");
}

static from(x) {
return new Reader(x);
}

static fromRawString(string) {
const buffer = new ArrayBuffer(string.length);
const view = new DataView(buffer);
Expand Down
19 changes: 19 additions & 0 deletions tests/reader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const test = require("ava");
const { Reader } = require("../build/ckb-js-toolkit.node");

test("reader functions", t => {
const ra = Reader.from("0x12345678");
const rb = Reader.from(Uint8Array.from([0x12, 0x34, 0x56, 0x78]));
const rc = Reader.from(Buffer.from("12345678", "hex"));
const rd = Reader.from(Uint8Array.from([0x12, 0x34, 0x56, 0x78]).buffer);

t.is(new DataView(ra.toArrayBuffer()).getUint32(0), 0x12345678);
t.is(new DataView(rb.toArrayBuffer()).getUint32(0), 0x12345678);
t.is(new DataView(rc.toArrayBuffer()).getUint32(0), 0x12345678);
t.is(new DataView(rd.toArrayBuffer()).getUint32(0), 0x12345678);

t.true(Reader.isReader(ra));
t.true(Reader.isReader(rb));
t.true(Reader.isReader(rc));
t.true(Reader.isReader(rd));
});