@shware/http 0.2.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +29 -1
- package/dist/index.mjs +45 -1
- package/package.json +1 -1
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
|
-
|
|
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 machineBits;
|
|
255
|
+
private readonly sequenceBits;
|
|
256
|
+
private readonly epochMillisecond;
|
|
257
|
+
private readonly sequenceMask;
|
|
258
|
+
private readonly maxMachineId;
|
|
259
|
+
private readonly machineIdShift;
|
|
260
|
+
private readonly timeStampShift;
|
|
261
|
+
private readonly machineId;
|
|
262
|
+
private sequence;
|
|
263
|
+
private lastTimestamp;
|
|
264
|
+
constructor(mackineId: 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
|
-
|
|
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
|
+
machineBits = BigInt(10);
|
|
322
|
+
sequenceBits = BigInt(12);
|
|
323
|
+
epochMillisecond = BigInt(1577808e6);
|
|
324
|
+
// 2020-1-1 00:00:00
|
|
325
|
+
sequenceMask = ~(BigInt(-1) << this.sequenceBits);
|
|
326
|
+
maxMachineId = ~(BigInt(-1) << this.machineBits);
|
|
327
|
+
machineIdShift = this.sequenceBits;
|
|
328
|
+
timeStampShift = this.machineBits + this.sequenceBits;
|
|
329
|
+
machineId;
|
|
330
|
+
sequence = BigInt(0);
|
|
331
|
+
lastTimestamp = BigInt(-1);
|
|
332
|
+
constructor(mackineId) {
|
|
333
|
+
this.machineId = BigInt(mackineId) % this.maxMachineId;
|
|
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.machineId << this.machineIdShift | this.sequence;
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
const machineId = Math.floor(Math.random() * 1024);
|
|
354
|
+
const uid = new UidGenerator(machineId);
|
|
355
|
+
|
|
356
|
+
export { Code, DEFAULT_MESSAGES, DetailType, Details, Status, StatusCode, StatusError, UidGenerator, uid, valid };
|