pockethost 1.4.0 → 1.6.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.
package/src/plugin.ts ADDED
@@ -0,0 +1,54 @@
1
+ import { forEach } from '@s-libs/micro-dash'
2
+ import { produce } from 'immer'
3
+ import {
4
+ PocketHostPlugin,
5
+ doSettingsFilter,
6
+ namespace,
7
+ newId,
8
+ onAfterPluginsLoadedAction,
9
+ onInstanceConfigFilter,
10
+ onInstanceLogAction,
11
+ onServeAction,
12
+ onServeSlugsFilter,
13
+ } from '../common'
14
+ import { dbg, info } from './cli'
15
+ import { INTERNAL_APP_SECRET, PH_PROJECT_DIR, settings } from './constants'
16
+ import { setConfig } from './core/config'
17
+ import { serve } from './server'
18
+
19
+ export const pockethost: PocketHostPlugin = async ({}) => {
20
+ onInstanceConfigFilter(async (config) => {
21
+ return produce(config, (draft) => {
22
+ draft.binds.hooks.push({
23
+ src: PH_PROJECT_DIR(`src/instance-app/hooks/**/*`),
24
+ base: PH_PROJECT_DIR(`src/instance-app/hooks`),
25
+ })
26
+ })
27
+ })
28
+
29
+ onInstanceLogAction(async ({ instance, type, data }) => {
30
+ dbg(namespace(instance.subdomain, type), data.toString())
31
+ })
32
+
33
+ onServeAction(async ({ only }) => {
34
+ if (!only.includes(`pockethost`)) return
35
+ serve()
36
+ })
37
+
38
+ onServeSlugsFilter(async (slugs) => {
39
+ return [...slugs, `pockethost`]
40
+ })
41
+
42
+ onAfterPluginsLoadedAction(async () => {
43
+ const allSettings = await doSettingsFilter(settings)
44
+ forEach(allSettings, (v, k) => {
45
+ dbg(`${k}: ${v}`)
46
+ })
47
+ }, 99)
48
+
49
+ onAfterPluginsLoadedAction(async () => {
50
+ if (INTERNAL_APP_SECRET()) return
51
+ info(`Generating internal app secret...`)
52
+ setConfig(`PH_INTERNAL_APP_SECRET`, newId(30))
53
+ })
54
+ }
package/src/server.ts CHANGED
@@ -3,7 +3,6 @@ import 'express-async-errors'
3
3
  import { default as httpProxy } from 'http-proxy'
4
4
  import {
5
5
  LoggerService,
6
- PocketHostPlugin,
7
6
  doAfterInstanceFoundAction,
8
7
  doAfterServerStartAction,
9
8
  doAppMountedAction,
@@ -12,14 +11,17 @@ import {
12
11
  doIncomingRequestAction,
13
12
  doRequestErrorAction,
14
13
  doRequestErrorMessageFilter,
15
- onInstanceLogAction,
16
- onServeAction,
17
- onServeSlugsFilter,
18
14
  } from '../common'
19
- import { APEX_DOMAIN, PORT, asyncExitHook } from '../core'
20
- import { dbg } from './cli'
21
-
22
- const serve = async () => {
15
+ import {
16
+ APEX_DOMAIN,
17
+ INTERNAL_APP_AUTH_HEADER,
18
+ INTERNAL_APP_PREFIX,
19
+ INTERNAL_APP_SECRET,
20
+ PORT,
21
+ asyncExitHook,
22
+ } from '../core'
23
+
24
+ export const serve = async () => {
23
25
  const _proxyLogger = LoggerService().create('ProxyService')
24
26
  const { dbg, error, info, trace, warn } = _proxyLogger
25
27
 
@@ -50,7 +52,25 @@ const serve = async () => {
50
52
  }
51
53
  app.use(errorHandler)
52
54
 
53
- doAppMountedAction({ app })
55
+ const internalApp = express()
56
+ internalApp.use((req, res, next) => {
57
+ dbg(`Got a request to internal app ${req.url}`)
58
+ next()
59
+ })
60
+ internalApp.use((req, res, next) => {
61
+ const authHeader = req.headers[INTERNAL_APP_AUTH_HEADER]
62
+ const internalAppSecret = INTERNAL_APP_SECRET()
63
+
64
+ if (authHeader !== internalAppSecret) {
65
+ res.status(403).send('Forbidden')
66
+ return
67
+ }
68
+
69
+ next()
70
+ })
71
+ app.use(`/${INTERNAL_APP_PREFIX}`, internalApp)
72
+
73
+ doAppMountedAction({ app, internalApp })
54
74
 
55
75
  app.all(`*`, async (req, res, next) => {
56
76
  const method = req.method
@@ -88,11 +108,7 @@ const serve = async () => {
88
108
  })
89
109
 
90
110
  const url = await doGetOrProvisionInstanceUrlFilter('', {
91
- req,
92
- res,
93
111
  instance,
94
- host,
95
- subdomain,
96
112
  })
97
113
 
98
114
  dbg(`Forwarding request for ${req.url} to instance ${url}`)
@@ -111,18 +127,3 @@ const serve = async () => {
111
127
  info(`Shutting down pockethost`)
112
128
  })
113
129
  }
114
-
115
- export const pockethost: PocketHostPlugin = async ({}) => {
116
- onInstanceLogAction(async ({ instance, type, data }) => {
117
- dbg(data.toString())
118
- })
119
-
120
- onServeAction(async ({ only }) => {
121
- if (!only.includes(`pockethost`)) return
122
- serve()
123
- })
124
-
125
- onServeSlugsFilter(async (slugs) => {
126
- return [...slugs, `pockethost`]
127
- })
128
- }