CljKit

node v10.24.1
version: 2.0.0
endpointsharetweet
const sci = require("@borkdude/sci"); const filename = require.resolve("@borkdude/sci"); const { dirname } = require("path"); const NODE_PATH = dirname(dirname(dirname(filename)));
const dynamicRequire = n => { const p = `${NODE_PATH}/${n}` return require(p); }
// Q: Why doesn't this work? (peer dependency hack?) require('request') dynamicRequire("request-promise-native")
const opts = (outBuf) => ({bindings: { println: (...x) => { outBuf.length === 0 && outBuf.push([[]]); outBuf[outBuf.length - 1][0].push(...(x.map(t => JSON.stringify(t))), "\n"); }, __nextCellBlock: () => { outBuf.push([[]]); }, __pushRes: (x) => {outBuf[outBuf.length - 1][1] = x}, npm: dynamicRequire, jsCall: (f, t, ...args) => f.call(t, ...args), jsCallMethod: (o, m, ...args) => o[m].call(o, ...args), jsGet: (o, k) => o[k], }}) sci.evalString(`(println "hello world")`, opts([])) // require("request-promise") // require('request') opts([]).bindings.npm("request") opts([]).bindings.println("hi") // opts.bindings.npm("request-promise") // sci.evalString(`(do // (npm "request") // (println (npm "request-promise")) // 3 // )`, opts([])) const eval = s => { const outBuf = [] const res = sci.evalString(s, opts(outBuf)) if (outBuf.length === 0) { outBuf.push([[]]) } if (outBuf[outBuf.length - 1][1] === undefined) { outBuf[outBuf.length -1][1] = res } return outBuf }
eval(` (clj->js {:cell1 (inc 123) :cell2 (inc 5)}) `)
eval(`(clj->js {:cell1 (inc 123) :cell2 (println (inc 5))})`)
// What parts need to be implemented?
1. We have to create an endpoint that evals cljs
2. We have to create and endpoint that returns the cljkit webpage
exports.endpoint = function(req, res) { res.writeHead(200, { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "*" }); if (req.method === "POST") { let body = ""; req.on("data", chunk => { body += chunk.toString(); // convert Buffer to string }); req.on("end", () => { console.log(body); try { const output = eval(body); res.end(JSON.stringify(output)); } catch (e) { res.end(JSON.stringify(e)); } }); } else { res.end("hi"); } };
Loading…

no comments

    sign in to comment