@futpib/parser 1.0.2 → 1.0.3

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 (284) hide show
  1. package/.github/copilot-instructions.md +149 -0
  2. package/.github/workflows/copilot-setup-steps.yml +18 -0
  3. package/.github/workflows/main.yml +29 -8
  4. package/.yarn/releases/yarn-4.9.4.cjs +942 -0
  5. package/.yarnrc.yml +1 -1
  6. package/build/allSettledStream.js +1 -1
  7. package/build/allSettledStream.test.js +2 -2
  8. package/build/androidPackageParser.d.ts +1 -1
  9. package/build/androidPackageParser.js +5 -3
  10. package/build/androidPackageParser.test.js +7 -7
  11. package/build/androidPackageUnparser.d.ts +2 -2
  12. package/build/androidPackageUnparser.js +18 -14
  13. package/build/androidPackageUnparser.test.js +7 -7
  14. package/build/arbitrarilySlicedAsyncInterator.js +2 -1
  15. package/build/arbitraryDalvikBytecode.d.ts +4 -0
  16. package/build/arbitraryDalvikBytecode.js +640 -0
  17. package/build/arbitraryDalvikExecutable.d.ts +3 -0
  18. package/build/arbitraryDalvikExecutable.js +282 -0
  19. package/build/arbitraryDosDateTime.js +1 -0
  20. package/build/arbitraryZipStream.js +1 -1
  21. package/build/arrayParser.js +2 -2
  22. package/build/arrayUnparser.d.ts +1 -1
  23. package/build/backsmali.d.ts +3 -1
  24. package/build/backsmali.js +31 -3
  25. package/build/customInvariant.d.ts +2 -1
  26. package/build/customInvariant.js +4 -6
  27. package/build/dalvikBytecodeParser/formatParsers.d.ts +76 -2
  28. package/build/dalvikBytecodeParser/formatParsers.js +146 -11
  29. package/build/dalvikBytecodeParser/formatSizes.d.ts +34 -0
  30. package/build/dalvikBytecodeParser/formatSizes.js +34 -0
  31. package/build/dalvikBytecodeParser/operationFormats.d.ts +225 -0
  32. package/build/dalvikBytecodeParser/operationFormats.js +225 -0
  33. package/build/dalvikBytecodeParser.d.ts +1105 -5
  34. package/build/dalvikBytecodeParser.js +658 -205
  35. package/build/dalvikBytecodeUnparser/formatUnparsers.d.ts +152 -0
  36. package/build/dalvikBytecodeUnparser/formatUnparsers.js +225 -0
  37. package/build/dalvikBytecodeUnparser.d.ts +3 -0
  38. package/build/dalvikBytecodeUnparser.js +642 -0
  39. package/build/dalvikBytecodeUnparser.test.d.ts +1 -0
  40. package/build/dalvikBytecodeUnparser.test.js +25 -0
  41. package/build/dalvikExecutable.d.ts +65 -8
  42. package/build/dalvikExecutable.js +36 -0
  43. package/build/dalvikExecutableParser/stringSyntaxParser.d.ts +1 -1
  44. package/build/dalvikExecutableParser/stringSyntaxParser.js +17 -17
  45. package/build/dalvikExecutableParser/typeParsers.d.ts +2 -1
  46. package/build/dalvikExecutableParser/typeParsers.js +16 -11
  47. package/build/dalvikExecutableParser/typedNumbers.d.ts +85 -69
  48. package/build/dalvikExecutableParser/typedNumbers.js +0 -1
  49. package/build/dalvikExecutableParser.d.ts +2 -2
  50. package/build/dalvikExecutableParser.js +655 -337
  51. package/build/dalvikExecutableParser.test.js +24 -22
  52. package/build/dalvikExecutableParserAgainstSmaliParser.test.js +223 -246
  53. package/build/dalvikExecutableUnparser/annotationUnparsers.d.ts +14 -0
  54. package/build/dalvikExecutableUnparser/annotationUnparsers.js +97 -0
  55. package/build/dalvikExecutableUnparser/poolBuilders.d.ts +49 -0
  56. package/build/dalvikExecutableUnparser/poolBuilders.js +140 -0
  57. package/build/dalvikExecutableUnparser/poolScanners.d.ts +4 -0
  58. package/build/dalvikExecutableUnparser/poolScanners.js +220 -0
  59. package/build/dalvikExecutableUnparser/sectionUnparsers.d.ts +25 -0
  60. package/build/dalvikExecutableUnparser/sectionUnparsers.js +581 -0
  61. package/build/dalvikExecutableUnparser/utils.d.ts +10 -0
  62. package/build/dalvikExecutableUnparser/utils.js +108 -0
  63. package/build/dalvikExecutableUnparser.d.ts +4 -0
  64. package/build/dalvikExecutableUnparser.js +406 -0
  65. package/build/dalvikExecutableUnparser.test.d.ts +1 -0
  66. package/build/dalvikExecutableUnparser.test.js +31 -0
  67. package/build/debugLogInputParser.js +1 -1
  68. package/build/disjunctionParser.d.ts +2 -2
  69. package/build/disjunctionParser.js +2 -2
  70. package/build/elementTerminatedArrayParser.d.ts +2 -2
  71. package/build/elementTerminatedArrayParser.js +1 -1
  72. package/build/elementTerminatedArrayParser.test.js +5 -5
  73. package/build/elementTerminatedSequenceArrayParser.d.ts +2 -2
  74. package/build/elementTerminatedSequenceArrayParser.js +1 -1
  75. package/build/elementTerminatedSequenceArrayParser.test.js +2 -2
  76. package/build/elementTerminatedSequenceParser.d.ts +2 -2
  77. package/build/elementTerminatedSequenceParser.js +1 -1
  78. package/build/elementTerminatedSequenceParser.test.js +2 -2
  79. package/build/endOfInputParser.d.ts +1 -1
  80. package/build/exactElementSwitchParser.d.ts +3 -0
  81. package/build/exactElementSwitchParser.js +22 -0
  82. package/build/fetchCid.js +2 -6
  83. package/build/fetchCid.test.d.ts +1 -0
  84. package/build/fetchCid.test.js +16 -0
  85. package/build/fixedLengthSequenceParser.test.js +2 -2
  86. package/build/hasExecutable.js +2 -2
  87. package/build/highResolutionTimer.js +1 -1
  88. package/build/inputReader.d.ts +1 -1
  89. package/build/inputReader.test.js +33 -45
  90. package/build/javaKeyStoreParser.test.js +6 -6
  91. package/build/jsonParser.js +8 -8
  92. package/build/lazyMessageError.d.ts +48 -0
  93. package/build/lazyMessageError.js +53 -0
  94. package/build/lazyMessageError.test.d.ts +1 -0
  95. package/build/lazyMessageError.test.js +15 -0
  96. package/build/leb128Parser.d.ts +1 -1
  97. package/build/leb128Parser.js +10 -10
  98. package/build/leb128Parser.test.js +7 -7
  99. package/build/negativeLookaheadParser.js +2 -2
  100. package/build/negativeLookaheadParser.test.js +4 -4
  101. package/build/noStackCaptureOverheadError.d.ts +4 -0
  102. package/build/noStackCaptureOverheadError.js +9 -0
  103. package/build/noStackCaptureOverheadError.test.d.ts +1 -0
  104. package/build/noStackCaptureOverheadError.test.js +15 -0
  105. package/build/nonEmptyArrayParser.js +2 -2
  106. package/build/nonEmptyArrayParser.test.js +2 -1
  107. package/build/optionalParser.js +2 -2
  108. package/build/parser.d.ts +2 -1
  109. package/build/parser.js +23 -8
  110. package/build/parser.test.js +78 -29
  111. package/build/parserConsumedSequenceParser.d.ts +1 -1
  112. package/build/parserConsumedSequenceParser.js +2 -2
  113. package/build/parserContext.d.ts +8 -6
  114. package/build/parserContext.js +60 -33
  115. package/build/parserContext.test.js +7 -3
  116. package/build/parserError.d.ts +603 -44
  117. package/build/parserError.js +98 -53
  118. package/build/parserImplementationInvariant.d.ts +1 -1
  119. package/build/parserImplementationInvariant.js +2 -2
  120. package/build/parserInputCompanion.js +2 -2
  121. package/build/promiseCompose.js +1 -2
  122. package/build/separatedArrayParser.js +2 -2
  123. package/build/separatedNonEmptyArrayParser.d.ts +2 -0
  124. package/build/separatedNonEmptyArrayParser.js +40 -0
  125. package/build/separatedNonEmptyArrayParser.test.d.ts +1 -0
  126. package/build/separatedNonEmptyArrayParser.test.js +66 -0
  127. package/build/sequenceBuffer.js +1 -1
  128. package/build/sequenceTerminatedSequenceParser.d.ts +2 -2
  129. package/build/sequenceTerminatedSequenceParser.js +3 -3
  130. package/build/sequenceTerminatedSequenceParser.test.js +1 -1
  131. package/build/sequenceUnparser.d.ts +1 -1
  132. package/build/skipToParser.d.ts +1 -1
  133. package/build/skipToParser.js +2 -2
  134. package/build/sliceBoundedParser.test.js +4 -9
  135. package/build/smali.d.ts +1 -1
  136. package/build/smali.js +6 -2
  137. package/build/smaliParser.d.ts +62 -6
  138. package/build/smaliParser.js +1721 -296
  139. package/build/smaliParser.test.js +338 -43
  140. package/build/stringFromAsyncIterable.d.ts +1 -0
  141. package/build/stringFromAsyncIterable.js +7 -0
  142. package/build/terminatedArrayParser.js +4 -4
  143. package/build/terminatedArrayParser.test.js +7 -7
  144. package/build/toAsyncIterator.js +4 -4
  145. package/build/unionParser.d.ts +1 -1
  146. package/build/unionParser.js +2 -2
  147. package/build/unionParser.test.js +3 -3
  148. package/build/unparser.d.ts +3 -3
  149. package/build/unparser.js +6 -4
  150. package/build/unparser.test.js +7 -19
  151. package/build/unparserContext.d.ts +2 -2
  152. package/build/unparserContext.js +2 -3
  153. package/build/unparserError.d.ts +2 -1
  154. package/build/unparserError.js +2 -1
  155. package/build/unparserImplementationInvariant.d.ts +1 -1
  156. package/build/unparserOutputCompanion.d.ts +1 -1
  157. package/build/unparserOutputCompanion.js +1 -1
  158. package/build/zipParser.js +1 -1
  159. package/build/zipUnparser.d.ts +3 -3
  160. package/build/zipUnparser.js +9 -19
  161. package/build/zipUnparser.test.js +1 -1
  162. package/package.json +19 -26
  163. package/src/allSettledStream.test.ts +2 -2
  164. package/src/allSettledStream.ts +3 -3
  165. package/src/androidPackageParser.test.ts +17 -19
  166. package/src/androidPackageParser.ts +129 -171
  167. package/src/androidPackageUnparser.test.ts +19 -21
  168. package/src/androidPackageUnparser.ts +23 -17
  169. package/src/arbitrarilySlicedAsyncInterable.ts +1 -1
  170. package/src/arbitrarilySlicedAsyncInterator.ts +4 -4
  171. package/src/arbitraryDalvikBytecode.ts +992 -0
  172. package/src/arbitraryDalvikExecutable.ts +434 -0
  173. package/src/arbitraryDosDateTime.ts +1 -0
  174. package/src/arbitraryZipStream.ts +1 -1
  175. package/src/arrayParser.ts +2 -2
  176. package/src/arrayUnparser.ts +2 -2
  177. package/src/backsmali.ts +48 -4
  178. package/src/bsonParser.test.ts +12 -14
  179. package/src/customInvariant.ts +8 -12
  180. package/src/dalvikBytecodeParser/formatParsers.ts +376 -17
  181. package/src/dalvikBytecodeParser/formatSizes.ts +35 -0
  182. package/src/dalvikBytecodeParser/operationFormats.ts +226 -0
  183. package/src/dalvikBytecodeParser.ts +1042 -243
  184. package/src/dalvikBytecodeUnparser/formatUnparsers.ts +442 -0
  185. package/src/dalvikBytecodeUnparser.test.ts +44 -0
  186. package/src/dalvikBytecodeUnparser.ts +758 -0
  187. package/src/dalvikExecutable.ts +110 -48
  188. package/src/dalvikExecutableParser/stringSyntaxParser.ts +33 -33
  189. package/src/dalvikExecutableParser/typeParsers.ts +23 -14
  190. package/src/dalvikExecutableParser/typedNumbers.ts +19 -19
  191. package/src/dalvikExecutableParser.test.ts +60 -60
  192. package/src/dalvikExecutableParser.test.ts.md +6 -6
  193. package/src/dalvikExecutableParser.test.ts.snap +0 -0
  194. package/src/dalvikExecutableParser.ts +911 -434
  195. package/src/dalvikExecutableParserAgainstSmaliParser.test.ts +256 -239
  196. package/src/dalvikExecutableUnparser/annotationUnparsers.ts +135 -0
  197. package/src/dalvikExecutableUnparser/poolBuilders.ts +189 -0
  198. package/src/dalvikExecutableUnparser/poolScanners.ts +297 -0
  199. package/src/dalvikExecutableUnparser/sectionUnparsers.ts +683 -0
  200. package/src/dalvikExecutableUnparser/utils.ts +149 -0
  201. package/src/dalvikExecutableUnparser.test.ts +57 -0
  202. package/src/dalvikExecutableUnparser.ts +581 -0
  203. package/src/debugLogInputParser.ts +1 -1
  204. package/src/disjunctionParser.ts +5 -5
  205. package/src/elementTerminatedArrayParser.test.ts +8 -8
  206. package/src/elementTerminatedArrayParser.ts +2 -2
  207. package/src/elementTerminatedSequenceArrayParser.test.ts +4 -6
  208. package/src/elementTerminatedSequenceArrayParser.ts +2 -2
  209. package/src/elementTerminatedSequenceParser.test.ts +4 -6
  210. package/src/elementTerminatedSequenceParser.ts +2 -2
  211. package/src/endOfInputParser.ts +1 -1
  212. package/src/exactElementSwitchParser.ts +41 -0
  213. package/src/fetchCid.test.ts +20 -0
  214. package/src/fetchCid.ts +3 -7
  215. package/src/fixedLengthSequenceParser.test.ts +10 -12
  216. package/src/hasExecutable.ts +2 -2
  217. package/src/highResolutionTimer.ts +1 -1
  218. package/src/inputReader.test.ts +39 -52
  219. package/src/inputReader.ts +2 -4
  220. package/src/inputReaderState.ts +1 -1
  221. package/src/inspect.ts +1 -1
  222. package/src/javaKeyStoreParser.test.ts +12 -14
  223. package/src/javaKeyStoreParser.ts +2 -6
  224. package/src/jsonParser.test.ts +2 -4
  225. package/src/jsonParser.ts +34 -38
  226. package/src/lazyMessageError.test.ts +21 -0
  227. package/src/lazyMessageError.ts +88 -0
  228. package/src/leb128Parser.test.ts +25 -23
  229. package/src/leb128Parser.ts +19 -19
  230. package/src/negativeLookaheadParser.test.ts +7 -11
  231. package/src/negativeLookaheadParser.ts +2 -2
  232. package/src/noStackCaptureOverheadError.test.ts +17 -0
  233. package/src/noStackCaptureOverheadError.ts +12 -0
  234. package/src/nonEmptyArrayParser.test.ts +3 -2
  235. package/src/nonEmptyArrayParser.ts +2 -2
  236. package/src/optionalParser.ts +2 -2
  237. package/src/parser.test.ts +96 -43
  238. package/src/parser.test.ts.md +13 -6
  239. package/src/parser.test.ts.snap +0 -0
  240. package/src/parser.ts +35 -12
  241. package/src/parserAccessorParser.ts +1 -1
  242. package/src/parserConsumedSequenceParser.ts +3 -3
  243. package/src/parserContext.test.ts +7 -3
  244. package/src/parserContext.ts +82 -48
  245. package/src/parserError.ts +143 -63
  246. package/src/parserImplementationInvariant.ts +3 -3
  247. package/src/parserInputCompanion.ts +2 -2
  248. package/src/promiseCompose.ts +2 -2
  249. package/src/separatedArrayParser.ts +3 -3
  250. package/src/separatedNonEmptyArrayParser.test.ts +117 -0
  251. package/src/separatedNonEmptyArrayParser.ts +61 -0
  252. package/src/sequenceBuffer.test.ts +9 -9
  253. package/src/sequenceBuffer.ts +4 -4
  254. package/src/sequenceTerminatedSequenceParser.test.ts +3 -5
  255. package/src/sequenceTerminatedSequenceParser.ts +4 -4
  256. package/src/sequenceUnparser.ts +2 -2
  257. package/src/skipToParser.ts +2 -2
  258. package/src/sliceBoundedParser.test.ts +4 -12
  259. package/src/sliceBoundedParser.ts +2 -2
  260. package/src/smali.ts +8 -3
  261. package/src/smaliParser.test.ts +377 -66
  262. package/src/smaliParser.test.ts.md +1635 -48
  263. package/src/smaliParser.test.ts.snap +0 -0
  264. package/src/smaliParser.ts +2751 -569
  265. package/src/stringFromAsyncIterable.ts +9 -0
  266. package/src/terminatedArrayParser.test.ts +11 -11
  267. package/src/terminatedArrayParser.ts +5 -7
  268. package/src/toAsyncIterator.ts +8 -8
  269. package/src/uint8Array.ts +2 -3
  270. package/src/unionParser.test.ts +22 -23
  271. package/src/unionParser.ts +6 -8
  272. package/src/unparser.test.ts +18 -34
  273. package/src/unparser.ts +13 -9
  274. package/src/unparserContext.ts +9 -13
  275. package/src/unparserError.ts +2 -1
  276. package/src/unparserImplementationInvariant.ts +1 -1
  277. package/src/unparserOutputCompanion.ts +1 -1
  278. package/src/zip.ts +2 -6
  279. package/src/zipParser.ts +10 -18
  280. package/src/zipUnparser.test.ts +1 -1
  281. package/src/zipUnparser.ts +52 -64
  282. package/tsconfig.json +7 -1
  283. package/xo.config.ts +15 -0
  284. package/.yarn/releases/yarn-4.5.3.cjs +0 -934
@@ -0,0 +1,9 @@
1
+ export async function stringFromAsyncIterable(asyncIterable: AsyncIterable<string>) {
2
+ let string = '';
3
+
4
+ for await (const chunk of asyncIterable) {
5
+ string += chunk;
6
+ }
7
+
8
+ return string;
9
+ }
@@ -2,7 +2,7 @@ import test from 'ava';
2
2
  import * as fc from 'fast-check';
3
3
  import { testProp } from '@fast-check/ava';
4
4
  import { createTerminatedArrayParser, createTerminatedArrayParserNaive, createTerminatedArrayParserUnsafe } from './terminatedArrayParser.js';
5
- import { Parser, runParser } from './parser.js';
5
+ import { type Parser, runParser } from './parser.js';
6
6
  import { stringParserInputCompanion, uint8ArrayParserInputCompanion } from './parserInputCompanion.js';
7
7
  import { HighResolutionTotalTimer } from './highResolutionTimer.js';
8
8
  import { createElementParser } from './elementParser.js';
@@ -64,7 +64,7 @@ testProp(
64
64
 
65
65
  if (
66
66
  result.status === 'rejected'
67
- && naiveResult.status === 'rejected'
67
+ && naiveResult.status === 'rejected'
68
68
  ) {
69
69
  result.reason.message = 'REDACTED';
70
70
  naiveResult.reason.message = 'REDACTED';
@@ -96,10 +96,10 @@ testProp.serial(
96
96
  })
97
97
  .map(string => ({
98
98
  string,
99
- terminator: string.slice(0 - terminatorLength)
99
+ terminator: string.slice(0 - terminatorLength),
100
100
  }))
101
101
  .filter(({ string, terminator }) => string.split(terminator).length === 2)
102
- ))
102
+ )),
103
103
  ],
104
104
  async (t, { string, terminator }) => {
105
105
  const terminatedArrayParserNaive = createTerminatedArrayParserNaive(
@@ -138,7 +138,7 @@ testProp.serial(
138
138
  };
139
139
  };
140
140
 
141
- const actualNaive = await naiveTotalTimer.measureAsync(() => runParser(
141
+ const actualNaive = await naiveTotalTimer.measureAsync(async () => runParser(
142
142
  createTestWrapperParser(terminatedArrayParserNaive),
143
143
  string,
144
144
  stringParserInputCompanion,
@@ -146,7 +146,7 @@ testProp.serial(
146
146
 
147
147
  t.is(actualNaive.string.length + actualNaive.terminator.length, string.length);
148
148
 
149
- const actual = await optimizedTotalTimer.measureAsync(() => runParser(
149
+ const actual = await optimizedTotalTimer.measureAsync(async () => runParser(
150
150
  createTestWrapperParser(terminatedArrayParser),
151
151
  string,
152
152
  stringParserInputCompanion,
@@ -164,7 +164,7 @@ test.serial(
164
164
  t => {
165
165
  t.true(
166
166
  optimizedTotalTimer.time * 1n < naiveTotalTimer.time,
167
- `Naive: ${naiveTotalTimer.time / 1000000n}ms, Optimized: ${optimizedTotalTimer.time / 1000000n}ms`,
167
+ `Naive: ${naiveTotalTimer.time / 1_000_000n}ms, Optimized: ${optimizedTotalTimer.time / 1_000_000n}ms`,
168
168
  );
169
169
  },
170
170
  );
@@ -184,10 +184,10 @@ testProp.serial(
184
184
  })
185
185
  .map(string => ({
186
186
  string,
187
- terminator: string.slice(0 - terminatorLength)
187
+ terminator: string.slice(0 - terminatorLength),
188
188
  }))
189
189
  .filter(({ string, terminator }) => string.split(terminator).length === 2)
190
- ))
190
+ )),
191
191
  ],
192
192
  async (t, { string, terminator }) => {
193
193
  const terminatedArrayParserNaive = createTerminatedArrayParserNaive(
@@ -234,7 +234,7 @@ testProp.serial(
234
234
 
235
235
  t.is(actualNaive.string.length + actualNaive.terminator.length, string.length);
236
236
 
237
- const actual = await unsafeTotalTimer.measureAsync(() => runParser(
237
+ const actual = await unsafeTotalTimer.measureAsync(async () => runParser(
238
238
  createTestWrapperParser(terminatedArrayParserUnsafe),
239
239
  string,
240
240
  stringParserInputCompanion,
@@ -252,7 +252,7 @@ test.serial(
252
252
  t => {
253
253
  t.true(
254
254
  unsafeTotalTimer.time * 1n < naiveTotalTimer.time,
255
- `Naive: ${naiveTotalTimer.time / 1000000n}ms, Unsafe: ${unsafeTotalTimer.time / 1000000n}ms`,
255
+ `Naive: ${naiveTotalTimer.time / 1_000_000n}ms, Unsafe: ${unsafeTotalTimer.time / 1_000_000n}ms`,
256
256
  );
257
257
  },
258
258
  );
@@ -1,13 +1,11 @@
1
1
  import { getParserName, type Parser, setParserName } from './parser.js';
2
- import { ParserParsingFailedError } from './parserError.js';
2
+ import { isParserParsingFailedError, ParserParsingFailedError } from './parserError.js';
3
3
  import { parserImplementationInvariant } from './parserImplementationInvariant.js';
4
4
  import { promiseCompose } from './promiseCompose.js';
5
5
  import { createUnionParser } from './unionParser.js';
6
6
 
7
7
  class Terminated<T> {
8
- constructor(
9
- public readonly value: T,
10
- ) {}
8
+ constructor(public readonly value: T) {}
11
9
  }
12
10
 
13
11
  export const createTerminatedArrayParserNaive = <ElementOutput, TerminatorOutput, Sequence>(
@@ -78,7 +76,7 @@ export const createTerminatedArrayParser = <ElementOutput, TerminatorOutput, Seq
78
76
  getParserName(terminatorParser, 'anonymousTerminator'),
79
77
  );
80
78
  } catch (error) {
81
- if (!(error instanceof ParserParsingFailedError)) {
79
+ if (!(isParserParsingFailedError(error))) {
82
80
  throw error;
83
81
  }
84
82
  } finally {
@@ -89,7 +87,7 @@ export const createTerminatedArrayParser = <ElementOutput, TerminatorOutput, Seq
89
87
 
90
88
  return [ elements, terminatorValue ];
91
89
  } catch (error) {
92
- if (!(error instanceof ParserParsingFailedError)) {
90
+ if (!(isParserParsingFailedError(error))) {
93
91
  throw error;
94
92
  }
95
93
  } finally {
@@ -126,7 +124,7 @@ export const createTerminatedArrayParserUnsafe = <ElementOutput, TerminatorOutpu
126
124
 
127
125
  return [ elements, terminatorValue ];
128
126
  } catch (error) {
129
- if (!(error instanceof ParserParsingFailedError)) {
127
+ if (!(isParserParsingFailedError(error))) {
130
128
  throw error;
131
129
  }
132
130
  } finally {
@@ -1,15 +1,15 @@
1
- import invariant from "invariant";
1
+ import invariant from 'invariant';
2
2
 
3
- function isAsyncIterable<T>(value: any): value is AsyncIterable<T> {
4
- return value && typeof value[Symbol.asyncIterator] === 'function';
3
+ function isAsyncIterable<T>(value: unknown): value is AsyncIterable<T> {
4
+ return typeof value === 'object' && value !== null && Symbol.asyncIterator in value && typeof (value as Record<symbol, unknown>)[Symbol.asyncIterator] === 'function';
5
5
  }
6
6
 
7
- function isIterable<T>(value: any): value is Iterable<T> {
8
- return value && typeof value[Symbol.iterator] === 'function';
7
+ function isIterable<T>(value: unknown): value is Iterable<T> {
8
+ return typeof value === 'object' && value !== null && Symbol.iterator in value && typeof (value as Record<symbol, unknown>)[Symbol.iterator] === 'function';
9
9
  }
10
10
 
11
- function isIterator<T>(value: any): value is Iterator<T> {
12
- return value && typeof value.next === 'function';
11
+ function isIterator<T>(value: unknown): value is Iterator<T> {
12
+ return typeof value === 'object' && value !== null && 'next' in value && typeof (value as Record<string, unknown>).next === 'function';
13
13
  }
14
14
 
15
15
  function iteratorToAsyncIterator<T>(iterator: Iterator<T>): AsyncIterator<T> {
@@ -24,7 +24,7 @@ export function toAsyncIterator<T>(value: AsyncIterator<T> | AsyncIterable<T> |
24
24
  || value instanceof Uint8Array
25
25
  ) {
26
26
  return (async function * () {
27
- yield value as any;
27
+ yield value as T;
28
28
  })();
29
29
  }
30
30
 
package/src/uint8Array.ts CHANGED
@@ -1,10 +1,9 @@
1
1
 
2
- export async function uint8ArrayAsyncIterableToUint8Array(
3
- uint8ArrayAsyncIterable: AsyncIterable<Uint8Array> | Iterable<Uint8Array>,
4
- ): Promise<Uint8Array> {
2
+ export async function uint8ArrayAsyncIterableToUint8Array(uint8ArrayAsyncIterable: AsyncIterable<Uint8Array> | Iterable<Uint8Array>): Promise<Uint8Array> {
5
3
  const chunks: Uint8Array[] = [];
6
4
  for await (const chunk of uint8ArrayAsyncIterable) {
7
5
  chunks.push(chunk);
8
6
  }
7
+
9
8
  return Buffer.concat(chunks);
10
9
  }
@@ -1,6 +1,6 @@
1
1
  import test from 'ava';
2
2
  import { createUnionParser } from './unionParser.js';
3
- import { Parser, runParser, setParserName } from './parser.js';
3
+ import { type Parser, runParser, setParserName } from './parser.js';
4
4
  import { stringParserInputCompanion } from './parserInputCompanion.js';
5
5
  import { createArrayParser } from './arrayParser.js';
6
6
  import { createExactElementParser } from './exactElementParser.js';
@@ -31,34 +31,32 @@ test('union of union of union', async t => {
31
31
  });
32
32
 
33
33
  test('sync and async child parsers', async t => {
34
- const parser = createArrayParser(
35
- createUnionParser<string, string>([
36
- async parserContext => {
37
- parserContext.invariant(
38
- parserContext.position % 2 === 0,
39
- 'Expected an even position.',
40
- );
34
+ const parser = createArrayParser(createUnionParser<string, string>([
35
+ async parserContext => {
36
+ parserContext.invariant(
37
+ parserContext.position % 2 === 0,
38
+ 'Expected an even position.',
39
+ );
41
40
 
42
- return parserContext.read(0);
43
- },
44
- parserContext => {
45
- parserContext.invariant(
46
- parserContext.position % 2 === 1,
47
- 'Expected an odd position.',
48
- );
41
+ return parserContext.read(0);
42
+ },
43
+ parserContext => {
44
+ parserContext.invariant(
45
+ parserContext.position % 2 === 1,
46
+ 'Expected an odd position.',
47
+ );
49
48
 
50
- parserContext.skip(1);
49
+ parserContext.skip(1);
51
50
 
52
- return String.fromCodePoint('A'.codePointAt(0)! + parserContext.position - 1);
53
- },
54
- ]),
55
- );
51
+ return String.fromCodePoint('A'.codePointAt(0)! + parserContext.position - 1);
52
+ },
53
+ ]));
56
54
 
57
55
  const result = await runParser(parser, 'a?c?', stringParserInputCompanion, {
58
56
  errorJoinMode: 'all',
59
57
  });
60
58
 
61
- t.deepEqual(result, 'aBcD'.split(''));
59
+ t.deepEqual(result, [ ...'aBcD' ]);
62
60
  });
63
61
 
64
62
  test('multiple parsers succeeded error', async t => {
@@ -70,8 +68,9 @@ test('multiple parsers succeeded error', async t => {
70
68
  const error = await t.throwsAsync(runParser(parser, 'foo', stringParserInputCompanion, {
71
69
  errorJoinMode: 'all',
72
70
  }), {
73
- instanceOf: ParserError,
74
- });
71
+ any: true,
72
+ name: 'ParserImplementationInvariantError',
73
+ }) as ParserError;
75
74
 
76
75
  t.true(error.message.includes('foo'));
77
76
  t.true(error.message.includes('foo1'));
@@ -1,9 +1,9 @@
1
1
  import { allSettledStream } from './allSettledStream.js';
2
2
  import { getParserName, setParserName, type Parser } from './parser.js';
3
3
  import { type ParserContext } from './parserContext.js';
4
- import { ParserParsingFailedError } from './parserError.js';
4
+ import { isParserParsingFailedError, ParserParsingFailedError } from './parserError.js';
5
5
  import { parserImplementationInvariant } from './parserImplementationInvariant.js';
6
- import { DeriveSequenceElement } from './sequence.js';
6
+ import { type DeriveSequenceElement } from './sequence.js';
7
7
 
8
8
  const bigintReplacer = (_key: string, value: unknown) => {
9
9
  if (typeof value === 'bigint') {
@@ -23,8 +23,8 @@ export const createUnionParser = <
23
23
  parserImplementationInvariant(childParsers.length > 0, 'Union parser must have at least one child parser.');
24
24
 
25
25
  type TaskContext = {
26
- childParser: Parser<unknown, Sequence, Element>,
27
- childParserContext: ParserContext<Sequence, Element>,
26
+ childParser: Parser<unknown, Sequence, Element>;
27
+ childParserContext: ParserContext<Sequence, Element>;
28
28
  };
29
29
 
30
30
  const unionParser: Parser<Output, Sequence, Element> = async parserContext => {
@@ -60,9 +60,7 @@ export const createUnionParser = <
60
60
  let didUnlookahead = false;
61
61
 
62
62
  for await (const childParserResult of childParserResults) {
63
- runningChildParserContexts = runningChildParserContexts.filter(
64
- runningChildParserContext => runningChildParserContext !== childParserResult.context,
65
- );
63
+ runningChildParserContexts = runningChildParserContexts.filter(runningChildParserContext => runningChildParserContext !== childParserResult.context);
66
64
 
67
65
  if (childParserResult.status === 'fulfilled') {
68
66
  successfulParserOutputs.push(childParserResult.value);
@@ -70,7 +68,7 @@ export const createUnionParser = <
70
68
  } else {
71
69
  const error = childParserResult.reason;
72
70
 
73
- if (error instanceof ParserParsingFailedError) {
71
+ if (isParserParsingFailedError(error)) {
74
72
  parserParsingFailedErrors.push(error);
75
73
  } else {
76
74
  throw error;
@@ -1,8 +1,8 @@
1
1
  import test from 'ava';
2
2
  import * as fc from 'fast-check';
3
- import { runUnparser, Unparser } from './unparser.js';
4
- import { stringUnparserOutputCompanion } from './unparserOutputCompanion.js';
5
3
  import { testProp } from '@fast-check/ava';
4
+ import { runUnparser, type Unparser } from './unparser.js';
5
+ import { stringUnparserOutputCompanion } from './unparserOutputCompanion.js';
6
6
 
7
7
  test('writeLater', async t => {
8
8
  const stringToBeWrittenBefore = 'before writeLater\n';
@@ -16,7 +16,7 @@ test('writeLater', async t => {
16
16
  let actualPositionBFromWithinWriteLater: number | undefined;
17
17
 
18
18
  const unparser: Unparser<void, string> = async function * (_input, unparserContext) {
19
- yield stringToBeWrittenBefore
19
+ yield stringToBeWrittenBefore;
20
20
 
21
21
  expectedPositionA = unparserContext.position;
22
22
 
@@ -127,19 +127,13 @@ test('writeLater deep', async t => {
127
127
  testProp(
128
128
  'writeLater positions and lengths',
129
129
  [
130
- fc.array(
131
- fc.oneof(
132
- fc.array(
133
- fc.oneof(
134
- fc.array(
135
- fc.string(),
136
- ),
137
- fc.string(),
138
- ),
139
- ),
130
+ fc.array(fc.oneof(
131
+ fc.array(fc.oneof(
132
+ fc.array(fc.string()),
140
133
  fc.string(),
141
- ),
142
- ),
134
+ )),
135
+ fc.string(),
136
+ )),
143
137
  ],
144
138
  async (t, stringArray3) => {
145
139
  const numberUnparser: Unparser<number, string> = async function * (input, unparserContext) {
@@ -148,12 +142,10 @@ testProp(
148
142
 
149
143
  const createLengthPrefixedUnparser = <T>(
150
144
  unparser: Unparser<T, string>,
151
- ): Unparser<T, string> => {
152
- return async function * (input, unparserContext) {
153
- const length = yield * unparserContext.writeLater(8);
154
- yield * unparser(input, unparserContext);
155
- yield * unparserContext.writeEarlier(length, numberUnparser, unparserContext.position - length.positionEnd);
156
- };
145
+ ): Unparser<T, string> => async function * (input, unparserContext) {
146
+ const length = yield * unparserContext.writeLater(8);
147
+ yield * unparser(input, unparserContext);
148
+ yield * unparserContext.writeEarlier(length, numberUnparser, unparserContext.position - length.positionEnd);
157
149
  };
158
150
 
159
151
  const unparser0: Unparser<string, string> = createLengthPrefixedUnparser(async function * (input, unparserContext) {
@@ -166,23 +158,15 @@ testProp(
166
158
  }
167
159
  });
168
160
 
169
- const unparser2: Unparser<(string | string[])[], string> = createLengthPrefixedUnparser(async function * (input, unparserContext) {
161
+ const unparser2: Unparser<Array<string | string[]>, string> = createLengthPrefixedUnparser(async function * (input, unparserContext) {
170
162
  for (const stringArray2 of input) {
171
- if (typeof stringArray2 === 'string') {
172
- yield * unparser0(stringArray2, unparserContext);
173
- } else {
174
- yield * unparser1(stringArray2, unparserContext);
175
- }
163
+ yield * (typeof stringArray2 === 'string' ? unparser0(stringArray2, unparserContext) : unparser1(stringArray2, unparserContext));
176
164
  }
177
165
  });
178
166
 
179
- const unparser3: Unparser<(string | (string | string[])[])[], string> = createLengthPrefixedUnparser(async function * (input, unparserContext) {
167
+ const unparser3: Unparser<Array<string | Array<string | string[]>>, string> = createLengthPrefixedUnparser(async function * (input, unparserContext) {
180
168
  for (const stringArray3 of input) {
181
- if (typeof stringArray3 === 'string') {
182
- yield * unparser0(stringArray3, unparserContext);
183
- } else {
184
- yield * unparser2(stringArray3, unparserContext);
185
- }
169
+ yield * (typeof stringArray3 === 'string' ? unparser0(stringArray3, unparserContext) : unparser2(stringArray3, unparserContext));
186
170
  }
187
171
  });
188
172
 
@@ -196,7 +180,7 @@ testProp(
196
180
 
197
181
  const actualString = strings.join('');
198
182
 
199
- type DeepArray<T> = T | DeepArray<T>[];
183
+ type DeepArray<T> = T | Array<DeepArray<T>>;
200
184
 
201
185
  function getExpectedString(stringArray: DeepArray<string>): string {
202
186
  let expectedString = stringArray;
package/src/unparser.ts CHANGED
@@ -1,8 +1,10 @@
1
- import invariant from "invariant";
2
- import { DeriveSequenceElement } from "./sequence.js";
3
- import { UnparserContext, UnparserContextImplementation, WriteEarlier, WriteLater } from "./unparserContext.js";
4
- import { UnparserOutputCompanion } from "./unparserOutputCompanion.js";
5
- import { unparserImplementationInvariant } from "./unparserImplementationInvariant.js";
1
+ import invariant from 'invariant';
2
+ import { type DeriveSequenceElement } from './sequence.js';
3
+ import {
4
+ type UnparserContext, UnparserContextImplementation, WriteEarlier, WriteLater,
5
+ } from './unparserContext.js';
6
+ import { type UnparserOutputCompanion } from './unparserOutputCompanion.js';
7
+ import { unparserImplementationInvariant } from './unparserImplementationInvariant.js';
6
8
 
7
9
  type UnparserIterableValue<Sequence, Element> =
8
10
  | Sequence
@@ -49,7 +51,7 @@ async function * elementsToSequences<
49
51
  for await (const value of values) {
50
52
  if (
51
53
  value instanceof WriteLater
52
- || value instanceof WriteEarlier
54
+ || value instanceof WriteEarlier
53
55
  ) {
54
56
  if (elements.length > 0) {
55
57
  const sequence = unparserOutputCompanion.from(elements);
@@ -91,7 +93,9 @@ function wrapUnparserResult<Sequence, Element>(
91
93
  unparserResult,
92
94
  unparserOutputCompanion,
93
95
  ),
94
- value => unparserContext.handleYield(value),
96
+ value => {
97
+ unparserContext.handleYield(value);
98
+ },
95
99
  );
96
100
  }
97
101
 
@@ -119,7 +123,7 @@ export async function * runUnparser<
119
123
  type PushOutput = (value: Sequence | WriteLater<Sequence, Element>) => void;
120
124
  type FinishOutput = () => void;
121
125
 
122
- const iteratorStack: [ Iterator, PushOutput, FinishOutput ][] = [
126
+ const iteratorStack: Array<[ Iterator, PushOutput, FinishOutput ]> = [
123
127
  [
124
128
  valuesWithoutElements[Symbol.asyncIterator](),
125
129
  value => outputQueue.push(value),
@@ -130,7 +134,7 @@ export async function * runUnparser<
130
134
  while (true) {
131
135
  while (
132
136
  outputQueue.length > 0
133
- && !(outputQueue[0] instanceof WriteLater)
137
+ && !(outputQueue[0] instanceof WriteLater)
134
138
  ) {
135
139
  yield outputQueue.shift() as Sequence;
136
140
  }
@@ -1,9 +1,8 @@
1
- /* eslint-disable prefer-arrow-callback */
2
1
 
3
- import { Unparser, UnparserResult } from "./unparser.js";
4
- import { UnparserOutputCompanion } from "./unparserOutputCompanion.js";
5
- import { unparserImplementationInvariant } from "./unparserImplementationInvariant.js";
6
- import { parserImplementationInvariant } from "./parserImplementationInvariant.js";
2
+ import { type Unparser, type UnparserResult } from './unparser.js';
3
+ import { type UnparserOutputCompanion } from './unparserOutputCompanion.js';
4
+ import { unparserImplementationInvariant } from './unparserImplementationInvariant.js';
5
+ import { parserImplementationInvariant } from './parserImplementationInvariant.js';
7
6
 
8
7
  export type UnparserContext<Sequence, Element> = {
9
8
  get position(): number;
@@ -19,7 +18,7 @@ export type UnparserContext<Sequence, Element> = {
19
18
  export class WriteLater<Sequence, Element> extends Error {
20
19
  name = 'WriteLater';
21
20
 
22
- constructor (
21
+ constructor(
23
22
  private readonly _position: number,
24
23
  private readonly _length: number,
25
24
  ) {
@@ -105,13 +104,10 @@ export class UnparserContextImplementation<Sequence, Element> implements Unparse
105
104
  );
106
105
  }
107
106
 
108
- handleYield(
109
- value:
110
- | Sequence
111
- | WriteLater<Sequence, Element>
112
- | WriteEarlier<Sequence, Element>
113
- ,
114
- ) {
107
+ handleYield(value:
108
+ | Sequence
109
+ | WriteLater<Sequence, Element>
110
+ | WriteEarlier<Sequence, Element>) {
115
111
  if (value instanceof WriteEarlier) {
116
112
  return;
117
113
  }
@@ -1,5 +1,6 @@
1
+ import { NormalLazyMessageError } from "./lazyMessageError.js";
1
2
 
2
- export class UnparserError extends Error {
3
+ export class UnparserError extends NormalLazyMessageError {
3
4
  name = 'UnparserError';
4
5
  }
5
6
 
@@ -1,6 +1,6 @@
1
1
  import { UnparserImplementationInvariantError } from './unparserError.js';
2
2
  import { type Falsy, customInvariant, type ValueOrAccessor } from './customInvariant.js';
3
3
 
4
- export function unparserImplementationInvariant<T>(value: T, format: ValueOrAccessor<string | string[]>, ...formatArguments: any[]): Exclude<T, Falsy> {
4
+ export function unparserImplementationInvariant<T>(value: T, format: ValueOrAccessor<string | string[]>, ...formatArguments: unknown[]): Exclude<T, Falsy> {
5
5
  return customInvariant(UnparserImplementationInvariantError, value, format, ...formatArguments);
6
6
  }
@@ -1,4 +1,4 @@
1
- import { StringParserInputCompanion, Uint8ArrayParserInputCompanion } from "./parserInputCompanion.js";
1
+ import { StringParserInputCompanion, Uint8ArrayParserInputCompanion } from './parserInputCompanion.js';
2
2
 
3
3
  export type UnparserOutputCompanion<Sequence, Element> = {
4
4
  is(value: unknown): value is Sequence;
package/src/zip.ts CHANGED
@@ -1,9 +1,7 @@
1
1
 
2
2
  export type ZipCompression =
3
3
  | 'store'
4
- | 'deflate'
5
- ;
6
-
4
+ | 'deflate';
7
5
  export type ZipEntryAttributes = {
8
6
  };
9
7
 
@@ -27,9 +25,7 @@ export type ZipDirectoryEntry = ZipEntryCommon & {
27
25
 
28
26
  export type ZipEntry =
29
27
  | ZipFileEntry
30
- | ZipDirectoryEntry
31
- ;
32
-
28
+ | ZipDirectoryEntry;
33
29
  export type Zip = {
34
30
  comment: string;
35
31
  entries: ZipEntry[];
package/src/zipParser.ts CHANGED
@@ -144,17 +144,13 @@ type ZipDataDescriptor = {
144
144
  };
145
145
 
146
146
  const zipDataDescriptorSignature: Uint8Array = Buffer.from('504b0708', 'hex');
147
- const zipDataDescriptorSignatureParser = createExactSequenceParser<Uint8Array>(
148
- zipDataDescriptorSignature
149
- );
147
+ const zipDataDescriptorSignatureParser = createExactSequenceParser<Uint8Array>(zipDataDescriptorSignature);
150
148
 
151
149
  const zipDataDescriptorParser: Parser<ZipDataDescriptor, Uint8Array> = promiseCompose(
152
150
  createTupleParser([
153
- createNegativeLookaheadParser(
154
- zipLocalFileHeaderSignatureParser,
155
- ),
151
+ createNegativeLookaheadParser(zipLocalFileHeaderSignatureParser),
156
152
  // FIXME: optional in spec
157
- //createOptionalParser(zipDataDescriptorSignatureParser),
153
+ // createOptionalParser(zipDataDescriptorSignatureParser),
158
154
  zipDataDescriptorSignatureParser,
159
155
  uint32LEParser,
160
156
  uint32LEParser,
@@ -186,12 +182,10 @@ export const zipLocalFileParser: Parser<ZipLocalFile, Uint8Array> = promiseCompo
186
182
  parserCreatorCompose(
187
183
  () => zipLocalFileHeaderParser,
188
184
  zipLocalFileHeader => {
189
- const sizeInDataDescriptor = Boolean(
190
- zipLocalFileHeader.generalPurposeBitFlag & 0b0000_0000_0000_1000
191
- && zipLocalFileHeader.crc32 === 0
192
- && zipLocalFileHeader.compressedSize === 0
193
- && zipLocalFileHeader.uncompressedSize === 0
194
- );
185
+ const sizeInDataDescriptor = Boolean(zipLocalFileHeader.generalPurposeBitFlag & 0b0000_0000_0000_1000
186
+ && zipLocalFileHeader.crc32 === 0
187
+ && zipLocalFileHeader.compressedSize === 0
188
+ && zipLocalFileHeader.uncompressedSize === 0);
195
189
 
196
190
  return createTupleParser([
197
191
  async () => zipLocalFileHeader,
@@ -210,9 +204,7 @@ export const zipLocalFileParser: Parser<ZipLocalFile, Uint8Array> = promiseCompo
210
204
  sizeInDataDescriptor
211
205
  ? (<T>(parser: T): T => parser)
212
206
  : createOptionalParser
213
- )(
214
- zipDataDescriptorParser
215
- ),
207
+ )(zipDataDescriptorParser),
216
208
  ]);
217
209
  },
218
210
  )(),
@@ -257,7 +249,7 @@ const zipVersionMadeByParser: Parser<ZipVersionMadeBy, Uint8Array> = promiseComp
257
249
 
258
250
  type ZipExternalFileAttributes = {
259
251
  directory: boolean;
260
- }
252
+ };
261
253
 
262
254
  const dosExternalFileAttributesParser: Parser<ZipExternalFileAttributes, Uint8Array> = promiseCompose(
263
255
  uint32LEParser,
@@ -499,7 +491,7 @@ export async function zipEntriesFromZipSegments({
499
491
 
500
492
  const isDirectory = (
501
493
  centralDirectoryHeader.externalFileAttributes.directory
502
- || commonFields.path.endsWith('/')
494
+ || commonFields.path.endsWith('/')
503
495
  );
504
496
 
505
497
  if (isDirectory) {
@@ -1,7 +1,7 @@
1
+ import fsPromises from 'node:fs/promises';
1
2
  import { fc, testProp } from '@fast-check/ava';
2
3
  import { temporaryFile } from 'tempy';
3
4
  import { execa } from 'execa';
4
- import fsPromises from 'node:fs/promises';
5
5
  import { runUnparser } from './unparser.js';
6
6
  import { createZipUnparser } from './zipUnparser.js';
7
7
  import { uint8ArrayUnparserOutputCompanion } from './unparserOutputCompanion.js';