@tybys/oid
Advanced tools
Comparing version 1.0.8 to 2.0.0
#!/usr/bin/env node | ||
var ObjectId = require('../index.js'); | ||
var ObjectId = require('../index.js').ObjectId; | ||
@@ -5,0 +5,0 @@ function printHelp() { |
@@ -1,1 +0,1 @@ | ||
(function(t,e){if(typeof exports==="object"&&typeof module==="object"){module.exports=e()}else if(typeof define==="function"&&define.amd){define([],e)}else if(typeof exports==="object"){exports["ObjectId"]=e()}else{t["ObjectId"]=e()}})(this,function(){"use strict";function t(t){var e;if(typeof __webpack_require__!=="undefined"){e=typeof __non_webpack_require__!=="undefined"&&typeof wx==="undefined"?__non_webpack_require__:undefined}else{e=typeof require!=="undefined"&&typeof wx==="undefined"?require:undefined}return e(t)}var u;try{u=t("buffer").Buffer}catch(S){}var e=Object.prototype.toString;function r(t){if(Array.isArray){return Array.isArray(t)}return e.call(t)==="[object Array]"}function n(t){return typeof Uint8Array!=="undefined"&&t instanceof Uint8Array||r(t)}function i(t){if(u){return u.alloc(t)}var e;if(typeof Uint8Array!=="undefined"){return new Uint8Array(t)}var r=[];for(e=0;e<t;e++){r[e]=0}return r}function o(t){if(u){return u.from(t)}var e;if(typeof Uint8Array!=="undefined"){if(typeof Uint8Array.from==="function"){return Uint8Array.from(t)}else{var r=new Uint8Array(t.length);for(e=0;e<t.length;e++){r[e]=t[e]}return r}}var n=[];for(e=0;e<t.length;e++){n[e]=t[e]}return n}function a(t){t=t||0;if(u&&u.isBuffer(this)){return this.readUInt32BE(t)}var e=this[t];var r=this[t+3];if(e===undefined||r===undefined){throw new RangeError("Attempt to write outside buffer bounds")}return e*Math.pow(2,24)+this[++t]*Math.pow(2,16)+this[++t]*Math.pow(2,8)+r}function s(t){if(u){return u.from(this).toString(t)}var e="";var r=0;if(t==="hex"){e="";for(r=0;r<this.length;r++){var n=this[r].toString(16);e+=n.length===1?"0"+n:n}return e}else if(t==="binary"){e="";for(r=0;r<this.length;r++){e+=String.fromCharCode(this[r])}return e}else{var i,o,a;var s,f;i="";o=this.length;r=0;while(r<o){a=this[r++];switch(a>>4){case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:i+=String.fromCharCode(a);break;case 12:case 13:s=this[r++];i+=String.fromCharCode((a&31)<<6|s&63);break;case 14:s=this[r++];f=this[r++];i+=String.fromCharCode((a&15)<<12|(s&63)<<6|(f&63)<<0);break}}return i}}function f(t){var e=i(t);for(var r=0;r<t;++r)e[r]=Math.floor(Math.random()*256);return e}var c=f;if(typeof window!=="undefined"&&window.crypto&&window.crypto.getRandomValues&&typeof Uint8Array!=="undefined"){c=function(t){return window.crypto.getRandomValues(new Uint8Array(t))}}else{var h;try{h=t("crypto");c=h.randomBytes}catch(S){}if(c==null){c=f}}function d(e,r){return function(){console.warn(r);var t=Array.prototype.slice.call(arguments);return e.apply(this,t)}}var g=c(5);var l=new RegExp("^[0-9a-fA-F]{24}$");var p=[];var y;for(y=0;y<256;y++){p[y]=(y<=15?"0":"")+y.toString(16)}var w=[];y=0;while(y<10)w[48+y]=y++;while(y<16)w[65-10+y]=w[97-10+y]=y++;function b(t){return s.call(t,"hex")}function m(t,e){var r=t[e];return new TypeError('ObjectId string "'+t+'" contains invalid character "'+r+'" with character code ('+t.charCodeAt(e)+"). All character codes for a non-hex string must be less than 256.")}function v(t){if(!(this instanceof v))throw new TypeError("Class constructor ObjectId cannot be invoked without 'new'");if(t instanceof v)return t;if(t==null||typeof t==="number"){this.id=v.generate(t);if(v.cacheHexString)this.__id=this.toString("hex");return}var e=v.isValid(t);if(!e&&t!=null){throw new TypeError("Argument passed in must be a single String of 12 bytes or a string of 24 hex characters")}else if(e&&typeof t==="string"&&t.length===24&&u){return new v(u.from(t,"hex"))}else if(e&&typeof t==="string"&&t.length===24){return v.createFromHexString(t)}else if(t!=null&&t.length===12){if(n(t)){this.id=o(t)}else{this.id=t}}else if(t!=null&&t.toHexString){return v.createFromHexString(t.toHexString())}else{throw new TypeError("Argument passed in must be a single String of 12 bytes or a string of 24 hex characters")}if(v.cacheHexString)this.__id=this.toString("hex")}v.prototype.toHexString=function(){if(v.cacheHexString&&this.__id)return this.__id;var t="";if(!this.id||!this.id.length){throw new TypeError("invalid ObjectId, ObjectId.id must be either a string or a Buffer, but is ["+JSON.stringify(this.id)+"]")}if(n(this.id)){t=b(this.id);if(v.cacheHexString)this.__id=t;return t}for(var e=0;e<this.id.length;e++){var r=p[this.id.charCodeAt(e)];if(typeof r!=="string"){throw m(this.id,e)}t+=r}if(v.cacheHexString)this.__id=t;return t};v.getInc=function(){return v.index=(v.index+1)%16777215};v.generate=function(t){if("number"!==typeof t){t=~~(Date.now()/1e3)}var e=v.getInc();var r=i(12);r[3]=t&255;r[2]=t>>8&255;r[1]=t>>16&255;r[0]=t>>24&255;r[4]=g[0];r[5]=g[1];r[6]=g[2];r[7]=g[3];r[8]=g[4];r[11]=e&255;r[10]=e>>8&255;r[9]=e>>16&255;return r};v.prototype.toString=function(t){if(n(this.id)){return s.call(this.id,typeof t==="string"?t:"hex")}return this.toHexString()};v.prototype.toJSON=function(){return this.toHexString()};v.prototype.equals=function(t){if(t instanceof v){return this.toString()===t.toString()}if(typeof t==="string"&&v.isValid(t)&&t.length===12&&n(this.id)){return t===s.call(this.id,"binary")}if(typeof t==="string"&&v.isValid(t)&&t.length===24){return t.toLowerCase()===this.toHexString()}if(typeof t==="string"&&v.isValid(t)&&t.length===12){return t===this.id}if(t!=null&&(t instanceof v||t.toHexString)){return t.toHexString()===this.toHexString()}return false};v.prototype.getTimestamp=function(){var t=new Date;var e=a.call(this.id,0);t.setTime(Math.floor(e)*1e3);return t};v.createPk=function(){return new v};v.createFromTime=function(t){var e=i(12);e[3]=t&255;e[2]=t>>8&255;e[1]=t>>16&255;e[0]=t>>24&255;return new v(e)};v.createFromHexString=function(t){if(typeof t==="undefined"||t!=null&&t.length!==24){throw new TypeError("Argument passed in must be a single String of 12 bytes or a string of 24 hex characters")}var e=i(12);var r=0;var n=0;while(n<24){e[r++]=w[t.charCodeAt(n++)]<<4|w[t.charCodeAt(n++)]}return new v(e)};v.isValid=function(t){if(t==null)return false;if(typeof t==="number"){return true}if(typeof t==="string"){return t.length===12||t.length===24&&l.test(t)}if(t instanceof v){return true}if(n(t)&&t.length===12){return true}if(t.toHexString){return t.id.length===12||t.id.length===24&&l.test(t.id)}return false};v.prototype.toExtendedJSON=function(){if(this.toHexString)return{$oid:this.toHexString()};return{$oid:this.toString("hex")}};v.fromExtendedJSON=function(t){return new v(t.$oid)};v.get_inc=d(function(){return v.getInc()},"Please use the static `ObjectId.getInc()` instead");v.prototype.get_inc=d(function(){return v.getInc()},"Please use the static `ObjectId.getInc()` instead");v.prototype.getInc=d(function(){return v.getInc()},"Please use the static `ObjectId.getInc()` instead");v.prototype.generate=d(function(t){return v.generate(t)},"Please use the static `ObjectId.generate(time)` instead");try{Object.defineProperty(v.prototype,"generationTime",{enumerable:true,get:function(){return this.id[3]|this.id[2]<<8|this.id[1]<<16|this.id[0]<<24},set:function(t){this.id[3]=t&255;this.id[2]=t>>8&255;this.id[1]=t>>16&255;this.id[0]=t>>24&255}})}catch(_){v.prototype.generationTime=0}var x;try{x=t("util");v.prototype[x.inspect.custom||"inspect"]=v.prototype.toString}catch(S){v.prototype.inspect=v.prototype.toString}v.index=~~(Math.random()*16777215);try{Object.defineProperty(v.prototype,"_bsontype",{value:"ObjectID"})}catch(_){v.prototype._bsontype="ObjectID"}return v}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t=t||self).oid={})}(this,(function(t){"use strict";var e,r={nativeRequire:"undefined"!=typeof __webpack_modules__?"function"==typeof __tybys_get_native_require__?__tybys_get_native_require__():"undefined"!=typeof __non_webpack_require__?__non_webpack_require__:undefined:"undefined"!=typeof __webpack_modules__?"undefined"!=typeof __non_webpack_require__?__non_webpack_require__:undefined:"undefined"!=typeof require?require:undefined}.nativeRequire;try{e=r("buffer").Buffer}catch(m){}var n=Object.prototype.toString;function i(t){return"undefined"!=typeof Uint8Array&&t instanceof Uint8Array||function(t){return Array.isArray?Array.isArray(t):"[object Array]"===n.call(t)}(t)}function o(t){if(e)return e.alloc(t);var r;if("undefined"!=typeof Uint8Array)return new Uint8Array(t);var n=[];for(r=0;r<t;r++)n[r]=0;return n}function a(t){if(t=t||0,e&&e.isBuffer(this))return this.readUInt32BE(t);var r=this[t],n=this[t+3];if(r===undefined||n===undefined)throw new RangeError("Attempt to write outside buffer bounds");return r*Math.pow(2,24)+this[++t]*Math.pow(2,16)+this[++t]*Math.pow(2,8)+n}function s(t){if(e)return e.from(this).toString(t);var r,n,i,o,a,s="",f=0;if("hex"===t){for(s="",f=0;f<this.length;f++){var c=this[f].toString(16);s+=1===c.length?"0"+c:c}return s}if("binary"===t){for(s="",f=0;f<this.length;f++)s+=String.fromCharCode(this[f]);return s}for(r="",n=this.length,f=0;f<n;)switch((i=this[f++])>>4){case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:r+=String.fromCharCode(i);break;case 12:case 13:o=this[f++],r+=String.fromCharCode((31&i)<<6|63&o);break;case 14:o=this[f++],a=this[f++],r+=String.fromCharCode((15&i)<<12|(63&o)<<6|(63&a)<<0)}return r}function f(t){for(var e=o(t),r=0;r<t;++r)e[r]=Math.floor(256*Math.random());return e}var c=f;if("undefined"!=typeof window&&window.crypto&&window.crypto.getRandomValues&&"undefined"!=typeof Uint8Array)c=function(t){return window.crypto.getRandomValues(new Uint8Array(t))};else{try{c=r("crypto").randomBytes}catch(m){}null==c&&(c=f)}function u(t,e){return function(){console.warn(e);var r=Array.prototype.slice.call(arguments);return t.apply(this,r)}}var d,h=c(5),g=new RegExp("^[0-9a-fA-F]{24}$"),p=[];for(d=0;d<256;d++)p[d]=(d<=15?"0":"")+d.toString(16);var y,l=[];for(d=0;d<10;)l[48+d]=d++;for(;d<16;)l[55+d]=l[87+d]=d++;function _(t){if(!(this instanceof _))throw new TypeError("Class constructor ObjectId cannot be invoked without 'new'");if(t instanceof _)return t;if(null==t||"number"==typeof t)return this.id=_.generate(t),void(_.cacheHexString&&(this.__id=this.toString("hex")));var r=_.isValid(t);if(!r&&null!=t)throw new TypeError("Argument passed in must be a single String of 12 bytes or a string of 24 hex characters");if(r&&"string"==typeof t&&24===t.length&&e)return new _(e.from(t,"hex"));if(r&&"string"==typeof t&&24===t.length)return _.createFromHexString(t);if(null==t||12!==t.length){if(null!=t&&t.toHexString)return _.createFromHexString(t.toHexString());throw new TypeError("Argument passed in must be a single String of 12 bytes or a string of 24 hex characters")}i(t)?this.id=function(t){if(e)return e.from(t);var r;if("undefined"!=typeof Uint8Array){if("function"==typeof Uint8Array.from)return Uint8Array.from(t);var n=new Uint8Array(t.length);for(r=0;r<t.length;r++)n[r]=t[r];return n}var i=[];for(r=0;r<t.length;r++)i[r]=t[r];return i}(t):this.id=t,_.cacheHexString&&(this.__id=this.toString("hex"))}_.prototype.toHexString=function(){if(_.cacheHexString&&this.__id)return this.__id;var t,e,r,n,o="";if(!this.id||!this.id.length)throw new TypeError("invalid ObjectId, ObjectId.id must be either a string or a Buffer, but is ["+JSON.stringify(this.id)+"]");if(i(this.id))return t=this.id,o=s.call(t,"hex"),_.cacheHexString&&(this.__id=o),o;for(var a=0;a<this.id.length;a++){var f=p[this.id.charCodeAt(a)];if("string"!=typeof f)throw e=this.id,n=void 0,n=e[r=a],new TypeError('ObjectId string "'+e+'" contains invalid character "'+n+'" with character code ('+e.charCodeAt(r)+"). All character codes for a non-hex string must be less than 256.");o+=f}return _.cacheHexString&&(this.__id=o),o},_.getInc=function(){return _.index=(_.index+1)%16777215},_.generate=function(t){"number"!=typeof t&&(t=~~(Date.now()/1e3));var e=_.getInc(),r=o(12);return r[3]=255&t,r[2]=t>>8&255,r[1]=t>>16&255,r[0]=t>>24&255,r[4]=h[0],r[5]=h[1],r[6]=h[2],r[7]=h[3],r[8]=h[4],r[11]=255&e,r[10]=e>>8&255,r[9]=e>>16&255,r},_.prototype.toString=function(t){return i(this.id)?s.call(this.id,"string"==typeof t?t:"hex"):this.toHexString()},_.prototype.toJSON=function(){return this.toHexString()},_.prototype.equals=function(t){return t instanceof _?this.toString()===t.toString():"string"==typeof t&&_.isValid(t)&&12===t.length&&i(this.id)?t===s.call(this.id,"binary"):"string"==typeof t&&_.isValid(t)&&24===t.length?t.toLowerCase()===this.toHexString():"string"==typeof t&&_.isValid(t)&&12===t.length?t===this.id:!(null==t||!(t instanceof _||t.toHexString))&&t.toHexString()===this.toHexString()},_.prototype.getTimestamp=function(){var t=new Date,e=a.call(this.id,0);return t.setTime(1e3*Math.floor(e)),t},_.createPk=function(){return new _},_.createFromTime=function(t){var e=o(12);return e[3]=255&t,e[2]=t>>8&255,e[1]=t>>16&255,e[0]=t>>24&255,new _(e)},_.createFromHexString=function(t){if(void 0===t||null!=t&&24!==t.length)throw new TypeError("Argument passed in must be a single String of 12 bytes or a string of 24 hex characters");for(var e=o(12),r=0,n=0;n<24;)e[r++]=l[t.charCodeAt(n++)]<<4|l[t.charCodeAt(n++)];return new _(e)},_.isValid=function(t){return null!=t&&("number"==typeof t||("string"==typeof t?12===t.length||24===t.length&&g.test(t):t instanceof _||(!(!i(t)||12!==t.length)||!!t.toHexString&&(12===t.id.length||24===t.id.length&&g.test(t.id)))))},_.prototype.toExtendedJSON=function(){return this.toHexString?{$oid:this.toHexString()}:{$oid:this.toString("hex")}},_.fromExtendedJSON=function(t){return new _(t.$oid)},_.get_inc=u((function(){return _.getInc()}),"Please use the static `ObjectId.getInc()` instead"),_.prototype.get_inc=u((function(){return _.getInc()}),"Please use the static `ObjectId.getInc()` instead"),_.prototype.getInc=u((function(){return _.getInc()}),"Please use the static `ObjectId.getInc()` instead"),_.prototype.generate=u((function(t){return _.generate(t)}),"Please use the static `ObjectId.generate(time)` instead");try{Object.defineProperty(_.prototype,"generationTime",{enumerable:!0,get:function(){return this.id[3]|this.id[2]<<8|this.id[1]<<16|this.id[0]<<24},set:function(t){this.id[3]=255&t,this.id[2]=t>>8&255,this.id[1]=t>>16&255,this.id[0]=t>>24&255}})}catch(S){_.prototype.generationTime=0}try{y=r("util"),_.prototype[y.inspect.custom||"inspect"]=_.prototype.toString}catch(m){_.prototype.inspect=_.prototype.toString}_.index=~~(16777215*Math.random());try{Object.defineProperty(_.prototype,"_bsontype",{value:"ObjectID"})}catch(S){_.prototype._bsontype="ObjectID"}var b=_,w={ObjectId:b};t.ObjectId=b,t["default"]=w,(function(){try{return Object.defineProperty(t,"__esModule",{value:!0})}catch(_){return t.__esModule=!0,t}})()})); |
@@ -1,2 +0,10 @@ | ||
declare class ObjectId { | ||
export declare interface EmptyDocument { | ||
$oid: string; | ||
} | ||
export declare interface Document extends EmptyDocument { | ||
[field: string]: any; | ||
} | ||
export declare class ObjectId { | ||
constructor(id?: number | string | number[] | Uint8Array | ObjectId); | ||
@@ -16,3 +24,3 @@ | ||
static fromExtendedJSON(doc: ObjectId.Document): ObjectId; | ||
static fromExtendedJSON(doc: Document): ObjectId; | ||
@@ -37,15 +45,3 @@ static index: number; | ||
toExtendedJSON(): ObjectId.EmptyDocument; | ||
toExtendedJSON(): EmptyDocument; | ||
} | ||
declare namespace ObjectId { | ||
export interface EmptyDocument { | ||
$oid: string; | ||
} | ||
export interface Document extends EmptyDocument { | ||
[field: string]: any; | ||
} | ||
} | ||
export = ObjectId; |
1014
index.js
@@ -1,614 +0,588 @@ | ||
(function(root, factory) { | ||
if (typeof exports === 'object' && typeof module === 'object') { | ||
module.exports = factory(); | ||
} else if (typeof define === 'function' && define.amd) { | ||
define([], factory); | ||
} else if (typeof exports === 'object') { | ||
exports['ObjectId'] = factory(); | ||
} else { | ||
root['ObjectId'] = factory(); | ||
} | ||
})(this, function() { | ||
'use strict'; | ||
var nativeRequire = require('@tybys/native-require').nativeRequire; | ||
function __node_require__(request) { | ||
var __r; | ||
if (typeof __webpack_require__ !== 'undefined') { | ||
__r = ((typeof __non_webpack_require__ !== 'undefined' && typeof wx === 'undefined') ? __non_webpack_require__ : undefined); | ||
} else { | ||
__r = ((typeof require !== 'undefined' && typeof wx === 'undefined') ? require : undefined); | ||
} | ||
return __r(request); | ||
} | ||
var __Buffer; | ||
try { | ||
__Buffer = nativeRequire('buffer').Buffer; | ||
} catch (e) { | ||
// empty | ||
} | ||
var __Buffer; | ||
try { | ||
__Buffer = __node_require__('buffer').Buffer; | ||
} catch (e) { | ||
// empty | ||
var toString = Object.prototype.toString; | ||
function isArray(o) { | ||
if (Array.isArray) { | ||
return Array.isArray(o); | ||
} | ||
return (toString.call(o) === '[object Array]'); | ||
} | ||
var toString = Object.prototype.toString; | ||
function isArrayLike(o) { | ||
return (typeof Uint8Array !== 'undefined' && o instanceof Uint8Array) || isArray(o); | ||
} | ||
function isArray(o) { | ||
if (Array.isArray) { | ||
return Array.isArray(o); | ||
} | ||
return (toString.call(o) === '[object Array]'); | ||
function createBuffer(len) { | ||
if (__Buffer) { | ||
return __Buffer.alloc(len); | ||
} | ||
function isArrayLike(o) { | ||
return (typeof Uint8Array !== 'undefined' && o instanceof Uint8Array) || isArray(o); | ||
var i; | ||
if (typeof Uint8Array !== 'undefined') { | ||
return new Uint8Array(len); | ||
} | ||
function createBuffer(len) { | ||
if (__Buffer) { | ||
return __Buffer.alloc(len); | ||
} | ||
var arr = []; | ||
for (i = 0; i < len; i++) { | ||
arr[i] = 0; | ||
} | ||
var i; | ||
if (typeof Uint8Array !== 'undefined') { | ||
return new Uint8Array(len); | ||
} | ||
return arr; | ||
} | ||
var arr = []; | ||
for (i = 0; i < len; i++) { | ||
arr[i] = 0; | ||
} | ||
return arr; | ||
function bufferFrom(buf) { | ||
if (__Buffer) { | ||
return __Buffer.from(buf); | ||
} | ||
function bufferFrom(buf) { | ||
if (__Buffer) { | ||
return __Buffer.from(buf); | ||
} | ||
var i; | ||
if (typeof Uint8Array !== 'undefined') { | ||
if (typeof Uint8Array.from === 'function') { | ||
return Uint8Array.from(buf); | ||
} else { | ||
var uint8arr = new Uint8Array(buf.length); | ||
for (i = 0; i < buf.length; i++) { | ||
uint8arr[i] = buf[i]; | ||
} | ||
return uint8arr; | ||
var i; | ||
if (typeof Uint8Array !== 'undefined') { | ||
if (typeof Uint8Array.from === 'function') { | ||
return Uint8Array.from(buf); | ||
} else { | ||
var uint8arr = new Uint8Array(buf.length); | ||
for (i = 0; i < buf.length; i++) { | ||
uint8arr[i] = buf[i]; | ||
} | ||
return uint8arr; | ||
} | ||
} | ||
var arr = []; | ||
for (i = 0; i < buf.length; i++) { | ||
arr[i] = buf[i]; | ||
} | ||
return arr; | ||
var arr = []; | ||
for (i = 0; i < buf.length; i++) { | ||
arr[i] = buf[i]; | ||
} | ||
return arr; | ||
} | ||
function readUInt32BE(offset) { | ||
offset = offset || 0; | ||
if (__Buffer && __Buffer.isBuffer(this)) { | ||
return this.readUInt32BE(offset); | ||
} | ||
var first = this[offset]; | ||
var last = this[offset + 3]; | ||
if (first === undefined || last === undefined) { | ||
throw new RangeError('Attempt to write outside buffer bounds'); | ||
} | ||
function readUInt32BE(offset) { | ||
offset = offset || 0; | ||
if (__Buffer && __Buffer.isBuffer(this)) { | ||
return this.readUInt32BE(offset); | ||
} | ||
var first = this[offset]; | ||
var last = this[offset + 3]; | ||
if (first === undefined || last === undefined) { | ||
throw new RangeError('Attempt to write outside buffer bounds'); | ||
} | ||
return first * Math.pow(2, 24) + | ||
this[++offset] * Math.pow(2, 16) + | ||
this[++offset] * Math.pow(2, 8) + | ||
last; | ||
return first * Math.pow(2, 24) + | ||
this[++offset] * Math.pow(2, 16) + | ||
this[++offset] * Math.pow(2, 8) + | ||
last; | ||
} | ||
function bufferToString(encoding) { | ||
if (__Buffer) { | ||
return __Buffer.from(this).toString(encoding); | ||
} | ||
function bufferToString(encoding) { | ||
if (__Buffer) { | ||
return __Buffer.from(this).toString(encoding); | ||
var res = ''; | ||
var i = 0; | ||
if (encoding === 'hex') { | ||
res = ''; | ||
for (i = 0; i < this.length; i++) { | ||
var hex = this[i].toString(16); | ||
res += (hex.length === 1 ? ('0' + hex) : hex); | ||
} | ||
var res = ''; | ||
var i = 0; | ||
if (encoding === 'hex') { | ||
res = ''; | ||
for (i = 0; i < this.length; i++) { | ||
var hex = this[i].toString(16); | ||
res += (hex.length === 1 ? ('0' + hex) : hex); | ||
} | ||
return res; | ||
} else if (encoding === 'binary') { | ||
res = ''; | ||
for (i = 0; i < this.length; i++) { | ||
res += String.fromCharCode(this[i]); | ||
} | ||
return res; | ||
} else { | ||
var out, len, c; | ||
var char2, char3; | ||
return res; | ||
} else if (encoding === 'binary') { | ||
res = ''; | ||
for (i = 0; i < this.length; i++) { | ||
res += String.fromCharCode(this[i]); | ||
} | ||
return res; | ||
} else { | ||
var out, len, c; | ||
var char2, char3; | ||
out = ''; | ||
len = this.length; | ||
i = 0; | ||
while (i < len) { | ||
c = this[i++]; | ||
switch (c >> 4) { | ||
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: | ||
// 0xxxxxxx | ||
out += String.fromCharCode(c); | ||
break; | ||
case 12: case 13: | ||
// 110x xxxx 10xx xxxx | ||
char2 = this[i++]; | ||
out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F)); | ||
break; | ||
case 14: | ||
// 1110 xxxx 10xx xxxx 10xx xxxx | ||
char2 = this[i++]; | ||
char3 = this[i++]; | ||
out += String.fromCharCode(((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0)); | ||
break; | ||
} | ||
out = ''; | ||
len = this.length; | ||
i = 0; | ||
while (i < len) { | ||
c = this[i++]; | ||
switch (c >> 4) { | ||
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: | ||
// 0xxxxxxx | ||
out += String.fromCharCode(c); | ||
break; | ||
case 12: case 13: | ||
// 110x xxxx 10xx xxxx | ||
char2 = this[i++]; | ||
out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F)); | ||
break; | ||
case 14: | ||
// 1110 xxxx 10xx xxxx 10xx xxxx | ||
char2 = this[i++]; | ||
char3 = this[i++]; | ||
out += String.fromCharCode(((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0)); | ||
break; | ||
} | ||
return out; | ||
} | ||
} | ||
function insecureRandomBytes(size) { | ||
var result = createBuffer(size); | ||
for (var i = 0; i < size; ++i) result[i] = Math.floor(Math.random() * 256); | ||
return result; | ||
return out; | ||
} | ||
} | ||
var randomBytes = insecureRandomBytes; | ||
if (typeof window !== 'undefined' && window.crypto && window.crypto.getRandomValues && typeof Uint8Array !== 'undefined') { | ||
randomBytes = function(size) { | ||
return window.crypto.getRandomValues(new Uint8Array(size)); | ||
}; | ||
} else { | ||
var crypto; | ||
try { | ||
crypto = __node_require__('crypto'); | ||
randomBytes = crypto.randomBytes; | ||
} catch (e) { | ||
// keep the fallback | ||
} | ||
function insecureRandomBytes(size) { | ||
var result = createBuffer(size); | ||
for (var i = 0; i < size; ++i) result[i] = Math.floor(Math.random() * 256); | ||
return result; | ||
} | ||
if (randomBytes == null) { | ||
randomBytes = insecureRandomBytes; | ||
} | ||
var randomBytes = insecureRandomBytes; | ||
if (typeof window !== 'undefined' && window.crypto && window.crypto.getRandomValues && typeof Uint8Array !== 'undefined') { | ||
randomBytes = function(size) { | ||
return window.crypto.getRandomValues(new Uint8Array(size)); | ||
}; | ||
} else { | ||
var crypto; | ||
try { | ||
crypto = nativeRequire('crypto'); | ||
randomBytes = crypto.randomBytes; | ||
} catch (e) { | ||
// keep the fallback | ||
} | ||
function deprecate(fn, msg) { | ||
return function() { | ||
console.warn(msg); | ||
var args = Array.prototype.slice.call(arguments); | ||
return fn.apply(this, args); | ||
}; | ||
if (randomBytes == null) { | ||
randomBytes = insecureRandomBytes; | ||
} | ||
} | ||
// constants | ||
var PROCESS_UNIQUE = randomBytes(5); | ||
function deprecate(fn, msg) { | ||
return function() { | ||
console.warn(msg); | ||
var args = Array.prototype.slice.call(arguments); | ||
return fn.apply(this, args); | ||
}; | ||
} | ||
// Regular expression that checks for hex value | ||
var checkForHexRegExp = new RegExp('^[0-9a-fA-F]{24}$'); | ||
// let hasBufferType = false; | ||
// constants | ||
var PROCESS_UNIQUE = randomBytes(5); | ||
// Check if buffer exists | ||
// try { | ||
// if (Buffer && Buffer.from) hasBufferType = true; | ||
// } catch (err) { | ||
// hasBufferType = false; | ||
// } | ||
// Regular expression that checks for hex value | ||
var checkForHexRegExp = new RegExp('^[0-9a-fA-F]{24}$'); | ||
// let hasBufferType = false; | ||
// Precomputed hex table enables speedy hex string conversion | ||
var hexTable = []; | ||
var i; | ||
for (i = 0; i < 256; i++) { | ||
hexTable[i] = (i <= 15 ? '0' : '') + i.toString(16); | ||
} | ||
// Check if buffer exists | ||
// try { | ||
// if (Buffer && Buffer.from) hasBufferType = true; | ||
// } catch (err) { | ||
// hasBufferType = false; | ||
// } | ||
// Lookup tables | ||
var decodeLookup = []; | ||
i = 0; | ||
while (i < 10) decodeLookup[0x30 + i] = i++; | ||
while (i < 16) decodeLookup[0x41 - 10 + i] = decodeLookup[0x61 - 10 + i] = i++; | ||
// Precomputed hex table enables speedy hex string conversion | ||
var hexTable = []; | ||
var i; | ||
for (i = 0; i < 256; i++) { | ||
hexTable[i] = (i <= 15 ? '0' : '') + i.toString(16); | ||
} | ||
// const _Buffer = Buffer; | ||
function convertToHex(bytes) { | ||
// return bytes.toString('hex'); | ||
return bufferToString.call(bytes, 'hex'); | ||
} | ||
// Lookup tables | ||
var decodeLookup = []; | ||
i = 0; | ||
while (i < 10) decodeLookup[0x30 + i] = i++; | ||
while (i < 16) decodeLookup[0x41 - 10 + i] = decodeLookup[0x61 - 10 + i] = i++; | ||
function makeObjectIdError(invalidString, index) { | ||
var invalidCharacter = invalidString[index]; | ||
return new TypeError( | ||
'ObjectId string "' + invalidString + '" contains invalid character "' + invalidCharacter + '" with character code (' + invalidString.charCodeAt( | ||
index | ||
) + '). All character codes for a non-hex string must be less than 256.' | ||
); | ||
} | ||
// const _Buffer = Buffer; | ||
function convertToHex(bytes) { | ||
// return bytes.toString('hex'); | ||
return bufferToString.call(bytes, 'hex'); | ||
} | ||
/** | ||
* Create an ObjectId type | ||
* | ||
* @constructor | ||
* @param {(string|Uint8Array|number)} id Can be a 24 byte hex string, 12 byte binary Buffer, or a Number. | ||
* @property {number} generationTime The generation time of this ObjectId instance | ||
* @return {ObjectId} instance of ObjectId. | ||
*/ | ||
function ObjectId(id) { | ||
if (!(this instanceof ObjectId)) throw new TypeError('Class constructor ObjectId cannot be invoked without \'new\''); | ||
function makeObjectIdError(invalidString, index) { | ||
var invalidCharacter = invalidString[index]; | ||
return new TypeError( | ||
'ObjectId string "' + invalidString + '" contains invalid character "' + invalidCharacter + '" with character code (' + invalidString.charCodeAt( | ||
index | ||
) + '). All character codes for a non-hex string must be less than 256.' | ||
); | ||
} | ||
// Duck-typing to support ObjectId from different npm packages | ||
if (id instanceof ObjectId) return id; | ||
/** | ||
* Create an ObjectId type | ||
* | ||
* @constructor | ||
* @param {(string|Uint8Array|number)} id Can be a 24 byte hex string, 12 byte binary Buffer, or a Number. | ||
* @property {number} generationTime The generation time of this ObjectId instance | ||
* @return {ObjectId} instance of ObjectId. | ||
*/ | ||
function ObjectId(id) { | ||
if (!(this instanceof ObjectId)) throw new TypeError('Class constructor ObjectId cannot be invoked without \'new\''); | ||
// The most common usecase (blank id, new objectId instance) | ||
if (id == null || typeof id === 'number') { | ||
// Generate a new id | ||
this.id = ObjectId.generate(id); | ||
// If we are caching the hex string | ||
if (ObjectId.cacheHexString) this.__id = this.toString('hex'); | ||
// Return the object | ||
return; | ||
} | ||
// Duck-typing to support ObjectId from different npm packages | ||
if (id instanceof ObjectId) return id; | ||
// Check if the passed in id is valid | ||
var valid = ObjectId.isValid(id); | ||
// The most common usecase (blank id, new objectId instance) | ||
if (id == null || typeof id === 'number') { | ||
// Generate a new id | ||
this.id = ObjectId.generate(id); | ||
// If we are caching the hex string | ||
if (ObjectId.cacheHexString) this.__id = this.toString('hex'); | ||
// Return the object | ||
return; | ||
} | ||
// Throw an error if it's not a valid setup | ||
if (!valid && id != null) { | ||
throw new TypeError( | ||
'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters' | ||
); | ||
} else if (valid && typeof id === 'string' && id.length === 24 && __Buffer) { | ||
return new ObjectId(__Buffer.from(id, 'hex')); | ||
} else if (valid && typeof id === 'string' && id.length === 24) { | ||
return ObjectId.createFromHexString(id); | ||
} else if (id != null && id.length === 12) { | ||
if (isArrayLike(id)) { | ||
this.id = bufferFrom(id); | ||
} else { | ||
// assume 12 byte string | ||
this.id = id; | ||
} | ||
} else if (id != null && id.toHexString) { | ||
// Duck-typing to support ObjectId from different npm packages | ||
return ObjectId.createFromHexString(id.toHexString()); | ||
// Check if the passed in id is valid | ||
var valid = ObjectId.isValid(id); | ||
// Throw an error if it's not a valid setup | ||
if (!valid && id != null) { | ||
throw new TypeError( | ||
'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters' | ||
); | ||
} else if (valid && typeof id === 'string' && id.length === 24 && __Buffer) { | ||
return new ObjectId(__Buffer.from(id, 'hex')); | ||
} else if (valid && typeof id === 'string' && id.length === 24) { | ||
return ObjectId.createFromHexString(id); | ||
} else if (id != null && id.length === 12) { | ||
if (isArrayLike(id)) { | ||
this.id = bufferFrom(id); | ||
} else { | ||
throw new TypeError( | ||
'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters' | ||
); | ||
// assume 12 byte string | ||
this.id = id; | ||
} | ||
if (ObjectId.cacheHexString) this.__id = this.toString('hex'); | ||
} else if (id != null && id.toHexString) { | ||
// Duck-typing to support ObjectId from different npm packages | ||
return ObjectId.createFromHexString(id.toHexString()); | ||
} else { | ||
throw new TypeError( | ||
'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters' | ||
); | ||
} | ||
/** | ||
* Return the ObjectId id as a 24 byte hex string representation | ||
* | ||
* @method | ||
* @return {string} return the 24 byte hex string representation. | ||
*/ | ||
ObjectId.prototype.toHexString = function() { | ||
if (ObjectId.cacheHexString && this.__id) return this.__id; | ||
if (ObjectId.cacheHexString) this.__id = this.toString('hex'); | ||
} | ||
var hexString = ''; | ||
if (!this.id || !this.id.length) { | ||
throw new TypeError( | ||
'invalid ObjectId, ObjectId.id must be either a string or a Buffer, but is [' + | ||
JSON.stringify(this.id) + | ||
']' | ||
); | ||
} | ||
/** | ||
* Return the ObjectId id as a 24 byte hex string representation | ||
* | ||
* @method | ||
* @return {string} return the 24 byte hex string representation. | ||
*/ | ||
ObjectId.prototype.toHexString = function() { | ||
if (ObjectId.cacheHexString && this.__id) return this.__id; | ||
if (isArrayLike(this.id)) { | ||
hexString = convertToHex(this.id); | ||
if (ObjectId.cacheHexString) this.__id = hexString; | ||
return hexString; | ||
} | ||
var hexString = ''; | ||
if (!this.id || !this.id.length) { | ||
throw new TypeError( | ||
'invalid ObjectId, ObjectId.id must be either a string or a Buffer, but is [' + | ||
JSON.stringify(this.id) + | ||
']' | ||
); | ||
} | ||
for (var i = 0; i < this.id.length; i++) { | ||
var hexChar = hexTable[this.id.charCodeAt(i)]; | ||
if (typeof hexChar !== 'string') { | ||
throw makeObjectIdError(this.id, i); | ||
} | ||
hexString += hexChar; | ||
} | ||
if (isArrayLike(this.id)) { | ||
hexString = convertToHex(this.id); | ||
if (ObjectId.cacheHexString) this.__id = hexString; | ||
return hexString; | ||
}; | ||
} | ||
/** | ||
* Update the ObjectId index used in generating new ObjectId's on the driver | ||
* | ||
* @method | ||
* @return {number} returns next index value. | ||
* @ignore | ||
*/ | ||
ObjectId.getInc = function() { | ||
return (ObjectId.index = (ObjectId.index + 1) % 0xffffff); | ||
}; | ||
/** | ||
* Generate a 12 byte id buffer used in ObjectId's | ||
* | ||
* @method | ||
* @param {number} [time] optional parameter allowing to pass in a second based timestamp. | ||
* @return {Uint8Array} return the 12 byte id buffer string. | ||
*/ | ||
ObjectId.generate = function(time) { | ||
if ('number' !== typeof time) { | ||
time = ~~(Date.now() / 1000); | ||
for (var i = 0; i < this.id.length; i++) { | ||
var hexChar = hexTable[this.id.charCodeAt(i)]; | ||
if (typeof hexChar !== 'string') { | ||
throw makeObjectIdError(this.id, i); | ||
} | ||
hexString += hexChar; | ||
} | ||
var inc = ObjectId.getInc(); | ||
var buffer = createBuffer(12); | ||
if (ObjectId.cacheHexString) this.__id = hexString; | ||
return hexString; | ||
}; | ||
// 4-byte timestamp | ||
buffer[3] = time & 0xff; | ||
buffer[2] = (time >> 8) & 0xff; | ||
buffer[1] = (time >> 16) & 0xff; | ||
buffer[0] = (time >> 24) & 0xff; | ||
/** | ||
* Update the ObjectId index used in generating new ObjectId's on the driver | ||
* | ||
* @method | ||
* @return {number} returns next index value. | ||
* @ignore | ||
*/ | ||
ObjectId.getInc = function() { | ||
return (ObjectId.index = (ObjectId.index + 1) % 0xffffff); | ||
}; | ||
// 5-byte process unique | ||
buffer[4] = PROCESS_UNIQUE[0]; | ||
buffer[5] = PROCESS_UNIQUE[1]; | ||
buffer[6] = PROCESS_UNIQUE[2]; | ||
buffer[7] = PROCESS_UNIQUE[3]; | ||
buffer[8] = PROCESS_UNIQUE[4]; | ||
/** | ||
* Generate a 12 byte id buffer used in ObjectId's | ||
* | ||
* @method | ||
* @param {number} [time] optional parameter allowing to pass in a second based timestamp. | ||
* @return {Uint8Array} return the 12 byte id buffer string. | ||
*/ | ||
ObjectId.generate = function(time) { | ||
if ('number' !== typeof time) { | ||
time = ~~(Date.now() / 1000); | ||
} | ||
// 3-byte counter | ||
buffer[11] = inc & 0xff; | ||
buffer[10] = (inc >> 8) & 0xff; | ||
buffer[9] = (inc >> 16) & 0xff; | ||
var inc = ObjectId.getInc(); | ||
var buffer = createBuffer(12); | ||
return buffer; | ||
}; | ||
// 4-byte timestamp | ||
buffer[3] = time & 0xff; | ||
buffer[2] = (time >> 8) & 0xff; | ||
buffer[1] = (time >> 16) & 0xff; | ||
buffer[0] = (time >> 24) & 0xff; | ||
/** | ||
* Converts the id into a 24 byte hex string for printing | ||
* | ||
* @return {String} return the 24 byte hex string representation. | ||
* @ignore | ||
*/ | ||
ObjectId.prototype.toString = function(format) { | ||
// Is the id a buffer then use the buffer toString method to return the format | ||
if (isArrayLike(this.id)) { | ||
// return this.id.toString(typeof format === 'string' ? format : 'hex'); | ||
return bufferToString.call(this.id, typeof format === 'string' ? format : 'hex'); | ||
} | ||
// 5-byte process unique | ||
buffer[4] = PROCESS_UNIQUE[0]; | ||
buffer[5] = PROCESS_UNIQUE[1]; | ||
buffer[6] = PROCESS_UNIQUE[2]; | ||
buffer[7] = PROCESS_UNIQUE[3]; | ||
buffer[8] = PROCESS_UNIQUE[4]; | ||
return this.toHexString(); | ||
}; | ||
// 3-byte counter | ||
buffer[11] = inc & 0xff; | ||
buffer[10] = (inc >> 8) & 0xff; | ||
buffer[9] = (inc >> 16) & 0xff; | ||
/** | ||
* Converts to its JSON representation. | ||
* | ||
* @return {String} return the 24 byte hex string representation. | ||
* @ignore | ||
*/ | ||
ObjectId.prototype.toJSON = function() { | ||
return this.toHexString(); | ||
}; | ||
return buffer; | ||
}; | ||
/** | ||
* Compares the equality of this ObjectId with `otherID`. | ||
* | ||
* @method | ||
* @param {object} otherId ObjectId instance to compare against. | ||
* @return {boolean} the result of comparing two ObjectId's | ||
*/ | ||
ObjectId.prototype.equals = function(otherId) { | ||
if (otherId instanceof ObjectId) { | ||
return this.toString() === otherId.toString(); | ||
} | ||
/** | ||
* Converts the id into a 24 byte hex string for printing | ||
* | ||
* @return {String} return the 24 byte hex string representation. | ||
* @ignore | ||
*/ | ||
ObjectId.prototype.toString = function(format) { | ||
// Is the id a buffer then use the buffer toString method to return the format | ||
if (isArrayLike(this.id)) { | ||
// return this.id.toString(typeof format === 'string' ? format : 'hex'); | ||
return bufferToString.call(this.id, typeof format === 'string' ? format : 'hex'); | ||
} | ||
if ( | ||
typeof otherId === 'string' && | ||
ObjectId.isValid(otherId) && | ||
otherId.length === 12 && | ||
(isArrayLike(this.id)) | ||
) { | ||
// return otherId === this.id.toString('binary'); | ||
return otherId === bufferToString.call(this.id, 'binary'); | ||
} | ||
return this.toHexString(); | ||
}; | ||
if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 24) { | ||
return otherId.toLowerCase() === this.toHexString(); | ||
} | ||
/** | ||
* Converts to its JSON representation. | ||
* | ||
* @return {String} return the 24 byte hex string representation. | ||
* @ignore | ||
*/ | ||
ObjectId.prototype.toJSON = function() { | ||
return this.toHexString(); | ||
}; | ||
if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 12) { | ||
return otherId === this.id; | ||
} | ||
/** | ||
* Compares the equality of this ObjectId with `otherID`. | ||
* | ||
* @method | ||
* @param {object} otherId ObjectId instance to compare against. | ||
* @return {boolean} the result of comparing two ObjectId's | ||
*/ | ||
ObjectId.prototype.equals = function(otherId) { | ||
if (otherId instanceof ObjectId) { | ||
return this.toString() === otherId.toString(); | ||
} | ||
if (otherId != null && (otherId instanceof ObjectId || otherId.toHexString)) { | ||
return otherId.toHexString() === this.toHexString(); | ||
} | ||
if ( | ||
typeof otherId === 'string' && | ||
ObjectId.isValid(otherId) && | ||
otherId.length === 12 && | ||
(isArrayLike(this.id)) | ||
) { | ||
// return otherId === this.id.toString('binary'); | ||
return otherId === bufferToString.call(this.id, 'binary'); | ||
} | ||
return false; | ||
}; | ||
if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 24) { | ||
return otherId.toLowerCase() === this.toHexString(); | ||
} | ||
/** | ||
* Returns the generation date (accurate up to the second) that this ID was generated. | ||
* | ||
* @method | ||
* @return {Date} the generation date | ||
*/ | ||
ObjectId.prototype.getTimestamp = function() { | ||
var timestamp = new Date(); | ||
var time = readUInt32BE.call(this.id, 0); | ||
timestamp.setTime(Math.floor(time) * 1000); | ||
return timestamp; | ||
}; | ||
if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 12) { | ||
return otherId === this.id; | ||
} | ||
/** | ||
* @ignore | ||
*/ | ||
ObjectId.createPk = function() { | ||
return new ObjectId(); | ||
}; | ||
if (otherId != null && (otherId instanceof ObjectId || otherId.toHexString)) { | ||
return otherId.toHexString() === this.toHexString(); | ||
} | ||
/** | ||
* Creates an ObjectId from a second based number, with the rest of the ObjectId zeroed out. Used for comparisons or sorting the ObjectId. | ||
* | ||
* @method | ||
* @param {number} time an integer number representing a number of seconds. | ||
* @return {ObjectId} return the created ObjectId | ||
*/ | ||
ObjectId.createFromTime = function(time) { | ||
var buffer = createBuffer(12); | ||
// Encode time into first 4 bytes | ||
buffer[3] = time & 0xff; | ||
buffer[2] = (time >> 8) & 0xff; | ||
buffer[1] = (time >> 16) & 0xff; | ||
buffer[0] = (time >> 24) & 0xff; | ||
// Return the new objectId | ||
return new ObjectId(buffer); | ||
}; | ||
return false; | ||
}; | ||
/** | ||
* Creates an ObjectId from a hex string representation of an ObjectId. | ||
* | ||
* @method | ||
* @param {string} hexString create a ObjectId from a passed in 24 byte hexstring. | ||
* @return {ObjectId} return the created ObjectId | ||
*/ | ||
ObjectId.createFromHexString = function(hexString) { | ||
// Throw an error if it's not a valid setup | ||
if (typeof hexString === 'undefined' || (hexString != null && hexString.length !== 24)) { | ||
throw new TypeError( | ||
'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters' | ||
); | ||
} | ||
/** | ||
* Returns the generation date (accurate up to the second) that this ID was generated. | ||
* | ||
* @method | ||
* @return {Date} the generation date | ||
*/ | ||
ObjectId.prototype.getTimestamp = function() { | ||
var timestamp = new Date(); | ||
var time = readUInt32BE.call(this.id, 0); | ||
timestamp.setTime(Math.floor(time) * 1000); | ||
return timestamp; | ||
}; | ||
// Use Buffer.from method if available | ||
// if (hasBufferType) return new ObjectId(Buffer.from(string, 'hex')); | ||
/** | ||
* @ignore | ||
*/ | ||
ObjectId.createPk = function() { | ||
return new ObjectId(); | ||
}; | ||
// Calculate lengths | ||
var array = createBuffer(12); | ||
/** | ||
* Creates an ObjectId from a second based number, with the rest of the ObjectId zeroed out. Used for comparisons or sorting the ObjectId. | ||
* | ||
* @method | ||
* @param {number} time an integer number representing a number of seconds. | ||
* @return {ObjectId} return the created ObjectId | ||
*/ | ||
ObjectId.createFromTime = function(time) { | ||
var buffer = createBuffer(12); | ||
// Encode time into first 4 bytes | ||
buffer[3] = time & 0xff; | ||
buffer[2] = (time >> 8) & 0xff; | ||
buffer[1] = (time >> 16) & 0xff; | ||
buffer[0] = (time >> 24) & 0xff; | ||
// Return the new objectId | ||
return new ObjectId(buffer); | ||
}; | ||
var n = 0; | ||
var i = 0; | ||
while (i < 24) { | ||
array[n++] = | ||
(decodeLookup[hexString.charCodeAt(i++)] << 4) | decodeLookup[hexString.charCodeAt(i++)]; | ||
} | ||
/** | ||
* Creates an ObjectId from a hex string representation of an ObjectId. | ||
* | ||
* @method | ||
* @param {string} hexString create a ObjectId from a passed in 24 byte hexstring. | ||
* @return {ObjectId} return the created ObjectId | ||
*/ | ||
ObjectId.createFromHexString = function(hexString) { | ||
// Throw an error if it's not a valid setup | ||
if (typeof hexString === 'undefined' || (hexString != null && hexString.length !== 24)) { | ||
throw new TypeError( | ||
'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters' | ||
); | ||
} | ||
return new ObjectId(array); | ||
}; | ||
// Use Buffer.from method if available | ||
// if (hasBufferType) return new ObjectId(Buffer.from(string, 'hex')); | ||
/** | ||
* Checks if a value is a valid bson ObjectId | ||
* | ||
* @method | ||
* @param {any} id | ||
* @return {boolean} return true if the value is a valid bson ObjectId, return false otherwise. | ||
*/ | ||
ObjectId.isValid = function(id) { | ||
if (id == null) return false; | ||
// Calculate lengths | ||
var array = createBuffer(12); | ||
if (typeof id === 'number') { | ||
return true; | ||
} | ||
var n = 0; | ||
var i = 0; | ||
while (i < 24) { | ||
array[n++] = | ||
(decodeLookup[hexString.charCodeAt(i++)] << 4) | decodeLookup[hexString.charCodeAt(i++)]; | ||
} | ||
if (typeof id === 'string') { | ||
return id.length === 12 || (id.length === 24 && checkForHexRegExp.test(id)); | ||
} | ||
return new ObjectId(array); | ||
}; | ||
if (id instanceof ObjectId) { | ||
return true; | ||
} | ||
/** | ||
* Checks if a value is a valid bson ObjectId | ||
* | ||
* @method | ||
* @param {any} id | ||
* @return {boolean} return true if the value is a valid bson ObjectId, return false otherwise. | ||
*/ | ||
ObjectId.isValid = function(id) { | ||
if (id == null) return false; | ||
if (isArrayLike(id) && id.length === 12) { | ||
return true; | ||
} | ||
if (typeof id === 'number') { | ||
return true; | ||
} | ||
// Duck-Typing detection of ObjectId like objects | ||
if (id.toHexString) { | ||
return id.id.length === 12 || (id.id.length === 24 && checkForHexRegExp.test(id.id)); | ||
} | ||
if (typeof id === 'string') { | ||
return id.length === 12 || (id.length === 24 && checkForHexRegExp.test(id)); | ||
} | ||
return false; | ||
}; | ||
if (id instanceof ObjectId) { | ||
return true; | ||
} | ||
/** | ||
* @ignore | ||
*/ | ||
ObjectId.prototype.toExtendedJSON = function() { | ||
if (this.toHexString) return { $oid: this.toHexString() }; | ||
return { $oid: this.toString('hex') }; | ||
}; | ||
if (isArrayLike(id) && id.length === 12) { | ||
return true; | ||
} | ||
/** | ||
* @ignore | ||
*/ | ||
ObjectId.fromExtendedJSON = function(doc) { | ||
return new ObjectId(doc.$oid); | ||
}; | ||
// Duck-Typing detection of ObjectId like objects | ||
if (id.toHexString) { | ||
return id.id.length === 12 || (id.id.length === 24 && checkForHexRegExp.test(id.id)); | ||
} | ||
// Deprecated methods | ||
ObjectId.get_inc = deprecate( | ||
function() { return ObjectId.getInc(); }, | ||
'Please use the static `ObjectId.getInc()` instead' | ||
); | ||
return false; | ||
}; | ||
ObjectId.prototype.get_inc = deprecate( | ||
function() { return ObjectId.getInc(); }, | ||
'Please use the static `ObjectId.getInc()` instead' | ||
); | ||
/** | ||
* @ignore | ||
*/ | ||
ObjectId.prototype.toExtendedJSON = function() { | ||
if (this.toHexString) return { $oid: this.toHexString() }; | ||
return { $oid: this.toString('hex') }; | ||
}; | ||
ObjectId.prototype.getInc = deprecate( | ||
function() { return ObjectId.getInc(); }, | ||
'Please use the static `ObjectId.getInc()` instead' | ||
); | ||
/** | ||
* @ignore | ||
*/ | ||
ObjectId.fromExtendedJSON = function(doc) { | ||
return new ObjectId(doc.$oid); | ||
}; | ||
ObjectId.prototype.generate = deprecate( | ||
function(time) { return ObjectId.generate(time); }, | ||
'Please use the static `ObjectId.generate(time)` instead' | ||
); | ||
// Deprecated methods | ||
ObjectId.get_inc = deprecate( | ||
function() { return ObjectId.getInc(); }, | ||
'Please use the static `ObjectId.getInc()` instead' | ||
); | ||
try { | ||
/** | ||
* @ignore | ||
*/ | ||
Object.defineProperty(ObjectId.prototype, 'generationTime', { | ||
enumerable: true, | ||
get: function() { | ||
return this.id[3] | (this.id[2] << 8) | (this.id[1] << 16) | (this.id[0] << 24); | ||
}, | ||
set: function(value) { | ||
// Encode time into first 4 bytes | ||
this.id[3] = value & 0xff; | ||
this.id[2] = (value >> 8) & 0xff; | ||
this.id[1] = (value >> 16) & 0xff; | ||
this.id[0] = (value >> 24) & 0xff; | ||
} | ||
}); | ||
} catch (err) { | ||
ObjectId.prototype.generationTime = 0; | ||
} | ||
ObjectId.prototype.get_inc = deprecate( | ||
function() { return ObjectId.getInc(); }, | ||
'Please use the static `ObjectId.getInc()` instead' | ||
); | ||
var util; | ||
try { | ||
util = __node_require__('util'); | ||
ObjectId.prototype[util.inspect.custom || 'inspect'] = ObjectId.prototype.toString; | ||
} catch (e) { | ||
ObjectId.prototype.inspect = ObjectId.prototype.toString; | ||
} | ||
ObjectId.prototype.getInc = deprecate( | ||
function() { return ObjectId.getInc(); }, | ||
'Please use the static `ObjectId.getInc()` instead' | ||
); | ||
/** | ||
* @ignore | ||
*/ | ||
ObjectId.index = ~~(Math.random() * 0xffffff); | ||
ObjectId.prototype.generate = deprecate( | ||
function(time) { return ObjectId.generate(time); }, | ||
'Please use the static `ObjectId.generate(time)` instead' | ||
); | ||
// In 4.0.0 and 4.0.1, this property name was changed to ObjectId to match the class name. | ||
// This caused interoperability problems with previous versions of the library, so in | ||
// later builds we changed it back to ObjectID (capital D) to match legacy implementations. | ||
try { | ||
Object.defineProperty(ObjectId.prototype, '_bsontype', { value: 'ObjectID' }); | ||
} catch (err) { | ||
ObjectId.prototype._bsontype = 'ObjectID'; | ||
} | ||
try { | ||
Object.defineProperty(ObjectId.prototype, 'generationTime', { | ||
enumerable: true, | ||
'get': function() { | ||
return this.id[3] | (this.id[2] << 8) | (this.id[1] << 16) | (this.id[0] << 24); | ||
}, | ||
'set': function(value) { | ||
// Encode time into first 4 bytes | ||
this.id[3] = value & 0xff; | ||
this.id[2] = (value >> 8) & 0xff; | ||
this.id[1] = (value >> 16) & 0xff; | ||
this.id[0] = (value >> 24) & 0xff; | ||
} | ||
}); | ||
} catch (err) { | ||
ObjectId.prototype.generationTime = 0; | ||
} | ||
return ObjectId; | ||
var util; | ||
try { | ||
util = nativeRequire('util'); | ||
ObjectId.prototype[util.inspect.custom || 'inspect'] = ObjectId.prototype.toString; | ||
} catch (e) { | ||
ObjectId.prototype.inspect = ObjectId.prototype.toString; | ||
} | ||
}); | ||
/** | ||
* @ignore | ||
*/ | ||
ObjectId.index = ~~(Math.random() * 0xffffff); | ||
// In 4.0.0 and 4.0.1, this property name was changed to ObjectId to match the class name. | ||
// This caused interoperability problems with previous versions of the library, so in | ||
// later builds we changed it back to ObjectID (capital D) to match legacy implementations. | ||
try { | ||
Object.defineProperty(ObjectId.prototype, '_bsontype', { value: 'ObjectID' }); | ||
} catch (err) { | ||
ObjectId.prototype._bsontype = 'ObjectID'; | ||
} | ||
exports.ObjectId = ObjectId; |
{ | ||
"name": "@tybys/oid", | ||
"version": "1.0.8", | ||
"version": "2.0.0", | ||
"bin": { | ||
@@ -17,3 +17,3 @@ "oid": "./bin/oid.js" | ||
"tsc": "tsc -p test/ts", | ||
"build": "uglifyjs index.js --ie8 -m -o dist/oid.min.js", | ||
"build": "node ./scripts/build.js", | ||
"webpack": "webpack ./test/webpack/index.js --devtool none --mode development --output ./test/webpack/main.js&&node ./test/webpack/main.js" | ||
@@ -31,12 +31,21 @@ }, | ||
"homepage": "https://github.com/toyobayashi/oid/tree/master/js#readme", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"devDependencies": { | ||
"@rollup/plugin-commonjs": "^11.1.0", | ||
"@rollup/plugin-node-resolve": "^7.1.3", | ||
"@types/node": "^12.7.5", | ||
"eslint": "^6.4.0", | ||
"mocha": "^6.2.0", | ||
"eslint": "^6.8.0", | ||
"mocha": "^7.1.2", | ||
"rollup": "^2.7.2", | ||
"rollup-plugin-terser": "^5.3.0", | ||
"should": "^13.2.3", | ||
"typescript": "^3.6.3", | ||
"uglify-js": "^3.6.0", | ||
"webpack": "^4.41.4", | ||
"webpack-cli": "^3.3.10" | ||
"typescript": "^3.8.3", | ||
"webpack": "^4.43.0", | ||
"webpack-cli": "^3.3.11" | ||
}, | ||
"dependencies": { | ||
"@tybys/native-require": "^1.1.0" | ||
} | ||
} |
@@ -5,4 +5,4 @@ # oid-js | ||
* `index.js`: 18KB | ||
* `dist/oid.min.js`: 8KB | ||
* `dist/oid.js`: 17KB | ||
* `dist/oid.min.js`: 7KB | ||
@@ -40,4 +40,3 @@ ## Usage | ||
<script> | ||
var oid = new ObjectId(); | ||
console.log(oid); | ||
console.log(new oid.ObjectId()); | ||
</script> | ||
@@ -49,5 +48,4 @@ ``` | ||
``` js | ||
const ObjectId = require('@tybys/oid') | ||
const oid = new ObjectId() | ||
console.log(oid) | ||
const { ObjectId } = require('@tybys/oid') | ||
console.log(new ObjectId()) | ||
``` | ||
@@ -58,5 +56,4 @@ | ||
``` ts | ||
import * as ObjectId from '@tybys/oid' | ||
const oid = new ObjectId() | ||
console.log(oid) | ||
import { ObjectId } from '@tybys/oid' | ||
console.log(new ObjectId()) | ||
``` | ||
@@ -71,3 +68,11 @@ | ||
``` ts | ||
declare class ObjectId { | ||
export declare interface EmptyDocument { | ||
$oid: string; | ||
} | ||
export declare interface Document extends EmptyDocument { | ||
[field: string]: any; | ||
} | ||
export declare class ObjectId { | ||
constructor(id?: number | string | number[] | Uint8Array | ObjectId); | ||
@@ -87,3 +92,3 @@ | ||
static fromExtendedJSON(doc: ObjectId.Document): ObjectId; | ||
static fromExtendedJSON(doc: Document): ObjectId; | ||
@@ -108,16 +113,4 @@ static index: number; | ||
toExtendedJSON(): ObjectId.EmptyDocument; | ||
toExtendedJSON(): EmptyDocument; | ||
} | ||
declare namespace ObjectId { | ||
export interface EmptyDocument { | ||
$oid: string; | ||
} | ||
export interface Document extends EmptyDocument { | ||
[field: string]: any; | ||
} | ||
} | ||
export = ObjectId; | ||
``` |
46554
7
1132
1
11
110
+ Added@tybys/native-require@^1.1.0
+ Added@tybys/native-require@1.3.1(transitive)