@tybys/oid
Version:
Use MongoDB ObjectID without installing bson.
2 lines (1 loc) • 11.4 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).oid={})}(this,(function(t){"use strict";var e;function r(){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?e=Symbol:(e=function(t){return"Symbol("+t+")"})["for"]=function(t){return"Symbol("+t+")"};var n,i=r();if("function"==typeof i)try{n=i("buffer").Buffer}catch(k){}function o(t){return Number.isNaN?Number.isNaN(t):"number"==typeof t&&isNaN(t)}var f=Object.prototype.toString;function a(t){return Array.isArray?Array.isArray(t):"[object Array]"===f.call(t)}function u(t){return"undefined"!=typeof Uint8Array&&t instanceof Uint8Array||"[object Uint8Array]"===f.call(t)||a(t)}function s(t){if(n)return n.alloc(t);var e;if("undefined"!=typeof Uint8Array)return new Uint8Array(t);var r=[];for(e=0;e<t;e++)r[e]=0;return r}function c(t){var e,r;if(n)return n.from(t);if("function"==typeof ArrayBuffer){if(t instanceof ArrayBuffer)return new Uint8Array(t);if((null===(e=ArrayBuffer.isView)||void 0===e?void 0:e.call(ArrayBuffer,t))||t instanceof Uint8Array){if("function"==typeof Uint8Array.from)return Uint8Array.from(t);var i=new Uint8Array(t.buffer,t.byteOffset,t.byteLength),o=new Uint8Array(t.byteLength);for(r=0;r<t.byteLength;r++)o[r]=i[r];return o}if(a(t))return"function"==typeof Uint8Array.from?Uint8Array.from(t):new Uint8Array(t);if("string"==typeof t)return c(v(t))}if("string"==typeof t)return v(t);var f=[];for(r=0;r<t.length;r++)f[r]=t[r];return f}function h(t){if(n)return n.from(t,"hex");var e;!function(t,e,r){void 0===r&&(r=0);var n,i=t.length-r,f=e.length;i>f/2&&(i=f/2);for(n=0;n<i;++n){var a=parseInt(e.substring(2*n,2*n+2),16);if(o(a))return n;t[r+n]=a}}(e="function"==typeof Uint8Array?new Uint8Array(Math.floor(t.length/2)):new Array(Math.floor(t.length/2)),t);for(var r=0;r<e.length;r++)e[r]=parseInt(t.substring(2*r,2*r+2),16);return e}function d(t){if(void 0===t&&(t=0),null==n?void 0:n.isBuffer(this))return this.readUInt32BE(t);var e=typeof t;if("number"!==e)throw new TypeError("The offset argument must be of type number. Received type "+e);var r=this[t],i=this[t+3];if(r===undefined||i===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)+i}function l(t){if(void 0===t&&(t=0),null==n?void 0:n.isBuffer(this))return this.readInt32BE(t);var e=typeof t;if("number"!==e)throw new TypeError("The offset argument must be of type number. Received type "+e);var r=this[t],i=this[t+3];if(r===undefined||i===undefined)throw new RangeError("Attempt to write outside buffer bounds");return(r<<24)+this[++t]*Math.pow(2,16)+this[++t]*Math.pow(2,8)+i}function y(t,e,r,n,i){return function(t,e,r,n,i,o){if(t>r||t<e){var f="bigint"==typeof e?"n":"",a=void 0;throw a=o>3?0===e||"function"==typeof BigInt&&BigInt(e)===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:">= "+e+f+" and <= "+r+f,new RangeError("value"+a)}if(n[i]===undefined||n[i+o]===undefined)throw new RangeError("Attempt to write outside buffer bounds")}(e=+e,n,i,t,r,3),t[r+3]=255&e,e>>>=8,t[r+2]=255&e,e>>>=8,t[r+1]=255&e,e>>>=8,t[r]=255&e,r+4}function p(t,e){return void 0===e&&(e=0),y(this,t,e,0,4294967295)}function g(t){if(!u(t))throw new TypeError('The "target" argument must be one of type Buffer or Uint8Array. Received type '+typeof t);if(this===t)return!0;if(this.length!==t.length)return!1;for(var e=0;e<this.length;e++)if(this[e]!==t[e])return!1;return!0}function b(t){if(n)return n.from(this).toString(t);var e="",r=0;if("hex"===t){for(e="",r=0;r<this.length;r++){var i=this[r].toString(16);e+=1===i.length?"0"+i:i}return e}if("binary"===t||"latin1"===t){for(e="",r=0;r<this.length;r++)e+=String.fromCharCode(this[r]>>>0&255);return e}var o=void 0,f=void 0,a=void 0,u=void 0;o="";var s=this.length;for(r=0;r<s;)switch((f=this[r++])>>4){case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:o+=String.fromCharCode(f);break;case 12:case 13:a=this[r++],o+=String.fromCharCode((31&f)<<6|63&a);break;case 14:a=this[r++],u=this[r++],o+=String.fromCharCode((15&f)<<12|(63&a)<<6|(63&u)<<0)}return o}function v(t,e){var r;void 0===e&&(e=Infinity);for(var n=t.length,i=null,o=[],f=0;f<n;++f){if((r=t.charCodeAt(f))>55295&&r<57344){if(!i){if(r>56319){(e-=3)>-1&&o.push(239,191,189);continue}if(f+1===n){(e-=3)>-1&&o.push(239,191,189);continue}i=r;continue}if(r<56320){(e-=3)>-1&&o.push(239,191,189),i=r;continue}r=65536+(i-55296<<10|r-56320)}else i&&(e-=3)>-1&&o.push(239,191,189);if(i=null,r<128){if((e-=1)<0)break;o.push(r)}else if(r<2048){if((e-=2)<0)break;o.push(r>>6|192,63&r|128)}else if(r<65536){if((e-=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((e-=4)<0)break;o.push(r>>18|240,r>>12&63|128,r>>6&63|128,63&r|128)}}return o}var m=r(),w="object"==typeof navigator&&"ReactNative"===navigator.product?"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.",A=function(t){"undefined"!=typeof console&&console.warn(w);for(var e=s(t),r=0;r<t;++r)e[r]=Math.floor(256*Math.random());return e},_=function(){if("undefined"!=typeof window){var t=window.crypto||window.msCrypto;if(null==t?void 0:t.getRandomValues)return function(e){return t.getRandomValues(new Uint8Array(e))}}if("undefined"!=typeof crypto&&crypto.getRandomValues)return function(t){return crypto.getRandomValues(new Uint8Array(t))};var e;try{e=m("crypto").randomBytes}catch(r){}return null!=e?e:A}();function S(t,e){if("function"==typeof m)return m("util").deprecate(t,e);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(e),r=!0),t.apply(this,n)}}function x(t){var e,r;if("function"==typeof ArrayBuffer&&"function"==typeof Uint8Array&&((null===(e=ArrayBuffer.isView)||void 0===e?void 0:e.call(ArrayBuffer,t))||t instanceof Uint8Array))return new Uint8Array(t.buffer,t.byteOffset,t.byteLength);if(r=t,-1!==["[object ArrayBuffer]","[object SharedArrayBuffer]"].indexOf(Object.prototype.toString.call(r)))return c(t);throw new TypeError("Must use either ArrayBuffer or TypedArray")}for(var B=function(){try{return Object.defineProperty({},"a",{value:0}),!0}catch(k){return!1}}(),j=B?function(t,e,r){return Object.defineProperty(t,e,r)}:function(t,e,r){return t[e]=r.value,t},I=_(5),U=/^[0-9a-fA-F]{24}$/,H=[],O=0;O<256;O++)H[O]=(O<=15?"0":"")+O.toString(16);for(var T=[],E=0;E<10;)T[48+E]=E++;for(;E<16;)T[55+E]=T[87+E]=E++;var N=e("id"),R=function(){function t(e){var r;if(!(this instanceof t))return new t(e);if(e instanceof t&&(this[N]=e.id,"__id"in e&&(this.__id=e.__id)),"object"==typeof e&&e&&"id"in e&&("toHexString"in e&&"function"==typeof e.toHexString?this[N]=h(e.toHexString()):this[N]="string"==typeof e.id?c(e.id):e.id),null!=e&&"number"!=typeof e||(this[N]=t.generate("number"==typeof e?e:undefined),t.cacheHexString&&(this.__id=b.call(this.id,"hex"))),"function"==typeof ArrayBuffer&&(((null===(r=ArrayBuffer.isView)||void 0===r?void 0:r.call(ArrayBuffer,e))||e instanceof Uint8Array)&&12===e.byteLength)&&(this[N]=x(e)),u(e)){if(12!==e.length)throw new TypeError("Argument passed in must be a Buffer or string of 12 bytes or a string of 24 hex characters");this[N]=c(e)}if("string"==typeof e)if(12===e.length){var n=c(e);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[N]=n}else{if(24!==e.length||!U.test(e))throw new TypeError("Argument passed in must be a Buffer or string of 12 bytes or a string of 24 hex characters");this[N]=h(e)}B||(this.id=this[N],this.generationTime=l.call(this.id,0)),t.cacheHexString&&(this.__id=b.call(this.id,"hex"))}return t.prototype.toHexString=function(){if(t.cacheHexString&&this.__id)return this.__id;var e=b.call(this.id,"hex");return t.cacheHexString&&!this.__id&&(this.__id=e),e},t.getInc=function(){return t.index=(t.index+1)%16777215},t.generate=function(e){"number"!=typeof e&&(e=~~(Date.now()/1e3));var r=t.getInc(),n=s(12);return p.call(n,e,0),n[4]=I[0],n[5]=I[1],n[6]=I[2],n[7]=I[3],n[8]=I[4],n[11]=255&r,n[10]=r>>8&255,n[9]=r>>16&255,n},t.prototype.toString=function(t){return t?b.call(this.id,t):this.toHexString()},t.prototype.toJSON=function(){return this.toHexString()},t.prototype.equals=function(e){return e!==undefined&&null!==e&&(e instanceof t?this.toString()===e.toString():"string"==typeof e&&t.isValid(e)&&12===e.length&&u(this.id)?e===b.call(this.id,"latin1"):"string"==typeof e&&t.isValid(e)&&24===e.length?e.toLowerCase()===this.toHexString():"string"==typeof e&&t.isValid(e)&&12===e.length?g.call(c(e),this.id):"object"==typeof e&&"toHexString"in e&&"function"==typeof e.toHexString&&e.toHexString()===this.toHexString())},t.prototype.getTimestamp=function(){var t=new Date,e=d.call(this.id,0);return t.setTime(1e3*Math.floor(e)),t},t.createPk=function(){return new t},t.createFromTime=function(e){var r=c([0,0,0,0,0,0,0,0,0,0,0,0]);return p.call(r,e,0),new t(r)},t.createFromHexString=function(e){if(void 0===e||null!=e&&24!==e.length)throw new TypeError("Argument passed in must be a single String of 12 bytes or a string of 24 hex characters");return new t(h(e))},t.isValid=function(e){return null!=e&&("number"==typeof e||("string"==typeof e?12===e.length||24===e.length&&U.test(e):e instanceof t||(!(!u(e)||12!==e.length)||"object"==typeof e&&"toHexString"in e&&"function"==typeof e.toHexString&&("string"==typeof e.id?12===e.id.length:24===e.toHexString().length&&U.test(b.call(e.id,"hex"))))))},t.prototype.toExtendedJSON=function(){return this.toHexString?{$oid:this.toHexString()}:{$oid:this.toString("hex")}},t.fromExtendedJSON=function(e){return new t(e.$oid)},t.prototype[e["for"]("nodejs.util.inspect.custom")]=function(){return this.inspect()},t.prototype.inspect=function(){return'new ObjectId("'+this.toHexString()+'")'},t.index=~~(16777215*Math.random()),t}();try{Object.defineProperty(R.prototype,"id",{configurable:!0,enumerable:!0,get:function(){return this[N]},set:function(t){this[N]=t,R.cacheHexString&&(this.__id=b.call(t,"hex"))}}),Object.defineProperty(R.prototype,"generationTime",{configurable:!0,enumerable:!0,get:function(){return l.call(this.id,0)},set:function(t){p.call(this.id,t,0)}})}catch(k){}j(R.prototype,"generate",{value:S((function(t){return R.generate(t)}),"Please use the static `ObjectId.generate(time)` instead")}),j(R.prototype,"getInc",{value:S((function(){return R.getInc()}),"Please use the static `ObjectId.getInc()` instead")}),j(R.prototype,"get_inc",{value:S((function(){return R.getInc()}),"Please use the static `ObjectId.getInc()` instead")}),j(R,"get_inc",{value:S((function(){return R.getInc()}),"Please use the static `ObjectId.getInc()` instead")}),j(R.prototype,"_bsontype",{value:"ObjectID"}),t.ObjectId=R,(function(){try{return Object.defineProperty(t,"__esModule",{value:!0})}catch(_){return t.__esModule=!0,t}})()}));