|
| 1 | +/* |
| 2 | + * Copyright (C) 2024-present Puter Technologies Inc. |
| 3 | + * |
| 4 | + * This file is part of Puter. |
| 5 | + * |
| 6 | + * Puter is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Affero General Public License as published |
| 8 | + * by the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Affero General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Affero General Public License |
| 17 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +const configurable_auth = require("../../middleware/configurable_auth"); |
| 21 | +const { Endpoint } = require("../../util/expressutil"); |
| 22 | +const BaseService = require("../BaseService"); |
| 23 | +const fs = require("node:fs"); |
| 24 | + |
| 25 | +const { createWorker, setCloudflareKeys, deleteWorker } = require("./workerUtils/cloudflareDeploy"); |
| 26 | +const { getUserInfo } = require("./workerUtils/puterUtils"); |
| 27 | + |
| 28 | +// This file is generated by webpack. To rebuild: cd to this directory and run `npm run build` |
| 29 | +let preamble; |
| 30 | +try { |
| 31 | + preamble = fs.readFileSync(__dirname + "/dist/workerPreamble.js", "utf-8"); |
| 32 | +} catch (e) { |
| 33 | + preamble = ""; |
| 34 | + console.error("WORKERS ERROR: Preamble has not been built! Workers will not have access to puter.js\nTo fix this cd into src/backend/src/worker and run npm run build") |
| 35 | +} |
| 36 | +const PREAMBLE_LENGTH = preamble.split("\n").length - 1 |
| 37 | + |
| 38 | +class WorkerService extends BaseService { |
| 39 | + ['__on_install.routes'](_, { app }) { |
| 40 | + setCloudflareKeys(this.config); |
| 41 | + |
| 42 | + } |
| 43 | + static IMPLEMENTS = { |
| 44 | + ['workers']: { |
| 45 | + async create({ fileData, workerName, authorization }) { |
| 46 | + try { |
| 47 | + const userData = await getUserInfo(authorization, this.global_config.api_base_url); |
| 48 | + return await createWorker(userData, authorization, workerName, preamble + fileData, PREAMBLE_LENGTH); |
| 49 | + } catch (e) { |
| 50 | + return {success: false, e} |
| 51 | + } |
| 52 | + }, |
| 53 | + async destroy({ workerName, authorization }) { |
| 54 | + try { |
| 55 | + const userData = await getUserInfo(authorization, this.global_config.api_base_url); |
| 56 | + return await deleteWorker(userData, authorization, workerName); |
| 57 | + } catch (e) { |
| 58 | + return {success: false, e} |
| 59 | + } |
| 60 | + }, |
| 61 | + async startLogs({ workerName, authorization }) { |
| 62 | + return await this.exec_({ runtime, code }); |
| 63 | + }, |
| 64 | + async endLogs({ workerName, authorization }) { |
| 65 | + return await this.exec_({ runtime, code }); |
| 66 | + }, |
| 67 | + } |
| 68 | + } |
| 69 | + async ['__on_driver.register.interfaces']() { |
| 70 | + const svc_registry = this.services.get('registry'); |
| 71 | + const col_interfaces = svc_registry.get('interfaces'); |
| 72 | + |
| 73 | + col_interfaces.set('workers', { |
| 74 | + description: 'Execute code with various languages.', |
| 75 | + methods: { |
| 76 | + create: { |
| 77 | + description: 'Create a backend worker', |
| 78 | + parameters: { |
| 79 | + fileData: { |
| 80 | + type: "string", |
| 81 | + description: "The code of the worker to upload" |
| 82 | + }, |
| 83 | + workerName: { |
| 84 | + type: "string", |
| 85 | + description: "The name of the worker you want to upload" |
| 86 | + }, |
| 87 | + authorization: { |
| 88 | + type: "string", |
| 89 | + description: "Puter token" |
| 90 | + } |
| 91 | + }, |
| 92 | + result: { type: 'json' }, |
| 93 | + }, |
| 94 | + startLogs: { |
| 95 | + description: 'Get logs for your backend worker', |
| 96 | + parameters: { |
| 97 | + workerName: { |
| 98 | + type: "string", |
| 99 | + description: "The name of the worker you want the logs of" |
| 100 | + }, |
| 101 | + authorization: { |
| 102 | + type: "string", |
| 103 | + description: "Puter token" |
| 104 | + } |
| 105 | + }, |
| 106 | + result: { type: 'json' }, |
| 107 | + }, |
| 108 | + endLogs: { |
| 109 | + description: 'Get logs for your backend worker', |
| 110 | + parameters: { |
| 111 | + workerName: { |
| 112 | + type: "string", |
| 113 | + description: "The name of the worker you want the logs of" |
| 114 | + }, |
| 115 | + authorization: { |
| 116 | + type: "string", |
| 117 | + description: "Puter token" |
| 118 | + } |
| 119 | + }, |
| 120 | + result: { type: 'json' }, |
| 121 | + }, |
| 122 | + destroy: { |
| 123 | + description: 'Get rid of your backend worker', |
| 124 | + parameters: { |
| 125 | + workerName: { |
| 126 | + type: "string", |
| 127 | + description: "The name of the worker you want to destroy" |
| 128 | + }, |
| 129 | + authorization: { |
| 130 | + type: "string", |
| 131 | + description: "Puter token" |
| 132 | + } |
| 133 | + }, |
| 134 | + result: { type: 'json' }, |
| 135 | + }, |
| 136 | + } |
| 137 | + }); |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +module.exports = { |
| 142 | + WorkerService, |
| 143 | +}; |
0 commit comments