uint8arraylist 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,2 @@
1
+ MIT: https://www.opensource.org/licenses/mit
2
+ Apache-2.0: https://www.apache.org/licenses/license-2.0
package/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # Uint8ArrayList
2
+
3
+ > Append and consume bytes using only no-copy operations
4
+
5
+ ## Install
6
+
7
+ ```console
8
+ $ npm i uint8arraylist
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```js
14
+ import { Uint8ArrayList } from 'uint8arraylist'
15
+
16
+ const list = new Uint8ArrayList()
17
+ list.append(Uint8Array.from([0, 1, 2]))
18
+ list.append(Uint8Array.from([3, 4, 5]))
19
+
20
+ list.toUint8Array()
21
+ // -> Uint8Array([0, 1, 2, 3, 4, 5])
22
+
23
+ list.consume(3)
24
+ list.toUint8Array()
25
+ // -> Uint8Array([3, 4, 5])
26
+ ```
27
+
28
+ ## Inspiration
29
+
30
+ Borrows liberally from [bl](https://www.npmjs.com/package/bl) but only uses native JS types.
@@ -0,0 +1,13 @@
1
+ export declare class Uint8ArrayList {
2
+ private bufs;
3
+ length: number;
4
+ constructor(data?: Uint8Array);
5
+ append(data: Uint8Array): void;
6
+ get(index: number): number;
7
+ /**
8
+ * Remove bytes from the front of the pool
9
+ */
10
+ consume(bytes: number): void;
11
+ toUint8Array(beginInclusive?: number, endExclusive?: number): Uint8Array;
12
+ }
13
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAEA,qBAAa,cAAc;IACzB,OAAO,CAAC,IAAI,CAAc;IACnB,MAAM,EAAE,MAAM,CAAA;gBAER,IAAI,CAAC,EAAE,UAAU;IAU9B,MAAM,CAAE,IAAI,EAAE,UAAU;IAKxB,GAAG,CAAE,KAAK,EAAE,MAAM;IAoBlB;;OAEG;IACH,OAAO,CAAE,KAAK,EAAE,MAAM;IAsBtB,YAAY,CAAE,cAAc,GAAE,MAAU,EAAE,YAAY,CAAC,EAAE,MAAM;CAkDhE"}
@@ -0,0 +1,91 @@
1
+ import { concat } from 'uint8arrays';
2
+ export class Uint8ArrayList {
3
+ constructor(data) {
4
+ this.bufs = [];
5
+ if (data != null) {
6
+ this.bufs.push(data);
7
+ }
8
+ this.length = this.bufs.reduce((acc, curr) => acc + curr.byteLength, 0);
9
+ }
10
+ append(data) {
11
+ this.bufs.push(data);
12
+ this.length += data.byteLength;
13
+ }
14
+ get(index) {
15
+ if (index == null || index < 0 || index >= this.length) {
16
+ throw new RangeError('index is out of bounds');
17
+ }
18
+ let offset = 0;
19
+ for (const buf of this.bufs) {
20
+ const bufEnd = offset + buf.byteLength;
21
+ if (index < bufEnd) {
22
+ return buf[index - offset];
23
+ }
24
+ offset = bufEnd;
25
+ }
26
+ throw new RangeError('index is out of bounds');
27
+ }
28
+ /**
29
+ * Remove bytes from the front of the pool
30
+ */
31
+ consume(bytes) {
32
+ // first, normalize the argument, in accordance with how Buffer does it
33
+ bytes = Math.trunc(bytes);
34
+ // do nothing if not a positive number
35
+ if (Number.isNaN(bytes) || bytes <= 0) {
36
+ return;
37
+ }
38
+ while (this.bufs.length) {
39
+ if (bytes >= this.bufs[0].byteLength) {
40
+ bytes -= this.bufs[0].byteLength;
41
+ this.length -= this.bufs[0].byteLength;
42
+ this.bufs.shift();
43
+ }
44
+ else {
45
+ this.bufs[0] = this.bufs[0].subarray(bytes);
46
+ this.length -= bytes;
47
+ break;
48
+ }
49
+ }
50
+ }
51
+ toUint8Array(beginInclusive = 0, endExclusive) {
52
+ endExclusive = endExclusive ?? (this.length > 0 ? this.length : 0);
53
+ if (beginInclusive < 0 || endExclusive > this.length) {
54
+ throw new RangeError('index out of bounds');
55
+ }
56
+ let bufs = [];
57
+ let offset = 0;
58
+ for (const buf of this.bufs) {
59
+ const bufStart = offset;
60
+ const bufEnd = bufStart + buf.byteLength;
61
+ const sliceStartInBuf = beginInclusive >= bufStart && beginInclusive < bufEnd;
62
+ const sliceEndsInBuf = endExclusive > bufStart && endExclusive <= bufEnd;
63
+ const bufInSlice = beginInclusive < bufStart && endExclusive >= bufEnd;
64
+ offset = bufEnd;
65
+ let startIndex;
66
+ let endIndex;
67
+ if (sliceStartInBuf) {
68
+ startIndex = beginInclusive - bufStart;
69
+ endIndex = buf.byteLength;
70
+ }
71
+ if (sliceEndsInBuf) {
72
+ endIndex = endExclusive - bufStart;
73
+ if (startIndex == null) {
74
+ startIndex = 0;
75
+ }
76
+ }
77
+ if (bufInSlice) {
78
+ startIndex = 0;
79
+ endIndex = buf.byteLength;
80
+ }
81
+ if (startIndex != null && endIndex != null) {
82
+ bufs.push(buf.subarray(startIndex, endIndex));
83
+ }
84
+ if (sliceEndsInBuf) {
85
+ break;
86
+ }
87
+ }
88
+ return concat(bufs, endExclusive - beginInclusive);
89
+ }
90
+ }
91
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAEpC,MAAM,OAAO,cAAc;IAIzB,YAAa,IAAiB;QAC5B,IAAI,CAAC,IAAI,GAAG,EAAE,CAAA;QAEd,IAAI,IAAI,IAAI,IAAI,EAAE;YAChB,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;SACrB;QAED,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,CAAC,GAAG,GAAG,IAAI,CAAC,UAAU,EAAE,CAAC,CAAC,CAAA;IACzE,CAAC;IAED,MAAM,CAAE,IAAgB;QACtB,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QACpB,IAAI,CAAC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AAU,EAAE;gBACd,UAAU,GAAG,CAAC,CAAA;gBACd,QAAQ,GAAG,GAAG,CAAC,UAAU,CAAA;aAC1B;YAED,IAAI,UAAU,IAAI,IAAI,IAAI,QAAQ,IAAI,IAAI,EAAE;gBAC1C,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,UAAU,EAAE,QAAQ,CAAC,CAAC,CAAA;aAC9C;YAED,IAAI,cAAc,EAAE;gBAClB,MAAK;aACN;SACF;QAED,OAAO,MAAM,CAAC,IAAI,EAAE,YAAY,GAAG,cAAc,CAAC,CAAA;IACpD,CAAC;CACF"}
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "uint8arraylist",
3
+ "version": "0.0.0",
4
+ "description": "Append and consume bytes using only no-copy operations",
5
+ "type": "module",
6
+ "types": "./dist/src/index.d.ts",
7
+ "files": [
8
+ "src",
9
+ "dist/src",
10
+ "!dist/test",
11
+ "!**/*.tsbuildinfo"
12
+ ],
13
+ "exports": {
14
+ ".": {
15
+ "import": "./dist/src/index.js"
16
+ }
17
+ },
18
+ "eslintConfig": {
19
+ "extends": "ipfs",
20
+ "parserOptions": {
21
+ "sourceType": "module"
22
+ }
23
+ },
24
+ "scripts": {
25
+ "lint": "aegir lint",
26
+ "dep-check": "aegir dep-check dist/src/**/*.js dist/test/**/*.js",
27
+ "build": "tsc",
28
+ "pretest": "npm run build",
29
+ "test": "aegir test -f ./dist/test/**/*.js",
30
+ "test:chrome": "npm run test -- -t browser",
31
+ "test:chrome-webworker": "npm run test -- -t webworker",
32
+ "test:firefox": "npm run test -- -t browser -- --browser firefox",
33
+ "test:firefox-webworker": "npm run test -- -t webworker -- --browser firefox",
34
+ "test:node": "npm run test -- -t node --cov",
35
+ "test:electron-main": "npm run test -- -t electron-main",
36
+ "release": "semantic-release"
37
+ },
38
+ "author": "Alex Potsides <alex@achingbrain.net>",
39
+ "license": "Apache-2.0 OR MIT",
40
+ "devDependencies": {
41
+ "aegir": "^36.1.3"
42
+ },
43
+ "dependencies": {
44
+ "uint8arrays": "^3.0.0"
45
+ }
46
+ }
package/src/index.ts ADDED
@@ -0,0 +1,117 @@
1
+ import { concat } from 'uint8arrays'
2
+
3
+ export class Uint8ArrayList {
4
+ private bufs: Uint8Array[]
5
+ public length: number
6
+
7
+ constructor (data?: Uint8Array) {
8
+ this.bufs = []
9
+
10
+ if (data != null) {
11
+ this.bufs.push(data)
12
+ }
13
+
14
+ this.length = this.bufs.reduce((acc, curr) => acc + curr.byteLength, 0)
15
+ }
16
+
17
+ append (data: Uint8Array) {
18
+ this.bufs.push(data)
19
+ this.length += data.byteLength
20
+ }
21
+
22
+ get (index: number) {
23
+ if (index == null || index < 0 || index >= this.length) {
24
+ throw new RangeError('index is out of bounds')
25
+ }
26
+
27
+ let offset = 0
28
+
29
+ for (const buf of this.bufs) {
30
+ const bufEnd = offset + buf.byteLength
31
+
32
+ if (index < bufEnd) {
33
+ return buf[index - offset]
34
+ }
35
+
36
+ offset = bufEnd
37
+ }
38
+
39
+ throw new RangeError('index is out of bounds')
40
+ }
41
+
42
+ /**
43
+ * Remove bytes from the front of the pool
44
+ */
45
+ consume (bytes: number) {
46
+ // first, normalize the argument, in accordance with how Buffer does it
47
+ bytes = Math.trunc(bytes)
48
+
49
+ // do nothing if not a positive number
50
+ if (Number.isNaN(bytes) || bytes <= 0) {
51
+ return
52
+ }
53
+
54
+ while (this.bufs.length) {
55
+ if (bytes >= this.bufs[0].byteLength) {
56
+ bytes -= this.bufs[0].byteLength
57
+ this.length -= this.bufs[0].byteLength
58
+ this.bufs.shift()
59
+ } else {
60
+ this.bufs[0] = this.bufs[0].subarray(bytes)
61
+ this.length -= bytes
62
+ break
63
+ }
64
+ }
65
+ }
66
+
67
+ toUint8Array (beginInclusive: number = 0, endExclusive?: number) {
68
+ endExclusive = endExclusive ?? (this.length > 0 ? this.length : 0)
69
+
70
+ if (beginInclusive < 0 || endExclusive > this.length) {
71
+ throw new RangeError('index out of bounds')
72
+ }
73
+
74
+ let bufs: Uint8Array[] = []
75
+ let offset = 0
76
+
77
+ for (const buf of this.bufs) {
78
+ const bufStart = offset
79
+ const bufEnd = bufStart + buf.byteLength
80
+ const sliceStartInBuf = beginInclusive >= bufStart && beginInclusive < bufEnd
81
+ const sliceEndsInBuf = endExclusive > bufStart && endExclusive <= bufEnd
82
+ const bufInSlice = beginInclusive < bufStart && endExclusive >= bufEnd
83
+ offset = bufEnd
84
+
85
+ let startIndex: number | undefined
86
+ let endIndex: number | undefined
87
+
88
+ if (sliceStartInBuf) {
89
+ startIndex = beginInclusive - bufStart
90
+ endIndex = buf.byteLength
91
+ }
92
+
93
+ if (sliceEndsInBuf) {
94
+ endIndex = endExclusive - bufStart
95
+
96
+ if (startIndex == null) {
97
+ startIndex = 0
98
+ }
99
+ }
100
+
101
+ if (bufInSlice) {
102
+ startIndex = 0
103
+ endIndex = buf.byteLength
104
+ }
105
+
106
+ if (startIndex != null && endIndex != null) {
107
+ bufs.push(buf.subarray(startIndex, endIndex))
108
+ }
109
+
110
+ if (sliceEndsInBuf) {
111
+ break
112
+ }
113
+ }
114
+
115
+ return concat(bufs, endExclusive - beginInclusive)
116
+ }
117
+ }