@gmod/cram 1.5.9 → 1.6.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.
Files changed (258) hide show
  1. package/CHANGELOG.md +90 -0
  2. package/README.md +182 -172
  3. package/dist/craiIndex.d.ts +37 -0
  4. package/dist/craiIndex.js +196 -301
  5. package/dist/craiIndex.js.map +1 -0
  6. package/dist/cram-bundle.js +6 -15
  7. package/dist/cramFile/codecs/_base.d.ts +6 -0
  8. package/dist/cramFile/codecs/_base.js +44 -53
  9. package/dist/cramFile/codecs/_base.js.map +1 -0
  10. package/dist/cramFile/codecs/beta.d.ts +4 -0
  11. package/dist/cramFile/codecs/beta.js +38 -48
  12. package/dist/cramFile/codecs/beta.js.map +1 -0
  13. package/dist/cramFile/codecs/byteArrayLength.d.ts +8 -0
  14. package/dist/cramFile/codecs/byteArrayLength.js +58 -78
  15. package/dist/cramFile/codecs/byteArrayLength.js.map +1 -0
  16. package/dist/cramFile/codecs/byteArrayStop.d.ts +6 -0
  17. package/dist/cramFile/codecs/byteArrayStop.js +62 -76
  18. package/dist/cramFile/codecs/byteArrayStop.js.map +1 -0
  19. package/dist/cramFile/codecs/external.d.ts +7 -0
  20. package/dist/cramFile/codecs/external.js +63 -81
  21. package/dist/cramFile/codecs/external.js.map +1 -0
  22. package/dist/cramFile/codecs/gamma.d.ts +4 -0
  23. package/dist/cramFile/codecs/gamma.js +43 -56
  24. package/dist/cramFile/codecs/gamma.js.map +1 -0
  25. package/dist/cramFile/codecs/huffman.d.ts +17 -0
  26. package/dist/cramFile/codecs/huffman.js +126 -199
  27. package/dist/cramFile/codecs/huffman.js.map +1 -0
  28. package/dist/cramFile/codecs/index.d.ts +2 -0
  29. package/dist/cramFile/codecs/index.js +31 -38
  30. package/dist/cramFile/codecs/index.js.map +1 -0
  31. package/dist/cramFile/codecs/subexp.d.ts +4 -0
  32. package/dist/cramFile/codecs/subexp.js +51 -64
  33. package/dist/cramFile/codecs/subexp.js.map +1 -0
  34. package/dist/cramFile/constants.d.ts +36 -0
  35. package/dist/cramFile/constants.js +52 -50
  36. package/dist/cramFile/constants.js.map +1 -0
  37. package/dist/cramFile/container/compressionScheme.d.ts +23 -0
  38. package/dist/cramFile/container/compressionScheme.js +115 -153
  39. package/dist/cramFile/container/compressionScheme.js.map +1 -0
  40. package/dist/cramFile/container/index.d.ts +13 -0
  41. package/dist/cramFile/container/index.js +169 -283
  42. package/dist/cramFile/container/index.js.map +1 -0
  43. package/dist/cramFile/file.d.ts +63 -0
  44. package/dist/cramFile/file.js +440 -766
  45. package/dist/cramFile/file.js.map +1 -0
  46. package/dist/cramFile/index.d.ts +2 -0
  47. package/dist/cramFile/index.js +7 -4
  48. package/dist/cramFile/index.js.map +1 -0
  49. package/dist/cramFile/record.d.ts +79 -0
  50. package/dist/cramFile/record.js +253 -308
  51. package/dist/cramFile/record.js.map +1 -0
  52. package/dist/cramFile/sectionParsers.d.ts +18 -0
  53. package/dist/cramFile/sectionParsers.js +324 -362
  54. package/dist/cramFile/sectionParsers.js.map +1 -0
  55. package/dist/cramFile/slice/decodeRecord.d.ts +2 -0
  56. package/dist/cramFile/slice/decodeRecord.js +278 -298
  57. package/dist/cramFile/slice/decodeRecord.js.map +1 -0
  58. package/dist/cramFile/slice/index.d.ts +20 -0
  59. package/dist/cramFile/slice/index.js +488 -789
  60. package/dist/cramFile/slice/index.js.map +1 -0
  61. package/dist/cramFile/util.d.ts +5 -0
  62. package/dist/cramFile/util.js +158 -144
  63. package/dist/cramFile/util.js.map +1 -0
  64. package/dist/errors.d.ts +23 -0
  65. package/dist/errors.js +66 -103
  66. package/dist/errors.js.map +1 -0
  67. package/dist/index.d.ts +4 -0
  68. package/dist/index.js +12 -12
  69. package/dist/index.js.map +1 -0
  70. package/dist/indexedCramFile.d.ts +39 -0
  71. package/dist/indexedCramFile.js +213 -315
  72. package/dist/indexedCramFile.js.map +1 -0
  73. package/dist/io/bufferCache.d.ts +12 -0
  74. package/dist/io/bufferCache.js +108 -128
  75. package/dist/io/bufferCache.js.map +1 -0
  76. package/dist/io/index.d.ts +5 -0
  77. package/dist/io/index.js +29 -27
  78. package/dist/io/index.js.map +1 -0
  79. package/dist/io/localFile.d.ts +10 -0
  80. package/dist/io/localFile.js +105 -162
  81. package/dist/io/localFile.js.map +1 -0
  82. package/dist/io/remoteFile.d.ts +16 -0
  83. package/dist/io/remoteFile.js +137 -206
  84. package/dist/io/remoteFile.js.map +1 -0
  85. package/dist/rans/constants.d.ts +3 -0
  86. package/dist/rans/constants.js +6 -6
  87. package/dist/rans/constants.js.map +1 -0
  88. package/dist/rans/d04.d.ts +1 -0
  89. package/dist/rans/d04.js +70 -99
  90. package/dist/rans/d04.js.map +1 -0
  91. package/dist/rans/d14.d.ts +1 -0
  92. package/dist/rans/d14.js +55 -93
  93. package/dist/rans/d14.js.map +1 -0
  94. package/dist/rans/decoding.d.ts +30 -0
  95. package/dist/rans/decoding.js +112 -159
  96. package/dist/rans/decoding.js.map +1 -0
  97. package/dist/rans/frequencies.d.ts +2 -0
  98. package/dist/rans/frequencies.js +110 -119
  99. package/dist/rans/frequencies.js.map +1 -0
  100. package/dist/rans/index.d.ts +1 -0
  101. package/dist/rans/index.js +111 -174
  102. package/dist/rans/index.js.map +1 -0
  103. package/dist/sam.d.ts +1 -0
  104. package/dist/sam.js +16 -41
  105. package/dist/sam.js.map +1 -0
  106. package/dist/unzip-pako.d.ts +2 -0
  107. package/dist/unzip-pako.js +9 -0
  108. package/dist/unzip-pako.js.map +1 -0
  109. package/dist/unzip.d.ts +2 -0
  110. package/dist/unzip.js +6 -0
  111. package/dist/unzip.js.map +1 -0
  112. package/errors.js +66 -103
  113. package/esm/craiIndex.d.ts +37 -0
  114. package/esm/craiIndex.js +158 -0
  115. package/esm/craiIndex.js.map +1 -0
  116. package/esm/cramFile/codecs/_base.d.ts +6 -0
  117. package/esm/cramFile/codecs/_base.js +42 -0
  118. package/esm/cramFile/codecs/_base.js.map +1 -0
  119. package/esm/cramFile/codecs/beta.d.ts +4 -0
  120. package/esm/cramFile/codecs/beta.js +15 -0
  121. package/esm/cramFile/codecs/beta.js.map +1 -0
  122. package/esm/cramFile/codecs/byteArrayLength.d.ts +8 -0
  123. package/esm/cramFile/codecs/byteArrayLength.js +35 -0
  124. package/esm/cramFile/codecs/byteArrayLength.js.map +1 -0
  125. package/esm/cramFile/codecs/byteArrayStop.d.ts +6 -0
  126. package/esm/cramFile/codecs/byteArrayStop.js +40 -0
  127. package/esm/cramFile/codecs/byteArrayStop.js.map +1 -0
  128. package/esm/cramFile/codecs/external.d.ts +7 -0
  129. package/esm/cramFile/codecs/external.js +40 -0
  130. package/esm/cramFile/codecs/external.js.map +1 -0
  131. package/esm/cramFile/codecs/gamma.d.ts +4 -0
  132. package/esm/cramFile/codecs/gamma.js +20 -0
  133. package/esm/cramFile/codecs/gamma.js.map +1 -0
  134. package/esm/cramFile/codecs/huffman.d.ts +17 -0
  135. package/esm/cramFile/codecs/huffman.js +107 -0
  136. package/esm/cramFile/codecs/huffman.js.map +1 -0
  137. package/esm/cramFile/codecs/index.d.ts +2 -0
  138. package/esm/cramFile/codecs/index.js +30 -0
  139. package/esm/cramFile/codecs/index.js.map +1 -0
  140. package/esm/cramFile/codecs/subexp.d.ts +4 -0
  141. package/esm/cramFile/codecs/subexp.js +28 -0
  142. package/esm/cramFile/codecs/subexp.js.map +1 -0
  143. package/esm/cramFile/constants.d.ts +36 -0
  144. package/esm/cramFile/constants.js +51 -0
  145. package/esm/cramFile/constants.js.map +1 -0
  146. package/esm/cramFile/container/compressionScheme.d.ts +23 -0
  147. package/esm/cramFile/container/compressionScheme.js +123 -0
  148. package/esm/cramFile/container/compressionScheme.js.map +1 -0
  149. package/esm/cramFile/container/index.d.ts +13 -0
  150. package/esm/cramFile/container/index.js +84 -0
  151. package/esm/cramFile/container/index.js.map +1 -0
  152. package/esm/cramFile/file.d.ts +63 -0
  153. package/esm/cramFile/file.js +281 -0
  154. package/esm/cramFile/file.js.map +1 -0
  155. package/esm/cramFile/index.d.ts +2 -0
  156. package/esm/cramFile/index.js +3 -0
  157. package/esm/cramFile/index.js.map +1 -0
  158. package/esm/cramFile/record.d.ts +79 -0
  159. package/esm/cramFile/record.js +297 -0
  160. package/esm/cramFile/record.js.map +1 -0
  161. package/esm/cramFile/sectionParsers.d.ts +18 -0
  162. package/esm/cramFile/sectionParsers.js +347 -0
  163. package/esm/cramFile/sectionParsers.js.map +1 -0
  164. package/esm/cramFile/slice/decodeRecord.d.ts +2 -0
  165. package/esm/cramFile/slice/decodeRecord.js +299 -0
  166. package/esm/cramFile/slice/decodeRecord.js.map +1 -0
  167. package/esm/cramFile/slice/index.d.ts +20 -0
  168. package/esm/cramFile/slice/index.js +364 -0
  169. package/esm/cramFile/slice/index.js.map +1 -0
  170. package/esm/cramFile/util.d.ts +5 -0
  171. package/esm/cramFile/util.js +161 -0
  172. package/esm/cramFile/util.js.map +1 -0
  173. package/esm/errors.d.ts +23 -0
  174. package/esm/errors.js +24 -0
  175. package/esm/errors.js.map +1 -0
  176. package/esm/index.d.ts +4 -0
  177. package/esm/index.js +5 -0
  178. package/esm/index.js.map +1 -0
  179. package/esm/indexedCramFile.d.ts +39 -0
  180. package/esm/indexedCramFile.js +155 -0
  181. package/esm/indexedCramFile.js.map +1 -0
  182. package/esm/io/bufferCache.d.ts +12 -0
  183. package/esm/io/bufferCache.js +54 -0
  184. package/esm/io/bufferCache.js.map +1 -0
  185. package/esm/io/index.d.ts +5 -0
  186. package/esm/io/index.js +24 -0
  187. package/esm/io/index.js.map +1 -0
  188. package/esm/io/localFile.d.ts +10 -0
  189. package/esm/io/localFile.js +31 -0
  190. package/esm/io/localFile.js.map +1 -0
  191. package/esm/io/remoteFile.d.ts +16 -0
  192. package/esm/io/remoteFile.js +64 -0
  193. package/esm/io/remoteFile.js.map +1 -0
  194. package/esm/rans/constants.d.ts +3 -0
  195. package/esm/rans/constants.js +5 -0
  196. package/esm/rans/constants.js.map +1 -0
  197. package/esm/rans/d04.d.ts +1 -0
  198. package/esm/rans/d04.js +67 -0
  199. package/esm/rans/d04.js.map +1 -0
  200. package/esm/rans/d14.d.ts +1 -0
  201. package/esm/rans/d14.js +52 -0
  202. package/esm/rans/d14.js.map +1 -0
  203. package/esm/rans/decoding.d.ts +30 -0
  204. package/esm/rans/decoding.js +118 -0
  205. package/esm/rans/decoding.js.map +1 -0
  206. package/esm/rans/frequencies.d.ts +2 -0
  207. package/esm/rans/frequencies.js +110 -0
  208. package/esm/rans/frequencies.js.map +1 -0
  209. package/esm/rans/index.d.ts +1 -0
  210. package/esm/rans/index.js +195 -0
  211. package/esm/rans/index.js.map +1 -0
  212. package/esm/sam.d.ts +1 -0
  213. package/esm/sam.js +16 -0
  214. package/esm/sam.js.map +1 -0
  215. package/esm/unzip-pako.d.ts +2 -0
  216. package/esm/unzip-pako.js +5 -0
  217. package/esm/unzip-pako.js.map +1 -0
  218. package/esm/unzip.d.ts +2 -0
  219. package/esm/unzip.js +3 -0
  220. package/esm/unzip.js.map +1 -0
  221. package/package.json +38 -35
  222. package/src/craiIndex.js +180 -0
  223. package/src/cramFile/codecs/_base.js +49 -0
  224. package/src/cramFile/codecs/beta.js +23 -0
  225. package/src/cramFile/codecs/byteArrayLength.js +55 -0
  226. package/src/cramFile/codecs/byteArrayStop.js +50 -0
  227. package/src/cramFile/codecs/external.js +54 -0
  228. package/src/cramFile/codecs/gamma.js +30 -0
  229. package/src/cramFile/codecs/huffman.js +137 -0
  230. package/src/cramFile/codecs/index.js +38 -0
  231. package/src/cramFile/codecs/subexp.js +32 -0
  232. package/src/cramFile/constants.js +55 -0
  233. package/src/cramFile/container/compressionScheme.js +144 -0
  234. package/src/cramFile/container/index.js +119 -0
  235. package/src/cramFile/file.js +347 -0
  236. package/src/cramFile/index.js +3 -0
  237. package/src/cramFile/record.js +337 -0
  238. package/src/cramFile/sectionParsers.js +379 -0
  239. package/src/cramFile/slice/decodeRecord.js +362 -0
  240. package/src/cramFile/slice/index.js +497 -0
  241. package/src/cramFile/util.js +169 -0
  242. package/src/errors.js +22 -0
  243. package/src/index.js +5 -0
  244. package/src/indexedCramFile.js +191 -0
  245. package/src/io/bufferCache.js +66 -0
  246. package/src/io/index.js +26 -0
  247. package/src/io/localFile.js +35 -0
  248. package/src/io/remoteFile.js +71 -0
  249. package/src/rans/README.md +1 -0
  250. package/src/rans/constants.js +5 -0
  251. package/src/rans/d04.js +83 -0
  252. package/src/rans/d14.js +59 -0
  253. package/src/rans/decoding.js +141 -0
  254. package/src/rans/frequencies.js +121 -0
  255. package/src/rans/index.js +249 -0
  256. package/src/sam.js +15 -0
  257. package/src/unzip-pako.ts +5 -0
  258. package/src/unzip.ts +2 -0
@@ -0,0 +1,121 @@
1
+ import { CramMalformedError } from '../errors'
2
+
3
+ import { TOTFREQ } from './constants'
4
+ import Decoding from './decoding'
5
+
6
+ function assert(result) {
7
+ if (!result) {
8
+ throw new CramMalformedError('assertion failed')
9
+ }
10
+ }
11
+
12
+ export function readStatsO0(
13
+ /* ByteBuffer */ cp,
14
+ /* Decoding.AriDecoder */ decoder,
15
+ /* Decoding.RansDecSymbol[] */ syms,
16
+ ) {
17
+ // Pre-compute reverse lookup of frequency.
18
+ let rle = 0
19
+ let x = 0
20
+ let j = cp.get() & 0xff
21
+ do {
22
+ if (decoder.fc[j] == null) {
23
+ decoder.fc[j] = new Decoding.FC()
24
+ }
25
+ decoder.fc[j].F = cp.get() & 0xff
26
+ if (decoder.fc[j].F >= 128) {
27
+ decoder.fc[j].F &= ~128
28
+ decoder.fc[j].F = ((decoder.fc[j].F & 127) << 8) | (cp.get() & 0xff)
29
+ }
30
+ decoder.fc[j].C = x
31
+
32
+ Decoding.symbolInit(syms[j], decoder.fc[j].C, decoder.fc[j].F)
33
+
34
+ /* Build reverse lookup table */
35
+ if (!decoder.R) {
36
+ decoder.R = new Array(TOTFREQ)
37
+ }
38
+ decoder.R.fill(j, x, x + decoder.fc[j].F)
39
+
40
+ x += decoder.fc[j].F
41
+
42
+ if (rle === 0 && j + 1 === (0xff & cp.getByteAt(cp.position()))) {
43
+ j = cp.get() & 0xff
44
+ rle = cp.get() & 0xff
45
+ } else if (rle !== 0) {
46
+ rle -= 1
47
+ j += 1
48
+ } else {
49
+ j = cp.get() & 0xff
50
+ }
51
+ } while (j !== 0)
52
+
53
+ assert(x < TOTFREQ)
54
+ }
55
+
56
+ export function readStatsO1(
57
+ /* ByteBuffer */ cp,
58
+ /* Decoding.AriDecoder[] */ D,
59
+ /* Decoding.RansDecSymbol[][] */ syms,
60
+ ) {
61
+ let rlei = 0
62
+ let i = 0xff & cp.get()
63
+ do {
64
+ let rlej = 0
65
+ let x = 0
66
+ let j = 0xff & cp.get()
67
+ if (D[i] == null) {
68
+ D[i] = new Decoding.AriDecoder()
69
+ }
70
+ do {
71
+ if (D[i].fc[j] == null) {
72
+ D[i].fc[j] = new Decoding.FC()
73
+ }
74
+ D[i].fc[j].F = 0xff & cp.get()
75
+ if (D[i].fc[j].F >= 128) {
76
+ D[i].fc[j].F &= ~128
77
+ D[i].fc[j].F = ((D[i].fc[j].F & 127) << 8) | (0xff & cp.get())
78
+ }
79
+ D[i].fc[j].C = x
80
+
81
+ if (D[i].fc[j].F === 0) {
82
+ D[i].fc[j].F = TOTFREQ
83
+ }
84
+
85
+ if (syms[i][j] == null) {
86
+ syms[i][j] = new Decoding.RansDecSymbol()
87
+ }
88
+
89
+ Decoding.symbolInit(syms[i][j], D[i].fc[j].C, D[i].fc[j].F)
90
+
91
+ /* Build reverse lookup table */
92
+ if (D[i].R == null) {
93
+ D[i].R = new Array(TOTFREQ)
94
+ }
95
+ D[i].R.fill(j, x, x + D[i].fc[j].F)
96
+
97
+ x += D[i].fc[j].F
98
+ assert(x <= TOTFREQ)
99
+
100
+ if (rlej === 0 && j + 1 === (0xff & cp.getByteAt(cp.position()))) {
101
+ j = 0xff & cp.get()
102
+ rlej = 0xff & cp.get()
103
+ } else if (rlej !== 0) {
104
+ rlej -= 1
105
+ j += 1
106
+ } else {
107
+ j = 0xff & cp.get()
108
+ }
109
+ } while (j !== 0)
110
+
111
+ if (rlei === 0 && i + 1 === (0xff & cp.getByteAt(cp.position()))) {
112
+ i = 0xff & cp.get()
113
+ rlei = 0xff & cp.get()
114
+ } else if (rlei !== 0) {
115
+ rlei -= 1
116
+ i += 1
117
+ } else {
118
+ i = 0xff & cp.get()
119
+ }
120
+ } while (i !== 0)
121
+ }
@@ -0,0 +1,249 @@
1
+ import { CramMalformedError } from '../errors'
2
+
3
+ import Decoding from './decoding'
4
+ import { readStatsO0, readStatsO1 } from './frequencies'
5
+
6
+ import D04 from './d04'
7
+ import D14 from './d14'
8
+
9
+ // const /* int */ ORDER_BYTE_LENGTH = 1
10
+ // const /* int */ COMPRESSED_BYTE_LENGTH = 4
11
+ const /* int */ RAW_BYTE_LENGTH = 4
12
+ // const /* int */ PREFIX_BYTE_LENGTH =
13
+ // ORDER_BYTE_LENGTH + COMPRESSED_BYTE_LENGTH + RAW_BYTE_LENGTH
14
+
15
+ // enum ORDER {
16
+ // ZERO, ONE;
17
+
18
+ // static ORDER fromInt(const /* int */ value) {
19
+ // try {
20
+ // return ORDER.values()[value];
21
+ // } catch (const ArrayIndexOutOfBoundsException e) {
22
+ // throw new RuntimeException("Unknown rANS order: " + value);
23
+ // }
24
+ // }
25
+ // }
26
+
27
+ // static ByteBuffer compress(const ByteBuffer input, const ORDER order, const ByteBuffer out) {
28
+ // if (input.remaining() == 0)
29
+ // return EMPTY_BUFFER;
30
+
31
+ // if (input.remaining() < 4)
32
+ // return encode_order0_way4(input, out);
33
+
34
+ // switch (order) {
35
+ // case ZERO:
36
+ // return encode_order0_way4(input, out);
37
+ // case ONE:
38
+ // return encode_order1_way4(input, out);
39
+
40
+ // default:
41
+ // throw new RuntimeException("Unknown rANS order: " + order);
42
+ // }
43
+ // }
44
+
45
+ // static /* ByteBuffer */ allocateIfNeeded(/* const int */ in_size,
46
+ // /* const ByteBuffer */ out_buf) {
47
+ // const /* int */ compressedSize = (/* int */) (1.05 * in_size + 257 * 257 * 3 + 4);
48
+ // if (out_buf == null)
49
+ // return ByteBuffer.allocate(compressedSize);
50
+ // if (out_buf.remaining() < compressedSize)
51
+ // throw new RuntimeException("Insufficient buffer size.");
52
+ // out_buf.order(ByteOrder.LITTLE_ENDIAN);
53
+ // return out_buf;
54
+ // }
55
+
56
+ // static ByteBuffer encode_order0_way4(const ByteBuffer input,
57
+ // ByteBuffer out_buf) {
58
+ // const /* int */ in_size = input.remaining();
59
+ // out_buf = allocateIfNeeded(in_size, out_buf);
60
+ // const /* int */ freqTableStart = PREFIX_BYTE_LENGTH;
61
+ // out_buf.position(freqTableStart);
62
+
63
+ // const /* int */[] F = Frequencies.calcFrequencies_o0(in);
64
+ // const RansEncSymbol[] syms = Frequencies.buildSyms_o0(F);
65
+
66
+ // const ByteBuffer cp = out_buf.slice();
67
+ // const /* int */ frequencyTable_size = Frequencies.writeFrequencies_o0(cp, F);
68
+
69
+ // input.rewind();
70
+ // const /* int */ compressedBlob_size = E04.compress(input, syms, cp);
71
+
72
+ // finalizeCompressed(0, out_buf, in_size, frequencyTable_size,
73
+ // compressedBlob_size);
74
+ // return out_buf;
75
+ // }
76
+
77
+ // static ByteBuffer encode_order1_way4(const ByteBuffer input,
78
+ // ByteBuffer out_buf) {
79
+ // const /* int */ in_size = input.remaining();
80
+ // out_buf = allocateIfNeeded(in_size, out_buf);
81
+ // const /* int */ freqTableStart = PREFIX_BYTE_LENGTH;
82
+ // out_buf.position(freqTableStart);
83
+
84
+ // const /* int */[][] F = Frequencies.calcFrequencies_o1(in);
85
+ // const RansEncSymbol[][] syms = Frequencies.buildSyms_o1(F);
86
+
87
+ // const ByteBuffer cp = out_buf.slice();
88
+ // const /* int */ frequencyTable_size = Frequencies.writeFrequencies_o1(cp, F);
89
+
90
+ // input.rewind();
91
+ // const /* int */ compressedBlob_size = E14.compress(input, syms, cp);
92
+
93
+ // finalizeCompressed(1, out_buf, in_size, frequencyTable_size,
94
+ // compressedBlob_size);
95
+ // return out_buf;
96
+ // }
97
+
98
+ // static void finalizeCompressed(const /* int */ order, const ByteBuffer out_buf,
99
+ // const /* int */ in_size, const /* int */ frequencyTable_size, const /* int */ compressedBlob_size) {
100
+ // out_buf.limit(PREFIX_BYTE_LENGTH + frequencyTable_size
101
+ // + compressedBlob_size);
102
+ // out_buf.put(0, (byte) order);
103
+ // out_buf.order(ByteOrder.LITTLE_ENDIAN);
104
+ // const /* int */ compressedSizeOffset = ORDER_BYTE_LENGTH;
105
+ // out_buf.putInt(compressedSizeOffset, frequencyTable_size
106
+ // + compressedBlob_size);
107
+ // const /* int */ rawSizeOffset = ORDER_BYTE_LENGTH + COMPRESSED_BYTE_LENGTH;
108
+ // out_buf.putInt(rawSizeOffset, in_size);
109
+ // out_buf.rewind();
110
+ // }
111
+
112
+ function /* static ByteBuffer */ uncompressOrder0Way4(
113
+ /* const ByteBuffer */ input,
114
+ /* const ByteBuffer */ out,
115
+ ) {
116
+ // input.order(ByteOrder.LITTLE_ENDIAN);
117
+ const D = new Decoding.AriDecoder()
118
+ const syms = new Array(256)
119
+ for (let i = 0; i < syms.length; i += 1) {
120
+ syms[i] = new Decoding.Symbol()
121
+ }
122
+
123
+ readStatsO0(input, D, syms)
124
+
125
+ D04(input, D, syms, out)
126
+
127
+ return out
128
+ }
129
+
130
+ function /* static ByteBuffer */ uncompressOrder1Way4(
131
+ /* const ByteBuffer */ input,
132
+ /* const ByteBuffer */ output,
133
+ ) {
134
+ const D = new Array(256)
135
+ for (let i = 0; i < D.length; i += 1) {
136
+ D[i] = new Decoding.AriDecoder()
137
+ }
138
+ const /* Decoding.RansDecSymbol[][] */ syms = new Array(256)
139
+ for (let i = 0; i < syms.length; i += 1) {
140
+ syms[i] = new Array(256)
141
+ for (let j = 0; j < syms[i].length; j += 1) {
142
+ syms[i][j] = new Decoding.Symbol()
143
+ }
144
+ }
145
+ readStatsO1(input, D, syms)
146
+
147
+ D14(input, output, D, syms)
148
+
149
+ return output
150
+ }
151
+
152
+ /* compat layer to make a node buffer act like a java ByteBuffer */
153
+ class ByteBuffer {
154
+ constructor(nodeBuffer, initialInputPosition = 0) {
155
+ this._buffer = nodeBuffer
156
+ this._position = initialInputPosition
157
+ this.length = nodeBuffer.length
158
+ }
159
+
160
+ get() {
161
+ const b = this._buffer[this._position]
162
+ this._position += 1
163
+ return b
164
+ }
165
+
166
+ getByte() {
167
+ return this.get()
168
+ }
169
+
170
+ getByteAt(position) {
171
+ return this._buffer[position]
172
+ }
173
+
174
+ position() {
175
+ return this._position
176
+ }
177
+
178
+ put(val) {
179
+ this._buffer[this._position] = val
180
+ this._position += 1
181
+ return val
182
+ }
183
+
184
+ putAt(position, val) {
185
+ this._buffer[position] = val
186
+ return val
187
+ }
188
+
189
+ setPosition(pos) {
190
+ this._position = pos
191
+ return pos
192
+ }
193
+
194
+ getInt() {
195
+ const i = this._buffer.readInt32LE(this._position)
196
+ this._position += 4
197
+ return i
198
+ }
199
+
200
+ remaining() {
201
+ return this._buffer.length - this._position
202
+ }
203
+ }
204
+
205
+ // static /* const */ ByteBuffer EMPTY_BUFFER = ByteBuffer.allocate(0);
206
+ export default function uncompress(
207
+ inputBuffer,
208
+ outputBuffer,
209
+ initialInputPosition = 0,
210
+ ) {
211
+ if (inputBuffer.length === 0) {
212
+ outputBuffer.fill(0)
213
+ return outputBuffer
214
+ }
215
+
216
+ const input = new ByteBuffer(inputBuffer, initialInputPosition)
217
+ // input.order(ByteOrder.LITTLE_ENDIAN);
218
+
219
+ const order = input.get()
220
+ if (order !== 0 && order !== 1) {
221
+ throw new CramMalformedError(`Invalid rANS order ${order}`)
222
+ }
223
+
224
+ const /* int */ inputSize = input.getInt()
225
+ if (inputSize !== input.remaining() - RAW_BYTE_LENGTH) {
226
+ throw new CramMalformedError('Incorrect input length.')
227
+ }
228
+
229
+ const /* int */ outputSize = input.getInt()
230
+ const output = new ByteBuffer(outputBuffer || Buffer.allocUnsafe(outputSize))
231
+ // TODO output.limit(outputSize)
232
+
233
+ if (output.length < outputSize) {
234
+ throw new CramMalformedError(
235
+ `Output buffer too small to fit ${outputSize} bytes.`,
236
+ )
237
+ }
238
+
239
+ switch (order) {
240
+ case 0:
241
+ return uncompressOrder0Way4(input, output)
242
+
243
+ case 1:
244
+ return uncompressOrder1Way4(input, output)
245
+
246
+ default:
247
+ throw new CramMalformedError(`Invalid rANS order: ${order}`)
248
+ }
249
+ }
package/src/sam.js ADDED
@@ -0,0 +1,15 @@
1
+ export function parseHeaderText(text) {
2
+ const lines = text.split(/\r?\n/)
3
+ const data = []
4
+ lines.forEach(line => {
5
+ const [tag, ...fields] = line.split(/\t/)
6
+ const parsedFields = fields.map(f => {
7
+ const [fieldTag, value] = f.split(':', 2)
8
+ return { tag: fieldTag, value }
9
+ })
10
+ if (tag) {
11
+ data.push({ tag: tag.substr(1), data: parsedFields })
12
+ }
13
+ })
14
+ return data
15
+ }
@@ -0,0 +1,5 @@
1
+ import { inflate } from 'pako'
2
+
3
+ export function unzip(input: Buffer) {
4
+ return Buffer.from(inflate(input))
5
+ }
package/src/unzip.ts ADDED
@@ -0,0 +1,2 @@
1
+ import { gunzipSync } from 'zlib'
2
+ export { gunzipSync as unzip }