@owlmeans/server-job 0.1.20-rc.1 → 0.2.0-alpha.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/README.md CHANGED
@@ -1,15 +1,62 @@
1
1
  # @owlmeans/server-job
2
2
 
3
- Server bridge from technical queue records to the sanitized `@owlmeans/job` domain contract.
4
- Every binding requires an application-owned `JobExposurePolicy`; there is no permissive default.
3
+ Server bridge from technical `@owlmeans/queue` records to the sanitized `@owlmeans/job` domain
4
+ contract. Every binding requires an application-owned `JobExposurePolicy`; there is no permissive
5
+ default. A server app uses it when long-running queue work must report status and progress to the
6
+ user's screen: one `serveJobEntrypoints` call binds list, get, cancel and watch handlers to a job
7
+ entrypoint group. It does not declare those routes (that is `@owlmeans/job`), does not run or store
8
+ jobs (that is `@owlmeans/queue` with a driver such as `@owlmeans/redis-queue`) and does not hold job
9
+ state in the browser (that is `@owlmeans/client-job`).
10
+
11
+ ## Installation
12
+
13
+ ```bash
14
+ bun add @owlmeans/server-job@^0.2.0-alpha.0
15
+ ```
16
+
17
+ ## Concepts
18
+
19
+ - **The policy is the security boundary** — `JobExposurePolicy` is required on every binding; there
20
+ is no default owner field, administrator bypass or raw-record response. Its parts:
21
+ - `audience` derives the authenticated application scope from the request — never a body owner id.
22
+ - `where` translates the closed public query into scoped technical criteria that always include
23
+ that scope.
24
+ - `lookup` resolves an opaque public id inside that scope.
25
+ - `map` allowlists fields and returns only `JobView`; `jobViewHelper.jobViewOf` bounds and
26
+ sanitizes JSON.
27
+ - `cancel` is optional, and cancellation is denied unless it explicitly approves.
28
+ - **List, get, cancel and watch** all apply the same policy. List answers newest first; get and
29
+ cancel answer `UnknownJob` for an absent id and for a foreign one alike, so existence outside the
30
+ audience never leaks.
31
+ - **Watch** pushes `JOB_EVENT` frames down a socket. A queue `JobEvent` carries no owner, so each
32
+ frame is reloaded inside the audience scope and emitted as a `JobViewEvent` (`upsert` with the
33
+ mapped job, or `remove` with a public id seen earlier on that connection); an unattributable event
34
+ is dropped rather than fanned out. Keep completed records long enough for watched completion events
35
+ to be projected.
36
+ - **Public projection** — `jobViewOf` maps technical states (`Active` → running, `Completed` →
37
+ succeeded, `Failed` → failed, anything else → queued) and timestamps, copies only the fields the
38
+ application supplies, bounds JSON depth, item count and string length, strips prototype keys and
39
+ non-JSON values, and never copies a raw failure.
40
+
41
+ ## Usage
42
+
43
+ ### 1. Declare the public contract
44
+
45
+ Declare the routes with `@owlmeans/job`, in the narrow package shared by the API and its client.
46
+ They are ordinary guarded HTTP/WS declarations and carry no queue name or broker option.
5
47
 
6
48
  ```typescript
7
49
  import { declareJobEntrypoints } from '@owlmeans/job'
8
- import { jobViewOf, serveJobEntrypoints } from '@owlmeans/server-job'
9
50
 
10
51
  export const reportJobs = declareJobEntrypoints('report-jobs', {
11
52
  path: '/reports/jobs', parent: api.reports.base,
12
53
  })
54
+ ```
55
+
56
+ ### 2. Bind the bridge with a policy
57
+
58
+ ```typescript
59
+ import { jobViewHelper, serveJobEntrypoints } from '@owlmeans/server-job'
13
60
 
14
61
  export const bindings = serveJobEntrypoints(reportJobs, {
15
62
  queue: REPORT_QUEUE,
@@ -23,29 +70,103 @@ export const bindings = serveJobEntrypoints(reportJobs, {
23
70
  }),
24
71
  lookup: async (id, audience, resource) =>
25
72
  resource.load({ publicId: id, 'data.ownerId': audience.userId }),
26
- map: record => jobViewOf(record, {
73
+ map: (record, audience) => jobViewHelper.jobViewOf(record, {
27
74
  id: publicIdOf(record),
28
75
  kind: publicKindOf(record),
29
76
  summary: publicSummaryOf(record),
30
77
  metadata: allowlistedMetadataOf(record),
31
78
  result: publicResultOf(record),
79
+ cancellable: ownerIdOf(record) === audience.userId,
32
80
  }),
33
81
  cancel: (record, audience) => ownerIdOf(record) === audience.userId,
34
82
  },
35
83
  })
36
84
  ```
37
85
 
38
- The policy is the security boundary:
86
+ `serveJobEntrypoints` returns the bindings for the group's base, list, watch, get and cancel
87
+ aliases; register them with the rest of the service's handlers. The helpers named in the policy
88
+ (`requireUserId`, `publicIdOf`, …) are the application's own.
89
+
90
+ ### 3. Report a failure safely
91
+
92
+ ```typescript
93
+ map: record => jobViewHelper.jobViewOf(record, {
94
+ id: publicIdOf(record),
95
+ kind: publicKindOf(record),
96
+ ...(record.state === JobState.Failed ? { error: jobViewHelper.publicJobError('export-failed') } : {}),
97
+ }),
98
+ ```
99
+
100
+ `publicJobError(type?)` answers `{ type, message: 'The background operation failed.' }` (type
101
+ `'job-failed'` by default). Pass an application-safe message only when one is explicitly available.
102
+
103
+ ### 4. Bind one handler yourself
104
+
105
+ Nothing is subclassed: an app that wants one handler of its own binds that alias itself, and can
106
+ still reuse the package's handlers for the rest.
107
+
108
+ ```typescript
109
+ import { bind } from '@owlmeans/server-entrypoint'
110
+ import { getJob, listJobs } from '@owlmeans/server-job'
111
+
112
+ const opts = { queue: REPORT_QUEUE, policy: reportJobPolicy }
113
+
114
+ export const bindings = [
115
+ bind(reportJobs.base),
116
+ bind(reportJobs.list, listJobs(reportJobs.list, opts)),
117
+ bind(reportJobs.get, getJob(reportJobs.get, opts)),
118
+ ]
119
+ ```
120
+
121
+ ## API
122
+
123
+ | Export | Kind | Purpose |
124
+ |---|---|---|
125
+ | `serveJobEntrypoints(entrypoints, opts)` | function | Bind base, list, watch, get and cancel of a `declareJobEntrypoints` group under one policy |
126
+ | `listJobs(protocol, opts)` | function → handler | The audience's jobs, newest first, filtered by `where`; paging only when `size` is given |
127
+ | `getJob(protocol, opts)` | function → handler | One job via `lookup` + `map`; `UnknownJob` for absent and foreign ids |
128
+ | `cancelJob(protocol, opts)` | function → handler | Remove an approved job and answer its mapped final record; `UnknownJob` unless `cancel` approves |
129
+ | `watchJobs(protocol, opts)` | function → socket handler | Push `JobViewEvent` frames (`JOB_EVENT`) for the audience's jobs |
130
+ | `jobViewHelper` | object | `jobViewOf(record, fields)`, `sanitizeJobJson(value)`, `publicJobError(type?)` |
131
+ | `createJobViewHelper()` | factory → `JobViewHelper` | Builds a `jobViewHelper` |
132
+ | `makeJobPolicyHelper(opts)` | factory → `JobPolicyHelper` | `jobsOf(ctx)` (the options' queue, else the context's sole queue) and `readExposedJob(resource, id, audience)` |
133
+ | `JobExposurePolicy<A>` | interface | `audience`, `where`, `lookup`, `map`, optional `cancel` |
134
+ | `JobHandlerOptions<A>` | interface | `{ queue?, policy }` |
135
+ | `JobAudience` | type | `Record<string, unknown>` — the shape an application's audience satisfies |
136
+ | `JobViewFields` | interface | What `jobViewOf` copies: `id`, `kind`, `summary?`, `metadata?`, `result?`, `error?`, `cancellable?` |
137
+ | `JobViewHelper`, `JobPolicyHelper` | interface | The two helper objects' contracts |
138
+ | `Config`, `Context` | interface | Server config with `queue`; server context with the API server and `jobs()` appended |
139
+ | `JobEntrypoints`, `JobListQuery` | type | Re-exported from `@owlmeans/job` |
140
+
141
+ `jobViewOf` limits: JSON depth 8, 100 items per array or object, strings 16 384 characters; `kind`
142
+ 128, `summary` 2048, error `type` 128 and `message` 2048, progress `message` 512 characters.
143
+
144
+ ## Common pitfalls
145
+
146
+ - `jobViewOf`, `sanitizeJobJson` and `publicJobError` are methods of `jobViewHelper`, not separate
147
+ exports.
148
+ - `where` must scope by audience even for an empty query: watch reloads every event's record with
149
+ `where(audience, {})`.
150
+ - Deriving `audience` from the request body or query instead of the authenticated request lets a
151
+ caller read someone else's jobs.
152
+ - A watched queue configured with `removeOnComplete` loses its completion events: the record they
153
+ would be attributed by is gone, so the event is dropped.
154
+ - Without `cancel`, every cancel answers `UnknownJob`; and `cancellable` in the view is `false`
155
+ unless `map` sets it.
156
+ - Cancellation is deletion in the queue contract: the job and its children leave the broker, a
157
+ finished job cancels to its final record, and a running processor is not interrupted — it keeps
158
+ its lock until it notices its own `signal`.
159
+ - A list `page` without a `size` is refused by the queue resource rather than silently windowed.
160
+ - Never put `failedReason`, stacks or raw payloads into a view; use `publicJobError()`.
161
+ - Name `queue` when the context declares more than one queue; it defaults to the sole one.
39
162
 
40
- - `audience` derives authenticated application scope.
41
- - `where` translates the closed public query into scoped technical criteria.
42
- - `lookup` resolves an opaque public id inside that scope.
43
- - `map` allowlists fields and returns only `JobView`; `jobViewOf` bounds and sanitizes JSON.
44
- - `cancel` is optional and cancellation is denied unless it explicitly approves.
163
+ ## Related packages
45
164
 
46
- List, get, cancel and watch all apply the same policy. Watch frames are reloaded inside the
47
- audience scope and emitted as `JobViewEvent`; an unattributable event is dropped. Keep completed
48
- records long enough for watched completion events to be projected.
165
+ - [`@owlmeans/job`](../job) — `declareJobEntrypoints`, `JobView`, `JobViewEvent`, `JOB_EVENT`
166
+ - [`@owlmeans/client-job`](../client-job) — browser state over `JobView`
167
+ - [`@owlmeans/queue`](../queue) — `QueueResource`, `JobRecord`, `JobState`, `UnknownJob`
168
+ - [`@owlmeans/redis-queue`](../redis-queue) — the Redis-backed queue driver
169
+ - [`@owlmeans/server-entrypoint`](../server-entrypoint) — `bind` for manual bindings
49
170
 
50
171
  <!-- owlmeans:agent-guidance:start -->
51
172
  ## Agent guidance
@@ -55,7 +176,7 @@ This package ships embedded agent skills under `agent-meta/`. After installing y
55
176
  your project's skill store (`.agents/skills/`):
56
177
 
57
178
  ```sh
58
- npx @owlmeans/agent-skills@^0.1.20-rc.1
179
+ npx @owlmeans/agent-skills@^0.2.0-alpha.0
59
180
  ```
60
181
 
61
182
  The embedded files are version-matched to this package release. Do not edit them
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "schemaVersion": 3,
3
3
  "package": "@owlmeans/server-job",
4
- "version": "0.1.20-rc.1",
5
- "generatedAt": "2026-11-04T18:40:59.888Z",
4
+ "version": "0.2.0-alpha.0",
5
+ "generatedAt": "2026-11-09T19:44:59.526Z",
6
6
  "canonicalRepo": "https://github.com/owlmeans/common",
7
7
  "entries": [
8
8
  {
@@ -8,7 +8,7 @@ user-invocable: false
8
8
  # @owlmeans/server-job
9
9
 
10
10
  **Layer:** Server
11
- **Install:** `"@owlmeans/server-job": "^0.1.20-rc.1"` in `dependencies`
11
+ **Install:** `"@owlmeans/server-job": "^0.2.0-alpha.0"` in `dependencies`
12
12
 
13
13
  This package is the boundary between technical `@owlmeans/queue` records and the safe
14
14
  `@owlmeans/job` contract. `serveJobEntrypoints` requires a `JobExposurePolicy`; there is no default
@@ -31,7 +31,7 @@ These are ordinary guarded HTTP/WS declarations. They carry no queue name or bro
31
31
  ## Bind the bridge
32
32
 
33
33
  ```typescript
34
- import { jobViewOf, serveJobEntrypoints } from '@owlmeans/server-job'
34
+ import { jobViewHelper, serveJobEntrypoints } from '@owlmeans/server-job'
35
35
 
36
36
  export const reportJobBindings = serveJobEntrypoints(reportJobs, {
37
37
  queue: REPORT_QUEUE,
@@ -45,7 +45,7 @@ export const reportJobBindings = serveJobEntrypoints(reportJobs, {
45
45
  }),
46
46
  lookup: async (id, audience, resource) =>
47
47
  resource.load({ publicId: id, 'data.ownerId': audience.userId }),
48
- map: record => jobViewOf(record, {
48
+ map: record => jobViewHelper.jobViewOf(record, {
49
49
  id: publicIdOf(record),
50
50
  kind: publicKindOf(record),
51
51
  summary: publicSummaryOf(record),
@@ -82,8 +82,8 @@ be projected.
82
82
  |---|---|
83
83
  | `serveJobEntrypoints` | Bind list/get/cancel/watch using a mandatory policy |
84
84
  | `listJobs` / `getJob` / `cancelJob` / `watchJobs` | Bind one handler manually |
85
- | `jobViewOf` | Safe record-to-view mapper |
86
- | `sanitizeJobJson` / `publicJobError` | Bounded JSON and safe failure helpers |
85
+ | `jobViewHelper.jobViewOf` | Safe record-to-view mapper |
86
+ | `jobViewHelper.sanitizeJobJson` / `jobViewHelper.publicJobError` | Bounded JSON and safe failure helpers |
87
87
  | `JobExposurePolicy` / `JobHandlerOptions` | Application policy contracts |
88
88
 
89
89
  ## Related
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@owlmeans/server-job",
3
- "version": "0.1.20-rc.1",
3
+ "version": "0.2.0-alpha.0",
4
4
  "license": "MIT",
5
5
  "homepage": "https://owlmeans.com/docs/",
6
6
  "repository": {
@@ -32,7 +32,7 @@
32
32
  }
33
33
  },
34
34
  "devDependencies": {
35
- "@owlmeans/dep-config": "^0.1.20-rc.1",
35
+ "@owlmeans/dep-config": "^0.2.0-alpha.0",
36
36
  "@types/bun": "^1.4.4",
37
37
  "@types/node": "^26.6.5",
38
38
  "nodemon": "^3.1.14",
@@ -42,19 +42,19 @@
42
42
  "access": "public"
43
43
  },
44
44
  "dependencies": {
45
- "@owlmeans/auth": "^0.1.20-rc.1",
46
- "@owlmeans/auth-common": "^0.1.20-rc.1",
47
- "@owlmeans/context": "^0.1.20-rc.1",
48
- "@owlmeans/entrypoint": "^0.1.20-rc.1",
49
- "@owlmeans/job": "^0.1.20-rc.1",
50
- "@owlmeans/log": "^0.1.20-rc.1",
51
- "@owlmeans/queue": "^0.1.20-rc.1",
52
- "@owlmeans/resource": "^0.1.20-rc.1",
53
- "@owlmeans/route": "^0.1.20-rc.1",
54
- "@owlmeans/server-api": "^0.1.20-rc.1",
55
- "@owlmeans/server-context": "^0.1.20-rc.1",
56
- "@owlmeans/server-entrypoint": "^0.1.20-rc.1",
57
- "@owlmeans/server-socket": "^0.1.20-rc.1",
58
- "@owlmeans/socket": "^0.1.20-rc.1"
45
+ "@owlmeans/auth": "^0.2.0-alpha.0",
46
+ "@owlmeans/auth-common": "^0.2.0-alpha.0",
47
+ "@owlmeans/context": "^0.2.0-alpha.0",
48
+ "@owlmeans/entrypoint": "^0.2.0-alpha.0",
49
+ "@owlmeans/job": "^0.2.0-alpha.0",
50
+ "@owlmeans/log": "^0.2.0-alpha.0",
51
+ "@owlmeans/queue": "^0.2.0-alpha.0",
52
+ "@owlmeans/resource": "^0.2.0-alpha.0",
53
+ "@owlmeans/route": "^0.2.0-alpha.0",
54
+ "@owlmeans/server-api": "^0.2.0-alpha.0",
55
+ "@owlmeans/server-context": "^0.2.0-alpha.0",
56
+ "@owlmeans/server-entrypoint": "^0.2.0-alpha.0",
57
+ "@owlmeans/server-socket": "^0.2.0-alpha.0",
58
+ "@owlmeans/socket": "^0.2.0-alpha.0"
59
59
  }
60
60
  }