serverless-offline-reasint
Version:
Emulate AWS λ and API Gateway locally when developing your Serverless project
117 lines (93 loc) • 2.45 kB
JavaScript
import { EOL } from "node:os"
import process from "node:process"
import { log } from "@serverless/utils/log.js"
import { invokeJavaLocal } from "java-invoke-local"
const { parse, stringify } = JSON
const { hasOwn } = Object
export default class JavaRunner {
static
constructor(funOptions, env) {
const { functionName, handler, servicePackage, functionPackage } =
funOptions
this.
this.
this.
this.
}
// no-op
// () => void
cleanup() {}
for (const item of value.split(EOL)) {
let json
// first check if it's JSON
try {
json = parse(item)
// nope, it's not JSON
} catch {
// no-op
}
// now let's see if we have a property __offline_payload__
if (
json &&
typeof json === "object" &&
hasOwn(json, JavaRunner.
) {
return json[JavaRunner.
}
}
return undefined
}
async run(event, context) {
const input = stringify({
context,
event,
})
const data = stringify({
artifact: this.
data: input,
function: this.
handler: this.
jsonOutput: true,
serverlessOffline: true,
})
const httpOptions = {
body: data,
method: "POST",
}
const port = process.env.JAVA_OFFLINE_SERVER || 8080
let result
try {
// Assume java-invoke-local server is running
const response = await fetch(
`http://localhost:${port}/invoke`,
httpOptions,
)
result = await response.text()
} catch {
log.notice(
'Local java server not running. For faster local invocations, run "java-invoke-local --server" in your project directory',
)
// Fallback invocation
const args = [
"-c",
this.
"-a",
this.
"-f",
this.
"-d",
input,
"--json-output",
"--serverless-offline",
]
result = invokeJavaLocal(args, this.
log.notice(result)
}
return this.
}
}