2020-10-30 03:17:28 +00:00
|
|
|
/**
|
|
|
|
* This file describes the code-server plugin API for adding new applications.
|
|
|
|
*/
|
2021-01-20 21:53:11 +00:00
|
|
|
import { field, Level, Logger } from "@coder/logger"
|
2020-10-30 03:17:28 +00:00
|
|
|
import * as express from "express"
|
2021-01-20 20:11:08 +00:00
|
|
|
import * as expressCore from "express-serve-static-core"
|
|
|
|
import ProxyServer from "http-proxy"
|
|
|
|
import * as net from "net"
|
2021-01-28 18:48:47 +00:00
|
|
|
import Websocket from "ws"
|
2020-10-30 03:17:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Overlay
|
|
|
|
*
|
|
|
|
* The homepage of code-server will launch into VS Code. However, there will be an overlay
|
|
|
|
* button that when clicked, will show all available applications with their names,
|
|
|
|
* icons and provider plugins. When one clicks on an app's icon, they will be directed
|
2020-11-03 22:09:28 +00:00
|
|
|
* to <code-server-root>/<plugin-path>/<app-path> to access the application.
|
2020-10-30 03:17:28 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Plugins
|
|
|
|
*
|
2020-11-05 04:10:41 +00:00
|
|
|
* Plugins are just node modules that contain a top level export "plugin" that implements
|
|
|
|
* the Plugin interface.
|
2020-10-30 03:17:28 +00:00
|
|
|
*
|
2020-11-03 21:42:18 +00:00
|
|
|
* 1. code-server uses $CS_PLUGIN to find plugins.
|
|
|
|
*
|
|
|
|
* e.g. CS_PLUGIN=/tmp/will:/tmp/teffen will cause code-server to load
|
|
|
|
* /tmp/will and /tmp/teffen as plugins.
|
|
|
|
*
|
|
|
|
* 2. code-server uses $CS_PLUGIN_PATH to find plugins. Each subdirectory in
|
2020-10-30 03:17:28 +00:00
|
|
|
* $CS_PLUGIN_PATH with a package.json where the engine is code-server is
|
|
|
|
* a valid plugin.
|
|
|
|
*
|
|
|
|
* e.g. CS_PLUGIN_PATH=/tmp/nhooyr:/tmp/ash will cause code-server to search
|
|
|
|
* /tmp/nhooyr and then /tmp/ash for plugins.
|
|
|
|
*
|
|
|
|
* CS_PLUGIN_PATH defaults to
|
|
|
|
* ~/.local/share/code-server/plugins:/usr/share/code-server/plugins
|
|
|
|
* if unset.
|
|
|
|
*
|
|
|
|
*
|
2020-11-03 21:42:18 +00:00
|
|
|
* 3. Built in plugins are loaded from __dirname/../plugins
|
2020-10-30 03:17:28 +00:00
|
|
|
*
|
2020-11-03 21:42:18 +00:00
|
|
|
* Plugins are required as soon as they are found and then initialized.
|
|
|
|
* See the Plugin interface for details.
|
2020-10-30 03:17:28 +00:00
|
|
|
*
|
2020-11-03 21:42:18 +00:00
|
|
|
* If two plugins are found with the exact same name, then code-server will
|
|
|
|
* use the first one and emit a warning.
|
2020-11-04 02:49:10 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-11-05 03:59:43 +00:00
|
|
|
/**
|
|
|
|
* Programmability
|
2020-10-30 03:17:28 +00:00
|
|
|
*
|
|
|
|
* There is also a /api/applications endpoint to allow programmatic access to all
|
|
|
|
* available applications. It could be used to create a custom application dashboard
|
2020-11-04 02:42:21 +00:00
|
|
|
* for example. An important difference with the API is that all application paths
|
|
|
|
* will be absolute (i.e have the plugin path prepended) so that they may be used
|
|
|
|
* directly.
|
2020-11-04 02:49:10 +00:00
|
|
|
*
|
|
|
|
* Example output:
|
|
|
|
*
|
|
|
|
* [
|
|
|
|
* {
|
|
|
|
* "name": "Test App",
|
2022-01-04 17:27:51 +00:00
|
|
|
* "version": "4.0.1",
|
2020-11-04 02:49:10 +00:00
|
|
|
* "iconPath": "/test-plugin/test-app/icon.svg",
|
|
|
|
* "path": "/test-plugin/test-app",
|
|
|
|
* "description": "This app does XYZ.",
|
|
|
|
* "homepageURL": "https://example.com",
|
|
|
|
* "plugin": {
|
|
|
|
* "name": "test-plugin",
|
|
|
|
* "version": "1.0.0",
|
|
|
|
* "modulePath": "/Users/nhooyr/src/cdr/code-server/test/test-plugin",
|
|
|
|
* "displayName": "Test Plugin",
|
|
|
|
* "description": "Plugin used in code-server tests.",
|
|
|
|
* "routerPath": "/test-plugin",
|
|
|
|
* "homepageURL": "https://example.com"
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ]
|
2020-10-30 03:17:28 +00:00
|
|
|
*/
|
|
|
|
|
2021-01-29 23:42:50 +00:00
|
|
|
export enum HttpCode {
|
|
|
|
Ok = 200,
|
|
|
|
Redirect = 302,
|
|
|
|
NotFound = 404,
|
|
|
|
BadRequest = 400,
|
|
|
|
Unauthorized = 401,
|
|
|
|
LargePayload = 413,
|
|
|
|
ServerError = 500,
|
|
|
|
}
|
|
|
|
|
|
|
|
export declare class HttpError extends Error {
|
|
|
|
constructor(message: string, status: HttpCode, details?: object)
|
|
|
|
}
|
|
|
|
|
2021-01-20 20:11:08 +00:00
|
|
|
export interface WebsocketRequest extends express.Request {
|
|
|
|
ws: net.Socket
|
|
|
|
head: Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
export type WebSocketHandler = (
|
|
|
|
req: WebsocketRequest,
|
|
|
|
res: express.Response,
|
|
|
|
next: express.NextFunction,
|
|
|
|
) => void | Promise<void>
|
|
|
|
|
|
|
|
export interface WebsocketRouter {
|
|
|
|
readonly router: express.Router
|
|
|
|
ws(route: expressCore.PathParams, ...handlers: WebSocketHandler[]): void
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a router for websocket routes.
|
|
|
|
*/
|
|
|
|
export function WsRouter(): WebsocketRouter
|
|
|
|
|
2021-01-28 18:48:47 +00:00
|
|
|
/**
|
|
|
|
* The websocket server used by code-server.
|
|
|
|
*/
|
|
|
|
export const wss: Websocket.Server
|
|
|
|
|
2021-01-13 22:26:11 +00:00
|
|
|
/**
|
|
|
|
* The Express import used by code-server.
|
|
|
|
*
|
|
|
|
* Re-exported so plugins don't have to import duplicate copies of Express and
|
|
|
|
* to avoid potential version differences or issues caused by running separate
|
|
|
|
* instances.
|
|
|
|
*/
|
|
|
|
export { express }
|
2021-01-13 22:25:39 +00:00
|
|
|
/**
|
|
|
|
* Use to add a field to a log.
|
|
|
|
*
|
|
|
|
* Re-exported so plugins don't have to import duplicate copies of the logger.
|
|
|
|
*/
|
2021-01-28 20:11:53 +00:00
|
|
|
export { field, Level, Logger }
|
2021-01-13 22:25:39 +00:00
|
|
|
|
2021-01-28 20:24:07 +00:00
|
|
|
/**
|
|
|
|
* code-server's proxy server.
|
|
|
|
*/
|
2021-01-19 22:43:36 +00:00
|
|
|
export const proxy: ProxyServer
|
|
|
|
|
2021-02-12 22:49:47 +00:00
|
|
|
/**
|
|
|
|
* Middleware to ensure the user is authenticated. Throws if they are not.
|
|
|
|
*/
|
2021-06-03 23:37:46 +00:00
|
|
|
export function ensureAuthenticated(
|
|
|
|
req: express.Request,
|
|
|
|
res?: express.Response,
|
|
|
|
next?: express.NextFunction,
|
|
|
|
): Promise<void>
|
2021-02-12 22:49:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the user is authenticated.
|
|
|
|
*/
|
2021-06-02 20:27:55 +00:00
|
|
|
export function authenticated(req: express.Request): Promise<void>
|
2021-02-12 22:49:47 +00:00
|
|
|
|
2021-01-28 20:24:07 +00:00
|
|
|
/**
|
|
|
|
* Replace variables in HTML: TO, BASE, CS_STATIC_BASE, and OPTIONS.
|
|
|
|
*/
|
|
|
|
export function replaceTemplates<T extends object>(
|
|
|
|
req: express.Request,
|
|
|
|
content: string,
|
|
|
|
extraOpts?: Omit<T, "base" | "csStaticBase" | "logLevel">,
|
|
|
|
): string
|
|
|
|
|
2020-10-30 03:17:28 +00:00
|
|
|
/**
|
2020-11-05 04:10:41 +00:00
|
|
|
* Your plugin module must have a top level export "plugin" that implements this interface.
|
2020-10-30 03:17:28 +00:00
|
|
|
*
|
2020-11-03 22:09:28 +00:00
|
|
|
* The plugin's router will be mounted at <code-sever-root>/<plugin-path>
|
2020-10-30 03:17:28 +00:00
|
|
|
*/
|
|
|
|
export interface Plugin {
|
2020-11-03 22:09:28 +00:00
|
|
|
/**
|
|
|
|
* name is used as the plugin's unique identifier.
|
|
|
|
* No two plugins may share the same name.
|
|
|
|
*
|
|
|
|
* Fetched from package.json.
|
|
|
|
*/
|
2020-11-04 02:42:21 +00:00
|
|
|
readonly name?: string
|
2020-11-03 22:09:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The version for the plugin in the overlay.
|
|
|
|
*
|
|
|
|
* Fetched from package.json.
|
|
|
|
*/
|
2020-11-04 02:42:21 +00:00
|
|
|
readonly version?: string
|
2020-11-03 22:09:28 +00:00
|
|
|
|
|
|
|
/**
|
2020-11-04 02:11:14 +00:00
|
|
|
* Name used in the overlay.
|
2020-11-03 22:09:28 +00:00
|
|
|
*/
|
2020-11-04 02:42:21 +00:00
|
|
|
readonly displayName: string
|
2020-11-04 02:11:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Used in overlay.
|
|
|
|
* Should be a full sentence describing the plugin.
|
|
|
|
*/
|
2020-11-04 02:42:21 +00:00
|
|
|
readonly description: string
|
2020-11-03 22:09:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The path at which the plugin router is to be registered.
|
|
|
|
*/
|
2020-11-04 02:42:21 +00:00
|
|
|
readonly routerPath: string
|
2020-11-03 22:09:28 +00:00
|
|
|
|
2020-11-04 02:45:25 +00:00
|
|
|
/**
|
|
|
|
* Link to plugin homepage.
|
|
|
|
*/
|
|
|
|
readonly homepageURL: string
|
|
|
|
|
2020-10-30 03:17:28 +00:00
|
|
|
/**
|
|
|
|
* init is called so that the plugin may initialize itself with the config.
|
|
|
|
*/
|
|
|
|
init(config: PluginConfig): void
|
|
|
|
|
2021-01-20 21:48:35 +00:00
|
|
|
/**
|
|
|
|
* Called when the plugin should dispose/shutdown everything.
|
|
|
|
*/
|
|
|
|
deinit?(): Promise<void>
|
|
|
|
|
2020-10-30 03:17:28 +00:00
|
|
|
/**
|
|
|
|
* Returns the plugin's router.
|
2020-11-03 22:09:28 +00:00
|
|
|
*
|
|
|
|
* Mounted at <code-sever-root>/<plugin-path>
|
2020-11-06 19:44:19 +00:00
|
|
|
*
|
|
|
|
* If not present, the plugin provides no routes.
|
2020-10-30 03:17:28 +00:00
|
|
|
*/
|
2020-11-06 19:44:19 +00:00
|
|
|
router?(): express.Router
|
2020-10-30 03:17:28 +00:00
|
|
|
|
2021-01-20 20:11:08 +00:00
|
|
|
/**
|
|
|
|
* Returns the plugin's websocket router.
|
|
|
|
*
|
|
|
|
* Mounted at <code-sever-root>/<plugin-path>
|
|
|
|
*
|
|
|
|
* If not present, the plugin provides no websockets.
|
|
|
|
*/
|
|
|
|
wsRouter?(): WebsocketRouter
|
|
|
|
|
2020-10-30 03:17:28 +00:00
|
|
|
/**
|
|
|
|
* code-server uses this to collect the list of applications that
|
|
|
|
* the plugin can currently provide.
|
|
|
|
* It is called when /api/applications is hit or the overlay needs to
|
|
|
|
* refresh the list of applications
|
|
|
|
*
|
|
|
|
* Ensure this is as fast as possible.
|
2020-11-06 19:44:19 +00:00
|
|
|
*
|
|
|
|
* If not present, the plugin provides no applications.
|
2020-10-30 03:17:28 +00:00
|
|
|
*/
|
2020-11-06 19:44:19 +00:00
|
|
|
applications?(): Application[] | Promise<Application[]>
|
2020-10-30 03:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PluginConfig contains the configuration required for initializing
|
|
|
|
* a plugin.
|
|
|
|
*/
|
|
|
|
export interface PluginConfig {
|
|
|
|
/**
|
|
|
|
* All plugin logs should be logged via this logger.
|
|
|
|
*/
|
|
|
|
readonly logger: Logger
|
2021-01-21 19:49:45 +00:00
|
|
|
|
|
|
|
/**
|
2021-02-09 19:35:06 +00:00
|
|
|
* This can be specified by the user on the command line. Plugins should
|
|
|
|
* default to this directory when applicable. For example, the Jupyter plugin
|
|
|
|
* uses this to launch in this directory.
|
2021-01-21 19:49:45 +00:00
|
|
|
*/
|
|
|
|
readonly workingDirectory?: string
|
2020-10-30 03:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Application represents a user accessible application.
|
|
|
|
*/
|
|
|
|
export interface Application {
|
|
|
|
readonly name: string
|
|
|
|
readonly version: string
|
|
|
|
|
2020-11-03 22:09:28 +00:00
|
|
|
/**
|
|
|
|
* When the user clicks on the icon in the overlay, they will be
|
|
|
|
* redirected to <code-server-root>/<plugin-path>/<app-path>
|
|
|
|
* where the application should be accessible.
|
|
|
|
*
|
|
|
|
* If undefined, then <code-server-root>/<plugin-path> is used.
|
|
|
|
*/
|
|
|
|
readonly path?: string
|
|
|
|
|
|
|
|
readonly description?: string
|
|
|
|
|
2020-10-30 03:17:28 +00:00
|
|
|
/**
|
|
|
|
* The path at which the icon for this application can be accessed.
|
2020-11-03 22:09:28 +00:00
|
|
|
* <code-server-root>/<plugin-path>/<app-path>/<icon-path>
|
2020-10-30 03:17:28 +00:00
|
|
|
*/
|
|
|
|
readonly iconPath: string
|
2020-11-04 02:45:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Link to application homepage.
|
|
|
|
*/
|
|
|
|
readonly homepageURL: string
|
2020-10-30 03:17:28 +00:00
|
|
|
}
|