Skip to content

Commit

Permalink
✨ Make Blast.createObjectId() also available on the server-side
Browse files Browse the repository at this point in the history
  • Loading branch information
skerit committed Feb 5, 2024
1 parent 187267d commit c5345e1
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 55 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
60 changes: 60 additions & 0 deletions lib/blast.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
* @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;
});
55 changes: 0 additions & 55 deletions lib/browsershims.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
var machine_id,
process_id,
counter = ~~(Math.random() * 10000);

var stringifyPrimitive = function(v) {
if (typeof v === 'string')
return v;
Expand Down Expand Up @@ -91,55 +87,4 @@ Blast.parseHTML = function parseHTML(html) {
} else {
return result[0];
}
};

/**
* Create an ObjectID string
*
* @author Jelle De Loecker <[email protected]>
* @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;
};

0 comments on commit c5345e1

Please sign in to comment.