@wener/utils 1.1.31 → 1.1.33

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 (317) hide show
  1. package/dist/LICENSE.txt +1 -0
  2. package/dist/cjs/index.cjs +21 -0
  3. package/dist/cjs/index.cjs.map +1 -0
  4. package/dist/esm/index.js +21 -0
  5. package/dist/esm/index.js.map +1 -0
  6. package/dist/system/index.js +21 -0
  7. package/dist/system/index.js.map +1 -0
  8. package/package.json +37 -69
  9. package/src/asyncs/nextOfAsyncIterator.ts +13 -1
  10. package/src/asyncs/timeout.ts +15 -12
  11. package/src/cn/index.ts +1 -0
  12. package/src/cn/parseChineseNumber.test.ts +24 -0
  13. package/src/cn/parseChineseNumber.ts +97 -0
  14. package/src/crypto/getRandomValues.ts +5 -3
  15. package/src/crypto/hashing.test.ts +1 -1
  16. package/src/errors/Errors.test.ts +22 -0
  17. package/src/errors/Errors.ts +1 -4
  18. package/src/index.ts +2 -0
  19. package/src/io/Bytes.ts +64 -0
  20. package/src/isomorphics/structuredClone.ts +0 -3
  21. package/src/langs/AsyncCloser.ts +45 -0
  22. package/src/langs/Closer.ts +39 -0
  23. package/src/server/index.ts +6 -0
  24. package/src/{servers → server}/polyfill/polyfillWebSocket.ts +5 -0
  25. package/src/{types/index.d.ts → types.d.ts} +12 -0
  26. package/tsconfig.json +7 -5
  27. package/index.ts +0 -1
  28. package/lib/arrays/MaybeArray.js +0 -41
  29. package/lib/arrays/MaybeArray.js.map +0 -1
  30. package/lib/arrays/arrayFromAsync.js +0 -7
  31. package/lib/arrays/arrayFromAsync.js.map +0 -1
  32. package/lib/asyncs/AsyncInterval.js +0 -14
  33. package/lib/asyncs/AsyncInterval.js.map +0 -1
  34. package/lib/asyncs/MaybePromise.js +0 -3
  35. package/lib/asyncs/MaybePromise.js.map +0 -1
  36. package/lib/asyncs/createAsyncIterator.js +0 -41
  37. package/lib/asyncs/createAsyncIterator.js.map +0 -1
  38. package/lib/asyncs/createLazyPromise.js +0 -65
  39. package/lib/asyncs/createLazyPromise.js.map +0 -1
  40. package/lib/asyncs/firstOfAsyncIterator.js +0 -11
  41. package/lib/asyncs/firstOfAsyncIterator.js.map +0 -1
  42. package/lib/asyncs/generatorOfStream.js +0 -16
  43. package/lib/asyncs/generatorOfStream.js.map +0 -1
  44. package/lib/asyncs/isIterator.js +0 -5
  45. package/lib/asyncs/isIterator.js.map +0 -1
  46. package/lib/asyncs/isPromise.js +0 -5
  47. package/lib/asyncs/isPromise.js.map +0 -1
  48. package/lib/asyncs/isThenable.js +0 -6
  49. package/lib/asyncs/isThenable.js.map +0 -1
  50. package/lib/asyncs/nextOfAsyncIterator.js +0 -25
  51. package/lib/asyncs/nextOfAsyncIterator.js.map +0 -1
  52. package/lib/asyncs/promiseOfCallback.js +0 -17
  53. package/lib/asyncs/promiseOfCallback.js.map +0 -1
  54. package/lib/asyncs/sleep.js +0 -3
  55. package/lib/asyncs/sleep.js.map +0 -1
  56. package/lib/asyncs/timeout.js +0 -25
  57. package/lib/asyncs/timeout.js.map +0 -1
  58. package/lib/browsers/copy.js +0 -53
  59. package/lib/browsers/copy.js.map +0 -1
  60. package/lib/browsers/download.js +0 -41
  61. package/lib/browsers/download.js.map +0 -1
  62. package/lib/browsers/getFileFromDataTransfer.js +0 -45
  63. package/lib/browsers/getFileFromDataTransfer.js.map +0 -1
  64. package/lib/browsers/loaders.js +0 -49
  65. package/lib/browsers/loaders.js.map +0 -1
  66. package/lib/crypto/base.js +0 -7
  67. package/lib/crypto/base.js.map +0 -1
  68. package/lib/crypto/getNodeCrypto.js +0 -18
  69. package/lib/crypto/getNodeCrypto.js.map +0 -1
  70. package/lib/crypto/getRandomValues.js +0 -29
  71. package/lib/crypto/getRandomValues.js.map +0 -1
  72. package/lib/crypto/hashing.js +0 -57
  73. package/lib/crypto/hashing.js.map +0 -1
  74. package/lib/crypto/md5.bench.js +0 -21
  75. package/lib/crypto/md5.bench.js.map +0 -1
  76. package/lib/crypto/md5.d.js +0 -3
  77. package/lib/crypto/md5.d.js.map +0 -1
  78. package/lib/crypto/md5.js +0 -121
  79. package/lib/crypto/md5.js.map +0 -1
  80. package/lib/crypto/pem/pem.js +0 -35
  81. package/lib/crypto/pem/pem.js.map +0 -1
  82. package/lib/crypto/randomUUID.js +0 -16
  83. package/lib/crypto/randomUUID.js.map +0 -1
  84. package/lib/crypto/ulid.js +0 -153
  85. package/lib/crypto/ulid.js.map +0 -1
  86. package/lib/errors/Errors.js +0 -181
  87. package/lib/errors/Errors.js.map +0 -1
  88. package/lib/fetch/createFetchWith.js +0 -23
  89. package/lib/fetch/createFetchWith.js.map +0 -1
  90. package/lib/fetch/createFetchWithLogging.js +0 -25
  91. package/lib/fetch/createFetchWithLogging.js.map +0 -1
  92. package/lib/fetch/createFetchWithRetry.js +0 -66
  93. package/lib/fetch/createFetchWithRetry.js.map +0 -1
  94. package/lib/fetch/dumpRequest.js +0 -45
  95. package/lib/fetch/dumpRequest.js.map +0 -1
  96. package/lib/fetch/dumpResponse.js +0 -16
  97. package/lib/fetch/dumpResponse.js.map +0 -1
  98. package/lib/fetch/index.js +0 -7
  99. package/lib/fetch/index.js.map +0 -1
  100. package/lib/fetch/types.js +0 -3
  101. package/lib/fetch/types.js.map +0 -1
  102. package/lib/http/HttpStatus.js +0 -94
  103. package/lib/http/HttpStatus.js.map +0 -1
  104. package/lib/i18n/createTranslate.js +0 -54
  105. package/lib/i18n/createTranslate.js.map +0 -1
  106. package/lib/index.js +0 -80
  107. package/lib/index.js.map +0 -1
  108. package/lib/io/AbstractEncoding.js +0 -7
  109. package/lib/io/AbstractEncoding.js.map +0 -1
  110. package/lib/io/ArrayBuffer.test-d.js +0 -7
  111. package/lib/io/ArrayBuffer.test-d.js.map +0 -1
  112. package/lib/io/ArrayBuffers.js +0 -210
  113. package/lib/io/ArrayBuffers.js.map +0 -1
  114. package/lib/io/Buffer.js +0 -25
  115. package/lib/io/Buffer.js.map +0 -1
  116. package/lib/io/base64.js +0 -54
  117. package/lib/io/base64.js.map +0 -1
  118. package/lib/io/isBuffer.js +0 -9
  119. package/lib/io/isBuffer.js.map +0 -1
  120. package/lib/io/isTransferable.js +0 -30
  121. package/lib/io/isTransferable.js.map +0 -1
  122. package/lib/isomorphics/getGlobalThis.js +0 -19
  123. package/lib/isomorphics/getGlobalThis.js.map +0 -1
  124. package/lib/isomorphics/structuredClone.js +0 -80
  125. package/lib/isomorphics/structuredClone.js.map +0 -1
  126. package/lib/langs/MaybeFunction.js +0 -9
  127. package/lib/langs/MaybeFunction.js.map +0 -1
  128. package/lib/langs/classOf.js +0 -5
  129. package/lib/langs/classOf.js.map +0 -1
  130. package/lib/langs/deepEqual.js +0 -87
  131. package/lib/langs/deepEqual.js.map +0 -1
  132. package/lib/langs/deepFreeze.js +0 -20
  133. package/lib/langs/deepFreeze.js.map +0 -1
  134. package/lib/langs/hashCode.ignored.js +0 -112
  135. package/lib/langs/hashCode.ignored.js.map +0 -1
  136. package/lib/langs/isClass.js +0 -5
  137. package/lib/langs/isClass.js.map +0 -1
  138. package/lib/langs/isDefined.js +0 -5
  139. package/lib/langs/isDefined.js.map +0 -1
  140. package/lib/langs/isEmptyObject.js +0 -6
  141. package/lib/langs/isEmptyObject.js.map +0 -1
  142. package/lib/langs/isFunction.js +0 -5
  143. package/lib/langs/isFunction.js.map +0 -1
  144. package/lib/langs/isPlainObject.js +0 -10
  145. package/lib/langs/isPlainObject.js.map +0 -1
  146. package/lib/langs/memoize.js +0 -22
  147. package/lib/langs/memoize.js.map +0 -1
  148. package/lib/langs/parseBoolean.js +0 -30
  149. package/lib/langs/parseBoolean.js.map +0 -1
  150. package/lib/langs/shallowClone.js +0 -15
  151. package/lib/langs/shallowClone.js.map +0 -1
  152. package/lib/langs/shallowEqual.js +0 -24
  153. package/lib/langs/shallowEqual.js.map +0 -1
  154. package/lib/libs/ms.js +0 -137
  155. package/lib/libs/ms.js.map +0 -1
  156. package/lib/logging/Logger.js +0 -5
  157. package/lib/logging/Logger.js.map +0 -1
  158. package/lib/logging/createChildLogger.js +0 -16
  159. package/lib/logging/createChildLogger.js.map +0 -1
  160. package/lib/logging/createLogger.js +0 -63
  161. package/lib/logging/createLogger.js.map +0 -1
  162. package/lib/logging/createNoopLogger.js +0 -13
  163. package/lib/logging/createNoopLogger.js.map +0 -1
  164. package/lib/logging/slog.js +0 -143
  165. package/lib/logging/slog.js.map +0 -1
  166. package/lib/maths/clamp.js +0 -21
  167. package/lib/maths/clamp.js.map +0 -1
  168. package/lib/maths/random.js +0 -17
  169. package/lib/maths/random.js.map +0 -1
  170. package/lib/modules/isModule.js +0 -7
  171. package/lib/modules/isModule.js.map +0 -1
  172. package/lib/modules/parseModuleId.js +0 -39
  173. package/lib/modules/parseModuleId.js.map +0 -1
  174. package/lib/objects/get.js +0 -16
  175. package/lib/objects/get.js.map +0 -1
  176. package/lib/objects/get.test-d.js +0 -33
  177. package/lib/objects/get.test-d.js.map +0 -1
  178. package/lib/objects/merge/index.js +0 -3
  179. package/lib/objects/merge/index.js.map +0 -1
  180. package/lib/objects/merge/isMergeableObject.js +0 -19
  181. package/lib/objects/merge/isMergeableObject.js.map +0 -1
  182. package/lib/objects/merge/merge.js +0 -89
  183. package/lib/objects/merge/merge.js.map +0 -1
  184. package/lib/objects/parseObjectPath.js +0 -39
  185. package/lib/objects/parseObjectPath.js.map +0 -1
  186. package/lib/objects/set.js +0 -38
  187. package/lib/objects/set.js.map +0 -1
  188. package/lib/schema/typebox/gen/codegen/common/encoder.js +0 -100
  189. package/lib/schema/typebox/gen/codegen/common/encoder.js.map +0 -1
  190. package/lib/schema/typebox/gen/codegen/common/formatter.js +0 -33
  191. package/lib/schema/typebox/gen/codegen/common/formatter.js.map +0 -1
  192. package/lib/schema/typebox/gen/codegen/common/index.js +0 -5
  193. package/lib/schema/typebox/gen/codegen/common/index.js.map +0 -1
  194. package/lib/schema/typebox/gen/codegen/common/jsdoc.js +0 -117
  195. package/lib/schema/typebox/gen/codegen/common/jsdoc.js.map +0 -1
  196. package/lib/schema/typebox/gen/codegen/expression/compiler.js +0 -367
  197. package/lib/schema/typebox/gen/codegen/expression/compiler.js.map +0 -1
  198. package/lib/schema/typebox/gen/codegen/expression/errors.js +0 -259
  199. package/lib/schema/typebox/gen/codegen/expression/errors.js.map +0 -1
  200. package/lib/schema/typebox/gen/codegen/expression/evaluator.js +0 -254
  201. package/lib/schema/typebox/gen/codegen/expression/evaluator.js.map +0 -1
  202. package/lib/schema/typebox/gen/codegen/expression/expression.js +0 -384
  203. package/lib/schema/typebox/gen/codegen/expression/expression.js.map +0 -1
  204. package/lib/schema/typebox/gen/codegen/expression/index.js +0 -6
  205. package/lib/schema/typebox/gen/codegen/expression/index.js.map +0 -1
  206. package/lib/schema/typebox/gen/codegen/index.js +0 -5
  207. package/lib/schema/typebox/gen/codegen/index.js.map +0 -1
  208. package/lib/schema/typebox/gen/codegen/model/index.js +0 -16
  209. package/lib/schema/typebox/gen/codegen/model/index.js.map +0 -1
  210. package/lib/schema/typebox/gen/codegen/model/model-to-arktype.js +0 -239
  211. package/lib/schema/typebox/gen/codegen/model/model-to-arktype.js.map +0 -1
  212. package/lib/schema/typebox/gen/codegen/model/model-to-expression.js +0 -359
  213. package/lib/schema/typebox/gen/codegen/model/model-to-expression.js.map +0 -1
  214. package/lib/schema/typebox/gen/codegen/model/model-to-grpc.js +0 -217
  215. package/lib/schema/typebox/gen/codegen/model/model-to-grpc.js.map +0 -1
  216. package/lib/schema/typebox/gen/codegen/model/model-to-io-ts.js +0 -253
  217. package/lib/schema/typebox/gen/codegen/model/model-to-io-ts.js.map +0 -1
  218. package/lib/schema/typebox/gen/codegen/model/model-to-javascript.js +0 -47
  219. package/lib/schema/typebox/gen/codegen/model/model-to-javascript.js.map +0 -1
  220. package/lib/schema/typebox/gen/codegen/model/model-to-json-schema.js +0 -171
  221. package/lib/schema/typebox/gen/codegen/model/model-to-json-schema.js.map +0 -1
  222. package/lib/schema/typebox/gen/codegen/model/model-to-typebox.js +0 -33
  223. package/lib/schema/typebox/gen/codegen/model/model-to-typebox.js.map +0 -1
  224. package/lib/schema/typebox/gen/codegen/model/model-to-typescript.js +0 -164
  225. package/lib/schema/typebox/gen/codegen/model/model-to-typescript.js.map +0 -1
  226. package/lib/schema/typebox/gen/codegen/model/model-to-valibot.js +0 -218
  227. package/lib/schema/typebox/gen/codegen/model/model-to-valibot.js.map +0 -1
  228. package/lib/schema/typebox/gen/codegen/model/model-to-value.js +0 -43
  229. package/lib/schema/typebox/gen/codegen/model/model-to-value.js.map +0 -1
  230. package/lib/schema/typebox/gen/codegen/model/model-to-yrel.js +0 -206
  231. package/lib/schema/typebox/gen/codegen/model/model-to-yrel.js.map +0 -1
  232. package/lib/schema/typebox/gen/codegen/model/model-to-yup.js +0 -204
  233. package/lib/schema/typebox/gen/codegen/model/model-to-yup.js.map +0 -1
  234. package/lib/schema/typebox/gen/codegen/model/model-to-zod.js +0 -227
  235. package/lib/schema/typebox/gen/codegen/model/model-to-zod.js.map +0 -1
  236. package/lib/schema/typebox/gen/codegen/model/model.js +0 -27
  237. package/lib/schema/typebox/gen/codegen/model/model.js.map +0 -1
  238. package/lib/schema/typebox/gen/codegen/typescript/index.js +0 -4
  239. package/lib/schema/typebox/gen/codegen/typescript/index.js.map +0 -1
  240. package/lib/schema/typebox/gen/codegen/typescript/typescript-to-model.js +0 -48
  241. package/lib/schema/typebox/gen/codegen/typescript/typescript-to-model.js.map +0 -1
  242. package/lib/schema/typebox/gen/codegen/typescript/typescript-to-typebox.js +0 -596
  243. package/lib/schema/typebox/gen/codegen/typescript/typescript-to-typebox.js.map +0 -1
  244. package/lib/schema/typebox/gen/index.js +0 -3
  245. package/lib/schema/typebox/gen/index.js.map +0 -1
  246. package/lib/schema/typebox/index.js +0 -3
  247. package/lib/schema/typebox/index.js.map +0 -1
  248. package/lib/schema/typebox/typebox.js +0 -23
  249. package/lib/schema/typebox/typebox.js.map +0 -1
  250. package/lib/server.js +0 -7
  251. package/lib/server.js.map +0 -1
  252. package/lib/servers/crypto/md5.js +0 -6
  253. package/lib/servers/crypto/md5.js.map +0 -1
  254. package/lib/servers/fetch/createFetchWithProxy.js +0 -4
  255. package/lib/servers/fetch/createFetchWithProxy.js.map +0 -1
  256. package/lib/servers/fetch/createFetchWithProxyByNodeFetch.js +0 -38
  257. package/lib/servers/fetch/createFetchWithProxyByNodeFetch.js.map +0 -1
  258. package/lib/servers/fetch/createFetchWithProxyByUndici.js +0 -64
  259. package/lib/servers/fetch/createFetchWithProxyByUndici.js.map +0 -1
  260. package/lib/servers/getPackageDir.js +0 -16
  261. package/lib/servers/getPackageDir.js.map +0 -1
  262. package/lib/servers/jsdom.js +0 -3
  263. package/lib/servers/jsdom.js.map +0 -1
  264. package/lib/servers/node-fetch.js +0 -4
  265. package/lib/servers/node-fetch.js.map +0 -1
  266. package/lib/servers/polyfill/polyfillBrowser.js +0 -17
  267. package/lib/servers/polyfill/polyfillBrowser.js.map +0 -1
  268. package/lib/servers/polyfill/polyfillCrypto.js +0 -11
  269. package/lib/servers/polyfill/polyfillCrypto.js.map +0 -1
  270. package/lib/servers/polyfill/polyfillFetch.js +0 -25
  271. package/lib/servers/polyfill/polyfillFetch.js.map +0 -1
  272. package/lib/servers/polyfill/polyfillJsDom.js +0 -64
  273. package/lib/servers/polyfill/polyfillJsDom.js.map +0 -1
  274. package/lib/servers/polyfill/polyfillWebSocket.js +0 -16
  275. package/lib/servers/polyfill/polyfillWebSocket.js.map +0 -1
  276. package/lib/servers/ws.js +0 -3
  277. package/lib/servers/ws.js.map +0 -1
  278. package/lib/strings/camelCase.js +0 -58
  279. package/lib/strings/camelCase.js.map +0 -1
  280. package/lib/strings/formatBytes.js +0 -43
  281. package/lib/strings/formatBytes.js.map +0 -1
  282. package/lib/strings/renderTemplate.js +0 -32
  283. package/lib/strings/renderTemplate.js.map +0 -1
  284. package/lib/typedoc.js +0 -4
  285. package/lib/typedoc.js.map +0 -1
  286. package/lib/types/global.d.js +0 -2
  287. package/lib/types/global.d.js.map +0 -1
  288. package/lib/types/index.d.js +0 -6
  289. package/lib/types/index.d.js.map +0 -1
  290. package/lib/validations/asserts.js +0 -14
  291. package/lib/validations/asserts.js.map +0 -1
  292. package/lib/validations/isUUID.js +0 -5
  293. package/lib/validations/isUUID.js.map +0 -1
  294. package/lib/validations/parseTimestamp.js +0 -29
  295. package/lib/validations/parseTimestamp.js.map +0 -1
  296. package/schema/typebox/gen/index.ts +0 -1
  297. package/schema/typebox/index.ts +0 -1
  298. package/server/jsdom.ts +0 -1
  299. package/server/node-fetch.ts +0 -1
  300. package/server/ws.ts +0 -1
  301. package/server.ts +0 -1
  302. package/src/server.ts +0 -6
  303. package/src/types/global.d.ts +0 -11
  304. package/types.d.ts +0 -1
  305. /package/src/{servers → server}/crypto/md5.ts +0 -0
  306. /package/src/{servers → server}/fetch/createFetchWithProxy.ts +0 -0
  307. /package/src/{servers → server}/fetch/createFetchWithProxyByNodeFetch.ts +0 -0
  308. /package/src/{servers → server}/fetch/createFetchWithProxyByUndici.ts +0 -0
  309. /package/src/{servers → server}/getPackageDir.ts +0 -0
  310. /package/src/{servers → server}/jsdom.ts +0 -0
  311. /package/src/{servers → server}/node-fetch.ts +0 -0
  312. /package/src/{servers → server}/polyfill/polyfillBrowser.test.ts +0 -0
  313. /package/src/{servers → server}/polyfill/polyfillBrowser.ts +0 -0
  314. /package/src/{servers → server}/polyfill/polyfillCrypto.ts +0 -0
  315. /package/src/{servers → server}/polyfill/polyfillFetch.ts +0 -0
  316. /package/src/{servers → server}/polyfill/polyfillJsDom.ts +0 -0
  317. /package/src/{servers → server}/ws.ts +0 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wener/utils",
3
- "version": "1.1.31",
3
+ "version": "1.1.33",
4
4
  "type": "module",
5
5
  "description": "Utils for daily use",
6
6
  "repository": {
@@ -10,16 +10,44 @@
10
10
  "homepage": "https://github.com/wenerme/wode#readme",
11
11
  "author": "wener",
12
12
  "license": "MIT",
13
+ "exports": {
14
+ ".": {
15
+ "types": "./src/index.ts",
16
+ "default": "./lib/index.js"
17
+ },
18
+ "./types": "./src/types.d.ts",
19
+ "./server": {
20
+ "types": "./src/server/index.ts",
21
+ "default": "./lib/server/index.js"
22
+ },
23
+ "./server/node-fetch": {
24
+ "types": "./src/server/node-fetch.ts",
25
+ "default": "./lib/server/node-fetch.js"
26
+ },
27
+ "./server/jsdom": {
28
+ "types": "./src/server/jsdom.ts",
29
+ "default": "./lib/server/jsdom.js"
30
+ },
31
+ "./server/ws": {
32
+ "types": "./src/server/ws.ts",
33
+ "default": "./lib/server/ws.js"
34
+ },
35
+ "./schema/typebox": {
36
+ "types": "./src/schema/typebox/index.ts",
37
+ "default": "./lib/schema/typebox/index.js"
38
+ },
39
+ "./schema/typebox/gen": {
40
+ "types": "./src/schema/typebox/gen/index.ts",
41
+ "default": "./lib/schema/typebox/gen/index.js"
42
+ },
43
+ "./package.json": "./package.json"
44
+ },
13
45
  "files": [
14
46
  "dist",
15
47
  "index.ts",
16
48
  "lib",
17
- "server",
18
- "schema",
19
- "server.ts",
20
49
  "src",
21
- "tsconfig.json",
22
- "types.d.ts"
50
+ "tsconfig.json"
23
51
  ],
24
52
  "keywords": [
25
53
  "utils",
@@ -40,13 +68,13 @@
40
68
  }
41
69
  },
42
70
  "devDependencies": {
43
- "@sinclair/typebox": "^0.32.8",
71
+ "@sinclair/typebox": "^0.32.15",
44
72
  "@types/lodash": "^4.14.202",
45
73
  "@types/ws": "^8.5.10",
46
- "https-proxy-agent": "^7.0.2",
74
+ "https-proxy-agent": "^7.0.4",
47
75
  "lodash": "^4.17.21",
48
76
  "node-fetch": "^3.3.2",
49
- "undici": "^6.3.0",
77
+ "undici": "^6.7.0",
50
78
  "zod": "^3.22.4"
51
79
  },
52
80
  "publishConfig": {
@@ -54,70 +82,10 @@
54
82
  "access": "public"
55
83
  },
56
84
  "sideEffects": false,
57
- "rollup": {
58
- "input": [
59
- "./src/index.ts",
60
- "./src/server.ts",
61
- "./src/servers/ws.ts",
62
- "./src/servers/jsdom.ts",
63
- "./src/servers/node-fetch.ts",
64
- "./src/schema/typebox/index.ts",
65
- "./src/schema/typebox/gen/index.ts"
66
- ],
67
- "external": [
68
- "crypto",
69
- "jsdom",
70
- "node-fetch",
71
- "ws",
72
- "https-proxy-agent",
73
- "undici",
74
- "zod",
75
- "typescript",
76
- "@sinclair/typebox"
77
- ]
78
- },
79
85
  "scripts": {
80
86
  "build": "make prepublish",
81
87
  "clean": "make clean",
82
88
  "dev": "make dev",
83
89
  "test": "make test"
84
- },
85
- "exports": {
86
- ".": {
87
- "types": "./src/index.ts",
88
- "typescript": "./src/index.ts",
89
- "system": "./dist/system/index.js",
90
- "import": "./lib/index.js"
91
- },
92
- "./types": {
93
- "types": "./types.d.ts"
94
- },
95
- "./server": {
96
- "types": "./src/server.ts",
97
- "typescript": "./src/server.ts",
98
- "system": "./dist/system/server.js",
99
- "import": "./lib/server.js"
100
- },
101
- "./server/node-fetch": {
102
- "types": "./server/node-fetch.ts",
103
- "default": "./lib/servers/node-fetch.js"
104
- },
105
- "./server/jsdom": {
106
- "types": "./server/jsdom.ts",
107
- "default": "./lib/servers/jsdom.js"
108
- },
109
- "./server/ws": {
110
- "types": "./server/ws.ts",
111
- "default": "./lib/servers/ws.js"
112
- },
113
- "./schema/typebox": {
114
- "types": "./schema/typebox/index.ts",
115
- "default": "./lib/schema/typebox/index.js"
116
- },
117
- "./schema/typebox/gen": {
118
- "types": "./schema/typebox/gen/index.ts",
119
- "default": "./lib/schema/typebox/gen/index.js"
120
- },
121
- "./package.json": "./package.json"
122
90
  }
123
91
  }
@@ -2,12 +2,16 @@ import { MaybePromise } from './MaybePromise';
2
2
  import { isIterator } from './isIterator';
3
3
  import { isPromise } from './isPromise';
4
4
 
5
+ type IteratorLike<T> = Iterable<T> | Iterator<T>;
6
+ type AsyncIteratorLike<T> = AsyncIterable<T> | AsyncIterator<T> | AsyncIterableIterator<T> | IteratorLike<T>;
7
+
5
8
  export function nextOfAsyncIterator<T>(
6
- it: MaybePromise<AsyncIterator<T> | Iterator<T> | T>,
9
+ it: MaybePromise<AsyncIterable<T> | Iterable<T> | AsyncIterator<T> | Iterator<T> | T>,
7
10
  ): MaybePromise<[value: T, done?: boolean]> {
8
11
  if (isPromise(it)) {
9
12
  return it.then((v) => nextOfAsyncIterator(v));
10
13
  }
14
+
11
15
  if (isIterator(it)) {
12
16
  let next = it.next();
13
17
  if (isPromise(next)) {
@@ -15,5 +19,13 @@ export function nextOfAsyncIterator<T>(
15
19
  }
16
20
  return [next.value, next.done];
17
21
  }
22
+ if (it && typeof it === 'object') {
23
+ if (Symbol.iterator in it) {
24
+ return nextOfAsyncIterator(it[Symbol.iterator]());
25
+ }
26
+ if (Symbol.asyncIterator in it) {
27
+ return nextOfAsyncIterator(it[Symbol.asyncIterator]());
28
+ }
29
+ }
18
30
  return [it];
19
31
  }
@@ -1,23 +1,26 @@
1
- export function timeout<T = any>(v: Promise<T>, ms: number): Promise<T> {
1
+ export function timeout<T = any>(
2
+ v: Promise<T> | ((args: { signal: AbortSignal }) => Promise<T>),
3
+ ms: number,
4
+ ): Promise<T> {
2
5
  const error = new TimeoutError();
3
6
  let timeout: any;
4
- return Promise.race([
7
+ let ac: AbortController | undefined;
8
+ if (typeof v === 'function') {
9
+ ac = new AbortController();
10
+ v = v({ signal: ac.signal });
11
+ }
12
+
13
+ return Promise.race<T>([
5
14
  v,
6
15
  new Promise((_resolve, reject) => {
7
16
  timeout = setTimeout(() => {
17
+ ac?.abort(error);
8
18
  reject(error);
9
19
  }, ms);
10
20
  }),
11
- ]).then(
12
- (v) => {
13
- clearTimeout(timeout);
14
- return v;
15
- },
16
- (e) => {
17
- clearTimeout(timeout);
18
- throw e;
19
- },
20
- ) as Promise<T>;
21
+ ]).finally(() => {
22
+ clearTimeout(timeout);
23
+ });
21
24
  }
22
25
 
23
26
  export class TimeoutError extends Error {
@@ -0,0 +1 @@
1
+ export { parseChineseNumber } from './parseChineseNumber';
@@ -0,0 +1,24 @@
1
+ import { expect, test } from 'vitest';
2
+ import { parseChineseNumber } from './parseChineseNumber';
3
+
4
+ test('parseChineseNumber', () => {
5
+ for (const [a, b] of [
6
+ ['0', 0],
7
+ ['10000', 10_000],
8
+ ['123', 123],
9
+ ['一万', 10_000],
10
+ ['一万一', 10_001],
11
+ ['一万一百一', 10_101],
12
+ ['一万零一百零一', 10_101],
13
+ ['一千零一十', 1010],
14
+ ['一千零一十一', 1011],
15
+ ['一捌玖', 189],
16
+ ['一百一', 101],
17
+ ['一百九十二', 192],
18
+ ['捌玖', 89],
19
+ ['捌玖零', 890],
20
+ ['零捌玖', 89],
21
+ ] as const) {
22
+ expect(parseChineseNumber(a)).toBe(b);
23
+ }
24
+ });
@@ -0,0 +1,97 @@
1
+ const ToNumber: Record<string, number> = {
2
+ 〇: 0,
3
+ 一: 1,
4
+ 二: 2,
5
+ 三: 3,
6
+ 四: 4,
7
+ 五: 5,
8
+ 六: 6,
9
+ 七: 7,
10
+ 八: 8,
11
+ 九: 9,
12
+ 十: 10,
13
+ 百: 100,
14
+ 千: 1000,
15
+ 万: 10_000,
16
+ 零: 0,
17
+ 壹: 1,
18
+ 贰: 2,
19
+ 叁: 3,
20
+ 肆: 4,
21
+ 伍: 5,
22
+ 陆: 6,
23
+ 柒: 7,
24
+ 捌: 8,
25
+ 玖: 9,
26
+ 拾: 10,
27
+ 佰: 100,
28
+ 仟: 1000,
29
+ 0: 0,
30
+ 1: 1,
31
+ 2: 2,
32
+ 3: 3,
33
+ 4: 4,
34
+ 5: 5,
35
+ 6: 6,
36
+ 7: 7,
37
+ 8: 8,
38
+ 9: 9,
39
+ };
40
+
41
+ // 一万一 -> 10001
42
+ // 一万一百一 -> 10101
43
+ // 一万 -> 10000
44
+ // 10000 -> 10000
45
+ export function parseChineseNumber(s: string | number) {
46
+ if (typeof s === 'number') {
47
+ return s;
48
+ }
49
+
50
+ let n = 0;
51
+ let c = 0;
52
+ let base = 0;
53
+ let shouldAddLast = false;
54
+ for (const char of s.split('').reverse()) {
55
+ c = ToNumber[char];
56
+ if (c === undefined) {
57
+ throw new Error(`无法解析的数字: ${char}`);
58
+ }
59
+
60
+ switch (c) {
61
+ case 10: {
62
+ base = 1;
63
+ break;
64
+ }
65
+
66
+ case 100: {
67
+ base = 2;
68
+ break;
69
+ }
70
+
71
+ case 1000: {
72
+ base = 3;
73
+ break;
74
+ }
75
+
76
+ case 10_000: {
77
+ base = 4;
78
+ break;
79
+ }
80
+ }
81
+
82
+ //
83
+ if (c < 10) {
84
+ n += c * 10 ** base;
85
+ base++;
86
+ shouldAddLast = false;
87
+ } else {
88
+ shouldAddLast = true;
89
+ }
90
+ }
91
+
92
+ if (shouldAddLast) {
93
+ n += c;
94
+ }
95
+
96
+ return n;
97
+ }
@@ -11,9 +11,11 @@ export let getRandomValues: <T extends Exclude<TypedArray, Float32Array | Float6
11
11
 
12
12
  function _getRandomValues<T extends Exclude<TypedArray, Float32Array | Float64Array>>(buf: T) {
13
13
  const nodeCrypto = getNodeCrypto();
14
- if (nodeCrypto?.webcrypto?.getRandomValues) {
15
- getRandomValues = nodeCrypto?.webcrypto?.getRandomValues?.bind(nodeCrypto?.webcrypto);
16
- return nodeCrypto.webcrypto.getRandomValues(buf);
14
+ // avoid type error
15
+ let wc = nodeCrypto?.webcrypto as any;
16
+ if (wc?.getRandomValues) {
17
+ getRandomValues = wc.getRandomValues?.bind(nodeCrypto?.webcrypto);
18
+ return wc.getRandomValues(buf);
17
19
  }
18
20
  if (nodeCrypto?.randomBytes) {
19
21
  if (!(buf instanceof Uint8Array)) {
@@ -1,6 +1,6 @@
1
1
  import { assert, beforeAll, expect, test } from 'vitest';
2
2
  import { ArrayBuffers } from '../io/ArrayBuffers';
3
- import { polyfillCrypto } from '../servers/polyfill/polyfillCrypto';
3
+ import { polyfillCrypto } from '../server/polyfill/polyfillCrypto';
4
4
  import { isUUID } from '../validations/isUUID';
5
5
  import { hex } from './base';
6
6
  import { hmac, sha1, sha256, sha384, sha512 } from './hashing';
@@ -0,0 +1,22 @@
1
+ import { test } from 'vitest';
2
+ import { Errors } from './Errors';
3
+
4
+ test('Errors', () => {
5
+ {
6
+ let a: string | undefined = '';
7
+ Errors.NotFound.check(a);
8
+ let b: string = a;
9
+ hole(b);
10
+ }
11
+ if (false) {
12
+ // 不能同时 asserts 且返回
13
+ // https://stackoverflow.com/a/73252858/1870054
14
+ // https://github.com/microsoft/TypeScript/issues/34636
15
+ let a: string | undefined;
16
+ Errors.NotFound.require(a);
17
+ // let c: string = a;
18
+ // hole(c);
19
+ }
20
+ });
21
+
22
+ function hole(..._: any[]) {}
@@ -147,9 +147,6 @@ export interface ErrorDetail {
147
147
 
148
148
  require<T>(v: T | undefined, o?: Partial<ErrorDetailInit> | string): NonNullable<T>;
149
149
 
150
- // 不支持 return value
151
- // https://stackoverflow.com/a/73252858/1870054
152
-
153
150
  check(condition: boolean, o?: Partial<ErrorDetailInit> | string): asserts condition;
154
151
 
155
152
  check<T>(v: T | undefined | null, o?: Partial<ErrorDetailInit> | string): asserts v is NonNullable<T>;
@@ -160,7 +157,7 @@ export class Errors {
160
157
  static Unauthorized: ErrorDetail = this.with({ status: 403 });
161
158
  static Forbidden: ErrorDetail = this.with({ status: 403 });
162
159
  static NotFound: ErrorDetail = this.with({ status: 404 });
163
- static InternalServerError: ErrorDetail = this.with({ status: 501 });
160
+ static InternalServerError: ErrorDetail = this.with({ status: 500 });
164
161
  static NotImplemented: ErrorDetail = this.with({ status: 501 });
165
162
  static ServiceUnavailable: ErrorDetail = this.with({ status: 503 });
166
163
 
package/src/index.ts CHANGED
@@ -42,6 +42,8 @@ export { isPlainObject } from './langs/isPlainObject';
42
42
  export { parseBoolean } from './langs/parseBoolean';
43
43
  export { maybeFunction, type MaybeFunction } from './langs/MaybeFunction';
44
44
  export { memoize } from './langs/memoize';
45
+ export { AsyncCloser } from './langs/AsyncCloser';
46
+ export { Closer } from './langs/Closer';
45
47
 
46
48
  export { isUUID } from './validations/isUUID';
47
49
  export { parseTimestamp } from './validations/parseTimestamp';
@@ -0,0 +1,64 @@
1
+ const objectToString = Object.prototype.toString;
2
+ const uint8ArrayStringified = '[object Uint8Array]';
3
+
4
+ /**
5
+ * Utils for UInt8Array
6
+ *
7
+ * @see https://github.com/sindresorhus/uint8array-extras/blob/main/index.js
8
+ */
9
+ export class Bytes {
10
+ static #decoder = new globalThis.TextDecoder();
11
+
12
+ static isUint8Array(value: any): value is Uint8Array {
13
+ if (!value) {
14
+ return false;
15
+ }
16
+
17
+ if (value.constructor === Uint8Array) {
18
+ return true;
19
+ }
20
+
21
+ return objectToString.call(value) === uint8ArrayStringified;
22
+ }
23
+
24
+ static require(value: any): Uint8Array {
25
+ if (!this.isUint8Array(value)) {
26
+ throw new TypeError(`Expected \`Uint8Array\`, got \`${typeof value}\``);
27
+ }
28
+ return value;
29
+ }
30
+
31
+ static toUint8Array(value: ArrayBuffer | ArrayBufferView): Uint8Array {
32
+ if (value instanceof ArrayBuffer) {
33
+ return new Uint8Array(value);
34
+ }
35
+
36
+ if (ArrayBuffer.isView(value)) {
37
+ return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
38
+ }
39
+
40
+ throw new TypeError(`Unsupported value, got \`${typeof value}\`.`);
41
+ }
42
+
43
+ static concat(arrays: Uint8Array[], totalLength: number): Uint8Array {
44
+ if (arrays.length === 0) {
45
+ return new Uint8Array(0);
46
+ }
47
+
48
+ totalLength ??= arrays.reduce((accumulator, currentValue) => accumulator + currentValue.length, 0);
49
+
50
+ const returnValue = new Uint8Array(totalLength);
51
+
52
+ let offset = 0;
53
+ for (const array of arrays) {
54
+ returnValue.set(this.require(array), offset);
55
+ offset += array.length;
56
+ }
57
+
58
+ return returnValue;
59
+ }
60
+
61
+ static toString(value: Uint8Array) {
62
+ return this.#decoder.decode(value);
63
+ }
64
+ }
@@ -1,8 +1,5 @@
1
1
  /* eslint no-proto:0 */
2
2
  import { classOf } from '../langs/classOf';
3
- import { getGlobalThis } from './getGlobalThis';
4
-
5
- const globalThis = getGlobalThis();
6
3
 
7
4
  /**
8
5
  * Clone an object using structured cloning algorithm
@@ -0,0 +1,45 @@
1
+ type AsyncClosable = { close(): void | Promise<void> } | (() => void | Promise<void>) | AsyncDisposable | Disposable;
2
+
3
+ /**
4
+ * @see AsyncDisposableStack
5
+ */
6
+ export class AsyncCloser implements AsyncDisposable {
7
+ closers: Array<AsyncClosable> = [];
8
+
9
+ defer(closer: AsyncClosable | null | undefined) {
10
+ if (!closer) {
11
+ return;
12
+ }
13
+ this.closers.push(closer);
14
+ }
15
+
16
+ add<T extends AsyncClosable | null | undefined>(closer: T): T {
17
+ if (!closer) {
18
+ return closer;
19
+ }
20
+ this.closers.push(closer);
21
+ return closer;
22
+ }
23
+
24
+ async [Symbol.asyncDispose]() {
25
+ for (let closer of this.closers) {
26
+ let o;
27
+ if (typeof closer === 'function') {
28
+ o = closer();
29
+ } else if (Symbol.asyncDispose in closer) {
30
+ o = closer[Symbol.asyncDispose]();
31
+ } else if (Symbol.dispose in closer) {
32
+ closer[Symbol.dispose]();
33
+ } else {
34
+ o = closer.close();
35
+ }
36
+ if (o && typeof o.then === 'function') {
37
+ await o;
38
+ }
39
+ }
40
+ }
41
+
42
+ close() {
43
+ return this[Symbol.asyncDispose]();
44
+ }
45
+ }
@@ -0,0 +1,39 @@
1
+ type Closable = { close(): void } | (() => void) | Disposable;
2
+
3
+ /**
4
+ * @see DisposableStack
5
+ */
6
+ export class Closer implements Disposable {
7
+ closers: Array<Closable> = [];
8
+
9
+ defer(closer: Closable | null | undefined) {
10
+ if (!closer) {
11
+ return;
12
+ }
13
+ this.closers.push(closer);
14
+ }
15
+
16
+ add<T extends Closable | null | undefined>(closer: T): T {
17
+ if (!closer) {
18
+ return closer;
19
+ }
20
+ this.closers.push(closer);
21
+ return closer;
22
+ }
23
+
24
+ [Symbol.dispose]() {
25
+ for (let closer of this.closers) {
26
+ if (typeof closer === 'function') {
27
+ closer();
28
+ } else if (Symbol.dispose in closer) {
29
+ closer[Symbol.dispose]();
30
+ } else {
31
+ closer.close();
32
+ }
33
+ }
34
+ }
35
+
36
+ close() {
37
+ this[Symbol.dispose]();
38
+ }
39
+ }
@@ -0,0 +1,6 @@
1
+ export { polyfillCrypto } from './polyfill/polyfillCrypto';
2
+ export { createFetchWithProxyByUndici } from './fetch/createFetchWithProxyByUndici';
3
+ export { createFetchWithProxy } from './fetch/createFetchWithProxy';
4
+
5
+ export { md5 } from './crypto/md5';
6
+ export { getPackageDir } from './getPackageDir';
@@ -8,6 +8,11 @@ export function polyfillWebSocket(ws?: any): MaybePromise<boolean> {
8
8
  if ('WebSocket' in globalThis) {
9
9
  return false;
10
10
  }
11
+ if ('then' in ws) {
12
+ return ws.then((v: any) => {
13
+ return polyfillWebSocket(v?.default || v);
14
+ });
15
+ }
11
16
  if (ws) {
12
17
  const { WebSocket } = ws;
13
18
  Object.assign(globalThis, { WebSocket });
@@ -1,3 +1,15 @@
1
+ declare var __DEV__: boolean;
2
+
3
+ namespace NodeJS {
4
+ interface Process {
5
+ // webpack check
6
+ readonly browser?: boolean;
7
+ }
8
+ interface ProcessEnv {
9
+ readonly NODE_ENV: 'development' | 'production' | 'test';
10
+ }
11
+ }
12
+
1
13
  export type AbstractConstructor<T = unknown> = abstract new (...args: any[]) => T;
2
14
  export type Constructor<T = unknown> = new (...args: any[]) => T;
3
15
  export type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
package/tsconfig.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "compilerOptions": {
3
- "moduleResolution": "node",
3
+ "moduleResolution": "bundler",
4
4
  "target": "ESNext",
5
5
  "module": "ESNext",
6
6
  "lib": [
@@ -15,17 +15,19 @@
15
15
  "allowSyntheticDefaultImports": true,
16
16
  "noImplicitAny": true,
17
17
  "noImplicitReturns": true,
18
- "noUnusedLocals": true,
19
- "noUnusedParameters": true,
18
+ "noUnusedLocals": false,
19
+ "noUnusedParameters": false,
20
20
  "pretty": true,
21
21
  "removeComments": false,
22
- "stripInternal": true
22
+ "stripInternal": true,
23
+ "isolatedModules": true,
24
+ "outDir": "lib"
23
25
  },
24
26
  "exclude": [
25
27
  "node_modules"
26
28
  ],
27
29
  "include": [
28
- "types.d.ts",
30
+ "src/types.d.ts",
29
31
  "src"
30
32
  ]
31
33
  }
package/index.ts DELETED
@@ -1 +0,0 @@
1
- export * from './src';