| 1 | # Durable Object Facets |
| 2 | |
| 3 | A facet is a child object inside a Durable Object, with a SQLite database of |
| 4 | its own. A supervisor class starts a facet from a class that a |
| 5 | [Worker Loader](dynamic-workers.md) provides, so generated or untrusted code |
| 6 | gets durable storage without a Durable Object namespace. Read the |
| 7 | [Cloudflare Durable Object Facets documentation](https://developers.cloudflare.com/dynamic-workers/usage/durable-object-facets/) |
| 8 | for the standard API. |
| 9 | |
| 10 | ## Example |
| 11 | |
| 12 | The [facets example](../../examples/facets) creates a facet from a class that a |
| 13 | Worker Loader provides. |
| 14 | |
| 15 | <!-- celld-example: facets --> |
| 16 | |
| 17 | ## API |
| 18 | |
| 19 | - `ctx.facets.get(name, callback)` returns a stub for the named facet. The |
| 20 | callback returns `{ class, id }`, and celld runs it only to start the facet. |
| 21 | - `worker.getDurableObjectClass("App")` on a Worker Loader binding, or |
| 22 | `ctx.exports.App`, supplies the `class`. |
| 23 | - `ctx.exports.App({ props })` creates a class handle with startup properties. |
| 24 | - `ctx.facets.abort(name, reason)` stops a facet and keeps its database. |
| 25 | - `ctx.facets.delete(name)` stops a facet and removes its database. |
| 26 | - The stub answers `fetch()` and the RPC methods of the class. |
| 27 | |
| 28 | ## A facet is a part of its root object |
| 29 | |
| 30 | A facet is not a cell. It has no ownership record and no fencing epoch of its |
| 31 | own, and it uses those of the root cell, which the |
| 32 | [Durable Objects page](durable-objects.md#ownership-and-the-single-threaded-model) |
| 33 | describes. |
| 34 | |
| 35 | A facet runs on the owner node of the root cell. An eviction, a reset, and a |
| 36 | move take the root object and its facets as one group, and the startup |
| 37 | callback runs again at the next call. |
| 38 | |
| 39 | As in workerd, a facet write commits in the database of the facet, so a |
| 40 | rollback of a root transaction does not undo a facet call inside it. A facet |
| 41 | and its root object therefore never commit atomically. celld holds an outbound |
| 42 | effect of a facet, and the reply of a facet call, until the facet stream proves |
| 43 | the writes of that call. The effect also passes the output gate of the root |
| 44 | cell. |
| 45 | |
| 46 | ## Starting and addressing a facet |
| 47 | |
| 48 | `ctx.facets.get(name, callback)` returns a stub, and celld runs the callback |
| 49 | only when the facet is not already running. The callback returns a `class` and |
| 50 | an optional `id`. The `class` comes from |
| 51 | `worker.getDurableObjectClass("App")` on a Worker Loader binding, or from |
| 52 | `ctx.exports.App` for an exported `DurableObject` class without a storage |
| 53 | migration; that facet runs in the isolate of its root. |
| 54 | |
| 55 | A loaded class can extend `DurableObject`, or it can be a plain class with a |
| 56 | `(state, env)` constructor. A plain class answers `fetch()`, and its RPC |
| 57 | methods require the `js_rpc` compatibility flag, as on workerd. |
| 58 | |
| 59 | `id` sets `ctx.id` in the facet. A `DurableObjectId` keeps its name, a string |
| 60 | ID stays a string, and an omitted ID inherits the parent ID and its name. An |
| 61 | unmigrated class handle has no `idFromName()`, so use a Durable Object |
| 62 | namespace to create a named ID. |
| 63 | |
| 64 | `ctx.exports.App({ props })` creates a class handle with startup properties. |
| 65 | The call copies the properties, so a later change to the object does not |
| 66 | reach `ctx.props`. The properties must support structured cloning. A |
| 67 | loopback class without properties supplies an empty object. |
| 68 | |
| 69 | - The name alone selects the database, so a changed `id` still reaches the |
| 70 | stored data. A name has a limit of 256 bytes. |
| 71 | - A facet can start facets of its own, to a total depth of 4 that counts the |
| 72 | root object. |
| 73 | - `ctx.facets.abort(name, reason)` stops a facet and keeps its database. A |
| 74 | call on the stopped stub throws the reason. |
| 75 | - `ctx.facets.delete(name)` stops the facet and removes its database and the |
| 76 | database of every facet below it. |
| 77 | |
| 78 | No name reaches a facet from outside its root object, so external traffic |
| 79 | must go through the supervisor. Use a separate Durable Object, addressed with |
| 80 | [`idFromName()`](durable-objects.md#identity-and-addressing), when children |
| 81 | must run on different nodes. |
| 82 | |
| 83 | ## WebSockets in a facet |
| 84 | |
| 85 | A facet can accept a WebSocket: its `fetch()` returns a `101` response with the |
| 86 | client end of a `WebSocketPair`, and the root object returns that response. The |
| 87 | facet can call `accept()` on the server end or pass it to |
| 88 | `ctx.acceptWebSocket()`. A facet can also open a WebSocket with |
| 89 | `new WebSocket(url)` or with an `Upgrade: websocket` fetch. celld delivers the |
| 90 | events of each such socket to the facet, not to the root object. Socket output |
| 91 | waits for the facet stream and passes the output gate of the root cell. |
| 92 | |
| 93 | A facet socket does not hibernate, because celld cannot start a facet again |
| 94 | without its root object. A socket that the facet accepts or opens keeps the root |
| 95 | cell resident until it closes. The client end of a `101` response from another |
| 96 | Durable Object does not. `getWebSockets()` returns the sockets of that facet. |
| 97 | |
| 98 | `ctx.facets.abort()` and `ctx.facets.delete()` close the sockets of each stopped |
| 99 | facet with code `1001`, and the facet receives no close event. workerd drops the |
| 100 | connection instead, so its client sees `1006`. A drain, an ownership move, or a |
| 101 | generation swap closes a facet socket with `1012`, and an output gate failure |
| 102 | closes it with `1011`. |
| 103 | |
| 104 | ## Differences from Cloudflare |
| 105 | |
| 106 | - A Durable Object binding cannot supply a facet class. A class with a storage |
| 107 | migration cannot supply one either, because `ctx.exports` holds that class as |
| 108 | a namespace. |
| 109 | - Each facet has a separate SQLite database, which celld replicates as a |
| 110 | separate stream under the bucket prefix of the root Durable Object. |
| 111 | - A facet cannot set an alarm. `storage.setAlarm()` throws inside a facet, so |
| 112 | the root object must hold the schedule. |
| 113 | - A facet socket does not hibernate. It keeps the root cell resident until the |
| 114 | socket closes. |
| 115 | - A facet stub is not awaitable, and a pipelined property path is unavailable, |
| 116 | so a call must name one method. |
| 117 | - The `clone()` method is unavailable. |
| 118 | |
| 119 | The [Cloudflare compatibility](../cloudflare-compat.md#services) |
| 120 | page lists the runtime APIs and the unsupported services. |