Skip to content

Commit a03e0e8

Browse files
authored
ASGI Implementation (#2)
* ASGI Implementation * Dispatch coros to Python event loop thread This rewrites the Asgi to dispatch each ASGI call coroutine to a separate Python event loop thread which will spin forever.
1 parent 4e003f5 commit a03e0e8

29 files changed

+3597
-455
lines changed

.cargo/cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[target.x86_64-unknown-linux-gnu]
2+
rustflags = ["-C", "link-args=-Wl,-export-dynamic"]
3+
4+
[target.aarch64-unknown-linux-gnu]
5+
rustflags = ["-C", "link-args=-Wl,-export-dynamic"]

.github/workflows/CI.yml

Lines changed: 172 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ env:
44
DEBUG: napi:*
55
APP_NAME: python-node
66
MACOSX_DEPLOYMENT_TARGET: '10.13'
7+
CARGO_NET_GIT_FETCH_WITH_CLI: 'true'
78

89
permissions:
910
contents: write
@@ -23,6 +24,10 @@ on:
2324
- docs/**
2425
pull_request:
2526

27+
concurrency:
28+
group: ${{ github.workflow }}-${{ github.ref }}-ci
29+
cancel-in-progress: true
30+
2631
jobs:
2732
build:
2833
strategy:
@@ -45,7 +50,9 @@ jobs:
4550
- uses: actions/checkout@v4
4651
- uses: webfactory/ssh-agent@v0.9.0
4752
with:
48-
ssh-private-key: ${{ secrets.HTTP_HANDLER_ACCESS_TOKEN }}
53+
ssh-private-key: |
54+
${{ secrets.HTTP_HANDLER_ACCESS_TOKEN }}
55+
${{ secrets.HTTP_REWRITER_ACCESS_TOKEN }}
4956
- uses: pnpm/action-setup@v4
5057
with:
5158
version: latest
@@ -88,22 +95,52 @@ jobs:
8895
run: |
8996
set -x
9097
98+
export CARGO_NET_GIT_FETCH_WITH_CLI=true
99+
91100
# Install apt dependencies
92101
apt-get update -y
93-
apt-get install -y openssh-client
102+
apt-get install -y openssh-client python3 python3-dev
94103
95104
# Setup pnpm
96105
corepack disable
97106
npm i -gf pnpm
98107
99-
# Set up SSH key (to checkout private repos with cargo)
108+
# Set up SSH keys (to checkout private repos with cargo)
100109
mkdir -p ~/.ssh
101-
chmod -R 400 ~/.ssh
102-
touch ~/.ssh/config ~/.ssh/known_hosts
103-
eval `ssh-agent -s`
104-
echo "${{ secrets.HTTP_HANDLER_ACCESS_TOKEN }}" | tr -d '\r' | ssh-add -
105-
ssh-add -l
106-
ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts
110+
chmod 700 ~/.ssh
111+
112+
# Save SSH keys to files
113+
echo "${{ secrets.HTTP_HANDLER_ACCESS_TOKEN }}" | tr -d '\r' > ~/.ssh/http_handler_key
114+
echo "${{ secrets.HTTP_REWRITER_ACCESS_TOKEN }}" | tr -d '\r' > ~/.ssh/http_rewriter_key
115+
chmod 600 ~/.ssh/http_handler_key
116+
chmod 600 ~/.ssh/http_rewriter_key
117+
118+
# Add GitHub to known hosts (for all aliases)
119+
ssh-keyscan -H github.com >> ~/.ssh/known_hosts
120+
121+
# Create SSH config with host aliases
122+
cat > ~/.ssh/config <<'EOF'
123+
Host github.com-http-handler
124+
HostName github.com
125+
User git
126+
IdentityFile ~/.ssh/http_handler_key
127+
IdentitiesOnly yes
128+
129+
Host github.com-http-rewriter
130+
HostName github.com
131+
User git
132+
IdentityFile ~/.ssh/http_rewriter_key
133+
IdentitiesOnly yes
134+
EOF
135+
chmod 600 ~/.ssh/config
136+
137+
# Configure git to rewrite URLs to use the correct host alias
138+
git config --global url."ssh://git@github.com-http-handler/platformatic/http-handler".insteadOf "ssh://git@github.com/platformatic/http-handler"
139+
git config --global url."ssh://git@github.com-http-rewriter/platformatic/http-rewriter".insteadOf "ssh://git@github.com/platformatic/http-rewriter"
140+
141+
# Also handle variations without .git suffix
142+
git config --global url."ssh://git@github.com-http-handler/platformatic/http-handler.git".insteadOf "ssh://git@github.com/platformatic/http-handler.git"
143+
git config --global url."ssh://git@github.com-http-rewriter/platformatic/http-rewriter.git".insteadOf "ssh://git@github.com/platformatic/http-rewriter.git"
107144
108145
${{ matrix.settings.build }}
109146
- name: Build
@@ -138,7 +175,9 @@ jobs:
138175
- uses: actions/checkout@v4
139176
- uses: webfactory/ssh-agent@v0.9.0
140177
with:
141-
ssh-private-key: ${{ secrets.HTTP_HANDLER_ACCESS_TOKEN }}
178+
ssh-private-key: |
179+
${{ secrets.HTTP_HANDLER_ACCESS_TOKEN }}
180+
${{ secrets.HTTP_REWRITER_ACCESS_TOKEN }}
142181
- uses: pnpm/action-setup@v4
143182
with:
144183
version: latest
@@ -155,6 +194,14 @@ jobs:
155194
- name: List packages
156195
run: ls -R .
157196
shell: bash
197+
- name: Check test directory
198+
run: |
199+
echo "Current directory: $(pwd)"
200+
echo "Test directory contents:"
201+
ls -la test/ || echo "test/ directory not found"
202+
echo "Looking for test files:"
203+
find . -name "*.test.mjs" -type f || echo "No test files found"
204+
shell: bash
158205
- run: cargo test
159206
- run: pnpm test
160207

@@ -227,32 +274,137 @@ jobs:
227274
run: |
228275
set -x
229276
277+
export CARGO_NET_GIT_FETCH_WITH_CLI=true
278+
230279
# Install apt dependencies
231280
apt-get update -y
232-
apt-get install -y openssh-client curl
281+
apt-get install -y openssh-client curl git build-essential python3 python3-dev
233282
234283
# Install rust toolchain
235284
curl https://sh.rustup.rs -sSf | bash -s -- -y -t ${{ matrix.settings.target }}
236-
source "$HOME/.cargo/env"
285+
. "$HOME/.cargo/env"
237286
238-
# Set up SSH key (to checkout private repos with cargo)
287+
# Set up SSH keys (to checkout private repos with cargo)
239288
mkdir -p ~/.ssh
240-
chmod -R 400 ~/.ssh
241-
touch ~/.ssh/config ~/.ssh/known_hosts
242-
eval `ssh-agent -s`
243-
echo "${{ secrets.HTTP_HANDLER_ACCESS_TOKEN }}" | tr -d '\r' | ssh-add -
244-
ssh-add -l
245-
ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts
289+
chmod 700 ~/.ssh
290+
291+
# Save SSH keys to files
292+
echo "${{ secrets.HTTP_HANDLER_ACCESS_TOKEN }}" | tr -d '\r' > ~/.ssh/http_handler_key
293+
echo "${{ secrets.HTTP_REWRITER_ACCESS_TOKEN }}" | tr -d '\r' > ~/.ssh/http_rewriter_key
294+
chmod 600 ~/.ssh/http_handler_key
295+
chmod 600 ~/.ssh/http_rewriter_key
296+
297+
# Add GitHub to known hosts (for all aliases)
298+
ssh-keyscan -H github.com >> ~/.ssh/known_hosts
299+
300+
# Create SSH config with host aliases
301+
cat > ~/.ssh/config <<'EOF'
302+
Host github.com-http-handler
303+
HostName github.com
304+
User git
305+
IdentityFile ~/.ssh/http_handler_key
306+
IdentitiesOnly yes
307+
308+
Host github.com-http-rewriter
309+
HostName github.com
310+
User git
311+
IdentityFile ~/.ssh/http_rewriter_key
312+
IdentitiesOnly yes
313+
EOF
314+
chmod 600 ~/.ssh/config
315+
316+
# Configure git to rewrite URLs to use the correct host alias
317+
git config --global url."ssh://git@github.com-http-handler/platformatic/http-handler".insteadOf "ssh://git@github.com/platformatic/http-handler"
318+
git config --global url."ssh://git@github.com-http-rewriter/platformatic/http-rewriter".insteadOf "ssh://git@github.com/platformatic/http-rewriter"
319+
320+
# Also handle variations without .git suffix
321+
git config --global url."ssh://git@github.com-http-handler/platformatic/http-handler.git".insteadOf "ssh://git@github.com/platformatic/http-handler.git"
322+
git config --global url."ssh://git@github.com-http-rewriter/platformatic/http-rewriter.git".insteadOf "ssh://git@github.com/platformatic/http-rewriter.git"
246323
247324
cargo test --target ${{ matrix.settings.target }}
248325
- name: Test bindings
249326
uses: addnab/docker-run-action@v3
250327
with:
251328
image: ${{ steps.docker.outputs.IMAGE }}
252-
options: -v ${{ steps.docker.outputs.PNPM_STORE_PATH }}:${{ steps.docker.outputs.PNPM_STORE_PATH }} -v ${{ github.workspace }}:${{ github.workspace }} -w ${{ github.workspace }} --platform ${{ steps.docker.outputs.PLATFORM }}
329+
options: -v ${{ steps.docker.outputs.PNPM_STORE_PATH }}:${{ steps.docker.outputs.PNPM_STORE_PATH }} -v ${{ github.workspace }}:${{ github.workspace }} -w ${{ github.workspace }} --platform ${{ steps.docker.outputs.PLATFORM }} -e CI=true -e GITHUB_ACTIONS=true
253330
run: |
331+
# Install Python 3.x
332+
apt-get update -y
333+
apt-get install -y python3 python3-dev patchelf
334+
335+
echo "=== Starting test setup ==="
336+
echo "Current directory: $(pwd)"
337+
echo "Python version: $(python3 --version)"
338+
echo "Patchelf version: $(patchelf --version)"
339+
echo "Using combined approach: SONAME patching + programmatic RTLD_GLOBAL"
340+
echo "CI environment: CI=$CI, GITHUB_ACTIONS=$GITHUB_ACTIONS"
341+
342+
# Check what .node files exist
343+
echo "=== Available .node files ==="
344+
ls -la *.node || echo "No .node files found"
345+
346+
# Check what .node files exist and patch Python dependencies
347+
echo "=== Checking .node file Python dependencies ==="
348+
for file in *.node; do
349+
if [ -f "$file" ]; then
350+
case "$file" in
351+
*linux*)
352+
echo "Checking $file..."
353+
echo "Python dependencies before patching:"
354+
ldd "$file" 2>/dev/null | grep python || echo "No Python dependencies found"
355+
356+
# Check if we need to patch SONAME
357+
current_python_lib=$(ldd "$file" 2>/dev/null | grep "libpython" | head -1 | awk '{print $1}')
358+
if [ -n "$current_python_lib" ]; then
359+
echo "Current Python library: $current_python_lib"
360+
361+
# Find the actual Python library on the system
362+
system_python_lib=$(find /usr/lib* -name "libpython3*.so.*" -type f 2>/dev/null | head -1)
363+
if [ -n "$system_python_lib" ]; then
364+
system_python_soname=$(basename "$system_python_lib")
365+
echo "System Python library: $system_python_soname"
366+
367+
# Only patch if they're different
368+
if [ "$current_python_lib" != "$system_python_soname" ]; then
369+
echo "Patching SONAME from $current_python_lib to $system_python_soname"
370+
patchelf --replace-needed "$current_python_lib" "$system_python_soname" "$file"
371+
echo "SONAME patching completed"
372+
else
373+
echo "SONAME already matches system Python"
374+
fi
375+
else
376+
echo "Warning: Could not find system Python library"
377+
fi
378+
else
379+
echo "No Python library dependency found"
380+
fi
381+
382+
echo "Python dependencies after patching:"
383+
ldd "$file" 2>/dev/null | grep python || echo "No Python dependencies found"
384+
echo "---"
385+
;;
386+
*)
387+
echo "Skipping non-Linux file: $file"
388+
;;
389+
esac
390+
fi
391+
done
392+
393+
# Install pnpm and run tests
394+
echo "=== Installing pnpm ==="
254395
corepack disable
255396
npm i -gf pnpm
397+
398+
echo "=== Running pnpm install ==="
399+
# Should be non-interactive in CI environment
400+
pnpm install --prefer-offline
401+
402+
echo "=== Setting up Python library path ==="
403+
export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH
404+
echo "LD_LIBRARY_PATH: $LD_LIBRARY_PATH"
405+
406+
407+
echo "=== Running tests ==="
256408
pnpm test
257409
258410
publish:

.github/workflows/lint.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
name: Lint
22

3+
env:
4+
CARGO_NET_GIT_FETCH_WITH_CLI: 'true'
5+
36
'on':
47
push:
58
branches:
@@ -24,6 +27,11 @@ jobs:
2427
runs-on: ubuntu-latest
2528
steps:
2629
- uses: actions/checkout@v4
30+
- uses: webfactory/ssh-agent@v0.9.0
31+
with:
32+
ssh-private-key: |
33+
${{ secrets.HTTP_HANDLER_ACCESS_TOKEN }}
34+
${{ secrets.HTTP_REWRITER_ACCESS_TOKEN }}
2735
- name: setup pnpm
2836
uses: pnpm/action-setup@v4
2937
with:

0 commit comments

Comments
 (0)