sync.js 140 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311
  1. var csv_parse_sync = (function (exports) {
  2. 'use strict';
  3. var global$1 = (typeof global !== "undefined" ? global :
  4. typeof self !== "undefined" ? self :
  5. typeof window !== "undefined" ? window : {});
  6. var lookup = [];
  7. var revLookup = [];
  8. var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array;
  9. var inited = false;
  10. function init () {
  11. inited = true;
  12. var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  13. for (var i = 0, len = code.length; i < len; ++i) {
  14. lookup[i] = code[i];
  15. revLookup[code.charCodeAt(i)] = i;
  16. }
  17. revLookup['-'.charCodeAt(0)] = 62;
  18. revLookup['_'.charCodeAt(0)] = 63;
  19. }
  20. function toByteArray (b64) {
  21. if (!inited) {
  22. init();
  23. }
  24. var i, j, l, tmp, placeHolders, arr;
  25. var len = b64.length;
  26. if (len % 4 > 0) {
  27. throw new Error('Invalid string. Length must be a multiple of 4')
  28. }
  29. // the number of equal signs (place holders)
  30. // if there are two placeholders, than the two characters before it
  31. // represent one byte
  32. // if there is only one, then the three characters before it represent 2 bytes
  33. // this is just a cheap hack to not do indexOf twice
  34. placeHolders = b64[len - 2] === '=' ? 2 : b64[len - 1] === '=' ? 1 : 0;
  35. // base64 is 4/3 + up to two characters of the original data
  36. arr = new Arr(len * 3 / 4 - placeHolders);
  37. // if there are placeholders, only get up to the last complete 4 chars
  38. l = placeHolders > 0 ? len - 4 : len;
  39. var L = 0;
  40. for (i = 0, j = 0; i < l; i += 4, j += 3) {
  41. tmp = (revLookup[b64.charCodeAt(i)] << 18) | (revLookup[b64.charCodeAt(i + 1)] << 12) | (revLookup[b64.charCodeAt(i + 2)] << 6) | revLookup[b64.charCodeAt(i + 3)];
  42. arr[L++] = (tmp >> 16) & 0xFF;
  43. arr[L++] = (tmp >> 8) & 0xFF;
  44. arr[L++] = tmp & 0xFF;
  45. }
  46. if (placeHolders === 2) {
  47. tmp = (revLookup[b64.charCodeAt(i)] << 2) | (revLookup[b64.charCodeAt(i + 1)] >> 4);
  48. arr[L++] = tmp & 0xFF;
  49. } else if (placeHolders === 1) {
  50. tmp = (revLookup[b64.charCodeAt(i)] << 10) | (revLookup[b64.charCodeAt(i + 1)] << 4) | (revLookup[b64.charCodeAt(i + 2)] >> 2);
  51. arr[L++] = (tmp >> 8) & 0xFF;
  52. arr[L++] = tmp & 0xFF;
  53. }
  54. return arr
  55. }
  56. function tripletToBase64 (num) {
  57. return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F]
  58. }
  59. function encodeChunk (uint8, start, end) {
  60. var tmp;
  61. var output = [];
  62. for (var i = start; i < end; i += 3) {
  63. tmp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]);
  64. output.push(tripletToBase64(tmp));
  65. }
  66. return output.join('')
  67. }
  68. function fromByteArray (uint8) {
  69. if (!inited) {
  70. init();
  71. }
  72. var tmp;
  73. var len = uint8.length;
  74. var extraBytes = len % 3; // if we have 1 byte left, pad 2 bytes
  75. var output = '';
  76. var parts = [];
  77. var maxChunkLength = 16383; // must be multiple of 3
  78. // go through the array every three bytes, we'll deal with trailing stuff later
  79. for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
  80. parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength)));
  81. }
  82. // pad the end with zeros, but make sure to not forget the extra bytes
  83. if (extraBytes === 1) {
  84. tmp = uint8[len - 1];
  85. output += lookup[tmp >> 2];
  86. output += lookup[(tmp << 4) & 0x3F];
  87. output += '==';
  88. } else if (extraBytes === 2) {
  89. tmp = (uint8[len - 2] << 8) + (uint8[len - 1]);
  90. output += lookup[tmp >> 10];
  91. output += lookup[(tmp >> 4) & 0x3F];
  92. output += lookup[(tmp << 2) & 0x3F];
  93. output += '=';
  94. }
  95. parts.push(output);
  96. return parts.join('')
  97. }
  98. function read (buffer, offset, isLE, mLen, nBytes) {
  99. var e, m;
  100. var eLen = nBytes * 8 - mLen - 1;
  101. var eMax = (1 << eLen) - 1;
  102. var eBias = eMax >> 1;
  103. var nBits = -7;
  104. var i = isLE ? (nBytes - 1) : 0;
  105. var d = isLE ? -1 : 1;
  106. var s = buffer[offset + i];
  107. i += d;
  108. e = s & ((1 << (-nBits)) - 1);
  109. s >>= (-nBits);
  110. nBits += eLen;
  111. for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8) {}
  112. m = e & ((1 << (-nBits)) - 1);
  113. e >>= (-nBits);
  114. nBits += mLen;
  115. for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8) {}
  116. if (e === 0) {
  117. e = 1 - eBias;
  118. } else if (e === eMax) {
  119. return m ? NaN : ((s ? -1 : 1) * Infinity)
  120. } else {
  121. m = m + Math.pow(2, mLen);
  122. e = e - eBias;
  123. }
  124. return (s ? -1 : 1) * m * Math.pow(2, e - mLen)
  125. }
  126. function write (buffer, value, offset, isLE, mLen, nBytes) {
  127. var e, m, c;
  128. var eLen = nBytes * 8 - mLen - 1;
  129. var eMax = (1 << eLen) - 1;
  130. var eBias = eMax >> 1;
  131. var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0);
  132. var i = isLE ? 0 : (nBytes - 1);
  133. var d = isLE ? 1 : -1;
  134. var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0;
  135. value = Math.abs(value);
  136. if (isNaN(value) || value === Infinity) {
  137. m = isNaN(value) ? 1 : 0;
  138. e = eMax;
  139. } else {
  140. e = Math.floor(Math.log(value) / Math.LN2);
  141. if (value * (c = Math.pow(2, -e)) < 1) {
  142. e--;
  143. c *= 2;
  144. }
  145. if (e + eBias >= 1) {
  146. value += rt / c;
  147. } else {
  148. value += rt * Math.pow(2, 1 - eBias);
  149. }
  150. if (value * c >= 2) {
  151. e++;
  152. c /= 2;
  153. }
  154. if (e + eBias >= eMax) {
  155. m = 0;
  156. e = eMax;
  157. } else if (e + eBias >= 1) {
  158. m = (value * c - 1) * Math.pow(2, mLen);
  159. e = e + eBias;
  160. } else {
  161. m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen);
  162. e = 0;
  163. }
  164. }
  165. for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {}
  166. e = (e << mLen) | m;
  167. eLen += mLen;
  168. for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {}
  169. buffer[offset + i - d] |= s * 128;
  170. }
  171. var toString = {}.toString;
  172. var isArray = Array.isArray || function (arr) {
  173. return toString.call(arr) == '[object Array]';
  174. };
  175. var INSPECT_MAX_BYTES = 50;
  176. /**
  177. * If `Buffer.TYPED_ARRAY_SUPPORT`:
  178. * === true Use Uint8Array implementation (fastest)
  179. * === false Use Object implementation (most compatible, even IE6)
  180. *
  181. * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+,
  182. * Opera 11.6+, iOS 4.2+.
  183. *
  184. * Due to various browser bugs, sometimes the Object implementation will be used even
  185. * when the browser supports typed arrays.
  186. *
  187. * Note:
  188. *
  189. * - Firefox 4-29 lacks support for adding new properties to `Uint8Array` instances,
  190. * See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438.
  191. *
  192. * - Chrome 9-10 is missing the `TypedArray.prototype.subarray` function.
  193. *
  194. * - IE10 has a broken `TypedArray.prototype.subarray` function which returns arrays of
  195. * incorrect length in some situations.
  196. * We detect these buggy browsers and set `Buffer.TYPED_ARRAY_SUPPORT` to `false` so they
  197. * get the Object implementation, which is slower but behaves correctly.
  198. */
  199. Buffer.TYPED_ARRAY_SUPPORT = global$1.TYPED_ARRAY_SUPPORT !== undefined
  200. ? global$1.TYPED_ARRAY_SUPPORT
  201. : true;
  202. /*
  203. * Export kMaxLength after typed array support is determined.
  204. */
  205. kMaxLength();
  206. function kMaxLength () {
  207. return Buffer.TYPED_ARRAY_SUPPORT
  208. ? 0x7fffffff
  209. : 0x3fffffff
  210. }
  211. function createBuffer (that, length) {
  212. if (kMaxLength() < length) {
  213. throw new RangeError('Invalid typed array length')
  214. }
  215. if (Buffer.TYPED_ARRAY_SUPPORT) {
  216. // Return an augmented `Uint8Array` instance, for best performance
  217. that = new Uint8Array(length);
  218. that.__proto__ = Buffer.prototype;
  219. } else {
  220. // Fallback: Return an object instance of the Buffer class
  221. if (that === null) {
  222. that = new Buffer(length);
  223. }
  224. that.length = length;
  225. }
  226. return that
  227. }
  228. /**
  229. * The Buffer constructor returns instances of `Uint8Array` that have their
  230. * prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of
  231. * `Uint8Array`, so the returned instances will have all the node `Buffer` methods
  232. * and the `Uint8Array` methods. Square bracket notation works as expected -- it
  233. * returns a single octet.
  234. *
  235. * The `Uint8Array` prototype remains unmodified.
  236. */
  237. function Buffer (arg, encodingOrOffset, length) {
  238. if (!Buffer.TYPED_ARRAY_SUPPORT && !(this instanceof Buffer)) {
  239. return new Buffer(arg, encodingOrOffset, length)
  240. }
  241. // Common case.
  242. if (typeof arg === 'number') {
  243. if (typeof encodingOrOffset === 'string') {
  244. throw new Error(
  245. 'If encoding is specified then the first argument must be a string'
  246. )
  247. }
  248. return allocUnsafe(this, arg)
  249. }
  250. return from(this, arg, encodingOrOffset, length)
  251. }
  252. Buffer.poolSize = 8192; // not used by this implementation
  253. // TODO: Legacy, not needed anymore. Remove in next major version.
  254. Buffer._augment = function (arr) {
  255. arr.__proto__ = Buffer.prototype;
  256. return arr
  257. };
  258. function from (that, value, encodingOrOffset, length) {
  259. if (typeof value === 'number') {
  260. throw new TypeError('"value" argument must not be a number')
  261. }
  262. if (typeof ArrayBuffer !== 'undefined' && value instanceof ArrayBuffer) {
  263. return fromArrayBuffer(that, value, encodingOrOffset, length)
  264. }
  265. if (typeof value === 'string') {
  266. return fromString(that, value, encodingOrOffset)
  267. }
  268. return fromObject(that, value)
  269. }
  270. /**
  271. * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError
  272. * if value is a number.
  273. * Buffer.from(str[, encoding])
  274. * Buffer.from(array)
  275. * Buffer.from(buffer)
  276. * Buffer.from(arrayBuffer[, byteOffset[, length]])
  277. **/
  278. Buffer.from = function (value, encodingOrOffset, length) {
  279. return from(null, value, encodingOrOffset, length)
  280. };
  281. if (Buffer.TYPED_ARRAY_SUPPORT) {
  282. Buffer.prototype.__proto__ = Uint8Array.prototype;
  283. Buffer.__proto__ = Uint8Array;
  284. if (typeof Symbol !== 'undefined' && Symbol.species &&
  285. Buffer[Symbol.species] === Buffer) ;
  286. }
  287. function assertSize (size) {
  288. if (typeof size !== 'number') {
  289. throw new TypeError('"size" argument must be a number')
  290. } else if (size < 0) {
  291. throw new RangeError('"size" argument must not be negative')
  292. }
  293. }
  294. function alloc (that, size, fill, encoding) {
  295. assertSize(size);
  296. if (size <= 0) {
  297. return createBuffer(that, size)
  298. }
  299. if (fill !== undefined) {
  300. // Only pay attention to encoding if it's a string. This
  301. // prevents accidentally sending in a number that would
  302. // be interpretted as a start offset.
  303. return typeof encoding === 'string'
  304. ? createBuffer(that, size).fill(fill, encoding)
  305. : createBuffer(that, size).fill(fill)
  306. }
  307. return createBuffer(that, size)
  308. }
  309. /**
  310. * Creates a new filled Buffer instance.
  311. * alloc(size[, fill[, encoding]])
  312. **/
  313. Buffer.alloc = function (size, fill, encoding) {
  314. return alloc(null, size, fill, encoding)
  315. };
  316. function allocUnsafe (that, size) {
  317. assertSize(size);
  318. that = createBuffer(that, size < 0 ? 0 : checked(size) | 0);
  319. if (!Buffer.TYPED_ARRAY_SUPPORT) {
  320. for (var i = 0; i < size; ++i) {
  321. that[i] = 0;
  322. }
  323. }
  324. return that
  325. }
  326. /**
  327. * Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance.
  328. * */
  329. Buffer.allocUnsafe = function (size) {
  330. return allocUnsafe(null, size)
  331. };
  332. /**
  333. * Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance.
  334. */
  335. Buffer.allocUnsafeSlow = function (size) {
  336. return allocUnsafe(null, size)
  337. };
  338. function fromString (that, string, encoding) {
  339. if (typeof encoding !== 'string' || encoding === '') {
  340. encoding = 'utf8';
  341. }
  342. if (!Buffer.isEncoding(encoding)) {
  343. throw new TypeError('"encoding" must be a valid string encoding')
  344. }
  345. var length = byteLength(string, encoding) | 0;
  346. that = createBuffer(that, length);
  347. var actual = that.write(string, encoding);
  348. if (actual !== length) {
  349. // Writing a hex string, for example, that contains invalid characters will
  350. // cause everything after the first invalid character to be ignored. (e.g.
  351. // 'abxxcd' will be treated as 'ab')
  352. that = that.slice(0, actual);
  353. }
  354. return that
  355. }
  356. function fromArrayLike (that, array) {
  357. var length = array.length < 0 ? 0 : checked(array.length) | 0;
  358. that = createBuffer(that, length);
  359. for (var i = 0; i < length; i += 1) {
  360. that[i] = array[i] & 255;
  361. }
  362. return that
  363. }
  364. function fromArrayBuffer (that, array, byteOffset, length) {
  365. array.byteLength; // this throws if `array` is not a valid ArrayBuffer
  366. if (byteOffset < 0 || array.byteLength < byteOffset) {
  367. throw new RangeError('\'offset\' is out of bounds')
  368. }
  369. if (array.byteLength < byteOffset + (length || 0)) {
  370. throw new RangeError('\'length\' is out of bounds')
  371. }
  372. if (byteOffset === undefined && length === undefined) {
  373. array = new Uint8Array(array);
  374. } else if (length === undefined) {
  375. array = new Uint8Array(array, byteOffset);
  376. } else {
  377. array = new Uint8Array(array, byteOffset, length);
  378. }
  379. if (Buffer.TYPED_ARRAY_SUPPORT) {
  380. // Return an augmented `Uint8Array` instance, for best performance
  381. that = array;
  382. that.__proto__ = Buffer.prototype;
  383. } else {
  384. // Fallback: Return an object instance of the Buffer class
  385. that = fromArrayLike(that, array);
  386. }
  387. return that
  388. }
  389. function fromObject (that, obj) {
  390. if (internalIsBuffer(obj)) {
  391. var len = checked(obj.length) | 0;
  392. that = createBuffer(that, len);
  393. if (that.length === 0) {
  394. return that
  395. }
  396. obj.copy(that, 0, 0, len);
  397. return that
  398. }
  399. if (obj) {
  400. if ((typeof ArrayBuffer !== 'undefined' &&
  401. obj.buffer instanceof ArrayBuffer) || 'length' in obj) {
  402. if (typeof obj.length !== 'number' || isnan(obj.length)) {
  403. return createBuffer(that, 0)
  404. }
  405. return fromArrayLike(that, obj)
  406. }
  407. if (obj.type === 'Buffer' && isArray(obj.data)) {
  408. return fromArrayLike(that, obj.data)
  409. }
  410. }
  411. throw new TypeError('First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.')
  412. }
  413. function checked (length) {
  414. // Note: cannot use `length < kMaxLength()` here because that fails when
  415. // length is NaN (which is otherwise coerced to zero.)
  416. if (length >= kMaxLength()) {
  417. throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
  418. 'size: 0x' + kMaxLength().toString(16) + ' bytes')
  419. }
  420. return length | 0
  421. }
  422. Buffer.isBuffer = isBuffer;
  423. function internalIsBuffer (b) {
  424. return !!(b != null && b._isBuffer)
  425. }
  426. Buffer.compare = function compare (a, b) {
  427. if (!internalIsBuffer(a) || !internalIsBuffer(b)) {
  428. throw new TypeError('Arguments must be Buffers')
  429. }
  430. if (a === b) return 0
  431. var x = a.length;
  432. var y = b.length;
  433. for (var i = 0, len = Math.min(x, y); i < len; ++i) {
  434. if (a[i] !== b[i]) {
  435. x = a[i];
  436. y = b[i];
  437. break
  438. }
  439. }
  440. if (x < y) return -1
  441. if (y < x) return 1
  442. return 0
  443. };
  444. Buffer.isEncoding = function isEncoding (encoding) {
  445. switch (String(encoding).toLowerCase()) {
  446. case 'hex':
  447. case 'utf8':
  448. case 'utf-8':
  449. case 'ascii':
  450. case 'latin1':
  451. case 'binary':
  452. case 'base64':
  453. case 'ucs2':
  454. case 'ucs-2':
  455. case 'utf16le':
  456. case 'utf-16le':
  457. return true
  458. default:
  459. return false
  460. }
  461. };
  462. Buffer.concat = function concat (list, length) {
  463. if (!isArray(list)) {
  464. throw new TypeError('"list" argument must be an Array of Buffers')
  465. }
  466. if (list.length === 0) {
  467. return Buffer.alloc(0)
  468. }
  469. var i;
  470. if (length === undefined) {
  471. length = 0;
  472. for (i = 0; i < list.length; ++i) {
  473. length += list[i].length;
  474. }
  475. }
  476. var buffer = Buffer.allocUnsafe(length);
  477. var pos = 0;
  478. for (i = 0; i < list.length; ++i) {
  479. var buf = list[i];
  480. if (!internalIsBuffer(buf)) {
  481. throw new TypeError('"list" argument must be an Array of Buffers')
  482. }
  483. buf.copy(buffer, pos);
  484. pos += buf.length;
  485. }
  486. return buffer
  487. };
  488. function byteLength (string, encoding) {
  489. if (internalIsBuffer(string)) {
  490. return string.length
  491. }
  492. if (typeof ArrayBuffer !== 'undefined' && typeof ArrayBuffer.isView === 'function' &&
  493. (ArrayBuffer.isView(string) || string instanceof ArrayBuffer)) {
  494. return string.byteLength
  495. }
  496. if (typeof string !== 'string') {
  497. string = '' + string;
  498. }
  499. var len = string.length;
  500. if (len === 0) return 0
  501. // Use a for loop to avoid recursion
  502. var loweredCase = false;
  503. for (;;) {
  504. switch (encoding) {
  505. case 'ascii':
  506. case 'latin1':
  507. case 'binary':
  508. return len
  509. case 'utf8':
  510. case 'utf-8':
  511. case undefined:
  512. return utf8ToBytes(string).length
  513. case 'ucs2':
  514. case 'ucs-2':
  515. case 'utf16le':
  516. case 'utf-16le':
  517. return len * 2
  518. case 'hex':
  519. return len >>> 1
  520. case 'base64':
  521. return base64ToBytes(string).length
  522. default:
  523. if (loweredCase) return utf8ToBytes(string).length // assume utf8
  524. encoding = ('' + encoding).toLowerCase();
  525. loweredCase = true;
  526. }
  527. }
  528. }
  529. Buffer.byteLength = byteLength;
  530. function slowToString (encoding, start, end) {
  531. var loweredCase = false;
  532. // No need to verify that "this.length <= MAX_UINT32" since it's a read-only
  533. // property of a typed array.
  534. // This behaves neither like String nor Uint8Array in that we set start/end
  535. // to their upper/lower bounds if the value passed is out of range.
  536. // undefined is handled specially as per ECMA-262 6th Edition,
  537. // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization.
  538. if (start === undefined || start < 0) {
  539. start = 0;
  540. }
  541. // Return early if start > this.length. Done here to prevent potential uint32
  542. // coercion fail below.
  543. if (start > this.length) {
  544. return ''
  545. }
  546. if (end === undefined || end > this.length) {
  547. end = this.length;
  548. }
  549. if (end <= 0) {
  550. return ''
  551. }
  552. // Force coersion to uint32. This will also coerce falsey/NaN values to 0.
  553. end >>>= 0;
  554. start >>>= 0;
  555. if (end <= start) {
  556. return ''
  557. }
  558. if (!encoding) encoding = 'utf8';
  559. while (true) {
  560. switch (encoding) {
  561. case 'hex':
  562. return hexSlice(this, start, end)
  563. case 'utf8':
  564. case 'utf-8':
  565. return utf8Slice(this, start, end)
  566. case 'ascii':
  567. return asciiSlice(this, start, end)
  568. case 'latin1':
  569. case 'binary':
  570. return latin1Slice(this, start, end)
  571. case 'base64':
  572. return base64Slice(this, start, end)
  573. case 'ucs2':
  574. case 'ucs-2':
  575. case 'utf16le':
  576. case 'utf-16le':
  577. return utf16leSlice(this, start, end)
  578. default:
  579. if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
  580. encoding = (encoding + '').toLowerCase();
  581. loweredCase = true;
  582. }
  583. }
  584. }
  585. // The property is used by `Buffer.isBuffer` and `is-buffer` (in Safari 5-7) to detect
  586. // Buffer instances.
  587. Buffer.prototype._isBuffer = true;
  588. function swap (b, n, m) {
  589. var i = b[n];
  590. b[n] = b[m];
  591. b[m] = i;
  592. }
  593. Buffer.prototype.swap16 = function swap16 () {
  594. var len = this.length;
  595. if (len % 2 !== 0) {
  596. throw new RangeError('Buffer size must be a multiple of 16-bits')
  597. }
  598. for (var i = 0; i < len; i += 2) {
  599. swap(this, i, i + 1);
  600. }
  601. return this
  602. };
  603. Buffer.prototype.swap32 = function swap32 () {
  604. var len = this.length;
  605. if (len % 4 !== 0) {
  606. throw new RangeError('Buffer size must be a multiple of 32-bits')
  607. }
  608. for (var i = 0; i < len; i += 4) {
  609. swap(this, i, i + 3);
  610. swap(this, i + 1, i + 2);
  611. }
  612. return this
  613. };
  614. Buffer.prototype.swap64 = function swap64 () {
  615. var len = this.length;
  616. if (len % 8 !== 0) {
  617. throw new RangeError('Buffer size must be a multiple of 64-bits')
  618. }
  619. for (var i = 0; i < len; i += 8) {
  620. swap(this, i, i + 7);
  621. swap(this, i + 1, i + 6);
  622. swap(this, i + 2, i + 5);
  623. swap(this, i + 3, i + 4);
  624. }
  625. return this
  626. };
  627. Buffer.prototype.toString = function toString () {
  628. var length = this.length | 0;
  629. if (length === 0) return ''
  630. if (arguments.length === 0) return utf8Slice(this, 0, length)
  631. return slowToString.apply(this, arguments)
  632. };
  633. Buffer.prototype.equals = function equals (b) {
  634. if (!internalIsBuffer(b)) throw new TypeError('Argument must be a Buffer')
  635. if (this === b) return true
  636. return Buffer.compare(this, b) === 0
  637. };
  638. Buffer.prototype.inspect = function inspect () {
  639. var str = '';
  640. var max = INSPECT_MAX_BYTES;
  641. if (this.length > 0) {
  642. str = this.toString('hex', 0, max).match(/.{2}/g).join(' ');
  643. if (this.length > max) str += ' ... ';
  644. }
  645. return '<Buffer ' + str + '>'
  646. };
  647. Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) {
  648. if (!internalIsBuffer(target)) {
  649. throw new TypeError('Argument must be a Buffer')
  650. }
  651. if (start === undefined) {
  652. start = 0;
  653. }
  654. if (end === undefined) {
  655. end = target ? target.length : 0;
  656. }
  657. if (thisStart === undefined) {
  658. thisStart = 0;
  659. }
  660. if (thisEnd === undefined) {
  661. thisEnd = this.length;
  662. }
  663. if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) {
  664. throw new RangeError('out of range index')
  665. }
  666. if (thisStart >= thisEnd && start >= end) {
  667. return 0
  668. }
  669. if (thisStart >= thisEnd) {
  670. return -1
  671. }
  672. if (start >= end) {
  673. return 1
  674. }
  675. start >>>= 0;
  676. end >>>= 0;
  677. thisStart >>>= 0;
  678. thisEnd >>>= 0;
  679. if (this === target) return 0
  680. var x = thisEnd - thisStart;
  681. var y = end - start;
  682. var len = Math.min(x, y);
  683. var thisCopy = this.slice(thisStart, thisEnd);
  684. var targetCopy = target.slice(start, end);
  685. for (var i = 0; i < len; ++i) {
  686. if (thisCopy[i] !== targetCopy[i]) {
  687. x = thisCopy[i];
  688. y = targetCopy[i];
  689. break
  690. }
  691. }
  692. if (x < y) return -1
  693. if (y < x) return 1
  694. return 0
  695. };
  696. // Finds either the first index of `val` in `buffer` at offset >= `byteOffset`,
  697. // OR the last index of `val` in `buffer` at offset <= `byteOffset`.
  698. //
  699. // Arguments:
  700. // - buffer - a Buffer to search
  701. // - val - a string, Buffer, or number
  702. // - byteOffset - an index into `buffer`; will be clamped to an int32
  703. // - encoding - an optional encoding, relevant is val is a string
  704. // - dir - true for indexOf, false for lastIndexOf
  705. function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) {
  706. // Empty buffer means no match
  707. if (buffer.length === 0) return -1
  708. // Normalize byteOffset
  709. if (typeof byteOffset === 'string') {
  710. encoding = byteOffset;
  711. byteOffset = 0;
  712. } else if (byteOffset > 0x7fffffff) {
  713. byteOffset = 0x7fffffff;
  714. } else if (byteOffset < -0x80000000) {
  715. byteOffset = -0x80000000;
  716. }
  717. byteOffset = +byteOffset; // Coerce to Number.
  718. if (isNaN(byteOffset)) {
  719. // byteOffset: it it's undefined, null, NaN, "foo", etc, search whole buffer
  720. byteOffset = dir ? 0 : (buffer.length - 1);
  721. }
  722. // Normalize byteOffset: negative offsets start from the end of the buffer
  723. if (byteOffset < 0) byteOffset = buffer.length + byteOffset;
  724. if (byteOffset >= buffer.length) {
  725. if (dir) return -1
  726. else byteOffset = buffer.length - 1;
  727. } else if (byteOffset < 0) {
  728. if (dir) byteOffset = 0;
  729. else return -1
  730. }
  731. // Normalize val
  732. if (typeof val === 'string') {
  733. val = Buffer.from(val, encoding);
  734. }
  735. // Finally, search either indexOf (if dir is true) or lastIndexOf
  736. if (internalIsBuffer(val)) {
  737. // Special case: looking for empty string/buffer always fails
  738. if (val.length === 0) {
  739. return -1
  740. }
  741. return arrayIndexOf(buffer, val, byteOffset, encoding, dir)
  742. } else if (typeof val === 'number') {
  743. val = val & 0xFF; // Search for a byte value [0-255]
  744. if (Buffer.TYPED_ARRAY_SUPPORT &&
  745. typeof Uint8Array.prototype.indexOf === 'function') {
  746. if (dir) {
  747. return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset)
  748. } else {
  749. return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset)
  750. }
  751. }
  752. return arrayIndexOf(buffer, [ val ], byteOffset, encoding, dir)
  753. }
  754. throw new TypeError('val must be string, number or Buffer')
  755. }
  756. function arrayIndexOf (arr, val, byteOffset, encoding, dir) {
  757. var indexSize = 1;
  758. var arrLength = arr.length;
  759. var valLength = val.length;
  760. if (encoding !== undefined) {
  761. encoding = String(encoding).toLowerCase();
  762. if (encoding === 'ucs2' || encoding === 'ucs-2' ||
  763. encoding === 'utf16le' || encoding === 'utf-16le') {
  764. if (arr.length < 2 || val.length < 2) {
  765. return -1
  766. }
  767. indexSize = 2;
  768. arrLength /= 2;
  769. valLength /= 2;
  770. byteOffset /= 2;
  771. }
  772. }
  773. function read (buf, i) {
  774. if (indexSize === 1) {
  775. return buf[i]
  776. } else {
  777. return buf.readUInt16BE(i * indexSize)
  778. }
  779. }
  780. var i;
  781. if (dir) {
  782. var foundIndex = -1;
  783. for (i = byteOffset; i < arrLength; i++) {
  784. if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) {
  785. if (foundIndex === -1) foundIndex = i;
  786. if (i - foundIndex + 1 === valLength) return foundIndex * indexSize
  787. } else {
  788. if (foundIndex !== -1) i -= i - foundIndex;
  789. foundIndex = -1;
  790. }
  791. }
  792. } else {
  793. if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength;
  794. for (i = byteOffset; i >= 0; i--) {
  795. var found = true;
  796. for (var j = 0; j < valLength; j++) {
  797. if (read(arr, i + j) !== read(val, j)) {
  798. found = false;
  799. break
  800. }
  801. }
  802. if (found) return i
  803. }
  804. }
  805. return -1
  806. }
  807. Buffer.prototype.includes = function includes (val, byteOffset, encoding) {
  808. return this.indexOf(val, byteOffset, encoding) !== -1
  809. };
  810. Buffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) {
  811. return bidirectionalIndexOf(this, val, byteOffset, encoding, true)
  812. };
  813. Buffer.prototype.lastIndexOf = function lastIndexOf (val, byteOffset, encoding) {
  814. return bidirectionalIndexOf(this, val, byteOffset, encoding, false)
  815. };
  816. function hexWrite (buf, string, offset, length) {
  817. offset = Number(offset) || 0;
  818. var remaining = buf.length - offset;
  819. if (!length) {
  820. length = remaining;
  821. } else {
  822. length = Number(length);
  823. if (length > remaining) {
  824. length = remaining;
  825. }
  826. }
  827. // must be an even number of digits
  828. var strLen = string.length;
  829. if (strLen % 2 !== 0) throw new TypeError('Invalid hex string')
  830. if (length > strLen / 2) {
  831. length = strLen / 2;
  832. }
  833. for (var i = 0; i < length; ++i) {
  834. var parsed = parseInt(string.substr(i * 2, 2), 16);
  835. if (isNaN(parsed)) return i
  836. buf[offset + i] = parsed;
  837. }
  838. return i
  839. }
  840. function utf8Write (buf, string, offset, length) {
  841. return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length)
  842. }
  843. function asciiWrite (buf, string, offset, length) {
  844. return blitBuffer(asciiToBytes(string), buf, offset, length)
  845. }
  846. function latin1Write (buf, string, offset, length) {
  847. return asciiWrite(buf, string, offset, length)
  848. }
  849. function base64Write (buf, string, offset, length) {
  850. return blitBuffer(base64ToBytes(string), buf, offset, length)
  851. }
  852. function ucs2Write (buf, string, offset, length) {
  853. return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length)
  854. }
  855. Buffer.prototype.write = function write (string, offset, length, encoding) {
  856. // Buffer#write(string)
  857. if (offset === undefined) {
  858. encoding = 'utf8';
  859. length = this.length;
  860. offset = 0;
  861. // Buffer#write(string, encoding)
  862. } else if (length === undefined && typeof offset === 'string') {
  863. encoding = offset;
  864. length = this.length;
  865. offset = 0;
  866. // Buffer#write(string, offset[, length][, encoding])
  867. } else if (isFinite(offset)) {
  868. offset = offset | 0;
  869. if (isFinite(length)) {
  870. length = length | 0;
  871. if (encoding === undefined) encoding = 'utf8';
  872. } else {
  873. encoding = length;
  874. length = undefined;
  875. }
  876. // legacy write(string, encoding, offset, length) - remove in v0.13
  877. } else {
  878. throw new Error(
  879. 'Buffer.write(string, encoding, offset[, length]) is no longer supported'
  880. )
  881. }
  882. var remaining = this.length - offset;
  883. if (length === undefined || length > remaining) length = remaining;
  884. if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) {
  885. throw new RangeError('Attempt to write outside buffer bounds')
  886. }
  887. if (!encoding) encoding = 'utf8';
  888. var loweredCase = false;
  889. for (;;) {
  890. switch (encoding) {
  891. case 'hex':
  892. return hexWrite(this, string, offset, length)
  893. case 'utf8':
  894. case 'utf-8':
  895. return utf8Write(this, string, offset, length)
  896. case 'ascii':
  897. return asciiWrite(this, string, offset, length)
  898. case 'latin1':
  899. case 'binary':
  900. return latin1Write(this, string, offset, length)
  901. case 'base64':
  902. // Warning: maxLength not taken into account in base64Write
  903. return base64Write(this, string, offset, length)
  904. case 'ucs2':
  905. case 'ucs-2':
  906. case 'utf16le':
  907. case 'utf-16le':
  908. return ucs2Write(this, string, offset, length)
  909. default:
  910. if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
  911. encoding = ('' + encoding).toLowerCase();
  912. loweredCase = true;
  913. }
  914. }
  915. };
  916. Buffer.prototype.toJSON = function toJSON () {
  917. return {
  918. type: 'Buffer',
  919. data: Array.prototype.slice.call(this._arr || this, 0)
  920. }
  921. };
  922. function base64Slice (buf, start, end) {
  923. if (start === 0 && end === buf.length) {
  924. return fromByteArray(buf)
  925. } else {
  926. return fromByteArray(buf.slice(start, end))
  927. }
  928. }
  929. function utf8Slice (buf, start, end) {
  930. end = Math.min(buf.length, end);
  931. var res = [];
  932. var i = start;
  933. while (i < end) {
  934. var firstByte = buf[i];
  935. var codePoint = null;
  936. var bytesPerSequence = (firstByte > 0xEF) ? 4
  937. : (firstByte > 0xDF) ? 3
  938. : (firstByte > 0xBF) ? 2
  939. : 1;
  940. if (i + bytesPerSequence <= end) {
  941. var secondByte, thirdByte, fourthByte, tempCodePoint;
  942. switch (bytesPerSequence) {
  943. case 1:
  944. if (firstByte < 0x80) {
  945. codePoint = firstByte;
  946. }
  947. break
  948. case 2:
  949. secondByte = buf[i + 1];
  950. if ((secondByte & 0xC0) === 0x80) {
  951. tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F);
  952. if (tempCodePoint > 0x7F) {
  953. codePoint = tempCodePoint;
  954. }
  955. }
  956. break
  957. case 3:
  958. secondByte = buf[i + 1];
  959. thirdByte = buf[i + 2];
  960. if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) {
  961. tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F);
  962. if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) {
  963. codePoint = tempCodePoint;
  964. }
  965. }
  966. break
  967. case 4:
  968. secondByte = buf[i + 1];
  969. thirdByte = buf[i + 2];
  970. fourthByte = buf[i + 3];
  971. if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) {
  972. tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F);
  973. if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) {
  974. codePoint = tempCodePoint;
  975. }
  976. }
  977. }
  978. }
  979. if (codePoint === null) {
  980. // we did not generate a valid codePoint so insert a
  981. // replacement char (U+FFFD) and advance only 1 byte
  982. codePoint = 0xFFFD;
  983. bytesPerSequence = 1;
  984. } else if (codePoint > 0xFFFF) {
  985. // encode to utf16 (surrogate pair dance)
  986. codePoint -= 0x10000;
  987. res.push(codePoint >>> 10 & 0x3FF | 0xD800);
  988. codePoint = 0xDC00 | codePoint & 0x3FF;
  989. }
  990. res.push(codePoint);
  991. i += bytesPerSequence;
  992. }
  993. return decodeCodePointsArray(res)
  994. }
  995. // Based on http://stackoverflow.com/a/22747272/680742, the browser with
  996. // the lowest limit is Chrome, with 0x10000 args.
  997. // We go 1 magnitude less, for safety
  998. var MAX_ARGUMENTS_LENGTH = 0x1000;
  999. function decodeCodePointsArray (codePoints) {
  1000. var len = codePoints.length;
  1001. if (len <= MAX_ARGUMENTS_LENGTH) {
  1002. return String.fromCharCode.apply(String, codePoints) // avoid extra slice()
  1003. }
  1004. // Decode in chunks to avoid "call stack size exceeded".
  1005. var res = '';
  1006. var i = 0;
  1007. while (i < len) {
  1008. res += String.fromCharCode.apply(
  1009. String,
  1010. codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH)
  1011. );
  1012. }
  1013. return res
  1014. }
  1015. function asciiSlice (buf, start, end) {
  1016. var ret = '';
  1017. end = Math.min(buf.length, end);
  1018. for (var i = start; i < end; ++i) {
  1019. ret += String.fromCharCode(buf[i] & 0x7F);
  1020. }
  1021. return ret
  1022. }
  1023. function latin1Slice (buf, start, end) {
  1024. var ret = '';
  1025. end = Math.min(buf.length, end);
  1026. for (var i = start; i < end; ++i) {
  1027. ret += String.fromCharCode(buf[i]);
  1028. }
  1029. return ret
  1030. }
  1031. function hexSlice (buf, start, end) {
  1032. var len = buf.length;
  1033. if (!start || start < 0) start = 0;
  1034. if (!end || end < 0 || end > len) end = len;
  1035. var out = '';
  1036. for (var i = start; i < end; ++i) {
  1037. out += toHex(buf[i]);
  1038. }
  1039. return out
  1040. }
  1041. function utf16leSlice (buf, start, end) {
  1042. var bytes = buf.slice(start, end);
  1043. var res = '';
  1044. for (var i = 0; i < bytes.length; i += 2) {
  1045. res += String.fromCharCode(bytes[i] + bytes[i + 1] * 256);
  1046. }
  1047. return res
  1048. }
  1049. Buffer.prototype.slice = function slice (start, end) {
  1050. var len = this.length;
  1051. start = ~~start;
  1052. end = end === undefined ? len : ~~end;
  1053. if (start < 0) {
  1054. start += len;
  1055. if (start < 0) start = 0;
  1056. } else if (start > len) {
  1057. start = len;
  1058. }
  1059. if (end < 0) {
  1060. end += len;
  1061. if (end < 0) end = 0;
  1062. } else if (end > len) {
  1063. end = len;
  1064. }
  1065. if (end < start) end = start;
  1066. var newBuf;
  1067. if (Buffer.TYPED_ARRAY_SUPPORT) {
  1068. newBuf = this.subarray(start, end);
  1069. newBuf.__proto__ = Buffer.prototype;
  1070. } else {
  1071. var sliceLen = end - start;
  1072. newBuf = new Buffer(sliceLen, undefined);
  1073. for (var i = 0; i < sliceLen; ++i) {
  1074. newBuf[i] = this[i + start];
  1075. }
  1076. }
  1077. return newBuf
  1078. };
  1079. /*
  1080. * Need to make sure that buffer isn't trying to write out of bounds.
  1081. */
  1082. function checkOffset (offset, ext, length) {
  1083. if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint')
  1084. if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length')
  1085. }
  1086. Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) {
  1087. offset = offset | 0;
  1088. byteLength = byteLength | 0;
  1089. if (!noAssert) checkOffset(offset, byteLength, this.length);
  1090. var val = this[offset];
  1091. var mul = 1;
  1092. var i = 0;
  1093. while (++i < byteLength && (mul *= 0x100)) {
  1094. val += this[offset + i] * mul;
  1095. }
  1096. return val
  1097. };
  1098. Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) {
  1099. offset = offset | 0;
  1100. byteLength = byteLength | 0;
  1101. if (!noAssert) {
  1102. checkOffset(offset, byteLength, this.length);
  1103. }
  1104. var val = this[offset + --byteLength];
  1105. var mul = 1;
  1106. while (byteLength > 0 && (mul *= 0x100)) {
  1107. val += this[offset + --byteLength] * mul;
  1108. }
  1109. return val
  1110. };
  1111. Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) {
  1112. if (!noAssert) checkOffset(offset, 1, this.length);
  1113. return this[offset]
  1114. };
  1115. Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) {
  1116. if (!noAssert) checkOffset(offset, 2, this.length);
  1117. return this[offset] | (this[offset + 1] << 8)
  1118. };
  1119. Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) {
  1120. if (!noAssert) checkOffset(offset, 2, this.length);
  1121. return (this[offset] << 8) | this[offset + 1]
  1122. };
  1123. Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) {
  1124. if (!noAssert) checkOffset(offset, 4, this.length);
  1125. return ((this[offset]) |
  1126. (this[offset + 1] << 8) |
  1127. (this[offset + 2] << 16)) +
  1128. (this[offset + 3] * 0x1000000)
  1129. };
  1130. Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) {
  1131. if (!noAssert) checkOffset(offset, 4, this.length);
  1132. return (this[offset] * 0x1000000) +
  1133. ((this[offset + 1] << 16) |
  1134. (this[offset + 2] << 8) |
  1135. this[offset + 3])
  1136. };
  1137. Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) {
  1138. offset = offset | 0;
  1139. byteLength = byteLength | 0;
  1140. if (!noAssert) checkOffset(offset, byteLength, this.length);
  1141. var val = this[offset];
  1142. var mul = 1;
  1143. var i = 0;
  1144. while (++i < byteLength && (mul *= 0x100)) {
  1145. val += this[offset + i] * mul;
  1146. }
  1147. mul *= 0x80;
  1148. if (val >= mul) val -= Math.pow(2, 8 * byteLength);
  1149. return val
  1150. };
  1151. Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) {
  1152. offset = offset | 0;
  1153. byteLength = byteLength | 0;
  1154. if (!noAssert) checkOffset(offset, byteLength, this.length);
  1155. var i = byteLength;
  1156. var mul = 1;
  1157. var val = this[offset + --i];
  1158. while (i > 0 && (mul *= 0x100)) {
  1159. val += this[offset + --i] * mul;
  1160. }
  1161. mul *= 0x80;
  1162. if (val >= mul) val -= Math.pow(2, 8 * byteLength);
  1163. return val
  1164. };
  1165. Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) {
  1166. if (!noAssert) checkOffset(offset, 1, this.length);
  1167. if (!(this[offset] & 0x80)) return (this[offset])
  1168. return ((0xff - this[offset] + 1) * -1)
  1169. };
  1170. Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) {
  1171. if (!noAssert) checkOffset(offset, 2, this.length);
  1172. var val = this[offset] | (this[offset + 1] << 8);
  1173. return (val & 0x8000) ? val | 0xFFFF0000 : val
  1174. };
  1175. Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) {
  1176. if (!noAssert) checkOffset(offset, 2, this.length);
  1177. var val = this[offset + 1] | (this[offset] << 8);
  1178. return (val & 0x8000) ? val | 0xFFFF0000 : val
  1179. };
  1180. Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) {
  1181. if (!noAssert) checkOffset(offset, 4, this.length);
  1182. return (this[offset]) |
  1183. (this[offset + 1] << 8) |
  1184. (this[offset + 2] << 16) |
  1185. (this[offset + 3] << 24)
  1186. };
  1187. Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) {
  1188. if (!noAssert) checkOffset(offset, 4, this.length);
  1189. return (this[offset] << 24) |
  1190. (this[offset + 1] << 16) |
  1191. (this[offset + 2] << 8) |
  1192. (this[offset + 3])
  1193. };
  1194. Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) {
  1195. if (!noAssert) checkOffset(offset, 4, this.length);
  1196. return read(this, offset, true, 23, 4)
  1197. };
  1198. Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) {
  1199. if (!noAssert) checkOffset(offset, 4, this.length);
  1200. return read(this, offset, false, 23, 4)
  1201. };
  1202. Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) {
  1203. if (!noAssert) checkOffset(offset, 8, this.length);
  1204. return read(this, offset, true, 52, 8)
  1205. };
  1206. Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) {
  1207. if (!noAssert) checkOffset(offset, 8, this.length);
  1208. return read(this, offset, false, 52, 8)
  1209. };
  1210. function checkInt (buf, value, offset, ext, max, min) {
  1211. if (!internalIsBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance')
  1212. if (value > max || value < min) throw new RangeError('"value" argument is out of bounds')
  1213. if (offset + ext > buf.length) throw new RangeError('Index out of range')
  1214. }
  1215. Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) {
  1216. value = +value;
  1217. offset = offset | 0;
  1218. byteLength = byteLength | 0;
  1219. if (!noAssert) {
  1220. var maxBytes = Math.pow(2, 8 * byteLength) - 1;
  1221. checkInt(this, value, offset, byteLength, maxBytes, 0);
  1222. }
  1223. var mul = 1;
  1224. var i = 0;
  1225. this[offset] = value & 0xFF;
  1226. while (++i < byteLength && (mul *= 0x100)) {
  1227. this[offset + i] = (value / mul) & 0xFF;
  1228. }
  1229. return offset + byteLength
  1230. };
  1231. Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) {
  1232. value = +value;
  1233. offset = offset | 0;
  1234. byteLength = byteLength | 0;
  1235. if (!noAssert) {
  1236. var maxBytes = Math.pow(2, 8 * byteLength) - 1;
  1237. checkInt(this, value, offset, byteLength, maxBytes, 0);
  1238. }
  1239. var i = byteLength - 1;
  1240. var mul = 1;
  1241. this[offset + i] = value & 0xFF;
  1242. while (--i >= 0 && (mul *= 0x100)) {
  1243. this[offset + i] = (value / mul) & 0xFF;
  1244. }
  1245. return offset + byteLength
  1246. };
  1247. Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) {
  1248. value = +value;
  1249. offset = offset | 0;
  1250. if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0);
  1251. if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value);
  1252. this[offset] = (value & 0xff);
  1253. return offset + 1
  1254. };
  1255. function objectWriteUInt16 (buf, value, offset, littleEndian) {
  1256. if (value < 0) value = 0xffff + value + 1;
  1257. for (var i = 0, j = Math.min(buf.length - offset, 2); i < j; ++i) {
  1258. buf[offset + i] = (value & (0xff << (8 * (littleEndian ? i : 1 - i)))) >>>
  1259. (littleEndian ? i : 1 - i) * 8;
  1260. }
  1261. }
  1262. Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) {
  1263. value = +value;
  1264. offset = offset | 0;
  1265. if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0);
  1266. if (Buffer.TYPED_ARRAY_SUPPORT) {
  1267. this[offset] = (value & 0xff);
  1268. this[offset + 1] = (value >>> 8);
  1269. } else {
  1270. objectWriteUInt16(this, value, offset, true);
  1271. }
  1272. return offset + 2
  1273. };
  1274. Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) {
  1275. value = +value;
  1276. offset = offset | 0;
  1277. if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0);
  1278. if (Buffer.TYPED_ARRAY_SUPPORT) {
  1279. this[offset] = (value >>> 8);
  1280. this[offset + 1] = (value & 0xff);
  1281. } else {
  1282. objectWriteUInt16(this, value, offset, false);
  1283. }
  1284. return offset + 2
  1285. };
  1286. function objectWriteUInt32 (buf, value, offset, littleEndian) {
  1287. if (value < 0) value = 0xffffffff + value + 1;
  1288. for (var i = 0, j = Math.min(buf.length - offset, 4); i < j; ++i) {
  1289. buf[offset + i] = (value >>> (littleEndian ? i : 3 - i) * 8) & 0xff;
  1290. }
  1291. }
  1292. Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) {
  1293. value = +value;
  1294. offset = offset | 0;
  1295. if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0);
  1296. if (Buffer.TYPED_ARRAY_SUPPORT) {
  1297. this[offset + 3] = (value >>> 24);
  1298. this[offset + 2] = (value >>> 16);
  1299. this[offset + 1] = (value >>> 8);
  1300. this[offset] = (value & 0xff);
  1301. } else {
  1302. objectWriteUInt32(this, value, offset, true);
  1303. }
  1304. return offset + 4
  1305. };
  1306. Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) {
  1307. value = +value;
  1308. offset = offset | 0;
  1309. if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0);
  1310. if (Buffer.TYPED_ARRAY_SUPPORT) {
  1311. this[offset] = (value >>> 24);
  1312. this[offset + 1] = (value >>> 16);
  1313. this[offset + 2] = (value >>> 8);
  1314. this[offset + 3] = (value & 0xff);
  1315. } else {
  1316. objectWriteUInt32(this, value, offset, false);
  1317. }
  1318. return offset + 4
  1319. };
  1320. Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) {
  1321. value = +value;
  1322. offset = offset | 0;
  1323. if (!noAssert) {
  1324. var limit = Math.pow(2, 8 * byteLength - 1);
  1325. checkInt(this, value, offset, byteLength, limit - 1, -limit);
  1326. }
  1327. var i = 0;
  1328. var mul = 1;
  1329. var sub = 0;
  1330. this[offset] = value & 0xFF;
  1331. while (++i < byteLength && (mul *= 0x100)) {
  1332. if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) {
  1333. sub = 1;
  1334. }
  1335. this[offset + i] = ((value / mul) >> 0) - sub & 0xFF;
  1336. }
  1337. return offset + byteLength
  1338. };
  1339. Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) {
  1340. value = +value;
  1341. offset = offset | 0;
  1342. if (!noAssert) {
  1343. var limit = Math.pow(2, 8 * byteLength - 1);
  1344. checkInt(this, value, offset, byteLength, limit - 1, -limit);
  1345. }
  1346. var i = byteLength - 1;
  1347. var mul = 1;
  1348. var sub = 0;
  1349. this[offset + i] = value & 0xFF;
  1350. while (--i >= 0 && (mul *= 0x100)) {
  1351. if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) {
  1352. sub = 1;
  1353. }
  1354. this[offset + i] = ((value / mul) >> 0) - sub & 0xFF;
  1355. }
  1356. return offset + byteLength
  1357. };
  1358. Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) {
  1359. value = +value;
  1360. offset = offset | 0;
  1361. if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80);
  1362. if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value);
  1363. if (value < 0) value = 0xff + value + 1;
  1364. this[offset] = (value & 0xff);
  1365. return offset + 1
  1366. };
  1367. Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) {
  1368. value = +value;
  1369. offset = offset | 0;
  1370. if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000);
  1371. if (Buffer.TYPED_ARRAY_SUPPORT) {
  1372. this[offset] = (value & 0xff);
  1373. this[offset + 1] = (value >>> 8);
  1374. } else {
  1375. objectWriteUInt16(this, value, offset, true);
  1376. }
  1377. return offset + 2
  1378. };
  1379. Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) {
  1380. value = +value;
  1381. offset = offset | 0;
  1382. if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000);
  1383. if (Buffer.TYPED_ARRAY_SUPPORT) {
  1384. this[offset] = (value >>> 8);
  1385. this[offset + 1] = (value & 0xff);
  1386. } else {
  1387. objectWriteUInt16(this, value, offset, false);
  1388. }
  1389. return offset + 2
  1390. };
  1391. Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) {
  1392. value = +value;
  1393. offset = offset | 0;
  1394. if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
  1395. if (Buffer.TYPED_ARRAY_SUPPORT) {
  1396. this[offset] = (value & 0xff);
  1397. this[offset + 1] = (value >>> 8);
  1398. this[offset + 2] = (value >>> 16);
  1399. this[offset + 3] = (value >>> 24);
  1400. } else {
  1401. objectWriteUInt32(this, value, offset, true);
  1402. }
  1403. return offset + 4
  1404. };
  1405. Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) {
  1406. value = +value;
  1407. offset = offset | 0;
  1408. if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
  1409. if (value < 0) value = 0xffffffff + value + 1;
  1410. if (Buffer.TYPED_ARRAY_SUPPORT) {
  1411. this[offset] = (value >>> 24);
  1412. this[offset + 1] = (value >>> 16);
  1413. this[offset + 2] = (value >>> 8);
  1414. this[offset + 3] = (value & 0xff);
  1415. } else {
  1416. objectWriteUInt32(this, value, offset, false);
  1417. }
  1418. return offset + 4
  1419. };
  1420. function checkIEEE754 (buf, value, offset, ext, max, min) {
  1421. if (offset + ext > buf.length) throw new RangeError('Index out of range')
  1422. if (offset < 0) throw new RangeError('Index out of range')
  1423. }
  1424. function writeFloat (buf, value, offset, littleEndian, noAssert) {
  1425. if (!noAssert) {
  1426. checkIEEE754(buf, value, offset, 4);
  1427. }
  1428. write(buf, value, offset, littleEndian, 23, 4);
  1429. return offset + 4
  1430. }
  1431. Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) {
  1432. return writeFloat(this, value, offset, true, noAssert)
  1433. };
  1434. Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) {
  1435. return writeFloat(this, value, offset, false, noAssert)
  1436. };
  1437. function writeDouble (buf, value, offset, littleEndian, noAssert) {
  1438. if (!noAssert) {
  1439. checkIEEE754(buf, value, offset, 8);
  1440. }
  1441. write(buf, value, offset, littleEndian, 52, 8);
  1442. return offset + 8
  1443. }
  1444. Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) {
  1445. return writeDouble(this, value, offset, true, noAssert)
  1446. };
  1447. Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) {
  1448. return writeDouble(this, value, offset, false, noAssert)
  1449. };
  1450. // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
  1451. Buffer.prototype.copy = function copy (target, targetStart, start, end) {
  1452. if (!start) start = 0;
  1453. if (!end && end !== 0) end = this.length;
  1454. if (targetStart >= target.length) targetStart = target.length;
  1455. if (!targetStart) targetStart = 0;
  1456. if (end > 0 && end < start) end = start;
  1457. // Copy 0 bytes; we're done
  1458. if (end === start) return 0
  1459. if (target.length === 0 || this.length === 0) return 0
  1460. // Fatal error conditions
  1461. if (targetStart < 0) {
  1462. throw new RangeError('targetStart out of bounds')
  1463. }
  1464. if (start < 0 || start >= this.length) throw new RangeError('sourceStart out of bounds')
  1465. if (end < 0) throw new RangeError('sourceEnd out of bounds')
  1466. // Are we oob?
  1467. if (end > this.length) end = this.length;
  1468. if (target.length - targetStart < end - start) {
  1469. end = target.length - targetStart + start;
  1470. }
  1471. var len = end - start;
  1472. var i;
  1473. if (this === target && start < targetStart && targetStart < end) {
  1474. // descending copy from end
  1475. for (i = len - 1; i >= 0; --i) {
  1476. target[i + targetStart] = this[i + start];
  1477. }
  1478. } else if (len < 1000 || !Buffer.TYPED_ARRAY_SUPPORT) {
  1479. // ascending copy from start
  1480. for (i = 0; i < len; ++i) {
  1481. target[i + targetStart] = this[i + start];
  1482. }
  1483. } else {
  1484. Uint8Array.prototype.set.call(
  1485. target,
  1486. this.subarray(start, start + len),
  1487. targetStart
  1488. );
  1489. }
  1490. return len
  1491. };
  1492. // Usage:
  1493. // buffer.fill(number[, offset[, end]])
  1494. // buffer.fill(buffer[, offset[, end]])
  1495. // buffer.fill(string[, offset[, end]][, encoding])
  1496. Buffer.prototype.fill = function fill (val, start, end, encoding) {
  1497. // Handle string cases:
  1498. if (typeof val === 'string') {
  1499. if (typeof start === 'string') {
  1500. encoding = start;
  1501. start = 0;
  1502. end = this.length;
  1503. } else if (typeof end === 'string') {
  1504. encoding = end;
  1505. end = this.length;
  1506. }
  1507. if (val.length === 1) {
  1508. var code = val.charCodeAt(0);
  1509. if (code < 256) {
  1510. val = code;
  1511. }
  1512. }
  1513. if (encoding !== undefined && typeof encoding !== 'string') {
  1514. throw new TypeError('encoding must be a string')
  1515. }
  1516. if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) {
  1517. throw new TypeError('Unknown encoding: ' + encoding)
  1518. }
  1519. } else if (typeof val === 'number') {
  1520. val = val & 255;
  1521. }
  1522. // Invalid ranges are not set to a default, so can range check early.
  1523. if (start < 0 || this.length < start || this.length < end) {
  1524. throw new RangeError('Out of range index')
  1525. }
  1526. if (end <= start) {
  1527. return this
  1528. }
  1529. start = start >>> 0;
  1530. end = end === undefined ? this.length : end >>> 0;
  1531. if (!val) val = 0;
  1532. var i;
  1533. if (typeof val === 'number') {
  1534. for (i = start; i < end; ++i) {
  1535. this[i] = val;
  1536. }
  1537. } else {
  1538. var bytes = internalIsBuffer(val)
  1539. ? val
  1540. : utf8ToBytes(new Buffer(val, encoding).toString());
  1541. var len = bytes.length;
  1542. for (i = 0; i < end - start; ++i) {
  1543. this[i + start] = bytes[i % len];
  1544. }
  1545. }
  1546. return this
  1547. };
  1548. // HELPER FUNCTIONS
  1549. // ================
  1550. var INVALID_BASE64_RE = /[^+\/0-9A-Za-z-_]/g;
  1551. function base64clean (str) {
  1552. // Node strips out invalid characters like \n and \t from the string, base64-js does not
  1553. str = stringtrim(str).replace(INVALID_BASE64_RE, '');
  1554. // Node converts strings with length < 2 to ''
  1555. if (str.length < 2) return ''
  1556. // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not
  1557. while (str.length % 4 !== 0) {
  1558. str = str + '=';
  1559. }
  1560. return str
  1561. }
  1562. function stringtrim (str) {
  1563. if (str.trim) return str.trim()
  1564. return str.replace(/^\s+|\s+$/g, '')
  1565. }
  1566. function toHex (n) {
  1567. if (n < 16) return '0' + n.toString(16)
  1568. return n.toString(16)
  1569. }
  1570. function utf8ToBytes (string, units) {
  1571. units = units || Infinity;
  1572. var codePoint;
  1573. var length = string.length;
  1574. var leadSurrogate = null;
  1575. var bytes = [];
  1576. for (var i = 0; i < length; ++i) {
  1577. codePoint = string.charCodeAt(i);
  1578. // is surrogate component
  1579. if (codePoint > 0xD7FF && codePoint < 0xE000) {
  1580. // last char was a lead
  1581. if (!leadSurrogate) {
  1582. // no lead yet
  1583. if (codePoint > 0xDBFF) {
  1584. // unexpected trail
  1585. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
  1586. continue
  1587. } else if (i + 1 === length) {
  1588. // unpaired lead
  1589. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
  1590. continue
  1591. }
  1592. // valid lead
  1593. leadSurrogate = codePoint;
  1594. continue
  1595. }
  1596. // 2 leads in a row
  1597. if (codePoint < 0xDC00) {
  1598. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
  1599. leadSurrogate = codePoint;
  1600. continue
  1601. }
  1602. // valid surrogate pair
  1603. codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000;
  1604. } else if (leadSurrogate) {
  1605. // valid bmp char, but last char was a lead
  1606. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD);
  1607. }
  1608. leadSurrogate = null;
  1609. // encode utf8
  1610. if (codePoint < 0x80) {
  1611. if ((units -= 1) < 0) break
  1612. bytes.push(codePoint);
  1613. } else if (codePoint < 0x800) {
  1614. if ((units -= 2) < 0) break
  1615. bytes.push(
  1616. codePoint >> 0x6 | 0xC0,
  1617. codePoint & 0x3F | 0x80
  1618. );
  1619. } else if (codePoint < 0x10000) {
  1620. if ((units -= 3) < 0) break
  1621. bytes.push(
  1622. codePoint >> 0xC | 0xE0,
  1623. codePoint >> 0x6 & 0x3F | 0x80,
  1624. codePoint & 0x3F | 0x80
  1625. );
  1626. } else if (codePoint < 0x110000) {
  1627. if ((units -= 4) < 0) break
  1628. bytes.push(
  1629. codePoint >> 0x12 | 0xF0,
  1630. codePoint >> 0xC & 0x3F | 0x80,
  1631. codePoint >> 0x6 & 0x3F | 0x80,
  1632. codePoint & 0x3F | 0x80
  1633. );
  1634. } else {
  1635. throw new Error('Invalid code point')
  1636. }
  1637. }
  1638. return bytes
  1639. }
  1640. function asciiToBytes (str) {
  1641. var byteArray = [];
  1642. for (var i = 0; i < str.length; ++i) {
  1643. // Node's code seems to be doing this and not & 0x7F..
  1644. byteArray.push(str.charCodeAt(i) & 0xFF);
  1645. }
  1646. return byteArray
  1647. }
  1648. function utf16leToBytes (str, units) {
  1649. var c, hi, lo;
  1650. var byteArray = [];
  1651. for (var i = 0; i < str.length; ++i) {
  1652. if ((units -= 2) < 0) break
  1653. c = str.charCodeAt(i);
  1654. hi = c >> 8;
  1655. lo = c % 256;
  1656. byteArray.push(lo);
  1657. byteArray.push(hi);
  1658. }
  1659. return byteArray
  1660. }
  1661. function base64ToBytes (str) {
  1662. return toByteArray(base64clean(str))
  1663. }
  1664. function blitBuffer (src, dst, offset, length) {
  1665. for (var i = 0; i < length; ++i) {
  1666. if ((i + offset >= dst.length) || (i >= src.length)) break
  1667. dst[i + offset] = src[i];
  1668. }
  1669. return i
  1670. }
  1671. function isnan (val) {
  1672. return val !== val // eslint-disable-line no-self-compare
  1673. }
  1674. // the following is from is-buffer, also by Feross Aboukhadijeh and with same lisence
  1675. // The _isBuffer check is for Safari 5-7 support, because it's missing
  1676. // Object.prototype.constructor. Remove this eventually
  1677. function isBuffer(obj) {
  1678. return obj != null && (!!obj._isBuffer || isFastBuffer(obj) || isSlowBuffer(obj))
  1679. }
  1680. function isFastBuffer (obj) {
  1681. return !!obj.constructor && typeof obj.constructor.isBuffer === 'function' && obj.constructor.isBuffer(obj)
  1682. }
  1683. // For Node v0.10 support. Remove this eventually.
  1684. function isSlowBuffer (obj) {
  1685. return typeof obj.readFloatLE === 'function' && typeof obj.slice === 'function' && isFastBuffer(obj.slice(0, 0))
  1686. }
  1687. class CsvError extends Error {
  1688. constructor(code, message, options, ...contexts) {
  1689. if(Array.isArray(message)) message = message.join(' ').trim();
  1690. super(message);
  1691. if(Error.captureStackTrace !== undefined){
  1692. Error.captureStackTrace(this, CsvError);
  1693. }
  1694. this.code = code;
  1695. for(const context of contexts){
  1696. for(const key in context){
  1697. const value = context[key];
  1698. this[key] = isBuffer(value) ? value.toString(options.encoding) : value == null ? value : JSON.parse(JSON.stringify(value));
  1699. }
  1700. }
  1701. }
  1702. }
  1703. const is_object = function(obj){
  1704. return (typeof obj === 'object' && obj !== null && !Array.isArray(obj));
  1705. };
  1706. const normalize_columns_array = function(columns){
  1707. const normalizedColumns = [];
  1708. for(let i = 0, l = columns.length; i < l; i++){
  1709. const column = columns[i];
  1710. if(column === undefined || column === null || column === false){
  1711. normalizedColumns[i] = { disabled: true };
  1712. }else if(typeof column === 'string'){
  1713. normalizedColumns[i] = { name: column };
  1714. }else if(is_object(column)){
  1715. if(typeof column.name !== 'string'){
  1716. throw new CsvError('CSV_OPTION_COLUMNS_MISSING_NAME', [
  1717. 'Option columns missing name:',
  1718. `property "name" is required at position ${i}`,
  1719. 'when column is an object literal'
  1720. ]);
  1721. }
  1722. normalizedColumns[i] = column;
  1723. }else {
  1724. throw new CsvError('CSV_INVALID_COLUMN_DEFINITION', [
  1725. 'Invalid column definition:',
  1726. 'expect a string or a literal object,',
  1727. `got ${JSON.stringify(column)} at position ${i}`
  1728. ]);
  1729. }
  1730. }
  1731. return normalizedColumns;
  1732. };
  1733. class ResizeableBuffer{
  1734. constructor(size=100){
  1735. this.size = size;
  1736. this.length = 0;
  1737. this.buf = Buffer.allocUnsafe(size);
  1738. }
  1739. prepend(val){
  1740. if(isBuffer(val)){
  1741. const length = this.length + val.length;
  1742. if(length >= this.size){
  1743. this.resize();
  1744. if(length >= this.size){
  1745. throw Error('INVALID_BUFFER_STATE');
  1746. }
  1747. }
  1748. const buf = this.buf;
  1749. this.buf = Buffer.allocUnsafe(this.size);
  1750. val.copy(this.buf, 0);
  1751. buf.copy(this.buf, val.length);
  1752. this.length += val.length;
  1753. }else {
  1754. const length = this.length++;
  1755. if(length === this.size){
  1756. this.resize();
  1757. }
  1758. const buf = this.clone();
  1759. this.buf[0] = val;
  1760. buf.copy(this.buf,1, 0, length);
  1761. }
  1762. }
  1763. append(val){
  1764. const length = this.length++;
  1765. if(length === this.size){
  1766. this.resize();
  1767. }
  1768. this.buf[length] = val;
  1769. }
  1770. clone(){
  1771. return Buffer.from(this.buf.slice(0, this.length));
  1772. }
  1773. resize(){
  1774. const length = this.length;
  1775. this.size = this.size * 2;
  1776. const buf = Buffer.allocUnsafe(this.size);
  1777. this.buf.copy(buf,0, 0, length);
  1778. this.buf = buf;
  1779. }
  1780. toString(encoding){
  1781. if(encoding){
  1782. return this.buf.slice(0, this.length).toString(encoding);
  1783. }else {
  1784. return Uint8Array.prototype.slice.call(this.buf.slice(0, this.length));
  1785. }
  1786. }
  1787. toJSON(){
  1788. return this.toString('utf8');
  1789. }
  1790. reset(){
  1791. this.length = 0;
  1792. }
  1793. }
  1794. // white space characters
  1795. // https://en.wikipedia.org/wiki/Whitespace_character
  1796. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Character_Classes#Types
  1797. // \f\n\r\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff
  1798. const np = 12;
  1799. const cr$1 = 13; // `\r`, carriage return, 0x0D in hexadécimal, 13 in decimal
  1800. const nl$1 = 10; // `\n`, newline, 0x0A in hexadecimal, 10 in decimal
  1801. const space = 32;
  1802. const tab = 9;
  1803. const init_state = function(options){
  1804. return {
  1805. bomSkipped: false,
  1806. bufBytesStart: 0,
  1807. castField: options.cast_function,
  1808. commenting: false,
  1809. // Current error encountered by a record
  1810. error: undefined,
  1811. enabled: options.from_line === 1,
  1812. escaping: false,
  1813. escapeIsQuote: isBuffer(options.escape) && isBuffer(options.quote) && Buffer.compare(options.escape, options.quote) === 0,
  1814. // columns can be `false`, `true`, `Array`
  1815. expectedRecordLength: Array.isArray(options.columns) ? options.columns.length : undefined,
  1816. field: new ResizeableBuffer(20),
  1817. firstLineToHeaders: options.cast_first_line_to_header,
  1818. needMoreDataSize: Math.max(
  1819. // Skip if the remaining buffer smaller than comment
  1820. options.comment !== null ? options.comment.length : 0,
  1821. // Skip if the remaining buffer can be delimiter
  1822. ...options.delimiter.map((delimiter) => delimiter.length),
  1823. // Skip if the remaining buffer can be escape sequence
  1824. options.quote !== null ? options.quote.length : 0,
  1825. ),
  1826. previousBuf: undefined,
  1827. quoting: false,
  1828. stop: false,
  1829. rawBuffer: new ResizeableBuffer(100),
  1830. record: [],
  1831. recordHasError: false,
  1832. record_length: 0,
  1833. recordDelimiterMaxLength: options.record_delimiter.length === 0 ? 0 : Math.max(...options.record_delimiter.map((v) => v.length)),
  1834. trimChars: [Buffer.from(' ', options.encoding)[0], Buffer.from('\t', options.encoding)[0]],
  1835. wasQuoting: false,
  1836. wasRowDelimiter: false,
  1837. timchars: [
  1838. Buffer.from(Buffer.from([cr$1], 'utf8').toString(), options.encoding),
  1839. Buffer.from(Buffer.from([nl$1], 'utf8').toString(), options.encoding),
  1840. Buffer.from(Buffer.from([np], 'utf8').toString(), options.encoding),
  1841. Buffer.from(Buffer.from([space], 'utf8').toString(), options.encoding),
  1842. Buffer.from(Buffer.from([tab], 'utf8').toString(), options.encoding),
  1843. ]
  1844. };
  1845. };
  1846. const underscore = function(str){
  1847. return str.replace(/([A-Z])/g, function(_, match){
  1848. return '_' + match.toLowerCase();
  1849. });
  1850. };
  1851. const normalize_options = function(opts){
  1852. const options = {};
  1853. // Merge with user options
  1854. for(const opt in opts){
  1855. options[underscore(opt)] = opts[opt];
  1856. }
  1857. // Normalize option `encoding`
  1858. // Note: defined first because other options depends on it
  1859. // to convert chars/strings into buffers.
  1860. if(options.encoding === undefined || options.encoding === true){
  1861. options.encoding = 'utf8';
  1862. }else if(options.encoding === null || options.encoding === false){
  1863. options.encoding = null;
  1864. }else if(typeof options.encoding !== 'string' && options.encoding !== null){
  1865. throw new CsvError('CSV_INVALID_OPTION_ENCODING', [
  1866. 'Invalid option encoding:',
  1867. 'encoding must be a string or null to return a buffer,',
  1868. `got ${JSON.stringify(options.encoding)}`
  1869. ], options);
  1870. }
  1871. // Normalize option `bom`
  1872. if(options.bom === undefined || options.bom === null || options.bom === false){
  1873. options.bom = false;
  1874. }else if(options.bom !== true){
  1875. throw new CsvError('CSV_INVALID_OPTION_BOM', [
  1876. 'Invalid option bom:', 'bom must be true,',
  1877. `got ${JSON.stringify(options.bom)}`
  1878. ], options);
  1879. }
  1880. // Normalize option `cast`
  1881. options.cast_function = null;
  1882. if(options.cast === undefined || options.cast === null || options.cast === false || options.cast === ''){
  1883. options.cast = undefined;
  1884. }else if(typeof options.cast === 'function'){
  1885. options.cast_function = options.cast;
  1886. options.cast = true;
  1887. }else if(options.cast !== true){
  1888. throw new CsvError('CSV_INVALID_OPTION_CAST', [
  1889. 'Invalid option cast:', 'cast must be true or a function,',
  1890. `got ${JSON.stringify(options.cast)}`
  1891. ], options);
  1892. }
  1893. // Normalize option `cast_date`
  1894. if(options.cast_date === undefined || options.cast_date === null || options.cast_date === false || options.cast_date === ''){
  1895. options.cast_date = false;
  1896. }else if(options.cast_date === true){
  1897. options.cast_date = function(value){
  1898. const date = Date.parse(value);
  1899. return !isNaN(date) ? new Date(date) : value;
  1900. };
  1901. }else if (typeof options.cast_date !== 'function'){
  1902. throw new CsvError('CSV_INVALID_OPTION_CAST_DATE', [
  1903. 'Invalid option cast_date:', 'cast_date must be true or a function,',
  1904. `got ${JSON.stringify(options.cast_date)}`
  1905. ], options);
  1906. }
  1907. // Normalize option `columns`
  1908. options.cast_first_line_to_header = null;
  1909. if(options.columns === true){
  1910. // Fields in the first line are converted as-is to columns
  1911. options.cast_first_line_to_header = undefined;
  1912. }else if(typeof options.columns === 'function'){
  1913. options.cast_first_line_to_header = options.columns;
  1914. options.columns = true;
  1915. }else if(Array.isArray(options.columns)){
  1916. options.columns = normalize_columns_array(options.columns);
  1917. }else if(options.columns === undefined || options.columns === null || options.columns === false){
  1918. options.columns = false;
  1919. }else {
  1920. throw new CsvError('CSV_INVALID_OPTION_COLUMNS', [
  1921. 'Invalid option columns:',
  1922. 'expect an array, a function or true,',
  1923. `got ${JSON.stringify(options.columns)}`
  1924. ], options);
  1925. }
  1926. // Normalize option `group_columns_by_name`
  1927. if(options.group_columns_by_name === undefined || options.group_columns_by_name === null || options.group_columns_by_name === false){
  1928. options.group_columns_by_name = false;
  1929. }else if(options.group_columns_by_name !== true){
  1930. throw new CsvError('CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME', [
  1931. 'Invalid option group_columns_by_name:',
  1932. 'expect an boolean,',
  1933. `got ${JSON.stringify(options.group_columns_by_name)}`
  1934. ], options);
  1935. }else if(options.columns === false){
  1936. throw new CsvError('CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME', [
  1937. 'Invalid option group_columns_by_name:',
  1938. 'the `columns` mode must be activated.'
  1939. ], options);
  1940. }
  1941. // Normalize option `comment`
  1942. if(options.comment === undefined || options.comment === null || options.comment === false || options.comment === ''){
  1943. options.comment = null;
  1944. }else {
  1945. if(typeof options.comment === 'string'){
  1946. options.comment = Buffer.from(options.comment, options.encoding);
  1947. }
  1948. if(!isBuffer(options.comment)){
  1949. throw new CsvError('CSV_INVALID_OPTION_COMMENT', [
  1950. 'Invalid option comment:',
  1951. 'comment must be a buffer or a string,',
  1952. `got ${JSON.stringify(options.comment)}`
  1953. ], options);
  1954. }
  1955. }
  1956. // Normalize option `comment_no_infix`
  1957. if(options.comment_no_infix === undefined || options.comment_no_infix === null || options.comment_no_infix === false){
  1958. options.comment_no_infix = false;
  1959. }else if(options.comment_no_infix !== true){
  1960. throw new CsvError('CSV_INVALID_OPTION_COMMENT', [
  1961. 'Invalid option comment_no_infix:',
  1962. 'value must be a boolean,',
  1963. `got ${JSON.stringify(options.comment_no_infix)}`
  1964. ], options);
  1965. }
  1966. // Normalize option `delimiter`
  1967. const delimiter_json = JSON.stringify(options.delimiter);
  1968. if(!Array.isArray(options.delimiter)) options.delimiter = [options.delimiter];
  1969. if(options.delimiter.length === 0){
  1970. throw new CsvError('CSV_INVALID_OPTION_DELIMITER', [
  1971. 'Invalid option delimiter:',
  1972. 'delimiter must be a non empty string or buffer or array of string|buffer,',
  1973. `got ${delimiter_json}`
  1974. ], options);
  1975. }
  1976. options.delimiter = options.delimiter.map(function(delimiter){
  1977. if(delimiter === undefined || delimiter === null || delimiter === false){
  1978. return Buffer.from(',', options.encoding);
  1979. }
  1980. if(typeof delimiter === 'string'){
  1981. delimiter = Buffer.from(delimiter, options.encoding);
  1982. }
  1983. if(!isBuffer(delimiter) || delimiter.length === 0){
  1984. throw new CsvError('CSV_INVALID_OPTION_DELIMITER', [
  1985. 'Invalid option delimiter:',
  1986. 'delimiter must be a non empty string or buffer or array of string|buffer,',
  1987. `got ${delimiter_json}`
  1988. ], options);
  1989. }
  1990. return delimiter;
  1991. });
  1992. // Normalize option `escape`
  1993. if(options.escape === undefined || options.escape === true){
  1994. options.escape = Buffer.from('"', options.encoding);
  1995. }else if(typeof options.escape === 'string'){
  1996. options.escape = Buffer.from(options.escape, options.encoding);
  1997. }else if (options.escape === null || options.escape === false){
  1998. options.escape = null;
  1999. }
  2000. if(options.escape !== null){
  2001. if(!isBuffer(options.escape)){
  2002. throw new Error(`Invalid Option: escape must be a buffer, a string or a boolean, got ${JSON.stringify(options.escape)}`);
  2003. }
  2004. }
  2005. // Normalize option `from`
  2006. if(options.from === undefined || options.from === null){
  2007. options.from = 1;
  2008. }else {
  2009. if(typeof options.from === 'string' && /\d+/.test(options.from)){
  2010. options.from = parseInt(options.from);
  2011. }
  2012. if(Number.isInteger(options.from)){
  2013. if(options.from < 0){
  2014. throw new Error(`Invalid Option: from must be a positive integer, got ${JSON.stringify(opts.from)}`);
  2015. }
  2016. }else {
  2017. throw new Error(`Invalid Option: from must be an integer, got ${JSON.stringify(options.from)}`);
  2018. }
  2019. }
  2020. // Normalize option `from_line`
  2021. if(options.from_line === undefined || options.from_line === null){
  2022. options.from_line = 1;
  2023. }else {
  2024. if(typeof options.from_line === 'string' && /\d+/.test(options.from_line)){
  2025. options.from_line = parseInt(options.from_line);
  2026. }
  2027. if(Number.isInteger(options.from_line)){
  2028. if(options.from_line <= 0){
  2029. throw new Error(`Invalid Option: from_line must be a positive integer greater than 0, got ${JSON.stringify(opts.from_line)}`);
  2030. }
  2031. }else {
  2032. throw new Error(`Invalid Option: from_line must be an integer, got ${JSON.stringify(opts.from_line)}`);
  2033. }
  2034. }
  2035. // Normalize options `ignore_last_delimiters`
  2036. if(options.ignore_last_delimiters === undefined || options.ignore_last_delimiters === null){
  2037. options.ignore_last_delimiters = false;
  2038. }else if(typeof options.ignore_last_delimiters === 'number'){
  2039. options.ignore_last_delimiters = Math.floor(options.ignore_last_delimiters);
  2040. if(options.ignore_last_delimiters === 0){
  2041. options.ignore_last_delimiters = false;
  2042. }
  2043. }else if(typeof options.ignore_last_delimiters !== 'boolean'){
  2044. throw new CsvError('CSV_INVALID_OPTION_IGNORE_LAST_DELIMITERS', [
  2045. 'Invalid option `ignore_last_delimiters`:',
  2046. 'the value must be a boolean value or an integer,',
  2047. `got ${JSON.stringify(options.ignore_last_delimiters)}`
  2048. ], options);
  2049. }
  2050. if(options.ignore_last_delimiters === true && options.columns === false){
  2051. throw new CsvError('CSV_IGNORE_LAST_DELIMITERS_REQUIRES_COLUMNS', [
  2052. 'The option `ignore_last_delimiters`',
  2053. 'requires the activation of the `columns` option'
  2054. ], options);
  2055. }
  2056. // Normalize option `info`
  2057. if(options.info === undefined || options.info === null || options.info === false){
  2058. options.info = false;
  2059. }else if(options.info !== true){
  2060. throw new Error(`Invalid Option: info must be true, got ${JSON.stringify(options.info)}`);
  2061. }
  2062. // Normalize option `max_record_size`
  2063. if(options.max_record_size === undefined || options.max_record_size === null || options.max_record_size === false){
  2064. options.max_record_size = 0;
  2065. }else if(Number.isInteger(options.max_record_size) && options.max_record_size >= 0);else if(typeof options.max_record_size === 'string' && /\d+/.test(options.max_record_size)){
  2066. options.max_record_size = parseInt(options.max_record_size);
  2067. }else {
  2068. throw new Error(`Invalid Option: max_record_size must be a positive integer, got ${JSON.stringify(options.max_record_size)}`);
  2069. }
  2070. // Normalize option `objname`
  2071. if(options.objname === undefined || options.objname === null || options.objname === false){
  2072. options.objname = undefined;
  2073. }else if(isBuffer(options.objname)){
  2074. if(options.objname.length === 0){
  2075. throw new Error(`Invalid Option: objname must be a non empty buffer`);
  2076. }
  2077. if(options.encoding === null);else {
  2078. options.objname = options.objname.toString(options.encoding);
  2079. }
  2080. }else if(typeof options.objname === 'string'){
  2081. if(options.objname.length === 0){
  2082. throw new Error(`Invalid Option: objname must be a non empty string`);
  2083. }
  2084. // Great, nothing to do
  2085. }else if(typeof options.objname === 'number');else {
  2086. throw new Error(`Invalid Option: objname must be a string or a buffer, got ${options.objname}`);
  2087. }
  2088. if(options.objname !== undefined){
  2089. if(typeof options.objname === 'number'){
  2090. if(options.columns !== false){
  2091. throw Error('Invalid Option: objname index cannot be combined with columns or be defined as a field');
  2092. }
  2093. }else { // A string or a buffer
  2094. if(options.columns === false){
  2095. throw Error('Invalid Option: objname field must be combined with columns or be defined as an index');
  2096. }
  2097. }
  2098. }
  2099. // Normalize option `on_record`
  2100. if(options.on_record === undefined || options.on_record === null){
  2101. options.on_record = undefined;
  2102. }else if(typeof options.on_record !== 'function'){
  2103. throw new CsvError('CSV_INVALID_OPTION_ON_RECORD', [
  2104. 'Invalid option `on_record`:',
  2105. 'expect a function,',
  2106. `got ${JSON.stringify(options.on_record)}`
  2107. ], options);
  2108. }
  2109. // Normalize option `quote`
  2110. if(options.quote === null || options.quote === false || options.quote === ''){
  2111. options.quote = null;
  2112. }else {
  2113. if(options.quote === undefined || options.quote === true){
  2114. options.quote = Buffer.from('"', options.encoding);
  2115. }else if(typeof options.quote === 'string'){
  2116. options.quote = Buffer.from(options.quote, options.encoding);
  2117. }
  2118. if(!isBuffer(options.quote)){
  2119. throw new Error(`Invalid Option: quote must be a buffer or a string, got ${JSON.stringify(options.quote)}`);
  2120. }
  2121. }
  2122. // Normalize option `raw`
  2123. if(options.raw === undefined || options.raw === null || options.raw === false){
  2124. options.raw = false;
  2125. }else if(options.raw !== true){
  2126. throw new Error(`Invalid Option: raw must be true, got ${JSON.stringify(options.raw)}`);
  2127. }
  2128. // Normalize option `record_delimiter`
  2129. if(options.record_delimiter === undefined){
  2130. options.record_delimiter = [];
  2131. }else if(typeof options.record_delimiter === 'string' || isBuffer(options.record_delimiter)){
  2132. if(options.record_delimiter.length === 0){
  2133. throw new CsvError('CSV_INVALID_OPTION_RECORD_DELIMITER', [
  2134. 'Invalid option `record_delimiter`:',
  2135. 'value must be a non empty string or buffer,',
  2136. `got ${JSON.stringify(options.record_delimiter)}`
  2137. ], options);
  2138. }
  2139. options.record_delimiter = [options.record_delimiter];
  2140. }else if(!Array.isArray(options.record_delimiter)){
  2141. throw new CsvError('CSV_INVALID_OPTION_RECORD_DELIMITER', [
  2142. 'Invalid option `record_delimiter`:',
  2143. 'value must be a string, a buffer or array of string|buffer,',
  2144. `got ${JSON.stringify(options.record_delimiter)}`
  2145. ], options);
  2146. }
  2147. options.record_delimiter = options.record_delimiter.map(function(rd, i){
  2148. if(typeof rd !== 'string' && ! isBuffer(rd)){
  2149. throw new CsvError('CSV_INVALID_OPTION_RECORD_DELIMITER', [
  2150. 'Invalid option `record_delimiter`:',
  2151. 'value must be a string, a buffer or array of string|buffer',
  2152. `at index ${i},`,
  2153. `got ${JSON.stringify(rd)}`
  2154. ], options);
  2155. }else if(rd.length === 0){
  2156. throw new CsvError('CSV_INVALID_OPTION_RECORD_DELIMITER', [
  2157. 'Invalid option `record_delimiter`:',
  2158. 'value must be a non empty string or buffer',
  2159. `at index ${i},`,
  2160. `got ${JSON.stringify(rd)}`
  2161. ], options);
  2162. }
  2163. if(typeof rd === 'string'){
  2164. rd = Buffer.from(rd, options.encoding);
  2165. }
  2166. return rd;
  2167. });
  2168. // Normalize option `relax_column_count`
  2169. if(typeof options.relax_column_count === 'boolean');else if(options.relax_column_count === undefined || options.relax_column_count === null){
  2170. options.relax_column_count = false;
  2171. }else {
  2172. throw new Error(`Invalid Option: relax_column_count must be a boolean, got ${JSON.stringify(options.relax_column_count)}`);
  2173. }
  2174. if(typeof options.relax_column_count_less === 'boolean');else if(options.relax_column_count_less === undefined || options.relax_column_count_less === null){
  2175. options.relax_column_count_less = false;
  2176. }else {
  2177. throw new Error(`Invalid Option: relax_column_count_less must be a boolean, got ${JSON.stringify(options.relax_column_count_less)}`);
  2178. }
  2179. if(typeof options.relax_column_count_more === 'boolean');else if(options.relax_column_count_more === undefined || options.relax_column_count_more === null){
  2180. options.relax_column_count_more = false;
  2181. }else {
  2182. throw new Error(`Invalid Option: relax_column_count_more must be a boolean, got ${JSON.stringify(options.relax_column_count_more)}`);
  2183. }
  2184. // Normalize option `relax_quotes`
  2185. if(typeof options.relax_quotes === 'boolean');else if(options.relax_quotes === undefined || options.relax_quotes === null){
  2186. options.relax_quotes = false;
  2187. }else {
  2188. throw new Error(`Invalid Option: relax_quotes must be a boolean, got ${JSON.stringify(options.relax_quotes)}`);
  2189. }
  2190. // Normalize option `skip_empty_lines`
  2191. if(typeof options.skip_empty_lines === 'boolean');else if(options.skip_empty_lines === undefined || options.skip_empty_lines === null){
  2192. options.skip_empty_lines = false;
  2193. }else {
  2194. throw new Error(`Invalid Option: skip_empty_lines must be a boolean, got ${JSON.stringify(options.skip_empty_lines)}`);
  2195. }
  2196. // Normalize option `skip_records_with_empty_values`
  2197. if(typeof options.skip_records_with_empty_values === 'boolean');else if(options.skip_records_with_empty_values === undefined || options.skip_records_with_empty_values === null){
  2198. options.skip_records_with_empty_values = false;
  2199. }else {
  2200. throw new Error(`Invalid Option: skip_records_with_empty_values must be a boolean, got ${JSON.stringify(options.skip_records_with_empty_values)}`);
  2201. }
  2202. // Normalize option `skip_records_with_error`
  2203. if(typeof options.skip_records_with_error === 'boolean');else if(options.skip_records_with_error === undefined || options.skip_records_with_error === null){
  2204. options.skip_records_with_error = false;
  2205. }else {
  2206. throw new Error(`Invalid Option: skip_records_with_error must be a boolean, got ${JSON.stringify(options.skip_records_with_error)}`);
  2207. }
  2208. // Normalize option `rtrim`
  2209. if(options.rtrim === undefined || options.rtrim === null || options.rtrim === false){
  2210. options.rtrim = false;
  2211. }else if(options.rtrim !== true){
  2212. throw new Error(`Invalid Option: rtrim must be a boolean, got ${JSON.stringify(options.rtrim)}`);
  2213. }
  2214. // Normalize option `ltrim`
  2215. if(options.ltrim === undefined || options.ltrim === null || options.ltrim === false){
  2216. options.ltrim = false;
  2217. }else if(options.ltrim !== true){
  2218. throw new Error(`Invalid Option: ltrim must be a boolean, got ${JSON.stringify(options.ltrim)}`);
  2219. }
  2220. // Normalize option `trim`
  2221. if(options.trim === undefined || options.trim === null || options.trim === false){
  2222. options.trim = false;
  2223. }else if(options.trim !== true){
  2224. throw new Error(`Invalid Option: trim must be a boolean, got ${JSON.stringify(options.trim)}`);
  2225. }
  2226. // Normalize options `trim`, `ltrim` and `rtrim`
  2227. if(options.trim === true && opts.ltrim !== false){
  2228. options.ltrim = true;
  2229. }else if(options.ltrim !== true){
  2230. options.ltrim = false;
  2231. }
  2232. if(options.trim === true && opts.rtrim !== false){
  2233. options.rtrim = true;
  2234. }else if(options.rtrim !== true){
  2235. options.rtrim = false;
  2236. }
  2237. // Normalize option `to`
  2238. if(options.to === undefined || options.to === null){
  2239. options.to = -1;
  2240. }else {
  2241. if(typeof options.to === 'string' && /\d+/.test(options.to)){
  2242. options.to = parseInt(options.to);
  2243. }
  2244. if(Number.isInteger(options.to)){
  2245. if(options.to <= 0){
  2246. throw new Error(`Invalid Option: to must be a positive integer greater than 0, got ${JSON.stringify(opts.to)}`);
  2247. }
  2248. }else {
  2249. throw new Error(`Invalid Option: to must be an integer, got ${JSON.stringify(opts.to)}`);
  2250. }
  2251. }
  2252. // Normalize option `to_line`
  2253. if(options.to_line === undefined || options.to_line === null){
  2254. options.to_line = -1;
  2255. }else {
  2256. if(typeof options.to_line === 'string' && /\d+/.test(options.to_line)){
  2257. options.to_line = parseInt(options.to_line);
  2258. }
  2259. if(Number.isInteger(options.to_line)){
  2260. if(options.to_line <= 0){
  2261. throw new Error(`Invalid Option: to_line must be a positive integer greater than 0, got ${JSON.stringify(opts.to_line)}`);
  2262. }
  2263. }else {
  2264. throw new Error(`Invalid Option: to_line must be an integer, got ${JSON.stringify(opts.to_line)}`);
  2265. }
  2266. }
  2267. return options;
  2268. };
  2269. const isRecordEmpty = function(record){
  2270. return record.every((field) => field == null || field.toString && field.toString().trim() === '');
  2271. };
  2272. const cr = 13; // `\r`, carriage return, 0x0D in hexadécimal, 13 in decimal
  2273. const nl = 10; // `\n`, newline, 0x0A in hexadecimal, 10 in decimal
  2274. const boms = {
  2275. // Note, the following are equals:
  2276. // Buffer.from("\ufeff")
  2277. // Buffer.from([239, 187, 191])
  2278. // Buffer.from('EFBBBF', 'hex')
  2279. 'utf8': Buffer.from([239, 187, 191]),
  2280. // Note, the following are equals:
  2281. // Buffer.from "\ufeff", 'utf16le
  2282. // Buffer.from([255, 254])
  2283. 'utf16le': Buffer.from([255, 254])
  2284. };
  2285. const transform = function(original_options = {}) {
  2286. const info = {
  2287. bytes: 0,
  2288. comment_lines: 0,
  2289. empty_lines: 0,
  2290. invalid_field_length: 0,
  2291. lines: 1,
  2292. records: 0
  2293. };
  2294. const options = normalize_options(original_options);
  2295. return {
  2296. info: info,
  2297. original_options: original_options,
  2298. options: options,
  2299. state: init_state(options),
  2300. __needMoreData: function(i, bufLen, end){
  2301. if(end) return false;
  2302. const {encoding, escape, quote} = this.options;
  2303. const {quoting, needMoreDataSize, recordDelimiterMaxLength} = this.state;
  2304. const numOfCharLeft = bufLen - i - 1;
  2305. const requiredLength = Math.max(
  2306. needMoreDataSize,
  2307. // Skip if the remaining buffer smaller than record delimiter
  2308. // If "record_delimiter" is yet to be discovered:
  2309. // 1. It is equals to `[]` and "recordDelimiterMaxLength" equals `0`
  2310. // 2. We set the length to windows line ending in the current encoding
  2311. // Note, that encoding is known from user or bom discovery at that point
  2312. // recordDelimiterMaxLength,
  2313. recordDelimiterMaxLength === 0 ? Buffer.from('\r\n', encoding).length : recordDelimiterMaxLength,
  2314. // Skip if remaining buffer can be an escaped quote
  2315. quoting ? ((escape === null ? 0 : escape.length) + quote.length) : 0,
  2316. // Skip if remaining buffer can be record delimiter following the closing quote
  2317. quoting ? (quote.length + recordDelimiterMaxLength) : 0,
  2318. );
  2319. return numOfCharLeft < requiredLength;
  2320. },
  2321. // Central parser implementation
  2322. parse: function(nextBuf, end, push, close){
  2323. const {bom, comment_no_infix, encoding, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
  2324. let {comment, escape, quote, record_delimiter} = this.options;
  2325. const {bomSkipped, previousBuf, rawBuffer, escapeIsQuote} = this.state;
  2326. let buf;
  2327. if(previousBuf === undefined){
  2328. if(nextBuf === undefined){
  2329. // Handle empty string
  2330. close();
  2331. return;
  2332. }else {
  2333. buf = nextBuf;
  2334. }
  2335. }else if(previousBuf !== undefined && nextBuf === undefined){
  2336. buf = previousBuf;
  2337. }else {
  2338. buf = Buffer.concat([previousBuf, nextBuf]);
  2339. }
  2340. // Handle UTF BOM
  2341. if(bomSkipped === false){
  2342. if(bom === false){
  2343. this.state.bomSkipped = true;
  2344. }else if(buf.length < 3){
  2345. // No enough data
  2346. if(end === false){
  2347. // Wait for more data
  2348. this.state.previousBuf = buf;
  2349. return;
  2350. }
  2351. }else {
  2352. for(const encoding in boms){
  2353. if(boms[encoding].compare(buf, 0, boms[encoding].length) === 0){
  2354. // Skip BOM
  2355. const bomLength = boms[encoding].length;
  2356. this.state.bufBytesStart += bomLength;
  2357. buf = buf.slice(bomLength);
  2358. // Renormalize original options with the new encoding
  2359. this.options = normalize_options({...this.original_options, encoding: encoding});
  2360. // Options will re-evaluate the Buffer with the new encoding
  2361. ({comment, escape, quote } = this.options);
  2362. break;
  2363. }
  2364. }
  2365. this.state.bomSkipped = true;
  2366. }
  2367. }
  2368. const bufLen = buf.length;
  2369. let pos;
  2370. for(pos = 0; pos < bufLen; pos++){
  2371. // Ensure we get enough space to look ahead
  2372. // There should be a way to move this out of the loop
  2373. if(this.__needMoreData(pos, bufLen, end)){
  2374. break;
  2375. }
  2376. if(this.state.wasRowDelimiter === true){
  2377. this.info.lines++;
  2378. this.state.wasRowDelimiter = false;
  2379. }
  2380. if(to_line !== -1 && this.info.lines > to_line){
  2381. this.state.stop = true;
  2382. close();
  2383. return;
  2384. }
  2385. // Auto discovery of record_delimiter, unix, mac and windows supported
  2386. if(this.state.quoting === false && record_delimiter.length === 0){
  2387. const record_delimiterCount = this.__autoDiscoverRecordDelimiter(buf, pos);
  2388. if(record_delimiterCount){
  2389. record_delimiter = this.options.record_delimiter;
  2390. }
  2391. }
  2392. const chr = buf[pos];
  2393. if(raw === true){
  2394. rawBuffer.append(chr);
  2395. }
  2396. if((chr === cr || chr === nl) && this.state.wasRowDelimiter === false){
  2397. this.state.wasRowDelimiter = true;
  2398. }
  2399. // Previous char was a valid escape char
  2400. // treat the current char as a regular char
  2401. if(this.state.escaping === true){
  2402. this.state.escaping = false;
  2403. }else {
  2404. // Escape is only active inside quoted fields
  2405. // We are quoting, the char is an escape chr and there is a chr to escape
  2406. // if(escape !== null && this.state.quoting === true && chr === escape && pos + 1 < bufLen){
  2407. if(escape !== null && this.state.quoting === true && this.__isEscape(buf, pos, chr) && pos + escape.length < bufLen){
  2408. if(escapeIsQuote){
  2409. if(this.__isQuote(buf, pos+escape.length)){
  2410. this.state.escaping = true;
  2411. pos += escape.length - 1;
  2412. continue;
  2413. }
  2414. }else {
  2415. this.state.escaping = true;
  2416. pos += escape.length - 1;
  2417. continue;
  2418. }
  2419. }
  2420. // Not currently escaping and chr is a quote
  2421. // TODO: need to compare bytes instead of single char
  2422. if(this.state.commenting === false && this.__isQuote(buf, pos)){
  2423. if(this.state.quoting === true){
  2424. const nextChr = buf[pos+quote.length];
  2425. const isNextChrTrimable = rtrim && this.__isCharTrimable(buf, pos+quote.length);
  2426. const isNextChrComment = comment !== null && this.__compareBytes(comment, buf, pos+quote.length, nextChr);
  2427. const isNextChrDelimiter = this.__isDelimiter(buf, pos+quote.length, nextChr);
  2428. const isNextChrRecordDelimiter = record_delimiter.length === 0 ? this.__autoDiscoverRecordDelimiter(buf, pos+quote.length) : this.__isRecordDelimiter(nextChr, buf, pos+quote.length);
  2429. // Escape a quote
  2430. // Treat next char as a regular character
  2431. if(escape !== null && this.__isEscape(buf, pos, chr) && this.__isQuote(buf, pos + escape.length)){
  2432. pos += escape.length - 1;
  2433. }else if(!nextChr || isNextChrDelimiter || isNextChrRecordDelimiter || isNextChrComment || isNextChrTrimable){
  2434. this.state.quoting = false;
  2435. this.state.wasQuoting = true;
  2436. pos += quote.length - 1;
  2437. continue;
  2438. }else if(relax_quotes === false){
  2439. const err = this.__error(
  2440. new CsvError('CSV_INVALID_CLOSING_QUOTE', [
  2441. 'Invalid Closing Quote:',
  2442. `got "${String.fromCharCode(nextChr)}"`,
  2443. `at line ${this.info.lines}`,
  2444. 'instead of delimiter, record delimiter, trimable character',
  2445. '(if activated) or comment',
  2446. ], this.options, this.__infoField())
  2447. );
  2448. if(err !== undefined) return err;
  2449. }else {
  2450. this.state.quoting = false;
  2451. this.state.wasQuoting = true;
  2452. this.state.field.prepend(quote);
  2453. pos += quote.length - 1;
  2454. }
  2455. }else {
  2456. if(this.state.field.length !== 0){
  2457. // In relax_quotes mode, treat opening quote preceded by chrs as regular
  2458. if(relax_quotes === false){
  2459. const info = this.__infoField();
  2460. const bom = Object.keys(boms).map(b => boms[b].equals(this.state.field.toString()) ? b : false).filter(Boolean)[0];
  2461. const err = this.__error(
  2462. new CsvError('INVALID_OPENING_QUOTE', [
  2463. 'Invalid Opening Quote:',
  2464. `a quote is found on field ${JSON.stringify(info.column)} at line ${info.lines}, value is ${JSON.stringify(this.state.field.toString(encoding))}`,
  2465. bom ? `(${bom} bom)` : undefined
  2466. ], this.options, info, {
  2467. field: this.state.field,
  2468. })
  2469. );
  2470. if(err !== undefined) return err;
  2471. }
  2472. }else {
  2473. this.state.quoting = true;
  2474. pos += quote.length - 1;
  2475. continue;
  2476. }
  2477. }
  2478. }
  2479. if(this.state.quoting === false){
  2480. const recordDelimiterLength = this.__isRecordDelimiter(chr, buf, pos);
  2481. if(recordDelimiterLength !== 0){
  2482. // Do not emit comments which take a full line
  2483. const skipCommentLine = this.state.commenting && (this.state.wasQuoting === false && this.state.record.length === 0 && this.state.field.length === 0);
  2484. if(skipCommentLine){
  2485. this.info.comment_lines++;
  2486. // Skip full comment line
  2487. }else {
  2488. // Activate records emition if above from_line
  2489. if(this.state.enabled === false && this.info.lines + (this.state.wasRowDelimiter === true ? 1: 0) >= from_line){
  2490. this.state.enabled = true;
  2491. this.__resetField();
  2492. this.__resetRecord();
  2493. pos += recordDelimiterLength - 1;
  2494. continue;
  2495. }
  2496. // Skip if line is empty and skip_empty_lines activated
  2497. if(skip_empty_lines === true && this.state.wasQuoting === false && this.state.record.length === 0 && this.state.field.length === 0){
  2498. this.info.empty_lines++;
  2499. pos += recordDelimiterLength - 1;
  2500. continue;
  2501. }
  2502. this.info.bytes = this.state.bufBytesStart + pos;
  2503. const errField = this.__onField();
  2504. if(errField !== undefined) return errField;
  2505. this.info.bytes = this.state.bufBytesStart + pos + recordDelimiterLength;
  2506. const errRecord = this.__onRecord(push);
  2507. if(errRecord !== undefined) return errRecord;
  2508. if(to !== -1 && this.info.records >= to){
  2509. this.state.stop = true;
  2510. close();
  2511. return;
  2512. }
  2513. }
  2514. this.state.commenting = false;
  2515. pos += recordDelimiterLength - 1;
  2516. continue;
  2517. }
  2518. if(this.state.commenting){
  2519. continue;
  2520. }
  2521. const commentCount = comment === null ? 0 : this.__compareBytes(comment, buf, pos, chr);
  2522. if(commentCount !== 0 && (comment_no_infix === false || this.state.field.length === 0)){
  2523. this.state.commenting = true;
  2524. continue;
  2525. }
  2526. const delimiterLength = this.__isDelimiter(buf, pos, chr);
  2527. if(delimiterLength !== 0){
  2528. this.info.bytes = this.state.bufBytesStart + pos;
  2529. const errField = this.__onField();
  2530. if(errField !== undefined) return errField;
  2531. pos += delimiterLength - 1;
  2532. continue;
  2533. }
  2534. }
  2535. }
  2536. if(this.state.commenting === false){
  2537. if(max_record_size !== 0 && this.state.record_length + this.state.field.length > max_record_size){
  2538. return this.__error(
  2539. new CsvError('CSV_MAX_RECORD_SIZE', [
  2540. 'Max Record Size:',
  2541. 'record exceed the maximum number of tolerated bytes',
  2542. `of ${max_record_size}`,
  2543. `at line ${this.info.lines}`,
  2544. ], this.options, this.__infoField())
  2545. );
  2546. }
  2547. }
  2548. const lappend = ltrim === false || this.state.quoting === true || this.state.field.length !== 0 || !this.__isCharTrimable(buf, pos);
  2549. // rtrim in non quoting is handle in __onField
  2550. const rappend = rtrim === false || this.state.wasQuoting === false;
  2551. if(lappend === true && rappend === true){
  2552. this.state.field.append(chr);
  2553. }else if(rtrim === true && !this.__isCharTrimable(buf, pos)){
  2554. return this.__error(
  2555. new CsvError('CSV_NON_TRIMABLE_CHAR_AFTER_CLOSING_QUOTE', [
  2556. 'Invalid Closing Quote:',
  2557. 'found non trimable byte after quote',
  2558. `at line ${this.info.lines}`,
  2559. ], this.options, this.__infoField())
  2560. );
  2561. }else {
  2562. if(lappend === false){
  2563. pos += this.__isCharTrimable(buf, pos) - 1;
  2564. }
  2565. continue;
  2566. }
  2567. }
  2568. if(end === true){
  2569. // Ensure we are not ending in a quoting state
  2570. if(this.state.quoting === true){
  2571. const err = this.__error(
  2572. new CsvError('CSV_QUOTE_NOT_CLOSED', [
  2573. 'Quote Not Closed:',
  2574. `the parsing is finished with an opening quote at line ${this.info.lines}`,
  2575. ], this.options, this.__infoField())
  2576. );
  2577. if(err !== undefined) return err;
  2578. }else {
  2579. // Skip last line if it has no characters
  2580. if(this.state.wasQuoting === true || this.state.record.length !== 0 || this.state.field.length !== 0){
  2581. this.info.bytes = this.state.bufBytesStart + pos;
  2582. const errField = this.__onField();
  2583. if(errField !== undefined) return errField;
  2584. const errRecord = this.__onRecord(push);
  2585. if(errRecord !== undefined) return errRecord;
  2586. }else if(this.state.wasRowDelimiter === true){
  2587. this.info.empty_lines++;
  2588. }else if(this.state.commenting === true){
  2589. this.info.comment_lines++;
  2590. }
  2591. }
  2592. }else {
  2593. this.state.bufBytesStart += pos;
  2594. this.state.previousBuf = buf.slice(pos);
  2595. }
  2596. if(this.state.wasRowDelimiter === true){
  2597. this.info.lines++;
  2598. this.state.wasRowDelimiter = false;
  2599. }
  2600. },
  2601. __onRecord: function(push){
  2602. const {columns, group_columns_by_name, encoding, info, from, relax_column_count, relax_column_count_less, relax_column_count_more, raw, skip_records_with_empty_values} = this.options;
  2603. const {enabled, record} = this.state;
  2604. if(enabled === false){
  2605. return this.__resetRecord();
  2606. }
  2607. // Convert the first line into column names
  2608. const recordLength = record.length;
  2609. if(columns === true){
  2610. if(skip_records_with_empty_values === true && isRecordEmpty(record)){
  2611. this.__resetRecord();
  2612. return;
  2613. }
  2614. return this.__firstLineToColumns(record);
  2615. }
  2616. if(columns === false && this.info.records === 0){
  2617. this.state.expectedRecordLength = recordLength;
  2618. }
  2619. if(recordLength !== this.state.expectedRecordLength){
  2620. const err = columns === false ?
  2621. new CsvError('CSV_RECORD_INCONSISTENT_FIELDS_LENGTH', [
  2622. 'Invalid Record Length:',
  2623. `expect ${this.state.expectedRecordLength},`,
  2624. `got ${recordLength} on line ${this.info.lines}`,
  2625. ], this.options, this.__infoField(), {
  2626. record: record,
  2627. })
  2628. :
  2629. new CsvError('CSV_RECORD_INCONSISTENT_COLUMNS', [
  2630. 'Invalid Record Length:',
  2631. `columns length is ${columns.length},`, // rename columns
  2632. `got ${recordLength} on line ${this.info.lines}`,
  2633. ], this.options, this.__infoField(), {
  2634. record: record,
  2635. });
  2636. if(relax_column_count === true ||
  2637. (relax_column_count_less === true && recordLength < this.state.expectedRecordLength) ||
  2638. (relax_column_count_more === true && recordLength > this.state.expectedRecordLength)){
  2639. this.info.invalid_field_length++;
  2640. this.state.error = err;
  2641. // Error is undefined with skip_records_with_error
  2642. }else {
  2643. const finalErr = this.__error(err);
  2644. if(finalErr) return finalErr;
  2645. }
  2646. }
  2647. if(skip_records_with_empty_values === true && isRecordEmpty(record)){
  2648. this.__resetRecord();
  2649. return;
  2650. }
  2651. if(this.state.recordHasError === true){
  2652. this.__resetRecord();
  2653. this.state.recordHasError = false;
  2654. return;
  2655. }
  2656. this.info.records++;
  2657. if(from === 1 || this.info.records >= from){
  2658. const {objname} = this.options;
  2659. // With columns, records are object
  2660. if(columns !== false){
  2661. const obj = {};
  2662. // Transform record array to an object
  2663. for(let i = 0, l = record.length; i < l; i++){
  2664. if(columns[i] === undefined || columns[i].disabled) continue;
  2665. // Turn duplicate columns into an array
  2666. if (group_columns_by_name === true && obj[columns[i].name] !== undefined) {
  2667. if (Array.isArray(obj[columns[i].name])) {
  2668. obj[columns[i].name] = obj[columns[i].name].concat(record[i]);
  2669. } else {
  2670. obj[columns[i].name] = [obj[columns[i].name], record[i]];
  2671. }
  2672. } else {
  2673. obj[columns[i].name] = record[i];
  2674. }
  2675. }
  2676. // Without objname (default)
  2677. if(raw === true || info === true){
  2678. const extRecord = Object.assign(
  2679. {record: obj},
  2680. (raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}),
  2681. (info === true ? {info: this.__infoRecord()}: {})
  2682. );
  2683. const err = this.__push(
  2684. objname === undefined ? extRecord : [obj[objname], extRecord]
  2685. , push);
  2686. if(err){
  2687. return err;
  2688. }
  2689. }else {
  2690. const err = this.__push(
  2691. objname === undefined ? obj : [obj[objname], obj]
  2692. , push);
  2693. if(err){
  2694. return err;
  2695. }
  2696. }
  2697. // Without columns, records are array
  2698. }else {
  2699. if(raw === true || info === true){
  2700. const extRecord = Object.assign(
  2701. {record: record},
  2702. raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {},
  2703. info === true ? {info: this.__infoRecord()}: {}
  2704. );
  2705. const err = this.__push(
  2706. objname === undefined ? extRecord : [record[objname], extRecord]
  2707. , push);
  2708. if(err){
  2709. return err;
  2710. }
  2711. }else {
  2712. const err = this.__push(
  2713. objname === undefined ? record : [record[objname], record]
  2714. , push);
  2715. if(err){
  2716. return err;
  2717. }
  2718. }
  2719. }
  2720. }
  2721. this.__resetRecord();
  2722. },
  2723. __firstLineToColumns: function(record){
  2724. const {firstLineToHeaders} = this.state;
  2725. try{
  2726. const headers = firstLineToHeaders === undefined ? record : firstLineToHeaders.call(null, record);
  2727. if(!Array.isArray(headers)){
  2728. return this.__error(
  2729. new CsvError('CSV_INVALID_COLUMN_MAPPING', [
  2730. 'Invalid Column Mapping:',
  2731. 'expect an array from column function,',
  2732. `got ${JSON.stringify(headers)}`
  2733. ], this.options, this.__infoField(), {
  2734. headers: headers,
  2735. })
  2736. );
  2737. }
  2738. const normalizedHeaders = normalize_columns_array(headers);
  2739. this.state.expectedRecordLength = normalizedHeaders.length;
  2740. this.options.columns = normalizedHeaders;
  2741. this.__resetRecord();
  2742. return;
  2743. }catch(err){
  2744. return err;
  2745. }
  2746. },
  2747. __resetRecord: function(){
  2748. if(this.options.raw === true){
  2749. this.state.rawBuffer.reset();
  2750. }
  2751. this.state.error = undefined;
  2752. this.state.record = [];
  2753. this.state.record_length = 0;
  2754. },
  2755. __onField: function(){
  2756. const {cast, encoding, rtrim, max_record_size} = this.options;
  2757. const {enabled, wasQuoting} = this.state;
  2758. // Short circuit for the from_line options
  2759. if(enabled === false){
  2760. return this.__resetField();
  2761. }
  2762. let field = this.state.field.toString(encoding);
  2763. if(rtrim === true && wasQuoting === false){
  2764. field = field.trimRight();
  2765. }
  2766. if(cast === true){
  2767. const [err, f] = this.__cast(field);
  2768. if(err !== undefined) return err;
  2769. field = f;
  2770. }
  2771. this.state.record.push(field);
  2772. // Increment record length if record size must not exceed a limit
  2773. if(max_record_size !== 0 && typeof field === 'string'){
  2774. this.state.record_length += field.length;
  2775. }
  2776. this.__resetField();
  2777. },
  2778. __resetField: function(){
  2779. this.state.field.reset();
  2780. this.state.wasQuoting = false;
  2781. },
  2782. __push: function(record, push){
  2783. const {on_record} = this.options;
  2784. if(on_record !== undefined){
  2785. const info = this.__infoRecord();
  2786. try{
  2787. record = on_record.call(null, record, info);
  2788. }catch(err){
  2789. return err;
  2790. }
  2791. if(record === undefined || record === null){ return; }
  2792. }
  2793. push(record);
  2794. },
  2795. // Return a tuple with the error and the casted value
  2796. __cast: function(field){
  2797. const {columns, relax_column_count} = this.options;
  2798. const isColumns = Array.isArray(columns);
  2799. // Dont loose time calling cast
  2800. // because the final record is an object
  2801. // and this field can't be associated to a key present in columns
  2802. if(isColumns === true && relax_column_count && this.options.columns.length <= this.state.record.length){
  2803. return [undefined, undefined];
  2804. }
  2805. if(this.state.castField !== null){
  2806. try{
  2807. const info = this.__infoField();
  2808. return [undefined, this.state.castField.call(null, field, info)];
  2809. }catch(err){
  2810. return [err];
  2811. }
  2812. }
  2813. if(this.__isFloat(field)){
  2814. return [undefined, parseFloat(field)];
  2815. }else if(this.options.cast_date !== false){
  2816. const info = this.__infoField();
  2817. return [undefined, this.options.cast_date.call(null, field, info)];
  2818. }
  2819. return [undefined, field];
  2820. },
  2821. // Helper to test if a character is a space or a line delimiter
  2822. __isCharTrimable: function(buf, pos){
  2823. const isTrim = (buf, pos) => {
  2824. const {timchars} = this.state;
  2825. loop1: for(let i = 0; i < timchars.length; i++){
  2826. const timchar = timchars[i];
  2827. for(let j = 0; j < timchar.length; j++){
  2828. if(timchar[j] !== buf[pos+j]) continue loop1;
  2829. }
  2830. return timchar.length;
  2831. }
  2832. return 0;
  2833. };
  2834. return isTrim(buf, pos);
  2835. },
  2836. // Keep it in case we implement the `cast_int` option
  2837. // __isInt(value){
  2838. // // return Number.isInteger(parseInt(value))
  2839. // // return !isNaN( parseInt( obj ) );
  2840. // return /^(\-|\+)?[1-9][0-9]*$/.test(value)
  2841. // }
  2842. __isFloat: function(value){
  2843. return (value - parseFloat(value) + 1) >= 0; // Borrowed from jquery
  2844. },
  2845. __compareBytes: function(sourceBuf, targetBuf, targetPos, firstByte){
  2846. if(sourceBuf[0] !== firstByte) return 0;
  2847. const sourceLength = sourceBuf.length;
  2848. for(let i = 1; i < sourceLength; i++){
  2849. if(sourceBuf[i] !== targetBuf[targetPos+i]) return 0;
  2850. }
  2851. return sourceLength;
  2852. },
  2853. __isDelimiter: function(buf, pos, chr){
  2854. const {delimiter, ignore_last_delimiters} = this.options;
  2855. if(ignore_last_delimiters === true && this.state.record.length === this.options.columns.length - 1){
  2856. return 0;
  2857. }else if(ignore_last_delimiters !== false && typeof ignore_last_delimiters === 'number' && this.state.record.length === ignore_last_delimiters - 1){
  2858. return 0;
  2859. }
  2860. loop1: for(let i = 0; i < delimiter.length; i++){
  2861. const del = delimiter[i];
  2862. if(del[0] === chr){
  2863. for(let j = 1; j < del.length; j++){
  2864. if(del[j] !== buf[pos+j]) continue loop1;
  2865. }
  2866. return del.length;
  2867. }
  2868. }
  2869. return 0;
  2870. },
  2871. __isRecordDelimiter: function(chr, buf, pos){
  2872. const {record_delimiter} = this.options;
  2873. const recordDelimiterLength = record_delimiter.length;
  2874. loop1: for(let i = 0; i < recordDelimiterLength; i++){
  2875. const rd = record_delimiter[i];
  2876. const rdLength = rd.length;
  2877. if(rd[0] !== chr){
  2878. continue;
  2879. }
  2880. for(let j = 1; j < rdLength; j++){
  2881. if(rd[j] !== buf[pos+j]){
  2882. continue loop1;
  2883. }
  2884. }
  2885. return rd.length;
  2886. }
  2887. return 0;
  2888. },
  2889. __isEscape: function(buf, pos, chr){
  2890. const {escape} = this.options;
  2891. if(escape === null) return false;
  2892. const l = escape.length;
  2893. if(escape[0] === chr){
  2894. for(let i = 0; i < l; i++){
  2895. if(escape[i] !== buf[pos+i]){
  2896. return false;
  2897. }
  2898. }
  2899. return true;
  2900. }
  2901. return false;
  2902. },
  2903. __isQuote: function(buf, pos){
  2904. const {quote} = this.options;
  2905. if(quote === null) return false;
  2906. const l = quote.length;
  2907. for(let i = 0; i < l; i++){
  2908. if(quote[i] !== buf[pos+i]){
  2909. return false;
  2910. }
  2911. }
  2912. return true;
  2913. },
  2914. __autoDiscoverRecordDelimiter: function(buf, pos){
  2915. const { encoding } = this.options;
  2916. // Note, we don't need to cache this information in state,
  2917. // It is only called on the first line until we find out a suitable
  2918. // record delimiter.
  2919. const rds = [
  2920. // Important, the windows line ending must be before mac os 9
  2921. Buffer.from('\r\n', encoding),
  2922. Buffer.from('\n', encoding),
  2923. Buffer.from('\r', encoding),
  2924. ];
  2925. loop: for(let i = 0; i < rds.length; i++){
  2926. const l = rds[i].length;
  2927. for(let j = 0; j < l; j++){
  2928. if(rds[i][j] !== buf[pos + j]){
  2929. continue loop;
  2930. }
  2931. }
  2932. this.options.record_delimiter.push(rds[i]);
  2933. this.state.recordDelimiterMaxLength = rds[i].length;
  2934. return rds[i].length;
  2935. }
  2936. return 0;
  2937. },
  2938. __error: function(msg){
  2939. const {encoding, raw, skip_records_with_error} = this.options;
  2940. const err = typeof msg === 'string' ? new Error(msg) : msg;
  2941. if(skip_records_with_error){
  2942. this.state.recordHasError = true;
  2943. if(this.options.on_skip !== undefined){
  2944. this.options.on_skip(err, raw ? this.state.rawBuffer.toString(encoding) : undefined);
  2945. }
  2946. // this.emit('skip', err, raw ? this.state.rawBuffer.toString(encoding) : undefined);
  2947. return undefined;
  2948. }else {
  2949. return err;
  2950. }
  2951. },
  2952. __infoDataSet: function(){
  2953. return {
  2954. ...this.info,
  2955. columns: this.options.columns
  2956. };
  2957. },
  2958. __infoRecord: function(){
  2959. const {columns, raw, encoding} = this.options;
  2960. return {
  2961. ...this.__infoDataSet(),
  2962. error: this.state.error,
  2963. header: columns === true,
  2964. index: this.state.record.length,
  2965. raw: raw ? this.state.rawBuffer.toString(encoding) : undefined
  2966. };
  2967. },
  2968. __infoField: function(){
  2969. const {columns} = this.options;
  2970. const isColumns = Array.isArray(columns);
  2971. return {
  2972. ...this.__infoRecord(),
  2973. column: isColumns === true ?
  2974. (columns.length > this.state.record.length ?
  2975. columns[this.state.record.length].name :
  2976. null
  2977. ) :
  2978. this.state.record.length,
  2979. quoting: this.state.wasQuoting,
  2980. };
  2981. }
  2982. };
  2983. };
  2984. const parse = function(data, opts={}){
  2985. if(typeof data === 'string'){
  2986. data = Buffer.from(data);
  2987. }
  2988. const records = opts && opts.objname ? {} : [];
  2989. const parser = transform(opts);
  2990. const push = (record) => {
  2991. if(parser.options.objname === undefined)
  2992. records.push(record);
  2993. else {
  2994. records[record[0]] = record[1];
  2995. }
  2996. };
  2997. const close = () => {};
  2998. const err1 = parser.parse(data, false, push, close);
  2999. if(err1 !== undefined) throw err1;
  3000. const err2 = parser.parse(undefined, true, push, close);
  3001. if(err2 !== undefined) throw err2;
  3002. return records;
  3003. };
  3004. exports.CsvError = CsvError;
  3005. exports.parse = parse;
  3006. return exports;
  3007. })({});