sync.js 140 KB

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