vaporia

0.0.1 • Public • Published

Vapornet

Build a Vapor Node

Usage

1. Frontend

Posting a JSON payload

<html>
<head>
<meta name="csrf" content="<%=csrf%>"
<script src="https://unpkg.com/vapornet"></script>
</head>
<body>
<input type="text" id='text'>
<script>
const url = "https://3fa76d5810a8.ngrok.io"
const vapor = new Vapornet(url)
document.querySelector("#text").addEventListener("change", async (e) => {
  const response = await vapor.post({
    modal: { title: "Sign", text: "Sign a message" },
    method: "post",
    path: "/posts",
    data: { text: e.target.value }
  })
  console.log("rseponse = ", response)
})
</script>
</body>
</html>

Posting file

<html>
<head>
<meta name="csrf" content="<%=csrf%>"
<script src="https://unpkg.com/vapornet"></script>
</head>
<body>
<input type='file' id='upload'>
<script>
document.querySelector("#upload").addEventListener("change", (e) => {
  let reader = new FileReader();
  reader.onloadend = async () => {
    let res = await vapor.post({
      modal: { title: "Sign", text: "Sign File buffer" },
      method: "post",
      path: "/files",
      data: reader.result
    })
    console.log("response = ", res)
  };
  reader.readAsArrayBuffer(e.target.files[0]);
})
</script>
</body>
</html>

2. Backend

const Vapornet = require('vapornet')
const fs = require('fs')
const { app, express, bitcoin } = new Vapornet(3006)
if (!fs.existsSync("files")) fs.mkdirSync("files") 

// Express
app.set('view engine', 'ejs')
app.set('views', path.resolve(process.cwd(), '../views'));
app.use(express.static(path.join(process.cwd(), '../public')))
app.get("/", (req, res) => {
  res.render("index", { token: req.csrfToken()  })
})
app.get("/files/:txid", (req, res) => {
  fs.createReadStream("files/" + req.params.txid).pipe(res)
})

// Vapor
bitcoin.post("/files", async (req, res, data) => {
  let inspect = packet.inspect({ tx: data.tx })
  if (/.*files$/.test(inspect.out[1].s4)) {
    let fb = inspect.out[1].b5
    fs.writeFile("files/" + inspect.id, fb, (err, r) => {
      console.log("saved file", inspect.id, data.id)
      res.json({ id: data.id })
    })
  }
})
bitcoin.post("/posts", async (req, res, data) => {
  res.json({ id: data.id })
})

Readme

Keywords

none

Package Sidebar

Install

npm i vaporia

Weekly Downloads

1

Version

0.0.1

License

MIT

Unpacked Size

8.03 kB

Total Files

4

Last publish

Collaborators

  • sobachef