Open nazrinnoorzan opened 3 years ago
Owh yeah, and I noticed why the folder and file name here inside the Lambda is difference? Why it becomes __resources/lambda_image.js ? And even I test execute directly from here also fail.
That's odd, I wouldn't expect to see __resources
. Can you share what your PHP class looks like?
Is this can only be used with Vapor?
Also no, you should be able to run it from localhost just fine!
That's odd, I wouldn't expect to see
__resources
. Can you share what your PHP class looks like?
app/Sidecar/OgImage.php
<?php
namespace App\Sidecar;
use Hammerstone\Sidecar\LambdaFunction;
class OgImage extends LambdaFunction
{
public function handler()
{
// Define your handler function.
// (Javascript file + export name.)
return 'resources/lambda/image.handler';
}
public function package()
{
// All files and folders needed for the function.
return [
'resources/lambda',
];
}
}
resources/lambda/image.js
// const { createCanvas } = require("canvas");
exports.handler = async function (event) {
console.log(event);
// const canvas = createCanvas(1200, 630);
// const context = canvas.getContext("2d");
// context.font = "bold 70pt Helvetica";
// context.textAlign = "center";
// context.fillStyle = "#3574d4";
// // Read the text out of the event passed in from PHP.
// context.fillText(event.text, 600, 170);
// // Return an image.
// return canvas.toDataURL("image/jpeg");
};
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Sidecar\OgImage;
Route::get('/', function () {
return view('welcome');
});
Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
Route::get('/ogimage', function () {
return OgImage::execute([
'title' => 'Executing Functions',
'url' => 'https://hammerstone.dev/sidecar/docs/main/functions/executing',
'template' => 'docs/sidecar'
]);
});
That's quite strange. I ran it locally and am seeing the expected folder structure.
Are you running this from a Windows machine? I might have to check on that if so
@aarondfrancis
Yup, on Windows 10.
For now my work around is edit back the handler function to the correct path created inside the Lambda, in this case return './__resources/lambda/image.handler';
I'll check on my 2nd Windows PC also to see this is happening or not.
@nazrinnoorzan thanks for the report. I'll try to add a Windows machine to the GitHub actions build so I can test it there. I don't really understand where that would be coming from. It looks like it's also removing the lambda
folder and renaming your file lambda_image.js
for some reason.
I have this same problem on Windows 11.
Isolated the problem to the Hammerstone\Sidecar\Package
class.
Paths have an extra \\
at the beginning after removing the base path.
As @wilsenhc said the problem is in Hammerstone\Sidecar\Package
class, looks to specifically be
prependBasePath()
which uses DIRECTORY_SEPARATOR
which resolves to \
on windows which affects the path written into the zip. Replacing with '/'
instead of DIRECTORY_SEPARATOR
fixes the issue. Other platforms should already be resolving to /
Is this can only be used with Vapor?
I've setup a local Laravel and follow your code implementation example, and I don't have any error when running the command php artisan sidecar:deploy --activate
But on the /ogimage, I got error saying - Lambda Execution Exception for App\Sidecar\OgImage: "Error: Cannot find module 'image' Require stack: - /var/runtime/UserFunction.js - /var/runtime/index.js. [TRACE] Runtime.ImportModuleError: Error: Cannot find module 'image' Require stack: - /var/runtime/UserFunction.js".