Skip to content

Commit

Permalink
fix(runtime): fix cloud sdk cannot initialize in custom deps module (#…
Browse files Browse the repository at this point in the history
…1711)

* fix(runtime): fix cloud sdk cannot initialize in custom deps module

* opt the init scripts

* chore: use fixed node version in dockerfile
  • Loading branch information
maslow committed Nov 27, 2023
1 parent 2fbc5f1 commit c1748d6
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 18 deletions.
4 changes: 2 additions & 2 deletions packages/cloud-sdk/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/cloud-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lafjs/cloud",
"version": "1.0.0-beta.13",
"version": "1.0.0-beta.13-fix",
"description": "The cloud sdk for laf cloud function",
"main": "dist/index.js",
"scripts": {
Expand Down
4 changes: 4 additions & 0 deletions packages/cloud-sdk/src/cloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export class Cloud implements CloudSdkInterface {
private _cloud: CloudSdkInterface

private get cloud(): CloudSdkInterface {
if (globalThis.createCloudSdk && !Cloud.create) {
Cloud.create = globalThis.createCloudSdk
}

if (!this._cloud) {
this._cloud = Cloud.create()
}
Expand Down
2 changes: 1 addition & 1 deletion runtimes/nodejs/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:20
FROM node:20.10.0

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

Expand Down
2 changes: 1 addition & 1 deletion runtimes/nodejs/Dockerfile.init
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:20
FROM node:20.10.0

WORKDIR /app

Expand Down
23 changes: 12 additions & 11 deletions runtimes/nodejs/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,21 @@ if [ -n "$NODE_MODULES_PULL_URL" ]; then
else
elapsed_time=$(expr $end_time - $start_time)
echo "Downloaded node_modules.tar in $elapsed_time seconds."
fi

# untar node_modules.tar
tar -xf node_modules.tar -C .

# check tar exit code
if [ $? -ne 0 ]; then
echo "Failed to extract node_modules cache."
else
end_time_2=$(date +%s)
elapsed_time_2=$(expr $end_time_2 - $end_time)
echo "Extracted node_modules cache in $elapsed_time_2 seconds."
# untar node_modules.tar
tar -xf node_modules.tar -C .

# check tar exit code
if [ $? -ne 0 ]; then
echo "Failed to extract node_modules cache."
else
end_time_2=$(date +%s)
elapsed_time_2=$(expr $end_time_2 - $end_time)
echo "Extracted node_modules cache in $elapsed_time_2 seconds."
fi
fi


# re-enable set -e
set -e
else
Expand Down
4 changes: 4 additions & 0 deletions runtimes/nodejs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import xmlparser from 'express-xml-bodyparser'
import './support/cloud-sdk'
import storageServer from './storage-server'
import { DatabaseChangeStream } from './support/database-change-stream'
import { createCloudSdk } from './support/cloud-sdk'

// hack: set createCloudSdk to global object to make it available in @lafjs/cloud package
globalThis.createCloudSdk = createCloudSdk

const app = express()

Expand Down
6 changes: 4 additions & 2 deletions runtimes/nodejs/src/support/cloud-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import Config from '../config'
import { FunctionContext } from './engine'
import { FunctionModule } from './engine/module'

Cloud.create = createCloudSdk
if (!Cloud.create) {
Cloud.create = createCloudSdk
}

/**
* object shared cross all functions & requests
Expand All @@ -19,7 +21,7 @@ const _shared_preference = new Map<string, any>()
*
* @returns
*/
function createCloudSdk() {
export function createCloudSdk() {
const cloud: CloudSdkInterface = {
database: () => getDb(DatabaseAgent.accessor),
invoke: invokeInFunction,
Expand Down

0 comments on commit c1748d6

Please sign in to comment.