{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/Jaciss/jai/blob/main/vite.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "857zQls5JMyI",
        "outputId": "cc522e07-ab97-4854-f0f3-d336b2c69538"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\n",
            "## Installing the NodeSource Node.js 16.x repo...\n",
            "\n",
            "\n",
            "## Populating apt-get cache...\n",
            "\n",
            "+ apt-get update\n",
            "Get:1 https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/ InRelease [3,626 B]\n",
            "Get:2 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]\n",
            "Ign:3 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64  InRelease\n",
            "Hit:4 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64  InRelease\n",
            "Hit:5 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64  Release\n",
            "Hit:6 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic InRelease\n",
            "Hit:7 http://archive.ubuntu.com/ubuntu bionic InRelease\n",
            "Get:8 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]\n",
            "Hit:9 http://ppa.launchpad.net/cran/libgit2/ubuntu bionic InRelease\n",
            "Hit:10 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic InRelease\n",
            "Get:11 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [83.3 kB]\n",
            "Hit:12 http://ppa.launchpad.net/graphics-drivers/ppa/ubuntu bionic InRelease\n",
            "Get:14 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [2,348 kB]\n",
            "Get:15 http://security.ubuntu.com/ubuntu bionic-security/restricted amd64 Packages [1,371 kB]\n",
            "Get:16 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [1,573 kB]\n",
            "Get:17 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [3,145 kB]\n",
            "Get:18 http://archive.ubuntu.com/ubuntu bionic-updates/restricted amd64 Packages [1,412 kB]\n",
            "Get:19 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [3,568 kB]\n",
            "Fetched 13.7 MB in 40s (340 kB/s)\n",
            "Reading package lists... Done\n",
            "\n",
            "## Confirming \"bionic\" is supported...\n",
            "\n",
            "+ curl -sLf -o /dev/null 'https://deb.nodesource.com/node_16.x/dists/bionic/Release'\n",
            "\n",
            "## Adding the NodeSource signing key to your keyring...\n",
            "\n",
            "+ curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | gpg --dearmor | tee /usr/share/keyrings/nodesource.gpg >/dev/null\n",
            "\n",
            "## Creating apt sources list file for the NodeSource Node.js 16.x repo...\n",
            "\n",
            "+ echo 'deb [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_16.x bionic main' > /etc/apt/sources.list.d/nodesource.list\n",
            "+ echo 'deb-src [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_16.x bionic main' >> /etc/apt/sources.list.d/nodesource.list\n",
            "\n",
            "## Running `apt-get update` for you...\n",
            "\n",
            "+ apt-get update\n",
            "Hit:1 https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/ InRelease\n",
            "Hit:2 http://archive.ubuntu.com/ubuntu bionic InRelease\n",
            "Hit:3 http://security.ubuntu.com/ubuntu bionic-security InRelease\n",
            "Get:4 https://deb.nodesource.com/node_16.x bionic InRelease [4,584 B]\n",
            "Hit:5 http://archive.ubuntu.com/ubuntu bionic-updates InRelease\n",
            "Hit:6 http://archive.ubuntu.com/ubuntu bionic-backports InRelease\n",
            "Ign:7 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64  InRelease\n",
            "Hit:8 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64  InRelease\n",
            "Hit:9 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64  Release\n",
            "Hit:10 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic InRelease\n",
            "Hit:11 http://ppa.launchpad.net/cran/libgit2/ubuntu bionic InRelease\n",
            "Hit:12 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic InRelease\n",
            "Get:13 https://deb.nodesource.com/node_16.x bionic/main amd64 Packages [773 B]\n",
            "Hit:14 http://ppa.launchpad.net/graphics-drivers/ppa/ubuntu bionic InRelease\n",
            "Fetched 5,357 B in 3s (2,002 B/s)\n",
            "Reading package lists... Done\n",
            "\n",
            "## Run `\u001b[1msudo apt-get install -y nodejs\u001b[m` to install Node.js 16.x and npm\n",
            "## You may also need development tools to build native addons:\n",
            "     sudo apt-get install gcc g++ make\n",
            "## To install the Yarn package manager, run:\n",
            "     curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/yarnkey.gpg >/dev/null\n",
            "     echo \"deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main\" | sudo tee /etc/apt/sources.list.d/yarn.list\n",
            "     sudo apt-get update && sudo apt-get install yarn\n",
            "\n",
            "\n",
            "Reading package lists... Done\n",
            "Building dependency tree       \n",
            "Reading state information... Done\n",
            "The following package was automatically installed and is no longer required:\n",
            "  libnvidia-common-460\n",
            "Use 'sudo apt autoremove' to remove it.\n",
            "The following NEW packages will be installed:\n",
            "  nodejs\n",
            "0 upgraded, 1 newly installed, 0 to remove and 30 not upgraded.\n",
            "Need to get 27.2 MB of archives.\n",
            "After this operation, 128 MB of additional disk space will be used.\n",
            "Get:1 https://deb.nodesource.com/node_16.x bionic/main amd64 nodejs amd64 16.19.0-deb-1nodesource1 [27.2 MB]\n",
            "Fetched 27.2 MB in 0s (55.9 MB/s)\n",
            "debconf: unable to initialize frontend: Dialog\n",
            "debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 76, <> line 1.)\n",
            "debconf: falling back to frontend: Readline\n",
            "debconf: unable to initialize frontend: Readline\n",
            "debconf: (This frontend requires a controlling tty.)\n",
            "debconf: falling back to frontend: Teletype\n",
            "dpkg-preconfigure: unable to re-open stdin: \n",
            "Selecting previously unselected package nodejs.\n",
            "(Reading database ... 124016 files and directories currently installed.)\n",
            "Preparing to unpack .../nodejs_16.19.0-deb-1nodesource1_amd64.deb ...\n",
            "Unpacking nodejs (16.19.0-deb-1nodesource1) ...\n",
            "Setting up nodejs (16.19.0-deb-1nodesource1) ...\n",
            "Processing triggers for man-db (2.8.3-2ubuntu0.1) ...\n"
          ]
        }
      ],
      "source": [
        "# !curl -fsSL https://deb.nodesource.com/setup_current.x | bash -\n",
        "# !curl -fsSL https://deb.nodesource.com/setup_14.x | bash -\n",
        "!curl -fsSL https://deb.nodesource.com/setup_16.x | bash -\n",
        "# !curl -fsSL https://deb.nodesource.com/setup_18.x | bash -\n",
        "!sudo apt install -y nodejs"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Lmeb2OVanqWd",
        "outputId": "7cda01a3-acd6-4d2c-f740-0b9fbd73aeaa"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\u001b[K\u001b[?25h\n",
            "Scaffolding project in /content/absurd...\n",
            "\n",
            "Done. Now run:\n",
            "\n",
            "  cd absurd\n",
            "  npm install\n",
            "  npm run dev\n",
            "\n",
            "\u001b[37;40mnpm\u001b[0m \u001b[0m\u001b[36;40mnotice\u001b[0m\u001b[35m\u001b[0m \n",
            "\u001b[0m\u001b[37;40mnpm\u001b[0m \u001b[0m\u001b[36;40mnotice\u001b[0m\u001b[35m\u001b[0m New \u001b[31mmajor\u001b[39m version of npm available! \u001b[31m8.19.3\u001b[39m -> \u001b[32m9.3.0\u001b[39m\n",
            "\u001b[0m\u001b[37;40mnpm\u001b[0m \u001b[0m\u001b[36;40mnotice\u001b[0m\u001b[35m\u001b[0m Changelog: \u001b[36mhttps://github.com/npm/cli/releases/tag/v9.3.0\u001b[39m\n",
            "\u001b[0m\u001b[37;40mnpm\u001b[0m \u001b[0m\u001b[36;40mnotice\u001b[0m\u001b[35m\u001b[0m Run \u001b[32mnpm install -g npm@9.3.0\u001b[39m to update!\n",
            "\u001b[0m\u001b[37;40mnpm\u001b[0m \u001b[0m\u001b[36;40mnotice\u001b[0m\u001b[35m\u001b[0m \n",
            "\u001b[0m"
          ]
        }
      ],
      "source": [
        "!npm create -y vite@latest absurd -- --template react-ts"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "!npm install -g npm@9.3.0"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Cv_jcyABhW0G",
        "outputId": "1f9998ae-7e8e-487a-f397-a9ea34d41dc5"
      },
      "execution_count": 20,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\u001b[K\u001b[?25h\n",
            "removed 14 packages, changed 75 packages, and audited 231 packages in 3s\n",
            "\n",
            "14 packages are looking for funding\n",
            "  run `npm fund` for details\n",
            "\n",
            "found \u001b[32m\u001b[1m0\u001b[22m\u001b[39m vulnerabilities\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 23,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "1cVZfRlzu23B",
        "outputId": "b5adab9a-323d-4d31-8d1a-f52ddb7cf395"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Overwriting absurd/package.json\n"
          ]
        }
      ],
      "source": [
        "%%writefile absurd/package.json\n",
        "{\n",
        "\t\"name\": \"absurd\",\n",
        "\t\"private\": true,\n",
        "\t\"type\": \"module\",\n",
        "\t\"version\": \"0.0.1\",\n",
        "\t\"scripts\": {\n",
        "\t\t\"dev\": \"vite --port 8000\",\n",
        "\t\t\"build\": \"tsc && vite build\",\n",
        "\t\t\"preview\": \"vite preview\",\n",
        "\t\t\"test\": \"vitest --reporter=json --outputFile.json=./test/test-results.json --reporter=junit --outputFile.junit=./test/test-results.junit\",\n",
        "\t\t\"test:ui\": \"vitest --ui --reporter=json --outputFile.json=./test/test-results.json --reporter=junit --outputFile.junit=./test/test-results.junit\",\n",
        "\t\t\"test:run\": \"vitest run --outputFile.json=./test-results.json --outputFile.junit=./test-results.junit\",\n",
        "\t\t\"storybook\": \"start-storybook -p 6006\",\n",
        "\t\t\"build-storybook\": \"build-storybook\",\n",
        "\t\t\"build-docs\": \"typedoc packages/million/index.ts packages/react/index.ts packages/html/index.ts packages/morph/index.ts packages/router/index.ts packages/utils/index.ts packages/jsx-runtime/index.ts packages/vite-plugin-million/index.ts --out docs --exclude node_modules --json docs/API.json --darkHighlightTheme github-dark --includes /var/www/projects/beach/resources/notes/ --hideGenerator true --commentStyle all --name 'Million (unoffcial, generated)' --includeVersion true\"\n",
        "\t},\n",
        "\t\"dependencies\": {\n",
        "\t\t\"@monaco-editor/loader\": \"^1.3.2\",\n",
        "\t\t\"@testing-library/react\": \"^13.4.0\",\n",
        "\t\t\"@vitejs/plugin-react\": \"^3.0.1\",\n",
        "\t\t\"bootstrap\": \"^5.2.3\",\n",
        "\t\t\"markdown-it\": \"^13.0.1\",\n",
        "\t\t\"million\": \"^1.12.6-beta.2\",\n",
        "\t\t\"monaco-editor\": \"^0.34.1\",\n",
        "\t\t\"mousetrap\": \"^1.6.5\",\n",
        "\t\t\"react\": \"^18.2.0\",\n",
        "\t\t\"react-bootstrap\": \"^2.7.0\",\n",
        "\t\t\"react-bootstrap-storybook\": \"^1.5.1\",\n",
        "\t\t\"react-dom\": \"^18.2.0\",\n",
        "\t\t\"react-router-dom\": \"^6.5.0\",\n",
        "\t\t\"@jlongster/sql.js\": \"^1.6.7\",\n",
        "\t\t\"absurd-sql\": \"^0.0.46\"\n",
        "\t},\n",
        "\t\"devDependencies\": {\n",
        "\t\t\"@mdx-js/mdx\": \"^2.0.0-rc.2\",\n",
        "\t\t\"@mdx-js/react\": \"^2.2.1\",\n",
        "\t\t\"@mdx-js/rollup\": \"^2.2.1\",\n",
        "\t\t\"@testing-library/jest-dom\": \"^5.16.5\",\n",
        "\t\t\"@vitest/ui\": \"latest\",\n",
        "\t\t\"remark-gfm\": \"^3.0.1\",\n",
        "\t\t\"typedoc-default-themes-dark\": \"^0.12.10\",\n",
        "\t\t\"typescript\": \"^4.9.4\",\n",
        "\t\t\"vite\": \"^4.0.4\",\n",
        "\t\t\"vite-plugin-mdx\": \"^3.5.11\",\n",
        "\t\t\"vitest\": \"^0.25.8\"\n",
        "\t}\n",
        "}"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# !npm install vite -d\n",
        "# !npm install @vitejs/plugin-react -d"
      ],
      "metadata": {
        "id": "1JxvDoCugilL"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "execution_count": 29,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "X5ZHI03jJv4K",
        "outputId": "39364c0f-7679-4f02-ac8f-295b7ffbbf92"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\u001b[K\u001b[?25h\n",
            "up to date, audited 432 packages in 1s\n",
            "\n",
            "169 packages are looking for funding\n",
            "  run `npm fund` for details\n",
            "\n",
            "found \u001b[32m\u001b[1m0\u001b[22m\u001b[39m vulnerabilities\n"
          ]
        }
      ],
      "source": [
        "!cd absurd && npm install\n",
        "#--verbose\n",
        "#--force"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "q1xxZc_fuCrL",
        "outputId": "95459574-da33-48a9-e0c7-b451af59fb72"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Overwriting absurd/vite.config.ts\n"
          ]
        }
      ],
      "source": [
        "%%writefile absurd/vite.config.ts\n",
        "import {\n",
        "  defineConfig\n",
        "} from 'vite';\n",
        "//import mdx from 'vite-plugin-mdx';\n",
        "import mdx from '@mdx-js/rollup'\n",
        "\n",
        "import {\n",
        "  million\n",
        "} from 'million/vite-plugin-million';\n",
        "import react from '@vitejs/plugin-react';\n",
        "\n",
        "const options = {\n",
        "  // See https://mdxjs.com/advanced/plugins\n",
        "  remarkPlugins: [\n",
        "    // E.g. `remark-frontmatter`\n",
        "  ],\n",
        "  rehypePlugins: [],\n",
        "}\n",
        "\n",
        "// https://vitejs.dev/config/\n",
        "export default defineConfig({\n",
        "  plugins: [\n",
        "    {\n",
        "      enforce: 'pre',\n",
        "      ...mdx()\n",
        "    }, \n",
        "    react(), \n",
        "    million({\n",
        "      react: false\n",
        "    }),\n",
        "    {\n",
        "      name: \"configure-response-headers\",\n",
        "      configureServer: (server) => {\n",
        "        server.middlewares.use((_req, res, next) => {\n",
        "          res.setHeader(\"Cross-Origin-Embedder-Policy\", \"require-corp\");\n",
        "          res.setHeader(\"Cross-Origin-Opener-Policy\", \"same-origin\");\n",
        "          next();\n",
        "        });\n",
        "      },\n",
        "    }\n",
        "  ],\n",
        "  //plugins: [{enforce: 'pre', ...mdx()},million()],\n",
        "  resolve: {\n",
        "    preserveSymlinks: true,\n",
        "    extensions: ['*', '.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.dev.js', '.wasm'],\n",
        "    //      fallback: {\n",
        "    //            \"stream\": require.resolve(\"stream-browserify\"),\n",
        "    //            \"buffer\": require.resolve(\"buffer\")\n",
        "    //        }\n",
        "  },\n",
        "  test: {\n",
        "    include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}', 'test/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}']\n",
        "  },\n",
        "  //  esbuild: {\n",
        "  //    jsxFactory: 'h',\n",
        "  //    jsxFragment: 'Fragment',\n",
        "  //jsxInject: `import { h, jsxs, Fragment } from 'million/jsx-runtime'`\n",
        "  //  }\n",
        "  //maybe exclude notes?\n",
        "  //optimizeDeps: {\n",
        "  //    exclude: ['notes'],\n",
        "  //  },\n",
        "});"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "LahA11Ps1bqN",
        "outputId": "ab1fca84-9dc5-44aa-c108-0944f168d4f5"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Overwriting absurd/src/App.tsx\n"
          ]
        }
      ],
      "source": [
        "%%writefile absurd/src/App.tsx\n",
        "import { useState } from 'react'\n",
        "import reactLogo from './assets/react.svg'\n",
        "import './App.css'\n",
        "\n",
        "\n",
        "console.log('init absurd')\n",
        "import { initBackend } from 'absurd-sql/dist/indexeddb-main-thread';\n",
        "\n",
        "function init() {\n",
        "  print('in init plz start worker')\n",
        "  let worker = new Worker(new URL('./index.worker.js', import.meta.url));\n",
        "  // This is only required because Safari doesn't support nested\n",
        "  // workers. This installs a handler that will proxy creating web\n",
        "  // workers through the main thread\n",
        "  initBackend(worker);\n",
        "  worker.addEventListener(\"message\", function handleMessageFromWorker(msg) {\n",
        "    console.log(\"message from worker received in main:\", msg);\n",
        "\n",
        "    const bufTransferredBackFromWorker = msg.data;\n",
        "\n",
        "    console.log(\n",
        "      \"buf.byteLength in main AFTER transfer back from worker:\",\n",
        "      bufTransferredBackFromWorker.byteLength\n",
        "    );\n",
        "  });\n",
        "\n",
        "  // create the buffer\n",
        "  const myBuf = new ArrayBuffer(8);\n",
        "\n",
        "  console.log(\n",
        "    \"buf.byteLength in main BEFORE transfer to worker:\",\n",
        "    myBuf.byteLength\n",
        "  );\n",
        "\n",
        "  // send myBuf to myWorker and transfer the underlying ArrayBuffer\n",
        "  worker.postMessage(myBuf, [myBuf]);\n",
        "\n",
        "  console.log(\n",
        "    \"buf.byteLength in main AFTER transfer to worker:\",\n",
        "    myBuf.byteLength\n",
        "  );\n",
        "}\n",
        "\n",
        "init();\n",
        "\n",
        "\n",
        "\n",
        "function App() {\n",
        "  const [count, setCount] = useState(0)\n",
        "\n",
        "  return (\n",
        "    <div className=\"App\">\n",
        "      <div>\n",
        "        <a href=\"https://vitejs.dev\" target=\"_blank\">\n",
        "          <img src=\"/vite.svg\" className=\"logo\" alt=\"Vite logo\" />\n",
        "        </a>\n",
        "        <a href=\"https://reactjs.org\" target=\"_blank\">\n",
        "          <img src={reactLogo} className=\"logo react\" alt=\"React logo\" />\n",
        "        </a>\n",
        "      </div>\n",
        "      <h1>Vite + React</h1>\n",
        "      <div className=\"card\">\n",
        "        <button onClick={() => setCount((count) => count + 1)}>\n",
        "          count iz {count}\n",
        "        </button>\n",
        "        <p>\n",
        "          Edit <code>src/App.tsx</code> and save to test HMR\n",
        "        </p>\n",
        "      </div>\n",
        "      <p className=\"read-the-docs\">\n",
        "        Click on the Vite and React logos to learn more\n",
        "      </p>\n",
        "    </div>\n",
        "  )\n",
        "}\n",
        "\n",
        "export default App\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "tUBdG_YX1mPt",
        "outputId": "2dc127b9-8a59-4d5a-c9cf-f7d1e3538e02"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Writing absurd/src/index.worker.js\n"
          ]
        }
      ],
      "source": [
        "%%writefile absurd/src/index.worker.js\n",
        "import initSqlJs from '@jlongster/sql.js';\n",
        "import { SQLiteFS } from 'absurd-sql';\n",
        "import IndexedDBBackend from 'absurd-sql/dist/indexeddb-backend';\n",
        "\n",
        "console.log('AT WORKER');\n",
        "\n",
        "self.onmessage = function handleMessageFromMain(msg) {\n",
        "  console.log(\"message from main received in worker:\", msg);\n",
        "\n",
        "  const bufTransferredFromMain = msg.data;\n",
        "\n",
        "  console.log(\n",
        "    \"buf.byteLength in worker BEFORE transfer back to main:\",\n",
        "    bufTransferredFromMain.byteLength\n",
        "  );\n",
        "\n",
        "  // send buf back to main and transfer the underlying ArrayBuffer\n",
        "  self.postMessage(bufTransferredFromMain, [bufTransferredFromMain]);\n",
        "\n",
        "  console.log(\n",
        "    \"buf.byteLength in worker AFTER transfer back to main:\",\n",
        "    bufTransferredFromMain.byteLength\n",
        "  );\n",
        "};\n",
        "\n",
        "async function init() {\n",
        "  console.log('init from in worker')\n",
        "  let SQL = await initSqlJs({ locateFile: file => file });\n",
        "  let sqlFS = new SQLiteFS(SQL.FS, new IndexedDBBackend());\n",
        "  SQL.register_for_idb(sqlFS);\n",
        "\n",
        "  SQL.FS.mkdir('/sql');\n",
        "  SQL.FS.mount(sqlFS, {}, '/sql');\n",
        "\n",
        "  let db = new SQL.Database('/sql/db.sqlite', { filename: true });\n",
        "  db.exec(`\n",
        "    PRAGMA page_size=8192;\n",
        "    PRAGMA journal_mode=MEMORY;\n",
        "  `);\n",
        "  return db;\n",
        "}\n",
        "\n",
        "async function runQueries() {\n",
        "  console.log('absurdly runQueries')\n",
        "  let db = await init();\n",
        "\n",
        "  try {\n",
        "    db.exec('CREATE TABLE kv (key TEXT PRIMARY KEY, value TEXT)');\n",
        "  } catch (e) {}\n",
        "\n",
        "  db.exec('BEGIN TRANSACTION');\n",
        "  let stmt = db.prepare('INSERT OR REPLACE INTO kv (key, value) VALUES (?, ?)');\n",
        "  for (let i = 0; i < 5; i++) {\n",
        "    stmt.run([i, ((Math.random() * 100) | 0).toString()]);\n",
        "  }\n",
        "  stmt.free();\n",
        "  db.exec('COMMIT');\n",
        "\n",
        "  stmt = db.prepare(`SELECT SUM(value) FROM kv`);\n",
        "  stmt.step();\n",
        "  console.log('Result:', stmt.getAsObject());\n",
        "  stmt.free();\n",
        "}\n",
        "\n",
        "runQueries();\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "vHAB1nPsRzzs",
        "outputId": "9a365fc9-a1a4-4577-81b9-4d294295291d"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Cloning into 'absurd-example-project'...\n",
            "remote: Enumerating objects: 21, done.\u001b[K\n",
            "remote: Counting objects: 100% (21/21), done.\u001b[K\n",
            "remote: Compressing objects: 100% (16/16), done.\u001b[K\n",
            "remote: Total 21 (delta 5), reused 20 (delta 4), pack-reused 0\u001b[K\n",
            "Unpacking objects: 100% (21/21), done.\n",
            "package.json  README.md  sql-wasm.wasm\tsrc  webpack.config.js\tyarn.lock\n",
            "index.html    package-lock.json  src\t\t     vite.config.ts\n",
            "node_modules  public\t\t tsconfig.json\n",
            "package.json  sql-wasm.wasm\t tsconfig.node.json\n"
          ]
        }
      ],
      "source": [
        "!git clone https://github.com/jlongster/absurd-example-project.git\n",
        "!ls absurd-example-project\n",
        "!cp absurd-example-project/sql-wasm.wasm absurd\n",
        "!ls absurd"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 17,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 255
        },
        "id": "qaqBfNlXJe6U",
        "outputId": "b1386078-0f1f-449a-efa7-3777d73dbbee"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Now serving Jai at: https://78onvw7kyzt-496ff2e9c6d22116-8000-colab.googleusercontent.com/\n",
            "\n",
            "> absurd@0.0.1 dev\n",
            "> vite --port 8000\n",
            "\n",
            "\n",
            "\u001b[1;1H\u001b[0J\n",
            "  \u001b[32m\u001b[1mVITE\u001b[22m v3.2.5\u001b[39m  \u001b[2mready in \u001b[0m\u001b[1m892\u001b[22m\u001b[2m\u001b[0m ms\u001b[22m\n",
            "\n",
            "  \u001b[32m➜\u001b[39m  \u001b[1mLocal\u001b[22m:   \u001b[36mhttp://localhost:\u001b[1m8000\u001b[22m/\u001b[39m\n",
            "\u001b[2m  \u001b[32m➜\u001b[39m  \u001b[1mNetwork\u001b[22m\u001b[2m: use \u001b[0m\u001b[1m--host\u001b[22m\u001b[2m\u001b[0m to expose\u001b[22m\n",
            "^C\n"
          ]
        }
      ],
      "source": [
        "from google.colab.output import eval_js\n",
        "print('Now serving Jai at:',eval_js(\"google.colab.kernel.proxyPort(8000)\"))\n",
        "!cd absurd && npm run dev"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "W_OEqeHhlUh-"
      },
      "source": [
        "# Absurd MESS"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "WebSocket connection to 'wss://78onvw7kyzt-496ff2e9c6d22116-8000-colab.googleusercontent.com/' failed: \n",
        "setupWebSocket @ client:244\n",
        "client:244 WebSocket connection to 'wss://localhost:8000/' failed: \n",
        "setupWebSocket @ client:244\n",
        "client:227 [vite] failed to connect to websocket.\n",
        "your current setup:\n",
        "  (browser) 78onvw7kyzt-496ff2e9c6d22116-8000-colab.googleusercontent.com/ <--[HTTP]--> localhost:8000/ (server)\n",
        "  (browser) 78onvw7kyzt-496ff2e9c6d22116-8000-colab.googleusercontent.com:/ <--[WebSocket (failing)]--> localhost:8000/ (server)\n",
        "Check out your Vite / network configuration and https://vitejs.dev/config/server-options.html#server-hmr ."
      ],
      "metadata": {
        "id": "43flR-vYf1iY"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "metadata": {
        "id": "PothtjQqVNmr"
      },
      "outputs": [],
      "source": [
        "# !git clone https://github.com/jlongster/absurd-example-project.git"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "N0NwjoObg4KD"
      },
      "source": [
        "first try"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "metadata": {
        "id": "0IhnQb5DgYpe"
      },
      "outputs": [],
      "source": [
        "# !curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -\n",
        "# !echo \"deb https://dl.yarnpkg.com/debian/ stable main\" | sudo tee /etc/apt/sources.list.d/yarn.list\n",
        "# !sudo apt update\n",
        "# !sudo apt install -y yarn\n",
        "\n",
        "# # !sudo apt upgrade nodejs\n",
        "# # !curl -fsSL https://deb.nodesource.com/setup_current.x | bash -\n",
        "\n",
        "# # setup_14.x\n",
        "# https://stackoverflow.com/questions/72921215/getting-glibc-2-28-not-found\n",
        "# !sudo apt-get install libc6\n",
        "# !node\n",
        "# !ldd --version\n",
        "#  #` to install Node.js 16.x and npm\n",
        "# !sudo apt-get install -y nodejs\n",
        "# !sudo apt remove nodejs\n",
        "# ## You may also need development tools to build native addons:\n",
        "#      sudo apt-get install gcc g++ make\n",
        "# ## To install the Yarn package manager, run:\n",
        "#      curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/yarnkey.gpg >/dev/null\n",
        "#      echo \"deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main\" | sudo tee /etc/apt/sources.list.d/yarn.list\n",
        "#      sudo apt-get update && sudo apt-get install yarn"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "5g3fancag6IJ"
      },
      "source": [
        "this should work"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "metadata": {
        "id": "BIchqW11YNca"
      },
      "outputs": [],
      "source": [
        "\n",
        "\n",
        "# curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/yarnkey.gpg >/dev/null\n",
        "# echo \"deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main\" | sudo tee /etc/apt/sources.list.d/yarn.list\n",
        "# sudo apt-get update && sudo apt-get install yarn"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "metadata": {
        "id": "MOkVlM23coCv",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "3d0c00dc-7000-4a0d-d8b4-b4475c08d56b"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "8.19.3\n"
          ]
        }
      ],
      "source": [
        "!npm -v"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 14,
      "metadata": {
        "id": "8c8zrxTthnMQ"
      },
      "outputs": [],
      "source": [
        "# !cd absurd-example-project && ls "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "metadata": {
        "id": "UykfK9wAYW3k"
      },
      "outputs": [],
      "source": [
        "# !cd absurd-example-project && ls && yarn\n",
        "# !cd absurd-example-project && yarn\n",
        "# !yarn absurd-example-project\n",
        "# !cd absurd-example-project && yarn serve --port 8000"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 16,
      "metadata": {
        "id": "1qvOfCsgvC8F"
      },
      "outputs": [],
      "source": [
        "# https://github.com/vitejs/vite/issues/3909 -- vite server headers for that shared array absurd sql uses\n",
        "\n",
        "# \"@storybook/addon-a11y\": \"^6.5.14\",\n",
        "#     \"@storybook/addons\": \"^6.5.14\",\n",
        "#     \"@storybook/components\": \"^6.5.14\",\n",
        "\n",
        "\n",
        "# \"@storybook/addon-actions\": \"^6.5.14\",\n",
        "#     \"@storybook/addon-essentials\": \"^6.5.14\",\n",
        "#     \"@storybook/addon-interactions\": \"^6.5.14\",\n",
        "#     \"@storybook/addon-links\": \"^6.5.14\",\n",
        "#     \"@storybook/builder-vite\": \"^0.2.5\",\n",
        "#     \"@storybook/react\": \"^6.5.14\",\n",
        "#     \"@storybook/testing-library\": \"^0.0.13\",\n",
        "#     \"storybook_vitest_addon\": \"^0.0.8\",\n",
        "#     \"storybook-addon-code-editor\": \"^0.3.7\",\n",
        "#     \"storybook-addon-jarle-monaco\": \"^0.5.0\","
      ]
    }
  ],
  "metadata": {
    "colab": {
      "provenance": [],
      "authorship_tag": "ABX9TyMaVC/DIlVjZfrwXjltwpQ6",
      "include_colab_link": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}