Skip to content

feat(http): add QuickJS script handler - #871

Open
ithewei wants to merge 13 commits into
masterfrom
js-http-script-handler
Open

feat(http): add QuickJS script handler#871
ithewei wants to merge 13 commits into
masterfrom
js-http-script-handler

Conversation

@ithewei

@ithewei ithewei commented Aug 19, 2026

Copy link
Copy Markdown
Owner

Summary

  • Add optional WITH_JS / QuickJS support and install HttpJsHandler.h plus public hv/hvjs.h when enabled.
  • Add HttpJsHandler plus HttpScriptHandler dispatch for .js scripts.
  • Split JS bindings into js/ with controlled modules: hv, hv/http, hv/ws, hv/redis, and hv/mqtt; no Node.js/npm module loading.
  • Use one QuickJS runtime per hloop_t via hloop_set_js_runtime / hloop_js_runtime, with one JSContext per HTTP request.
  • Support Promise-based APIs for async / await, including request-level timeout, built-in QuickJS memory/stack limits, and interrupt handling for CPU-bound scripts.
  • Harden request cleanup by tracking/cancelling outstanding Promise ops and deferred deletes; ws/mqtt recv() remain message-driven and rely on request timeout_ms as the HTTP lifecycle safety budget.
  • Document JS handler usage, module scope, timeout semantics, Redis connection cost, MQTT reconnect semantics, and the current string-only binary limitation.
  • Add JS integration tests covering HTTP async concurrency, pending Promise timeout, CPU interrupt, Redis, WebSocket, MQTT failure handling, and generic 500 responses.
  • Fix AsyncRedisClient pending request callback reentrancy by popping completed/failed requests before invoking callbacks.
  • Update Linux CI to build pinned Bellard QuickJS from source with PIC, then run one combined --with-js shared/static libhv build instead of the previous static-only JS pass plus second libhv rebuild.

Testing

  • git diff --check
  • QuickJS source PIC check: clone bellard/quickjs at 04be246001599f5995fa2f2d8c91a0f198d3f34c, append CFLAGS_OPT+=-fPIC, build libquickjs.a; verified compile commands keep CONFIG_VERSION and include -fPIC.
  • .github/workflows/CI.yml shell block syntax: extracted each run: | block and ran bash -n.
  • .github/workflows/CI.yml YAML parse: ruby -e 'require "yaml"; YAML.load_file(".github/workflows/CI.yml"); puts "yaml ok"'.
  • cmake -S . -B /tmp/libhv-js-final-check -DWITH_JS=ON -DWITH_HTTP=ON -DWITH_HTTP_SERVER=ON -DWITH_HTTP_CLIENT=ON -DWITH_REDIS=ON -DWITH_MQTT=ON -DBUILD_SHARED=OFF -DBUILD_STATIC=ON -DBUILD_EXAMPLES=ON -DBUILD_UNITTEST=ON -DQUICKJS_ROOT=/opt/homebrew/opt/quickjs
  • cmake --build /tmp/libhv-js-final-check --target hvjs http_js_handler_test http_js_redis_test http_js_ws_test http_js_mqtt_test -j2
  • /tmp/libhv-js-final-check/bin/http_js_handler_test && /tmp/libhv-js-final-check/bin/http_js_redis_test && /tmp/libhv-js-final-check/bin/http_js_ws_test && /tmp/libhv-js-final-check/bin/http_js_mqtt_test && /tmp/libhv-js-final-check/bin/hvjs examples/js/sleep.js
  • cmake --install /tmp/libhv-js-final-check --prefix /tmp/libhv-install-js-final
  • External consumer: find_package(libhv REQUIRED CONFIG), #include <hv/hvjs.h>, link libhv::hv_static, build and run with -DCMAKE_PREFIX_PATH=/tmp/libhv-install-js-final -DQUICKJS_ROOT=/opt/homebrew/opt/quickjs
  • make libhv hvjs unittest WITH_JS=yes WITH_HTTP=yes WITH_MQTT=yes WITH_REDIS=yes -j2
  • DYLD_LIBRARY_PATH=$(pwd)/lib:$DYLD_LIBRARY_PATH bash scripts/unittest.sh
  • make http_server_test WITH_JS=yes WITH_HTTP=yes -j2

Copilot AI lite review requested due to automatic review settings August 19, 2026 22:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings August 20, 2026 04:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings August 20, 2026 05:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings August 20, 2026 05:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings August 20, 2026 05:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings August 20, 2026 06:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@ithewei
ithewei requested a lite review from Copilot August 20, 2026 08:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings August 20, 2026 20:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings August 21, 2026 04:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings August 21, 2026 04:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings August 21, 2026 04:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings August 21, 2026 06:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings August 21, 2026 06:42
@ithewei
ithewei force-pushed the js-http-script-handler branch from a767821 to 65bb368 Compare August 21, 2026 06:42

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings August 21, 2026 07:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Expose stable JS-layer run helpers mirroring hvlua_dofile/hvlua_dostring
so callers can run a script on a loop's per-loop QuickJS runtime without
touching the task/runtime plumbing. The script body is wrapped in an
async function, so top-level await works; global require/print/arg are
installed. Returns 1 when finished synchronously, 0 when pending on async
work (caller runs the loop), <0 on setup/load/runtime error, with an
optional exit_code set on reject/timeout.

Thin examples/hvjs.cpp down to a hvlua.cpp-style runner that just creates
the loop, publishes TLS, and calls hvjs_dofile.
Copilot AI review requested due to automatic review settings August 21, 2026 09:11

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants