mirror of
https://github.com/LukeHagar/yotocol.git
synced 2025-12-06 04:22:12 +00:00
1 line
10 KiB
JavaScript
1 line
10 KiB
JavaScript
!function(t,e){"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?module.exports=e():t.Ndef=e()}(this,function(){var t=function(){throw"Dont construct NdefUtils!"};t.hasFlag=function(t,e){return(t&e)===e};var e=function(t,e,r,n,_){if("undefined"==typeof t||"undefined"==typeof e||"undefined"==typeof r||"undefined"==typeof n||"undefined"==typeof _)throw"Parameters missing from NdefRecord constructor call";if(null===t||null===e||null===r||null===_)throw"Passed null to non-nullable constructor parameter in NdefRecord";this.chunked=t,this.tnf=e,this.type=r,this.id=n,this.payload=_};e.HDR_FLAG_MESSAGE_BEGIN=128,e.HDR_FLAG_MESSAGE_END=64,e.HDR_FLAG_CHUNKED=32,e.HDR_FLAG_SHORT_RECORD=16,e.HDR_FLAG_ID_LENGTH_PRESENT=8,e.HDR_MASK_TNF=7,e.TNF_EMPTY=0,e.TNF_WELL_KNOWN=1,e.TNF_MEDIA=2,e.TNF_ABSOLUTE_URI=3,e.TNF_EXTERNAL=4,e.TNF_UNKNOWN=5,e.TNF_UNCHANGED=6,e.TNF_RESERVED=7,e.URI_PRE_NONE=0,e.URI_PRE_HTTP_WWW=1,e.URI_PRE_HTTPS_WWW=2,e.URI_PRE_HTTP=3,e.URI_PRE_HTTPS=4,e.URI_PRE_TEL=5,e.URI_PRE_MAILTO=6,e.URI_PRE_FTP_ANON=7,e.URI_PRE_FTP_FTP=8,e.URI_PRE_FTPS=9,e.URI_PRE_SFTP=10,e.URI_PRE_SMB=11,e.URI_PRE_NFS=12,e.URI_PRE_FTP=13,e.URI_PRE_DAV=14,e.URI_PRE_NEWS=15,e.URI_PRE_TELNET=16,e.URI_PRE_IMAP=17,e.URI_PRE_RTSP=18,e.URI_PRE_URN=19,e.URI_PRE_POP=20,e.URI_PRE_SIP=21,e.URI_PRE_SIPS=22,e.URI_PRE_TFTP=23,e.URI_PRE_BTSPP=24,e.URI_PRE_BTL2CAP=25,e.URI_PRE_BTGOEP=26,e.URI_PRE_TCPOBEX=27,e.URI_PRE_IRDAOBEX=28,e.URI_PRE_FILE=29,e.URI_PRE_URN_EPC_ID=30,e.URI_PRE_URN_EPC_TAG=31,e.URI_PRE_URN_EPC_PAT=32,e.URI_PRE_URN_EPC_RAW=33,e.URI_PRE_URN_EPC=34,e.URI_PRE_URN_NFC=35,e.prototype.isChunked=function(){return this.chunked},e.prototype.getTnf=function(){return this.tnf},e.prototype.getType=function(){return this.type},e.prototype.getId=function(){return this.id},e.prototype.getPayload=function(){return this.payload},e.prototype.toByteArray=function(t,r){var n=!0,_=!0;this.payload.length>=255&&(n=!1),"undefined"!=typeof this.id&&null!==this.id&&0!==this.id.length||(_=!1);var R=2;R+=this.type.length,R+=n?1:4,_&&(R+=1,R+=this.id.length),R+=this.payload.length;var a=new Uint8Array(R),i=this.tnf&e.HDR_MASK_TNF;n&&(i|=e.HDR_FLAG_SHORT_RECORD),_&&(i|=e.HDR_FLAG_ID_LENGTH_PRESENT),t&&(i|=e.HDR_FLAG_MESSAGE_BEGIN),r&&(i|=e.HDR_FLAG_MESSAGE_END),this.isChunked()&&(i|=e.HDR_FLAG_CHUNKED);var s=0;a[s++]=i,a[s++]=this.type.length,n?a[s++]=255&this.payload.length:(a[s++]=255&this.payload.length>>>24,a[s++]=255&this.payload.length>>>16,a[s++]=255&this.payload.length>>>8,a[s++]=255&this.payload.length>>>0),_&&(a[s++]=255&this.id.length);for(var o=0;o<this.type.length;o++)a[s++]=this.type[o];if(_)for(var P=0;P<this.id.length;P++)a[s++]=this.id[P];for(var E=0;E<this.payload.length;E++)a[s++]=this.payload[E];if(a.length!==s)throw"NDEF Record was not successfully generated";return a};var r=function(t){if("undefined"==typeof t||null===t||0===t.length)throw"You must supply a non-zero length array of NdefRecords to construct an NdefMessage";this.ndefRecords=t};r.prototype.getRecords=function(){return this.ndefRecords},r.prototype.toByteArray=function(){for(var t=new Uint8Array(0),e=0;e<this.ndefRecords.length;e++){var r=this.ndefRecords[e].toByteArray(0===e,e===this.ndefRecords.length-1),n=new Uint8Array(t.length+r.length);n.set(t),n.set(r,t.length),t=n}return t},r.fromBytes=function(n){if("undefined"==typeof n||null===n)throw"Bytes must be defined and non-null";if(n=new Uint8Array(n),n.length<3)throw"Byte array is too short to contain any kind of NDEF message";for(var _=function(t,e){if(t.length<e+1)throw"Message ended abruptly, trying to access index "+e+" from an array of "+t.length+" items."},R=[],a=!1,i=!1,s=0;!a;){if(s>=n.length)throw"Ran out of bytes before message started";var o=n[s];if(i=i||t.hasFlag(o,e.HDR_FLAG_MESSAGE_BEGIN)){a=t.hasFlag(o,e.HDR_FLAG_MESSAGE_END);var P=t.hasFlag(o,e.HDR_FLAG_CHUNKED),E=t.hasFlag(o,e.HDR_FLAG_SHORT_RECORD),f=t.hasFlag(o,e.HDR_FLAG_ID_LENGTH_PRESENT),u=o&e.HDR_MASK_TNF,U=s+1;_(n,U);var h=n[U],p=0,c=E?1:4,I=U+1;if(E)_(n,I),p=n[I];else{_(n,I+3);var l=new Uint32Array(n.slice(I,F+4));p=l[0]}var T=0,d=I+c;_(n,d),f&&(T=n[d]);var N=d+(f?1:0),y=N+h;_(n,y);var g=new Uint8Array(n.buffer,N,h),A=y;_(n,A+T);var W=new Uint8Array(n.buffer,A,T),F=A+T;_(n,F+p-1);var S=new Uint8Array(n.buffer,F,p);s=F+p,R.push(new e(P,u,g,W,S))}else s++}return new r(R)};var n=function(){throw"Do not instantiate ndef record utils"};n.createUriRecord=function(t){var r=n.resolveUriToPrefix(t),_=r.prefixCode,R=r.content,a=n.stringToUint8Array(R),i=new Uint8Array(a.length+1);return i[0]=_,i.set(a,1),new e(!1,e.TNF_WELL_KNOWN,new Uint8Array([85]),null,i)},n.createTextRecord=function(t,r){var _=n.stringToUint8Array(t);"undefined"==typeof r&&(r="en");var R=n.stringToUint8Array(r),a=new Uint8Array(_.length+R.length+1);return a[0]=R.length,a.set(R,1),a.set(_,1+R.length),new e(!1,e.TNF_WELL_KNOWN,new Uint8Array([84]),null,a)},n.stringToUint8Array=function(t){var e=encodeURIComponent(t),r=e.replace(/%([0-9A-F]{2})/g,function(t,e){return String.fromCharCode("0x"+e)}),n=new Uint8Array(r.length);return Array.prototype.forEach.call(r,function(t,e){n[e]=t.charCodeAt(0)}),n},n.uint8ArrayToString=function(t){var e=Array.prototype.map.call(t,function(t){return String.fromCharCode(t)}).join(""),r=e.replace(/(.)/g,function(t,e){var r=e.charCodeAt(0).toString(16).toUpperCase();return r.length<2&&(r="0"+r),"%"+r});return decodeURIComponent(r)},n.resolveUriString=function(t){return n.resolveUriToPrefix(t)},n.resolveUriToPrefix=function(t){var r=t,n=function(t,e,r){return{prefixCode:e,prefix:t,content:r.slice(t.length),fullUri:r}};if(r.startsWith("http")){if(r.startsWith("https://www."))return n("https://www.",e.URI_PRE_HTTPS_WWW,r);if(r.startsWith("https://"))return n("https://",e.URI_PRE_HTTPS,r);if(r.startsWith("http://www."))return n("http://www.",e.URI_PRE_HTTP_WWW,r);if(r.startsWith("http://"))return n("http://",e.URI_PRE_HTTP,r)}else if(r.startsWith("ftp")){if(r.startsWith("ftp://ftp."))return n("ftp://ftp.",e.URI_PRE_FTP_FTP,r);if(r.startsWith("ftps://"))return n("ftps://",e.URI_PRE_FTPS,r);if(r.startsWith("ftp://anonymous:anonymous@"))return n("ftp://anonymous:anonymous@",e.URI_PRE_FTP_ANON,r);if(r.startsWith("ftp://"))return n("ftp://",e.URI_PRE_FTP,r)}else{if(r.startsWith("mailto:"))return n("mailto:",e.URI_PRE_MAILTO,r);if(r.startsWith("tel:"))return n("tel:",e.URI_PRE_TEL,r);if(r.startsWith("sftp://"))return n("sftp://",e.URI_PRE_SFTP,r);if(r.startsWith("smb://"))return n("smb://",e.URI_PRE_SMB,r);if(r.startsWith("nfs://"))return n("nfs://",e.URI_PRE_NFS,r);if(r.startsWith("dav://"))return n("dav://",e.URI_PRE_DAV,r);if(r.startsWith("news:"))return n("news:",e.URI_PRE_NEWS,r);if(r.startsWith("telnet://"))return n("telnet://",e.URI_PRE_TELNET,r);if(r.startsWith("imap:"))return n("imap:",e.URI_PRE_IMAP,r);if(r.startsWith("rtsp://"))return n("rtsp://",e.URI_PRE_RTSP,r);if(r.startsWith("pop:"))return n("pop:",e.URI_PRE_POP,r);if(r.startsWith("sip:"))return n("sip:",e.URI_PRE_SIP,r);if(r.startsWith("sips:"))return n("sips:",e.URI_PRE_SIPS,r);if(r.startsWith("tftp:"))return n("tftp:",e.URI_PRE_TFTP,r);if(r.startsWith("btspp://"))return n("btspp://",e.URI_PRE_BTSPP,r);if(r.startsWith("btl2cap://"))return n("btl2cap://",e.URI_PRE_BTL2CAP,r);if(r.startsWith("btgoep://"))return n("btgoep://",e.URI_PRE_BTGOEP,r);if(r.startsWith("tcpobex://"))return n("tcpobex://",e.URI_PRE_TCPOBEX,r);if(r.startsWith("irdaobex://"))return n("irdaobex://",e.URI_PRE_IRDAOBEX,r);if(r.startsWith("file://"))return n("file://",e.URI_PRE_FILE,r);if(r.startsWith("urn:epc:id:"))return n("urn:epc:id:",e.URI_PRE_URN_EPC_ID,r);if(r.startsWith("urn:epc:tag:"))return n("urn:epc:tag:",e.URI_PRE_URN_EPC_TAG,r);if(r.startsWith("urn:epc:pat:"))return n("urn:epc:pat:",e.URI_PRE_URN_EPC_PAT,r);if(r.startsWith("urn:epc:raw:"))return n("urn:epc:raw:",e.URI_PRE_EPC_RAW,r);if(r.startsWith("urn:epc:"))return n("urn:epc:",e.URI_PRE_URN_EPC,r);if(r.startsWith("urn:nfc:"))return n("urn:nfc:",e.URI_PRE_URN_NFC,r);if(r.startsWith("urn:"))return n("urn:",e.URI_PRE_URN,r)}return n("",e.URI_PRE_NONE,t)},n.resolveUriRecordToString=function(t){return n.resolveUrlFromPrefix(t)},n.resolveUrlFromPrefix=function(t){if(t.getTnf()!==e.TNF_WELL_KNOWN||85!==t.getType()[0])throw"Not a WELL_KNOWN URI record";var r=t.getPayload(),_=r[0],R=r.slice(1),a="";switch(_){case e.URI_PRE_NONE:break;case e.URI_PRE_HTTP_WWW:a="http://www.";break;case e.URI_PRE_HTTPS_WWW:a="https://www.";break;case e.URI_PRE_HTTP:a="http://";break;case e.URI_PRE_HTTPS:a="https://";break;case e.URI_PRE_TEL:a="tel:";break;case e.URI_PRE_MAILTO:a="mailto:";break;case e.URI_PRE_FTP_ANON:a="ftp://anonymous:anonymous@";break;case e.URI_PRE_FTP_FTP:a="ftp://ftp.";break;case e.URI_PRE_FTPS:a="ftps://";break;case e.URI_PRE_SFTP:a="sftp://";break;case e.URI_PRE_SMB:a="smb://";break;case e.URI_PRE_NFS:a="nfs://";break;case e.URI_PRE_FTP:a="ftp://";break;case e.URI_PRE_DAV:a="dav://";break;case e.URI_PRE_NEWS:a="news:";break;case e.URI_PRE_TELNET:a="telnet://";break;case e.URI_PRE_IMAP:a="imap:";break;case e.URI_PRE_RTSP:a="rtsp://";break;case e.URI_PRE_URN:a="urn:";break;case e.URI_PRE_POP:a="pop:";break;case e.URI_PRE_SIP:a="sip:";break;case e.URI_PRE_SIPS:a="sips:";break;case e.URI_PRE_TFTP:a="tftp:";break;case e.URI_PRE_BTSPP:a="btspp://";break;case e.URI_PRE_BTL2CAP:a="btl2cap://";break;case e.URI_PRE_BTGOEP:a="btgoep://";break;case e.URI_PRE_TCPOBEX:a="tcpobex://";break;case e.URI_PRE_IRDAOBEX:a="irdaobex://";break;case e.URI_PRE_FILE:a="file://";break;case e.URI_PRE_URN_EPC_ID:a="urn:epc:id:";break;case e.URI_PRE_URN_EPC_TAG:a="urn:epc:tag:";break;case e.URI_PRE_URN_EPC_PAT:a="urn:epc:pat:";break;case e.URI_PRE_URN_EPC_RAW:a="urn:epc:raw:";break;case e.URI_PRE_URN_EPC:a="urn:epc:";break;case e.URI_PRE_URN_NFC:a="urn:nfc:";break;default:throw"Invalid URI code"}return a+n.uint8ArrayToString(R)},n.resolveTextRecord=function(t){if(t.getTnf()!==e.TNF_WELL_KNOWN||84!==t.getType()[0])throw"Not a WELL_KNOWN text record";var r=t.getPayload();if(0===r.length)throw"Missing error code";var _=63&r[0];if(r.length<_+1)throw"Payload too short to contain language code";var R=n.uint8ArrayToString(r.slice(1,1+_)),a="";return r.length>1+_&&(a=n.uint8ArrayToString(r.slice(1+_))),{language:R,content:a}};var _={};return _.Message=r,_.Record=e,_.Utils=n,_}); |