From c5345e14bb4930f9f7ce6e01d0cfd008543bafe9 Mon Sep 17 00:00:00 2001 From: Jelle De Loecker Date: Mon, 5 Feb 2024 15:16:04 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Make=20`Blast.createObjectId()`=20a?= =?UTF-8?q?lso=20available=20on=20the=20server-side?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + lib/blast.js | 60 +++++++++++++++++++++++++++++++++++++++++++++ lib/browsershims.js | 55 ----------------------------------------- 3 files changed, 61 insertions(+), 55 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb20461..a411ebc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * Add `Blast.parseClassPath(path)` method * Add `WeakValueSet` class * Add `WeakValueSetMap` class +* Make `Blast.createObjectId()` also available on the server-side ## 0.8.18 (2024-01-19) diff --git a/lib/blast.js b/lib/blast.js index 3883ab9..af20ed0 100644 --- a/lib/blast.js +++ b/lib/blast.js @@ -38,4 +38,64 @@ defStat(function listToTree(list) { } return root.children; +}); + +let machine_id, + process_id, + counter = ~~(Math.random() * 10000); + +/** + * Create an ObjectID string + * + * @author Jelle De Loecker + * @since 0.2.0 + * @version 0.9.0 + * + * @return {string} + */ +defStat(function createObjectId() { + + // Start with 4 bytes for the time in seconds + let time = parseInt(Date.now()/1000).toString(16).slice(0, 8); + let result = time; + + if (machine_id == null) { + + if (Blast.isBrowser) { + machine_id = Math.abs(Bound.String.fowler(navigator.userAgent)).toString(16); + process_id = Blast.Classes.Crypto.pseudoHex().padStart(4, '0'); + } else if (Blast.isServer) { + let libcrypto = require('crypto'); + const hostname = require('os').hostname() || 'protoblast'; + machine_id = libcrypto.createHash('md5').update(hostname).digest('hex').padStart(6, '0'); + process_id = process.pid.toString(16).padStart(4, '0'); + } + + if (machine_id.length < 6) { + machine_id += result; + } + + // Get the first 6 pieces + machine_id = machine_id.slice(0, 6); + + if (process_id.length > 4) { + process_id = process_id.slice(0, 4); + } + } + + result += machine_id; + result += process_id; + + // Create the counter + let count = (counter++).toString(16); + + if (count.length < 6) { + count = count.padStart(6, '0'); + } else { + count = count.slice(-6); + } + + result += count; + + return result; }); \ No newline at end of file diff --git a/lib/browsershims.js b/lib/browsershims.js index cda0776..ed21970 100644 --- a/lib/browsershims.js +++ b/lib/browsershims.js @@ -1,7 +1,3 @@ -var machine_id, - process_id, - counter = ~~(Math.random() * 10000); - var stringifyPrimitive = function(v) { if (typeof v === 'string') return v; @@ -91,55 +87,4 @@ Blast.parseHTML = function parseHTML(html) { } else { return result[0]; } -}; - -/** - * Create an ObjectID string - * - * @author Jelle De Loecker - * @since 0.2.0 - * @version 0.2.0 - * - * @return {String} - */ -Blast.createObjectId = function createObjectId() { - - var result, - count, - time; - - // Start with 4 bytes for the time in seconds - time = parseInt(Date.now()/1000).toString(16).slice(0, 8); - result = time; - - // Add the machine identifier - if (!machine_id) { - machine_id = Math.abs(Bound.String.fowler(navigator.userAgent)).toString(16); - - if (machine_id.length < 6) { - machine_id += result; - } - - // Get the first 6 pieces - machine_id = machine_id.slice(0, 6); - } - - result += machine_id; - - if (!process_id) { - process_id = Blast.Classes.Crypto.pseudoHex().slice(0, 4); - } - - result += process_id; - - // Create the counter - count = (counter++).toString(16); - - if (count.length < 6) { - count = Bound.String.multiply('0', 6 - count.length) + count; - } - - result += count; - - return result; }; \ No newline at end of file