-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathextensions.ts
More file actions
407 lines (384 loc) · 14.5 KB
/
Copy pathextensions.ts
File metadata and controls
407 lines (384 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
* Copyright (C) 2024-present Puter Technologies Inc.
*
* This file is part of Puter.
*
* Puter is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import type { RequestHandler } from 'express';
import type { puterClients } from './clients';
import {
EventListener,
EventMap,
EventMetadata,
ListenKey,
MatchingEvents,
} from './clients/event/types';
import type {
IExtensionClientInstances,
IPuterClientRegistry,
} from './clients/types';
import type { puterControllers } from './controllers';
import type {
IExtensionControllerInstances,
IPuterControllerRegistry,
} from './controllers/types';
import type {
RouteDescriptor,
RouteMethod,
RouteOptions,
RoutePath,
} from './core/http/types';
import type { puterDrivers } from './drivers';
import type {
IExtensionDriverInstances,
IPuterDriverRegistry,
} from './drivers/types';
import {
clientsContainers,
configContainer,
controllersContainers,
driversContainers,
servicesContainers,
storesContainers,
} from './exports';
import type { puterServices } from './services';
import type {
IExtensionServiceInstances,
IPuterServiceRegistry,
} from './services/types';
import type { puterStores } from './stores';
import type {
IExtensionStoreInstances,
IPuterStoreRegistry,
} from './stores/types';
import type { IConfig, LayerInstances } from './types';
/**
* The in-memory registry an extension's module-scope code writes into, and that
* `PuterServer` drains during boot. Every field is optional at write time — an
* extension that only needs routes never touches the registries.
*/
export const extensionStore = {
clients: {} as IPuterClientRegistry,
stores: {} as IPuterStoreRegistry,
services: {} as IPuterServiceRegistry,
controllers: {} as IPuterControllerRegistry,
drivers: {} as IPuterDriverRegistry,
globalMiddlewares: [] as RequestHandler[],
events: {} as Record<string, EventListener[]>,
/**
* Extension-declared routes. Shape matches the controller-layer
* `RouteDescriptor`, so both flow through the same materializer
* (`PuterServer#materializeRoute`) and inherit the same options →
* middleware translation (subdomain, auth, body parsers, ...).
*/
routeHandlers: [] as RouteDescriptor[],
};
/**
* Internal: normalize `(path, handler)` or `(path, options, handler)` into a
* single `RouteDescriptor` the server can materialize.
*/
const pushRoute = (
method: RouteMethod,
path: RoutePath,
optionsOrHandler: RouteOptions | RequestHandler,
maybeHandler?: RequestHandler,
): void => {
const handler =
typeof optionsOrHandler === 'function'
? optionsOrHandler
: maybeHandler;
const options =
typeof optionsOrHandler === 'function' ? {} : optionsOrHandler;
if (!handler) {
throw new Error(
`extension.${method}('${String(path)}', ...) missing handler`,
);
}
extensionStore.routeHandlers.push({ method, path, options, handler });
};
interface ExtensionRouteFn {
(path: RoutePath, handler: RequestHandler): void;
(path: RoutePath, options: RouteOptions, handler: RequestHandler): void;
}
const makeRouteFn = (method: RouteMethod): ExtensionRouteFn => {
return ((
path: RoutePath,
optionsOrHandler: RouteOptions | RequestHandler,
maybeHandler?: RequestHandler,
) => {
pushRoute(method, path, optionsOrHandler, maybeHandler);
}) as ExtensionRouteFn;
};
/**
* `extension.use` mirrors `app.use` and supports three shapes: use(handler)
* use(options, handler) use(path, handler) use(path, options, handler) Pathless
* calls register global middleware — the server materializer drops the path
* when calling `app.use` (see `RouteDescriptor.path?`).
*/
interface ExtensionUseFn {
(handler: RequestHandler): void;
(options: RouteOptions, handler: RequestHandler): void;
(path: RoutePath, handler: RequestHandler): void;
(path: RoutePath, options: RouteOptions, handler: RequestHandler): void;
}
const isRequestHandler = (v: unknown): v is RequestHandler =>
typeof v === 'function';
const isRoutePath = (v: unknown): v is RoutePath =>
typeof v === 'string' || v instanceof RegExp || Array.isArray(v);
const makeUseFn = (): ExtensionUseFn => {
return ((
a: RoutePath | RouteOptions | RequestHandler,
b?: RouteOptions | RequestHandler,
c?: RequestHandler,
): void => {
let path: RoutePath | undefined;
let options: RouteOptions = {};
let handler: RequestHandler | undefined;
if (isRoutePath(a)) {
path = a;
if (isRequestHandler(b)) {
handler = b;
} else {
options = (b as RouteOptions) ?? {};
handler = c;
}
} else if (isRequestHandler(a)) {
handler = a;
} else {
options = (a as RouteOptions) ?? {};
handler = isRequestHandler(b) ? b : undefined;
}
if (!handler) {
throw new Error('extension.use(...) missing handler');
}
extensionStore.routeHandlers.push({
method: 'use',
...(path !== undefined ? { path } : {}),
options,
handler,
});
}) as ExtensionUseFn;
};
/**
* Global `extension` API available inside every dynamically-loaded extension
* module. Exposes:
*
* - Registry writers: `registerClient`, `registerStore`, `registerService`,
* `registerController`, `registerDriver`.
* - Event subscription: `on(event, handler)`.
* - Imperative route registration: `get`, `post`, `put`, `delete`, `patch`,
* `head`, `options`, `all`, `use`. Each accepts the same `RouteOptions`
* vocabulary used by controllers (subdomain, requireAuth, bodyJson, …) so
* extension routes get identical gate + parser treatment.
* - Back-reference lookup: `import('service:foo')` / `'client:bar'` /
* `'store:baz'` / `'controller:qux'` / `'driver:fred'` — returns a lazy proxy
* to the registered instance (thrown on use-before-init).
*/
/**
* Wrap a resolved layer instance so that pulling a method off the import comes
* out _bound_ to the instance. Extensions routinely grab a method as a bare
* reference — `const { write } = extension.import('service').fs` or `const w =
* svc.fs.write` — then call it detached; without binding, `this` is `undefined`
* and the method's private-field access throws on the first line. Getters keep
* the real instance as their receiver, so private-field reads inside accessors
* still resolve. Only `get` is trapped; writes, `in`, and descriptor reads fall
* through to the instance unchanged.
*
* Trade-off: each method access returns a fresh bound function, so reference
* identity is not stable (`svc.fs.write !== svc.fs.write`). That's acceptable
* for the import surface, where instances are grabbed once and methods called.
*/
const bindLayerMethods = <T>(instance: T): T => {
if (instance === null || typeof instance !== 'object') {
return instance;
}
return new Proxy(instance as object, {
get(target, prop) {
const value = Reflect.get(target, prop, target);
return typeof value === 'function'
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
(value as (...a: any[]) => unknown).bind(target)
: value;
},
}) as T;
};
/**
* Lazy lookup proxy over one layer's instance container.
*
* Extension modules are imported before the layers are constructed, so a name
* read at module scope is normally still absent. Both levels therefore resolve
* against the container on _every_ access: `extension.import('client').db`
* captured at import time keeps working once the real client lands, which is
* the whole point of the proxy.
*
* A name that never gets registered throws on first property read rather than
* returning `undefined`, so a typo surfaces at the access site instead of
* silently becoming a no-op. Callers probing for an optional layer entry must
* do so inside a try/catch.
*/
const makeLayerImportProxy = (
layer: string,
containers: Record<string, unknown>,
): object =>
new Proxy(
{},
{
get: (_target: object, prop: string) => {
const instance = containers[prop];
if (instance) return bindLayerMethods(instance);
return new Proxy(
{},
{
get: (_target2: object, prop2: string) => {
const late = containers[prop];
if (!late) {
throw new Error(
`extension.import('${layer}:${prop}') missing property '${String(prop2)}'`,
);
}
return bindLayerMethods(late)[
prop2 as keyof typeof late
];
},
},
);
},
},
);
export const extension = {
// -- Config access -----------------------------------------------
//
// Lazy proxy to the server config. Populated by PuterServer during
// boot, so extensions can read it at request time (not import time).
get config(): IConfig {
return configContainer;
},
// -- Event subscription -------------------------------------------
on: <P extends ListenKey>(
key: P,
callback: (
key: MatchingEvents<P>,
data: EventMap[MatchingEvents<P>],
meta: EventMetadata,
) => Promise<void> | void,
) => {
if (!extensionStore.events[key]) {
extensionStore.events[key] = [];
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
extensionStore.events[key].push(callback as any);
},
// -- Registry writers ---------------------------------------------
registerClient: (
name: string,
client: IPuterClientRegistry[keyof IPuterClientRegistry],
) => {
extensionStore.clients[name] = client;
},
registerStore: (
name: string,
store: IPuterStoreRegistry[keyof IPuterStoreRegistry],
) => {
extensionStore.stores[name] = store;
},
registerService: (
name: string,
service: IPuterServiceRegistry[keyof IPuterServiceRegistry],
) => {
extensionStore.services[name] = service;
},
registerController: (
name: string,
controller: IPuterControllerRegistry[keyof IPuterControllerRegistry],
) => {
extensionStore.controllers[name] = controller;
},
registerDriver: (
name: string,
driver: IPuterDriverRegistry[keyof IPuterDriverRegistry],
) => {
extensionStore.drivers[name] = driver;
},
registerGlobalMiddleware: (middleware: RequestHandler) => {
extensionStore.globalMiddlewares.push(middleware);
},
// -- Route registration -------------------------------------------
//
// Supports two call shapes per verb:
// extension.get('/path', handler)
// extension.get('/path', options, handler)
//
// The `options` object is the same `RouteOptions` shape controllers use,
// so everything that works on a controller route (subdomain, requireAuth,
// requireUserActor, adminOnly, allowedAppIds, middleware, bodyJson,
// bodyRaw, bodyText, bodyUrlencoded) works here identically.
get: makeRouteFn('get'),
post: makeRouteFn('post'),
put: makeRouteFn('put'),
delete: makeRouteFn('delete'),
patch: makeRouteFn('patch'),
head: makeRouteFn('head'),
options: makeRouteFn('options'),
all: makeRouteFn('all'),
use: makeUseFn(),
// -- Import proxy -------------------------------------------------
import: <S extends string>(
name: S,
): S extends 'client' | 'clients'
? LayerInstances<typeof puterClients> & IExtensionClientInstances
: S extends 'store' | 'stores'
? LayerInstances<typeof puterStores> & IExtensionStoreInstances
: S extends 'service' | 'services'
? LayerInstances<typeof puterServices> & IExtensionServiceInstances
: S extends 'controller' | 'controllers'
? LayerInstances<typeof puterControllers> &
IExtensionControllerInstances
: S extends 'driver' | 'drivers'
? LayerInstances<typeof puterDrivers> &
IExtensionDriverInstances
: never => {
switch (name) {
case 'clients':
case 'client':
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return makeLayerImportProxy('client', clientsContainers) as any;
case 'stores':
case 'store':
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return makeLayerImportProxy('store', storesContainers) as any;
case 'services':
case 'service':
return makeLayerImportProxy(
'service',
servicesContainers,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
) as any;
case 'controllers':
case 'controller':
return makeLayerImportProxy(
'controller',
controllersContainers,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
) as any;
case 'drivers':
case 'driver':
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return makeLayerImportProxy('driver', driversContainers) as any;
default:
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return undefined as any;
}
},
};