How kong/kong-js-pdk works
Project type and purpose
• This is an open-source tool (kong-js-pdk) that provides a Plugin Development Kit (PDK) and a standalone plugin server to enable writing Kong Gateway plugins in JavaScript.
• Its main features are:
• A CLI/binary (bin/kong-js-pluginserver / cli.js) to launch the server.
• A runtime (server.js, listener.js) that speaks Kong’s plugin-server protocol.
• A JavaScript PDK (lib/*.js + kong/*.d.ts) exposing Kong contexts and actions.
• Example plugins and tests demonstrating how to integrate.
File structure
• Top-level
• Configuration & metadata: .babelrc, .eslintrc.js, package.json, GitHub workflows, changelog, etc.
• Entry points: cli.js (CLI wrapper) and bin/kong-js-pluginserver (shell shim).
• Runtime code: server.js (HTTP/TCP listener), listener.js (handles incoming RPC).
• PDK implementation: lib/mod.js, lib/pipe.js, plus pdk.js (exports PDK API).
• Type definitions: kong/*.d.ts for every Kong context (request, response, router, etc.).
• Examples and tests under examples/ and __tests__/.
README for additional insights
• Confirms it’s a plugin server + PDK for JavaScript support in Kong (>= 2.3.0).
• Points to Kong Docs for usage and plugin lifecycle.
• Mentions future work (TypeScript rewrite, per-plugin server).
Main components
a. Kong Gateway (external service)
b. CLI / Plugin Server launcher
c. Plugin Server process
• Listener (TCP/HTTP)
• RPC handler
• PDK core modules
• User plugin code
d. User plugins (examples/js-*)
e. (Optional) Test harness & examples
Relationships and interactions
• The operator runs kong-js-pluginserver (CLI) to start the Plugin Server.
• Kong Gateway (configured with config.lua pointing at the server’s address) makes RPC calls to the server for each plugin hook (e.g., access, header_filter).
• Listener accepts each RPC request, invokes the PDK layer (lib/mod.js + type definitions) to expose Kong contexts.
• PDK calls are routed into the user’s plugin code.
• Plugin code uses PDK APIs to read request/response, call upstream services, mutate headers, etc.
• The PDK serializes the result, sends a response back over the same RPC channel to Kong.
Architectural patterns and principles
• Plugin Architecture: decouples Kong core from custom logic via an RPC boundary.
• Adapter/Facade: PDK modules expose a friendly JS API over Kong’s low-level RPC protocol.
• Separation of Concerns: Listener → PDK core → User plugin code.
• Extensibility: New hooks or contexts can be added by extending kong/*.d.ts and lib/ modules.
Detail level
• every major module: server.js, listener.js, lib/mod.js, type definitions in kong/, and how they chain.
• How cli.js/bin bootstraps server.js.