https://github.com/apache/apisix/issues/5106

Abstract

Apache APISIX has been supporting writing customized plugins using Lua, Java, Go and Python.
I propose to add JavaScript Plugin Runner for Apache APISIX.

Background

JavaScript is widely used

JavaScript is a widely used dynamic language. According to SlashData, JavaScript, which includes CoffeeScript and TypeScript in the survey, is by far the most popular language, with 12.4 million developers using it worldwide.

Apache APISIX has been supporting writing customized plugins using Lua, Java, Go and Python.
LuaJIT is really fast, and Lua is very easy to learn. But out-of-the-box JavaScript plugin runner will unlock more developers with JavaScript background for Apache APISIX.

Node.js and Deno

Node.js is the most popular JavaScript runtime built on Chrome’s V8 JavaScript engine.
Deno, yet another JavaScript runtime built on V8, with 77.9K stars on GitHub, was created by ry (creator of Node.js) to address his 10 Things I Regret About Node.js.
It would be nice if APISIX provides first class support for both Node.js and Deno.

WebAssembly support

Supporting WebAssembly in Apache APISIX has been discussed at https://github.com/apache/apisix/issues/157.
Both Node.js and Deno can execute WebAssembly modules.

  1. https://nodejs.org/api/wasi.html
  2. https://deno.land/manual@v1.13.2/getting_started/webassembly

There are many WebAssembly runtime, including but not limited to

  1. Wasmtime, a Bytecode Alliance project (the Wasmtime team is moving to Fastly)
  2. WasmEdge (formerly SSVM), open sourced by Second State, a CNCF project
  3. Wasmer, used by Dart
  4. Lucet, open sourced by Fastly, a Bytecode Alliance project (in the medium-to-long term, the plan is to merge Lucet and Wasmtime)

It’s possible to support WebAssembly in JavaScript Plugin Runner, but maybe a separated Apache APISIX WebAssembly Runner is better?

Proposal

To address the problems described above, I propose to add JavaScript Plugin Runner for Apache APISIX. It will provide first class support for both Node.js and Deno.

Demo Implementation

I have created a demo implementation: https://github.com/zenozeng/apisix-javascript-plugin-runner.

Plugin Example

  1. # examples/config.yaml
  2. ext-plugin:
  3. cmd:
  4. - "/usr/local/apisix/javascript-plugin-runner/bin/runner"
  5. - "/usr/local/apisix/javascript-plugin-runner/examples/say.js"
  6. apisix:
  7. port_admin: 9180
  8. admin_key:
  9. -
  10. name: "admin"
  11. key: "YOUR_ADMIN_KEY"
  12. role: admin
  13. etcd:
  14. host:
  15. - "http://etcd:2379"
  1. class SayPlugin {
  2. getName() {
  3. return "say"
  4. }
  5. parseConf(conf) {
  6. return JSON.parse(conf)
  7. }
  8. // Filter will be executed on every request with the say plugin configured.
  9. async filter(conf, request, response) {
  10. const headers = new Map()
  11. headers.set('X-Resp-A6-JavaScript-Plugin', 'Say')
  12. response.body = conf.body
  13. response.headers = headers
  14. }
  15. }
  16. module.exports = SayPlugin
  1. # default config.apisix.allow_admin is 127.0.0.0/24, using docker exec
  2. docker exec -it apisix curl -H "Content-Type: application/json" \
  3. -H 'X-API-KEY: YOUR_ADMIN_KEY' \
  4. -X PUT \
  5. --data '{"uri":"/say","methods":["PUT","GET"],"plugins":{"ext-plugin-pre-req":{"conf":[{"name":"say","value":"{\"body\":\"123\"}"}]}}}' \
  6. http://127.0.0.1:9180/apisix/admin/routes/1
  1. curl -v http://127.0.0.1:9080/say
  2. < HTTP/1.1 200 OK
  3. < Date: Fri, 09 Jul 2021 18:20:24 GMT
  4. < Content-Type: text/plain; charset=utf-8
  5. < Transfer-Encoding: chunked
  6. < Connection: keep-alive
  7. < X-Resp-A6-JavaScript-Plugin: Say
  8. < Server: APISIX/2.7
  9. <
  10. * Connection #0 to host 127.0.0.1 left intact
  11. 123

Interface

  1. interface Plugin {
  2. getName(): string
  3. parseConf(conf: string): string
  4. filter(conf: Object, request: Request, response: Response): Promise<void>
  5. }
  6. interface Request {
  7. // The id is for debug. It will be recycled when the number is exhausted
  8. id: number;
  9. // the path of the request.
  10. path: string;
  11. // The associated Headers object of the request.
  12. // See·https://developer.mozilla.org/en-US/docs/Web/API/Headers
  13. headers: Headers;
  14. srcIp: number[];
  15. // Request's method (GET, POST, etc.)
  16. method: string;
  17. // The associated Args object of the request.
  18. args: Args;
  19. }
  20. interface Args {
  21. keys(): Iterable<string>
  22. get(k: string): string
  23. set(k: string, v: string): Args
  24. }
  25. interface Headers {
  26. keys(): Iterable<string>
  27. get(k: string): string
  28. set(k: string, v: string): Headers
  29. }
  30. interface Response {
  31. // The status code of the response (200, 404, etc.)
  32. status?: number;
  33. // The associated Headers object of the response.
  34. // See https://developer.mozilla.org/en-US/docs/Web/API/Headers
  35. headers?: Headers;
  36. // The body of the response
  37. body?: Uint8Array | string;
  38. }

References

  1. https://www.zdnet.com/article/programming-language-popularity-javascript-leads-5-million-new-developers-since-2017/
  2. https://medium.com/@ApacheAPISIX/why-apache-apisix-chose-nginx-and-lua-to-build-api-gateway-3d4f81e3e1f1
  3. https://medium.com/@ApacheAPISIX/how-to-write-an-apache-apisix-plugin-in-java-3fb7b75444fa
  4. https://deno.com/blog/v1
  5. https://www.youtube.com/watch?v=M3BM9TB-8yA
  6. https://github.com/second-state/wasm32-wasi-benchmark
  7. https://www.fastly.com/blog/how-lucet-wasmtime-make-stronger-compiler-together