@chidchanun/bcp 0.2.6 → 0.2.8

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/README.md CHANGED
@@ -1,14 +1,14 @@
1
1
  # BCP Framework
2
2
 
3
- BCP Framework is a React full-stack framework for file-based routing, SSR, SPA navigation, server data loading, guarded application flows, API routes, authentication, authorization, database access, validation, uploads, storage and standalone Node.js production deployment.
3
+ BCP Framework is a React full-stack framework for file-based routing, SSR, SPA navigation, server data loading, guarded application flows, API routes, authentication, authorization, database access, background jobs, observability, validation, uploads, storage and standalone Node.js production deployment.
4
4
 
5
- > **Development target:** `0.2.6Authorization & Security v2`
5
+ > **Development target:** `0.2.8Background Jobs Platform`
6
6
  >
7
- > `0.2.6` is an unreleased development target until local validation, RC checks, tagging and npm publication complete.
7
+ > `0.2.8` is an unreleased development target until local validation, RC checks, tagging and npm publication complete.
8
8
 
9
9
  ## 0.2 platform
10
10
 
11
- `0.2.0` established the Framework Platform baseline, `0.2.1` added the Documentation Platform, `0.2.2` added Configuration & Environment v2, `0.2.3` added Database Platform v2, `0.2.4` added Application Packaging, `0.2.5` added Authentication Platform v2, and `0.2.6` adds permission/policy authorization plus CSRF and same-origin request protection without intentionally removing the existing public application model.
11
+ `0.2.0` established the Framework Platform baseline, `0.2.1` added the Documentation Platform, `0.2.2` added Configuration & Environment v2, `0.2.3` added Database Platform v2, `0.2.4` added Application Packaging, `0.2.5` added Authentication Platform v2, `0.2.6` added Authorization & Security v2, `0.2.7` added Observability Platform v2, and `0.2.8` adds a provider-neutral background-job queue/worker contract with delayed work, retry/backoff, cancellation and process-local reference storage.
12
12
 
13
13
  Machine-readable platform contracts:
14
14
 
@@ -35,10 +35,11 @@ docs/api-manifest.json
35
35
  | Authorization | Auth/guest/role/permission route guards, flat permissions and resource-aware policies |
36
36
  | Request security | Same-origin validation and signed CSRF tokens for unsafe mutations |
37
37
  | Middleware | Middleware System v2 with onion execution |
38
+ | Background jobs | Adapter contract, in-memory queue, delayed jobs, retries/backoff, cancellation and concurrent workers |
39
+ | Observability | Structured logs, counters/gauges/histograms, Prometheus output, request metrics and health/readiness checks |
38
40
  | Validation | Typed validators and structured validation errors |
39
41
  | Error handling | HTTP error helpers and consistent error responses |
40
42
  | Database | Provider-neutral MySQL, PostgreSQL and SQLite adapters, transactions, lifecycle and migrations |
41
- | Logging | Structured logger, request logger and request IDs |
42
43
  | Uploads | Buffered multipart helpers and production multipart streaming |
43
44
  | Storage | Local + S3-compatible storage, streaming, list/copy/move, metadata, bulk delete and signed URLs |
44
45
  | Caching | Response cache and revalidation primitives |
@@ -242,8 +243,6 @@ Read more:
242
243
 
243
244
  ## Authorization & Security v2 — 0.2.6
244
245
 
245
- ### Permissions
246
-
247
246
  Use flat permissions directly from `bcp/auth`:
248
247
 
249
248
  ```ts
@@ -268,24 +267,7 @@ await requirePermission(
268
267
  );
269
268
  ```
270
269
 
271
- Route trees can use a permission guard:
272
-
273
- ```ts
274
- import {
275
- createPermissionGuard,
276
- } from "bcp/auth";
277
-
278
- export const guard =
279
- createPermissionGuard(
280
- "admin.access"
281
- );
282
- ```
283
-
284
- The default permission field is `permissions`. Applications can select another field such as `scopes`.
285
-
286
- ### Resource policies
287
-
288
- For ownership, tenant or resource-state rules:
270
+ Resource-aware policies:
289
271
 
290
272
  ```ts
291
273
  import {
@@ -312,17 +294,6 @@ await authorize(
312
294
  );
313
295
  ```
314
296
 
315
- Available policy helpers:
316
-
317
- ```text
318
- can()
319
- cannot()
320
- authorize()
321
- AuthorizationError
322
- ```
323
-
324
- ### Same-origin and CSRF protection
325
-
326
297
  Request-security helpers are exposed from `bcp/server`:
327
298
 
328
299
  ```ts
@@ -333,43 +304,117 @@ import {
333
304
  } from "bcp/server";
334
305
  ```
335
306
 
336
- Protect an unsafe API/action mutation with origin validation:
307
+ Read more: [Authorization & Security v2](docs/authorization-security.md)
308
+
309
+ ## Observability Platform v2 — 0.2.7
310
+
311
+ Create one application metrics registry:
337
312
 
338
313
  ```ts
339
- await requireSameOriginRequest();
314
+ import {
315
+ createMetricsRegistry,
316
+ } from "bcp/observability";
317
+
318
+ export const metrics =
319
+ createMetricsRegistry();
320
+ ```
321
+
322
+ Supported metric types:
323
+
324
+ ```text
325
+ counter
326
+ gauge
327
+ histogram
340
328
  ```
341
329
 
342
- For signed CSRF protection:
330
+ Expose Prometheus-compatible text:
343
331
 
344
332
  ```ts
345
- const csrfToken =
346
- await createCsrfToken();
333
+ import {
334
+ createMetricsResponse,
335
+ } from "bcp/observability";
336
+
337
+ export function GET() {
338
+ return createMetricsResponse(
339
+ metrics
340
+ );
341
+ }
342
+ ```
347
343
 
348
- // Render/send csrfToken through the trusted application UI.
344
+ Health/readiness registry:
349
345
 
350
- await requireCsrfRequest({
351
- token: submittedToken,
352
- });
346
+ ```ts
347
+ import {
348
+ createHealthRegistry,
349
+ } from "bcp/observability";
350
+
351
+ export const health =
352
+ createHealthRegistry();
353
353
  ```
354
354
 
355
- Default CSRF header/cookie:
355
+ Read more: [Observability Platform v2](docs/observability.md)
356
356
 
357
- ```text
358
- X-BCP-CSRF
359
- bcp_csrf
357
+ ## Background Jobs Platform — 0.2.8
358
+
359
+ Create a server-side queue:
360
+
361
+ ```ts
362
+ import {
363
+ createJobQueue,
364
+ } from "bcp/jobs";
365
+
366
+ export const jobs =
367
+ createJobQueue();
360
368
  ```
361
369
 
362
- Secret resolution:
370
+ Register a handler:
363
371
 
364
- ```text
365
- explicit options.secret
366
-
367
- BCP_CSRF_SECRET
368
-
369
- BCP_SESSION_SECRET
372
+ ```ts
373
+ jobs.register<{
374
+ userId: number;
375
+ }>(
376
+ "email.welcome",
377
+ async ({ payload }) => {
378
+ await sendWelcomeEmail(
379
+ payload.userId
380
+ );
381
+ }
382
+ );
370
383
  ```
371
384
 
372
- Read more: [Authorization & Security v2](docs/authorization-security.md)
385
+ Enqueue immediately or with a delay:
386
+
387
+ ```ts
388
+ await jobs.enqueue(
389
+ "email.welcome",
390
+ {
391
+ userId: 42,
392
+ },
393
+ {
394
+ delayMs: 5_000,
395
+ maxAttempts: 5,
396
+ }
397
+ );
398
+ ```
399
+
400
+ Start concurrent workers:
401
+
402
+ ```ts
403
+ const worker =
404
+ jobs.startWorker({
405
+ concurrency: 4,
406
+ pollIntervalMs: 250,
407
+ });
408
+
409
+ // graceful shutdown
410
+ await worker.stop();
411
+ ```
412
+
413
+ The default in-memory adapter is process-local and is intended for development/tests/prototypes. Durable multi-process deployments should implement `JobQueueAdapter` against shared infrastructure. Adapter `reserve()` must atomically claim work.
414
+
415
+ Retry behavior is configurable with fixed or callback-based backoff. The default is capped exponential backoff. Jobs can also be cancelled and inspected with `get()` / `list()`.
416
+
417
+ Read more: [Background Jobs Platform](docs/background-jobs.md)
373
418
 
374
419
  ## Public entrypoints
375
420
 
@@ -384,6 +429,8 @@ bcp/validation
384
429
  bcp/error
385
430
  bcp/database
386
431
  bcp/auth
432
+ bcp/jobs
433
+ bcp/observability
387
434
  bcp/server
388
435
  bcp/server-only
389
436
  bcp/middleware
@@ -446,6 +493,9 @@ Loader / action / API route
446
493
  React SSR
447
494
 
448
495
  Hydration / SPA navigation
496
+
497
+ Operational side channels:
498
+ background jobs + structured logs + metrics + health/readiness
449
499
  ```
450
500
 
451
501
  ## Production build
@@ -491,7 +541,7 @@ npm run test:e2e
491
541
  npm run rc:check
492
542
  ```
493
543
 
494
- `0.2.6` adds Authorization & Security v2 unit and prepared-package smoke checks covering permission matching, policy authorization, permission route guards, same-origin mutation validation and signed CSRF tokens.
544
+ `0.2.8` adds Background Jobs Platform unit and prepared-package smoke checks covering delayed work, retries/backoff, cancellation, worker concurrency, server-only boundaries and the public `bcp/jobs` package surface.
495
545
 
496
546
  Do not tag or publish until the final release commit passes the complete RC sequence.
497
547
 
@@ -512,12 +562,14 @@ Do not tag or publish until the final release commit passes the complete RC sequ
512
562
  | `0.2.4` | Application Packaging |
513
563
  | `0.2.5` | Authentication Platform v2 |
514
564
  | `0.2.6` | Authorization & Security v2 |
565
+ | `0.2.7` | Observability Platform v2 |
566
+ | `0.2.8` | Background Jobs Platform |
515
567
 
516
568
  ## Roadmap
517
569
 
518
- `0.2.6Authorization & Security v2` establishes the permission/policy and browser-mutation security layer on top of Authentication Platform v2.
570
+ `0.2.8Background Jobs Platform` establishes a provider-neutral queue/worker contract on top of the existing server runtime and lifecycle model.
519
571
 
520
- The next `0.2.x` milestone can build on the existing runtime, database, auth, security and packaging contracts. Native `.exe`, desktop and mobile compilation remain later roadmap work.
572
+ Later `0.2.x` work can add durable queue providers, recurring schedules, distributed leases or workflow orchestration without changing the base enqueue/worker contract. Native `.exe`, desktop and mobile compilation remain later roadmap work.
521
573
 
522
574
  ## License
523
575
 
package/docs/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  The `docs/` directory is the documentation source of truth for BCP Framework and is organized for **`bcp-docs-web`**.
4
4
 
5
- > **Documentation target:** BCP Framework `0.2.6Authorization & Security v2`
5
+ > **Documentation target:** BCP Framework `0.2.8Background Jobs Platform`
6
6
  >
7
7
  > **Release state:** unreleased development target until RC validation, tagging and npm publication complete.
8
8
 
@@ -52,66 +52,51 @@ Framework source and tests remain authoritative for runtime behavior.
52
52
  | `0.2.4` | Application Packaging |
53
53
  | `0.2.5` | Authentication Platform v2 |
54
54
  | `0.2.6` | Authorization & Security v2 |
55
+ | `0.2.7` | Observability Platform v2 |
56
+ | `0.2.8` | Background Jobs Platform |
55
57
 
56
- ## 0.2.6Authorization & Security v2
58
+ ## 0.2.8Background Jobs Platform
57
59
 
58
- `0.2.6` adds server-side permission checks, resource-aware authorization policies, permission route guards, same-origin mutation validation and signed CSRF protection.
60
+ `0.2.8` adds a server-only provider-neutral queue/worker layer without adding a third-party queue dependency.
59
61
 
60
62
  New/updated documentation sources:
61
63
 
62
64
  | Source | Purpose |
63
65
  | --- | --- |
64
- | `authorization-security.md` | Permissions, policies, permission guards, same-origin validation and CSRF APIs |
65
- | `auth-route-guards.md` | Auth/guest/role/permission route guard guidance |
66
- | `api-reference.md` | Public authorization and request-security exports |
67
- | `platform-manifest.json` | Authorization/security capability flags |
68
- | `api-manifest.json` | `bcp/auth` and `bcp/server` guide ownership |
69
- | `releases/0.2.6.md` | Authorization & Security v2 release notes |
66
+ | `background-jobs.md` | Queue contract, memory adapter, delay, retries, workers and production guidance |
67
+ | `api-reference.md` | Public `bcp/jobs` exports |
68
+ | `platform-manifest.json` | Background Jobs capability flags and public entrypoint |
69
+ | `api-manifest.json` | `bcp/jobs` guide ownership |
70
+ | `docs-web-manifest.json` | Background Jobs navigation and `0.2.8` release route |
71
+ | `releases/0.2.8.md` | Background Jobs Platform release notes |
70
72
 
71
- Primary authorization APIs:
73
+ Primary APIs:
72
74
 
73
75
  ```ts
74
76
  import {
75
- authorize,
76
- can,
77
- createPermissionGuard,
78
- defineAuthorizationPolicy,
79
- hasPermission,
80
- requirePermission,
81
- } from "bcp/auth";
77
+ createJobQueue,
78
+ createMemoryJobQueueAdapter,
79
+ } from "bcp/jobs";
82
80
  ```
83
81
 
84
- Primary browser-mutation security APIs:
85
-
86
- ```ts
87
- import {
88
- createCsrfToken,
89
- requireCsrfRequest,
90
- requireSameOriginRequest,
91
- } from "bcp/server";
92
- ```
93
-
94
- ## Security model
95
-
96
- The `0.2.6` server model separates concerns:
82
+ Runtime model:
97
83
 
98
84
  ```text
99
- authentication
100
- -> identify a signed/revocable session
101
-
102
- authorization
103
- -> permissions + policies + route guards
104
-
105
- request security
106
- -> Origin/Referer validation + signed CSRF token
107
-
108
- input validation
109
- -> application schema/domain checks
85
+ web/API/action
86
+ |
87
+ | enqueue
88
+ v
89
+ JobQueueAdapter
90
+ |
91
+ v
92
+ worker loop(s)
93
+ |
94
+ +-> success
95
+ +-> retry/backoff
96
+ +-> failed/cancelled
110
97
  ```
111
98
 
112
- All authorization decisions must remain server-side. Client UI permission checks may improve presentation but do not replace route/action/API enforcement.
113
-
114
- CSRF tokens use `BCP_CSRF_SECRET` when configured and otherwise fall back to `BCP_SESSION_SECRET`. Security secrets must contain at least 32 UTF-8 bytes.
99
+ The built-in memory adapter is process-local and not durable. Multi-process/container production deployments should implement `JobQueueAdapter` against shared durable infrastructure.
115
100
 
116
101
  ## Update rule
117
102
 
@@ -150,13 +135,14 @@ Important current routes:
150
135
  | Website route | Markdown source |
151
136
  | --- | --- |
152
137
  | `/docs/authentication` | `authentication.md` |
153
- | `/docs/auth-session-store` | `auth-session-store.md` |
154
- | `/docs/auth-route-guards` | `auth-route-guards.md` |
155
138
  | `/docs/authorization-security` | `authorization-security.md` |
139
+ | `/docs/observability` | `observability.md` |
140
+ | `/docs/background-jobs` | `background-jobs.md` |
141
+ | `/docs/development-logging` | `development-logging.md` |
156
142
  | `/docs/application-packaging` | `application-packaging.md` |
157
143
  | `/docs/database` | `database.md` |
158
144
  | `/docs/api-reference` | `api-reference.md` |
159
- | `/releases/0.2.6` | `releases/0.2.6.md` |
145
+ | `/releases/0.2.8` | `releases/0.2.8.md` |
160
146
 
161
147
  Every route/source pair is validated by unit tests.
162
148
 
@@ -196,6 +182,8 @@ bcp/validation
196
182
  bcp/error
197
183
  bcp/database
198
184
  bcp/auth
185
+ bcp/jobs
186
+ bcp/observability
199
187
  bcp/server
200
188
  bcp/server-only
201
189
  bcp/middleware
@@ -235,7 +223,7 @@ synchronize CMS/search/navigation
235
223
 
236
224
  ## Release validation
237
225
 
238
- Before publishing `0.2.6`:
226
+ Before publishing `0.2.8`:
239
227
 
240
228
  ```bash
241
229
  npm run typecheck
@@ -246,16 +234,15 @@ npm run test:e2e
246
234
  npm run rc:check
247
235
  ```
248
236
 
249
- Authorization & Security v2 validation covers:
237
+ Background Jobs Platform validation covers:
250
238
 
251
- - permission normalization and any/all matching,
252
- - permission route guards,
253
- - sync/async authorization policies,
254
- - `AuthorizationError` denial behavior,
255
- - same-origin mutation checks,
256
- - signed CSRF tokens,
257
- - malformed/cross-origin rejection,
258
- - public `bcp/auth` and `bcp/server` exports,
239
+ - immediate and delayed jobs,
240
+ - retry/backoff behavior,
241
+ - terminal success/failure/cancellation states,
242
+ - duplicate job IDs,
243
+ - worker concurrency and graceful stop,
244
+ - public `bcp/jobs` exports,
245
+ - server-only browser/client boundaries,
259
246
  - prepared npm package contents,
260
247
  - docs/platform/API version parity.
261
248
 
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "schemaVersion": 1,
3
3
  "framework": "bcp",
4
- "version": "0.2.6",
4
+ "version": "0.2.8",
5
5
  "releaseState": "unreleased",
6
6
  "coverage": "public-entrypoints",
7
7
  "entrypoints": [
@@ -94,6 +94,28 @@
94
94
  "/docs/session-auth"
95
95
  ]
96
96
  },
97
+ {
98
+ "package": "bcp/jobs",
99
+ "source": "packages/client/src/jobs.ts",
100
+ "environment": "server",
101
+ "route": "/docs/api-reference#bcp-jobs",
102
+ "summary": "Background job queue contract with in-memory adapter, delayed jobs, retry/backoff, cancellation and concurrent workers.",
103
+ "guides": [
104
+ "/docs/background-jobs",
105
+ "/docs/observability"
106
+ ]
107
+ },
108
+ {
109
+ "package": "bcp/observability",
110
+ "source": "packages/client/src/observability.ts",
111
+ "environment": "server",
112
+ "route": "/docs/api-reference#bcp-observability",
113
+ "summary": "In-process metrics, Prometheus exposition, request metrics middleware and health/readiness checks.",
114
+ "guides": [
115
+ "/docs/observability",
116
+ "/docs/development-logging"
117
+ ]
118
+ },
97
119
  {
98
120
  "package": "bcp/server",
99
121
  "source": "packages/client/src/server.ts",
@@ -226,6 +226,73 @@ Authorization & Security v2 adds permission checks, permission route guards and
226
226
 
227
227
  Related guides: [Authentication](authentication.md), [Auth Session Stores](auth-session-store.md), [Auth Route Guards](auth-route-guards.md), [Authorization & Security v2](authorization-security.md), [JWT Sessions](session-auth.md).
228
228
 
229
+ ## `bcp/jobs`
230
+
231
+ Server-only Background Jobs Platform APIs.
232
+
233
+ ```ts
234
+ import {
235
+ createJobQueue,
236
+ createMemoryJobQueueAdapter,
237
+ type BackgroundJobQueue,
238
+ type EnqueueJobOptions,
239
+ type JobHandler,
240
+ type JobHandlerContext,
241
+ type JobQueueAdapter,
242
+ type JobQueueOptions,
243
+ type JobRecord,
244
+ type JobRetryDelay,
245
+ type JobState,
246
+ type JobWorker,
247
+ type MemoryJobQueueAdapter,
248
+ type StartJobWorkerOptions,
249
+ } from "bcp/jobs";
250
+ ```
251
+
252
+ `createJobQueue()` provides delayed enqueueing, retry/backoff, cancellation, manual `processNext()` execution and concurrent workers. The default adapter is process-local memory storage and is intended for development, tests and single-process prototypes.
253
+
254
+ Production applications that require durable processing should implement `JobQueueAdapter` against shared infrastructure. Adapter `reserve()` must atomically claim one eligible queued job so multiple workers cannot process the same reservation concurrently.
255
+
256
+ The queue contract is designed for practical at-least-once processing; handlers should be idempotent when duplicate side effects are unsafe.
257
+
258
+ Related guides: [Background Jobs Platform](background-jobs.md), [Observability Platform v2](observability.md).
259
+
260
+ ## `bcp/observability`
261
+
262
+ Server-only Observability Platform v2 APIs.
263
+
264
+ ```ts
265
+ import {
266
+ createHealthRegistry,
267
+ createMetricsRegistry,
268
+ createMetricsResponse,
269
+ createRequestMetricsMiddleware,
270
+ type CounterMetric,
271
+ type GaugeMetric,
272
+ type HealthCheck,
273
+ type HealthCheckOptions,
274
+ type HealthCheckReportItem,
275
+ type HealthCheckResult,
276
+ type HealthRegistry,
277
+ type HealthReport,
278
+ type HistogramMetric,
279
+ type HistogramOptions,
280
+ type MetricDefinitionOptions,
281
+ type MetricLabels,
282
+ type MetricLabelValue,
283
+ type MetricsRegistry,
284
+ type RequestMetricsOptions,
285
+ } from "bcp/observability";
286
+ ```
287
+
288
+ `createMetricsRegistry()` provides process-local counters, gauges and histograms. `createMetricsResponse()` renders Prometheus-compatible text exposition. `createRequestMetricsMiddleware()` measures HTTP request counts and duration using bounded `method` / `status` labels by default and intentionally does not label by raw path.
289
+
290
+ `createHealthRegistry()` registers synchronous or asynchronous liveness/readiness checks, applies per-check timeouts and produces JSON health responses with HTTP `200` when all checks pass or `503` when any check fails.
291
+
292
+ The built-in registry is process-local. Multi-instance deployments should scrape each process/container or aggregate through external monitoring infrastructure.
293
+
294
+ Related guides: [Observability Platform v2](observability.md), [Logging](development-logging.md).
295
+
229
296
  ## `bcp/server`
230
297
 
231
298
  Server request/runtime APIs.