pockethost 1.0.0-rc.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/common.ts +1 -0
  2. package/core.ts +2 -0
  3. package/package.json +24 -112
  4. package/readme.md +36 -146
  5. package/src/cli/PluginCommand/index.ts +104 -0
  6. package/src/cli/PluginCommand/util.ts +138 -0
  7. package/src/cli/commands/ConfigCommand/index.ts +99 -0
  8. package/src/cli/commands/ServeCommand/index.ts +20 -0
  9. package/src/cli/index.ts +38 -0
  10. package/src/common/CleanupManager.ts +65 -0
  11. package/src/common/Logger.ts +168 -0
  12. package/src/common/ResourceAllocator.ts +49 -0
  13. package/src/common/TimerManager.ts +64 -0
  14. package/src/common/assert.ts +26 -0
  15. package/src/common/debug.ts +8 -0
  16. package/src/common/events.ts +53 -0
  17. package/src/common/index.ts +12 -0
  18. package/src/common/mergeConfig.ts +27 -0
  19. package/src/common/mkSingleton.ts +28 -0
  20. package/src/common/plugin/action.ts +60 -0
  21. package/src/common/plugin/filter.ts +65 -0
  22. package/src/common/plugin/helpers.ts +17 -0
  23. package/src/common/plugin/index.ts +27 -0
  24. package/src/common/pocketbase-client-helpers/RestHelper.ts +69 -0
  25. package/src/common/pocketbase-client-helpers/index.ts +1 -0
  26. package/src/common/promiseSerializer.ts +22 -0
  27. package/src/common/schema/Instance.ts +45 -0
  28. package/src/common/schema/InstanceLog.ts +12 -0
  29. package/src/common/schema/Rest/CreateInstance.ts +20 -0
  30. package/src/common/schema/Rest/DeleteInstance.ts +21 -0
  31. package/src/common/schema/Rest/GetUserTokenInfo.ts +22 -0
  32. package/src/common/schema/Rest/UpdateInstance.ts +61 -0
  33. package/src/common/schema/Rest/index.ts +23 -0
  34. package/src/common/schema/User.ts +24 -0
  35. package/src/common/schema/index.ts +6 -0
  36. package/src/common/schema/types.ts +18 -0
  37. package/src/common/schema/util.ts +1 -0
  38. package/src/constants.ts +74 -0
  39. package/src/core/Settings.ts +119 -0
  40. package/src/core/asyncExecutionGuard.ts +49 -0
  41. package/src/core/exit.ts +11 -0
  42. package/src/core/index.ts +7 -0
  43. package/src/core/internal.ts +3 -0
  44. package/src/core/smartFetch.ts +32 -0
  45. package/src/core/tryFetch.ts +66 -0
  46. package/src/server.ts +119 -0
  47. package/dist/index.mjs +0 -3156
  48. package/dist/instance-app/migrations/1699198734_pockethost_defaults.js +0 -39
  49. package/dist/instance-app/pb_hooks/_ph_admin_sync.pb.js +0 -71
  50. package/dist/instance-app/pb_hooks/_ph_lib.js +0 -19
  51. package/dist/instance-app/tsconfig.json +0 -8
  52. package/dist/instance-app/types/goja.d.ts +0 -58
  53. package/dist/instance-app/types/lib.d.ts +0 -5
  54. package/dist/instance-app/types/types.d.ts +0 -19573
  55. package/dist/mothership-app/migrations/1705062576_collections_snapshot.js +0 -1863
  56. package/dist/mothership-app/migrations/1705083826_updated_stats.js +0 -721
  57. package/dist/mothership-app/migrations/1706060446_updated_instances.js +0 -44
  58. package/dist/mothership-app/pb_hooks/src/instances-create.pb.js +0 -65
  59. package/dist/mothership-app/pb_hooks/src/instances-delete.pb.js +0 -69
  60. package/dist/mothership-app/pb_hooks/src/instances-migrate-versions.pb.js +0 -33
  61. package/dist/mothership-app/pb_hooks/src/instances-notify-discord-after-create.pb.js +0 -31
  62. package/dist/mothership-app/pb_hooks/src/instances-notify-on-maintenance-mode.pb.js +0 -52
  63. package/dist/mothership-app/pb_hooks/src/instances-reset-idle.pb.js +0 -7
  64. package/dist/mothership-app/pb_hooks/src/instances-update.pb.js +0 -121
  65. package/dist/mothership-app/pb_hooks/src/instances-validate-before-create.pb.js +0 -16
  66. package/dist/mothership-app/pb_hooks/src/instances-validate-before-update.pb.js +0 -43
  67. package/dist/mothership-app/pb_hooks/src/lib.js +0 -171
  68. package/dist/mothership-app/pb_hooks/src/ls.pb.js +0 -159
  69. package/dist/mothership-app/pb_hooks/src/mail.pb.js +0 -49
  70. package/dist/mothership-app/pb_hooks/src/notifications.pb.js +0 -62
  71. package/dist/mothership-app/pb_hooks/src/random-words.js +0 -2085
  72. package/dist/mothership-app/pb_hooks/src/signup.pb.js +0 -214
  73. package/dist/mothership-app/pb_hooks/src/sns.pb.js +0 -133
  74. package/dist/mothership-app/pb_hooks/src/unsubscribe.pb.js +0 -33
  75. package/dist/mothership-app/pb_hooks/src/user-token.pb.js +0 -33
  76. package/dist/mothership-app/pb_hooks/src/user-welcome-msg.pb.js +0 -32
  77. package/dist/mothership-app/pb_hooks/src/versions-route.pb.js +0 -12
  78. package/dist/mothership-app/pb_hooks/src/versions.js +0 -0
  79. package/dist/mothership-app/pb_hooks/tsconfig.json +0 -8
  80. package/dist/mothership-app/pb_hooks/types/global.d.ts +0 -1
  81. package/dist/mothership-app/pb_hooks/types/goja.d.ts +0 -58
  82. package/dist/mothership-app/pb_hooks/types/lemon.d.ts +0 -12
  83. package/dist/mothership-app/pb_hooks/types/lib.d.ts +0 -55
  84. package/dist/mothership-app/pb_hooks/types/types.d.ts +0 -19573
  85. package/dist/mothership-app/ph_app/index.js +0 -11
  86. package/dist/mothership-app/ph_app/package-lock.json +0 -657
  87. package/dist/mothership-app/ph_app/package.json +0 -14
package/common.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './src/common'
package/core.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './src/common'
2
+ export * from './src/core'
package/package.json CHANGED
@@ -1,144 +1,56 @@
1
1
  {
2
2
  "name": "pockethost",
3
- "version": "1.0.0-rc.1",
3
+ "version": "1.0.0",
4
4
  "author": {
5
5
  "name": "Ben Allfree",
6
6
  "url": "https://github.com/benallfree"
7
7
  },
8
8
  "license": "MIT",
9
- "main": "dist/index.mjs",
10
- "scripts": {
11
- "check:types": "concurrently 'pnpm:check:types:*'",
12
- "check:types:dashboard": "cd frontends/dashboard && pnpm check:types",
13
- "check:types:pockethost": "tsc --noEmit --skipLibCheck",
14
- "lint": "prettier -c \"./**/*.{ts,js,cjs,svelte,json}\"",
15
- "lint:fix": "prettier -w \"./**/*.{ts,js,cjs,svelte,json}\"",
16
- "download-versions": "tsx ./src/cli/download.ts",
17
- "build": "concurrently 'pnpm:build:*'",
18
- "build-cli": "tsx build.ts",
19
- "build-frontends": "concurrently 'pnpm:build:frontend:*'",
20
- "build:frontend:dashboard": "cd frontends/dashboard && pnpm build",
21
- "build:docker": "cd src/services/PocketBaseService && docker build . -t pockethost-instance",
22
- "build:frontend:lander": "cd frontends/lander && pnpm build",
23
- "build:frontend:superadmin": "cd frontends/superadmin && pnpm build",
24
- "dev": "NODE_ENV=development concurrently 'pnpm:dev:*'",
25
- "dev-cli": "nodemon --signal SIGTERM dist/index.mjs",
26
- "dev:lander": "cd frontends/lander && pnpm start",
27
- "dev:dashboard": "cd frontends/dashboard && pnpm dev",
28
- "dev:superadmin": "cd frontends/superadmin && pnpm dev",
29
- "prod:proxy": "dotenv tsx ./src/cli/proxy/server.ts",
30
- "prod:edge:daemon": "tsx src/cli/edge-daemon.ts",
31
- "prod:edge:ftp": "tsx src/cli/edge-ftp.ts",
32
- "prod:edge:health": "tsx src/cli/edge-health.ts",
33
- "prod:edge:syslog": "tsx src/cli/edge-syslogd.ts",
34
- "prod:mothership": "tsx src/cli/mothership.ts",
35
- "plop": "plop",
36
- "nofile": "cat /proc/sys/fs/file-nr",
37
- "mail": "tsx ./src/cli/sendmail.ts",
38
- "watch": "node build.cjs"
39
- },
40
- "prettier": {
41
- "semi": false,
42
- "useTabs": false,
43
- "singleQuote": true,
44
- "trailingComma": "all",
45
- "plugins": [
46
- "./node_modules/prettier-plugin-organize-imports/index.js",
47
- "./node_modules/prettier-plugin-svelte/plugin.js",
48
- "./node_modules/prettier-plugin-jsdoc/dist/index.js"
49
- ]
50
- },
9
+ "main": "./common.ts",
10
+ "module": "./common.ts",
51
11
  "type": "module",
12
+ "bin": {
13
+ "pockethost": "src/cli/index.ts"
14
+ },
52
15
  "dependencies": {
53
16
  "@s-libs/micro-dash": "^16.1.0",
54
- "@types/node-os-utils": "^1.3.4",
55
- "@types/winston-syslog": "^2.4.3",
56
17
  "ajv": "^8.12.0",
57
18
  "boolean": "^3.2.0",
58
19
  "bottleneck": "^2.19.5",
20
+ "chalk": "^5.3.0",
59
21
  "commander": "^11.1.0",
60
- "cors": "^2.8.5",
61
- "decompress": "^4.2.1",
62
- "decompress-unzip": "https://github.com/pockethost/decompress-unzip.git#6ef397b9a2df11d39c7b26ce779e123833844751",
63
- "dockerode": "^3.3.5",
64
22
  "dotenv": "^16.3.1",
65
- "event-source-polyfill": "^1.0.31",
66
- "eventsource": "^2.0.2",
23
+ "env-paths": "^3.0.0",
67
24
  "exit-hook": "^4.0.0",
68
25
  "express": "^4.18.2",
69
26
  "express-async-errors": "^3.1.1",
70
- "find-up": "^6.3.0",
71
- "ftp-srv": "github:pockethost/ftp-srv#0fc708bae0d5d7a55ce948767f082d6fcfb2af59",
72
27
  "get-port": "^6.1.2",
73
- "glob": "^10.3.10",
74
28
  "http-proxy": "^1.18.1",
75
- "http-proxy-middleware": "^2.0.6",
76
- "ip-cidr": "^3.1.0",
77
29
  "json-stringify-safe": "^5.0.1",
78
- "knex": "^2.5.1",
79
- "memorystream": "^0.3.1",
30
+ "listr2": "^8.2.3",
80
31
  "nanoid": "^5.0.2",
81
32
  "node-fetch": "^3.3.2",
82
- "node-os-utils": "^1.3.7",
83
- "pocketbase": "^0.20.1",
84
- "semver": "^7.5.4",
85
- "sqlite3": "^5.1.6",
86
- "syslog-parse": "^2.0.0",
87
- "tail": "^2.2.6",
33
+ "pocketbase": "^0.21.3",
34
+ "rimraf": "^5.0.5",
35
+ "semver": "^7.6.2",
88
36
  "tmp": "^0.2.1",
89
- "url-pattern": "^1.0.3",
90
- "vhost": "^3.0.2",
91
- "winston": "^3.11.0",
92
- "winston-syslog": "^2.7.0"
37
+ "tsx": "^4.15.7",
38
+ "type-fest": "^4.6.0",
39
+ "typescript": "^5.4.5",
40
+ "url-pattern": "^1.0.3"
93
41
  },
94
42
  "devDependencies": {
95
- "@swc/cli": "^0.1.62",
96
- "@swc/core": "^1.3.95",
97
- "@types/bootstrap": "^5.2.8",
98
- "@types/copyfiles": "^2.4.4",
99
- "@types/cors": "^2.8.17",
100
- "@types/d3-scale": "^4.0.6",
101
- "@types/d3-scale-chromatic": "^3.0.1",
102
- "@types/decompress": "^4.2.6",
103
- "@types/dockerode": "^3.3.21",
104
- "@types/eventsource": "^1.1.14",
105
43
  "@types/express": "^4.17.21",
106
44
  "@types/http-proxy": "^1.17.13",
107
- "@types/js-cookie": "^3.0.5",
108
45
  "@types/json-stringify-safe": "^5.0.2",
109
- "@types/marked": "^4.3.2",
110
- "@types/memorystream": "^0.3.3",
111
- "@types/ncp": "^2.0.8",
112
46
  "@types/node": "^20.8.10",
113
47
  "@types/semver": "^7.5.4",
114
- "@types/tail": "^2.2.2",
115
- "@types/tmp": "^0.2.5",
116
- "@types/unzipper": "^0.10.8",
117
- "@types/vhost": "^3.0.9",
118
- "chalk": "^5.3.0",
119
- "chokidar-cli": "^3.0.0",
120
- "concurrently": "^8.2.2",
121
- "copyfiles": "^2.4.1",
122
- "date-fns": "^2.30.0",
123
- "dotenv-cli": "^7.3.0",
124
- "esbuild": "^0.20.0",
125
- "esbuild-node-externals": "^1.13.0",
126
- "js-yaml": "^4.1.0",
127
- "ncp": "^2.0.0",
128
- "nodemon": "^3.0.3",
129
- "ora": "^7.0.1",
130
- "plop": "^4.0.0",
131
- "postinstall-postinstall": "^2.1.0",
132
- "prettier": "^3.0.3",
133
- "prettier-plugin-jsdoc": "^1.3.0",
134
- "prettier-plugin-organize-imports": "^3.2.3",
135
- "prettier-plugin-svelte": "^3.0.3",
136
- "rizzdown": "^0.0.6",
137
- "svelte": "^4.2.2",
138
- "tslib": "^2.6.2",
139
- "tsx": "^3.14.0",
140
- "type-fest": "^4.6.0",
141
- "typescript": "^5.2.2"
48
+ "@types/tmp": "^0.2.5"
142
49
  },
143
- "packageManager": "pnpm@8.10.2"
144
- }
50
+ "scripts": {
51
+ "check-types": "tsc --noEmit ",
52
+ "lint": "prettier -c \"./**/*.{ts,js,cjs,svelte,json}\"",
53
+ "dev": "NODE_ENV=development tsx --watch ./src/cli/index.ts",
54
+ "start": "tsx ./src/cli/index.ts"
55
+ }
56
+ }
package/readme.md CHANGED
@@ -1,168 +1,58 @@
1
- # pockethost.io
1
+ # PocketHost
2
2
 
3
- ## Introduction
3
+ PocketHost is the open source multi-tenant PocketBase server. PocketHost can run dozens, hundreds, even thousands of PocketBase instances on a single server or across a global cloud.
4
4
 
5
- PocketHost is the open source cloud hosting platform for PocketBase. https://pockethost.io is the flagship service running PocketHost, where you can host your PocketBase projects with zero setup. Create a project like you would in Firebase and Supabase and let PocketHost do the rest.
5
+ Extend PocketHost with its Plugin ecosystem. Start with the bare-bones dev server and add plugins to grow your enterprise into a global cloud.
6
6
 
7
- Features:
7
+ ---
8
8
 
9
- - Create unlimited PocketBase instances
10
- - Each instance runs on a subdomain of `pockethost.io`
11
- - Run your instance in an ultra-beefy shared environment
9
+ ### Sponsored by https://pockethost.io. Instantly host your PocketBase projects.
12
10
 
13
- **Focus on your app**
11
+ ---
14
12
 
15
- Get a live PocketBase instance in 10 seconds with no backend setup:
13
+ ## Quickstart
16
14
 
17
- 1. Create an account at pockethost.io
18
- 2. Provision your first PocketBase instance
19
- 3. Connect from anywhere
20
-
21
- ```ts
22
- const client = new PocketBase(`https://harvest.pockethost.io`)
23
- ```
24
-
25
- **Batteries Included**
26
-
27
- Here's all the Linux/devops stuff that PocketHost does for you:
28
-
29
- - Email and DKIM+SPF and more
30
- - DNS jargon: MX, TXT, CNAME
31
- - SSL cert provisioning and management
32
- - Storage
33
- - Volume mounts
34
- - Cloud computing or VPS deployment
35
- - CDN and static asset hosting
36
- - Amazon AWS
37
- - Lots more - scaling, firewalls, DDoS defense, user security, log rotation, patches, updates, build tools, CPU architectures, multitenancy, on and on
38
-
39
- PocketHost is free and open source project licensed under the MIT License.
40
-
41
- This monorepo contains the entire open source stack that powers pockethost.io. You can use it to run your own private or public multitenant platform.
42
-
43
- **Questions?**
44
-
45
- Head over to Discord and ask away.
46
-
47
- ## Frontend only
48
-
49
- If you just want to work on the UI/frontend client apps, they are configured to connect to pockethost.io out of the box. This is the most convenient way to make UI modifications to the PocketHost project.
50
-
51
- ```bash
52
- git clone git@github.com:benallfree/pockethost.git
53
- cd pockethost
54
- pnpm
55
- pnpm dev:lander # Marketing/blog area
56
- pnpm dev:dashboard # Dashboard/control panel
57
- ```
58
-
59
- ## All our base
60
-
61
- **Running in dev mode**
62
-
63
- Note for OS X users: if the `pocketbase` binaries do not run, it's probably your [security settings](https://support.apple.com/guide/mac-help/open-a-mac-app-from-an-unidentified-developer-mh40616/mac).
64
-
65
- **Prerequisites**
66
-
67
- Local PocketHost development relies on Caddy to create reverse proxies into the various services powering the PocketHost backend. We use `lvh.me` to work with subdomains that resolve to localhost.
68
-
69
- ```bash
70
- brew install caddy
71
15
  ```
72
-
73
- Then:
74
-
75
- ```bash
76
- git clone git@github.com:benallfree/pockethost.git
77
- cd pockethost
78
- pnpm
79
- cp .env-template .env
16
+ npx pockethost serve
80
17
  ```
81
18
 
82
- `.env-template` is preconfigured to make all of PocketHost run locally using `lvh.me` as follows:
83
-
84
- ```bash
85
- pnpm dev
86
-
87
- # marketing/blog
88
- open https://pockethost.lvh.me
89
-
90
- # dashboard
91
- open https://app.pockethost.lvh.me
92
-
93
- # mothership
94
- open https://pockethost-central.pockethost.lvh.me
95
-
96
- # sample edge server (ph1)
97
- open https://instance1.ph1.edge.pockethost.lvh.me
98
-
99
- # second edge server (ph2)
100
- open https://instance2.ph2.edge.pockethost.lvh.me
101
- ```
102
-
103
- Additionally, the setup creates the following users and instances:
104
-
105
- **Admin**
106
- https://pockethost-central.pockethost.lvh.me
107
- login: `admin@pockethost.lvh.me`
108
- password: `password`
109
-
110
- **User**
111
- https://app.pockethost.lvh.me
112
- login: `user@pockethost.lvh.me`
113
- password: `password`
114
-
115
- The user `user@pockethost.lvh.me` owns `instance1` and `instance2`, each which is running in its own region on separate edge servers.
116
-
117
- That's it! You can control all this and much more from the `.env-template` file. It's fully documented, but here it is again just in case.
118
-
119
- ## Public Variables (available to frontend and backend)
19
+ ## Configuration
120
20
 
121
- | Name | Default | Discussion |
122
- | ----------------------- | --------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
123
- | PUBLIC_DEBUG | `true` | The global debugging variable, used by both frontends and backend services. |
124
- | PUBLIC_HTTP_PROTOCOL | `https` | You can use `http` if you like, but there is really no reason to. Caddy automatically provisions SSL certificates even for local development, and Cloudflare (recommended for DNS and CDN) does too. |
125
- | PUBLIC_APEX_DOMAIN | `pockethost.lvh.me` | The apex domain is the domain on which all other subdomains live. If you set up your own PocketHost-based service, you'll want to change this. If you're just focusing on contributing to the project (please do!), then you can leave this as-is. |
126
- | PUBLIC_APP_DOMAIN | `app.<apex>` | The fully qualified domain on which the dashboard/control panel lives (app). Probably no reason to alter this, as it defaults to `app` on the apex domain defined earlier. |
127
- | PUBLIC_BLOG_DOMAIN | `<apex>` | The marketing/blog site lives on `<apex>` itself. |
128
- | PUBLIC_EDGE_APEX_DOMAIN | `<id>.edge.<apex>` | Edge servers are the work horses of PocketHost. They receive incoming requests and route them to PocketBase instances. Each edge server needs a unique `<id>`, which are generally named after geographic regions such as `sfo-1`, `nyc-2`, etc. |
129
- | PUBLIC_MOTHERSHIP_URL | `pockethost-central.<apex>` | The mothership URL is configurable because there may be cases where you want to deploy the mothership to its own central region. It does not live on an edge node. |
21
+ PocketHost supports configuration by environment variable. All PocketHost environment variables begin with `PH_`.
130
22
 
131
- ## Mothership Variables (backend only)
23
+ PocketHost will search the current directory for an `.env` when it loads. Anything in `.env` will be merged with and override any `<PH_HOME>/.env` that may exist.
132
24
 
133
- The Mothership is a set of backend services, including a central `pocketbase` instance, that is the source of truth for the state of PocketHost.
25
+ Use `pockethost config` to set global defaults stored in `<PH_HOME>/.env`.
134
26
 
135
- | Name | Default | Discussion |
136
- | ------------------------- | -------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
137
- | MOTHERSHIP_MIGRATIONS_DIR | `<root>/src/mothership-app/migrations` | The directory where the Mothership migrations live. This is typically kept in revision control, so the default location is within the project directory structure. |
138
- | MOTHERSHIP_HOOKS_DIR | `<root>/src/mothership-app/pb_hooks` | The directory where the Mothership `pb_hooks` live. This is typically kept in revision control, so the default location is within the project directory structure. |
139
- | MOTHERSHIP_ADMIN_USERNAME | `admin@pockethost.lvh.me` | This admin login is created the first time PocketHost runs. |
140
- | MOTHERSHIP_ADMIN_PASSWORD | `password` | |
141
- | DEMO_USER_USERNAME | `user@pockethost.lvh.me` | This login is created the first time PocketHost runs |
142
- | DEMO_USER_PASSWORD | `password` | |
143
- | MOTHERSHIP_PORT | `8091` | The port the Mothership service will listen on. |
144
- | MOTHERSHIP_SEMVER | `(blank)` | The semver used to lock the Mothership to a specific `pocketbase` version range. The Mothership will never launch with a `pocketbase` binary version outside this range. |
145
- | MOTHERSHIP_PRIVATE_URL | `http://mothership.pockdthost.lvh.me` | This should be set to an intranet IP address in production settings. |
27
+ ### Variables
146
28
 
147
- ## Edge Variables (backend only)
29
+ | Name | Default | Discussion |
30
+ | -------------- | -------------------------- | ----------------------------------------------------------------------------- |
31
+ | PH_HOME | `~/.pockethost` | PocketHost's home directory where it holds all data. |
32
+ | PH_APEX_DOMAIN | `pockethost.lvh.me` | The apex domain used for routing and determining instance names. |
33
+ | PH_PORT | `3000` | The port PocketHost listens on |
34
+ | PH_DEBUG | `NODE_ENV==='development'` | Debug mode |
35
+ | PH_PLUGINS | (see below) | A comma-separated list of Plugins to load |
36
+ | PH_DATA_DIR | `~/.pockethost/data` | The root directory for storing PocketHost data, including instance databases. |
37
+ | PH_DEV | `NODE_ENV==='development'` | Whether PocketHost is running in development mode. |
148
38
 
149
- An Edge node is the workhorse of PocketHost. Edge nodes manage `pocketbase` instances, spin them up, shut them down when idle, implement security, and afford regional access to instances. They communicate with the Mothership through `MOTHERSHIP_PRIVATE_URL`.
39
+ ### Default Plugins
150
40
 
151
- | Name | Default | Discussion |
152
- | -------------------------- | -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
153
- | EDGE_DATA_ROOT | `<root>/.data` | Where the edge node should store its pocketbase instance data |
154
- | EDGE_INSTANCE_MIN_IDLE_TTL | `5000` | The minimum amount of time an instance must remain idle (no connections) before an edge node is allowed to shut it down to free up resources |
155
- | EDGE_INSTANCE_POOL_SIZE | `20` | The maximum number of simultaneous instances the edge node will allow to run concurrently. If this limit is exceeded, connections to instances not yet running in the pool will be denied. |
156
- | \*\*IP_CIDR | `(blank)` | A comma-separated list of upstream proxy IP addresses or ranges PocketHost will respond to. |
41
+ - `@pockethost/plugin-console-logger`
42
+ - `@pockethost/plugin-launcher-spawn`
157
43
 
158
- \* Note: `PUBLIC_*` environment variables are visible to the frontend as well.
44
+ ## Plugins
159
45
 
160
- \*\* Note: [Cloudflare IP addresses](https://www.cloudflare.com/ips/) are currently: `173.245.48.0/20,103.21.244.0/22,103.22.200.0/22,103.31.4.0/22,141.101.64.0/18,108.162.192.0/18,190.93.240.0/20,188.114.96.0/20,197.234.240.0/22,198.41.128.0/17,162.158.0.0/15,104.16.0.0/13,104.24.0.0/14,172.64.0.0/13,131.0.72.0/22`. [Learn more about IP CIDR](https://developers.cloudflare.com/fundamentals/setup/allow-cloudflare-ip-addresses/).
46
+ PocketHost uses a hook architecture and features many plugins. Some plugins have a CLI component, whereas others add purely programmatic support to alter how PocketHost works:
161
47
 
162
- ## Security Variables (backend only)
48
+ | name | description |
49
+ | ---------------------- | -------------------------------------------------------------------------------------------------------------------------------------- |
50
+ | plugin-ftp-server | Used to provide secure FTPS access to PocketBase instance data. This FTP server can run along-side a PocketHost server. |
51
+ | plugin-launcher-docker | Launches PocketBase instances via Docker. This is useful for running untrusted PocketBase code and instances. pockethost.io uses this. |
52
+ | plugin-launcher-spawn | Launches PocketBase instances via `spawn()` on the local machine. PocketHost uses this by default. |
53
+ | plugin-waf | A Web Application Firewall |
54
+ | plugin-waf-enforce-ssl | Enforce SSL at the WAF |
163
55
 
164
- | Name | Default | Discussion |
165
- | ------- | --------- | ------------------------------------------------------------------------------------------- |
166
- | IP_CIDR | `(blank)` | A comma-separated list of upstream proxy IP addresses or ranges PocketHost will respond to. |
56
+ ## Support
167
57
 
168
- Note: [Cloudflare IP addresses](https://www.cloudflare.com/ips/) are currently: `173.245.48.0/20,103.21.244.0/22,103.22.200.0/22,103.31.4.0/22,141.101.64.0/18,108.162.192.0/18,190.93.240.0/20,188.114.96.0/20,197.234.240.0/22,198.41.128.0/17,162.158.0.0/15,104.16.0.0/13,104.24.0.0/14,172.64.0.0/13,131.0.72.0/22`. [Learn more about IP CIDR](https://developers.cloudflare.com/fundamentals/setup/allow-cloudflare-ip-addresses/).
58
+ PocketHost has a thriving [Discord community](https://discord.gg/nVTxCMEcGT).
@@ -0,0 +1,104 @@
1
+ import { Command } from 'commander'
2
+ import { parse } from 'dotenv'
3
+ import { existsSync, readFileSync, writeFileSync } from 'fs'
4
+ import { error, info } from '..'
5
+ import { PH_HOME, PH_PLUGINS, PH_PROJECT_DIR } from '../../constants'
6
+ import { getCompatibleVersions, removePackage } from './util'
7
+
8
+ const envFile = () => {
9
+ const envFile = PH_HOME(`.env`)
10
+ if (!existsSync(envFile)) {
11
+ writeFileSync(envFile, '')
12
+ }
13
+ return envFile
14
+ }
15
+
16
+ const _parse = () =>
17
+ parse(readFileSync(envFile(), { encoding: 'utf8' }).toString())
18
+
19
+ export const PluginCommand = () => {
20
+ const cmd = new Command(`plugin`)
21
+ .description(`Manage PocketHost plugins`)
22
+ .addCommand(
23
+ new Command(`install`)
24
+ .alias(`i`)
25
+ .argument(`<name>`, `NPM registry plugin name`)
26
+ .description(`Install a plugin`)
27
+ .action(async (name: string) => {
28
+ try {
29
+ const pkg = JSON.parse(
30
+ readFileSync(PH_PROJECT_DIR(`package.json`), 'utf8').toString(),
31
+ )
32
+ const { version } = pkg
33
+ await getCompatibleVersions(`pockethost`, version, [name])
34
+
35
+ const values = _parse()
36
+ values[`PH_PLUGINS`] = [
37
+ ...(values[`PH_PLUGINS`]
38
+ ?.split(/,/)
39
+ .map((v) => v.trim())
40
+ .filter((v) => !!v) || []),
41
+ name,
42
+ ].join(`,`)
43
+ writeFileSync(
44
+ envFile(),
45
+ Object.entries(values)
46
+ .map(([k, v]) => `${k}=${v}`)
47
+ .join('\n'),
48
+ )
49
+ info(`Installed ${name}`)
50
+ info(`Written to ${envFile()}`)
51
+ } catch (e) {
52
+ error(`${e}`)
53
+ }
54
+ }),
55
+ )
56
+
57
+ .addCommand(
58
+ new Command(`remove`)
59
+ .alias(`rm`)
60
+ .alias(`uninstall`)
61
+ .alias(`delete`)
62
+ .argument(`<name>`, `Plugin name`)
63
+ .description(`Remove a plugin`)
64
+ .action(async (name) => {
65
+ try {
66
+ await removePackage(name)
67
+ const values = _parse()
68
+ values[`PH_PLUGINS`] = [
69
+ ...(values[`PH_PLUGINS`]
70
+ ?.split(/,/)
71
+ .map((v) => v.trim())
72
+ .filter((v) => v !== name) || []),
73
+ ].join(`,`)
74
+ writeFileSync(
75
+ envFile(),
76
+ Object.entries(values)
77
+ .map(([k, v]) => `${k}=${v}`)
78
+ .join('\n'),
79
+ )
80
+ info(`Removed ${name}`)
81
+ info(`Written to ${envFile()}`)
82
+ } catch (e) {
83
+ error(`${e}`)
84
+ }
85
+ }),
86
+ )
87
+ .addCommand(
88
+ new Command(`update`)
89
+ .alias(`up`)
90
+ .description(`Update all plugins`)
91
+ .action(async () => {
92
+ try {
93
+ const pkg = JSON.parse(
94
+ readFileSync(PH_PROJECT_DIR(`package.json`), 'utf8').toString(),
95
+ )
96
+ const { version } = pkg
97
+ await getCompatibleVersions(`pockethost`, version, PH_PLUGINS())
98
+ } catch (e) {
99
+ error(`${e}`)
100
+ }
101
+ }),
102
+ )
103
+ return cmd
104
+ }
@@ -0,0 +1,138 @@
1
+ import { exec } from 'child_process'
2
+ import fs from 'fs/promises'
3
+ import { Listr, ListrTask } from 'listr2'
4
+ import { createRequire } from 'module'
5
+ import fetch from 'node-fetch'
6
+ import semver from 'semver'
7
+ import util from 'util'
8
+
9
+ const execPromise = util.promisify(exec)
10
+ const require = createRequire(import.meta.url)
11
+
12
+ async function getPackageJson(packageName: string): Promise<any | null> {
13
+ try {
14
+ const packageJsonPath = require.resolve(`${packageName}/package.json`)
15
+ const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf8'))
16
+ return packageJson
17
+ } catch (error) {
18
+ return null
19
+ }
20
+ }
21
+
22
+ async function fetchPackageData(packageName: string): Promise<any | null> {
23
+ const npmRegistryUrl = `https://registry.npmjs.org/${packageName}`
24
+ try {
25
+ const response = await fetch(npmRegistryUrl)
26
+ return await response.json()
27
+ } catch (error) {
28
+ return null
29
+ }
30
+ }
31
+
32
+ async function installPackage(
33
+ packageName: string,
34
+ version: string,
35
+ ): Promise<void> {
36
+ try {
37
+ await execPromise(`npm install -g ${packageName}@${version}`)
38
+ } catch (e) {
39
+ throw new Error(`Error installing ${packageName}@${version}: ${e}`)
40
+ }
41
+ }
42
+
43
+ export async function removePackage(packageName: string): Promise<void> {
44
+ try {
45
+ await execPromise(`npm remove -g ${packageName}`)
46
+ } catch (e) {
47
+ throw new Error(`Error removing ${packageName}: ${e}`)
48
+ }
49
+ }
50
+
51
+ async function checkPackageCompatibility(
52
+ packageA: string,
53
+ versionA: string,
54
+ packageB: string,
55
+ ): Promise<string> {
56
+ const localPackageJson = await getPackageJson(packageB)
57
+
58
+ if (localPackageJson) {
59
+ const { version: localPackageBVersion, dependencies } = localPackageJson
60
+
61
+ if (dependencies && semver.satisfies(versionA, dependencies[packageA])) {
62
+ return `Local ${packageB}@${localPackageBVersion} is already compatible with ${packageA}@${versionA}`
63
+ }
64
+ }
65
+
66
+ const packageBData = await fetchPackageData(packageB)
67
+ if (!packageBData) {
68
+ throw new Error(`Error fetching data for ${packageB}`)
69
+ }
70
+
71
+ const versions = packageBData.versions
72
+ let latestCompatibleVersion: string | null = null
73
+
74
+ for (const version in versions) {
75
+ const dependencies = versions[version].dependencies
76
+ if (
77
+ dependencies &&
78
+ packageA in dependencies &&
79
+ semver.satisfies(versionA, dependencies[packageA])
80
+ ) {
81
+ if (
82
+ !latestCompatibleVersion ||
83
+ semver.gt(version, latestCompatibleVersion)
84
+ ) {
85
+ latestCompatibleVersion = version
86
+ }
87
+ }
88
+ }
89
+
90
+ if (latestCompatibleVersion) {
91
+ await installPackage(packageB, latestCompatibleVersion)
92
+ return `${packageB}@${latestCompatibleVersion} installed successfully`
93
+ } else {
94
+ throw new Error(
95
+ `No compatible version of ${packageB} found for ${packageA}@${versionA}`,
96
+ )
97
+ }
98
+ }
99
+
100
+ export async function getCompatibleVersions(
101
+ packageA: string,
102
+ versionA: string,
103
+ packagesB: string[],
104
+ ): Promise<void> {
105
+ const tasks = packagesB.map(
106
+ (packageB) =>
107
+ ({
108
+ title: `Checking ${packageB}...`,
109
+ task: async (_, task) => {
110
+ try {
111
+ const result = await checkPackageCompatibility(
112
+ packageA,
113
+ versionA,
114
+ packageB,
115
+ )
116
+ task.output = result
117
+ } catch (error) {
118
+ task.output = `${error}`
119
+ throw error
120
+ }
121
+ },
122
+ }) as ListrTask,
123
+ )
124
+
125
+ const listr = new Listr(tasks, {
126
+ concurrent: true,
127
+ rendererOptions: { collapseSubtasks: false },
128
+ })
129
+
130
+ await listr.run()
131
+ }
132
+
133
+ // Example usage
134
+ // const packageA = 'express'
135
+ // const versionA = '4.17.1'
136
+ // const packagesB = ['some-package', 'another-package', 'yet-another-package']
137
+
138
+ // getCompatibleVersions(packageA, versionA, packagesB)