Is it possible dynamically create serverless function on vercel from code?

maybe it's somehow possible from build output api?
8 Replies
gxp91
gxp91•12mo ago
could you give some more context on the ask i.e what is your end goal? You could technically use the eval function in javascript to allow dynamically generated code to run but it is extremly dangerous and should only be used when you know what you are doing and the code is NOT coming from a users input since it could lead to cross site scripting attacks.
web3moloh
web3moloh•12mo ago
Generally i want to call serverless function on vercel and create another serveless function in runtime that it was possible immediately call new function after creation without rebuilding a project. Problem with any attacks or danger code i want to solve by creating branch on github under every username that every user has separate codebase and environment (how i understand vercel creating unique environment under every commit and branch) not sure about this solution but i think it could work. Originally Idea that user + AI can create automative solution by prompt. Like 30+ nodes(serverless functions) with different logic and they can communicate with each other by defined protocol. This is how i am trying to create function that create serverless function https://github.com/andyvauliln/turbo-demo/blob/main/apps/web/api/create/run.js
GitHub
turbo-demo/apps/web/api/create/run.js at main · andyvauliln/turbo-d...
Contribute to andyvauliln/turbo-demo development by creating an account on GitHub.
web3moloh
web3moloh•12mo ago
it's an error Unhandled Promise Rejection {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"Error: ENOENT: no such file or directory, mkdir '.vercel'","reason":{"errorType":"Error","errorMessage":"ENOENT: no such file or directory, mkdir '.vercel'","code":"ENOENT","errno":-2,"syscall":"mkdir","path":".vercel","stack":["Error: ENOENT: no such file or directory, mkdir '.vercel'"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: Error: ENOENT: no such file or directory, mkdir '.vercel'"," at process.<anonymous> (file:///var/runtime/index.mjs:1189:17)"," at process.emit (node:events:525:35)"," at process.emit (/var/task/_vc/sourcemap_support.js:602:21)"," at emit (node:internal/process/promises:149:20)"," at processPromiseRejections (node:internal/process/promises:283:27)"," at processTicksAndRejections (node:internal/process/task_queues:96:32)"]} Unknown application error occurred Runtime.Unknown
gxp91
gxp91•12mo ago
yeah so instead of writing the code to a file you cul just use the eval function to run the code here is an over simplified example of what i mean. Lets say on aa database/repo you have some code you want to remotely execute i.e GitHub Repo: testRepo File: helloWorld.js Content:
function print(msg, usr) {
return `${usr} says: ${msg}`;
}

console.log(print('testing 1234', 'gxp91');
function print(msg, usr) {
return `${usr} says: ${msg}`;
}

console.log(print('testing 1234', 'gxp91');
in your code you would have some logic that retrieves that code then to execute it you would execute it using eval i.e
const code = asyn getCode();
eval(code);
const code = asyn getCode();
eval(code);
This can get tricky because if the code is asyncronous then you will have promise race conditions ect. you could get a little fancier and wrap the external async in a promise to handle that and potentially chain it but that is one potential way of doing it (may not be the best)
web3moloh
web3moloh•12mo ago
hm, iya this could work i just not sure about package installation, i think if not possible create function dynamically i can commit it on github and turbo build it'll take much more time but generally looks better than eval in long term vision because ll be cool later has features like see changes history and navigate there, plus you can always see your changes, better to test and have logs from (build and runtime)
gxp91
gxp91•12mo ago
if you wanted all of that to work instead of writing the code into a file you could integrate with github and auto commit the "new code" into some sort of file structure in your app and then get another webhook to "auto deploy" your vercel app but thats ALOT more work 🙂 at this point your basically creating a SaaS. I would recommend looking at some other open source saas solutions to see how they do it.
web3moloh
web3moloh•12mo ago
iay could work, i am going do open source so maybe later some one help with ALOT of work), ok thank you so much, ll try work on concept further
gxp91
gxp91•12mo ago
Be careful not to publish your api keys on github :3
Want results from more Discord servers?
Add your server
More Posts