xaviiic / xid-js Goto Github PK
View Code? Open in Web Editor NEWxid js is a globally unique id generator for Node.js
License: MIT License
xid js is a globally unique id generator for Node.js
License: MIT License
I want to generate xids in browser. The code below is based on this repository. I used base32-encode
package instead of custom encoder implementation, and RFC4648-HEX instead of Crockford to be compatible with pg-xid
Would be nice to make xid-js
isomorphic, and have the ability to provide a custom encoder.
import base32Encode from 'base32-encode'
const crypto = window.crypto
const randBuf = new Uint8Array(4)
function randInt() {
crypto.getRandomValues(randBuf)
return bin2int(randBuf)
}
function machineId() {
const lsKey = 'xid-machine-id'
const storedId = localStorage.getItem(lsKey)
if (storedId) {
return parseInt(storedId)
}
const id = randInt()
localStorage.setItem(lsKey, `${id}`)
return id
}
const mid = machineId() & 0xffffff
const pid = randInt() & 0xffff
let seq = randInt()
let time = (Date.now() / 1000) | 0
const buf = new ArrayBuffer(12)
const view = new DataView(buf, 0, 12)
view.setUint32(0, time)
setArrayBufBytesAtOffset(buf, uint32FirstNBytes(mid, 3), 4)
view.setUint16(7, pid)
function xid(): string {
const now = (Date.now() / 1000) | 0
if (time !== now) {
view.setUint32(0, now)
time = now
}
const c = seq & 0xffffff
seq += 1
setArrayBufBytesAtOffset(buf, uint32FirstNBytes(c, 3), 9)
return base32Encode(buf, 'RFC4648-HEX', { padding: false })
.substring(0, 20)
.toLowerCase()
}
function uint32FirstNBytes(uint32: number, numbBytes: number): ArrayBuffer {
const buf = new ArrayBuffer(4)
const view = new DataView(buf)
view.setUint32(0, uint32)
return buf.slice(buf.byteLength - numbBytes)
}
function setArrayBufBytesAtOffset(
dest: ArrayBuffer,
bytes: ArrayBuffer,
offset: number
) {
new Uint8Array(dest).set(new Uint8Array(bytes), offset)
}
function bin2int(bin: Uint8Array) {
let i = 0
const len = bin.length
let num = 0
while (i < len) {
num <<= 8
num += bin[i]
i++
}
return num
}
export default xid
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.