window.utility2.stateInit({"utility2":{"assetsDict":{"/assets.example.html":"\n\n
\n\n\n\nswgg\n\n\n\n\n\n\n\n\n\n\n swgg (0.0.1)\n\n
\nthe greatest app in the world!
\n\n\n\n\n\n
\n
\n
\n
\n\n\n\n
\n swgg\n \n \n \n \n
\n
\n
\n \n
\n\n
\n
\n
\n [\n this document was created with\n
swgg\n ]\n
\n\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","/assets.example.js":"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n/*\nexample.js\n\nthis script will run a web-demo of swgg\n\ninstruction\n 1. save this script as example.js\n 2. run shell-command:\n $ npm install swgg && \\\n PORT=8081 node example.js\n 3. open a browser to http://127.0.0.1:8081 and play with web-demo\n 4. edit this script to suit your needs\n*/\n\n\n\n/* istanbul instrument in package swgg */\n/* istanbul ignore next */\n/* jslint utility2:true */\n(function (globalThis) {\n \"use strict\";\n let ArrayPrototypeFlat;\n let TextXxcoder;\n let consoleError;\n let local;\n // init globalThis\n globalThis.globalThis = globalThis.globalThis || globalThis;\n // init debug_inline\n if (!globalThis[\"debug\\u0049nline\"]) {\n consoleError = console.error;\n globalThis[\"debug\\u0049nline\"] = function (...argList) {\n /*\n * this function will both print to stderr\n * and return [0]\n */\n // debug argList\n globalThis[\"debug\\u0049nlineArgList\"] = argList;\n consoleError(\"\\n\\ndebug\\u0049nline\");\n consoleError.apply(console, argList);\n consoleError(\"\\n\");\n // return arg0 for inspection\n return argList[0];\n };\n }\n // polyfill\n ArrayPrototypeFlat = function (depth) {\n /*\n * this function will polyfill Array.prototype.flat\n * https://github.com/jonathantneal/array-flat-polyfill\n */\n depth = (\n globalThis.isNaN(depth)\n ? 1\n : Number(depth)\n );\n if (!depth) {\n return Array.prototype.slice.call(this);\n }\n return Array.prototype.reduce.call(this, function (acc, cur) {\n if (Array.isArray(cur)) {\n // recurse\n acc.push.apply(acc, ArrayPrototypeFlat.call(cur, depth - 1));\n } else {\n acc.push(cur);\n }\n return acc;\n }, []);\n };\n Array.prototype.flat = Array.prototype.flat || ArrayPrototypeFlat;\n Array.prototype.flatMap = Array.prototype.flatMap || function flatMap(\n ...argList\n ) {\n /*\n * this function will polyfill Array.prototype.flatMap\n * https://github.com/jonathantneal/array-flat-polyfill\n */\n return this.map(...argList).flat();\n };\n (function () {\n try {\n globalThis.TextDecoder = (\n globalThis.TextDecoder || require(\"util\").TextDecoder\n );\n globalThis.TextEncoder = (\n globalThis.TextEncoder || require(\"util\").TextEncoder\n );\n } catch (ignore) {}\n }());\n TextXxcoder = function () {\n /*\n * this function will polyfill TextDecoder/TextEncoder\n * https://gist.github.com/Yaffle/5458286\n */\n return;\n };\n TextXxcoder.prototype.decode = function (octets) {\n /*\n * this function will polyfill TextDecoder.prototype.decode\n * https://gist.github.com/Yaffle/5458286\n */\n let bytesNeeded;\n let codePoint;\n let ii;\n let kk;\n let octet;\n let string;\n string = \"\";\n ii = 0;\n while (ii < octets.length) {\n octet = octets[ii];\n bytesNeeded = 0;\n codePoint = 0;\n if (octet <= 0x7F) {\n bytesNeeded = 0;\n codePoint = octet & 0xFF;\n } else if (octet <= 0xDF) {\n bytesNeeded = 1;\n codePoint = octet & 0x1F;\n } else if (octet <= 0xEF) {\n bytesNeeded = 2;\n codePoint = octet & 0x0F;\n } else if (octet <= 0xF4) {\n bytesNeeded = 3;\n codePoint = octet & 0x07;\n }\n if (octets.length - ii - bytesNeeded > 0) {\n kk = 0;\n while (kk < bytesNeeded) {\n octet = octets[ii + kk + 1];\n codePoint = (codePoint << 6) | (octet & 0x3F);\n kk += 1;\n }\n } else {\n codePoint = 0xFFFD;\n bytesNeeded = octets.length - ii;\n }\n string += String.fromCodePoint(codePoint);\n ii += bytesNeeded + 1;\n }\n return string;\n };\n TextXxcoder.prototype.encode = function (string) {\n /*\n * this function will polyfill TextEncoder.prototype.encode\n * https://gist.github.com/Yaffle/5458286\n */\n let bits;\n let cc;\n let codePoint;\n let ii;\n let length;\n let octets;\n octets = [];\n length = string.length;\n ii = 0;\n while (ii < length) {\n codePoint = string.codePointAt(ii);\n cc = 0;\n bits = 0;\n if (codePoint <= 0x0000007F) {\n cc = 0;\n bits = 0x00;\n } else if (codePoint <= 0x000007FF) {\n cc = 6;\n bits = 0xC0;\n } else if (codePoint <= 0x0000FFFF) {\n cc = 12;\n bits = 0xE0;\n } else if (codePoint <= 0x001FFFFF) {\n cc = 18;\n bits = 0xF0;\n }\n octets.push(bits | (codePoint >> cc));\n cc -= 6;\n while (cc >= 0) {\n octets.push(0x80 | ((codePoint >> cc) & 0x3F));\n cc -= 6;\n }\n ii += (\n codePoint >= 0x10000\n ? 2\n : 1\n );\n }\n return octets;\n };\n globalThis.TextDecoder = globalThis.TextDecoder || TextXxcoder;\n globalThis.TextEncoder = globalThis.TextEncoder || TextXxcoder;\n // init local\n local = {};\n local.local = local;\n globalThis.globalLocal = local;\n // init isBrowser\n local.isBrowser = (\n typeof globalThis.XMLHttpRequest === \"function\"\n && globalThis.navigator\n && typeof globalThis.navigator.userAgent === \"string\"\n );\n // init function\n local.assertOrThrow = function (passed, message) {\n /*\n * this function will throw err. if is falsy\n */\n let err;\n if (passed) {\n return;\n }\n err = (\n (\n message\n && typeof message.message === \"string\"\n && typeof message.stack === \"string\"\n )\n // if message is errObj, then leave as is\n ? message\n : new Error(\n typeof message === \"string\"\n // if message is a string, then leave as is\n ? message\n // else JSON.stringify message\n : JSON.stringify(message, null, 4)\n )\n );\n throw err;\n };\n local.fsRmrfSync = function (dir) {\n /*\n * this function will sync \"rm -rf\" \n */\n let child_process;\n try {\n child_process = require(\"child_process\");\n } catch (ignore) {\n return;\n }\n child_process.spawnSync(\"rm\", [\n \"-rf\", dir\n ], {\n stdio: [\n \"ignore\", 1, 2\n ]\n });\n };\n local.fsWriteFileWithMkdirpSync = function (file, data) {\n /*\n * this function will sync write to with \"mkdir -p\"\n */\n let fs;\n try {\n fs = require(\"fs\");\n } catch (ignore) {\n return;\n }\n // try to write file\n try {\n fs.writeFileSync(file, data);\n } catch (ignore) {\n // mkdir -p\n require(\"child_process\").spawnSync(\n \"mkdir\",\n [\n \"-p\", require(\"path\").dirname(file)\n ],\n {\n stdio: [\n \"ignore\", 1, 2\n ]\n }\n );\n // rewrite file\n fs.writeFileSync(file, data);\n }\n };\n local.functionOrNop = function (fnc) {\n /*\n * this function will if exists,\n * return ,\n * else return \n */\n return fnc || local.nop;\n };\n local.nop = function () {\n /*\n * this function will do nothing\n */\n return;\n };\n local.objectAssignDefault = function (target, source) {\n /*\n * this function will if items from are\n * null, undefined, or empty-string,\n * then overwrite them with items from \n */\n target = target || {};\n Object.keys(source || {}).forEach(function (key) {\n if (\n target[key] === null\n || target[key] === undefined\n || target[key] === \"\"\n ) {\n target[key] = target[key] || source[key];\n }\n });\n return target;\n };\n local.value = function (val) {\n /*\n * this function will return \n */\n return val;\n };\n local.valueOrEmptyList = function (val) {\n /*\n * this function will return or []\n */\n return val || [];\n };\n local.valueOrEmptyObject = function (val) {\n /*\n * this function will return or {}\n */\n return val || {};\n };\n local.valueOrEmptyString = function (val) {\n /*\n * this function will return or \"\"\n */\n return val || \"\";\n };\n // require builtin\n if (!local.isBrowser) {\n local.assert = require(\"assert\");\n local.buffer = require(\"buffer\");\n local.child_process = require(\"child_process\");\n local.cluster = require(\"cluster\");\n local.crypto = require(\"crypto\");\n local.dgram = require(\"dgram\");\n local.dns = require(\"dns\");\n local.domain = require(\"domain\");\n local.events = require(\"events\");\n local.fs = require(\"fs\");\n local.http = require(\"http\");\n local.https = require(\"https\");\n local.net = require(\"net\");\n local.os = require(\"os\");\n local.path = require(\"path\");\n local.querystring = require(\"querystring\");\n local.readline = require(\"readline\");\n local.repl = require(\"repl\");\n local.stream = require(\"stream\");\n local.string_decoder = require(\"string_decoder\");\n local.timers = require(\"timers\");\n local.tls = require(\"tls\");\n local.tty = require(\"tty\");\n local.url = require(\"url\");\n local.util = require(\"util\");\n local.vm = require(\"vm\");\n local.zlib = require(\"zlib\");\n }\n}((typeof globalThis === \"object\" && globalThis) || (function () {\n return Function(\"return this\")(); // jslint ignore:line\n}())));\n\n\n\n(function (local) {\n\"use strict\";\n\n\n\n// run shared js-env code - init-before\n(function () {\n// init local\nlocal = (\n globalThis.utility2_rollup\n || globalThis.utility2_swgg\n || globalThis.utility2_moduleExports\n);\n// init exports\nglobalThis.local = local;\n// init assets\nlocal.assetsDict[\"/assets.swgg.swagger.json\"] = (\n local.assetsDict[\"/assets.swgg.swagger.petstore.json\"]\n);\n// load db\nlocal.db.dbLoad(function () {\n console.error(\"db loaded from \" + local.storageDir);\n});\n}());\n\n\n\n// run shared js-env code - function\n(function () {\nlocal.middlewareCrudCustom = function (req, response, nextMiddleware) {\n/*\n * this function will run the middleware to run custom-crud-operations\n */\n let crud;\n let opt;\n let result;\n opt = {};\n local.gotoNext(opt, function (err, data) {\n switch (opt.gotoState) {\n case 1:\n crud = req.swgg.crud;\n switch (crud.crudType[0]) {\n // hack-istanbul - test err handling-behavior\n case \"crudErrorPre\":\n opt.gotoNext(local.errDefault);\n return;\n case \"getInventory\":\n crud.dbTable.crudGetManyByQuery({\n query: {},\n projection: [\n \"status\"\n ]\n }, opt.gotoNext);\n break;\n default:\n opt.gotoState = Infinity;\n opt.gotoNext();\n }\n break;\n case 2:\n switch (crud.crudType[0]) {\n case \"getInventory\":\n result = {};\n data.forEach(function (element) {\n result[element.status] = result[element.status] || 0;\n result[element.status] += 1;\n });\n opt.gotoNext(null, result);\n break;\n }\n break;\n case 3:\n local.swgg.serverRespondJsonapi(req, response, err, data);\n break;\n default:\n nextMiddleware(err, data);\n }\n });\n opt.gotoState = 0;\n opt.gotoNext();\n};\n\nlocal.middlewareInitCustom = function (req, response, nextMiddleware) {\n/*\n * this function will run the middleware to custom-init and \n */\n // enable cors\n // https://en.wikipedia.org/wiki/Cross-origin_resource_sharing\n response.setHeader(\n \"Access-Control-Allow-Methods\",\n \"DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT\"\n );\n response.setHeader(\"Access-Control-Allow-Origin\", \"*\");\n // init content-type\n response.setHeader(\"Content-Type\", \"application/json; charset=UTF-8\");\n // ignore .map files\n if (req.urlParsed.pathname.slice(-4) === \".map\") {\n local.serverRespondDefault(req, response, 404);\n return;\n }\n nextMiddleware();\n};\n}());\n\n\n\n// run shared js-env code - init-after\n(function () {\n// init assets\n/* jslint ignore:start */\nlocal.assetsDict['/assets.index.template.html'] = local.assetsDict['/assets.swgg.html']\n .replace((/\\n<\\/script>\\n/), '\\\n\\n\\\n\\n\\\n\\n\\\n\\n\\\n {{env.npm_package_name}} ({{env.npm_package_version}})\\n\\\n\\n\\\n
\\n\\\n{{env.npm_package_description}}
\\n\\\n\\n\\\n\\n\\\n\\n\\\n\\n\\\n\\n\\\n
\\n\\\n
\\n\\\n
\\n\\\n
\\n\\\n\\n\\\n')\n .replace('assets.swgg.swagger.json', 'assets.swgg.swagger.server.json')\n .replace((/\\n\\n\\\n{{#unless isRollup}}\\n\\\n\\n\\\n\\n\\\n\\n\\\nutility2-comment -->\\n\\\n\\n\\\n\\n\\\n\\n\\\n\\n\\\n\\n\\\n\\n\\\n