@almadar/orb 16.9.1 → 17.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 (41) hide show
  1. package/README.md +5 -0
  2. package/package.json +9 -6
  3. package/scripts/postinstall.js +38 -0
  4. package/shells/almadar-shell/package.json +17 -3
  5. package/shells/almadar-shell/packages/client/package.json +7 -7
  6. package/shells/almadar-shell/packages/client/src/App.tsx +23 -2
  7. package/shells/almadar-shell/packages/client/src/config/firebase.ts +23 -2
  8. package/shells/almadar-shell/packages/client/src/config/mockAuth.ts +257 -0
  9. package/shells/almadar-shell/packages/client/src/features/auth/AuthContext.tsx +22 -9
  10. package/shells/almadar-shell/packages/client/src/features/auth/authService.ts +28 -7
  11. package/shells/almadar-shell/packages/client/src/features/auth/components/PersonaSwitcher.tsx +50 -0
  12. package/shells/almadar-shell/packages/client/src/features/auth/components/ProtectedRoute.tsx +9 -0
  13. package/shells/almadar-shell/packages/client/src/features/auth/index.ts +2 -1
  14. package/shells/almadar-shell/packages/client/src/features/auth/types.ts +18 -2
  15. package/shells/almadar-shell/packages/server/package.json +9 -7
  16. package/shells/almadar-shell/packages/server/pnpm-lock.yaml +4723 -0
  17. package/shells/almadar-shell/packages/server/src/app.ts +58 -3
  18. package/shells/almadar-shell/packages/server/src/hooks-providers.ts +11 -0
  19. package/shells/almadar-shell/packages/server/src/index.ts +41 -2
  20. package/shells/almadar-shell/packages/server/src/services/clients.ts +17 -0
  21. package/shells/almadar-shell/packages/server/src/sse.ts +20 -0
  22. package/shells/almadar-shell/packages/server/src/types/express.d.ts +2 -8
  23. package/shells/almadar-shell/packages/server/tsconfig.json +0 -1
  24. package/shells/almadar-shell/packages/shared/package.json +1 -1
  25. package/shells/almadar-shell/pnpm-lock.yaml +3963 -1119
  26. package/shells/almadar-shell-hono/package.json +12 -6
  27. package/shells/almadar-shell-hono/packages/client/package.json +6 -6
  28. package/shells/almadar-shell-hono/packages/client/src/App.tsx +1 -5
  29. package/shells/almadar-shell-hono/packages/client/src/config/firebase.ts +23 -2
  30. package/shells/almadar-shell-hono/packages/client/src/features/auth/AuthContext.tsx +11 -6
  31. package/shells/almadar-shell-hono/packages/client/src/features/auth/authService.ts +10 -7
  32. package/shells/almadar-shell-hono/packages/client/src/features/auth/components/ProtectedRoute.tsx +9 -0
  33. package/shells/almadar-shell-hono/packages/server/package.json +5 -5
  34. package/shells/almadar-shell-hono/packages/server/src/app.ts +39 -0
  35. package/shells/almadar-shell-hono/packages/server/src/hooks-providers.ts +11 -0
  36. package/shells/almadar-shell-hono/packages/server/src/index.ts +41 -2
  37. package/shells/almadar-shell-hono/packages/server/src/serve.ts +4 -2
  38. package/shells/almadar-shell-hono/packages/server/src/services/clients.ts +17 -0
  39. package/shells/almadar-shell-hono/packages/server/src/sse.ts +20 -0
  40. package/shells/almadar-shell-hono/packages/server/tsconfig.json +0 -1
  41. package/shells/almadar-shell-hono/pnpm-lock.yaml +4370 -1238
@@ -4,21 +4,39 @@
4
4
 
5
5
  import express, { type Express } from 'express';
6
6
  import cors from 'cors';
7
+ import helmet from 'helmet';
8
+ import rateLimit from 'express-rate-limit';
7
9
  import {
8
10
  env,
9
11
  logger,
10
12
  errorHandler,
11
13
  notFoundHandler,
12
14
  debugEventsRouter,
15
+ personasRouter,
16
+ pushRouter,
17
+ pushServiceWorkerHandler,
18
+ PUSH_SERVICE_WORKER_PATH,
19
+ reportsRouter,
20
+ createHooksRouter,
13
21
  } from '@almadar/server';
22
+ import { hookProviders } from './hooks-providers.js';
23
+ import { broadcastBusEvent } from './sse.js';
14
24
  import { registerRoutes } from './routes.js';
15
25
 
16
26
  export const app: Express = express();
17
27
 
18
28
  // Middleware
19
- app.use(cors({ origin: true, credentials: true }));
20
- app.use(express.json());
21
- app.use(express.urlencoded({ extended: true }));
29
+ app.use(helmet());
30
+ // CORS: env-driven allowlist (CORS_ORIGIN) — never reflect arbitrary origins with credentials.
31
+ app.use(cors({ origin: env.CORS_ORIGIN, credentials: true }));
32
+ app.use(express.json({ limit: '1mb' }));
33
+ app.use(express.urlencoded({ extended: true, limit: '1mb' }));
34
+ // Rate limiting protects deployed apps; verification walkers and local dev
35
+ // fire hundreds of bridge events per minute and must not be throttled —
36
+ // ORB_DISABLE_RATE_LIMIT=1 (set by the verify harness) bypasses it.
37
+ if (process.env.ORB_DISABLE_RATE_LIMIT !== '1') {
38
+ app.use('/api', rateLimit({ windowMs: 15 * 60 * 1000, max: 300, standardHeaders: true, legacyHeaders: false }));
39
+ }
22
40
 
23
41
  // Health check
24
42
  app.get('/health', (_req, res) => {
@@ -28,9 +46,46 @@ app.get('/health', (_req, res) => {
28
46
  // Debug event bus endpoints (dev-only, no-op in production)
29
47
  app.use('/api/debug', debugEventsRouter());
30
48
 
49
+ // Web Push surface (browser/push-subscribe): public VAPID key + the shared
50
+ // service worker (root-scoped — a SW's max scope is its own directory).
51
+ app.use('/api/push', pushRouter);
52
+ app.get(PUSH_SERVICE_WORKER_PATH, pushServiceWorkerHandler);
53
+
54
+ // Inbound webhook ingress (I-19 decision): mounted BEFORE the authenticated
55
+ // /api routes — hook senders (Google Calendar watch channels, e-sign status,
56
+ // banking callbacks) cannot present a Firebase token, so each provider
57
+ // VERIFIES its own signature/channel token and unverified requests get 400.
58
+ // Providers are DERIVED from this app's invoked services (generated
59
+ // hooks-providers.ts, I-26). Dispatch = SSE bus broadcast to every connected
60
+ // client, so client circuits' `listens` fire; cron pull stays the backstop.
61
+ app.use(
62
+ '/api/hooks',
63
+ createHooksRouter({
64
+ providers: hookProviders(),
65
+ dispatch: (event, payload) => {
66
+ broadcastBusEvent(undefined, {
67
+ type: 'bus',
68
+ event,
69
+ payload,
70
+ source: { orbital: 'webhook', trait: 'ingress' },
71
+ timestamp: Date.now(),
72
+ });
73
+ },
74
+ }),
75
+ );
76
+
77
+ // Dev persona roster (no-op unless ALLOW_DEV_AUTH_BYPASS). Mounted BEFORE
78
+ // registerRoutes, which applies authenticateFirebase to /api — a pre-login
79
+ // persona picker cannot present a token it does not have yet.
80
+ app.use('/api', personasRouter());
81
+
31
82
  // Register generated routes
32
83
  registerRoutes(app);
33
84
 
85
+ // Server-side report export (Excel/PDF/CSV). Mounted AFTER registerRoutes so
86
+ // the /api auth middleware it registers covers this route too.
87
+ app.use('/api/reports', reportsRouter);
88
+
34
89
  // Error handling
35
90
  app.use(notFoundHandler);
36
91
  app.use(errorHandler);
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Hook Providers
3
+ *
4
+ * Checked-in stub so the template builds standalone in CI; the compiler
5
+ * overwrites it with the provider map derived from the app's invoked
6
+ * services × the registry's hook declarations (I-26).
7
+ */
8
+
9
+ export function hookProviders() {
10
+ return {};
11
+ }
@@ -4,14 +4,53 @@
4
4
 
5
5
  import { initializeFirebase, env, logger } from '@almadar/server';
6
6
 
7
- // Initialize Firebase before anything else uses it
8
- initializeFirebase();
7
+ // Initialize Firebase before anything else uses it. When no credentials
8
+ // are configured (local dev without FIREBASE_*/FIRESTORE_EMULATOR_HOST),
9
+ // boot anyway — a purely client-side app must still serve.
10
+ try {
11
+ initializeFirebase();
12
+ } catch (e) {
13
+ const message = e instanceof Error ? e.message : String(e);
14
+ // In production the real data source IS Firebase (env.ts refuses to boot with
15
+ // USE_MOCK_DATA=true there), so swallowing this leaves a server that starts
16
+ // cleanly and then throws on the first request. Fail at boot instead.
17
+ if (env.NODE_ENV === 'production') {
18
+ logger.error(`Firebase is required when NODE_ENV=production — refusing to start: ${message}`);
19
+ throw e;
20
+ }
21
+ logger.warn(`Firebase not configured — auth/db routes disabled: ${message}`);
22
+ }
9
23
 
24
+ import { validateIntegrationEnv, FirestoreCredentialPersistence } from '@almadar/server';
10
25
  import { app } from './app.js';
26
+ import { invokedServices, installTenantCredentialStore } from './services/clients.js';
27
+
28
+ // Fail fast in production when a required integration credential is missing
29
+ // (see SECRETS.md); registers the invoked services for /health reporting.
30
+ validateIntegrationEnv(invokedServices);
11
31
 
12
32
  const PORT = env.PORT || 3030;
13
33
 
14
34
  async function start(): Promise<void> {
35
+ // W4 tenant credential store: with a master key configured, stored
36
+ // credentials (Firestore rows, AES-256-GCM) layer over the env —
37
+ // store → env → unconfigured — and go live on change without restart.
38
+ if (process.env.ALMADAR_CREDENTIAL_MASTER_KEY) {
39
+ try {
40
+ await installTenantCredentialStore(new FirestoreCredentialPersistence());
41
+ logger.info('Tenant credential store installed');
42
+ } catch (e) {
43
+ const message = e instanceof Error ? e.message : String(e);
44
+ // A configured master key IS the deployment's statement that the store
45
+ // is in use — failing to serve it in production is a boot failure.
46
+ if (env.NODE_ENV === 'production') {
47
+ logger.error(`Tenant credential store failed to initialize — refusing to start: ${message}`);
48
+ throw e;
49
+ }
50
+ logger.warn(`Tenant credential store unavailable — falling back to env credentials: ${message}`);
51
+ }
52
+ }
53
+
15
54
  // Seed mock data when USE_MOCK_DATA is enabled
16
55
  if (env.USE_MOCK_DATA) {
17
56
  try {
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Service Clients — placeholder
3
+ *
4
+ * Compiler generates the real service clients here (delegating to
5
+ * @almadar/integrations; see orbital-shell-typescript codegen/service.rs).
6
+ * This stub only lets the template build standalone; an emitted app
7
+ * overwrites it wholesale.
8
+ */
9
+
10
+ export const declaredServices = [] as const;
11
+
12
+ export const invokedServices = [] as const;
13
+
14
+ /** Overwritten at emit time with the RuntimeIntegrationManager-backed store install. */
15
+ export async function installTenantCredentialStore(_adapter: object): Promise<void> {
16
+ // {{GENERATED_SERVICE_CLIENTS}}
17
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Live-push SSE channel — placeholder
3
+ *
4
+ * Compiler generates the real SSE module here (connection registry +
5
+ * cross-client broadcast; see orbital-shell-typescript backend/server/sse.rs).
6
+ * This stub only lets the template build standalone; an emitted app
7
+ * overwrites it wholesale.
8
+ */
9
+
10
+ export interface BusBroadcastItem {
11
+ type: 'bus';
12
+ event: string;
13
+ payload?: object;
14
+ source: { orbital: string; trait: string };
15
+ timestamp: number;
16
+ }
17
+
18
+ export function broadcastBusEvent(_originClientId: string | undefined, _item: BusBroadcastItem): void {
19
+ // {{GENERATED_SSE_BROADCAST}}
20
+ }
@@ -1,15 +1,9 @@
1
- import type { EventPayloadValue } from '@almadar/core';
1
+ import type { RawUserClaims } from '@almadar/core';
2
2
 
3
3
  declare global {
4
4
  namespace Express {
5
5
  interface Request {
6
- firebaseUser?: {
7
- uid: string;
8
- email?: string;
9
- name?: string;
10
- picture?: string;
11
- [key: string]: EventPayloadValue;
12
- };
6
+ firebaseUser?: RawUserClaims & { uid: string };
13
7
  }
14
8
  }
15
9
  }
@@ -5,7 +5,6 @@
5
5
  "moduleResolution": "bundler",
6
6
  "outDir": "./dist",
7
7
  "strict": true,
8
- "strictNullChecks": false,
9
8
  "esModuleInterop": true,
10
9
  "skipLibCheck": true,
11
10
  "forceConsistentCasingInFileNames": true,
@@ -5,7 +5,7 @@
5
5
  "main": "src/index.ts",
6
6
  "types": "src/index.ts",
7
7
  "dependencies": {
8
- "@almadar/core": "^7.14.3",
8
+ "@almadar/core": "^10.99.0",
9
9
  "zod": "^3.22.0"
10
10
  }
11
11
  }