@wener/utils 1.1.51 → 1.1.54

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 (497) hide show
  1. package/lib/arrays/MaybeArray.js +58 -8
  2. package/lib/arrays/arrayFromAsync.js +254 -6
  3. package/lib/asyncs/AsyncInterval.js +145 -9
  4. package/lib/asyncs/MaybePromise.js +0 -2
  5. package/lib/asyncs/Promises.js +95 -48
  6. package/lib/asyncs/createAsyncIterator.js +313 -23
  7. package/lib/asyncs/createLazyPromise.js +72 -31
  8. package/lib/asyncs/createLazyPromise.test.js +367 -0
  9. package/lib/asyncs/firstOfAsyncIterator.js +6 -6
  10. package/lib/asyncs/generatorOfStream.js +125 -14
  11. package/lib/asyncs/isIterator.js +1 -3
  12. package/lib/asyncs/isPromise.js +2 -4
  13. package/lib/asyncs/isThenable.js +0 -2
  14. package/lib/asyncs/nextOfAsyncIterator.js +15 -9
  15. package/lib/asyncs/promiseOfCallback.js +2 -4
  16. package/lib/asyncs/timeout.js +122 -13
  17. package/lib/browsers/copy.js +7 -8
  18. package/lib/browsers/download.js +215 -40
  19. package/lib/browsers/getFileFromDataTransfer.js +17 -14
  20. package/lib/browsers/loaders.js +251 -18
  21. package/lib/crypto/base.js +3 -3
  22. package/lib/crypto/getNodeCrypto.js +3 -5
  23. package/lib/crypto/hashing.js +236 -30
  24. package/lib/crypto/hashing.test.js +323 -0
  25. package/lib/crypto/md5.bench.js +11 -11
  26. package/lib/crypto/md5.d.js +0 -2
  27. package/lib/crypto/md5.js +0 -2
  28. package/lib/crypto/md5.test.js +7 -0
  29. package/lib/crypto/pem/pem.js +197 -35
  30. package/lib/crypto/pem/pem.test.js +104 -0
  31. package/lib/crypto/randomUUIDv7.js +0 -2
  32. package/lib/crypto/ulid.js +84 -48
  33. package/lib/crypto/ulid.test.js +115 -0
  34. package/lib/emitter/types.js +0 -2
  35. package/lib/errors/DetailError.js +297 -0
  36. package/lib/errors/Errors.js +136 -137
  37. package/lib/errors/Errors.test.js +24 -0
  38. package/lib/fetch/HttpStatus.js +1 -3
  39. package/lib/fetch/createFetchWith.js +188 -19
  40. package/lib/fetch/createFetchWithLogging.js +17 -16
  41. package/lib/fetch/createFetchWithRetry.js +40 -24
  42. package/lib/fetch/dumpRequest.js +467 -41
  43. package/lib/fetch/dumpRequest.test.js +384 -0
  44. package/lib/fetch/dumpResponse.js +271 -16
  45. package/lib/fetch/dumpResponse.test.js +393 -0
  46. package/lib/fetch/http.types.js +0 -2
  47. package/lib/fetch/index.js +5 -7
  48. package/lib/fetch/isTextContentType.js +5 -0
  49. package/lib/fetch/resolveRequest.js +7 -0
  50. package/lib/fetch/types.js +0 -2
  51. package/lib/i18n/createTranslate.js +40 -20
  52. package/lib/i18n/createTranslate.test.js +177 -0
  53. package/lib/index.js +75 -76
  54. package/lib/io/AbstractEncoding.js +0 -2
  55. package/lib/io/ArrayBuffer.test-d.js +4 -6
  56. package/lib/io/ArrayBuffers.base64.test.js +60 -0
  57. package/lib/io/ArrayBuffers.js +396 -171
  58. package/lib/io/ArrayBuffers.test.js +77 -0
  59. package/lib/io/Buffer.js +190 -20
  60. package/lib/io/Buffer.test.js +22 -0
  61. package/lib/io/ByteBuffer.js +752 -441
  62. package/lib/io/ByteBuffer.test.js +184 -0
  63. package/lib/io/base64.js +17 -19
  64. package/lib/io/dump.js +16 -16
  65. package/lib/io/isBuffer.js +2 -3
  66. package/lib/io/isBuffer.test.js +8 -0
  67. package/lib/io/isTransferable.js +16 -8
  68. package/lib/io/isTransferable.test.js +17 -0
  69. package/lib/io/parseDataUri.js +288 -40
  70. package/lib/io/parseDataUri.test.js +286 -0
  71. package/lib/io/types.js +1 -0
  72. package/lib/langs/AsyncCloser.js +270 -34
  73. package/lib/langs/Closer.js +95 -29
  74. package/lib/langs/MaybeFunction.js +38 -5
  75. package/lib/langs/classOf.js +0 -2
  76. package/lib/langs/deepEqual.js +82 -22
  77. package/lib/langs/deepEqual.test.js +11 -0
  78. package/lib/langs/deepFreeze.js +28 -9
  79. package/lib/langs/getGlobalStates.js +14 -10
  80. package/lib/langs/getObjectId.js +4 -6
  81. package/lib/langs/ifPresent.js +0 -2
  82. package/lib/langs/isClass.js +0 -2
  83. package/lib/langs/isDefined.js +0 -2
  84. package/lib/langs/isEmptyObject.js +1 -3
  85. package/lib/langs/isFunction.js +0 -2
  86. package/lib/langs/isNil.js +3 -0
  87. package/lib/langs/isPlainObject.js +2 -4
  88. package/lib/langs/langs.test.js +126 -0
  89. package/lib/langs/memoize.js +11 -7
  90. package/lib/langs/mixin.js +7 -4
  91. package/lib/langs/mixin.test.js +135 -0
  92. package/lib/langs/parseBoolean.js +11 -8
  93. package/lib/langs/parseDate.js +20 -0
  94. package/lib/langs/shallowClone.js +5 -3
  95. package/lib/langs/shallowEqual.js +8 -6
  96. package/lib/libs/ms.js +28 -26
  97. package/lib/libs/ms.test.js +301 -0
  98. package/lib/logger/const.js +15 -0
  99. package/lib/logger/index.js +1 -0
  100. package/lib/logger/types.js +1 -0
  101. package/lib/maths/clamp.js +81 -12
  102. package/lib/maths/clamp.test.js +186 -0
  103. package/lib/maths/createRandom.test.js +36 -0
  104. package/lib/maths/random.js +162 -13
  105. package/lib/modules/isModule.js +0 -2
  106. package/lib/modules/parseModuleId.js +14 -15
  107. package/lib/modules/parseModuleId.test.js +153 -0
  108. package/lib/objects/computeIfAbsent.js +8 -3
  109. package/lib/objects/get.js +26 -8
  110. package/lib/objects/get.test-d.js +23 -25
  111. package/lib/objects/merge/index.js +1 -3
  112. package/lib/objects/merge/isMergeableObject.js +5 -3
  113. package/lib/objects/merge/merge.js +82 -26
  114. package/lib/objects/merge/merge.test.js +1397 -0
  115. package/lib/objects/parseObjectPath.js +47 -15
  116. package/lib/objects/parseObjectPath.test.js +138 -0
  117. package/lib/objects/set.js +25 -19
  118. package/lib/objects/set.test.js +522 -0
  119. package/lib/schema/typebox/index.js +2 -3
  120. package/lib/schema/typebox/typebox.js +6 -6
  121. package/lib/scripts/getGenerateContext.js +386 -77
  122. package/lib/server/crypto/md5.js +0 -2
  123. package/lib/server/fetch/createFetchWithProxy.js +2 -4
  124. package/lib/server/fetch/createFetchWithProxyByNodeFetch.js +258 -31
  125. package/lib/server/fetch/createFetchWithProxyByUndici.js +307 -49
  126. package/lib/server/getPackageDir.js +2 -3
  127. package/lib/server/index.js +5 -7
  128. package/lib/server/jsdom.js +1 -3
  129. package/lib/server/node-fetch.js +2 -4
  130. package/lib/server/polyfill/polyfillBrowser.js +163 -9
  131. package/lib/server/polyfill/polyfillBrowser.test.js +164 -0
  132. package/lib/server/polyfill/polyfillCrypto.js +157 -9
  133. package/lib/server/polyfill/polyfillFetch.js +13 -13
  134. package/lib/server/polyfill/polyfillJsDom.js +264 -58
  135. package/lib/server/polyfill/polyfillWebSocket.js +11 -11
  136. package/lib/server/ws.js +1 -3
  137. package/lib/strings/bytes.test.js +327 -0
  138. package/lib/strings/camelCase.js +23 -14
  139. package/lib/strings/formatBytes.js +29 -20
  140. package/lib/strings/parseBytes.js +54 -0
  141. package/lib/strings/renderTemplate.js +17 -12
  142. package/lib/strings/renderTemplate.test.js +104 -0
  143. package/lib/typedoc.js +2 -4
  144. package/lib/types.d.js +0 -2
  145. package/lib/validations/asserts.js +0 -2
  146. package/lib/validations/isUUID.js +0 -2
  147. package/lib/validations/isUUID.test.js +87 -0
  148. package/lib/validations/parseTimestamp.js +12 -7
  149. package/lib/validations/parseTimestamp.test.js +6 -0
  150. package/lib/web/getGlobalThis.js +3 -4
  151. package/lib/web/getRandomValues.js +28 -20
  152. package/lib/web/randomUUID.js +7 -8
  153. package/lib/web/requestIdleCallback.js +13 -9
  154. package/lib/web/structuredClone.js +35 -28
  155. package/lib/web/structuredClone.test.js +81 -0
  156. package/package.json +9 -14
  157. package/src/arrays/MaybeArray.ts +22 -22
  158. package/src/arrays/arrayFromAsync.ts +3 -3
  159. package/src/asyncs/AsyncInterval.ts +8 -8
  160. package/src/asyncs/Promises.ts +51 -51
  161. package/src/asyncs/createAsyncIterator.ts +33 -33
  162. package/src/asyncs/createLazyPromise.test.ts +73 -73
  163. package/src/asyncs/createLazyPromise.ts +68 -73
  164. package/src/asyncs/firstOfAsyncIterator.ts +5 -5
  165. package/src/asyncs/generatorOfStream.ts +12 -12
  166. package/src/asyncs/isIterator.ts +1 -1
  167. package/src/asyncs/isThenable.ts +2 -2
  168. package/src/asyncs/nextOfAsyncIterator.ts +20 -20
  169. package/src/asyncs/promiseOfCallback.ts +13 -13
  170. package/src/asyncs/timeout.ts +23 -23
  171. package/src/browsers/copy.ts +48 -48
  172. package/src/browsers/download.ts +38 -36
  173. package/src/browsers/getFileFromDataTransfer.ts +35 -35
  174. package/src/browsers/loaders.ts +49 -49
  175. package/src/crypto/base.ts +3 -3
  176. package/src/crypto/getNodeCrypto.ts +2 -2
  177. package/src/crypto/hashing.test.ts +33 -47
  178. package/src/crypto/hashing.ts +67 -67
  179. package/src/crypto/md5.bench.ts +20 -24
  180. package/src/crypto/md5.test.ts +3 -3
  181. package/src/crypto/pem/pem.test.ts +26 -42
  182. package/src/crypto/pem/pem.ts +65 -65
  183. package/src/crypto/ulid.test.ts +31 -31
  184. package/src/crypto/ulid.ts +116 -116
  185. package/src/emitter/types.ts +6 -6
  186. package/src/errors/DetailError.ts +151 -0
  187. package/src/errors/Errors.test.ts +15 -15
  188. package/src/errors/Errors.ts +97 -238
  189. package/src/fetch/HttpStatus.ts +77 -77
  190. package/src/fetch/createFetchWith.ts +25 -25
  191. package/src/fetch/createFetchWithLogging.ts +12 -15
  192. package/src/fetch/createFetchWithRetry.ts +79 -79
  193. package/src/fetch/dumpRequest.test.ts +176 -0
  194. package/src/fetch/dumpRequest.ts +149 -52
  195. package/src/fetch/dumpResponse.test.ts +160 -0
  196. package/src/fetch/dumpResponse.ts +48 -20
  197. package/src/fetch/http.types.ts +35 -35
  198. package/src/fetch/isTextContentType.ts +12 -0
  199. package/src/fetch/resolveRequest.ts +1 -0
  200. package/src/fetch/types.ts +2 -2
  201. package/src/i18n/createTranslate.test.ts +90 -113
  202. package/src/i18n/createTranslate.ts +55 -55
  203. package/src/index.ts +26 -26
  204. package/src/io/AbstractEncoding.ts +12 -12
  205. package/src/io/ArrayBuffer.test-d.ts +1 -1
  206. package/src/io/ArrayBuffers.base64.test.ts +52 -52
  207. package/src/io/ArrayBuffers.test.ts +14 -14
  208. package/src/io/ArrayBuffers.ts +645 -415
  209. package/src/io/Buffer.test.ts +17 -17
  210. package/src/io/Buffer.ts +17 -17
  211. package/src/io/ByteBuffer.test.ts +37 -37
  212. package/src/io/ByteBuffer.ts +574 -566
  213. package/src/io/base64.ts +46 -44
  214. package/src/io/dump.ts +22 -22
  215. package/src/io/isBuffer.test.ts +2 -2
  216. package/src/io/isBuffer.ts +1 -1
  217. package/src/io/isTransferable.test.ts +4 -4
  218. package/src/io/isTransferable.ts +29 -29
  219. package/src/io/parseDataUri.test.ts +22 -27
  220. package/src/io/parseDataUri.ts +57 -57
  221. package/src/io/types.ts +13 -0
  222. package/src/langs/AsyncCloser.ts +34 -34
  223. package/src/langs/Closer.ts +28 -28
  224. package/src/langs/MaybeFunction.ts +5 -5
  225. package/src/langs/classOf.ts +1 -1
  226. package/src/langs/deepEqual.test.ts +1 -12
  227. package/src/langs/deepEqual.ts +82 -82
  228. package/src/langs/deepFreeze.ts +11 -11
  229. package/src/langs/getGlobalStates.ts +16 -16
  230. package/src/langs/getObjectId.ts +10 -13
  231. package/src/langs/ifPresent.ts +2 -2
  232. package/src/langs/isClass.ts +1 -1
  233. package/src/langs/isDefined.ts +1 -1
  234. package/src/langs/isEmptyObject.ts +1 -1
  235. package/src/langs/isFunction.ts +1 -1
  236. package/src/langs/isNil.ts +3 -0
  237. package/src/langs/isPlainObject.ts +4 -4
  238. package/src/langs/langs.test.ts +18 -18
  239. package/src/langs/memoize.ts +21 -21
  240. package/src/langs/mixin.test.ts +22 -26
  241. package/src/langs/mixin.ts +6 -9
  242. package/src/langs/parseBoolean.ts +30 -26
  243. package/src/langs/parseDate.ts +13 -0
  244. package/src/langs/shallowClone.ts +11 -11
  245. package/src/langs/shallowEqual.ts +19 -19
  246. package/src/libs/ms.test.ts +274 -274
  247. package/src/libs/ms.ts +137 -137
  248. package/src/logger/const.ts +18 -0
  249. package/src/logger/index.ts +2 -0
  250. package/src/logger/types.ts +10 -0
  251. package/src/maths/clamp.test.ts +18 -18
  252. package/src/maths/clamp.ts +17 -17
  253. package/src/maths/createRandom.test.ts +12 -0
  254. package/src/maths/random.ts +50 -14
  255. package/src/modules/isModule.ts +4 -4
  256. package/src/modules/parseModuleId.test.ts +61 -68
  257. package/src/modules/parseModuleId.ts +29 -43
  258. package/src/objects/computeIfAbsent.ts +10 -10
  259. package/src/objects/get.test-d.ts +33 -51
  260. package/src/objects/get.ts +31 -31
  261. package/src/objects/merge/isMergeableObject.ts +5 -5
  262. package/src/objects/merge/merge.test.ts +658 -880
  263. package/src/objects/merge/merge.ts +84 -84
  264. package/src/objects/parseObjectPath.test.ts +13 -13
  265. package/src/objects/parseObjectPath.ts +25 -25
  266. package/src/objects/set.test.ts +271 -366
  267. package/src/objects/set.ts +36 -36
  268. package/src/schema/typebox/typebox.ts +20 -26
  269. package/src/scripts/getGenerateContext.ts +79 -79
  270. package/src/server/crypto/md5.ts +1 -1
  271. package/src/server/fetch/createFetchWithProxyByNodeFetch.ts +28 -31
  272. package/src/server/fetch/createFetchWithProxyByUndici.ts +63 -69
  273. package/src/server/getPackageDir.ts +7 -7
  274. package/src/server/polyfill/polyfillBrowser.test.ts +10 -10
  275. package/src/server/polyfill/polyfillBrowser.ts +3 -3
  276. package/src/server/polyfill/polyfillCrypto.ts +6 -6
  277. package/src/server/polyfill/polyfillFetch.ts +11 -21
  278. package/src/server/polyfill/polyfillJsDom.ts +59 -71
  279. package/src/server/polyfill/polyfillWebSocket.ts +14 -14
  280. package/src/strings/bytes.test.ts +69 -0
  281. package/src/strings/camelCase.ts +58 -63
  282. package/src/strings/formatBytes.ts +50 -29
  283. package/src/strings/parseBytes.ts +66 -0
  284. package/src/strings/renderTemplate.test.ts +10 -17
  285. package/src/strings/renderTemplate.ts +19 -22
  286. package/src/types.d.ts +9 -9
  287. package/src/validations/asserts.ts +8 -8
  288. package/src/validations/isUUID.test.ts +8 -8
  289. package/src/validations/isUUID.ts +1 -1
  290. package/src/validations/parseTimestamp.test.ts +2 -2
  291. package/src/validations/parseTimestamp.ts +21 -21
  292. package/src/web/getGlobalThis.ts +6 -6
  293. package/src/web/getRandomValues.ts +29 -29
  294. package/src/web/randomUUID.ts +5 -5
  295. package/src/web/requestIdleCallback.ts +7 -10
  296. package/src/web/structuredClone.test.ts +8 -8
  297. package/src/web/structuredClone.ts +62 -62
  298. package/tsconfig.json +26 -34
  299. package/lib/arrays/MaybeArray.js.map +0 -1
  300. package/lib/arrays/arrayFromAsync.js.map +0 -1
  301. package/lib/asyncs/AsyncInterval.js.map +0 -1
  302. package/lib/asyncs/MaybePromise.js.map +0 -1
  303. package/lib/asyncs/Promises.js.map +0 -1
  304. package/lib/asyncs/createAsyncIterator.js.map +0 -1
  305. package/lib/asyncs/createLazyPromise.js.map +0 -1
  306. package/lib/asyncs/firstOfAsyncIterator.js.map +0 -1
  307. package/lib/asyncs/generatorOfStream.js.map +0 -1
  308. package/lib/asyncs/isIterator.js.map +0 -1
  309. package/lib/asyncs/isPromise.js.map +0 -1
  310. package/lib/asyncs/isThenable.js.map +0 -1
  311. package/lib/asyncs/nextOfAsyncIterator.js.map +0 -1
  312. package/lib/asyncs/promiseOfCallback.js.map +0 -1
  313. package/lib/asyncs/timeout.js.map +0 -1
  314. package/lib/browsers/copy.js.map +0 -1
  315. package/lib/browsers/download.js.map +0 -1
  316. package/lib/browsers/getFileFromDataTransfer.js.map +0 -1
  317. package/lib/browsers/loaders.js.map +0 -1
  318. package/lib/cn/division/DivisionCode.js +0 -215
  319. package/lib/cn/division/DivisionCode.js.map +0 -1
  320. package/lib/cn/division/binarySearch.js +0 -27
  321. package/lib/cn/division/binarySearch.js.map +0 -1
  322. package/lib/cn/formatChineseAmount.js +0 -77
  323. package/lib/cn/formatChineseAmount.js.map +0 -1
  324. package/lib/cn/id/Mod11.js +0 -43
  325. package/lib/cn/id/Mod11.js.map +0 -1
  326. package/lib/cn/id/ResidentIdNumber.js +0 -98
  327. package/lib/cn/id/ResidentIdNumber.js.map +0 -1
  328. package/lib/cn/id/types.js +0 -8
  329. package/lib/cn/id/types.js.map +0 -1
  330. package/lib/cn/index.js +0 -14
  331. package/lib/cn/index.js.map +0 -1
  332. package/lib/cn/parseChineseNumber.js +0 -94
  333. package/lib/cn/parseChineseNumber.js.map +0 -1
  334. package/lib/cn/pinyin/cartesianProduct.js +0 -22
  335. package/lib/cn/pinyin/cartesianProduct.js.map +0 -1
  336. package/lib/cn/pinyin/data.json +0 -413
  337. package/lib/cn/pinyin/loader.js +0 -15
  338. package/lib/cn/pinyin/loader.js.map +0 -1
  339. package/lib/cn/pinyin/preload.js +0 -4
  340. package/lib/cn/pinyin/preload.js.map +0 -1
  341. package/lib/cn/pinyin/toPinyinPure.js +0 -32
  342. package/lib/cn/pinyin/toPinyinPure.js.map +0 -1
  343. package/lib/cn/pinyin/transform.js +0 -14
  344. package/lib/cn/pinyin/transform.js.map +0 -1
  345. package/lib/cn/types.js +0 -3
  346. package/lib/cn/types.js.map +0 -1
  347. package/lib/cn/uscc/Mod31.js +0 -49
  348. package/lib/cn/uscc/Mod31.js.map +0 -1
  349. package/lib/cn/uscc/USCC.js +0 -94
  350. package/lib/cn/uscc/USCC.js.map +0 -1
  351. package/lib/cn/uscc/isUSCC.js +0 -8
  352. package/lib/cn/uscc/isUSCC.js.map +0 -1
  353. package/lib/crypto/base.js.map +0 -1
  354. package/lib/crypto/getNodeCrypto.js.map +0 -1
  355. package/lib/crypto/hashing.js.map +0 -1
  356. package/lib/crypto/md5.bench.js.map +0 -1
  357. package/lib/crypto/md5.d.js.map +0 -1
  358. package/lib/crypto/md5.js.map +0 -1
  359. package/lib/crypto/pem/pem.js.map +0 -1
  360. package/lib/crypto/randomUUIDv7.js.map +0 -1
  361. package/lib/crypto/ulid.js.map +0 -1
  362. package/lib/emitter/types.js.map +0 -1
  363. package/lib/errors/Errors.js.map +0 -1
  364. package/lib/fetch/HttpStatus.js.map +0 -1
  365. package/lib/fetch/createFetchWith.js.map +0 -1
  366. package/lib/fetch/createFetchWithLogging.js.map +0 -1
  367. package/lib/fetch/createFetchWithRetry.js.map +0 -1
  368. package/lib/fetch/dumpRequest.js.map +0 -1
  369. package/lib/fetch/dumpResponse.js.map +0 -1
  370. package/lib/fetch/http.types.js.map +0 -1
  371. package/lib/fetch/index.js.map +0 -1
  372. package/lib/fetch/types.js.map +0 -1
  373. package/lib/i18n/createTranslate.js.map +0 -1
  374. package/lib/index.js.map +0 -1
  375. package/lib/io/AbstractEncoding.js.map +0 -1
  376. package/lib/io/ArrayBuffer.test-d.js.map +0 -1
  377. package/lib/io/ArrayBuffers.js.map +0 -1
  378. package/lib/io/Buffer.js.map +0 -1
  379. package/lib/io/ByteBuffer.js.map +0 -1
  380. package/lib/io/base64.js.map +0 -1
  381. package/lib/io/dump.js.map +0 -1
  382. package/lib/io/isBuffer.js.map +0 -1
  383. package/lib/io/isTransferable.js.map +0 -1
  384. package/lib/io/parseDataUri.js.map +0 -1
  385. package/lib/langs/AsyncCloser.js.map +0 -1
  386. package/lib/langs/Closer.js.map +0 -1
  387. package/lib/langs/MaybeFunction.js.map +0 -1
  388. package/lib/langs/classOf.js.map +0 -1
  389. package/lib/langs/deepEqual.js.map +0 -1
  390. package/lib/langs/deepFreeze.js.map +0 -1
  391. package/lib/langs/getGlobalStates.js.map +0 -1
  392. package/lib/langs/getObjectId.js.map +0 -1
  393. package/lib/langs/ifPresent.js.map +0 -1
  394. package/lib/langs/isClass.js.map +0 -1
  395. package/lib/langs/isDefined.js.map +0 -1
  396. package/lib/langs/isEmptyObject.js.map +0 -1
  397. package/lib/langs/isFunction.js.map +0 -1
  398. package/lib/langs/isNullish.js +0 -5
  399. package/lib/langs/isNullish.js.map +0 -1
  400. package/lib/langs/isPlainObject.js.map +0 -1
  401. package/lib/langs/memoize.js.map +0 -1
  402. package/lib/langs/mixin.js.map +0 -1
  403. package/lib/langs/parseBoolean.js.map +0 -1
  404. package/lib/langs/shallowClone.js.map +0 -1
  405. package/lib/langs/shallowEqual.js.map +0 -1
  406. package/lib/libs/ms.js.map +0 -1
  407. package/lib/logging/Logger.js +0 -5
  408. package/lib/logging/Logger.js.map +0 -1
  409. package/lib/logging/createChildLogger.js +0 -16
  410. package/lib/logging/createChildLogger.js.map +0 -1
  411. package/lib/logging/createLogger.js +0 -63
  412. package/lib/logging/createLogger.js.map +0 -1
  413. package/lib/logging/createNoopLogger.js +0 -13
  414. package/lib/logging/createNoopLogger.js.map +0 -1
  415. package/lib/logging/slog.js +0 -143
  416. package/lib/logging/slog.js.map +0 -1
  417. package/lib/maths/clamp.js.map +0 -1
  418. package/lib/maths/random.js.map +0 -1
  419. package/lib/mitt/index.js +0 -77
  420. package/lib/mitt/index.js.map +0 -1
  421. package/lib/modules/isModule.js.map +0 -1
  422. package/lib/modules/parseModuleId.js.map +0 -1
  423. package/lib/objects/computeIfAbsent.js.map +0 -1
  424. package/lib/objects/get.js.map +0 -1
  425. package/lib/objects/get.test-d.js.map +0 -1
  426. package/lib/objects/merge/index.js.map +0 -1
  427. package/lib/objects/merge/isMergeableObject.js.map +0 -1
  428. package/lib/objects/merge/merge.js.map +0 -1
  429. package/lib/objects/parseObjectPath.js.map +0 -1
  430. package/lib/objects/set.js.map +0 -1
  431. package/lib/schema/typebox/index.js.map +0 -1
  432. package/lib/schema/typebox/typebox.js.map +0 -1
  433. package/lib/scripts/getGenerateContext.js.map +0 -1
  434. package/lib/server/crypto/md5.js.map +0 -1
  435. package/lib/server/fetch/createFetchWithProxy.js.map +0 -1
  436. package/lib/server/fetch/createFetchWithProxyByNodeFetch.js.map +0 -1
  437. package/lib/server/fetch/createFetchWithProxyByUndici.js.map +0 -1
  438. package/lib/server/getPackageDir.js.map +0 -1
  439. package/lib/server/index.js.map +0 -1
  440. package/lib/server/jsdom.js.map +0 -1
  441. package/lib/server/node-fetch.js.map +0 -1
  442. package/lib/server/polyfill/polyfillBrowser.js.map +0 -1
  443. package/lib/server/polyfill/polyfillCrypto.js.map +0 -1
  444. package/lib/server/polyfill/polyfillFetch.js.map +0 -1
  445. package/lib/server/polyfill/polyfillJsDom.js.map +0 -1
  446. package/lib/server/polyfill/polyfillWebSocket.js.map +0 -1
  447. package/lib/server/ws.js.map +0 -1
  448. package/lib/strings/camelCase.js.map +0 -1
  449. package/lib/strings/formatBytes.js.map +0 -1
  450. package/lib/strings/renderTemplate.js.map +0 -1
  451. package/lib/typedoc.js.map +0 -1
  452. package/lib/types.d.js.map +0 -1
  453. package/lib/validations/asserts.js.map +0 -1
  454. package/lib/validations/isUUID.js.map +0 -1
  455. package/lib/validations/parseTimestamp.js.map +0 -1
  456. package/lib/web/getGlobalThis.js.map +0 -1
  457. package/lib/web/getRandomValues.js.map +0 -1
  458. package/lib/web/randomUUID.js.map +0 -1
  459. package/lib/web/requestIdleCallback.js.map +0 -1
  460. package/lib/web/structuredClone.js.map +0 -1
  461. package/src/cn/README.md +0 -3
  462. package/src/cn/division/DivisionCode.ts +0 -173
  463. package/src/cn/division/binarySearch.test.ts +0 -64
  464. package/src/cn/division/binarySearch.ts +0 -25
  465. package/src/cn/division/division.test.ts +0 -22
  466. package/src/cn/formatChineseAmount.ts +0 -61
  467. package/src/cn/id/Mod11.ts +0 -26
  468. package/src/cn/id/ResidentIdNumber.ts +0 -126
  469. package/src/cn/id/id.test.ts +0 -20
  470. package/src/cn/id/types.ts +0 -26
  471. package/src/cn/index.ts +0 -17
  472. package/src/cn/parseChineseNumber.test.ts +0 -159
  473. package/src/cn/parseChineseNumber.ts +0 -97
  474. package/src/cn/pinyin/cartesianProduct.test.ts +0 -64
  475. package/src/cn/pinyin/cartesianProduct.ts +0 -24
  476. package/src/cn/pinyin/data.json +0 -413
  477. package/src/cn/pinyin/loader.ts +0 -12
  478. package/src/cn/pinyin/preload.ts +0 -3
  479. package/src/cn/pinyin/toPinyin.test.ts +0 -12
  480. package/src/cn/pinyin/toPinyinPure.ts +0 -43
  481. package/src/cn/pinyin/transform.ts +0 -12
  482. package/src/cn/scripts/gen.test.ts +0 -131
  483. package/src/cn/types.ts +0 -11
  484. package/src/cn/uscc/Mod31.ts +0 -38
  485. package/src/cn/uscc/USCC.ts +0 -94
  486. package/src/cn/uscc/isUSCC.ts +0 -8
  487. package/src/cn/uscc/uscc.test.ts +0 -16
  488. package/src/langs/isNullish.ts +0 -3
  489. package/src/logging/Logger.ts +0 -25
  490. package/src/logging/createChildLogger.ts +0 -16
  491. package/src/logging/createLogger.ts +0 -50
  492. package/src/logging/createNoopLogger.ts +0 -13
  493. package/src/logging/logger.test.ts +0 -36
  494. package/src/logging/slog.test.ts +0 -8
  495. package/src/logging/slog.ts +0 -221
  496. package/src/mitt/README.md +0 -1
  497. package/src/mitt/index.ts +0 -114
@@ -6,43 +6,43 @@ import { parseObjectPath, type ObjectKey, type ObjectPath } from './parseObjectP
6
6
  * {@link https://github.com/lukeed/dset dset}
7
7
  */
8
8
  export function set<T extends object, V>(obj: T, key: ObjectKey | ObjectPath, val: V, merging = true) {
9
- const path = parseObjectPath(key);
10
- let i = 0;
11
- const len = path.length;
12
- let current: any = obj;
13
- let x, k;
14
- while (i < len) {
15
- k = path[i++];
16
- if (k === '__proto__' || k === 'constructor' || k === 'prototype') break;
17
- // noinspection PointlessArithmeticExpressionJS
18
- current = current[k] =
19
- i === len
20
- ? merging
21
- ? merge(current[k], val)
22
- : val
23
- : typeof (x = current[k]) === typeof path
24
- ? x
25
- : // @ts-expect-error hacky type check
26
- path[i] * 0 !== 0 || !!~('' + path[i]).indexOf('.') // eslint-disable-line
27
- ? {}
28
- : [];
29
- }
9
+ const path = parseObjectPath(key);
10
+ let i = 0;
11
+ const len = path.length;
12
+ let current: any = obj;
13
+ let x, k;
14
+ while (i < len) {
15
+ k = path[i++];
16
+ if (k === '__proto__' || k === 'constructor' || k === 'prototype') break;
17
+ // noinspection PointlessArithmeticExpressionJS
18
+ current = current[k] =
19
+ i === len
20
+ ? merging
21
+ ? merge(current[k], val)
22
+ : val
23
+ : typeof (x = current[k]) === typeof path
24
+ ? x
25
+ : // @ts-expect-error hacky type check
26
+ path[i] * 0 !== 0 || !!~('' + path[i]).indexOf('.') // eslint-disable-line
27
+ ? {}
28
+ : [];
29
+ }
30
30
  }
31
31
 
32
32
  export function merge(a: any, b: any) {
33
- let k;
34
- if (typeof a === 'object' && typeof b === 'object') {
35
- if (Array.isArray(a) && Array.isArray(b)) {
36
- for (k = 0; k < b.length; k++) {
37
- a[k] = merge(a[k], b[k]);
38
- }
39
- } else {
40
- for (k in b) {
41
- if (k === '__proto__' || k === 'constructor' || k === 'prototype') break;
42
- a[k] = merge(a[k], b[k]);
43
- }
44
- }
45
- return a;
46
- }
47
- return b;
33
+ let k;
34
+ if (typeof a === 'object' && typeof b === 'object') {
35
+ if (Array.isArray(a) && Array.isArray(b)) {
36
+ for (k = 0; k < b.length; k++) {
37
+ a[k] = merge(a[k], b[k]);
38
+ }
39
+ } else {
40
+ for (k in b) {
41
+ if (k === '__proto__' || k === 'constructor' || k === 'prototype') break;
42
+ a[k] = merge(a[k], b[k]);
43
+ }
44
+ }
45
+ return a;
46
+ }
47
+ return b;
48
48
  }
@@ -3,32 +3,26 @@ import { Value } from '@sinclair/typebox/value';
3
3
 
4
4
  // works like zod parse
5
5
  export const Parse = <T extends TSchema>(T: T, value: unknown) =>
6
- // run transform, get T
7
- Value.Decode(
8
- T,
9
- // remove additional
10
- Value.Clean(
11
- T,
12
- // add missing
13
- Value.Default(
14
- T,
15
- // '1' -> 1
16
- Value.Convert(T, value),
17
- ),
18
- ),
19
- );
6
+ // run transform, get T
7
+ Value.Decode(
8
+ T,
9
+ // remove additional
10
+ Value.Clean(
11
+ T,
12
+ // add missing
13
+ Value.Default(
14
+ T,
15
+ // '1' -> 1
16
+ Value.Convert(T, value),
17
+ ),
18
+ ),
19
+ );
20
20
 
21
21
  export const SafeParse = <T extends TSchema>(T: T, value: unknown) => {
22
- let out = Value.Clean(T, Value.Default(T, Value.Convert(T, value)));
23
- if (Value.Check(T, out)) {
24
- // decode 也会 check
25
- return {
26
- data: Value.Decode(T, out),
27
- success: true,
28
- };
29
- }
30
- return {
31
- errors: Array.from(Value.Errors(T, [], value)),
32
- success: false,
33
- };
22
+ let out = Value.Clean(T, Value.Default(T, Value.Convert(T, value)));
23
+ if (Value.Check(T, out)) {
24
+ // decode 也会 check
25
+ return { data: Value.Decode(T, out), success: true };
26
+ }
27
+ return { errors: Array.from(Value.Errors(T, [], value)), success: false };
34
28
  };
@@ -8,92 +8,92 @@ import { formatBytes } from '../strings/formatBytes';
8
8
  let _root: GenerateContext;
9
9
 
10
10
  export interface GenerateContext {
11
- pkgDir: string;
12
- rootDataDir: string;
13
- rootCacheDir: string;
14
- dataDir: string;
15
- cacheDir: string;
16
- srcDir: string;
17
- cache: (url: string) => Promise<string>;
18
- writeData: (path: string, content: string) => Promise<void>;
19
- writeSrc: (path: string, content: string) => Promise<void>;
11
+ pkgDir: string;
12
+ rootDataDir: string;
13
+ rootCacheDir: string;
14
+ dataDir: string;
15
+ cacheDir: string;
16
+ srcDir: string;
17
+ cache: (url: string) => Promise<string>;
18
+ writeData: (path: string, content: string) => Promise<void>;
19
+ writeSrc: (path: string, content: string) => Promise<void>;
20
20
  }
21
21
 
22
22
  async function cache(dir: string, url: string): Promise<string> {
23
- const key = path.basename(url);
24
- await fs.mkdir(path.join(dir), { recursive: true });
25
- const file = path.join(dir, key);
26
- try {
27
- await fs.stat(file);
28
- console.info('[cache] hit', url);
29
- return await fs.readFile(file, 'utf-8');
30
- } catch (e) {}
31
- console.info('[cache] miss', url);
32
- const text = await fetch(url).then((v) => {
33
- if (v.status >= 300) {
34
- throw new Error(`[cache] fetch ${url} ${v.status} ${v.statusText}`);
35
- }
36
- return v.text();
37
- });
38
- const size = new Blob([text]).size;
39
- console.info(`[cache] write ${file} ${formatBytes(size)} (${size})`);
40
- await fs.writeFile(file, text);
41
- return text;
23
+ const key = path.basename(url);
24
+ await fs.mkdir(path.join(dir), { recursive: true });
25
+ const file = path.join(dir, key);
26
+ try {
27
+ await fs.stat(file);
28
+ console.info('[cache] hit', url);
29
+ return await fs.readFile(file, 'utf-8');
30
+ } catch (e) {}
31
+ console.info('[cache] miss', url);
32
+ const text = await fetch(url).then((v) => {
33
+ if (v.status >= 300) {
34
+ throw new Error(`[cache] fetch ${url} ${v.status} ${v.statusText}`);
35
+ }
36
+ return v.text();
37
+ });
38
+ const size = new Blob([text]).size;
39
+ console.info(`[cache] write ${file} ${formatBytes(size)} (${size})`);
40
+ await fs.writeFile(file, text);
41
+ return text;
42
42
  }
43
43
 
44
44
  export function getGenerateContext(prefix?: string): MaybePromise<GenerateContext> {
45
- if (prefix) {
46
- return Promise.resolve(getGenerateContext()).then((c) => {
47
- const cacheDir = path.resolve(c.cacheDir, prefix);
48
- const dataDir = path.resolve(c.dataDir, prefix);
49
- const srcDir = path.resolve(c.srcDir, prefix);
50
- return {
51
- ...c,
52
- dataDir,
53
- cacheDir,
54
- srcDir,
55
- cache: (url: string) => cache(cacheDir, url),
56
- writeData: (file: string, content: string) => write(dataDir, file, content),
57
- writeSrc: (file: string, content: string) => write(srcDir, file, content),
58
- };
59
- });
60
- }
61
- return (
62
- _root ||
63
- Promise.resolve().then(async () => {
64
- const pkgDir = await getPackageDir();
65
- Errors.BadRequest.check(pkgDir, 'pkgDir not found');
66
- const dataDir = path.resolve(pkgDir, 'data');
67
- const cacheDir = path.resolve(pkgDir, 'cache');
68
- const srcDir = path.resolve(pkgDir, 'src');
69
- await fs.mkdir(cacheDir, { recursive: true });
70
- await fs.mkdir(dataDir, { recursive: true });
71
- return (_root = Object.freeze({
72
- pkgDir,
73
- dataDir,
74
- cacheDir,
75
- srcDir,
76
- rootCacheDir: cacheDir,
77
- rootDataDir: dataDir,
78
- cache: (url: string) => cache(cacheDir, url),
79
- writeData: (file: string, content: string) => write(dataDir, file, content),
80
- writeSrc: (file: string, content: string) => write(srcDir, file, content),
81
- }));
82
- })
83
- );
45
+ if (prefix) {
46
+ return Promise.resolve(getGenerateContext()).then((c) => {
47
+ const cacheDir = path.resolve(c.cacheDir, prefix);
48
+ const dataDir = path.resolve(c.dataDir, prefix);
49
+ const srcDir = path.resolve(c.srcDir, prefix);
50
+ return {
51
+ ...c,
52
+ dataDir,
53
+ cacheDir,
54
+ srcDir,
55
+ cache: (url: string) => cache(cacheDir, url),
56
+ writeData: (file: string, content: string) => write(dataDir, file, content),
57
+ writeSrc: (file: string, content: string) => write(srcDir, file, content),
58
+ };
59
+ });
60
+ }
61
+ return (
62
+ _root
63
+ || Promise.resolve().then(async () => {
64
+ const pkgDir = await getPackageDir();
65
+ Errors.BadRequest.check(pkgDir, 'pkgDir not found');
66
+ const dataDir = path.resolve(pkgDir, 'data');
67
+ const cacheDir = path.resolve(pkgDir, 'cache');
68
+ const srcDir = path.resolve(pkgDir, 'src');
69
+ await fs.mkdir(cacheDir, { recursive: true });
70
+ await fs.mkdir(dataDir, { recursive: true });
71
+ return (_root = Object.freeze({
72
+ pkgDir,
73
+ dataDir,
74
+ cacheDir,
75
+ srcDir,
76
+ rootCacheDir: cacheDir,
77
+ rootDataDir: dataDir,
78
+ cache: (url: string) => cache(cacheDir, url),
79
+ writeData: (file: string, content: string) => write(dataDir, file, content),
80
+ writeSrc: (file: string, content: string) => write(srcDir, file, content),
81
+ }));
82
+ })
83
+ );
84
84
  }
85
85
 
86
86
  async function write(dir: string, file: string, content: string) {
87
- const size = new Blob([content]).size;
88
- const dst = path.join(dir, file);
89
- let last;
90
- try {
91
- last = (await fs.stat(dst)).size;
92
- } catch {}
93
- console.info(
94
- `[write] ${path.relative(_root.pkgDir, dir)} ${file} ${
95
- last ? (last !== size ? `${formatBytes(last)} (${last}) -> ` : 'SAME SIZE ') : ''
96
- }${formatBytes(size)} (${size})`,
97
- );
98
- await fs.writeFile(dst, content);
87
+ const size = new Blob([content]).size;
88
+ const dst = path.join(dir, file);
89
+ let last;
90
+ try {
91
+ last = (await fs.stat(dst)).size;
92
+ } catch {}
93
+ console.info(
94
+ `[write] ${path.relative(_root.pkgDir, dir)} ${file} ${
95
+ last ? (last !== size ? `${formatBytes(last)} (${last}) -> ` : 'SAME SIZE ') : ''
96
+ }${formatBytes(size)} (${size})`,
97
+ );
98
+ await fs.writeFile(dst, content);
99
99
  }
@@ -1,5 +1,5 @@
1
1
  import { createHash } from 'node:crypto';
2
2
 
3
3
  export function md5(input: Buffer | string) {
4
- return createHash('md5').update(input).digest('hex') as string;
4
+ return createHash('md5').update(input).digest('hex') as string;
5
5
  }
@@ -2,40 +2,37 @@ import type { FetchLike } from '../../fetch';
2
2
  import { getGlobalThis } from '../../web/getGlobalThis';
3
3
 
4
4
  export function createFetchWithProxyByNodeFetch({
5
- proxy,
6
- fetch,
5
+ proxy,
6
+ fetch,
7
7
  }: { proxy?: string; fetch?: FetchLike } = {}): FetchLike {
8
- const globalThis = getGlobalThis();
9
- if (!proxy) {
10
- return fetch || globalThis.fetch;
11
- }
8
+ const globalThis = getGlobalThis();
9
+ if (!proxy) {
10
+ return fetch || globalThis.fetch;
11
+ }
12
12
 
13
- let agent: any;
14
- const Request = globalThis.Request;
15
- let NodeRequest: any;
16
- let NodeFetch: any;
17
- return async (url, init?: RequestInit) => {
18
- if (!agent) {
19
- const { default: createHttpsProxyAgent } = await import('https-proxy-agent');
20
- agent = (createHttpsProxyAgent as any)(proxy);
21
- }
13
+ let agent: any;
14
+ const Request = globalThis.Request;
15
+ let NodeRequest: any;
16
+ let NodeFetch: any;
17
+ return async (url, init?: RequestInit) => {
18
+ if (!agent) {
19
+ const { default: createHttpsProxyAgent } = await import('https-proxy-agent');
20
+ agent = (createHttpsProxyAgent as any)(proxy);
21
+ }
22
22
 
23
- // node-fetch 才可以,node v18 fetch 不支持
24
- if (!NodeRequest) {
25
- ({ Request: NodeRequest, default: NodeFetch } = await import('node-fetch'));
26
- }
23
+ // node-fetch 才可以,node v18 fetch 不支持
24
+ if (!NodeRequest) {
25
+ ({ Request: NodeRequest, default: NodeFetch } = await import('node-fetch'));
26
+ }
27
27
 
28
- fetch ||= NodeFetch;
28
+ fetch ||= NodeFetch;
29
29
 
30
- if (url instanceof Request) {
31
- return (fetch as any)(new Request(url, { agent } as any));
32
- }
33
- if ((url as any) instanceof NodeRequest) {
34
- return (fetch as any)(new NodeRequest(url, { agent } as any));
35
- }
36
- return (fetch as any)(url, {
37
- ...init,
38
- agent,
39
- } as any);
40
- };
30
+ if (url instanceof Request) {
31
+ return (fetch as any)(new Request(url, { agent } as any));
32
+ }
33
+ if ((url as any) instanceof NodeRequest) {
34
+ return (fetch as any)(new NodeRequest(url, { agent } as any));
35
+ }
36
+ return (fetch as any)(url, { ...init, agent } as any);
37
+ };
41
38
  }
@@ -3,77 +3,71 @@ import { createFetchWith, type FetchLike } from '../../fetch';
3
3
  import { getGlobalThis } from '../../web/getGlobalThis';
4
4
 
5
5
  export function createFetchWithProxyByUndici({
6
- proxy,
7
- token: _token,
8
- fetch,
9
- undici,
6
+ proxy,
7
+ token: _token,
8
+ fetch,
9
+ undici,
10
10
  }: {
11
- proxy?: string;
12
- token?: string;
13
- fetch?: FetchLike;
14
- undici?: MaybePromise<{ fetch: any; ProxyAgent: any }>;
11
+ proxy?: string;
12
+ token?: string;
13
+ fetch?: FetchLike;
14
+ undici?: MaybePromise<{ fetch: any; ProxyAgent: any }>;
15
15
  } = {}): FetchLike {
16
- if (!proxy) {
17
- return fetch || globalThis.fetch;
18
- }
16
+ if (!proxy) {
17
+ return fetch || globalThis.fetch;
18
+ }
19
19
 
20
- if ((getGlobalThis() as any).Bun) {
21
- return createFetchWith({
22
- fetch,
23
- onRequest: ({ url, req, next }) => {
24
- return next(url, {
25
- ...req,
26
- // https://github.com/oven-sh/bun/issues/1829
27
- proxy,
28
- } as RequestInit);
29
- },
30
- });
31
- }
20
+ if ((getGlobalThis() as any).Bun) {
21
+ return createFetchWith({
22
+ fetch,
23
+ onRequest: ({ url, req, next }) => {
24
+ return next(url, {
25
+ ...req,
26
+ // https://github.com/oven-sh/bun/issues/1829
27
+ proxy,
28
+ } as RequestInit);
29
+ },
30
+ });
31
+ }
32
32
 
33
- let agent: any;
34
- // https://github.com/nodejs/undici/blob/main/docs/best-practices/proxy.md
35
- return async (...args) => {
36
- const init = (args[1] ||= {}) as RequestInit & {
37
- duplex?: string;
38
- dispatcher?: any;
39
- };
40
- {
41
- const body = init.body;
42
- if (typeof body === 'object' && body && (body instanceof ReadableStream || Symbol.asyncIterator in body)) {
43
- // request.duplex must be set if request.body is ReadableStream or Async Iterables
44
- init.duplex ||= 'half';
45
- }
46
- }
47
- if (!agent) {
48
- let uri = proxy;
49
- let token = _token;
50
- {
51
- let u: URL | undefined;
52
- try {
53
- u = new URL(proxy);
54
- } catch (e) {}
55
- if (!token && u && (u.username || u.password)) {
56
- token = `Basic ${btoa(`${u.username || ''}:${u.password}`)}`;
57
- u.username = '';
58
- u.password = '';
59
- uri = u.toString();
60
- }
61
- }
62
- // if in next use 'next/dist/compiled/undici'
63
- undici ||= import('undici');
64
- const mod = await undici;
65
- const ProxyAgent = mod.ProxyAgent as new (_: any) => any;
66
- fetch ||= mod.fetch as FetchLike;
67
- // https://github.com/nodejs/undici/blob/main/docs/api/ProxyAgent.md
68
- agent = new ProxyAgent({
69
- uri,
70
- token,
71
- });
72
- // https://github.com/nodejs/node/issues/43187#issuecomment-1134634174
73
- // (global as any)[Symbol.for('undici.globalDispatcher.1')] = agent;
74
- // fixme should unwrap error https://github.com/nodejs/undici/issues/1248
75
- }
76
- init.dispatcher = agent;
77
- return await fetch!(...args);
78
- };
33
+ let agent: any;
34
+ // https://github.com/nodejs/undici/blob/main/docs/best-practices/proxy.md
35
+ return async (...args) => {
36
+ const init = (args[1] ||= {}) as RequestInit & { duplex?: string; dispatcher?: any };
37
+ {
38
+ const body = init.body;
39
+ if (typeof body === 'object' && body && (body instanceof ReadableStream || Symbol.asyncIterator in body)) {
40
+ // request.duplex must be set if request.body is ReadableStream or Async Iterables
41
+ init.duplex ||= 'half';
42
+ }
43
+ }
44
+ if (!agent) {
45
+ let uri = proxy;
46
+ let token = _token;
47
+ {
48
+ let u: URL | undefined;
49
+ try {
50
+ u = new URL(proxy);
51
+ } catch (e) {}
52
+ if (!token && u && (u.username || u.password)) {
53
+ token = `Basic ${btoa(`${u.username || ''}:${u.password}`)}`;
54
+ u.username = '';
55
+ u.password = '';
56
+ uri = u.toString();
57
+ }
58
+ }
59
+ // if in next use 'next/dist/compiled/undici'
60
+ undici ||= import('undici');
61
+ const mod = await undici;
62
+ const ProxyAgent = mod.ProxyAgent as new (_: any) => any;
63
+ fetch ||= mod.fetch as FetchLike;
64
+ // https://github.com/nodejs/undici/blob/main/docs/api/ProxyAgent.md
65
+ agent = new ProxyAgent({ uri, token });
66
+ // https://github.com/nodejs/node/issues/43187#issuecomment-1134634174
67
+ // (global as any)[Symbol.for('undici.globalDispatcher.1')] = agent;
68
+ // fixme should unwrap error https://github.com/nodejs/undici/issues/1248
69
+ }
70
+ init.dispatcher = agent;
71
+ return await fetch!(...args);
72
+ };
79
73
  }
@@ -6,13 +6,13 @@ import path from 'node:path';
6
6
  // const __dirname = path.dirname(__filename);
7
7
 
8
8
  export function getPackageDir(currentDir: string = process.cwd()) {
9
- while (!fs.existsSync(path.join(currentDir, 'package.json'))) {
10
- currentDir = path.resolve(currentDir, '..');
11
- if (currentDir === '/') {
12
- return undefined;
13
- }
14
- }
15
- return currentDir;
9
+ while (!fs.existsSync(path.join(currentDir, 'package.json'))) {
10
+ currentDir = path.resolve(currentDir, '..');
11
+ if (currentDir === '/') {
12
+ return undefined;
13
+ }
14
+ }
15
+ return currentDir;
16
16
  }
17
17
 
18
18
  function findUp() {}
@@ -4,17 +4,17 @@ import { polyfillBrowser } from './polyfillBrowser';
4
4
  import { polyfillFetch } from './polyfillFetch';
5
5
 
6
6
  beforeAll(async () => {
7
- // return false in nodejs18
8
- polyfillFetch(nodeFetch);
9
- expect(polyfillFetch()).toBeFalsy();
10
- await polyfillBrowser();
7
+ // return false in nodejs18
8
+ polyfillFetch(nodeFetch);
9
+ expect(polyfillFetch()).toBeFalsy();
10
+ await polyfillBrowser();
11
11
  });
12
12
 
13
13
  test('polyfillBrowser', () => {
14
- expect(fetch).toBeTruthy();
15
- expect(window).toBeTruthy();
16
- expect(document).toBeTruthy();
17
- expect(crypto).toBeTruthy();
18
- // not the same
19
- expect(window).not.toBe(globalThis);
14
+ expect(fetch).toBeTruthy();
15
+ expect(window).toBeTruthy();
16
+ expect(document).toBeTruthy();
17
+ expect(crypto).toBeTruthy();
18
+ // not the same
19
+ expect(window).not.toBe(globalThis);
20
20
  });
@@ -11,7 +11,7 @@ import { polyfillJsDom } from './polyfillJsDom';
11
11
  * - `crypto`
12
12
  */
13
13
  export async function polyfillBrowser() {
14
- await polyfillCrypto();
15
- await polyfillFetch();
16
- await polyfillJsDom();
14
+ await polyfillCrypto();
15
+ await polyfillFetch();
16
+ await polyfillJsDom();
17
17
  }
@@ -1,10 +1,10 @@
1
1
  import { getGlobalThis } from '../../web/getGlobalThis';
2
2
 
3
3
  export async function polyfillCrypto() {
4
- const globalThis = getGlobalThis();
5
- if ('crypto' in globalThis) {
6
- return false;
7
- }
8
- (globalThis as any).crypto = (await import('node:crypto')).webcrypto as Crypto;
9
- return true;
4
+ const globalThis = getGlobalThis();
5
+ if ('crypto' in globalThis) {
6
+ return false;
7
+ }
8
+ (globalThis as any).crypto = (await import('node:crypto')).webcrypto as Crypto;
9
+ return true;
10
10
  }
@@ -3,25 +3,15 @@ import type { MaybePromise } from '../../asyncs/MaybePromise';
3
3
  export function polyfillFetch(nodeFetch: typeof import('node-fetch')): boolean;
4
4
  export function polyfillFetch(nodeFetch?: undefined): Promise<boolean>;
5
5
  export function polyfillFetch(nodeFetch?: typeof import('node-fetch')): MaybePromise<boolean> {
6
- if ('fetch' in globalThis) {
7
- return false;
8
- }
9
- // sync mode
10
- if (nodeFetch) {
11
- const { default: fetch, Response, Headers, Request, AbortError, FetchError, FormData, Blob, File } = nodeFetch;
12
- Object.assign(globalThis, {
13
- fetch,
14
- Response,
15
- Headers,
16
- Request,
17
- AbortError,
18
- FetchError,
19
- FormData,
20
- Blob,
21
- File,
22
- });
23
- // abort controller
24
- return true;
25
- }
26
- return import('node-fetch').then((v) => polyfillFetch(v));
6
+ if ('fetch' in globalThis) {
7
+ return false;
8
+ }
9
+ // sync mode
10
+ if (nodeFetch) {
11
+ const { default: fetch, Response, Headers, Request, AbortError, FetchError, FormData, Blob, File } = nodeFetch;
12
+ Object.assign(globalThis, { fetch, Response, Headers, Request, AbortError, FetchError, FormData, Blob, File });
13
+ // abort controller
14
+ return true;
15
+ }
16
+ return import('node-fetch').then((v) => polyfillFetch(v));
27
17
  }