@futpib/parser 1.0.0 → 1.0.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 (380) hide show
  1. package/build/allSettledStream.js +32 -14
  2. package/build/allSettledStream.test.js +32 -0
  3. package/build/androidPackage.d.ts +39 -0
  4. package/build/androidPackageParser.d.ts +17 -0
  5. package/build/androidPackageParser.js +183 -0
  6. package/build/{apkParser.test.js → androidPackageParser.test.js} +7 -7
  7. package/build/androidPackageUnparser.d.ts +4 -0
  8. package/build/androidPackageUnparser.js +90 -0
  9. package/build/androidPackageUnparser.test.js +26 -0
  10. package/build/arbitrarilySlicedAsyncInterable.d.ts +3 -1
  11. package/build/arbitrarilySlicedAsyncInterable.js +3 -3
  12. package/build/arbitraryFileSystemEntry.js +1 -1
  13. package/build/arbitraryZip.d.ts +1 -1
  14. package/build/arbitraryZip.js +13 -19
  15. package/build/arbitraryZipPermissions.d.ts +1 -8
  16. package/build/arbitraryZipPermissions.js +1 -16
  17. package/build/arbitraryZipStream.d.ts +1 -1
  18. package/build/arbitraryZipStream.js +3 -3
  19. package/build/arrayParser.d.ts +1 -1
  20. package/build/arrayParser.js +2 -2
  21. package/build/arrayParser.test.js +4 -4
  22. package/build/arrayUnparser.d.ts +2 -0
  23. package/build/arrayUnparser.js +8 -0
  24. package/build/backsmali.d.ts +1 -0
  25. package/build/backsmali.js +22 -0
  26. package/build/bsonParser.js +6 -2
  27. package/build/bsonParser.test.js +2 -2
  28. package/build/{parserInvariant.d.ts → customInvariant.d.ts} +1 -1
  29. package/build/{parserInvariant.js → customInvariant.js} +1 -1
  30. package/build/dalvikBytecodeParser/formatParsers.d.ts +97 -0
  31. package/build/dalvikBytecodeParser/formatParsers.js +169 -0
  32. package/build/dalvikBytecodeParser.d.ts +107 -0
  33. package/build/dalvikBytecodeParser.js +836 -0
  34. package/build/dalvikExecutable.d.ts +158 -0
  35. package/build/dalvikExecutable.js +20 -0
  36. package/build/dalvikExecutableParser/stringSyntaxParser.d.ts +4 -0
  37. package/build/dalvikExecutableParser/stringSyntaxParser.js +76 -0
  38. package/build/dalvikExecutableParser/typeParsers.d.ts +10 -0
  39. package/build/dalvikExecutableParser/typeParsers.js +34 -0
  40. package/build/dalvikExecutableParser/typedNumbers.d.ts +90 -0
  41. package/build/dalvikExecutableParser/typedNumbers.js +19 -0
  42. package/build/dalvikExecutableParser.d.ts +5 -0
  43. package/build/dalvikExecutableParser.js +1439 -0
  44. package/build/dalvikExecutableParser.test.js +70 -0
  45. package/build/dalvikExecutableParserAgainstSmaliParser.test.js +298 -0
  46. package/build/debugLogInputParser.d.ts +4 -0
  47. package/build/debugLogInputParser.js +16 -0
  48. package/build/debugLogParser.d.ts +1 -1
  49. package/build/debugLogParser.js +15 -4
  50. package/build/disjunctionParser.d.ts +2 -1
  51. package/build/disjunctionParser.js +4 -2
  52. package/build/elementParser.d.ts +2 -2
  53. package/build/elementParser.js +1 -1
  54. package/build/elementTerminatedArrayParser.d.ts +3 -0
  55. package/build/elementTerminatedArrayParser.js +18 -0
  56. package/build/elementTerminatedArrayParser.test.js +52 -0
  57. package/build/elementTerminatedSequenceArrayParser.d.ts +3 -0
  58. package/build/elementTerminatedSequenceArrayParser.js +32 -0
  59. package/build/elementTerminatedSequenceArrayParser.test.js +34 -0
  60. package/build/elementTerminatedSequenceParser.d.ts +3 -0
  61. package/build/elementTerminatedSequenceParser.js +27 -0
  62. package/build/elementTerminatedSequenceParser.test.js +34 -0
  63. package/build/endOfInputParser.d.ts +2 -2
  64. package/build/exactElementParser.d.ts +1 -1
  65. package/build/exactElementParser.js +10 -5
  66. package/build/exactSequenceParser.d.ts +2 -1
  67. package/build/exactSequenceParser.js +12 -2
  68. package/build/fetchCid.d.ts +1 -0
  69. package/build/fetchCid.js +107 -0
  70. package/build/fixedLengthSequenceParser.d.ts +1 -0
  71. package/build/fixedLengthSequenceParser.js +18 -1
  72. package/build/fixedLengthSequenceParser.test.js +41 -0
  73. package/build/hasExecutable.d.ts +1 -0
  74. package/build/hasExecutable.js +8 -0
  75. package/build/highResolutionTimer.d.ts +16 -0
  76. package/build/highResolutionTimer.js +42 -0
  77. package/build/index.d.ts +5 -2
  78. package/build/index.js +3 -0
  79. package/build/inputReader.d.ts +14 -3
  80. package/build/inputReader.js +43 -6
  81. package/build/inputReader.test.js +172 -7
  82. package/build/inputReaderState.d.ts +10 -0
  83. package/build/inputReaderState.js +16 -0
  84. package/build/inspect.d.ts +1 -0
  85. package/build/inspect.js +7 -0
  86. package/build/javaKeyStoreParser.d.ts +2 -0
  87. package/build/javaKeyStoreParser.js +67 -0
  88. package/build/javaKeyStoreParser.test.d.ts +1 -0
  89. package/build/javaKeyStoreParser.test.js +16 -0
  90. package/build/jsonParser.d.ts +2 -0
  91. package/build/jsonParser.js +13 -15
  92. package/build/jsonParser.test.js +2 -2
  93. package/build/leb128Parser.d.ts +7 -0
  94. package/build/leb128Parser.js +82 -0
  95. package/build/leb128Parser.test.d.ts +1 -0
  96. package/build/leb128Parser.test.js +107 -0
  97. package/build/listParser.d.ts +1 -1
  98. package/build/listParser.js +5 -5
  99. package/build/lookaheadParser.d.ts +2 -0
  100. package/build/lookaheadParser.js +14 -0
  101. package/build/{negativeLookahead.d.ts → negativeLookaheadParser.d.ts} +1 -1
  102. package/build/negativeLookaheadParser.js +24 -0
  103. package/build/negativeLookaheadParser.test.d.ts +1 -0
  104. package/build/negativeLookaheadParser.test.js +30 -0
  105. package/build/nonEmptyArrayParser.d.ts +2 -0
  106. package/build/nonEmptyArrayParser.js +32 -0
  107. package/build/nonEmptyArrayParser.test.d.ts +1 -0
  108. package/build/nonEmptyArrayParser.test.js +16 -0
  109. package/build/optionalParser.d.ts +1 -1
  110. package/build/optionalParser.js +2 -2
  111. package/build/parser.d.ts +13 -4
  112. package/build/parser.js +67 -32
  113. package/build/parser.test.js +94 -27
  114. package/build/parserAccessorParser.d.ts +1 -1
  115. package/build/parserAccessorParser.js +9 -1
  116. package/build/parserConsumedSequenceParser.d.ts +2 -0
  117. package/build/parserConsumedSequenceParser.js +22 -0
  118. package/build/parserContext.d.ts +20 -6
  119. package/build/parserContext.js +70 -40
  120. package/build/parserContext.test.js +34 -7
  121. package/build/parserCreatorCompose.d.ts +4 -3
  122. package/build/parserCreatorCompose.js +9 -3
  123. package/build/parserError.d.ts +6 -0
  124. package/build/parserError.js +6 -6
  125. package/build/parserImplementationInvariant.d.ts +1 -1
  126. package/build/parserImplementationInvariant.js +2 -2
  127. package/build/parserInputCompanion.d.ts +35 -0
  128. package/build/parserInputCompanion.js +68 -0
  129. package/build/promiseCompose.d.ts +1 -1
  130. package/build/promiseCompose.js +12 -1
  131. package/build/promiseSettled.d.ts +1 -0
  132. package/build/promiseSettled.js +4 -0
  133. package/build/quantifierParser.d.ts +2 -0
  134. package/build/quantifierParser.js +17 -0
  135. package/build/separatedArrayParser.d.ts +2 -0
  136. package/build/separatedArrayParser.js +39 -0
  137. package/build/separatedArrayParser.test.d.ts +1 -0
  138. package/build/separatedArrayParser.test.js +21 -0
  139. package/build/sequenceBuffer.d.ts +13 -3
  140. package/build/sequenceBuffer.js +60 -8
  141. package/build/sequenceBuffer.test.js +59 -2
  142. package/build/sequenceTerminatedSequenceParser.d.ts +5 -0
  143. package/build/sequenceTerminatedSequenceParser.js +32 -0
  144. package/build/sequenceTerminatedSequenceParser.test.d.ts +1 -0
  145. package/build/sequenceTerminatedSequenceParser.test.js +37 -0
  146. package/build/sequenceUnparser.d.ts +2 -0
  147. package/build/sequenceUnparser.js +6 -0
  148. package/build/skipParser.d.ts +1 -1
  149. package/build/skipParser.js +4 -2
  150. package/build/skipToParser.d.ts +2 -0
  151. package/build/skipToParser.js +11 -0
  152. package/build/sliceBoundedParser.d.ts +2 -2
  153. package/build/sliceBoundedParser.js +8 -3
  154. package/build/sliceBoundedParser.test.js +37 -3
  155. package/build/smali.d.ts +1 -0
  156. package/build/smali.js +17 -0
  157. package/build/smaliParser.d.ts +12 -0
  158. package/build/smaliParser.js +656 -0
  159. package/build/smaliParser.test.d.ts +1 -0
  160. package/build/smaliParser.test.js +115 -0
  161. package/build/terminatedArrayParser.d.ts +4 -2
  162. package/build/terminatedArrayParser.js +82 -5
  163. package/build/terminatedArrayParser.test.d.ts +1 -0
  164. package/build/terminatedArrayParser.test.js +131 -0
  165. package/build/toAsyncIterable.d.ts +1 -0
  166. package/build/toAsyncIterable.js +7 -0
  167. package/build/toAsyncIterator.d.ts +1 -0
  168. package/build/toAsyncIterator.js +33 -0
  169. package/build/tupleParser.d.ts +4 -0
  170. package/build/tupleParser.js +1 -5
  171. package/build/uint8Array.d.ts +1 -0
  172. package/build/uint8Array.js +7 -0
  173. package/build/unionParser.d.ts +2 -1
  174. package/build/unionParser.js +27 -12
  175. package/build/unionParser.test.d.ts +1 -0
  176. package/build/unionParser.test.js +60 -0
  177. package/build/unparser.d.ts +8 -0
  178. package/build/unparser.js +104 -0
  179. package/build/unparser.test.d.ts +1 -0
  180. package/build/unparser.test.js +150 -0
  181. package/build/unparserContext.d.ts +31 -0
  182. package/build/unparserContext.js +74 -0
  183. package/build/unparserError.d.ts +9 -0
  184. package/build/unparserError.js +9 -0
  185. package/build/unparserImplementationInvariant.d.ts +2 -0
  186. package/build/unparserImplementationInvariant.js +5 -0
  187. package/build/unparserOutputCompanion.d.ts +15 -0
  188. package/build/unparserOutputCompanion.js +13 -0
  189. package/build/zip.d.ts +9 -17
  190. package/build/zipParser.d.ts +20 -12
  191. package/build/zipParser.js +83 -71
  192. package/build/zipParser.test.js +2 -7
  193. package/build/zipUnparser.d.ts +8 -0
  194. package/build/zipUnparser.js +200 -0
  195. package/build/zipUnparser.test.d.ts +1 -0
  196. package/build/zipUnparser.test.js +80 -0
  197. package/package.json +26 -8
  198. package/src/allSettledStream.test.ts +40 -0
  199. package/src/allSettledStream.ts +47 -15
  200. package/src/androidPackage.ts +48 -0
  201. package/src/{apkParser.test.ts → androidPackageParser.test.ts} +7 -8
  202. package/src/androidPackageParser.test.ts.md +271 -0
  203. package/src/androidPackageParser.test.ts.snap +0 -0
  204. package/src/androidPackageParser.ts +440 -0
  205. package/src/androidPackageUnparser.test.ts +36 -0
  206. package/src/androidPackageUnparser.ts +120 -0
  207. package/src/arbitrarilySlicedAsyncInterable.ts +7 -2
  208. package/src/arbitraryFileSystemEntry.ts +2 -4
  209. package/src/arbitraryZip.ts +20 -27
  210. package/src/arbitraryZipPermissions.ts +0 -25
  211. package/src/arbitraryZipStream.ts +4 -4
  212. package/src/arrayParser.test.ts +4 -4
  213. package/src/arrayParser.ts +3 -2
  214. package/src/arrayUnparser.ts +13 -0
  215. package/src/backsmali.ts +30 -0
  216. package/src/bsonParser.test.ts +2 -2
  217. package/src/bsonParser.ts +16 -5
  218. package/src/{parserInvariant.ts → customInvariant.ts} +2 -2
  219. package/src/dalvikBytecodeParser/formatParsers.ts +421 -0
  220. package/src/dalvikBytecodeParser.ts +2074 -0
  221. package/src/dalvikExecutable.ts +220 -0
  222. package/src/dalvikExecutableParser/stringSyntaxParser.ts +145 -0
  223. package/src/dalvikExecutableParser/typeParsers.ts +65 -0
  224. package/src/dalvikExecutableParser/typedNumbers.ts +57 -0
  225. package/src/dalvikExecutableParser.test.ts +89 -0
  226. package/src/dalvikExecutableParser.test.ts.md +634 -0
  227. package/src/dalvikExecutableParser.test.ts.snap +0 -0
  228. package/src/dalvikExecutableParser.ts +2768 -0
  229. package/src/dalvikExecutableParserAgainstSmaliParser.test.ts +346 -0
  230. package/src/debugLogInputParser.ts +28 -0
  231. package/src/debugLogParser.ts +20 -4
  232. package/src/disjunctionParser.ts +10 -5
  233. package/src/elementParser.ts +3 -3
  234. package/src/elementTerminatedArrayParser.test.ts +99 -0
  235. package/src/elementTerminatedArrayParser.ts +31 -0
  236. package/src/elementTerminatedSequenceArrayParser.test.ts +54 -0
  237. package/src/elementTerminatedSequenceArrayParser.ts +52 -0
  238. package/src/elementTerminatedSequenceParser.test.ts +54 -0
  239. package/src/elementTerminatedSequenceParser.ts +43 -0
  240. package/src/endOfInputParser.ts +4 -4
  241. package/src/exactElementParser.ts +18 -12
  242. package/src/exactSequenceParser.ts +24 -3
  243. package/src/fetchCid.ts +125 -0
  244. package/src/fixedLengthSequenceParser.test.ts +77 -0
  245. package/src/fixedLengthSequenceParser.ts +28 -1
  246. package/src/hasExecutable.ts +11 -0
  247. package/src/highResolutionTimer.ts +49 -0
  248. package/src/index.ts +15 -2
  249. package/src/inputReader.test.ts +216 -7
  250. package/src/inputReader.ts +80 -5
  251. package/src/inputReaderState.ts +33 -0
  252. package/src/inspect.ts +9 -0
  253. package/src/javaKeyStore.ts +0 -0
  254. package/src/javaKeyStoreParser.test.ts +22 -0
  255. package/src/javaKeyStoreParser.test.ts.md +103 -0
  256. package/src/javaKeyStoreParser.test.ts.snap +0 -0
  257. package/src/javaKeyStoreParser.ts +136 -0
  258. package/src/jsonParser.test.ts +2 -2
  259. package/src/jsonParser.ts +23 -34
  260. package/src/leb128Parser.test.ts +171 -0
  261. package/src/leb128Parser.ts +125 -0
  262. package/src/listParser.ts +6 -6
  263. package/src/lookaheadParser.ts +19 -0
  264. package/src/negativeLookaheadParser.test.ts +53 -0
  265. package/src/negativeLookaheadParser.ts +36 -0
  266. package/src/nonEmptyArrayParser.test.ts +20 -0
  267. package/src/nonEmptyArrayParser.ts +44 -0
  268. package/src/optionalParser.ts +4 -3
  269. package/src/parser.test.ts +148 -27
  270. package/src/parser.test.ts.md +21 -21
  271. package/src/parser.test.ts.snap +0 -0
  272. package/src/parser.ts +153 -49
  273. package/src/parserAccessorParser.ts +12 -2
  274. package/src/parserConsumedSequenceParser.ts +29 -0
  275. package/src/parserContext.test.ts +38 -7
  276. package/src/parserContext.ts +127 -51
  277. package/src/parserCreatorCompose.ts +25 -7
  278. package/src/parserError.ts +9 -6
  279. package/src/parserImplementationInvariant.ts +2 -2
  280. package/src/parserInputCompanion.ts +102 -0
  281. package/src/promiseCompose.ts +17 -3
  282. package/src/promiseSettled.ts +6 -0
  283. package/src/quantifierParser.ts +25 -0
  284. package/src/separatedArrayParser.test.ts +34 -0
  285. package/src/separatedArrayParser.ts +55 -0
  286. package/src/sequenceBuffer.test.ts +72 -2
  287. package/src/sequenceBuffer.ts +93 -7
  288. package/src/sequenceTerminatedSequenceParser.test.ts +60 -0
  289. package/src/sequenceTerminatedSequenceParser.ts +62 -0
  290. package/src/sequenceUnparser.ts +9 -0
  291. package/src/skipParser.ts +7 -5
  292. package/src/skipToParser.ts +16 -0
  293. package/src/sliceBoundedParser.test.ts +45 -3
  294. package/src/sliceBoundedParser.ts +21 -3
  295. package/src/smali.ts +24 -0
  296. package/src/smaliParser.test.ts +132 -0
  297. package/src/smaliParser.test.ts.md +2320 -0
  298. package/src/smaliParser.test.ts.snap +0 -0
  299. package/src/smaliParser.ts +1166 -0
  300. package/src/terminatedArrayParser.test.ts +258 -0
  301. package/src/terminatedArrayParser.ts +111 -6
  302. package/src/toAsyncIterable.ts +7 -0
  303. package/src/toAsyncIterator.ts +48 -0
  304. package/src/tupleParser.ts +8 -5
  305. package/src/uint8Array.ts +10 -0
  306. package/src/unionParser.test.ts +79 -0
  307. package/src/unionParser.ts +44 -16
  308. package/src/unparser.test.ts +221 -0
  309. package/src/unparser.ts +209 -0
  310. package/src/unparserContext.ts +127 -0
  311. package/src/unparserError.ts +12 -0
  312. package/src/unparserImplementationInvariant.ts +6 -0
  313. package/src/unparserOutputCompanion.ts +24 -0
  314. package/src/zip.ts +10 -22
  315. package/src/zipParser.test.ts +2 -8
  316. package/src/zipParser.ts +223 -146
  317. package/src/zipUnparser.test.ts +119 -0
  318. package/src/zipUnparser.ts +300 -0
  319. package/build/apk.d.ts +0 -13
  320. package/build/apkParser.d.ts +0 -3
  321. package/build/apkParser.js +0 -135
  322. package/build/arbitraryDosDate.d.ts +0 -2
  323. package/build/arbitraryDosDate.js +0 -8
  324. package/build/arbitraryZipEntry.d.ts +0 -3
  325. package/build/arbitraryZipEntry.js +0 -26
  326. package/build/createDisjunctionParser.d.ts +0 -2
  327. package/build/createDisjunctionParser.js +0 -47
  328. package/build/createExactParser.d.ts +0 -2
  329. package/build/createExactParser.js +0 -12
  330. package/build/createSequentialUnionParser.d.ts +0 -2
  331. package/build/createSequentialUnionParser.js +0 -69
  332. package/build/fixedLengthChunkParser.d.ts +0 -2
  333. package/build/fixedLengthChunkParser.js +0 -12
  334. package/build/fixedLengthParser.d.ts +0 -2
  335. package/build/fixedLengthParser.js +0 -12
  336. package/build/inputChunkBuffer.d.ts +0 -15
  337. package/build/inputChunkBuffer.js +0 -40
  338. package/build/inputChunkBuffer.test.js +0 -34
  339. package/build/inputCompanion.d.ts +0 -18
  340. package/build/inputCompanion.js +0 -28
  341. package/build/invariantDefined.d.ts +0 -1
  342. package/build/invariantDefined.js +0 -5
  343. package/build/invariantIdentity.d.ts +0 -3
  344. package/build/invariantIdentity.js +0 -5
  345. package/build/jsonParser2.d.ts +0 -3
  346. package/build/jsonParser2.js +0 -52
  347. package/build/jsonParser2.test.js +0 -22
  348. package/build/negativeLookahead.js +0 -20
  349. package/build/parserCompose.d.ts +0 -3
  350. package/build/parserCompose.js +0 -7
  351. package/build/parserImplementationInvariantInvariant.d.ts +0 -3
  352. package/build/parserImplementationInvariantInvariant.js +0 -15
  353. package/build/promiseFish.d.ts +0 -1
  354. package/build/promiseFish.js +0 -3
  355. package/build/sequenceParser.d.ts +0 -3
  356. package/build/sequenceParser.js +0 -10
  357. package/build/terminatedSequenceParser.d.ts +0 -2
  358. package/build/terminatedSequenceParser.js +0 -24
  359. package/build/zipEntry.d.ts +0 -28
  360. package/build/zipFile.d.ts +0 -32
  361. package/build/zipFileEntry.d.ts +0 -6
  362. package/src/apk.ts +0 -16
  363. package/src/apkParser.test.ts.md +0 -268
  364. package/src/apkParser.test.ts.snap +0 -0
  365. package/src/apkParser.ts +0 -327
  366. package/src/inputCompanion.ts +0 -43
  367. package/src/invariantDefined.ts +0 -6
  368. package/src/invariantIdentity.ts +0 -8
  369. package/src/negativeLookahead.ts +0 -26
  370. /package/build/{apk.js → androidPackage.js} +0 -0
  371. /package/build/{apkParser.test.d.ts → androidPackageParser.test.d.ts} +0 -0
  372. /package/build/{arbitraryDosPermissions.d.ts → androidPackageUnparser.test.d.ts} +0 -0
  373. /package/build/{arbitraryDosPermissions.js → dalvikExecutableParser.test.d.ts} +0 -0
  374. /package/build/{inputChunkBuffer.test.d.ts → dalvikExecutableParserAgainstSmaliParser.test.d.ts} +0 -0
  375. /package/build/{jsonParser2.test.d.ts → elementTerminatedArrayParser.test.d.ts} +0 -0
  376. /package/build/{parserParsingInvariant.d.ts → elementTerminatedSequenceArrayParser.test.d.ts} +0 -0
  377. /package/build/{parserParsingInvariant.js → elementTerminatedSequenceParser.test.d.ts} +0 -0
  378. /package/build/{zipEntry.js → fixedLengthSequenceParser.test.d.ts} +0 -0
  379. /package/build/{zipFile.js → javaKeyStore.d.ts} +0 -0
  380. /package/build/{zipFileEntry.js → javaKeyStore.js} +0 -0
@@ -0,0 +1,104 @@
1
+ import invariant from "invariant";
2
+ import { UnparserContextImplementation, WriteEarlier, WriteLater } from "./unparserContext.js";
3
+ import { unparserImplementationInvariant } from "./unparserImplementationInvariant.js";
4
+ async function* onYield(asyncIterable, onYield) {
5
+ for await (const value of asyncIterable) {
6
+ onYield(value);
7
+ yield value;
8
+ }
9
+ }
10
+ async function* elementsToSequences(values, unparserOutputCompanion) {
11
+ let elements = [];
12
+ for await (const value of values) {
13
+ if (value instanceof WriteLater
14
+ || value instanceof WriteEarlier) {
15
+ if (elements.length > 0) {
16
+ const sequence = unparserOutputCompanion.from(elements);
17
+ yield sequence;
18
+ elements = [];
19
+ }
20
+ yield value;
21
+ }
22
+ else if (unparserOutputCompanion.is(value)) {
23
+ if (unparserOutputCompanion.length(value) === 0) {
24
+ continue;
25
+ }
26
+ if (elements.length > 0) {
27
+ const sequence = unparserOutputCompanion.from(elements);
28
+ yield sequence;
29
+ elements = [];
30
+ }
31
+ yield value;
32
+ }
33
+ else {
34
+ elements.push(value);
35
+ }
36
+ }
37
+ if (elements.length > 0) {
38
+ const sequence = unparserOutputCompanion.from(elements);
39
+ yield sequence;
40
+ }
41
+ }
42
+ function wrapUnparserResult(unparserResult, unparserContext, unparserOutputCompanion) {
43
+ return onYield(elementsToSequences(unparserResult, unparserOutputCompanion), value => unparserContext.handleYield(value));
44
+ }
45
+ export async function* runUnparser(unparser, input, unparserOutputCompanion) {
46
+ const unparserContext = new UnparserContextImplementation(unparserOutputCompanion);
47
+ const values = unparser(input, unparserContext);
48
+ const valuesWithoutElements = wrapUnparserResult(values, unparserContext, unparserOutputCompanion);
49
+ const outputQueue = [];
50
+ const iteratorStack = [
51
+ [
52
+ valuesWithoutElements[Symbol.asyncIterator](),
53
+ value => outputQueue.push(value),
54
+ () => { },
55
+ ],
56
+ ];
57
+ while (true) {
58
+ while (outputQueue.length > 0
59
+ && !(outputQueue[0] instanceof WriteLater)) {
60
+ yield outputQueue.shift();
61
+ }
62
+ if (iteratorStack.length === 0) {
63
+ break;
64
+ }
65
+ const [iterator, pushOutput, finishOutput] = iteratorStack[0];
66
+ const iteratorResult = await iterator.next();
67
+ if (iteratorResult.done) {
68
+ finishOutput();
69
+ iteratorStack.shift();
70
+ continue;
71
+ }
72
+ const { value } = iteratorResult;
73
+ if (value instanceof WriteEarlier) {
74
+ const { writeLater, unparserResult, unparserContext } = value;
75
+ invariant(unparserContext instanceof UnparserContextImplementation, 'UnparserContext not an instance of UnparserContextImplementation.');
76
+ function getOutputQueueWriteLaterIndex() {
77
+ const outputQueueWriteLaterIndex = outputQueue.indexOf(writeLater);
78
+ unparserImplementationInvariant(outputQueueWriteLaterIndex !== -1, [
79
+ 'WriteLater has already been written or was not yielded yet.',
80
+ 'WriteLater stack: %s',
81
+ 'End of WriteLater stack.',
82
+ ], writeLater.stack);
83
+ return outputQueueWriteLaterIndex;
84
+ }
85
+ const unparserResultWithoutElements = wrapUnparserResult(unparserResult, unparserContext, unparserOutputCompanion);
86
+ const newPushOutput = value => {
87
+ const outputQueueWriteLaterIndex = getOutputQueueWriteLaterIndex();
88
+ outputQueue.splice(outputQueueWriteLaterIndex, 0, value);
89
+ };
90
+ const newFinishOutput = () => {
91
+ const outputQueueWriteLaterIndex = getOutputQueueWriteLaterIndex();
92
+ const [removedValue] = outputQueue.splice(outputQueueWriteLaterIndex, 1);
93
+ invariant(removedValue === writeLater, 'WriteLater was not removed from the output queue.');
94
+ };
95
+ iteratorStack.unshift([
96
+ unparserResultWithoutElements[Symbol.asyncIterator](),
97
+ newPushOutput,
98
+ newFinishOutput,
99
+ ]);
100
+ continue;
101
+ }
102
+ pushOutput(value);
103
+ }
104
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,150 @@
1
+ import test from 'ava';
2
+ import * as fc from 'fast-check';
3
+ import { runUnparser } from './unparser.js';
4
+ import { stringUnparserOutputCompanion } from './unparserOutputCompanion.js';
5
+ import { testProp } from '@fast-check/ava';
6
+ test('writeLater', async (t) => {
7
+ const stringToBeWrittenBefore = 'before writeLater\n';
8
+ const stringToBeWrittenLater = 'written later\n';
9
+ const stringToBeWrittenAfter = 'after writeLater\n';
10
+ let expectedPositionA;
11
+ let expectedPositionB;
12
+ let actualPositionAFromWithinWriteLater;
13
+ let actualPositionBFromWithinWriteLater;
14
+ const unparser = async function* (_input, unparserContext) {
15
+ yield stringToBeWrittenBefore;
16
+ expectedPositionA = unparserContext.position;
17
+ const writeLater = yield* unparserContext.writeLater(stringToBeWrittenLater.length);
18
+ expectedPositionB = unparserContext.position;
19
+ yield stringToBeWrittenAfter;
20
+ yield* unparserContext.writeEarlier(writeLater, async function* (_input, unparserContext) {
21
+ actualPositionAFromWithinWriteLater = unparserContext.position;
22
+ yield stringToBeWrittenLater;
23
+ actualPositionBFromWithinWriteLater = unparserContext.position;
24
+ }, undefined);
25
+ };
26
+ const stringsAsyncIterable = runUnparser(unparser, undefined, stringUnparserOutputCompanion);
27
+ const strings = [];
28
+ for await (const string of stringsAsyncIterable) {
29
+ strings.push(string);
30
+ }
31
+ const string = strings.join('');
32
+ t.is(string, stringToBeWrittenBefore + stringToBeWrittenLater + stringToBeWrittenAfter);
33
+ t.is(actualPositionAFromWithinWriteLater, expectedPositionA, 'position a');
34
+ t.is(actualPositionBFromWithinWriteLater, expectedPositionB, 'position b');
35
+ t.is(expectedPositionA, stringToBeWrittenBefore.length);
36
+ t.is(expectedPositionB, stringToBeWrittenBefore.length + stringToBeWrittenLater.length);
37
+ });
38
+ test('writeLater one deep', async (t) => {
39
+ const unparser = async function* (_input, unparserContext) {
40
+ yield '(';
41
+ const writeLaterA = yield* unparserContext.writeLater(3);
42
+ yield ')';
43
+ yield* unparserContext.writeEarlier(writeLaterA, async function* (_input, unparserContext) {
44
+ yield '[';
45
+ const writeLaterB = yield* unparserContext.writeLater(1);
46
+ yield ']';
47
+ yield* unparserContext.writeEarlier(writeLaterB, async function* (_input, unparserContext) {
48
+ yield 'a';
49
+ }, undefined);
50
+ }, undefined);
51
+ };
52
+ const stringsAsyncIterable = runUnparser(unparser, undefined, stringUnparserOutputCompanion);
53
+ const strings = [];
54
+ for await (const string of stringsAsyncIterable) {
55
+ strings.push(string);
56
+ }
57
+ const string = strings.join('');
58
+ t.is(string, '([a])');
59
+ });
60
+ test('writeLater deep', async (t) => {
61
+ const unparser = async function* (_input, unparserContext) {
62
+ yield '(';
63
+ const writeLaterA = yield* unparserContext.writeLater(5);
64
+ yield ')';
65
+ let writeLaterC;
66
+ yield* unparserContext.writeEarlier(writeLaterA, async function* (_input, unparserContext) {
67
+ yield '[';
68
+ const writeLaterB = yield* unparserContext.writeLater(3);
69
+ yield ']';
70
+ yield* unparserContext.writeEarlier(writeLaterB, async function* (_input, unparserContext) {
71
+ yield '{';
72
+ writeLaterC = yield* unparserContext.writeLater(1);
73
+ yield '}';
74
+ }, undefined);
75
+ }, undefined);
76
+ yield* unparserContext.writeEarlier(writeLaterC, async function* (_input, unparserContext) {
77
+ yield 'a';
78
+ }, undefined);
79
+ yield 'z';
80
+ };
81
+ const stringsAsyncIterable = runUnparser(unparser, undefined, stringUnparserOutputCompanion);
82
+ const strings = [];
83
+ for await (const string of stringsAsyncIterable) {
84
+ strings.push(string);
85
+ }
86
+ const string = strings.join('');
87
+ t.is(string, '([{a}])z');
88
+ });
89
+ testProp('writeLater positions and lengths', [
90
+ fc.array(fc.oneof(fc.array(fc.oneof(fc.array(fc.string()), fc.string())), fc.string())),
91
+ ], async (t, stringArray3) => {
92
+ const numberUnparser = async function* (input, unparserContext) {
93
+ yield input.toString().padStart(8, '0');
94
+ };
95
+ const createLengthPrefixedUnparser = (unparser) => {
96
+ return async function* (input, unparserContext) {
97
+ const length = yield* unparserContext.writeLater(8);
98
+ yield* unparser(input, unparserContext);
99
+ yield* unparserContext.writeEarlier(length, numberUnparser, unparserContext.position - length.positionEnd);
100
+ };
101
+ };
102
+ const unparser0 = createLengthPrefixedUnparser(async function* (input, unparserContext) {
103
+ yield input;
104
+ });
105
+ const unparser1 = createLengthPrefixedUnparser(async function* (input, unparserContext) {
106
+ for (const string of input) {
107
+ yield* unparser0(string, unparserContext);
108
+ }
109
+ });
110
+ const unparser2 = createLengthPrefixedUnparser(async function* (input, unparserContext) {
111
+ for (const stringArray2 of input) {
112
+ if (typeof stringArray2 === 'string') {
113
+ yield* unparser0(stringArray2, unparserContext);
114
+ }
115
+ else {
116
+ yield* unparser1(stringArray2, unparserContext);
117
+ }
118
+ }
119
+ });
120
+ const unparser3 = createLengthPrefixedUnparser(async function* (input, unparserContext) {
121
+ for (const stringArray3 of input) {
122
+ if (typeof stringArray3 === 'string') {
123
+ yield* unparser0(stringArray3, unparserContext);
124
+ }
125
+ else {
126
+ yield* unparser2(stringArray3, unparserContext);
127
+ }
128
+ }
129
+ });
130
+ const stringsAsyncIterable = runUnparser(unparser3, stringArray3, stringUnparserOutputCompanion);
131
+ const strings = [];
132
+ for await (const string of stringsAsyncIterable) {
133
+ strings.push(string);
134
+ }
135
+ const actualString = strings.join('');
136
+ function getExpectedString(stringArray) {
137
+ let expectedString = stringArray;
138
+ if (typeof stringArray !== 'string') {
139
+ expectedString = stringArray.map(getExpectedString).join('');
140
+ }
141
+ return [
142
+ expectedString.length.toString().padStart(8, '0'),
143
+ expectedString,
144
+ ].join('');
145
+ }
146
+ const expectedString = getExpectedString(stringArray3);
147
+ t.deepEqual(actualString, expectedString);
148
+ }, {
149
+ verbose: true,
150
+ });
@@ -0,0 +1,31 @@
1
+ import { Unparser, UnparserResult } from "./unparser.js";
2
+ import { UnparserOutputCompanion } from "./unparserOutputCompanion.js";
3
+ export type UnparserContext<Sequence, Element> = {
4
+ get position(): number;
5
+ writeLater(length: number): AsyncIterable<WriteLater<Sequence, Element>, WriteLater<Sequence, Element>>;
6
+ writeEarlier<Input>(writeLater: WriteLater<Sequence, Element>, unparser: Unparser<Input, Sequence, Element>, input: Input): AsyncIterable<WriteEarlier<Sequence, Element>, void>;
7
+ };
8
+ export declare class WriteLater<Sequence, Element> extends Error {
9
+ private readonly _position;
10
+ private readonly _length;
11
+ name: string;
12
+ constructor(_position: number, _length: number);
13
+ get position(): number;
14
+ get positionEnd(): number;
15
+ get length(): number;
16
+ }
17
+ export declare class WriteEarlier<Sequence, Element> {
18
+ readonly writeLater: WriteLater<Sequence, Element>;
19
+ readonly unparserResult: UnparserResult<Sequence, Element>;
20
+ readonly unparserContext: UnparserContext<Sequence, Element>;
21
+ constructor(writeLater: WriteLater<Sequence, Element>, unparserResult: UnparserResult<Sequence, Element>, unparserContext: UnparserContext<Sequence, Element>);
22
+ }
23
+ export declare class UnparserContextImplementation<Sequence, Element> implements UnparserContext<Sequence, Element> {
24
+ private readonly _outputCompanion;
25
+ private _position;
26
+ constructor(_outputCompanion: UnparserOutputCompanion<Sequence, Element>, position?: number);
27
+ get position(): number;
28
+ writeLater(length: number): AsyncIterable<WriteLater<Sequence, Element>, WriteLater<Sequence, Element>>;
29
+ writeEarlier<Input>(writeLater: WriteLater<Sequence, Element>, unparser: Unparser<Input, Sequence, Element>, input: Input): AsyncIterable<WriteEarlier<Sequence, Element>, void>;
30
+ handleYield(value: Sequence | WriteLater<Sequence, Element> | WriteEarlier<Sequence, Element>): void;
31
+ }
@@ -0,0 +1,74 @@
1
+ /* eslint-disable prefer-arrow-callback */
2
+ import { unparserImplementationInvariant } from "./unparserImplementationInvariant.js";
3
+ import { parserImplementationInvariant } from "./parserImplementationInvariant.js";
4
+ export class WriteLater extends Error {
5
+ _position;
6
+ _length;
7
+ name = 'WriteLater';
8
+ constructor(_position, _length) {
9
+ super(`(position: ${_position}, length: ${_length})`);
10
+ this._position = _position;
11
+ this._length = _length;
12
+ }
13
+ get position() {
14
+ return this._position;
15
+ }
16
+ get positionEnd() {
17
+ return this._position + this._length;
18
+ }
19
+ get length() {
20
+ return this._length;
21
+ }
22
+ }
23
+ export class WriteEarlier {
24
+ writeLater;
25
+ unparserResult;
26
+ unparserContext;
27
+ constructor(writeLater, unparserResult, unparserContext) {
28
+ this.writeLater = writeLater;
29
+ this.unparserResult = unparserResult;
30
+ this.unparserContext = unparserContext;
31
+ }
32
+ }
33
+ export class UnparserContextImplementation {
34
+ _outputCompanion;
35
+ _position;
36
+ constructor(_outputCompanion, position = 0) {
37
+ this._outputCompanion = _outputCompanion;
38
+ this._position = position;
39
+ }
40
+ get position() {
41
+ return this._position;
42
+ }
43
+ async *writeLater(length) {
44
+ parserImplementationInvariant(length >= 0, 'Length of WriteLater must be non-negative, got %s.', length);
45
+ const writeLater = new WriteLater(this._position, length);
46
+ yield writeLater;
47
+ return writeLater;
48
+ }
49
+ async *writeEarlier(writeLater, unparser, input) {
50
+ const unparserContext = new UnparserContextImplementation(this._outputCompanion, writeLater.position);
51
+ yield new WriteEarlier(writeLater, (async function* () {
52
+ const startPosition = unparserContext.position;
53
+ yield* unparser(input, unparserContext);
54
+ const endPosition = unparserContext.position;
55
+ const writtenLength = endPosition - startPosition;
56
+ unparserImplementationInvariant(writtenLength === writeLater.length, [
57
+ 'WriteEarlier was supposed to write %s elements but wrote %s elements instead.',
58
+ 'Corresponding WriteLater stack (this is where it was created): %s',
59
+ 'End of corresponding WriteEarlier stack.',
60
+ ], writeLater.length, writtenLength, writeLater.stack);
61
+ })(), unparserContext);
62
+ }
63
+ handleYield(value) {
64
+ if (value instanceof WriteEarlier) {
65
+ return;
66
+ }
67
+ if (value instanceof WriteLater) {
68
+ this._position += value.length;
69
+ return;
70
+ }
71
+ const length = this._outputCompanion.length(value);
72
+ this._position += length;
73
+ }
74
+ }
@@ -0,0 +1,9 @@
1
+ export declare class UnparserError extends Error {
2
+ name: string;
3
+ }
4
+ export declare class UnparserImplementationError extends UnparserError {
5
+ name: string;
6
+ }
7
+ export declare class UnparserImplementationInvariantError extends UnparserImplementationError {
8
+ name: string;
9
+ }
@@ -0,0 +1,9 @@
1
+ export class UnparserError extends Error {
2
+ name = 'UnparserError';
3
+ }
4
+ export class UnparserImplementationError extends UnparserError {
5
+ name = 'UnparserImplementationError';
6
+ }
7
+ export class UnparserImplementationInvariantError extends UnparserImplementationError {
8
+ name = 'UnparserImplementationInvariantError';
9
+ }
@@ -0,0 +1,2 @@
1
+ import { type Falsy, type ValueOrAccessor } from './customInvariant.js';
2
+ export declare function unparserImplementationInvariant<T>(value: T, format: ValueOrAccessor<string | string[]>, ...formatArguments: any[]): Exclude<T, Falsy>;
@@ -0,0 +1,5 @@
1
+ import { UnparserImplementationInvariantError } from './unparserError.js';
2
+ import { customInvariant } from './customInvariant.js';
3
+ export function unparserImplementationInvariant(value, format, ...formatArguments) {
4
+ return customInvariant(UnparserImplementationInvariantError, value, format, ...formatArguments);
5
+ }
@@ -0,0 +1,15 @@
1
+ import { StringParserInputCompanion, Uint8ArrayParserInputCompanion } from "./parserInputCompanion.js";
2
+ export type UnparserOutputCompanion<Sequence, Element> = {
3
+ is(value: unknown): value is Sequence;
4
+ from(elements: Element[]): Sequence;
5
+ concat(sequences: Sequence[]): Sequence;
6
+ length(sequence: Sequence): number;
7
+ };
8
+ export declare class StringUnparserOutputCompanion extends StringParserInputCompanion implements UnparserOutputCompanion<string, string> {
9
+ concat(sequences: string[]): string;
10
+ }
11
+ export declare const stringUnparserOutputCompanion: StringUnparserOutputCompanion;
12
+ export declare class Uint8ArrayUnparserOutputCompanion extends Uint8ArrayParserInputCompanion implements UnparserOutputCompanion<Uint8Array, number> {
13
+ concat(sequences: Uint8Array[]): Uint8Array;
14
+ }
15
+ export declare const uint8ArrayUnparserOutputCompanion: Uint8ArrayUnparserOutputCompanion;
@@ -0,0 +1,13 @@
1
+ import { StringParserInputCompanion, Uint8ArrayParserInputCompanion } from "./parserInputCompanion.js";
2
+ export class StringUnparserOutputCompanion extends StringParserInputCompanion {
3
+ concat(sequences) {
4
+ return sequences.join('');
5
+ }
6
+ }
7
+ export const stringUnparserOutputCompanion = new StringUnparserOutputCompanion();
8
+ export class Uint8ArrayUnparserOutputCompanion extends Uint8ArrayParserInputCompanion {
9
+ concat(sequences) {
10
+ return Buffer.concat(sequences);
11
+ }
12
+ }
13
+ export const uint8ArrayUnparserOutputCompanion = new Uint8ArrayUnparserOutputCompanion();
package/build/zip.d.ts CHANGED
@@ -1,31 +1,23 @@
1
1
  export type ZipCompression = 'store' | 'deflate';
2
- export type ZipUnixPermissions = {
3
- type: 'unix';
4
- unixPermissions: number;
5
- };
6
- export type ZipDosPermissions = {
7
- type: 'dos';
8
- dosPermissions: number;
9
- };
10
- export type ZipPermissions = ZipUnixPermissions | ZipDosPermissions;
11
- export type ZipFileEntry = {
12
- type: 'file';
2
+ export type ZipEntryAttributes = {};
3
+ type ZipEntryCommon = {
13
4
  path: string;
14
5
  date: Date;
15
6
  comment: string;
16
- permissions: ZipPermissions;
7
+ hostSystem: 'unix' | 'dos';
8
+ attributes: ZipEntryAttributes;
9
+ };
10
+ export type ZipFileEntry = ZipEntryCommon & {
11
+ type: 'file';
17
12
  compression: ZipCompression;
18
13
  content: Uint8Array;
19
14
  };
20
- export type ZipDirectoryEntry = {
15
+ export type ZipDirectoryEntry = ZipEntryCommon & {
21
16
  type: 'directory';
22
- path: string;
23
- date: Date;
24
- comment: string;
25
- permissions: ZipPermissions;
26
17
  };
27
18
  export type ZipEntry = ZipFileEntry | ZipDirectoryEntry;
28
19
  export type Zip = {
29
20
  comment: string;
30
21
  entries: ZipEntry[];
31
22
  };
23
+ export {};
@@ -1,21 +1,26 @@
1
- import { Parser } from "./parser.js";
2
- import { Zip, ZipCompression, ZipEntry } from "./zip.js";
3
- type ZipLocalFileHeader = {
1
+ import { type Parser } from './parser.js';
2
+ import { type Zip, type ZipCompression, type ZipEntry } from './zip.js';
3
+ export type ZipLocalFileHeader = {
4
4
  versionNeededToExtract: number;
5
5
  generalPurposeBitFlag: number;
6
6
  compressionMethod: ZipCompression;
7
- lastModFile: Date;
7
+ lastModifiedFile: Date;
8
8
  crc32: number;
9
9
  compressedSize: number;
10
10
  uncompressedSize: number;
11
- fileName: string;
11
+ filePath: string;
12
12
  extraField: Uint8Array;
13
13
  };
14
- type ZipLocalFile = {
14
+ type ZipDataDescriptor = {
15
+ crc32: number;
16
+ compressedSize: number;
17
+ uncompressedSize: number;
18
+ };
19
+ export type ZipLocalFile = {
15
20
  zipLocalFileHeader: ZipLocalFileHeader;
16
21
  zipEncryptionHeader: unknown;
17
22
  compressedData: Uint8Array;
18
- zipDataDescriptor: unknown;
23
+ zipDataDescriptor: undefined | ZipDataDescriptor;
19
24
  };
20
25
  export declare const zipLocalFileParser: Parser<ZipLocalFile, Uint8Array>;
21
26
  export declare const zipArchiveDecryptionHeaderParser: Parser<unknown, Uint8Array>;
@@ -24,27 +29,30 @@ type ZipVersionMadeBy = {
24
29
  hostSystem: number;
25
30
  zipSpecificationVersion: number;
26
31
  };
27
- type ZipCentralDirectoryHeader = {
32
+ type ZipExternalFileAttributes = {
33
+ directory: boolean;
34
+ };
35
+ export type ZipCentralDirectoryHeader = {
28
36
  versionMadeBy: ZipVersionMadeBy;
29
37
  versionNeededToExtract: number;
30
38
  generalPurposeBitFlag: number;
31
39
  compressionMethod: ZipCompression;
32
- lastModFile: Date;
40
+ lastModifiedFile: Date;
33
41
  crc32: number;
34
42
  compressedSize: number;
35
43
  uncompressedSize: number;
36
44
  diskNumberStart: number;
37
45
  internalFileAttributes: number;
38
- externalFileAttributes: number;
46
+ externalFileAttributes: ZipExternalFileAttributes;
39
47
  relativeOffsetOfLocalHeader: number;
40
- fileName: string;
48
+ filePath: string;
41
49
  extraField: Uint8Array;
42
50
  fileComment: string;
43
51
  };
44
52
  export declare const zipCentralDirectoryHeaderParser: Parser<ZipCentralDirectoryHeader, Uint8Array>;
45
53
  export declare const zip64EndOfCentralDirectoryRecordParser: Parser<unknown, Uint8Array>;
46
54
  export declare const zip64EndOfCentralDirectoryLocatorParser: Parser<unknown, Uint8Array>;
47
- type ZipEndOfCentralDirectoryRecord = {
55
+ export type ZipEndOfCentralDirectoryRecord = {
48
56
  numberOfThisDisk: number;
49
57
  numberOfTheDiskWithTheStartOfTheCentralDirectory: number;
50
58
  totalNumberOfEntriesInTheCentralDirectoryOnThisDisk: number;