runtime/nodejs/v20/lib/launcher.js (54 lines of code) (raw):
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
try {
const main = require("./index__.js")
const readline = require('readline');
const fs = require("fs")
const os = require("os")
async function actionLoop() {
const out = fs.createWriteStream(null,
{ fd: 3, encoding: "utf8" })
process.stdin.setEncoding('utf8');
const rl = readline.createInterface({
input: process.stdin
});
out.write(JSON.stringify({ "ok": true }) + "\n");
for await (const line of rl) {
try {
let args = JSON.parse(line)
let value = args.value || {}
for (let key in args) {
if (key !== "value") {
let envar = "__OW_" + key.toUpperCase()
process.env[envar] = args[key]
}
}
let result = {}
result = main(value)
if (typeof result === 'undefined') {
result = {}
}
if (Promise.resolve(result) == result)
try {
result = await result
} catch (error) {
if (typeof error === 'undefined') {
error = {}
}
result = { "error": error }
}
out.write(JSON.stringify(result) + "\n");
} catch (err) {
console.log(err);
let message = err.message || err.toString()
let error = { "error": message }
out.write(JSON.stringify(error) + "\n");
}
}
}
actionLoop()
} catch (e) {
if (e.code == "MODULE_NOT_FOUND") {
console.log("zipped actions must contain either package.json or index.js at the root.")
}
console.log(e)
process.exit(1)
}