UNPKG

node-p5

Version:
1,419 lines (1,378 loc) 1.08 MB
const jsdom = require("jsdom"); const fs = require('fs'); const path = require('path'); const Jimp = require('jimp'); const isURL = require('is-url'); const axios = require('axios'); /* TODO loadJSON() -- done loadStrings() -- done loadTable() -- not a priority loadXML() -- not a priority loadBytes() -- not a priority httpGet() -- not doing anymore, just use axios or request httpPost() -- not doing anymore, just use axios or request httpDo() -- not doing anymore, just use axios or request createWriter() -- not a priority save() -- meh saveCanvas() -- done saveJSON() -- done saveStrings() -- done saveTable() -- not a priority .... registerMethod() -- done registerPlugin() -- done */ function pad(n, width, z) { z = z || '0'; n = n + ''; return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n; } const GIFEncoder = require('gifencoder'); const pngFileStream = require('png-file-stream'); const dom = new jsdom.JSDOM(`<!DOCTYPE html><html><head></head><body></body></html>`, { predendToBeVisual: true, runScripts: 'outside-only' }); const { window } = dom; const { document } = window; let Canvas = require('../jsdom/lib/jsdom/utils').Canvas; let mainSketch; let mainCanvas; let pp; module.exports = { loadFont: f => { if(typeof f == "object") { Canvas.registerFont(f.path, { family: f.family }); return f.family; } else { let filepath = f.split('.')[f.split('.').length-2]; let filepathparts = filepath.split(/\//g); let filename = filepathparts[filepathparts.length - 1]; Canvas.registerFont(f, { family: filename }); return filename; } }, loadImage: src => { return () => { return new Promise((resolve, reject) => { mainSketch.loadImage(src).then(img => { resolve(img); }) }); } }, loadJSON: f => { return () => { return new Promise((resolve, reject) => { mainSketch.loadJSON(f).then(data => { resolve(data); }); }); } }, loadStrings: f => { return () => { return new Promise((resolve, reject) => { mainSketch.loadStrings(f).then(data => { resolve(data); }); }); } }, registerMethod: (name, fnc) => { if(pp) { if(typeof name == 'string') { //DONT USE ARROW NOTATION FOR BIND TO WORK LOL fnc = fnc.bind({ p5: pp.prototype }); //add the function to the p5 instance Object.defineProperty(pp.prototype, name, { configurable: true, enumerable: true, value: fnc }); pp.prototype.registerMethod(name, pp.prototype[name]); //add the function to node-p5 Object.defineProperty(module.exports, name, { configurable: true, enumerable: true, value: () => { return () => { return fnc(); } } }); } } else throw new Error('p5 not defined?'); }, registerPlugin: obj => { Object.keys(obj).forEach(key => { module.exports.registerMethod(key, obj[key]); }); } } /*! p5.js v0.8.0 April 08, 2019 */ !(function(e) { pp = e(); // helper for writing color to array - https://p5js.org/reference/#/p5.Image function writeColor(image, width, x, y, col) { let index = (x + y * width) * 4; image.pixels[index] = col[0]; image.pixels[index + 1] = col[1]; image.pixels[index + 2] = col[2]; image.pixels[index + 3] = col[3]; } // VECTORS pp.prototype.Vector = pp.Vector; // IO // LOAD JSON pp.prototype.loadJSON = (f, cb) => { return new Promise((resolve, reject) => { if(isURL(f)) { axios.get(f).then(res => { if(!cb) resolve(res.data); else cb(null, res.data); }).catch(err => { if(!cb) reject(err); else cb(err); }); } else { if(!cb) resolve(JSON.parse(fs.readFileSync(f, 'utf8'))); else cb(null, JSON.parse(fs.readFileSync(f, 'utf8'))); } }); } pp.prototype.registerMethod('loadJSON', pp.prototype.loadJSON); //LOAD STRINGS pp.prototype.loadStrings = (f, cb) => { return new Promise((resolve, reject) => { if(isURL(f)) { axios.get(f).then(res => { try { let data = res.data.replace(/\r/g, "").split('\n'); if(cb) cb(null, data); else resolve(data); } catch (err) { if(cb) cb(err); else reject(err); } }).catch(err => { if(cb) cb(err); else reject(err); }); } else { fs.readFile(f, 'utf8', (err, data) => { if(err) { if(cb) cb(err); else reject(err); } else { try { data = data.replace(/\r/g, "").split('\n'); if(cb) cb(null, data); else resolve(data); } catch (err) { if(cb) cb(err); else reject(err); } } }); } }); } pp.prototype.registerMethod('loadStrings', pp.prototype.loadStrings); //LOAD IMAGE pp.prototype.loadImage = async (path, cb) => { return new Promise(async (resolve, reject) => { let img, base, w, h; try { base = await Jimp.read(path); w = base.getWidth(); h = base.getHeight(); img = new pp.Image(w, h); } catch (error) { if(!cb) reject(error); else cb(error); } img.loadPixels(); for(let i = 0; i < w; i++) { for(let j = 0; j < h; j++) { let col = Jimp.intToRGBA(base.getPixelColor(i, j)); writeColor(img, w, i, j, [col.r, col.g, col.b, col.a]); } } img.updatePixels(); if(typeof cb === "function") cb(null, img); else { resolve(img); } }); } pp.prototype.registerMethod('loadImage', pp.prototype.loadImage); //SAVE JSON pp.prototype.saveJSON = (json, filename, optimize = false) => { return new Promise((resolve, reject) => { if (typeof json == "object") { let str = optimize ? JSON.stringify(json) : JSON.stringify(json, null, 4); fs.writeFile(filename, str, err => { if(err) reject(err); resolve(filename); }); } else reject('First parameter must be an object'); }); } pp.prototype.registerMethod('saveJSON', pp.prototype.saveJSON); //SAVE STRINGS pp.prototype.saveStrings = (list, filename, extension = 'txt', separator = '\n') => { return new Promise((resolve, reject) => { if(typeof list == "object" && list.length != undefined) { let farr = filename.split('.'); if(farr.length > 1 && extension == 'txt') { filename = filename.replace(`.${farr[farr.length - 1]}`, ''); extension = farr[farr.length - 1]; } let str = ''; list.forEach(item => { str += `${item}${separator}` }); fs.writeFile(`${filename}.${extension}`, str, err => { if(err) reject(err); resolve(`${filename}.${extension}`); }) } else reject('First parameter must be an array.'); }); } pp.prototype.registerMethod('saveStrings', pp.prototype.saveStrings); // GET CANVAS DATA URL pp.prototype.getCanvasDataURL = c => { return c.canvas.toDataURL(); } pp.prototype.registerMethod('getCanvasDataURL', pp.prototype.getCanvasDataURL) //SAVE CANVAS pp.prototype.saveCanvas = (c, f, ext) => { let extensions = ['png', 'jpg']; return new Promise((resolve, reject) => { if(!c.canvas) reject(new Error('No canvas passed to SaveCanvas')); let f_arr = f.split('.'); if(!extensions.includes(f_arr[f_arr.length-1])) { if(ext) { f = `${f}.${ext}`; } else { f = `${f}.png`; } } fs.writeFile(`${f}`, pp.prototype.getCanvasDataURL(c).replace(/^data:image\/png;base64,/, ""), 'base64', err => { if(err) reject(err); else resolve(f); }); }); } pp.prototype.registerMethod('saveCanvas', pp.prototype.saveCanvas); // pp.prototype.doStuffAtEndOfDraw = () => { // if(isSavingFrames) { // if(mainCanvas) { // savedFrames.push(pp.prototype.getCanvasDataURL(mainCanvas)); // } // } // } // pp.prototype.registerMethod('post', pp.prototype.doStuffAtEndOfDraw); pp.prototype.saveFrames = (cnv, dir, ext, dur, framerate, cb) => { return new Promise((resolve, reject) => { //get the frames as base64 mainSketch.noLoop(); mainSketch.redraw(); let nrOfFrames = framerate * dur; let sFrames = []; for(let i = 0; i < nrOfFrames; i++) { sFrames.push(pp.prototype.getCanvasDataURL(cnv)); mainSketch.redraw(); } mainSketch.loop(); //cleanup folder if(!(fs.existsSync(dir) && fs.lstatSync(dir).isDirectory())) { fs.mkdirSync(dir); } else { let files = fs.readdirSync(dir); for (const file of files) { fs.unlinkSync(path.join(dir, file), err => { if (err) throw err; }); } } if(typeof ext === "object") { //save as gif let mag = sFrames.length.toString().length; sFrames.forEach((frame, i) => { fs.writeFileSync(`${dir}/frame-${pad(i, mag)}.png`, frame.replace(/^data:image\/png;base64,/, ""), 'base64', err => { if(err) { if(cb) cb(err); else reject(err); } }); }); let encoder = new GIFEncoder(mainSketch.width, mainSketch.height); let str = ''; for(let i = 0; i < mag; i++) { str += '?'; } let options = {repeat: ext.repeat || 0, delay: ext.delay || Math.floor(1000 / framerate), quality: ext.quality || 10}; let stream = pngFileStream(`${dir}/frame-${str}.png`) .pipe(encoder.createWriteStream(options)) .pipe(fs.createWriteStream(`${dir}/${dir}.gif`)); stream.on('finish', () => { if(cb) cb(); else resolve(); }); } else { //save as images sFrames.forEach((frame, i) => { fs.writeFileSync(`${dir}/${i}.${ext}`, frame.replace(/^data:image\/png;base64,/, ""), 'base64', err => { if(err) { if(cb) cb(err); else reject(err); } }); }); } }); } pp.prototype.registerMethod('saveFrames', pp.prototype.saveFrames); module.exports.createSketch = (s, toPreload = { 0: { function: null }}) => { mainSketch = new pp(async c => { let newObj = {}; let index = 0; //declare the preload function c.preload = async () => { let keys = (typeof toPreload == "object") ? Object.keys(toPreload) : 0; //iterate through all the keys of the 'toPreload' object passed to createSketch keys.forEach(async (key, i) => { //if the 'function' parameter is a string, it means its a built in function of p5 if (typeof toPreload[key] === 'function') { let funcVal = toPreload[key](); //check whether it returns a promise or just a value and add it to the returned 'preloaded' object if(funcVal instanceof Promise) newObj[key] = await funcVal; else newObj[key] = funcVal; //if it is a promise wait for it and add it to the object } else if (toPreload[key] instanceof Promise) { newObj[key] = await toPreload[key]; } index++; //decrement preload so that p5 can move on to setup if(index == keys.length) { while(c._preloadCount) { c._decrementPreload(); } //call the sketch and pass the p5 variable and the preloaded object s(c, newObj); if(keys[0] != '0') { c.setup(); } } }); } }); return mainSketch; } // module.exports.Constructor = pp; // if ("object" == typeof exports && "undefined" != typeof module) // module.exports = pp; // else if ("function" == typeof define && define.amd) define([], e); // else { // ("undefined" != typeof window // ? window // : "undefined" != typeof global // ? global // : "undefined" != typeof self // ? self // : this // ).p5 = e(); // } })(function() { return (function o(a, s, h) { function l(t, e) { if (!s[t]) { if (!a[t]) { var r = "function" == typeof require && require; if (!e && r) return r(t, !0); if (u) return u(t, !0); var i = new Error("Cannot find module '" + t + "'"); throw ((i.code = "MODULE_NOT_FOUND"), i); } var n = (s[t] = { exports: {} }); a[t][0].call( n.exports, function(e) { return l(a[t][1][e] || e); }, n, n.exports, o, a, s, h ); } return s[t].exports; } for ( var u = "function" == typeof require && require, e = 0; e < h.length; e++ ) l(h[e]); return l; })( { 1: [ function(e, t, r) { "use strict"; (r.byteLength = function(e) { var t = d(e), r = t[0], i = t[1]; return (3 * (r + i)) / 4 - i; }), (r.toByteArray = function(e) { for ( var t, r = d(e), i = r[0], n = r[1], o = new p(((l = i), (u = n), (3 * (l + u)) / 4 - u)), a = 0, s = 0 < n ? i - 4 : i, h = 0; h < s; h += 4 ) (t = (c[e.charCodeAt(h)] << 18) | (c[e.charCodeAt(h + 1)] << 12) | (c[e.charCodeAt(h + 2)] << 6) | c[e.charCodeAt(h + 3)]), (o[a++] = (t >> 16) & 255), (o[a++] = (t >> 8) & 255), (o[a++] = 255 & t); var l, u; 2 === n && ((t = (c[e.charCodeAt(h)] << 2) | (c[e.charCodeAt(h + 1)] >> 4)), (o[a++] = 255 & t)); 1 === n && ((t = (c[e.charCodeAt(h)] << 10) | (c[e.charCodeAt(h + 1)] << 4) | (c[e.charCodeAt(h + 2)] >> 2)), (o[a++] = (t >> 8) & 255), (o[a++] = 255 & t)); return o; }), (r.fromByteArray = function(e) { for ( var t, r = e.length, i = r % 3, n = [], o = 0, a = r - i; o < a; o += 16383 ) n.push(h(e, o, a < o + 16383 ? a : o + 16383)); 1 === i ? ((t = e[r - 1]), n.push(s[t >> 2] + s[(t << 4) & 63] + "==")) : 2 === i && ((t = (e[r - 2] << 8) + e[r - 1]), n.push( s[t >> 10] + s[(t >> 4) & 63] + s[(t << 2) & 63] + "=" )); return n.join(""); }); for ( var s = [], c = [], p = "undefined" != typeof Uint8Array ? Uint8Array : Array, i = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", n = 0, o = i.length; n < o; ++n ) (s[n] = i[n]), (c[i.charCodeAt(n)] = n); function d(e) { var t = e.length; if (0 < t % 4) throw new Error("Invalid string. Length must be a multiple of 4"); var r = e.indexOf("="); return -1 === r && (r = t), [r, r === t ? 0 : 4 - (r % 4)]; } function h(e, t, r) { for (var i, n, o = [], a = t; a < r; a += 3) (i = ((e[a] << 16) & 16711680) + ((e[a + 1] << 8) & 65280) + (255 & e[a + 2])), o.push( s[((n = i) >> 18) & 63] + s[(n >> 12) & 63] + s[(n >> 6) & 63] + s[63 & n] ); return o.join(""); } (c["-".charCodeAt(0)] = 62), (c["_".charCodeAt(0)] = 63); }, {} ], 2: [function(e, t, r) {}, {}], 3: [ function(e, t, r) { "use strict"; var i = e("base64-js"), o = e("ieee754"); (r.Buffer = c), (r.SlowBuffer = function(e) { +e != e && (e = 0); return c.alloc(+e); }), (r.INSPECT_MAX_BYTES = 50); var n = 2147483647; function a(e) { if (n < e) throw new RangeError( 'The value "' + e + '" is invalid for option "size"' ); var t = new Uint8Array(e); return (t.__proto__ = c.prototype), t; } function c(e, t, r) { if ("number" != typeof e) return s(e, t, r); if ("string" == typeof t) throw new TypeError( 'The "string" argument must be of type string. Received type number' ); return l(e); } function s(e, t, r) { if ("string" == typeof e) return (function(e, t) { ("string" == typeof t && "" !== t) || (t = "utf8"); if (!c.isEncoding(t)) throw new TypeError("Unknown encoding: " + t); var r = 0 | d(e, t), i = a(r), n = i.write(e, t); n !== r && (i = i.slice(0, n)); return i; })(e, t); if (ArrayBuffer.isView(e)) return u(e); if (null == e) throw TypeError( "The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type " + typeof e ); if (O(e, ArrayBuffer) || (e && O(e.buffer, ArrayBuffer))) return (function(e, t, r) { if (t < 0 || e.byteLength < t) throw new RangeError('"offset" is outside of buffer bounds'); if (e.byteLength < t + (r || 0)) throw new RangeError('"length" is outside of buffer bounds'); var i; i = void 0 === t && void 0 === r ? new Uint8Array(e) : void 0 === r ? new Uint8Array(e, t) : new Uint8Array(e, t, r); return (i.__proto__ = c.prototype), i; })(e, t, r); if ("number" == typeof e) throw new TypeError( 'The "value" argument must not be of type number. Received type number' ); var i = e.valueOf && e.valueOf(); if (null != i && i !== e) return c.from(i, t, r); var n = (function(e) { if (c.isBuffer(e)) { var t = 0 | p(e.length), r = a(t); return 0 === r.length || e.copy(r, 0, 0, t), r; } if (void 0 !== e.length) return "number" != typeof e.length || B(e.length) ? a(0) : u(e); if ("Buffer" === e.type && Array.isArray(e.data)) return u(e.data); })(e); if (n) return n; if ( "undefined" != typeof Symbol && null != Symbol.toPrimitive && "function" == typeof e[Symbol.toPrimitive] ) return c.from(e[Symbol.toPrimitive]("string"), t, r); throw new TypeError( "The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type " + typeof e ); } function h(e) { if ("number" != typeof e) throw new TypeError('"size" argument must be of type number'); if (e < 0) throw new RangeError( 'The value "' + e + '" is invalid for option "size"' ); } function l(e) { return h(e), a(e < 0 ? 0 : 0 | p(e)); } function u(e) { for ( var t = e.length < 0 ? 0 : 0 | p(e.length), r = a(t), i = 0; i < t; i += 1 ) r[i] = 255 & e[i]; return r; } function p(e) { if (n <= e) throw new RangeError( "Attempt to allocate Buffer larger than maximum size: 0x" + n.toString(16) + " bytes" ); return 0 | e; } function d(e, t) { if (c.isBuffer(e)) return e.length; if (ArrayBuffer.isView(e) || O(e, ArrayBuffer)) return e.byteLength; if ("string" != typeof e) throw new TypeError( 'The "string" argument must be one of type string, Buffer, or ArrayBuffer. Received type ' + typeof e ); var r = e.length, i = 2 < arguments.length && !0 === arguments[2]; if (!i && 0 === r) return 0; for (var n = !1; ; ) switch (t) { case "ascii": case "latin1": case "binary": return r; case "utf8": case "utf-8": return k(e).length; case "ucs2": case "ucs-2": case "utf16le": case "utf-16le": return 2 * r; case "hex": return r >>> 1; case "base64": return I(e).length; default: if (n) return i ? -1 : k(e).length; (t = ("" + t).toLowerCase()), (n = !0); } } function f(e, t, r) { var i = e[t]; (e[t] = e[r]), (e[r] = i); } function m(e, t, r, i, n) { if (0 === e.length) return -1; if ( ("string" == typeof r ? ((i = r), (r = 0)) : 2147483647 < r ? (r = 2147483647) : r < -2147483648 && (r = -2147483648), B((r = +r)) && (r = n ? 0 : e.length - 1), r < 0 && (r = e.length + r), r >= e.length) ) { if (n) return -1; r = e.length - 1; } else if (r < 0) { if (!n) return -1; r = 0; } if (("string" == typeof t && (t = c.from(t, i)), c.isBuffer(t))) return 0 === t.length ? -1 : v(e, t, r, i, n); if ("number" == typeof t) return ( (t &= 255), "function" == typeof Uint8Array.prototype.indexOf ? n ? Uint8Array.prototype.indexOf.call(e, t, r) : Uint8Array.prototype.lastIndexOf.call(e, t, r) : v(e, [t], r, i, n) ); throw new TypeError("val must be string, number or Buffer"); } function v(e, t, r, i, n) { var o, a = 1, s = e.length, h = t.length; if ( void 0 !== i && ("ucs2" === (i = String(i).toLowerCase()) || "ucs-2" === i || "utf16le" === i || "utf-16le" === i) ) { if (e.length < 2 || t.length < 2) return -1; (s /= a = 2), (h /= 2), (r /= 2); } function l(e, t) { return 1 === a ? e[t] : e.readUInt16BE(t * a); } if (n) { var u = -1; for (o = r; o < s; o++) if (l(e, o) === l(t, -1 === u ? 0 : o - u)) { if ((-1 === u && (u = o), o - u + 1 === h)) return u * a; } else -1 !== u && (o -= o - u), (u = -1); } else for (s < r + h && (r = s - h), o = r; 0 <= o; o--) { for (var c = !0, p = 0; p < h; p++) if (l(e, o + p) !== l(t, p)) { c = !1; break; } if (c) return o; } return -1; } function g(e, t, r, i) { r = Number(r) || 0; var n = e.length - r; i ? n < (i = Number(i)) && (i = n) : (i = n); var o = t.length; o / 2 < i && (i = o / 2); for (var a = 0; a < i; ++a) { var s = parseInt(t.substr(2 * a, 2), 16); if (B(s)) return a; e[r + a] = s; } return a; } function y(e, t, r, i) { return U( (function(e) { for (var t = [], r = 0; r < e.length; ++r) t.push(255 & e.charCodeAt(r)); return t; })(t), e, r, i ); } function b(e, t, r) { return 0 === t && r === e.length ? i.fromByteArray(e) : i.fromByteArray(e.slice(t, r)); } function _(e, t, r) { r = Math.min(e.length, r); for (var i = [], n = t; n < r; ) { var o, a, s, h, l = e[n], u = null, c = 239 < l ? 4 : 223 < l ? 3 : 191 < l ? 2 : 1; if (n + c <= r) switch (c) { case 1: l < 128 && (u = l); break; case 2: 128 == (192 & (o = e[n + 1])) && 127 < (h = ((31 & l) << 6) | (63 & o)) && (u = h); break; case 3: (o = e[n + 1]), (a = e[n + 2]), 128 == (192 & o) && 128 == (192 & a) && 2047 < (h = ((15 & l) << 12) | ((63 & o) << 6) | (63 & a)) && (h < 55296 || 57343 < h) && (u = h); break; case 4: (o = e[n + 1]), (a = e[n + 2]), (s = e[n + 3]), 128 == (192 & o) && 128 == (192 & a) && 128 == (192 & s) && 65535 < (h = ((15 & l) << 18) | ((63 & o) << 12) | ((63 & a) << 6) | (63 & s)) && h < 1114112 && (u = h); } null === u ? ((u = 65533), (c = 1)) : 65535 < u && ((u -= 65536), i.push(((u >>> 10) & 1023) | 55296), (u = 56320 | (1023 & u))), i.push(u), (n += c); } return (function(e) { var t = e.length; if (t <= x) return String.fromCharCode.apply(String, e); var r = "", i = 0; for (; i < t; ) r += String.fromCharCode.apply(String, e.slice(i, (i += x))); return r; })(i); } (r.kMaxLength = n), (c.TYPED_ARRAY_SUPPORT = (function() { try { var e = new Uint8Array(1); return ( (e.__proto__ = { __proto__: Uint8Array.prototype, foo: function() { return 42; } }), 42 === e.foo() ); } catch (e) { return !1; } })()) || "undefined" == typeof console || "function" != typeof console.error || console.error( "This browser lacks typed array (Uint8Array) support which is required by `buffer` v5.x. Use `buffer` v4.x if you require old browser support." ), Object.defineProperty(c.prototype, "parent", { enumerable: !0, get: function() { if (c.isBuffer(this)) return this.buffer; } }), Object.defineProperty(c.prototype, "offset", { enumerable: !0, get: function() { if (c.isBuffer(this)) return this.byteOffset; } }), "undefined" != typeof Symbol && null != Symbol.species && c[Symbol.species] === c && Object.defineProperty(c, Symbol.species, { value: null, configurable: !0, enumerable: !1, writable: !1 }), (c.poolSize = 8192), (c.from = function(e, t, r) { return s(e, t, r); }), (c.prototype.__proto__ = Uint8Array.prototype), (c.__proto__ = Uint8Array), (c.alloc = function(e, t, r) { return ( (n = t), (o = r), h((i = e)), i <= 0 ? a(i) : void 0 !== n ? "string" == typeof o ? a(i).fill(n, o) : a(i).fill(n) : a(i) ); var i, n, o; }), (c.allocUnsafe = function(e) { return l(e); }), (c.allocUnsafeSlow = function(e) { return l(e); }), (c.isBuffer = function(e) { return null != e && !0 === e._isBuffer && e !== c.prototype; }), (c.compare = function(e, t) { if ( (O(e, Uint8Array) && (e = c.from(e, e.offset, e.byteLength)), O(t, Uint8Array) && (t = c.from(t, t.offset, t.byteLength)), !c.isBuffer(e) || !c.isBuffer(t)) ) throw new TypeError( 'The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array' ); if (e === t) return 0; for ( var r = e.length, i = t.length, n = 0, o = Math.min(r, i); n < o; ++n ) if (e[n] !== t[n]) { (r = e[n]), (i = t[n]); break; } return r < i ? -1 : i < r ? 1 : 0; }), (c.isEncoding = function(e) { switch (String(e).toLowerCase()) { case "hex": case "utf8": case "utf-8": case "ascii": case "latin1": case "binary": case "base64": case "ucs2": case "ucs-2": case "utf16le": case "utf-16le": return !0; default: return !1; } }), (c.concat = function(e, t) { if (!Array.isArray(e)) throw new TypeError( '"list" argument must be an Array of Buffers' ); if (0 === e.length) return c.alloc(0); var r; if (void 0 === t) for (r = t = 0; r < e.length; ++r) t += e[r].length; var i = c.allocUnsafe(t), n = 0; for (r = 0; r < e.length; ++r) { var o = e[r]; if ((O(o, Uint8Array) && (o = c.from(o)), !c.isBuffer(o))) throw new TypeError( '"list" argument must be an Array of Buffers' ); o.copy(i, n), (n += o.length); } return i; }), (c.byteLength = d), (c.prototype._isBuffer = !0), (c.prototype.swap16 = function() { var e = this.length; if (e % 2 != 0) throw new RangeError( "Buffer size must be a multiple of 16-bits" ); for (var t = 0; t < e; t += 2) f(this, t, t + 1); return this; }), (c.prototype.swap32 = function() { var e = this.length; if (e % 4 != 0) throw new RangeError( "Buffer size must be a multiple of 32-bits" ); for (var t = 0; t < e; t += 4) f(this, t, t + 3), f(this, t + 1, t + 2); return this; }), (c.prototype.swap64 = function() { var e = this.length; if (e % 8 != 0) throw new RangeError( "Buffer size must be a multiple of 64-bits" ); for (var t = 0; t < e; t += 8) f(this, t, t + 7), f(this, t + 1, t + 6), f(this, t + 2, t + 5), f(this, t + 3, t + 4); return this; }), (c.prototype.toLocaleString = c.prototype.toString = function() { var e = this.length; return 0 === e ? "" : 0 === arguments.length ? _(this, 0, e) : function(e, t, r) { var i = !1; if (((void 0 === t || t < 0) && (t = 0), t > this.length)) return ""; if ( ((void 0 === r || r > this.length) && (r = this.length), r <= 0) ) return ""; if ((r >>>= 0) <= (t >>>= 0)) return ""; for (e || (e = "utf8"); ; ) switch (e) { case "hex": return T(this, t, r); case "utf8": case "utf-8": return _(this, t, r); case "ascii": return w(this, t, r); case "latin1": case "binary": return S(this, t, r); case "base64": return b(this, t, r); case "ucs2": case "ucs-2": case "utf16le": case "utf-16le": return M(this, t, r); default: if (i) throw new TypeError("Unknown encoding: " + e); (e = (e + "").toLowerCase()), (i = !0); } }.apply(this, arguments); }), (c.prototype.equals = function(e) { if (!c.isBuffer(e)) throw new TypeError("Argument must be a Buffer"); return this === e || 0 === c.compare(this, e); }), (c.prototype.inspect = function() { var e = "", t = r.INSPECT_MAX_BYTES; return ( (e = this.toString("hex", 0, t) .replace(/(.{2})/g, "$1 ") .trim()), this.length > t && (e += " ... "), "<Buffer " + e + ">" ); }), (c.prototype.compare = function(e, t, r, i, n) { if ( (O(e, Uint8Array) && (e = c.from(e, e.offset, e.byteLength)), !c.isBuffer(e)) ) throw new TypeError( 'The "target" argument must be one of type Buffer or Uint8Array. Received type ' + typeof e ); if ( (void 0 === t && (t = 0), void 0 === r && (r = e ? e.length : 0), void 0 === i && (i = 0), void 0 === n && (n = this.length), t < 0 || r > e.length || i < 0 || n > this.length) ) throw new RangeError("out of range index"); if (n <= i && r <= t) return 0; if (n <= i) return -1; if (r <= t) return 1; if (this === e) return 0; for ( var o = (n >>>= 0) - (i >>>= 0), a = (r >>>= 0) - (t >>>= 0), s = Math.min(o, a), h = this.slice(i, n), l = e.slice(t, r), u = 0; u < s; ++u ) if (h[u] !== l[u]) { (o = h[u]), (a = l[u]); break; } return o < a ? -1 : a < o ? 1 : 0; }), (c.prototype.includes = function(e, t, r) { return -1 !== this.indexOf(e, t, r); }), (c.prototype.indexOf = function(e, t, r) { return m(this, e, t, r, !0); }), (c.prototype.lastIndexOf = function(e, t, r) { return m(this, e, t, r, !1); }), (c.prototype.write = function(e, t, r, i) { if (void 0 === t) (i = "utf8"), (r = this.length), (t = 0); else if (void 0 === r && "string" == typeof t) (i = t), (r = this.length), (t = 0); else { if (!isFinite(t)) throw new Error( "Buffer.write(string, encoding, offset[, length]) is no longer supported" ); (t >>>= 0), isFinite(r) ? ((r >>>= 0), void 0 === i && (i = "utf8")) : ((i = r), (r = void 0)); } var n = this.length - t; if ( ((void 0 === r || n < r) && (r = n), (0 < e.length && (r < 0 || t < 0)) || t > this.length) ) throw new RangeError("Attempt to write outside buffer bounds"); i || (i = "utf8"); for (var o, a, s, h, l, u, c, p, d, f = !1; ; ) switch (i) { case "hex": return g(this, e, t, r); case "utf8": case "utf-8": return ( (p = t), (d = r), U(k(e, (c = this).length - p), c, p, d) ); case "ascii": return y(this, e, t, r); case "latin1": case "binary": return y(this, e, t, r); case "base64": return (h = this), (l = t), (u = r), U(I(e), h, l, u); case "ucs2": case "ucs-2": case "utf16le": case "utf-16le": return ( (a = t), (s = r), U( (function(e, t) { for ( var r, i, n, o = [], a = 0; a < e.length && !((t -= 2) < 0); ++a ) (r = e.charCodeAt(a)), (i = r >> 8), (n = r % 256), o.push(n), o.push(i); return o; })(e, (o = this).length - a), o, a, s ) ); default: if (f) throw new TypeError("Unknown encoding: " + i); (i = ("" + i).toLowerCase()), (f = !0); } }), (c.prototype.toJSON = function() { return { type: "Buffer", data: Array.prototype.slice.call(this._arr || this, 0) }; }); var x = 4096; function w(e, t, r) { var i = ""; r = Math.min(e.length, r); for (var n = t; n < r; ++n) i += String.fromCharCode(127 & e[n]); return i; } function S(e, t, r) { var i = ""; r = Math.min(e.length, r); for (var n = t; n < r; ++n) i += String.fromCharCode(e[n]); return i; } function T(e, t, r) { var i = e.length; (!t || t < 0) && (t = 0), (!r || r < 0 || i < r) && (r = i); for (var n = "", o = t; o < r; ++o) n += A(e[o]); return n; } function M(e, t, r) { for (var i = e.slice(t, r), n = "", o = 0; o < i.length; o += 2) n += String.fromCharCode(i[o] + 256 * i[o + 1]); return n; } function E(e, t, r) { if (e % 1 != 0 || e < 0) throw new RangeError("offset is not uint"); if (r < e + t) throw new RangeError("Trying to access beyond buffer length"); } function C(e, t, r, i, n, o) { if (!c.isBuffer(e)) throw new TypeError( '"buffer" argument must be a Buffer instance' ); if (n < t || t < o) throw new RangeError('"value" argument is out of bounds'); if (r + i > e.length) throw new RangeError("Index out of range"); } function R(e, t, r, i, n, o) { if (r + i > e.length) throw new RangeError("Index out of range"); if (r < 0) throw new RangeError("Index out of range"); } function L(e, t, r, i, n) { return ( (t = +t), (r >>>= 0), n || R(e, 0, r, 4), o.write(e, t, r, i, 23, 4), r + 4 ); } function P(e, t, r, i, n) { return ( (t = +t), (r >>>= 0), n || R(e, 0, r, 8), o.write(e, t, r, i, 52, 8), r + 8 ); } (c.prototype.slice = function(e, t) { var r = this.length; (e = ~~e) < 0 ? (e += r) < 0 && (e = 0) : r < e && (e = r), (t = void 0 === t ? r : ~~t) < 0 ? (t += r) < 0 && (t = 0) : r < t && (t = r), t < e && (t = e); var i = this.subarray(e, t); return (i.__proto__ = c.prototype), i; }), (c.prototype.readUIntLE = function(e, t, r) { (e >>>= 0), (t >>>= 0), r || E(e, t, this.length); for (var i = this[e], n = 1, o = 0; ++o < t && (n *= 256); ) i += this[e + o] * n; return i; }), (c.prototype.readUIntBE = function(e, t, r) { (e >>>= 0), (t >>>= 0), r || E(e, t, this.length); for (var i = this[e + --t], n = 1; 0 < t && (n *= 256); ) i += this[e + --t] * n; return i; }), (c.prototype.readUInt8 = function(e, t) { return (e >>>= 0), t || E(e, 1, this.length), this[e]; }), (c.prototype.readUInt16LE = function(e, t) { return ( (e >>>= 0), t || E(e, 2, this.length), this[e] | (this[e + 1] << 8) ); }), (c.prototype.readUInt16BE = function(e, t) { return ( (e >>>= 0), t || E(e, 2, this.length), (this[e] << 8) | this[e + 1] ); }), (c.prototype.readUInt32LE = function(e, t) { return ( (e >>>= 0), t || E(e, 4, this.length), (this[e] | (this[e + 1] << 8) | (this[e + 2] << 16)) + 16777216 * this[e + 3] ); }), (c.prototype.readUInt32BE = function(e, t) { return ( (e >>>= 0), t || E(e, 4, this.length), 16777216 * this[e] + ((this[e + 1] << 16) | (this[e + 2] << 8) | this[e + 3]) ); }), (c.prototype.readIntLE = function(e, t, r) { (e >>>= 0), (t >>>= 0), r || E(e, t, this.length); for (var i = this[e], n = 1, o = 0; ++o < t && (n *= 256); ) i += this[e + o] * n; return (n *= 128) <= i && (i -= Math.pow(2, 8 * t)), i; }), (c.prototype.readIntBE = function(e, t, r) { (e >>>= 0), (t >>>= 0), r || E(e, t, this.length); for (var i = t, n = 1, o = this[e + --i]; 0 < i && (n *= 256); ) o += this[e + --i] * n; return (n *= 128) <= o && (o -= Math.pow(2, 8 * t)), o; }), (c.prototype.readInt8 = function(e, t) { return ( (e >>>= 0), t || E(e, 1, this.length), 128 & this[e] ? -1 * (255 - this[e] + 1) : this[e] ); }), (c.prototype.readInt16LE = function(e, t) { (e >>>= 0), t || E(e, 2, this.length); var r = this[e] | (this[e + 1] << 8); return 32768 & r ? 4294901760 | r : r; }), (c.prototype.readInt16BE = function(e, t) { (e >>>= 0), t || E(e, 2, this.length); var r = this[e + 1] | (this[e] << 8); return 32768 & r ? 4294901760 | r : r; }), (c.prototype.readInt32LE = function(e, t) { return ( (e >>>= 0), t || E(e, 4, this.length), this[e] | (this[e + 1] << 8) | (this[e + 2] << 16) | (this[e + 3] << 24) ); }), (c.prototype.readInt32BE = function(e, t) { return ( (e >>>= 0), t || E(e, 4, this.length), (this[e] << 24) | (this[e + 1] << 16) | (this[e + 2] << 8) | this[e + 3] ); }), (c.prototype.readFloatLE = function(e, t) { return ( (e >>>= 0), t || E(e, 4, this.length), o.read(this, e, !0, 23, 4) ); }), (c.prototype.readFloatBE = function(e, t) { return ( (e >>>= 0), t || E(e, 4, this.length), o.read(this, e, !1, 23, 4) ); }), (c.prototype.readDoubleLE = function(e, t) { return ( (e >>>= 0), t || E(e, 8, this.length), o.read(this, e, !0, 52, 8) ); }), (c.prototype.readDoubleBE = function(e, t) { return ( (e >>>= 0), t || E(e, 8, this.length), o.read(this, e, !1, 52, 8) ); }), (c.prototype.writeUIntLE = function(e, t, r, i) {