chubakabra 0.2.0 → 0.3.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/node/index.js +43 -25
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chubakabra",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "description": "Browser and Node.js utilities for working with key-addressable byte ranges in large binary files",
6
6
  "exports": {
package/src/node/index.js CHANGED
@@ -7,17 +7,22 @@ const RANGE_SIZE = 16
7
7
  export function readKvrHeader(path)
8
8
  {
9
9
  const fd = fs.openSync(path, "r")
10
- const b = Buffer.alloc(HEADER_SIZE)
11
- fs.readSync(fd, b, 0, HEADER_SIZE, 0)
10
+ const buf = Buffer.alloc(HEADER_SIZE)
11
+ fs.readSync(fd, buf, 0, HEADER_SIZE, 0)
12
12
  fs.closeSync(fd)
13
13
 
14
- if (b.toString("ascii", 0, 3) !== MAGIC || b[3] !== 1)
14
+ if (
15
+ buf[0] !== 0x4B || // K
16
+ buf[1] !== 0x56 || // V
17
+ buf[2] !== 0x52 || // R
18
+ buf[3] !== 1
19
+ )
15
20
  throw new Error("Invalid KVR file")
16
21
 
17
22
  return {
18
- keySize: b.readUInt16LE(4),
19
- indexCount: b.readUInt32LE(6),
20
- indexOffset: Number(b.readBigUInt64LE(10))
23
+ keySize: buf.readUInt16LE(4),
24
+ indexCount: buf.readUInt32LE(6),
25
+ indexOffset: Number(buf.readBigUInt64LE(10))
21
26
  }
22
27
  }
23
28
 
@@ -37,6 +42,7 @@ export function readKvrIndex(path)
37
42
  for (let i = 0; i < h.indexCount; i++)
38
43
  {
39
44
  const o = i * entrySize
45
+
40
46
  entries.push({
41
47
  key: buf.subarray(o, o + h.keySize),
42
48
  range: [
@@ -46,7 +52,10 @@ export function readKvrIndex(path)
46
52
  })
47
53
  }
48
54
 
49
- return { keySize: h.keySize, entries }
55
+ return {
56
+ keySize: h.keySize,
57
+ entries
58
+ }
50
59
  }
51
60
 
52
61
  export function readKvrRange(path, range)
@@ -61,11 +70,23 @@ export function readKvrRange(path, range)
61
70
 
62
71
  export function writeKvr(path, entries, codec)
63
72
  {
73
+ if (entries.length === 0)
74
+ throw new Error("Empty entries")
75
+
64
76
  const keySize = entries[0].key.length * codec.size
77
+
78
+ for (const e of entries)
79
+ {
80
+ if (e.key.length * codec.size !== keySize)
81
+ throw new Error("Inconsistent key size")
82
+ }
83
+
65
84
  const fd = fs.openSync(path, "w")
66
85
 
67
86
  const header = Buffer.alloc(HEADER_SIZE)
68
- header.write(MAGIC, 0)
87
+ header[0] = 0x4B
88
+ header[1] = 0x56
89
+ header[2] = 0x52
69
90
  header[3] = 1
70
91
  header.writeUInt16LE(keySize, 4)
71
92
  header.writeUInt32LE(entries.length, 6)
@@ -76,7 +97,6 @@ export function writeKvr(path, entries, codec)
76
97
  let offset = HEADER_SIZE
77
98
  const index = []
78
99
 
79
- // DATA
80
100
  for (const e of entries)
81
101
  {
82
102
  index.push({
@@ -89,21 +109,19 @@ export function writeKvr(path, entries, codec)
89
109
  offset += e.data.length
90
110
  }
91
111
 
92
- // INDEX
93
112
  const indexOffset = offset
94
113
 
95
114
  for (const i of index)
96
115
  {
97
- const b = Buffer.alloc(keySize + RANGE_SIZE)
98
- i.key.copy(b, 0)
99
- b.writeBigUInt64LE(BigInt(i.from), keySize)
100
- b.writeBigUInt64LE(BigInt(i.to), keySize + 8)
116
+ const buf = Buffer.alloc(keySize + RANGE_SIZE)
117
+ i.key.copy(buf, 0)
118
+ buf.writeBigUInt64LE(BigInt(i.from), keySize)
119
+ buf.writeBigUInt64LE(BigInt(i.to), keySize + 8)
101
120
 
102
- fs.writeSync(fd, b, 0, b.length, offset)
103
- offset += b.length
121
+ fs.writeSync(fd, buf, 0, buf.length, offset)
122
+ offset += buf.length
104
123
  }
105
124
 
106
- // PATCH header
107
125
  header.writeBigUInt64LE(BigInt(indexOffset), 10)
108
126
  fs.writeSync(fd, header, 0, HEADER_SIZE, 0)
109
127
 
@@ -112,19 +130,19 @@ export function writeKvr(path, entries, codec)
112
130
 
113
131
  export function packKey(arr, codec)
114
132
  {
115
- const b = Buffer.alloc(arr.length * codec.size)
133
+ const buf = Buffer.alloc(arr.length * codec.size)
116
134
  for (let i = 0; i < arr.length; i++)
117
- codec.write(b, arr[i], i * codec.size)
118
- return b
135
+ codec.write(buf, arr[i], i * codec.size)
136
+ return buf
119
137
  }
120
138
 
121
139
  export function unpackKey(buf, codec)
122
140
  {
123
- const n = buf.length / codec.size
124
- const a = new Array(n)
125
- for (let i = 0; i < n; i++)
126
- a[i] = codec.read(buf, i * codec.size)
127
- return a
141
+ const count = buf.length / codec.size
142
+ const arr = new Array(count)
143
+ for (let i = 0; i < count; i++)
144
+ arr[i] = codec.read(buf, i * codec.size)
145
+ return arr
128
146
  }
129
147
 
130
148
  export function createElementCodec(type)