@nxtedition/slice 1.1.0 → 1.1.2

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/lib/index.d.ts CHANGED
@@ -1,4 +1,9 @@
1
1
  import util from 'node:util';
2
+ export type SliceLike = {
3
+ buffer: Buffer;
4
+ byteOffset: number;
5
+ byteLength: number;
6
+ };
2
7
  export declare class Slice {
3
8
  buffer: Buffer;
4
9
  byteOffset: number;
package/lib/index.js CHANGED
@@ -2,6 +2,12 @@ import util from 'node:util'
2
2
 
3
3
  const EMPTY_BUF = Buffer.alloc(0)
4
4
 
5
+
6
+
7
+
8
+
9
+
10
+
5
11
  export class Slice {
6
12
  buffer = EMPTY_BUF
7
13
  byteOffset = 0
@@ -36,6 +42,12 @@ export class Slice {
36
42
  throw new RangeError(`Invalid maxByteLength: ${maxByteLength}`)
37
43
  }
38
44
 
45
+ if (byteOffset + byteLength > buffer.byteLength) {
46
+ throw new RangeError(
47
+ `byteOffset + byteLength (${byteOffset + byteLength}) exceeds buffer size (${buffer.byteLength})`,
48
+ )
49
+ }
50
+
39
51
  this.buffer = buffer
40
52
  this.byteOffset = byteOffset
41
53
  this.byteLength = byteLength
@@ -59,16 +71,6 @@ export class Slice {
59
71
  sourceStart ,
60
72
  sourceEnd ,
61
73
  ) {
62
- if (target instanceof Slice) {
63
- if (targetStart === undefined) {
64
- targetStart = target.byteOffset
65
- } else {
66
- targetStart += target.byteOffset
67
- }
68
-
69
- target = target.buffer
70
- }
71
-
72
74
  if (sourceStart === undefined) {
73
75
  sourceStart = this.byteOffset
74
76
  } else {
@@ -81,6 +83,20 @@ export class Slice {
81
83
  sourceEnd += this.byteOffset
82
84
  }
83
85
 
86
+ let targetEnd
87
+
88
+ if (target instanceof Slice) {
89
+ targetEnd = target.byteOffset + target.byteLength
90
+ if (targetStart === undefined) {
91
+ targetStart = target.byteOffset
92
+ } else {
93
+ targetStart += target.byteOffset
94
+ }
95
+
96
+ target = target.buffer
97
+ sourceEnd = Math.min(sourceEnd, sourceStart + (targetEnd - targetStart))
98
+ }
99
+
84
100
  if (
85
101
  sourceStart === targetStart &&
86
102
  this.buffer.buffer === target.buffer &&
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/slice",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "type": "module",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -30,5 +30,5 @@
30
30
  "tsd": "^0.33.0",
31
31
  "typescript": "^5.9.3"
32
32
  },
33
- "gitHead": "80358629a4abe8d9065ac1d1b92ef378493c2dcb"
33
+ "gitHead": "46b274a6aae8f201e61655dcd8cd8d3ce24af85a"
34
34
  }