Skip to content

Commit

Permalink
fix: windows platform specific runtime fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Inqnuam committed May 21, 2023
1 parent 0583edf commit 5ff4798
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-aws-lambda",
"version": "4.5.5",
"version": "4.5.6",
"description": "AWS Application Load Balancer and API Gateway - Lambda dev tool for Serverless. Allows Express synthax in handlers. Supports packaging, local invoking and offline ALB, APG, S3, SNS, SQS, DynamoDB Stream server mocking.",
"author": "Inqnuam",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ class ServerlessAwsLambda extends Daemon {
const handlerPath = (lambda as Serverless.FunctionDefinitionHandler).handler;
const ext = path.extname(handlerPath);
const handlerName = ext.slice(1);
const esEntryPoint = path.posix.resolve(handlerPath.replace(ext, ""));
const esEntryPoint = path.resolve(handlerPath.replace(ext, ""));

const region = this.runtimeConfig.environment.AWS_REGION ?? this.runtimeConfig.environment.REGION;
const slsDeclaration: any = this.serverless.service.getFunction(funcName);
Expand Down
9 changes: 4 additions & 5 deletions src/lib/runtime/runners/python/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ def decimal_serializer(o):

handlerPath = sys.argv[1]
handlerName = sys.argv[2]
lambdaName = sys.argv[3]
timeout = int(sys.argv[4])
timeout = int(sys.argv[3])
sys.path.append(".")


class LambdaContext(object):
def __init__(self, reqId="1234567890"):
self.name = lambdaName
self.name = os.environ["AWS_LAMBDA_FUNCTION_NAME"]
self.version = "$LATEST"
self.created = time()
self.timeout = timeout
Expand Down Expand Up @@ -72,7 +71,7 @@ def log(self):
_mods = [m.__name__ for m in sys.modules.values() if m]

watchFiles = json.dumps([x for x in _mods if x.startswith("src")])
sys.stdout.write(f"__|watch|__{watchFiles}")
print(f"__|watch|__{watchFiles}__|watchEnd|__", flush=True)
sys.stdout.flush()

for line in sys.stdin:
Expand All @@ -82,7 +81,7 @@ def log(self):
response = handler(input["event"], context)
jsonRes = json.dumps(response, default=decimal_serializer)
sys.stdout.flush()
sys.stdout.write(f"__|response|__{jsonRes}")
print(f"__|response|__{jsonRes}__|responseEnd|__", flush=True)
sys.stdout.flush()
except:
error, ex_value, ex_traceback = sys.exc_info()
Expand Down
40 changes: 30 additions & 10 deletions src/lib/runtime/runners/python/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ export class PythonRunner implements Runner {
filesTime: Map<string, number> = new Map();
watcherListener: (event: "rename" | "change", filename: string | Buffer) => void;
emitRebuild: Function;
static wrapper = __dirname.replace("/dist", "/src/lib/runtime/runners/python/index.py");
static wrapper = __dirname.replace(`${path.sep}dist`, "/src/lib/runtime/runners/python/index.py");
static DELIMITER = "__|response|__";
static DELIMITEREND = "__|responseEnd|__";
static ERR_RESPONSE = "__|error|__";
static WATCH = "__|watch|__";
static WATCHEND = "__|watchEnd|__";
constructor(
{
name,
Expand Down Expand Up @@ -97,14 +99,22 @@ export class PythonRunner implements Runner {
const data = chunk.toString();

try {
if (data.includes(PythonRunner.WATCH)) {
const hasWatchFiles = data.includes(PythonRunner.WATCH);
if (hasWatchFiles) {
this.setWatchFiles(data);
} else if (data.includes(PythonRunner.DELIMITER)) {
}

if (data.includes(PythonRunner.DELIMITER)) {
const output = data.split(PythonRunner.DELIMITER);
const res = output[output.length - 1];
const res = output[output.length - 1].split(PythonRunner.DELIMITEREND)[0];

if (output.length > 1) {
const printable = output.slice(0, -1).join("\n");
let printable;
if (hasWatchFiles) {
printable = output[0].split(PythonRunner.WATCHEND)[1];
} else {
printable = output.slice(0, -1).join("\n");
}
if (printable.trim()) {
console.log(printable);
}
Expand All @@ -117,7 +127,15 @@ export class PythonRunner implements Runner {
this.python!.stderr.removeListener("data", errorHandler);
resolve(result);
} else if (data.trim()) {
console.log(data);
let printable;
if (hasWatchFiles) {
printable = data.split(PythonRunner.WATCHEND)[1];
} else {
printable = data;
}
if (printable.trim()) {
console.log(printable);
}
}
} catch (error) {
console.log("err", error);
Expand Down Expand Up @@ -161,8 +179,10 @@ export class PythonRunner implements Runner {

setWatchFiles = async (data: string) => {
try {
const output = data.split(PythonRunner.WATCH);
const rawFiles = output[output.length - 1];
const rawOutput = data.split(PythonRunner.WATCH);
const output = rawOutput[rawOutput.length - 1].split(PythonRunner.WATCHEND);
const rawFiles = output[0];

const files = JSON.parse(rawFiles).map((x: string) => `${x.replace(/\./g, path.sep)}.py`);

for (const f of files) {
Expand All @@ -177,10 +197,10 @@ export class PythonRunner implements Runner {
};
load = () => {
if (!this.bin) {
this.bin = this.runtime.includes(".") ? this.runtime.split(".")[0] : this.runtime;
this.bin = process.platform === "win32" ? "python" : this.runtime.includes(".") ? this.runtime.split(".")[0] : this.runtime;
}

this.python = spawn(this.bin, ["-u", PythonRunner.wrapper, this.pyModulePath, this.handlerName, this.name, String(this.timeout)], {
this.python = spawn(this.bin, [PythonRunner.wrapper, this.pyModulePath, this.handlerName, String(this.timeout)], {
env: this.environment,
});

Expand Down
2 changes: 1 addition & 1 deletion src/lib/runtime/runners/ruby/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class RubyRunner implements Runner {
emitRebuild: Function;
watcherListener: (event: "rename" | "change", filename: string | Buffer) => void;
watchers: FSWatcher[] = [];
static wrapper = __dirname.replace("/dist", "/src/lib/runtime/runners/ruby/index.rb");
static wrapper = __dirname.replace(`${path.sep}dist`, "/src/lib/runtime/runners/ruby/index.rb");
static DELIMITER = "__|response|__";
static ERR_RESPONSE = "__|error|__";
static WATCH = "__|watch|__";
Expand Down

0 comments on commit 5ff4798

Please sign in to comment.