Skip to content

Commit c1748d6

Browse files
authored
fix(runtime): fix cloud sdk cannot initialize in custom deps module (#1711)
* fix(runtime): fix cloud sdk cannot initialize in custom deps module * opt the init scripts * chore: use fixed node version in dockerfile
1 parent 2fbc5f1 commit c1748d6

File tree

8 files changed

+29
-18
lines changed

8 files changed

+29
-18
lines changed

packages/cloud-sdk/package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cloud-sdk/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lafjs/cloud",
3-
"version": "1.0.0-beta.13",
3+
"version": "1.0.0-beta.13-fix",
44
"description": "The cloud sdk for laf cloud function",
55
"main": "dist/index.js",
66
"scripts": {

packages/cloud-sdk/src/cloud.ts

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ export class Cloud implements CloudSdkInterface {
1919
private _cloud: CloudSdkInterface
2020

2121
private get cloud(): CloudSdkInterface {
22+
if (globalThis.createCloudSdk && !Cloud.create) {
23+
Cloud.create = globalThis.createCloudSdk
24+
}
25+
2226
if (!this._cloud) {
2327
this._cloud = Cloud.create()
2428
}

runtimes/nodejs/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM node:20
1+
FROM node:20.10.0
22

33
RUN apt update && apt-get install build-essential libcairo2-dev libpango1.0-dev libjpeg-dev libgif-dev librsvg2-dev git -y
44

runtimes/nodejs/Dockerfile.init

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM node:20
1+
FROM node:20.10.0
22

33
WORKDIR /app
44

runtimes/nodejs/init.sh

+12-11
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,21 @@ if [ -n "$NODE_MODULES_PULL_URL" ]; then
2929
else
3030
elapsed_time=$(expr $end_time - $start_time)
3131
echo "Downloaded node_modules.tar in $elapsed_time seconds."
32-
fi
33-
34-
# untar node_modules.tar
35-
tar -xf node_modules.tar -C .
3632

37-
# check tar exit code
38-
if [ $? -ne 0 ]; then
39-
echo "Failed to extract node_modules cache."
40-
else
41-
end_time_2=$(date +%s)
42-
elapsed_time_2=$(expr $end_time_2 - $end_time)
43-
echo "Extracted node_modules cache in $elapsed_time_2 seconds."
33+
# untar node_modules.tar
34+
tar -xf node_modules.tar -C .
35+
36+
# check tar exit code
37+
if [ $? -ne 0 ]; then
38+
echo "Failed to extract node_modules cache."
39+
else
40+
end_time_2=$(date +%s)
41+
elapsed_time_2=$(expr $end_time_2 - $end_time)
42+
echo "Extracted node_modules cache in $elapsed_time_2 seconds."
43+
fi
4444
fi
4545

46+
4647
# re-enable set -e
4748
set -e
4849
else

runtimes/nodejs/src/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ import xmlparser from 'express-xml-bodyparser'
2121
import './support/cloud-sdk'
2222
import storageServer from './storage-server'
2323
import { DatabaseChangeStream } from './support/database-change-stream'
24+
import { createCloudSdk } from './support/cloud-sdk'
25+
26+
// hack: set createCloudSdk to global object to make it available in @lafjs/cloud package
27+
globalThis.createCloudSdk = createCloudSdk
2428

2529
const app = express()
2630

runtimes/nodejs/src/support/cloud-sdk.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import Config from '../config'
77
import { FunctionContext } from './engine'
88
import { FunctionModule } from './engine/module'
99

10-
Cloud.create = createCloudSdk
10+
if (!Cloud.create) {
11+
Cloud.create = createCloudSdk
12+
}
1113

1214
/**
1315
* object shared cross all functions & requests
@@ -19,7 +21,7 @@ const _shared_preference = new Map<string, any>()
1921
*
2022
* @returns
2123
*/
22-
function createCloudSdk() {
24+
export function createCloudSdk() {
2325
const cloud: CloudSdkInterface = {
2426
database: () => getDb(DatabaseAgent.accessor),
2527
invoke: invokeInFunction,

0 commit comments

Comments
 (0)