-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Make
Blast.createObjectId()
also available on the server-side
- Loading branch information
Showing
3 changed files
with
61 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -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; | ||
}; |