-
-
Notifications
You must be signed in to change notification settings - Fork 676
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(runtime): detach custom dependency, support node_module caching (#…
…1658) * feat: add custom dep path * chore: remove log4js from runtime, allow adding dup npm deps * chore: rm unused imports * fix(runtime): support custom dep path in import() * chore: merge upstream main * fix(runtime): add --experimental-vm-modules for runtime * feat(runtime): support node_modules cache in init scripts * fix(runtime): fix download error in init script * feat(server): support node_modules cache in server --------- Co-authored-by: maslow <wangfugen@126.com>
- Loading branch information
Showing
22 changed files
with
9,013 additions
and
1,696 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
FROM lafyun/runtime-node:latest | ||
FROM node:20 | ||
|
||
CMD [ "sh", "/app/init.sh" ] | ||
WORKDIR /app | ||
|
||
COPY ./init.sh /app/init.sh | ||
|
||
CMD [ "sh", "/app/init.sh" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,108 @@ | ||
#!/bin/sh | ||
|
||
set -e | ||
# node ./dist/init.js | ||
|
||
# skip init if $DEPENDENCIES is empty | ||
if [ -z "$DEPENDENCIES" ]; then | ||
echo "No dependencies to install." | ||
cp -r /app/* /tmp/app | ||
exit 0 | ||
fi | ||
|
||
# if $NODE_MODULES_URL is not empty | ||
if [ -n "$NODE_MODULES_PULL_URL" ]; then | ||
echo "Downloading node_modules from $NODE_MODULES_PULL_URL" | ||
|
||
# get start time | ||
start_time=$(date +%s) | ||
|
||
# temporarily disable set -e | ||
set +e | ||
|
||
# download node_modules.tar and untar to `node_modules` | ||
curl -sSfL $NODE_MODULES_PULL_URL -o node_modules.tar | ||
|
||
end_time=$(date +%s) | ||
|
||
# if error | ||
if [ $? -ne 0 ]; then | ||
echo "Failed to download node_modules cache." | ||
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." | ||
fi | ||
|
||
# re-enable set -e | ||
set -e | ||
else | ||
echo "No node_modules cache found, continuing installation." | ||
fi | ||
|
||
CACHED_DEPENDENCIES="" | ||
# if node_modules/.dependencies exists | ||
if [ -f "node_modules/.dependencies" ]; then | ||
CACHED_DEPENDENCIES=`cat node_modules/.dependencies` | ||
fi | ||
|
||
echo "Cached dependencies: $CACHED_DEPENDENCIES" | ||
echo "Dependencies to install: $DEPENDENCIES" | ||
|
||
# if $CACHED_DEPENDENCIES is equal to $DEPENDENCIES | ||
if [ "$CACHED_DEPENDENCIES" = "$DEPENDENCIES" ]; then | ||
echo "No dependencies changed since last cache build." | ||
exit 0 | ||
else | ||
echo "Dependencies changed since last cache build." | ||
fi | ||
|
||
|
||
# npm install $DEPENDENCIES | ||
start_time=$(date +%s) | ||
echo "npm install $DEPENDENCIES $NPM_INSTALL_FLAGS" | ||
npm install $DEPENDENCIES $NPM_INSTALL_FLAGS | ||
cp -r /app/* /tmp/app | ||
end_time=$(date +%s) | ||
elapsed_time=$(expr $end_time - $start_time) | ||
echo "Installed dependencies in $elapsed_time seconds." | ||
|
||
# if $NODE_MODULES_PUSH_URL is not empty | ||
if [ -n "$NODE_MODULES_PUSH_URL" ]; then | ||
# temporarily disable set -e | ||
set +e | ||
|
||
start_time=$(date +%s) | ||
echo $DEPENDENCIES > node_modules/.dependencies | ||
echo "Uploading node_modules to $NODE_MODULES_PUSH_URL" | ||
|
||
# tar `node_modules` to node_modules.tar | ||
tar -cf node_modules.tar ./node_modules | ||
|
||
end_time_1=$(date +%s) | ||
elapsed_time=$(expr $end_time_1 - $start_time) | ||
echo "Compressed node_modules in $elapsed_time seconds." | ||
|
||
# upload node_modules.tar to $NODE_MODULES_PUSH_URL | ||
curl -sSfL -X PUT -T node_modules.tar $NODE_MODULES_PUSH_URL | ||
|
||
|
||
if [ $? -ne 0 ]; then | ||
echo "Failed to upload node_modules cache." | ||
else | ||
end_time_2=$(date +%s) | ||
elapsed_time_2=$(expr $end_time_2 - $end_time) | ||
echo "Uploaded node_modules.tar in $elapsed_time_2 seconds." | ||
fi | ||
|
||
# re-enable set -e | ||
set -e | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
#!/bin/sh | ||
|
||
# source .env | ||
echo "****** start service: node $FLAGS --experimental-fetch ./dist/index.js *******" | ||
exec node $FLAGS ./dist/index.js | ||
echo "****** start service: node $FLAGS --experimental-vm-modules --experimental-fetch ./dist/index.js *******" | ||
exec node $FLAGS --experimental-vm-modules --experimental-fetch ./dist/index.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.