UNPKG

@tybys/oid

Version:

Use MongoDB ObjectID without installing bson.

2 lines (1 loc) 11.2 kB
"use strict";var t;function e(){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(){try{return Object.defineProperty(exports,"__esModule",{value:!0})}catch(_){return exports.__esModule=!0,exports}})(),"function"==typeof Symbol?t=Symbol:(t=function(t){return"Symbol("+t+")"})["for"]=function(t){return"Symbol("+t+")"};var r,n=e();if("function"==typeof n)try{r=n("buffer").Buffer}catch(R){}function i(t){return Number.isNaN?Number.isNaN(t):"number"==typeof t&&isNaN(t)}var o=Object.prototype.toString;function f(t){return Array.isArray?Array.isArray(t):"[object Array]"===o.call(t)}function a(t){return"undefined"!=typeof Uint8Array&&t instanceof Uint8Array||"[object Uint8Array]"===o.call(t)||f(t)}function u(t){if(r)return r.alloc(t);var e;if("undefined"!=typeof Uint8Array)return new Uint8Array(t);var n=[];for(e=0;e<t;e++)n[e]=0;return n}function s(t){var e,n;if(r)return r.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(n=0;n<t.byteLength;n++)o[n]=i[n];return o}if(f(t))return"function"==typeof Uint8Array.from?Uint8Array.from(t):new Uint8Array(t);if("string"==typeof t)return s(b(t))}if("string"==typeof t)return b(t);var a=[];for(n=0;n<t.length;n++)a[n]=t[n];return a}function c(t){if(r)return r.from(t,"hex");var e;!function(t,e,r){void 0===r&&(r=0);var n,o=t.length-r,f=e.length;o>f/2&&(o=f/2);for(n=0;n<o;++n){var a=parseInt(e.substring(2*n,2*n+2),16);if(i(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 n=0;n<e.length;n++)e[n]=parseInt(t.substring(2*n,2*n+2),16);return e}function h(t){if(void 0===t&&(t=0),null==r?void 0:r.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 n=this[t],i=this[t+3];if(n===undefined||i===undefined)throw new RangeError("Attempt to write outside buffer bounds");return n*Math.pow(2,24)+this[++t]*Math.pow(2,16)+this[++t]*Math.pow(2,8)+i}function d(t){if(void 0===t&&(t=0),null==r?void 0:r.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 n=this[t],i=this[t+3];if(n===undefined||i===undefined)throw new RangeError("Attempt to write outside buffer bounds");return(n<<24)+this[++t]*Math.pow(2,16)+this[++t]*Math.pow(2,8)+i}function l(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 y(t,e){return void 0===e&&(e=0),l(this,t,e,0,4294967295)}function p(t){if(!a(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 g(t){if(r)return r.from(this).toString(t);var e="",n=0;if("hex"===t){for(e="",n=0;n<this.length;n++){var i=this[n].toString(16);e+=1===i.length?"0"+i:i}return e}if("binary"===t||"latin1"===t){for(e="",n=0;n<this.length;n++)e+=String.fromCharCode(this[n]>>>0&255);return e}var o=void 0,f=void 0,a=void 0,u=void 0;o="";var s=this.length;for(n=0;n<s;)switch((f=this[n++])>>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[n++],o+=String.fromCharCode((31&f)<<6|63&a);break;case 14:a=this[n++],u=this[n++],o+=String.fromCharCode((15&f)<<12|(63&a)<<6|(63&u)<<0)}return o}function b(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 v=e(),m="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.",w=function(t){"undefined"!=typeof console&&console.warn(m);for(var e=u(t),r=0;r<t;++r)e[r]=Math.floor(256*Math.random());return e},A=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=v("crypto").randomBytes}catch(r){}return null!=e?e:w}();function _(t,e){if("function"==typeof v)return v("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 S(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 s(t);throw new TypeError("Must use either ArrayBuffer or TypedArray")}for(var x=function(){try{return Object.defineProperty({},"a",{value:0}),!0}catch(R){return!1}}(),B=x?function(t,e,r){return Object.defineProperty(t,e,r)}:function(t,e,r){return t[e]=r.value,t},I=A(5),j=/^[0-9a-fA-F]{24}$/,U=[],H=0;H<256;H++)U[H]=(H<=15?"0":"")+H.toString(16);for(var O=[],E=0;E<10;)O[48+E]=E++;for(;E<16;)O[55+E]=O[87+E]=E++;var T=t("id"),N=function(){function e(t){var r;if(!(this instanceof e))return new e(t);if(t instanceof e&&(this[T]=t.id,"__id"in t&&(this.__id=t.__id)),"object"==typeof t&&t&&"id"in t&&("toHexString"in t&&"function"==typeof t.toHexString?this[T]=c(t.toHexString()):this[T]="string"==typeof t.id?s(t.id):t.id),null!=t&&"number"!=typeof t||(this[T]=e.generate("number"==typeof t?t:undefined),e.cacheHexString&&(this.__id=g.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[T]=S(t)),a(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[T]=s(t)}if("string"==typeof t)if(12===t.length){var n=s(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[T]=n}else{if(24!==t.length||!j.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[T]=c(t)}x||(this.id=this[T],this.generationTime=d.call(this.id,0)),e.cacheHexString&&(this.__id=g.call(this.id,"hex"))}return e.prototype.toHexString=function(){if(e.cacheHexString&&this.__id)return this.__id;var t=g.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=u(12);return y.call(n,t,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},e.prototype.toString=function(t){return t?g.call(this.id,t):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&&a(this.id)?t===g.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?p.call(s(t),this.id):"object"==typeof t&&"toHexString"in t&&"function"==typeof t.toHexString&&t.toHexString()===this.toHexString())},e.prototype.getTimestamp=function(){var t=new Date,e=h.call(this.id,0);return t.setTime(1e3*Math.floor(e)),t},e.createPk=function(){return new e},e.createFromTime=function(t){var r=s([0,0,0,0,0,0,0,0,0,0,0,0]);return y.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(c(t))},e.isValid=function(t){return null!=t&&("number"==typeof t||("string"==typeof t?12===t.length||24===t.length&&j.test(t):t instanceof e||(!(!a(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&&j.test(g.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[t["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(N.prototype,"id",{configurable:!0,enumerable:!0,get:function(){return this[T]},set:function(t){this[T]=t,N.cacheHexString&&(this.__id=g.call(t,"hex"))}}),Object.defineProperty(N.prototype,"generationTime",{configurable:!0,enumerable:!0,get:function(){return d.call(this.id,0)},set:function(t){y.call(this.id,t,0)}})}catch(R){}B(N.prototype,"generate",{value:_((function(t){return N.generate(t)}),"Please use the static `ObjectId.generate(time)` instead")}),B(N.prototype,"getInc",{value:_((function(){return N.getInc()}),"Please use the static `ObjectId.getInc()` instead")}),B(N.prototype,"get_inc",{value:_((function(){return N.getInc()}),"Please use the static `ObjectId.getInc()` instead")}),B(N,"get_inc",{value:_((function(){return N.getInc()}),"Please use the static `ObjectId.getInc()` instead")}),B(N.prototype,"_bsontype",{value:"ObjectID"}),exports.ObjectId=N;