Skip to content

Commit

Permalink
Generate id's with custom length and letters (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ughuuu authored Nov 28, 2023
1 parent 97188b9 commit e01f8e7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
10 changes: 10 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# Id generation ===========================================================
# If you change this, check the collision probability https://zelark.github.io/nano-id-cc/
NORAY_OID_LENGTH=21 # For 10 id/hour, 15 trillion years
# NORAY_OID_LENGTH=4 # For 10 id/hour, 2 days
# NORAY_OID_LENGTH=5 # For 10 id/hour, 19 days
# NORAY_OID_LENGTH=6 # For 10 id/hour, 155 days
NORAY_OID_CHARSET=useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict
NORAY_PID_LENGTH=128
NORAY_PID_CHARSET=useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict

# Socket ======================================================================
# TCP hostname to listen on
NORAY_SOCKET_HOST=::1
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@foxssake/noray",
"version": "1.3.4",
"version": "1.4.0",
"description": "Online multiplayer orchestrator and potential game platform",
"main": "src/noray.mjs",
"bin": {
Expand Down
11 changes: 11 additions & 0 deletions src/config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as dotenv from 'dotenv'
import { byteSize, duration, integer, number, ports } from './config.parsers.mjs'
import logger, { getLogLevel } from './logger.mjs'
import { urlAlphabet } from 'nanoid'

dotenv.config()

Expand All @@ -10,6 +11,16 @@ const env = process.env
* Noray configuration type.
*/
export class NorayConfig {
oid = {
length: integer(env.NORAY_OID_LENGTH) ?? 21,
charset: env.NORAY_OID_CHARSET ?? urlAlphabet
}

pid = {
length: integer(env.NORAY_PID_LENGTH) ?? 128,
charset: env.NORAY_PID_CHARSET ?? urlAlphabet
}

socket = {
host: env.NORAY_SOCKET_HOST ?? '::1',
port: integer(env.NORAY_SOCKET_PORT) ?? 8890
Expand Down
11 changes: 7 additions & 4 deletions src/hosts/host.entity.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
import * as net from 'node:net'
import * as dgram from 'node:dgram'
/* eslint-enable */
import { nanoid } from 'nanoid'
import * as nanoid from 'nanoid'
import { config } from '../config.mjs'

const generateOID = nanoid.customAlphabet(config.oid.charset, config.oid.length)
const generatePID = nanoid.customAlphabet(config.pid.charset, config.pid.length)

/**
* Host entity.
Expand Down Expand Up @@ -46,8 +50,7 @@ export class HostEntity {
*/
constructor (options) {
options && Object.assign(this, options)

this.oid ??= nanoid()
this.pid ??= nanoid(128)
this.oid ??= generateOID()
this.pid ??= generatePID()
}
}

0 comments on commit e01f8e7

Please sign in to comment.