@wener/utils 1.1.48 → 1.1.49

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 (145) hide show
  1. package/lib/asyncs/Promises.js +9 -1
  2. package/lib/asyncs/Promises.js.map +1 -1
  3. package/lib/asyncs/createLazyPromise.js +52 -51
  4. package/lib/asyncs/createLazyPromise.js.map +1 -1
  5. package/lib/asyncs/firstOfAsyncIterator.js.map +1 -1
  6. package/lib/asyncs/nextOfAsyncIterator.js.map +1 -1
  7. package/lib/asyncs/timeout.js +4 -4
  8. package/lib/asyncs/timeout.js.map +1 -1
  9. package/lib/cn/types.js +3 -0
  10. package/lib/cn/types.js.map +1 -0
  11. package/lib/crypto/getNodeCrypto.js +1 -0
  12. package/lib/crypto/getNodeCrypto.js.map +1 -1
  13. package/lib/fetch/createFetchWith.js.map +1 -1
  14. package/lib/fetch/createFetchWithRetry.js.map +1 -1
  15. package/lib/index.js +2 -0
  16. package/lib/index.js.map +1 -1
  17. package/lib/io/ArrayBuffers.js.map +1 -1
  18. package/lib/io/ByteBuffer.js +233 -34
  19. package/lib/io/ByteBuffer.js.map +1 -1
  20. package/lib/langs/getGlobalStates.js +17 -0
  21. package/lib/langs/getGlobalStates.js.map +1 -0
  22. package/lib/langs/getObjectId.js +18 -0
  23. package/lib/langs/getObjectId.js.map +1 -0
  24. package/lib/langs/isNullish.js +5 -0
  25. package/lib/langs/isNullish.js.map +1 -0
  26. package/lib/logging/slog.js.map +1 -1
  27. package/lib/maths/clamp.js +7 -6
  28. package/lib/maths/clamp.js.map +1 -1
  29. package/lib/schema/typebox/typebox.js.map +1 -1
  30. package/lib/server/fetch/createFetchWithProxyByUndici.js.map +1 -1
  31. package/package.json +3 -3
  32. package/src/asyncs/Promises.ts +12 -2
  33. package/src/asyncs/createLazyPromise.test.ts +52 -13
  34. package/src/asyncs/createLazyPromise.ts +66 -61
  35. package/src/asyncs/firstOfAsyncIterator.ts +1 -1
  36. package/src/asyncs/nextOfAsyncIterator.ts +1 -1
  37. package/src/asyncs/timeout.ts +4 -4
  38. package/src/cn/types.ts +11 -0
  39. package/src/crypto/getNodeCrypto.ts +1 -0
  40. package/src/fetch/createFetchWith.ts +1 -1
  41. package/src/fetch/createFetchWithRetry.ts +1 -1
  42. package/src/index.ts +2 -0
  43. package/src/io/ArrayBuffers.ts +7 -3
  44. package/src/io/ByteBuffer.test.ts +26 -14
  45. package/src/io/ByteBuffer.ts +270 -38
  46. package/src/langs/getGlobalStates.ts +21 -0
  47. package/src/langs/getObjectId.ts +20 -0
  48. package/src/langs/isNullish.ts +3 -0
  49. package/src/langs/mixin.test.ts +4 -0
  50. package/src/logging/slog.ts +1 -1
  51. package/src/maths/clamp.test.ts +5 -1
  52. package/src/maths/clamp.ts +7 -7
  53. package/src/objects/merge/merge.test.ts +1 -1
  54. package/src/schema/typebox/typebox.ts +1 -1
  55. package/src/server/fetch/createFetchWithProxyByUndici.ts +2 -2
  56. package/tsconfig.json +4 -1
  57. package/lib/schema/typebox/gen/codegen/common/encoder.js +0 -94
  58. package/lib/schema/typebox/gen/codegen/common/encoder.js.map +0 -1
  59. package/lib/schema/typebox/gen/codegen/common/formatter.js +0 -33
  60. package/lib/schema/typebox/gen/codegen/common/formatter.js.map +0 -1
  61. package/lib/schema/typebox/gen/codegen/common/index.js +0 -29
  62. package/lib/schema/typebox/gen/codegen/common/index.js.map +0 -1
  63. package/lib/schema/typebox/gen/codegen/common/jsdoc.js +0 -117
  64. package/lib/schema/typebox/gen/codegen/common/jsdoc.js.map +0 -1
  65. package/lib/schema/typebox/gen/codegen/expression/compiler.js +0 -364
  66. package/lib/schema/typebox/gen/codegen/expression/compiler.js.map +0 -1
  67. package/lib/schema/typebox/gen/codegen/expression/errors.js +0 -259
  68. package/lib/schema/typebox/gen/codegen/expression/errors.js.map +0 -1
  69. package/lib/schema/typebox/gen/codegen/expression/evaluator.js +0 -254
  70. package/lib/schema/typebox/gen/codegen/expression/evaluator.js.map +0 -1
  71. package/lib/schema/typebox/gen/codegen/expression/expression.js +0 -381
  72. package/lib/schema/typebox/gen/codegen/expression/expression.js.map +0 -1
  73. package/lib/schema/typebox/gen/codegen/expression/index.js +0 -32
  74. package/lib/schema/typebox/gen/codegen/expression/index.js.map +0 -1
  75. package/lib/schema/typebox/gen/codegen/index.js +0 -29
  76. package/lib/schema/typebox/gen/codegen/index.js.map +0 -1
  77. package/lib/schema/typebox/gen/codegen/model/index.js +0 -40
  78. package/lib/schema/typebox/gen/codegen/model/index.js.map +0 -1
  79. package/lib/schema/typebox/gen/codegen/model/model-to-arktype.js +0 -260
  80. package/lib/schema/typebox/gen/codegen/model/model-to-arktype.js.map +0 -1
  81. package/lib/schema/typebox/gen/codegen/model/model-to-expression.js +0 -383
  82. package/lib/schema/typebox/gen/codegen/model/model-to-expression.js.map +0 -1
  83. package/lib/schema/typebox/gen/codegen/model/model-to-grpc.js +0 -238
  84. package/lib/schema/typebox/gen/codegen/model/model-to-grpc.js.map +0 -1
  85. package/lib/schema/typebox/gen/codegen/model/model-to-io-ts.js +0 -274
  86. package/lib/schema/typebox/gen/codegen/model/model-to-io-ts.js.map +0 -1
  87. package/lib/schema/typebox/gen/codegen/model/model-to-javascript.js +0 -47
  88. package/lib/schema/typebox/gen/codegen/model/model-to-javascript.js.map +0 -1
  89. package/lib/schema/typebox/gen/codegen/model/model-to-json-schema.js +0 -192
  90. package/lib/schema/typebox/gen/codegen/model/model-to-json-schema.js.map +0 -1
  91. package/lib/schema/typebox/gen/codegen/model/model-to-typebox.js +0 -33
  92. package/lib/schema/typebox/gen/codegen/model/model-to-typebox.js.map +0 -1
  93. package/lib/schema/typebox/gen/codegen/model/model-to-typescript.js +0 -188
  94. package/lib/schema/typebox/gen/codegen/model/model-to-typescript.js.map +0 -1
  95. package/lib/schema/typebox/gen/codegen/model/model-to-valibot.js +0 -239
  96. package/lib/schema/typebox/gen/codegen/model/model-to-valibot.js.map +0 -1
  97. package/lib/schema/typebox/gen/codegen/model/model-to-value.js +0 -43
  98. package/lib/schema/typebox/gen/codegen/model/model-to-value.js.map +0 -1
  99. package/lib/schema/typebox/gen/codegen/model/model-to-yrel.js +0 -227
  100. package/lib/schema/typebox/gen/codegen/model/model-to-yrel.js.map +0 -1
  101. package/lib/schema/typebox/gen/codegen/model/model-to-yup.js +0 -225
  102. package/lib/schema/typebox/gen/codegen/model/model-to-yup.js.map +0 -1
  103. package/lib/schema/typebox/gen/codegen/model/model-to-zod.js +0 -248
  104. package/lib/schema/typebox/gen/codegen/model/model-to-zod.js.map +0 -1
  105. package/lib/schema/typebox/gen/codegen/model/model.js +0 -27
  106. package/lib/schema/typebox/gen/codegen/model/model.js.map +0 -1
  107. package/lib/schema/typebox/gen/codegen/typescript/index.js +0 -28
  108. package/lib/schema/typebox/gen/codegen/typescript/index.js.map +0 -1
  109. package/lib/schema/typebox/gen/codegen/typescript/typescript-to-model.js +0 -72
  110. package/lib/schema/typebox/gen/codegen/typescript/typescript-to-model.js.map +0 -1
  111. package/lib/schema/typebox/gen/codegen/typescript/typescript-to-typebox.js +0 -620
  112. package/lib/schema/typebox/gen/codegen/typescript/typescript-to-typebox.js.map +0 -1
  113. package/lib/schema/typebox/gen/index.js +0 -3
  114. package/lib/schema/typebox/gen/index.js.map +0 -1
  115. package/src/schema/typebox/gen/codegen/common/encoder.ts +0 -99
  116. package/src/schema/typebox/gen/codegen/common/formatter.ts +0 -31
  117. package/src/schema/typebox/gen/codegen/common/index.ts +0 -29
  118. package/src/schema/typebox/gen/codegen/common/jsdoc.ts +0 -93
  119. package/src/schema/typebox/gen/codegen/expression/compiler.ts +0 -377
  120. package/src/schema/typebox/gen/codegen/expression/errors.ts +0 -302
  121. package/src/schema/typebox/gen/codegen/expression/evaluator.ts +0 -268
  122. package/src/schema/typebox/gen/codegen/expression/expression.ts +0 -538
  123. package/src/schema/typebox/gen/codegen/expression/index.ts +0 -32
  124. package/src/schema/typebox/gen/codegen/index.ts +0 -29
  125. package/src/schema/typebox/gen/codegen/model/index.ts +0 -40
  126. package/src/schema/typebox/gen/codegen/model/model-to-arktype.ts +0 -266
  127. package/src/schema/typebox/gen/codegen/model/model-to-expression.ts +0 -378
  128. package/src/schema/typebox/gen/codegen/model/model-to-grpc.ts +0 -244
  129. package/src/schema/typebox/gen/codegen/model/model-to-io-ts.ts +0 -294
  130. package/src/schema/typebox/gen/codegen/model/model-to-javascript.ts +0 -42
  131. package/src/schema/typebox/gen/codegen/model/model-to-json-schema.ts +0 -166
  132. package/src/schema/typebox/gen/codegen/model/model-to-typebox.ts +0 -32
  133. package/src/schema/typebox/gen/codegen/model/model-to-typescript.ts +0 -189
  134. package/src/schema/typebox/gen/codegen/model/model-to-valibot.ts +0 -236
  135. package/src/schema/typebox/gen/codegen/model/model-to-value.ts +0 -42
  136. package/src/schema/typebox/gen/codegen/model/model-to-yrel.ts +0 -232
  137. package/src/schema/typebox/gen/codegen/model/model-to-yup.ts +0 -226
  138. package/src/schema/typebox/gen/codegen/model/model-to-zod.ts +0 -251
  139. package/src/schema/typebox/gen/codegen/model/model.ts +0 -33
  140. package/src/schema/typebox/gen/codegen/typescript/index.ts +0 -28
  141. package/src/schema/typebox/gen/codegen/typescript/typescript-to-model.ts +0 -61
  142. package/src/schema/typebox/gen/codegen/typescript/typescript-to-typebox.ts +0 -647
  143. package/src/schema/typebox/gen/gen.test.ts +0 -12
  144. package/src/schema/typebox/gen/index.ts +0 -1
  145. /package/src/schema/{typebox/gen/README.md → README.md} +0 -0
@@ -34,18 +34,40 @@ function asBuffer(o: AnyBuffer) {
34
34
  /**
35
35
  * @see {@link https://www.egret.uk/docs/egretengine/engine/egret.ByteArray egret.ByteArray}
36
36
  * @see {@link https://github.com/protobufjs/bytebuffer.js protobufjs/bytebuffer.js}
37
- * @see {@link https://netty.io/4.1/api/io/netty/buffer/ByteBuf.html ByteBuf}
37
+ * @see {@link https://netty.io/4.1/api/io/netty/buffer/ByteBuf.html io.netty.buffer.ByteBuf}
38
+ * @see {@link https://nodejs.org/api/buffer.html node:buffer}
39
+ * @see {@link https://www.postgresql.org/docs/current/datatype.html PostgreSQL Data Types}
38
40
  */
39
41
  export class ByteBuffer {
40
42
  position = 0;
41
43
 
42
44
  #buffer: ArrayBufferLike;
43
45
  #view: DataView;
46
+ #bytes: Uint8Array;
47
+ // #endian: 'big' | 'little' = 'big';
48
+ #bigEndian = true;
44
49
 
45
50
  constructor(buffer: AnyBuffer = new ArrayBuffer(0, { maxByteLength: 1024 })) {
46
51
  this.#buffer = asBuffer(buffer);
47
52
  // NOTE prefer view over buffer, avoid the slice overhead ?
48
53
  this.#view = new DataView(this.#buffer);
54
+ this.#bytes = new Uint8Array(this.#buffer);
55
+ }
56
+
57
+ get littleEndian() {
58
+ return !this.#bigEndian;
59
+ }
60
+
61
+ set littleEndian(v: boolean) {
62
+ this.#bigEndian = !v;
63
+ }
64
+
65
+ get bigEndian() {
66
+ return this.#bigEndian;
67
+ }
68
+
69
+ set bigEndian(v: boolean) {
70
+ this.#bigEndian = v;
49
71
  }
50
72
 
51
73
  get view() {
@@ -59,6 +81,11 @@ export class ByteBuffer {
59
81
  set buffer(buffer: AnyBuffer) {
60
82
  this.#buffer = asBuffer(buffer);
61
83
  this.#view = new DataView(this.#buffer);
84
+ this.#bytes = new Uint8Array(this.#buffer);
85
+ }
86
+
87
+ get bytes(): Uint8Array {
88
+ return this.#bytes;
62
89
  }
63
90
 
64
91
  get length() {
@@ -69,13 +96,8 @@ export class ByteBuffer {
69
96
  this.resize(length);
70
97
  }
71
98
 
72
- private willWrite(length: number) {
73
- if (this.remaining() < length) {
74
- this.resize(this.position + length);
75
- }
76
- }
77
-
78
99
  resize(newLength: number) {
100
+ // 1.2 for buffer growth
79
101
  this.buffer = ArrayBuffers.resize(this.buffer, newLength, Math.ceil(newLength * 1.2));
80
102
  }
81
103
 
@@ -86,8 +108,13 @@ export class ByteBuffer {
86
108
 
87
109
  writeBytes(bytes: ArrayBufferLike, len: number = bytes.byteLength) {
88
110
  this.willWrite(len);
89
- if (len !== bytes.byteLength) bytes = bytes.slice(0, len);
90
- new Uint8Array(this.buffer).set(new Uint8Array(bytes), this.position);
111
+ // ensure len first
112
+ this.bytes.set(new Uint8Array(bytes).subarray(0, len), this.position);
113
+ if (len > bytes.byteLength) {
114
+ // fill zero
115
+ this.bytes.fill(0, this.position + bytes.byteLength, this.position + len);
116
+ }
117
+
91
118
  this.position += len;
92
119
  }
93
120
 
@@ -105,49 +132,56 @@ export class ByteBuffer {
105
132
 
106
133
  writeInt16(value: number) {
107
134
  this.willWrite(2);
108
- this.view.setInt16(this.position, value);
135
+ this.view.setInt16(this.position, value, this.littleEndian);
109
136
  this.position += 2;
110
137
  }
111
138
 
112
139
  writeUint16(value: number) {
113
140
  this.willWrite(2);
114
- this.view.setUint16(this.position, value);
141
+ this.view.setUint16(this.position, value, this.littleEndian);
115
142
  this.position += 2;
116
143
  }
117
144
 
145
+ writeUint24(value: number) {
146
+ this.willWrite(3);
147
+ this.view.setUint8(this.position, value & 0xff);
148
+ this.view.setUint16(this.position + 1, value >> 8, this.littleEndian);
149
+ this.position += 3;
150
+ }
151
+
118
152
  writeInt32(value: number) {
119
153
  this.willWrite(4);
120
- this.view.setInt32(this.position, value);
154
+ this.view.setInt32(this.position, value, this.littleEndian);
121
155
  this.position += 4;
122
156
  }
123
157
 
124
158
  writeUint32(value: number) {
125
159
  this.willWrite(4);
126
- this.view.setUint32(this.position, value);
160
+ this.view.setUint32(this.position, value, this.littleEndian);
127
161
  this.position += 4;
128
162
  }
129
163
 
130
- writeInt64(value: bigint) {
164
+ writeInt64(value: bigint | number) {
131
165
  this.willWrite(8);
132
- this.view.setBigInt64(this.position, value);
166
+ this.view.setBigInt64(this.position, bigintOf(value), this.littleEndian);
133
167
  this.position += 8;
134
168
  }
135
169
 
136
- writeUint64(value: bigint) {
170
+ writeUint64(value: bigint | number) {
137
171
  this.willWrite(8);
138
- this.view.setBigUint64(this.position, value);
172
+ this.view.setBigUint64(this.position, bigintOf(value), this.littleEndian);
139
173
  this.position += 8;
140
174
  }
141
175
 
142
176
  writeFloat32(value: number) {
143
177
  this.willWrite(4);
144
- this.view.setFloat32(this.position, value);
178
+ this.view.setFloat32(this.position, value, this.littleEndian);
145
179
  this.position += 4;
146
180
  }
147
181
 
148
182
  writeFloat64(value: number) {
149
183
  this.willWrite(8);
150
- this.view.setFloat64(this.position, value);
184
+ this.view.setFloat64(this.position, value, this.littleEndian);
151
185
  this.position += 8;
152
186
  }
153
187
 
@@ -156,13 +190,32 @@ export class ByteBuffer {
156
190
  }
157
191
 
158
192
  writeString(value: string): void;
193
+
159
194
  writeString(value: string, len?: number): void;
195
+
160
196
  writeString(value: string, len?: number) {
161
- let encoder = new TextEncoder();
162
- let bytes = encoder.encode(value);
197
+ let bytes = this.encodeText(value);
163
198
  this.writeBytes(bytes, len);
164
199
  }
165
200
 
201
+ readSizeString(n?: number) {
202
+ return this.readString(requireNumber(this.readUint(n)));
203
+ }
204
+
205
+ writeSizeString(value: string, n?: number) {
206
+ let out = this.encodeText(value);
207
+ this.writeUint(out.length, n);
208
+ this.writeBytes(out);
209
+ }
210
+
211
+ encodeText(value: string) {
212
+ return new TextEncoder().encode(value);
213
+ }
214
+
215
+ decodeText(bytes: ArrayBufferLike) {
216
+ return new TextDecoder().decode(bytes);
217
+ }
218
+
166
219
  readByte() {
167
220
  return this.view.getUint8(this.position++);
168
221
  }
@@ -185,67 +238,82 @@ export class ByteBuffer {
185
238
  return value;
186
239
  }
187
240
 
241
+ readUnsignedByte() {
242
+ return this.readUint8();
243
+ }
244
+
245
+ readThrough(end: number) {
246
+ // readUntil 不包含 end
247
+ let idx = this.bytes.indexOf(end, this.position);
248
+ if (idx === -1) {
249
+ throw new Error(`Byte ${end} not found starting from position ${this.position}`);
250
+ }
251
+ let bytes = this.buffer.slice(this.position, idx + 1);
252
+ this.position = idx + 1;
253
+ return bytes;
254
+ }
255
+
188
256
  readInt16() {
189
- let value = this.view.getInt16(this.position);
257
+ let value = this.view.getInt16(this.position, this.littleEndian);
190
258
  this.position += 2;
191
259
  return value;
192
260
  }
193
261
 
194
262
  readUint16() {
195
- let value = this.view.getUint16(this.position);
263
+ let value = this.view.getUint16(this.position, this.littleEndian);
196
264
  this.position += 2;
197
265
  return value;
198
266
  }
199
267
 
200
268
  readInt32() {
201
- let value = this.view.getInt32(this.position);
269
+ let value = this.view.getInt32(this.position, this.littleEndian);
202
270
  this.position += 4;
203
271
  return value;
204
272
  }
205
273
 
206
274
  readUint32() {
207
- let value = this.view.getUint32(this.position);
275
+ let value = this.view.getUint32(this.position, this.littleEndian);
208
276
  this.position += 4;
209
277
  return value;
210
278
  }
211
279
 
212
280
  readInt64() {
213
- let value = this.view.getBigInt64(this.position);
281
+ let value = this.view.getBigInt64(this.position, this.littleEndian);
214
282
  this.position += 8;
215
283
  return safeNumber(value);
216
284
  }
217
285
 
218
286
  readUint64() {
219
- let value = this.view.getBigUint64(this.position);
287
+ let value = this.view.getBigUint64(this.position, this.littleEndian);
220
288
  this.position += 8;
221
289
  return safeNumber(value);
222
290
  }
223
291
 
224
292
  readFloat32() {
225
- let value = this.view.getFloat32(this.position);
293
+ let value = this.view.getFloat32(this.position, this.littleEndian);
226
294
  this.position += 4;
227
295
  return value;
228
296
  }
229
297
 
230
298
  readFloat64() {
231
- let value = this.view.getFloat64(this.position);
299
+ let value = this.view.getFloat64(this.position, this.littleEndian);
232
300
  this.position += 8;
233
301
  return value;
234
302
  }
235
303
 
236
304
  readBoolean() {
305
+ // or !== 0?
237
306
  return this.readByte() === 1;
238
307
  }
239
308
 
240
- readString(length: number) {
309
+ readString(length: number): string {
241
310
  let bytes = this.readBytes(length);
242
- let decoder = new TextDecoder();
243
311
  let a = new Uint8Array(bytes);
244
312
  let idx = a.indexOf(0);
245
313
  if (idx !== -1) {
246
314
  bytes = bytes.slice(0, idx);
247
315
  }
248
- return decoder.decode(bytes);
316
+ return this.decodeText(bytes);
249
317
  }
250
318
 
251
319
  readHexString(length: number) {
@@ -253,17 +321,35 @@ export class ByteBuffer {
253
321
  return ArrayBuffers.toHex(bytes);
254
322
  }
255
323
 
324
+ writeHexString(value: string, len?: number) {
325
+ this.writeBytes(ArrayBuffers.fromHex(value), len);
326
+ }
327
+
256
328
  writeInt24(value: number) {
329
+ // fixme byte order
257
330
  this.writeUint8(value & 0xff);
258
331
  this.writeUint16(value >> 8);
259
332
  }
260
333
 
261
334
  readInt24() {
262
- return this.readUint8() | (this.readUint16() << 8);
335
+ // fixme recheck
336
+ let value = this.readUint24();
337
+ if (value & 0x800000) {
338
+ // If the sign bit is set, extend the sign
339
+ value |= 0xff000000;
340
+ }
341
+ return value;
342
+ }
343
+
344
+ readUint24() {
345
+ // fixme recheck
346
+ return this.readUint8() | (this.readUint8() << 8) | (this.readUint8() << 16);
263
347
  }
264
348
 
265
349
  writeZero(length: number) {
266
- this.writeBytes(new Uint8Array(length).buffer);
350
+ this.willWrite(length);
351
+ this.bytes.fill(0, this.position, this.position + length);
352
+ this.position += length;
267
353
  }
268
354
 
269
355
  writeValue(typ: TypedValue['type'], val: TypedValue['value']): void;
@@ -285,16 +371,26 @@ export class ByteBuffer {
285
371
  case 'int16':
286
372
  this.writeInt16(value);
287
373
  break;
374
+ case 'uint24':
375
+ this.writeUint24(value);
376
+ break;
377
+ case 'int24':
378
+ this.writeInt24(value);
379
+ break;
288
380
  case 'uint32':
381
+ case 'uint':
289
382
  this.writeUint32(value);
290
383
  break;
291
384
  case 'int32':
385
+ case 'int':
292
386
  this.writeInt32(value);
293
387
  break;
294
388
  case 'float32':
389
+ case 'float':
295
390
  this.writeFloat32(value);
296
391
  break;
297
392
  case 'float64':
393
+ case 'double':
298
394
  this.writeFloat64(value);
299
395
  break;
300
396
  case 'string':
@@ -311,22 +407,132 @@ export class ByteBuffer {
311
407
  }
312
408
  }
313
409
 
314
- readInt() {
315
- return this.readInt32();
410
+ readUnsignedShort() {
411
+ return this.readUint16();
412
+ }
413
+
414
+ readShort() {
415
+ return this.readInt16();
416
+ }
417
+
418
+ readInt(bytesOrBits: number = 32) {
419
+ switch (bytesOrBits) {
420
+ case 1:
421
+ case 8:
422
+ return this.readInt8();
423
+ case 2:
424
+ case 16:
425
+ return this.readInt16();
426
+ case 3:
427
+ case 24:
428
+ return this.readInt24();
429
+ case 4:
430
+ case 32:
431
+ return this.readInt32();
432
+ // skip 8 byte for bits
433
+ case 64:
434
+ return this.readInt64();
435
+ default:
436
+ throw new Error(`Unknown int${bytesOrBits}`);
437
+ }
438
+ }
439
+
440
+ readUint(bytesOrBits: number = 32) {
441
+ switch (bytesOrBits) {
442
+ case 1:
443
+ case 8:
444
+ return this.readUint8();
445
+ case 2:
446
+ case 16:
447
+ return this.readUint16();
448
+ case 3:
449
+ case 24:
450
+ return this.readUint24();
451
+ case 4:
452
+ case 32:
453
+ return this.readUint32();
454
+ // skip 8 byte for bits
455
+ case 64:
456
+ return this.readUint64();
457
+ default:
458
+ throw new Error(`Unexpected int size: ${bytesOrBits}`);
459
+ }
460
+ }
461
+
462
+ writeUint(value: number, bytesOrBits: number = 32) {
463
+ switch (bytesOrBits) {
464
+ case 1:
465
+ case 8:
466
+ this.writeUint8(value);
467
+ break;
468
+ case 2:
469
+ case 16:
470
+ this.writeUint16(value);
471
+ break;
472
+ case 3:
473
+ case 24:
474
+ this.writeUint24(value);
475
+ break;
476
+ case 4:
477
+ case 32:
478
+ this.writeUint32(value);
479
+ break;
480
+ case 64:
481
+ this.writeUint64(value);
482
+ break;
483
+ default:
484
+ throw new Error(`Unexpected uint size: ${bytesOrBits}`);
485
+ }
486
+ }
487
+
488
+ readFloat(n: number = 32) {
489
+ switch (n) {
490
+ case 4:
491
+ case 32:
492
+ return this.readFloat32();
493
+ case 8:
494
+ case 64:
495
+ return this.readFloat64();
496
+ default:
497
+ throw new Error(`Unexpected float size: ${n}`);
498
+ }
316
499
  }
317
500
 
318
- readFloat() {
319
- return this.readFloat32();
501
+ writeFloat(value: number, n: number = 32) {
502
+ switch (n) {
503
+ case 4:
504
+ case 32:
505
+ this.writeFloat32(value);
506
+ break;
507
+ case 8:
508
+ case 64:
509
+ this.writeFloat64(value);
510
+ break;
511
+ default:
512
+ throw new Error(`Unexpected float size: ${n}`);
513
+ }
320
514
  }
321
515
 
322
516
  readDouble() {
323
517
  return this.readFloat64();
324
518
  }
325
519
 
520
+ writeDouble(value: number) {
521
+ this.writeFloat64(value);
522
+ }
523
+
326
524
  remaining() {
327
525
  return this.length - this.position;
328
526
  }
329
527
 
528
+ hasRemaining() {
529
+ return this.remaining() > 0;
530
+ }
531
+
532
+ readRemaining() {
533
+ return this.readBytes(this.remaining());
534
+ }
535
+
330
536
  toUint8Array() {
331
537
  return new Uint8Array(this.buffer);
332
538
  }
@@ -338,6 +544,12 @@ export class ByteBuffer {
338
544
  toBase64() {
339
545
  return ArrayBuffers.toBase64(this.buffer);
340
546
  }
547
+
548
+ private willWrite(length: number) {
549
+ if (this.remaining() < length) {
550
+ this.resize(this.position + length);
551
+ }
552
+ }
341
553
  }
342
554
 
343
555
  export interface TypedValue {
@@ -348,9 +560,15 @@ export interface TypedValue {
348
560
  | 'int8'
349
561
  | 'uint16'
350
562
  | 'int16'
563
+ | 'uint24'
564
+ | 'int24'
565
+ | 'int'
566
+ | 'uint'
351
567
  | 'uint32'
352
568
  | 'int32'
569
+ | 'float'
353
570
  | 'float32'
571
+ | 'double'
354
572
  | 'float64'
355
573
  | 'string'
356
574
  | 'boolean';
@@ -364,3 +582,17 @@ function safeNumber(n: bigint) {
364
582
  }
365
583
  return Number(n);
366
584
  }
585
+
586
+ function bigintOf(n: number | bigint) {
587
+ if (typeof n === 'bigint') {
588
+ return n;
589
+ }
590
+ return BigInt(n);
591
+ }
592
+
593
+ function requireNumber(n: number | bigint) {
594
+ if (typeof n !== 'number') {
595
+ throw new Error(`Expected number, got ${n}`);
596
+ }
597
+ return n;
598
+ }
@@ -0,0 +1,21 @@
1
+ import { computeIfAbsent, getGlobalThis } from '@wener/utils';
2
+
3
+ let _holder: any;
4
+
5
+ export function setGlobalStates(states: Record<string, any>) {
6
+ _holder = states;
7
+ }
8
+
9
+ export function getGlobalStates(): Record<string, any>;
10
+ export function getGlobalStates<T>(key: string, create: () => T): T;
11
+ export function getGlobalStates<T>(key: string): T | undefined;
12
+ export function getGlobalStates(key?: string, create?: () => any): any {
13
+ _holder ||= (getGlobalThis() as any).__GLOBAL_STATES__ ||= {};
14
+ if (key) {
15
+ if (!create) {
16
+ return _holder[key];
17
+ }
18
+ return computeIfAbsent(_holder, key, create);
19
+ }
20
+ return _holder;
21
+ }
@@ -0,0 +1,20 @@
1
+ import { getGlobalStates } from './getGlobalStates';
2
+
3
+ export function getObjectId(k: any): number {
4
+ const state = getGlobalStates('ObjectId', () => {
5
+ return {
6
+ id: 0,
7
+ map: new WeakMap(),
8
+ };
9
+ });
10
+
11
+ const { map } = state;
12
+
13
+ if (map.has(k)) {
14
+ return map.get(k);
15
+ }
16
+
17
+ const id = state.id++;
18
+ map.set(k, id);
19
+ return id;
20
+ }
@@ -0,0 +1,3 @@
1
+ export function isNullish(value: any): value is null | undefined {
2
+ return value === null || value === undefined;
3
+ }
@@ -4,10 +4,12 @@ import { mixin } from './mixin';
4
4
  export type Constructor<T = {}> = new (...args: any[]) => T;
5
5
 
6
6
  test('mixin', () => {
7
+ // @ts-ignore
7
8
  class User extends mixin(BaseResource, createBarFields()) {}
8
9
 
9
10
  let usr = new User();
10
11
  // type not working
12
+ // @ts-ignore
11
13
  expect(usr.foo, 'foo');
12
14
  expect(usr).toEqual({
13
15
  foo: 'foo',
@@ -22,6 +24,8 @@ class BaseResource {
22
24
 
23
25
  function createBarFields() {
24
26
  return <TBase extends Constructor>(Base: TBase) => {
27
+ // nested type not working
28
+ // @ts-ignore
25
29
  class HasBarMixin extends mixin(Base, withFooFields) {
26
30
  bar?: string = 'bar';
27
31
  }
@@ -1,4 +1,4 @@
1
- import { Logger, LogLevel } from './Logger';
1
+ import type { Logger, LogLevel } from './Logger';
2
2
 
3
3
  enum Level {
4
4
  trace = -8,
@@ -13,7 +13,11 @@ test('clamp', () => {
13
13
  [[null, { min: 1, max: 10 }], 1],
14
14
  [[null, { min: 1, max: 10, default: 5 }], 5],
15
15
  [[2n, 1, 10, 5], 2n],
16
+ [[1, 2], 2], // min
17
+ [[1, undefined, 0], 0], // max
18
+ [[], undefined],
19
+ [[null], undefined],
16
20
  ]) {
17
- expect(clamp.apply(null, a as any)).toBe(b);
21
+ expect(clamp.apply(null, a as any), `${a} -> ${b}`).toBe(b);
18
22
  }
19
23
  });
@@ -1,23 +1,23 @@
1
- // export function clamp<T>(value: T | null | undefined, o: { min: T; max: T; default?: T }): T;
1
+ import { isDefined } from '../langs/isDefined';
2
+ import { isNullish } from '../langs/isNullish';
2
3
 
4
+ // export function clamp<T>(value: T | null | undefined, opts: { min?: T; max?: T; default?: T }): T;
3
5
  export function clamp<T>(value: T | null | undefined, min: T, max: T, def?: T): T;
4
6
  export function clamp<T>(value: T | null | undefined, ...o: any[]): T {
5
7
  let min: T, max: T, def: T;
6
- if (o.length === 1) {
8
+ if (o.length === 1 && o[0] && typeof o[0] === 'object') {
7
9
  ({ min, max, default: def = min! } = o[0]);
8
10
  } else {
9
11
  [min, max, def = min!] = o;
10
12
  }
11
- if (value === null || value === undefined) {
13
+ if (isNullish(value)) {
12
14
  return def;
13
15
  }
14
- if (value < min) {
16
+ if (isDefined(min) && value < min) {
15
17
  return min;
16
18
  }
17
- if (value > max) {
19
+ if (isDefined(max) && value > max) {
18
20
  return max;
19
21
  }
20
22
  return value;
21
23
  }
22
-
23
- // type Comparable = number | string | Date | BigInt;
@@ -1,5 +1,5 @@
1
1
  import { assert, describe, test } from 'vitest';
2
- import { merge, MergeOptions } from './merge';
2
+ import { merge, type MergeOptions } from './merge';
3
3
 
4
4
  describe('custom-array-merge', () => {
5
5
  test('custom merge array', () => {
@@ -1,4 +1,4 @@
1
- import { TSchema } from '@sinclair/typebox';
1
+ import type { TSchema } from '@sinclair/typebox';
2
2
  import { Value } from '@sinclair/typebox/value';
3
3
 
4
4
  // works like zod parse
@@ -1,5 +1,5 @@
1
- import { MaybePromise } from '../../asyncs/MaybePromise';
2
- import { createFetchWith, FetchLike } from '../../fetch';
1
+ import type { MaybePromise } from '../../asyncs/MaybePromise';
2
+ import { createFetchWith, type FetchLike } from '../../fetch';
3
3
  import { getGlobalThis } from '../../runtime/getGlobalThis';
4
4
 
5
5
  export function createFetchWithProxyByUndici({
package/tsconfig.json CHANGED
@@ -12,6 +12,7 @@
12
12
  "sourceMap": true,
13
13
  "skipLibCheck": true,
14
14
  "esModuleInterop": true,
15
+ "verbatimModuleSyntax": true,
15
16
  "allowSyntheticDefaultImports": true,
16
17
  "noImplicitAny": true,
17
18
  "noImplicitReturns": true,
@@ -21,7 +22,9 @@
21
22
  "removeComments": false,
22
23
  "stripInternal": true,
23
24
  "isolatedModules": true,
24
- "outDir": "lib"
25
+ "outDir": "lib",
26
+ "baseUrl": ".",
27
+ "rootDir": "./src"
25
28
  },
26
29
  "exclude": [
27
30
  "node_modules"