@tybys/oid
Version:
Use MongoDB ObjectID without installing bson.
2 lines (1 loc) • 12.6 kB
JavaScript
var _Symbol;function tryGetRequireFunction(){return"undefined"!=typeof __webpack_public_path__||"undefined"!=typeof __webpack_public_path__?"undefined"!=typeof __non_webpack_require__?__non_webpack_require__:undefined:"undefined"!=typeof require?require:undefined}"function"==typeof Symbol?_Symbol=Symbol:(_Symbol=function(e){return"Symbol("+e+")"})["for"]=function(e){return"Symbol("+e+")"};var __Buffer,_require$1=tryGetRequireFunction();if("function"==typeof _require$1)try{__Buffer=_require$1("buffer").Buffer}catch(_){}function numberIsNaN(e){return Number.isNaN?Number.isNaN(e):"number"==typeof e&&isNaN(e)}var toString=Object.prototype.toString;function isArray(e){return Array.isArray?Array.isArray(e):"[object Array]"===toString.call(e)}function isBufferOrArray(e){return"undefined"!=typeof Uint8Array&&e instanceof Uint8Array||"[object Uint8Array]"===toString.call(e)||isArray(e)}function bufferAlloc(e){if(__Buffer)return __Buffer.alloc(e);var t;if("undefined"!=typeof Uint8Array)return new Uint8Array(e);var r=[];for(t=0;t<e;t++)r[t]=0;return r}function bufferFrom(e){var t,r;if(__Buffer)return __Buffer.from(e);if("function"==typeof ArrayBuffer){if(e instanceof ArrayBuffer)return new Uint8Array(e);if((null===(t=ArrayBuffer.isView)||void 0===t?void 0:t.call(ArrayBuffer,e))||e instanceof Uint8Array){if("function"==typeof Uint8Array.from)return Uint8Array.from(e);var n=new Uint8Array(e.buffer,e.byteOffset,e.byteLength),i=new Uint8Array(e.byteLength);for(r=0;r<e.byteLength;r++)i[r]=n[r];return i}if(isArray(e))return"function"==typeof Uint8Array.from?Uint8Array.from(e):new Uint8Array(e);if("string"==typeof e)return bufferFrom(utf8ToBytes(e))}if("string"==typeof e)return utf8ToBytes(e);var o=[];for(r=0;r<e.length;r++)o[r]=e[r];return o}function bufferFromHex(e){if(__Buffer)return __Buffer.from(e,"hex");var t;hexWrite(t="function"==typeof Uint8Array?new Uint8Array(Math.floor(e.length/2)):new Array(Math.floor(e.length/2)),e);for(var r=0;r<t.length;r++)t[r]=parseInt(e.substring(2*r,2*r+2),16);return t}function readUInt32BE(e){if(void 0===e&&(e=0),null==__Buffer?void 0:__Buffer.isBuffer(this))return this.readUInt32BE(e);var t=typeof e;if("number"!==t)throw new TypeError("The offset argument must be of type number. Received type "+t);var r=this[e],n=this[e+3];if(r===undefined||n===undefined)throw new RangeError("Attempt to write outside buffer bounds");return r*Math.pow(2,24)+this[++e]*Math.pow(2,16)+this[++e]*Math.pow(2,8)+n}function readInt32BE(e){if(void 0===e&&(e=0),null==__Buffer?void 0:__Buffer.isBuffer(this))return this.readInt32BE(e);var t=typeof e;if("number"!==t)throw new TypeError("The offset argument must be of type number. Received type "+t);var r=this[e],n=this[e+3];if(r===undefined||n===undefined)throw new RangeError("Attempt to write outside buffer bounds");return(r<<24)+this[++e]*Math.pow(2,16)+this[++e]*Math.pow(2,8)+n}function checkInt(e,t,r,n,i,o){if(e>r||e<t){var f="bigint"==typeof t?"n":"",u=void 0;throw u=o>3?0===t||"function"==typeof BigInt&&BigInt(t)===BigInt(0)?">= 0"+f+" and < 2"+f+" ** "+8*(o+1)+f:">= -(2"+f+" ** "+(8*(o+1)-1)+f+") and < 2 ** "+(8*(o+1)-1)+f:">= "+t+f+" and <= "+r+f,new RangeError("value"+u)}if(n[i]===undefined||n[i+o]===undefined)throw new RangeError("Attempt to write outside buffer bounds")}function writeU_Int32BE(e,t,r,n,i){return checkInt(t=+t,n,i,e,r,3),e[r+3]=255&t,t>>>=8,e[r+2]=255&t,t>>>=8,e[r+1]=255&t,t>>>=8,e[r]=255&t,r+4}function writeUInt32BE(e,t){return void 0===t&&(t=0),writeU_Int32BE(this,e,t,0,4294967295)}function bufferEquals(e){if(!isBufferOrArray(e))throw new TypeError('The "target" argument must be one of type Buffer or Uint8Array. Received type '+typeof e);if(this===e)return!0;if(this.length!==e.length)return!1;for(var t=0;t<this.length;t++)if(this[t]!==e[t])return!1;return!0}function bufferToString(e){if(__Buffer)return __Buffer.from(this).toString(e);var t="",r=0;if("hex"===e){for(t="",r=0;r<this.length;r++){var n=this[r].toString(16);t+=1===n.length?"0"+n:n}return t}if("binary"===e||"latin1"===e){for(t="",r=0;r<this.length;r++)t+=String.fromCharCode(this[r]>>>0&255);return t}var i=void 0,o=void 0,f=void 0,u=void 0;i="";var a=this.length;for(r=0;r<a;)switch((o=this[r++])>>4){case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:i+=String.fromCharCode(o);break;case 12:case 13:f=this[r++],i+=String.fromCharCode((31&o)<<6|63&f);break;case 14:f=this[r++],u=this[r++],i+=String.fromCharCode((15&o)<<12|(63&f)<<6|(63&u)<<0)}return i}function utf8ToBytes(e,t){var r;void 0===t&&(t=Infinity);for(var n=e.length,i=null,o=[],f=0;f<n;++f){if((r=e.charCodeAt(f))>55295&&r<57344){if(!i){if(r>56319){(t-=3)>-1&&o.push(239,191,189);continue}if(f+1===n){(t-=3)>-1&&o.push(239,191,189);continue}i=r;continue}if(r<56320){(t-=3)>-1&&o.push(239,191,189),i=r;continue}r=65536+(i-55296<<10|r-56320)}else i&&(t-=3)>-1&&o.push(239,191,189);if(i=null,r<128){if((t-=1)<0)break;o.push(r)}else if(r<2048){if((t-=2)<0)break;o.push(r>>6|192,63&r|128)}else if(r<65536){if((t-=3)<0)break;o.push(r>>12|224,r>>6&63|128,63&r|128)}else{if(!(r<1114112))throw new Error("Invalid code point");if((t-=4)<0)break;o.push(r>>18|240,r>>12&63|128,r>>6&63|128,63&r|128)}}return o}function hexWrite(e,t,r){void 0===r&&(r=0);var n,i=e.length-r,o=t.length;for(i>o/2&&(i=o/2),n=0;n<i;++n){var f=parseInt(t.substring(2*n,2*n+2),16);if(numberIsNaN(f))return n;e[r+n]=f}return n}var _require=tryGetRequireFunction(),isReactNative="object"==typeof navigator&&"ReactNative"===navigator.product,insecureWarning=isReactNative?"BSON: For React Native please polyfill crypto.getRandomValues, e.g. using: https://www.npmjs.com/package/react-native-get-random-values.":"BSON: No cryptographic implementation for random bytes present, falling back to a less secure implementation.",insecureRandomBytes=function(e){"undefined"!=typeof console&&console.warn(insecureWarning);for(var t=bufferAlloc(e),r=0;r<e;++r)t[r]=Math.floor(256*Math.random());return t},detectRandomBytes=function(){if("undefined"!=typeof window){var e=window.crypto||window.msCrypto;if(null==e?void 0:e.getRandomValues)return function(t){return e.getRandomValues(new Uint8Array(t))}}if("undefined"!=typeof crypto&&crypto.getRandomValues)return function(e){return crypto.getRandomValues(new Uint8Array(e))};var t;try{t=_require("crypto").randomBytes}catch(r){}return null!=t?t:insecureRandomBytes},randomBytes=detectRandomBytes();function isAnyArrayBuffer(e){return-1!==["[object ArrayBuffer]","[object SharedArrayBuffer]"].indexOf(Object.prototype.toString.call(e))}function deprecate(e,t){if("function"==typeof _require)return _require("util").deprecate(e,t);var r=!1;return function(){for(var n=[],i=0;i<arguments.length;i++)n[i]=arguments[i];return r||("undefined"!=typeof console&&console.warn(t),r=!0),e.apply(this,n)}}function ensureBuffer(e){var t;if("function"==typeof ArrayBuffer&&"function"==typeof Uint8Array&&((null===(t=ArrayBuffer.isView)||void 0===t?void 0:t.call(ArrayBuffer,e))||e instanceof Uint8Array))return new Uint8Array(e.buffer,e.byteOffset,e.byteLength);if(isAnyArrayBuffer(e))return bufferFrom(e);throw new TypeError("Must use either ArrayBuffer or TypedArray")}for(var supportDefineProperty=function(){try{return Object.defineProperty({},"a",{value:0}),!0}catch(_){return!1}}(),defValue=supportDefineProperty?function(e,t,r){return Object.defineProperty(e,t,r)}:function(e,t,r){return e[t]=r.value,e},PROCESS_UNIQUE=randomBytes(5),checkForHexRegExp=/^[0-9a-fA-F]{24}$/,hexTable=[],i_1=0;i_1<256;i_1++)hexTable[i_1]=(i_1<=15?"0":"")+i_1.toString(16);for(var decodeLookup=[],i=0;i<10;)decodeLookup[48+i]=i++;for(;i<16;)decodeLookup[55+i]=decodeLookup[87+i]=i++;var kId=_Symbol("id"),ObjectId=function(){function e(t){var r;if(!(this instanceof e))return new e(t);if(t instanceof e&&(this[kId]=t.id,"__id"in t&&(this.__id=t.__id)),"object"==typeof t&&t&&"id"in t&&("toHexString"in t&&"function"==typeof t.toHexString?this[kId]=bufferFromHex(t.toHexString()):this[kId]="string"==typeof t.id?bufferFrom(t.id):t.id),null!=t&&"number"!=typeof t||(this[kId]=e.generate("number"==typeof t?t:undefined),e.cacheHexString&&(this.__id=bufferToString.call(this.id,"hex"))),"function"==typeof ArrayBuffer&&(((null===(r=ArrayBuffer.isView)||void 0===r?void 0:r.call(ArrayBuffer,t))||t instanceof Uint8Array)&&12===t.byteLength)&&(this[kId]=ensureBuffer(t)),isBufferOrArray(t)){if(12!==t.length)throw new TypeError("Argument passed in must be a Buffer or string of 12 bytes or a string of 24 hex characters");this[kId]=bufferFrom(t)}if("string"==typeof t)if(12===t.length){var n=bufferFrom(t);if(12!==n.length)throw new TypeError("Argument passed in must be a Buffer or string of 12 bytes or a string of 24 hex characters");this[kId]=n}else{if(24!==t.length||!checkForHexRegExp.test(t))throw new TypeError("Argument passed in must be a Buffer or string of 12 bytes or a string of 24 hex characters");this[kId]=bufferFromHex(t)}supportDefineProperty||(this.id=this[kId],this.generationTime=readInt32BE.call(this.id,0)),e.cacheHexString&&(this.__id=bufferToString.call(this.id,"hex"))}return e.prototype.toHexString=function(){if(e.cacheHexString&&this.__id)return this.__id;var t=bufferToString.call(this.id,"hex");return e.cacheHexString&&!this.__id&&(this.__id=t),t},e.getInc=function(){return e.index=(e.index+1)%16777215},e.generate=function(t){"number"!=typeof t&&(t=~~(Date.now()/1e3));var r=e.getInc(),n=bufferAlloc(12);return writeUInt32BE.call(n,t,0),n[4]=PROCESS_UNIQUE[0],n[5]=PROCESS_UNIQUE[1],n[6]=PROCESS_UNIQUE[2],n[7]=PROCESS_UNIQUE[3],n[8]=PROCESS_UNIQUE[4],n[11]=255&r,n[10]=r>>8&255,n[9]=r>>16&255,n},e.prototype.toString=function(e){return e?bufferToString.call(this.id,e):this.toHexString()},e.prototype.toJSON=function(){return this.toHexString()},e.prototype.equals=function(t){return t!==undefined&&null!==t&&(t instanceof e?this.toString()===t.toString():"string"==typeof t&&e.isValid(t)&&12===t.length&&isBufferOrArray(this.id)?t===bufferToString.call(this.id,"latin1"):"string"==typeof t&&e.isValid(t)&&24===t.length?t.toLowerCase()===this.toHexString():"string"==typeof t&&e.isValid(t)&&12===t.length?bufferEquals.call(bufferFrom(t),this.id):"object"==typeof t&&"toHexString"in t&&"function"==typeof t.toHexString&&t.toHexString()===this.toHexString())},e.prototype.getTimestamp=function(){var e=new Date,t=readUInt32BE.call(this.id,0);return e.setTime(1e3*Math.floor(t)),e},e.createPk=function(){return new e},e.createFromTime=function(t){var r=bufferFrom([0,0,0,0,0,0,0,0,0,0,0,0]);return writeUInt32BE.call(r,t,0),new e(r)},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");return new e(bufferFromHex(t))},e.isValid=function(t){return null!=t&&("number"==typeof t||("string"==typeof t?12===t.length||24===t.length&&checkForHexRegExp.test(t):t instanceof e||(!(!isBufferOrArray(t)||12!==t.length)||"object"==typeof t&&"toHexString"in t&&"function"==typeof t.toHexString&&("string"==typeof t.id?12===t.id.length:24===t.toHexString().length&&checkForHexRegExp.test(bufferToString.call(t.id,"hex"))))))},e.prototype.toExtendedJSON=function(){return this.toHexString?{$oid:this.toHexString()}:{$oid:this.toString("hex")}},e.fromExtendedJSON=function(t){return new e(t.$oid)},e.prototype[_Symbol["for"]("nodejs.util.inspect.custom")]=function(){return this.inspect()},e.prototype.inspect=function(){return'new ObjectId("'+this.toHexString()+'")'},e.index=~~(16777215*Math.random()),e}();try{Object.defineProperty(ObjectId.prototype,"id",{configurable:!0,enumerable:!0,get:function(){return this[kId]},set:function(e){this[kId]=e,ObjectId.cacheHexString&&(this.__id=bufferToString.call(e,"hex"))}}),Object.defineProperty(ObjectId.prototype,"generationTime",{configurable:!0,enumerable:!0,get:function(){return readInt32BE.call(this.id,0)},set:function(e){writeUInt32BE.call(this.id,e,0)}})}catch(_){}defValue(ObjectId.prototype,"generate",{value:deprecate((function(e){return ObjectId.generate(e)}),"Please use the static `ObjectId.generate(time)` instead")}),defValue(ObjectId.prototype,"getInc",{value:deprecate((function(){return ObjectId.getInc()}),"Please use the static `ObjectId.getInc()` instead")}),defValue(ObjectId.prototype,"get_inc",{value:deprecate((function(){return ObjectId.getInc()}),"Please use the static `ObjectId.getInc()` instead")}),defValue(ObjectId,"get_inc",{value:deprecate((function(){return ObjectId.getInc()}),"Please use the static `ObjectId.getInc()` instead")}),defValue(ObjectId.prototype,"_bsontype",{value:"ObjectID"});export{ObjectId};