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.
- package/common.ts +1 -0
- package/core.ts +2 -0
- package/package.json +24 -112
- package/readme.md +36 -146
- package/src/cli/PluginCommand/index.ts +104 -0
- package/src/cli/PluginCommand/util.ts +138 -0
- package/src/cli/commands/ConfigCommand/index.ts +99 -0
- package/src/cli/commands/ServeCommand/index.ts +20 -0
- package/src/cli/index.ts +38 -0
- package/src/common/CleanupManager.ts +65 -0
- package/src/common/Logger.ts +168 -0
- package/src/common/ResourceAllocator.ts +49 -0
- package/src/common/TimerManager.ts +64 -0
- package/src/common/assert.ts +26 -0
- package/src/common/debug.ts +8 -0
- package/src/common/events.ts +53 -0
- package/src/common/index.ts +12 -0
- package/src/common/mergeConfig.ts +27 -0
- package/src/common/mkSingleton.ts +28 -0
- package/src/common/plugin/action.ts +60 -0
- package/src/common/plugin/filter.ts +65 -0
- package/src/common/plugin/helpers.ts +17 -0
- package/src/common/plugin/index.ts +27 -0
- package/src/common/pocketbase-client-helpers/RestHelper.ts +69 -0
- package/src/common/pocketbase-client-helpers/index.ts +1 -0
- package/src/common/promiseSerializer.ts +22 -0
- package/src/common/schema/Instance.ts +45 -0
- package/src/common/schema/InstanceLog.ts +12 -0
- package/src/common/schema/Rest/CreateInstance.ts +20 -0
- package/src/common/schema/Rest/DeleteInstance.ts +21 -0
- package/src/common/schema/Rest/GetUserTokenInfo.ts +22 -0
- package/src/common/schema/Rest/UpdateInstance.ts +61 -0
- package/src/common/schema/Rest/index.ts +23 -0
- package/src/common/schema/User.ts +24 -0
- package/src/common/schema/index.ts +6 -0
- package/src/common/schema/types.ts +18 -0
- package/src/common/schema/util.ts +1 -0
- package/src/constants.ts +74 -0
- package/src/core/Settings.ts +119 -0
- package/src/core/asyncExecutionGuard.ts +49 -0
- package/src/core/exit.ts +11 -0
- package/src/core/index.ts +7 -0
- package/src/core/internal.ts +3 -0
- package/src/core/smartFetch.ts +32 -0
- package/src/core/tryFetch.ts +66 -0
- package/src/server.ts +119 -0
- package/dist/index.mjs +0 -3156
- package/dist/instance-app/migrations/1699198734_pockethost_defaults.js +0 -39
- package/dist/instance-app/pb_hooks/_ph_admin_sync.pb.js +0 -71
- package/dist/instance-app/pb_hooks/_ph_lib.js +0 -19
- package/dist/instance-app/tsconfig.json +0 -8
- package/dist/instance-app/types/goja.d.ts +0 -58
- package/dist/instance-app/types/lib.d.ts +0 -5
- package/dist/instance-app/types/types.d.ts +0 -19573
- package/dist/mothership-app/migrations/1705062576_collections_snapshot.js +0 -1863
- package/dist/mothership-app/migrations/1705083826_updated_stats.js +0 -721
- package/dist/mothership-app/migrations/1706060446_updated_instances.js +0 -44
- package/dist/mothership-app/pb_hooks/src/instances-create.pb.js +0 -65
- package/dist/mothership-app/pb_hooks/src/instances-delete.pb.js +0 -69
- package/dist/mothership-app/pb_hooks/src/instances-migrate-versions.pb.js +0 -33
- package/dist/mothership-app/pb_hooks/src/instances-notify-discord-after-create.pb.js +0 -31
- package/dist/mothership-app/pb_hooks/src/instances-notify-on-maintenance-mode.pb.js +0 -52
- package/dist/mothership-app/pb_hooks/src/instances-reset-idle.pb.js +0 -7
- package/dist/mothership-app/pb_hooks/src/instances-update.pb.js +0 -121
- package/dist/mothership-app/pb_hooks/src/instances-validate-before-create.pb.js +0 -16
- package/dist/mothership-app/pb_hooks/src/instances-validate-before-update.pb.js +0 -43
- package/dist/mothership-app/pb_hooks/src/lib.js +0 -171
- package/dist/mothership-app/pb_hooks/src/ls.pb.js +0 -159
- package/dist/mothership-app/pb_hooks/src/mail.pb.js +0 -49
- package/dist/mothership-app/pb_hooks/src/notifications.pb.js +0 -62
- package/dist/mothership-app/pb_hooks/src/random-words.js +0 -2085
- package/dist/mothership-app/pb_hooks/src/signup.pb.js +0 -214
- package/dist/mothership-app/pb_hooks/src/sns.pb.js +0 -133
- package/dist/mothership-app/pb_hooks/src/unsubscribe.pb.js +0 -33
- package/dist/mothership-app/pb_hooks/src/user-token.pb.js +0 -33
- package/dist/mothership-app/pb_hooks/src/user-welcome-msg.pb.js +0 -32
- package/dist/mothership-app/pb_hooks/src/versions-route.pb.js +0 -12
- package/dist/mothership-app/pb_hooks/src/versions.js +0 -0
- package/dist/mothership-app/pb_hooks/tsconfig.json +0 -8
- package/dist/mothership-app/pb_hooks/types/global.d.ts +0 -1
- package/dist/mothership-app/pb_hooks/types/goja.d.ts +0 -58
- package/dist/mothership-app/pb_hooks/types/lemon.d.ts +0 -12
- package/dist/mothership-app/pb_hooks/types/lib.d.ts +0 -55
- package/dist/mothership-app/pb_hooks/types/types.d.ts +0 -19573
- package/dist/mothership-app/ph_app/index.js +0 -11
- package/dist/mothership-app/ph_app/package-lock.json +0 -657
- package/dist/mothership-app/ph_app/package.json +0 -14
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { DEBUG } from '../debug'
|
|
2
|
+
|
|
3
|
+
// Global registry of filters and actions
|
|
4
|
+
|
|
5
|
+
export enum PocketHostAction {
|
|
6
|
+
/** Core actions */
|
|
7
|
+
Core_Init = 'init',
|
|
8
|
+
Core_OnAppMount = 'on_app_mount',
|
|
9
|
+
Core_AfterServeStart = 'after_serve_start',
|
|
10
|
+
|
|
11
|
+
Core_Cli_Before_ServeCommand = 'before_serve',
|
|
12
|
+
Core_Log = 'log',
|
|
13
|
+
Core_InstanceLog = 'instance_log',
|
|
14
|
+
|
|
15
|
+
Core_StartInstance = 'start_instance',
|
|
16
|
+
|
|
17
|
+
Core_BeforeInstanceStarted = 'before_instance_started',
|
|
18
|
+
Core_AfterInstanceStarted = 'after_instance_started',
|
|
19
|
+
|
|
20
|
+
Core_BeforeInstanceStopped = 'before_instance_stopped',
|
|
21
|
+
Core_AfterInstanceStopped = 'after_instance_stopped',
|
|
22
|
+
|
|
23
|
+
Core_AfterInstanceFound = 'after_instance_found',
|
|
24
|
+
|
|
25
|
+
Core_OnRequestError = 'edge_on_request_error',
|
|
26
|
+
Core_Request = 'edge_request',
|
|
27
|
+
|
|
28
|
+
/** Firewall actions */
|
|
29
|
+
Waf_OnRequestError = 'waf_on_request_error',
|
|
30
|
+
Waf_OnAppMiddleware = 'waf_on_app_middleware',
|
|
31
|
+
|
|
32
|
+
/** Edge actions */
|
|
33
|
+
/** Health actions */
|
|
34
|
+
Health_OnMessage = 'health_on_message',
|
|
35
|
+
Health_OnError = 'health_on_error',
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const actions: {
|
|
39
|
+
[key in PocketHostAction]?: ActionHandler[]
|
|
40
|
+
} = {}
|
|
41
|
+
|
|
42
|
+
export type ActionHandler = (...args: any[]) => Promise<void>
|
|
43
|
+
|
|
44
|
+
export async function registerAction(
|
|
45
|
+
action: PocketHostAction,
|
|
46
|
+
handler: (...args: any[]) => Promise<void>,
|
|
47
|
+
) {
|
|
48
|
+
if (!(action in actions)) actions[action] = []
|
|
49
|
+
actions[action]!.push(handler)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export async function action(actionName: PocketHostAction, ...rest: any[]) {
|
|
53
|
+
if (DEBUG() && actionName !== PocketHostAction.Core_Log) {
|
|
54
|
+
console.log(`a:${actionName}`)
|
|
55
|
+
}
|
|
56
|
+
const action = actions[actionName]
|
|
57
|
+
if (!action) return false
|
|
58
|
+
await Promise.all(action.map((handler) => handler(...rest)))
|
|
59
|
+
return true
|
|
60
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { DEBUG } from '../debug'
|
|
2
|
+
|
|
3
|
+
export enum PocketHostFilter {
|
|
4
|
+
/** Core filters */
|
|
5
|
+
Core_CliCommands = 'core_cli_commands',
|
|
6
|
+
Core_SpawnConfig = 'core_spawn_config',
|
|
7
|
+
Core_GetInstanceByRequestInfo = 'core_get_instance_by_request_info',
|
|
8
|
+
Core_GetInstanceById = 'core_get_instance_by_id',
|
|
9
|
+
Core_ErrorSpawningInstanceMessage = 'core_error_spawning_instance_message',
|
|
10
|
+
Core_FailedToLaunchInstanceMessage = 'core_failed_to_launch_instance_message',
|
|
11
|
+
Core_AuthenticateRequest = 'core_authenticate_request',
|
|
12
|
+
Core_RequestError_Message = 'core_request_error_message',
|
|
13
|
+
Core_GetOrProvisionInstanceUrl = 'core_get_or_provision_instance_url',
|
|
14
|
+
Core_PocketBaseLaunchHandler = 'core_pocket_base_launch_handler',
|
|
15
|
+
Core_NewInstanceRecord = 'core_new_instance_record',
|
|
16
|
+
|
|
17
|
+
/** Mothership Filters */
|
|
18
|
+
Mothership_MaintenanceMode_Message = 'mothership_maintenance_mode_message',
|
|
19
|
+
Mothership_UnverifiedAccountError_Message = 'mothership_unverified_account_error_message',
|
|
20
|
+
Mothership_PublicUrl = 'mothership_public_url',
|
|
21
|
+
|
|
22
|
+
/** Firewall Filters */
|
|
23
|
+
Waf_VHosts = 'waf_vhosts',
|
|
24
|
+
Waf_OnError_Message = 'waf_on_error_message',
|
|
25
|
+
Waf_AppMount = 'waf_app_mount',
|
|
26
|
+
|
|
27
|
+
/** Edge self checks */
|
|
28
|
+
EdgeSelfCheck_Checks = 'edge_self_check_checks',
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type FilterHandler<T> = (carry: T, ...rest: any[]) => T | Promise<T>
|
|
32
|
+
type FilterEntry<T> = {
|
|
33
|
+
priority: number
|
|
34
|
+
handler: FilterHandler<T>
|
|
35
|
+
}
|
|
36
|
+
const filters: {
|
|
37
|
+
[key in PocketHostFilter]?: FilterEntry<any>[]
|
|
38
|
+
} = {}
|
|
39
|
+
|
|
40
|
+
export async function registerFilter<T>(
|
|
41
|
+
filter: PocketHostFilter,
|
|
42
|
+
handler: FilterHandler<T>,
|
|
43
|
+
priority: number = 10,
|
|
44
|
+
) {
|
|
45
|
+
if (!(filter in filters)) filters[filter] = []
|
|
46
|
+
filters[filter]!.push({ priority, handler })
|
|
47
|
+
filters[filter]!.sort((a, b) => a.priority - b.priority)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export async function filter<TRet, TIn = TRet>(
|
|
51
|
+
filterName: PocketHostFilter,
|
|
52
|
+
initialValue: TIn,
|
|
53
|
+
...rest: any[]
|
|
54
|
+
) {
|
|
55
|
+
if (DEBUG()) {
|
|
56
|
+
console.log(`f:${filterName}`)
|
|
57
|
+
}
|
|
58
|
+
const filter = filters[filterName]
|
|
59
|
+
if (!filter) return initialValue
|
|
60
|
+
return filter.reduce(
|
|
61
|
+
(carry, entry) =>
|
|
62
|
+
carry.then((carryRes) => entry.handler(carryRes, ...rest)),
|
|
63
|
+
Promise.resolve(initialValue),
|
|
64
|
+
) as TRet
|
|
65
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { InstanceFields } from '../schema'
|
|
2
|
+
import { ActionHandler, PocketHostAction, registerAction } from './action'
|
|
3
|
+
import { PocketHostFilter, filter } from './filter'
|
|
4
|
+
|
|
5
|
+
export async function getInstanceById(instanceId: string) {
|
|
6
|
+
return await filter<InstanceFields | undefined>(
|
|
7
|
+
PocketHostFilter.Core_GetInstanceById,
|
|
8
|
+
undefined,
|
|
9
|
+
{ instanceId },
|
|
10
|
+
)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const onAppMount = (cb: ActionHandler) =>
|
|
14
|
+
registerAction(PocketHostAction.Core_OnAppMount, cb)
|
|
15
|
+
|
|
16
|
+
export const onInit = (cb: ActionHandler) =>
|
|
17
|
+
registerAction(PocketHostAction.Core_Init, cb)
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { isString } from '@s-libs/micro-dash'
|
|
2
|
+
import { registerAction } from './action'
|
|
3
|
+
import { registerFilter } from './filter'
|
|
4
|
+
export * from './action'
|
|
5
|
+
export * from './filter'
|
|
6
|
+
export * from './helpers'
|
|
7
|
+
|
|
8
|
+
export type PocketHostPlugin = (api: PocketHostPluginApi) => Promise<void>
|
|
9
|
+
|
|
10
|
+
export type PocketHostPluginApi = {
|
|
11
|
+
registerAction: typeof registerAction
|
|
12
|
+
registerFilter: typeof registerFilter
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const loadPlugins = async (plugins: (string | PocketHostPlugin)[]) => {
|
|
16
|
+
await Promise.all(
|
|
17
|
+
plugins.map(async (pluginPathOrModule) => {
|
|
18
|
+
const plugin = await (async () => {
|
|
19
|
+
if (!isString(pluginPathOrModule)) return pluginPathOrModule
|
|
20
|
+
const plugin = await import(pluginPathOrModule)
|
|
21
|
+
return plugin.default
|
|
22
|
+
})()
|
|
23
|
+
await plugin({ registerAction, registerFilter })
|
|
24
|
+
}),
|
|
25
|
+
)
|
|
26
|
+
return plugins
|
|
27
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import Ajv, { JSONSchemaType } from 'ajv'
|
|
2
|
+
import PocketBase, { ClientResponseError } from 'pocketbase'
|
|
3
|
+
import type { JsonObject } from 'type-fest'
|
|
4
|
+
import { LoggerService, RestCommands, RestMethods } from '..'
|
|
5
|
+
|
|
6
|
+
export type RestHelperConfig = {
|
|
7
|
+
client: PocketBase
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export type RestHelper = ReturnType<typeof createRestHelper>
|
|
11
|
+
|
|
12
|
+
export const createRestHelper = (config: RestHelperConfig) => {
|
|
13
|
+
const _logger = LoggerService().create(`RestHelper`)
|
|
14
|
+
const { client } = config
|
|
15
|
+
|
|
16
|
+
const mkRest = <TPayload extends JsonObject, TResult extends JsonObject>(
|
|
17
|
+
cmd: RestCommands,
|
|
18
|
+
method: RestMethods,
|
|
19
|
+
schema: JSONSchemaType<TPayload>,
|
|
20
|
+
) => {
|
|
21
|
+
const validator = new Ajv().compile(schema)
|
|
22
|
+
return async (payload: TPayload): Promise<TResult> => {
|
|
23
|
+
const _restCallLogger = _logger.create(cmd)
|
|
24
|
+
const { dbg, error } = _restCallLogger
|
|
25
|
+
|
|
26
|
+
dbg(`Executing REST call`)
|
|
27
|
+
if (!validator(payload)) {
|
|
28
|
+
throw new Error(`Invalid REST payload: ${validator.errors}`)
|
|
29
|
+
}
|
|
30
|
+
const _payload = { ...payload }
|
|
31
|
+
|
|
32
|
+
const url = `/api/${cmd}${
|
|
33
|
+
method === RestMethods.Post ? '' : '/:id'
|
|
34
|
+
}`.replace(/:([a-zA-Z]+)/g, (_, key) => {
|
|
35
|
+
if (!(key in _payload)) {
|
|
36
|
+
throw new Error(`Payload must include '${key}`)
|
|
37
|
+
}
|
|
38
|
+
const value = _payload[key]!
|
|
39
|
+
delete _payload[key]
|
|
40
|
+
return encodeURIComponent(value.toString())
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
const options: any = {
|
|
44
|
+
method: method,
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (method !== RestMethods.Get) {
|
|
48
|
+
options.body = _payload
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
dbg({ url, options })
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
const res = await client.send(url, options)
|
|
55
|
+
dbg(res)
|
|
56
|
+
return res
|
|
57
|
+
} catch (e) {
|
|
58
|
+
if (e instanceof ClientResponseError) {
|
|
59
|
+
error(`REST error: ${e.originalError}`)
|
|
60
|
+
throw e.originalError
|
|
61
|
+
}
|
|
62
|
+
error(`REST error: ${e}`)
|
|
63
|
+
throw e
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return { mkRest }
|
|
69
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './RestHelper'
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
type JobQueue = {
|
|
2
|
+
[key: string]: Promise<any> | undefined
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
const jobQueue: JobQueue = {}
|
|
6
|
+
|
|
7
|
+
export function enqueueJob(
|
|
8
|
+
key: string,
|
|
9
|
+
promiseFn: () => Promise<any>,
|
|
10
|
+
): Promise<any> {
|
|
11
|
+
const currentJob = jobQueue[key]
|
|
12
|
+
|
|
13
|
+
const chainedPromise = currentJob ? currentJob.then(promiseFn) : promiseFn()
|
|
14
|
+
|
|
15
|
+
jobQueue[key] = chainedPromise.finally(() => {
|
|
16
|
+
if (jobQueue[key] === chainedPromise) {
|
|
17
|
+
delete jobQueue[key]
|
|
18
|
+
}
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
return chainedPromise
|
|
22
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { BaseFields, RecordId, Subdomain, UserFields, UserId } from '.'
|
|
2
|
+
|
|
3
|
+
export type VersionId = string
|
|
4
|
+
|
|
5
|
+
export const INSTANCE_COLLECTION = 'instances'
|
|
6
|
+
|
|
7
|
+
export enum InstanceStatus {
|
|
8
|
+
Unknown = '',
|
|
9
|
+
Idle = 'idle',
|
|
10
|
+
Port = 'porting',
|
|
11
|
+
Starting = 'starting',
|
|
12
|
+
Running = 'running',
|
|
13
|
+
Failed = 'failed',
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type InstanceSecretKey = string
|
|
17
|
+
export type InstanceSecretValue = string
|
|
18
|
+
export type InstanceSecretCollection = {
|
|
19
|
+
[name: InstanceSecretKey]: InstanceSecretValue
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type InstanceFields = BaseFields & {
|
|
23
|
+
subdomain: Subdomain
|
|
24
|
+
uid: UserId
|
|
25
|
+
status: InstanceStatus
|
|
26
|
+
version: VersionId
|
|
27
|
+
secrets: InstanceSecretCollection | null
|
|
28
|
+
maintenance: boolean
|
|
29
|
+
suspension: string
|
|
30
|
+
syncAdmin: boolean
|
|
31
|
+
cname: string
|
|
32
|
+
dev: boolean
|
|
33
|
+
cname_active: boolean
|
|
34
|
+
notifyMaintenanceMode: boolean
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export type WithUser = {
|
|
38
|
+
expand: { uid: UserFields }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type InstanceFields_WithUser = InstanceFields & WithUser
|
|
42
|
+
|
|
43
|
+
export type InstanceFields_Create = Omit<InstanceFields, keyof BaseFields>
|
|
44
|
+
|
|
45
|
+
export type InstanceRecordsById = { [_: RecordId]: InstanceFields }
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { JSONSchemaType } from 'ajv'
|
|
2
|
+
import { InstanceFields, Subdomain } from '..'
|
|
3
|
+
|
|
4
|
+
export type CreateInstancePayload = {
|
|
5
|
+
subdomain: Subdomain
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type CreateInstanceResult = {
|
|
9
|
+
instance: InstanceFields
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const CreateInstancePayloadSchema: JSONSchemaType<CreateInstancePayload> =
|
|
13
|
+
{
|
|
14
|
+
type: 'object',
|
|
15
|
+
properties: {
|
|
16
|
+
subdomain: { type: 'string' },
|
|
17
|
+
},
|
|
18
|
+
required: ['subdomain'],
|
|
19
|
+
additionalProperties: false,
|
|
20
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { JSONSchemaType } from 'ajv'
|
|
2
|
+
import { InstanceId } from '..'
|
|
3
|
+
|
|
4
|
+
export type DeleteInstancePayload = {
|
|
5
|
+
id: InstanceId
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type DeleteInstanceResult = {
|
|
9
|
+
status: 'ok' | 'error'
|
|
10
|
+
message?: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const DeleteInstancePayloadSchema: JSONSchemaType<DeleteInstancePayload> =
|
|
14
|
+
{
|
|
15
|
+
type: 'object',
|
|
16
|
+
properties: {
|
|
17
|
+
id: { type: 'string' },
|
|
18
|
+
},
|
|
19
|
+
required: ['id'],
|
|
20
|
+
additionalProperties: false,
|
|
21
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { JSONSchemaType } from 'ajv'
|
|
2
|
+
import { UserId } from '..'
|
|
3
|
+
|
|
4
|
+
export type GetUserTokenPayload = {
|
|
5
|
+
id: UserId
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type GetUserTokenResult = {
|
|
9
|
+
id: string
|
|
10
|
+
email: string
|
|
11
|
+
tokenKey: string
|
|
12
|
+
passwordHash: string
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const GetUserTokenPayloadSchema: JSONSchemaType<GetUserTokenPayload> = {
|
|
16
|
+
type: 'object',
|
|
17
|
+
properties: {
|
|
18
|
+
id: { type: 'string' },
|
|
19
|
+
},
|
|
20
|
+
required: ['id'],
|
|
21
|
+
additionalProperties: false,
|
|
22
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { JSONSchemaType } from 'ajv'
|
|
2
|
+
import { InstanceFields, InstanceId } from '..'
|
|
3
|
+
|
|
4
|
+
export type UpdateInstancePayload = {
|
|
5
|
+
id: InstanceId
|
|
6
|
+
fields: Partial<
|
|
7
|
+
Pick<
|
|
8
|
+
InstanceFields,
|
|
9
|
+
| 'maintenance'
|
|
10
|
+
| 'secrets'
|
|
11
|
+
| 'subdomain'
|
|
12
|
+
| 'syncAdmin'
|
|
13
|
+
| 'version'
|
|
14
|
+
| 'dev'
|
|
15
|
+
| 'cname'
|
|
16
|
+
| 'notifyMaintenanceMode'
|
|
17
|
+
>
|
|
18
|
+
>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const SECRET_KEY_REGEX = /^[A-Z][A-Z0-9_]*$/
|
|
22
|
+
|
|
23
|
+
export type UpdateInstanceResult = {
|
|
24
|
+
status: 'ok' | 'error'
|
|
25
|
+
message?: string
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const UpdateInstancePayloadSchema: JSONSchemaType<UpdateInstancePayload> =
|
|
29
|
+
{
|
|
30
|
+
type: 'object',
|
|
31
|
+
properties: {
|
|
32
|
+
id: { type: 'string' },
|
|
33
|
+
fields: {
|
|
34
|
+
type: 'object',
|
|
35
|
+
properties: {
|
|
36
|
+
syncAdmin: { type: 'boolean', nullable: true },
|
|
37
|
+
subdomain: { type: 'string', nullable: true },
|
|
38
|
+
maintenance: { type: 'boolean', nullable: true },
|
|
39
|
+
version: {
|
|
40
|
+
type: 'string',
|
|
41
|
+
nullable: true,
|
|
42
|
+
},
|
|
43
|
+
secrets: {
|
|
44
|
+
type: 'object',
|
|
45
|
+
nullable: true,
|
|
46
|
+
patternProperties: {
|
|
47
|
+
[SECRET_KEY_REGEX.source]: {
|
|
48
|
+
anyOf: [{ type: 'string' }],
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
required: [],
|
|
52
|
+
},
|
|
53
|
+
dev: { type: 'boolean', nullable: true },
|
|
54
|
+
cname: { type: 'string', nullable: true },
|
|
55
|
+
notifyMaintenanceMode: { type: 'boolean', nullable: true },
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
required: ['id', 'fields'],
|
|
60
|
+
additionalProperties: false,
|
|
61
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import Ajv from 'ajv'
|
|
2
|
+
import { JsonObject } from 'type-fest'
|
|
3
|
+
|
|
4
|
+
export enum RestMethods {
|
|
5
|
+
Get = 'GET',
|
|
6
|
+
Post = 'POST',
|
|
7
|
+
Put = 'PUT',
|
|
8
|
+
Delete = 'DELETE',
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export enum RestCommands {
|
|
12
|
+
Instance = 'instance',
|
|
13
|
+
UserToken = 'userToken',
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type RestPayloadBase = JsonObject
|
|
17
|
+
|
|
18
|
+
export const ajv = new Ajv()
|
|
19
|
+
|
|
20
|
+
export * from './CreateInstance'
|
|
21
|
+
export * from './DeleteInstance'
|
|
22
|
+
export * from './GetUserTokenInfo'
|
|
23
|
+
export * from './UpdateInstance'
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { BaseFields } from './types'
|
|
2
|
+
|
|
3
|
+
export enum SubscriptionType {
|
|
4
|
+
Legacy = 'legacy',
|
|
5
|
+
Free = 'free',
|
|
6
|
+
Premium = 'premium',
|
|
7
|
+
Lifetime = 'lifetime',
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const PLAN_NAMES = {
|
|
11
|
+
[SubscriptionType.Legacy]: 'Legacy',
|
|
12
|
+
[SubscriptionType.Free]: 'Hacker',
|
|
13
|
+
[SubscriptionType.Premium]: `Pro`,
|
|
14
|
+
[SubscriptionType.Lifetime]: `Founder's Edition`,
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export type UserFields = BaseFields & {
|
|
18
|
+
email: string
|
|
19
|
+
verified: boolean
|
|
20
|
+
isLegacy: boolean
|
|
21
|
+
isFounder: boolean
|
|
22
|
+
subscription: SubscriptionType
|
|
23
|
+
notifyMaintenanceMode: boolean
|
|
24
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export type RecordId = string
|
|
2
|
+
export type UserId = RecordId
|
|
3
|
+
export type InstanceId = RecordId
|
|
4
|
+
export type Semver = string
|
|
5
|
+
export type InternalInstanceId = RecordId
|
|
6
|
+
export type Subdomain = string
|
|
7
|
+
export type Port = number
|
|
8
|
+
export type IsoDate = string
|
|
9
|
+
export type ProcessId = number
|
|
10
|
+
export type Username = string
|
|
11
|
+
export type Password = string
|
|
12
|
+
export type CollectionName = string
|
|
13
|
+
export type Seconds = number
|
|
14
|
+
export type BaseFields = {
|
|
15
|
+
id: RecordId
|
|
16
|
+
created: IsoDate
|
|
17
|
+
updated: IsoDate
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const pocketNow = () => new Date().toISOString()
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { forEach } from '@s-libs/micro-dash'
|
|
2
|
+
import { config } from 'dotenv'
|
|
3
|
+
import envPaths from 'env-paths'
|
|
4
|
+
import { dirname, join } from 'path'
|
|
5
|
+
import { cwd } from 'process'
|
|
6
|
+
import { fileURLToPath } from 'url'
|
|
7
|
+
import {
|
|
8
|
+
InstanceId,
|
|
9
|
+
mkBoolean,
|
|
10
|
+
mkCsvString,
|
|
11
|
+
mkNumber,
|
|
12
|
+
mkPath,
|
|
13
|
+
mkString,
|
|
14
|
+
} from '../core'
|
|
15
|
+
import { DEBUG, IS_DEV } from './common/debug'
|
|
16
|
+
import { Settings } from './core/Settings'
|
|
17
|
+
|
|
18
|
+
const __dirname = dirname(fileURLToPath(import.meta.url))
|
|
19
|
+
|
|
20
|
+
export const _PH_HOME = process.env.PH_HOME || envPaths(`pockethost`).data
|
|
21
|
+
|
|
22
|
+
config({ path: [join(_PH_HOME, '.env'), join(cwd(), `.env`)] })
|
|
23
|
+
|
|
24
|
+
export const _PH_PROJECT_DIR = join(__dirname, '..')
|
|
25
|
+
export const _APEX_DOMAIN = process.env.APEX_DOMAIN || 'pockethost.lvh.me'
|
|
26
|
+
export const _HTTP_PROTOCOL = process.env.HTTP_PROTOCOL || `https:`
|
|
27
|
+
|
|
28
|
+
export function isSettingKey(key: string): key is keyof typeof settings {
|
|
29
|
+
return key in settings
|
|
30
|
+
}
|
|
31
|
+
export const settings = Settings({
|
|
32
|
+
PH_PLUGINS: mkCsvString([
|
|
33
|
+
`@pockethost/plugin-console-logger`,
|
|
34
|
+
`@pockethost/plugin-launcher-spawn`,
|
|
35
|
+
]),
|
|
36
|
+
|
|
37
|
+
PH_HOME: mkPath(_PH_HOME),
|
|
38
|
+
PH_PROJECT_DIR: mkPath(_PH_PROJECT_DIR),
|
|
39
|
+
|
|
40
|
+
PH_APEX_DOMAIN: mkString(_APEX_DOMAIN),
|
|
41
|
+
|
|
42
|
+
PH_PORT: mkNumber(3000),
|
|
43
|
+
|
|
44
|
+
PH_DATA_DIR: mkPath(join(_PH_HOME, 'data'), { create: true }),
|
|
45
|
+
PH_DEV: mkBoolean(IS_DEV()),
|
|
46
|
+
PH_DEBUG: mkBoolean(DEBUG()),
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
/** Accessors */
|
|
50
|
+
export const PH_PLUGINS = () => settings.PH_PLUGINS
|
|
51
|
+
|
|
52
|
+
export const PH_HOME = (...paths: string[]) => join(settings.PH_HOME, ...paths)
|
|
53
|
+
export const PH_PROJECT_DIR = (...paths: string[]) =>
|
|
54
|
+
join(settings.PH_PROJECT_DIR, ...paths)
|
|
55
|
+
|
|
56
|
+
export const APEX_DOMAIN = () => settings.PH_APEX_DOMAIN
|
|
57
|
+
|
|
58
|
+
export const PORT = () => settings.PH_PORT
|
|
59
|
+
|
|
60
|
+
export const DATA_DIR = (...paths: string[]) =>
|
|
61
|
+
join(settings.PH_DATA_DIR, ...paths)
|
|
62
|
+
export const NODE_ENV = () => process.env.NODE_ENV
|
|
63
|
+
export const INSTANCE_DATA_DIR = (id: InstanceId, ...paths: string[]) =>
|
|
64
|
+
join(DATA_DIR(), id, ...paths)
|
|
65
|
+
|
|
66
|
+
export const logConstants = () => {
|
|
67
|
+
forEach(settings, (v, k) => {
|
|
68
|
+
console.log(`${k}: ${v}`)
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (DEBUG()) {
|
|
73
|
+
logConstants()
|
|
74
|
+
}
|