@shware/http 0.2.0 → 0.2.1

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.
package/dist/index.d.mts CHANGED
@@ -238,4 +238,32 @@ type Result<S extends ZodSchema> = {
238
238
  type Target = 'json' | 'form' | 'query' | 'param' | 'header' | 'cookie';
239
239
  declare function valid<S extends ZodSchema>(request: Request, target: Target, schema: S): Promise<Result<S>>;
240
240
 
241
- export { type AppErrorReason, type AuthenticationErrorReason, type BadRequest, Code, DEFAULT_MESSAGES, type DebugInfo, type Detail, DetailType, Details, type ErrorBody, type ErrorInfo, type ErrorReason, type Help, type LocalizedMessage, type ModerationErrorReason, type MultipartErrorReason, type NetworkErrorReason, type PreconditionFailure, type QuotaFailure, type RequestInfo, type ResourceInfo, type Result, type RetryInfo, Status, StatusCode, StatusError, type StatusErrorReason, valid };
241
+ /**
242
+ * A distributed unique ID generator inspired by Twitter's Snowflake.
243
+ *
244
+ * ID data bits:
245
+ * +----------------------+----------------+-----------+
246
+ * | delta millisecond | worker node id | sequence |
247
+ * +----------------------+----------------+-----------+
248
+ * | 41bits | 10bits | 12bits |
249
+ * +----------------------+----------------+-----------+
250
+ *
251
+ */
252
+ declare class UidGenerator {
253
+ readonly MAX: bigint;
254
+ private readonly workerBits;
255
+ private readonly sequenceBits;
256
+ private readonly epochMillisecond;
257
+ private readonly sequenceMask;
258
+ private readonly maxWorkerId;
259
+ private readonly workerIdShift;
260
+ private readonly timeStampShift;
261
+ private readonly workerId;
262
+ private sequence;
263
+ private lastTimestamp;
264
+ constructor(workerId: number);
265
+ readonly next: () => bigint;
266
+ }
267
+ declare const uid: UidGenerator;
268
+
269
+ export { type AppErrorReason, type AuthenticationErrorReason, type BadRequest, Code, DEFAULT_MESSAGES, type DebugInfo, type Detail, DetailType, Details, type ErrorBody, type ErrorInfo, type ErrorReason, type Help, type LocalizedMessage, type ModerationErrorReason, type MultipartErrorReason, type NetworkErrorReason, type PreconditionFailure, type QuotaFailure, type RequestInfo, type ResourceInfo, type Result, type RetryInfo, Status, StatusCode, StatusError, type StatusErrorReason, UidGenerator, uid, valid };
package/dist/index.mjs CHANGED
@@ -309,4 +309,48 @@ async function valid(request, target, schema) {
309
309
  return { data: null, error };
310
310
  }
311
311
 
312
- export { Code, DEFAULT_MESSAGES, DetailType, Details, Status, StatusCode, StatusError, valid };
312
+ function getNextMillisecond(timestamp) {
313
+ let now = BigInt(Date.now());
314
+ while (now < timestamp) {
315
+ now = BigInt(Date.now());
316
+ }
317
+ return now;
318
+ }
319
+ class UidGenerator {
320
+ MAX = (1n << 63n) - 1n;
321
+ workerBits = BigInt(10);
322
+ sequenceBits = BigInt(12);
323
+ epochMillisecond = BigInt(1577808e6);
324
+ // 2020-1-1 00:00:00
325
+ sequenceMask = ~(BigInt(-1) << this.sequenceBits);
326
+ maxWorkerId = ~(BigInt(-1) << this.workerBits);
327
+ workerIdShift = this.sequenceBits;
328
+ timeStampShift = this.workerBits + this.sequenceBits;
329
+ workerId;
330
+ sequence = BigInt(0);
331
+ lastTimestamp = BigInt(-1);
332
+ constructor(workerId2) {
333
+ this.workerId = BigInt(workerId2) % this.maxWorkerId;
334
+ }
335
+ next = () => {
336
+ let timestamp = BigInt(Date.now());
337
+ if (timestamp < this.lastTimestamp) {
338
+ console.error(`Clock moved backwards. time gap = ${this.lastTimestamp - timestamp}`);
339
+ timestamp = getNextMillisecond(this.lastTimestamp);
340
+ }
341
+ if (timestamp === this.lastTimestamp) {
342
+ this.sequence = this.sequence + BigInt(1) & this.sequenceMask;
343
+ if (this.sequence === BigInt(0)) {
344
+ timestamp = getNextMillisecond(timestamp);
345
+ }
346
+ } else {
347
+ this.sequence = BigInt(0);
348
+ }
349
+ this.lastTimestamp = timestamp;
350
+ return timestamp - this.epochMillisecond << this.timeStampShift | this.workerId << this.workerIdShift | this.sequence;
351
+ };
352
+ }
353
+ const workerId = Math.floor(Math.random() * 1024);
354
+ const uid = new UidGenerator(workerId);
355
+
356
+ export { Code, DEFAULT_MESSAGES, DetailType, Details, Status, StatusCode, StatusError, UidGenerator, uid, valid };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shware/http",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "dev": "tsc --watch",