Giter Site home page Giter Site logo

xid-js's Introduction

Hi there ๐Ÿ‘‹

xid-js's People

Contributors

xaviiic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

xid-js's Issues

Using xid-js in browser

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.