@codeworksh/schema 0.0.0-stage → 0.1.0-canary.165.2.g336229f9
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/LICENSE +21 -0
- package/README.md +47 -2
- package/common.d.mts +86 -0
- package/common.mjs +88 -0
- package/common.mjs.map +1 -0
- package/daemon.d.mts +12 -0
- package/daemon.mjs +22 -0
- package/daemon.mjs.map +1 -0
- package/door.d.mts +33 -0
- package/door.mjs +52 -0
- package/door.mjs.map +1 -0
- package/event.d.mts +79 -0
- package/event.mjs +35 -0
- package/event.mjs.map +1 -0
- package/forwarded.d.mts +13 -0
- package/forwarded.mjs +25 -0
- package/forwarded.mjs.map +1 -0
- package/host/envelope.d.mts +36 -0
- package/host/envelope.mjs +35 -0
- package/host/envelope.mjs.map +1 -0
- package/host/part.d.mts +2 -0
- package/host/part.mjs +60 -0
- package/host/part.mjs.map +1 -0
- package/host/principal.d.mts +36 -0
- package/host/principal.mjs +24 -0
- package/host/principal.mjs.map +1 -0
- package/host/records.d.mts +1051 -0
- package/host/records.mjs +494 -0
- package/host/records.mjs.map +1 -0
- package/host.d.mts +2224 -0
- package/host.mjs +812 -0
- package/host.mjs.map +1 -0
- package/ids.d.mts +53 -0
- package/ids.mjs +60 -0
- package/ids.mjs.map +1 -0
- package/jws.d.mts +84 -0
- package/jws.mjs +119 -0
- package/jws.mjs.map +1 -0
- package/location.d.mts +55 -0
- package/location.mjs +51 -0
- package/location.mjs.map +1 -0
- package/machine.d.mts +77 -0
- package/machine.mjs +48 -0
- package/machine.mjs.map +1 -0
- package/mcp.d.mts +24 -0
- package/mcp.mjs +36 -0
- package/mcp.mjs.map +1 -0
- package/model.d.mts +18 -0
- package/model.mjs +35 -0
- package/model.mjs.map +1 -0
- package/package.json +182 -5
- package/pairing.d.mts +67 -0
- package/pairing.mjs +67 -0
- package/pairing.mjs.map +1 -0
- package/part-Bzm-yNax.d.mts +69 -0
- package/project.d.mts +30 -0
- package/project.mjs +27 -0
- package/project.mjs.map +1 -0
- package/records-316ZUpKX.mjs +127 -0
- package/records-316ZUpKX.mjs.map +1 -0
- package/relay.d.mts +285 -0
- package/relay.mjs +115 -0
- package/relay.mjs.map +1 -0
- package/repo.d.mts +42 -0
- package/repo.mjs +46 -0
- package/repo.mjs.map +1 -0
- package/rolldown-runtime-D7D4PA-g.mjs +13 -0
- package/sandbox/driver.d.mts +10 -0
- package/sandbox/driver.mjs +13 -0
- package/sandbox/driver.mjs.map +1 -0
- package/sandbox/errors.d.mts +130 -0
- package/sandbox/errors.mjs +112 -0
- package/sandbox/errors.mjs.map +1 -0
- package/sandbox/instance.d.mts +152 -0
- package/sandbox/instance.mjs +150 -0
- package/sandbox/instance.mjs.map +1 -0
- package/session.d.mts +59 -0
- package/session.mjs +69 -0
- package/session.mjs.map +1 -0
- package/settings.d.mts +22 -0
- package/settings.mjs +32 -0
- package/settings.mjs.map +1 -0
- package/shell.d.mts +5 -0
- package/shell.mjs +7 -0
- package/shell.mjs.map +1 -0
- package/space-CR-9UxdT.d.mts +25 -0
- package/space.d.mts +2 -0
- package/space.mjs +33 -0
- package/space.mjs.map +1 -0
- package/thread.d.mts +50 -0
- package/thread.mjs +69 -0
- package/thread.mjs.map +1 -0
- package/usage-B_TDmpKC.d.mts +15 -0
- package/usage.d.mts +2 -0
- package/usage.mjs +43 -0
- package/usage.mjs.map +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sanchit Kudari <0xsanchit@gmail.com> & Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,48 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @codeworksh/schema
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The schemas that more than one part of CodeWork speaks: the ids, projects, spaces, locations, events and sandbox instances the harness persists and plugins read, the CodeWork RPC server's contract, and Relay's HTTP API, signed machine requests, pairing, the server's WebSocket door and the machine status the desktop app reads. The plugin SDK (`packages/plugin`), the harness, the CLI and RPC server (`packages/codework`), the app packages and Relay (`infra/relay`) all import it.
|
|
4
|
+
|
|
5
|
+
It is published to npm, with `effect` as a peer dependency and `uuidv7` for minting ids. Each module is its own subpath, so a consumer loads only what it imports:
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { Door } from "@codeworksh/schema/door";
|
|
9
|
+
import { Relay } from "@codeworksh/schema/relay";
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Boundaries
|
|
13
|
+
|
|
14
|
+
- **Only `effect`.** This code runs in Node and in the desktop renderer, a browser, so it uses Effect and WebCrypto and nothing from Node or `@codeworksh/aikit`. A Node import here would break the renderer.
|
|
15
|
+
- **Shapes, not services.** It declares what crosses the wire and the small helpers both ends need to agree on it (signing, token checks), plus only the middleware and context tags Effect's `HttpApi` and `RpcGroup` require on the contract itself (`Relay.Person`, `Relay.PersonAuth`, `HostPrincipal.Current`, `HostPrincipal.Door`). Who stores what, and how a call is handled, stays in the consumer.
|
|
16
|
+
- **One home per schema.** A shape two packages share is declared here once and imported, never redeclared: a brand or class declared twice is two types, and persisted rows and wire payloads depend on its tags and identifiers.
|
|
17
|
+
- **Fields are wire format.** Machines, desktop apps and Relay update at different times, so an older peer may be on the other end. Add a new field as optional rather than renaming or removing one.
|
|
18
|
+
|
|
19
|
+
## Code Map
|
|
20
|
+
|
|
21
|
+
Every module but `shell` also exports itself as a namespace, the way the example above imports `Door` and `Relay`.
|
|
22
|
+
|
|
23
|
+
- [`src/relay.ts`](./src/relay.ts) declares Relay's HTTP API (`Relay.Api`) and its errors, and re-exports [`src/records.ts`](./src/records.ts): its records and the signed request claims, kept apart so `machine` does not load the API.
|
|
24
|
+
- [`src/jws.ts`](./src/jws.ts) signs and verifies compact Ed25519 JWS with WebCrypto, so a machine signs and Relay verifies with the same code.
|
|
25
|
+
- [`src/pairing.ts`](./src/pairing.ts) is how another computer pairs with a CodeWork server over HTTP.
|
|
26
|
+
- [`src/door.ts`](./src/door.ts) carries the WebSocket token as a subprotocol and checks it at the listener.
|
|
27
|
+
- [`src/forwarded.ts`](./src/forwarded.ts) decides whether a request reached the listener over TLS through a trusted proxy.
|
|
28
|
+
- [`src/machine.ts`](./src/machine.ts) is a server's machine status and connect events as its clients see them over RPC.
|
|
29
|
+
- [`src/shell.ts`](./src/shell.ts) quotes a path as one shell word.
|
|
30
|
+
- [`src/ids.ts`](./src/ids.ts) mints and brands session, thread and message ids.
|
|
31
|
+
- [`src/project.ts`](./src/project.ts), [`src/repo.ts`](./src/repo.ts), [`src/space.ts`](./src/space.ts) and [`src/location.ts`](./src/location.ts) describe where work happens.
|
|
32
|
+
- [`src/event.ts`](./src/event.ts) is the event core: ids, payloads and `define`.
|
|
33
|
+
- [`src/sandbox/instance.ts`](./src/sandbox/instance.ts), [`driver.ts`](./src/sandbox/driver.ts) and [`errors.ts`](./src/sandbox/errors.ts) are a sandbox's identity, state and lifecycle errors.
|
|
34
|
+
- [`src/host.ts`](./src/host.ts) is the CodeWork RPC server's contract (`Host.Api`), which the CLI, the server and the desktop app server import, re-exporting [`src/host/records.ts`](./src/host/records.ts) (the wire data records, kept apart so UI code does not load the `RpcGroup` or server errors), with event envelopes, message parts and the door's middleware in [`src/host`](./src/host).
|
|
35
|
+
- [`src/session.ts`](./src/session.ts), [`thread.ts`](./src/thread.ts), [`settings.ts`](./src/settings.ts), [`model.ts`](./src/model.ts), [`mcp.ts`](./src/mcp.ts), [`daemon.ts`](./src/daemon.ts) and [`usage.ts`](./src/usage.ts) hold the harness's errors and values that cross that contract; the harness re-exports them.
|
|
36
|
+
- [`src/common.ts`](./src/common.ts) holds the small schema helpers the rest build on (`AbsolutePath`, `optional`, `withStatics`).
|
|
37
|
+
|
|
38
|
+
## Working Locally
|
|
39
|
+
|
|
40
|
+
```sh
|
|
41
|
+
pnpm typecheck # tsc --noEmit
|
|
42
|
+
pnpm build # vp pack, into dist/pack
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Learn More
|
|
46
|
+
|
|
47
|
+
- [Relay](../../docs/internals/10_relay.md): the API, the signed machine requests and the keys behind them.
|
|
48
|
+
- [Server](../../docs/internals/09_server.md): the door, pairing and trusted proxies.
|
package/common.d.mts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
declare namespace common_d_exports {
|
|
3
|
+
export { AbsolutePath, common_d_exports as Common, DateTimeUtcFromMillis, DeepMutable, Newtype, NonNegativeCost, NonNegativeInt, PositiveInt, optional, withStatics };
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Integer greater than zero.
|
|
7
|
+
*/
|
|
8
|
+
export declare const PositiveInt: Schema.Int;
|
|
9
|
+
/**
|
|
10
|
+
* Integer greater than or equal to zero.
|
|
11
|
+
*/
|
|
12
|
+
export declare const NonNegativeInt: Schema.Int;
|
|
13
|
+
/**
|
|
14
|
+
* Cost greater than or equal with finite value
|
|
15
|
+
*/
|
|
16
|
+
export declare const NonNegativeCost: Schema.Finite;
|
|
17
|
+
/**
|
|
18
|
+
* Absolute POSIX file path (e.g., `/home/user/projects/myapp/src/main.ts`).
|
|
19
|
+
*/
|
|
20
|
+
export declare const AbsolutePath: Schema.brand<Schema.String, "AbsolutePath">;
|
|
21
|
+
export type AbsolutePath = Schema.Schema.Type<typeof AbsolutePath>;
|
|
22
|
+
/**
|
|
23
|
+
* Optional public JSON field that can hold explicit `undefined` on the type
|
|
24
|
+
* side but encodes it as an omitted key, matching legacy `JSON.stringify`.
|
|
25
|
+
*/
|
|
26
|
+
export declare const optional: <S extends Schema.Top>(schema: S) => Schema.decodeTo<Schema.optional<Schema.toType<S>>, Schema.optionalKey<S>, never, never>;
|
|
27
|
+
/**
|
|
28
|
+
* Strip `readonly` from a nested type. Stand-in for `effect`'s `Types.DeepMutable`
|
|
29
|
+
* until `effect:core/x228my` ("Types.DeepMutable widens unknown to `{}`") lands.
|
|
30
|
+
*
|
|
31
|
+
* The upstream version falls through `unknown` into `{ -readonly [K in keyof T]: ... }`
|
|
32
|
+
* where `keyof unknown = never`, so `unknown` collapses to `{}`. This local
|
|
33
|
+
* version gates the object branch on `extends object` (which `unknown` does
|
|
34
|
+
* not) so `unknown` passes through untouched.
|
|
35
|
+
*
|
|
36
|
+
* Primitive bailout matches upstream — without it, branded strings like
|
|
37
|
+
* `string & Brand<"SessionID">` fall into the object branch and get their
|
|
38
|
+
* prototype methods walked.
|
|
39
|
+
*
|
|
40
|
+
* Tuple branch preserves readonly tuples (e.g. `ConfigPlugin.Spec`'s
|
|
41
|
+
* `readonly [string, Options]`); the general array branch would otherwise
|
|
42
|
+
* widen them to unbounded arrays.
|
|
43
|
+
*/
|
|
44
|
+
export type DeepMutable<T> = T extends string | number | boolean | bigint | symbol | Function ? T : T extends readonly [unknown, ...unknown[]] ? { -readonly [K in keyof T]: DeepMutable<T[K]>; } : T extends readonly (infer U)[] ? DeepMutable<U>[] : T extends object ? { -readonly [K in keyof T]: DeepMutable<T[K]>; } : T;
|
|
45
|
+
/**
|
|
46
|
+
* Attach static methods to a schema object. Designed to be used with `.pipe()`:
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* export const Foo = fooSchema.pipe(
|
|
50
|
+
* withStatics((schema) => ({
|
|
51
|
+
* zero: schema.make(0),
|
|
52
|
+
* from: Schema.decodeUnknownOption(schema),
|
|
53
|
+
* }))
|
|
54
|
+
* )
|
|
55
|
+
*/
|
|
56
|
+
export declare const withStatics: <S extends object, M extends Record<string, unknown>>(methods: (schema: S) => M) => (schema: S) => S & M;
|
|
57
|
+
/**
|
|
58
|
+
* Nominal wrapper for scalar types. The class itself is a valid schema —
|
|
59
|
+
* pass it directly to `Schema.decode`, `Schema.decodeEffect`, etc.
|
|
60
|
+
*
|
|
61
|
+
* Overrides `~type.make` on the derived `Schema.Opaque` so `Schema.Schema.Type`
|
|
62
|
+
* of a field using this newtype resolves to `Self` rather than the underlying
|
|
63
|
+
* branded phantom. Without that override, passing a class instance to code
|
|
64
|
+
* typed against `Schema.Schema.Type<FieldSchema>` would require a cast even
|
|
65
|
+
* though the values are structurally equivalent at runtime.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* class QuestionID extends Newtype<QuestionID>()("QuestionID", Schema.String) {
|
|
69
|
+
* static make(id: string): QuestionID {
|
|
70
|
+
* return this.make(id)
|
|
71
|
+
* }
|
|
72
|
+
* }
|
|
73
|
+
*
|
|
74
|
+
* Schema.decodeEffect(QuestionID)(input)
|
|
75
|
+
*/
|
|
76
|
+
export declare function Newtype<Self>(): <const Tag extends string, S extends Schema.Top>(tag: Tag, schema: S) => (abstract new (_: never) => {
|
|
77
|
+
readonly _newtype: Tag;
|
|
78
|
+
}) & {
|
|
79
|
+
readonly make: (value: Schema.Schema.Type<S>) => Self;
|
|
80
|
+
} & Omit<Schema.Opaque<Self, S, {}>, "make" | "~type.make"> & {
|
|
81
|
+
readonly "~type.make": Self;
|
|
82
|
+
};
|
|
83
|
+
export declare const DateTimeUtcFromMillis: Schema.decodeTo<Schema.DateTimeUtc, Schema.Finite, never, never>;
|
|
84
|
+
//#endregion
|
|
85
|
+
export { common_d_exports as Common };
|
|
86
|
+
//# sourceMappingURL=common.d.mts.map
|
package/common.mjs
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
|
|
2
|
+
import { DateTime, Option, Schema, SchemaGetter } from "effect";
|
|
3
|
+
//#region src/common.ts
|
|
4
|
+
var common_exports = /* @__PURE__ */ __exportAll({
|
|
5
|
+
AbsolutePath: () => AbsolutePath,
|
|
6
|
+
Common: () => common_exports,
|
|
7
|
+
DateTimeUtcFromMillis: () => DateTimeUtcFromMillis,
|
|
8
|
+
Newtype: () => Newtype,
|
|
9
|
+
NonNegativeCost: () => NonNegativeCost,
|
|
10
|
+
NonNegativeInt: () => NonNegativeInt,
|
|
11
|
+
PositiveInt: () => PositiveInt,
|
|
12
|
+
optional: () => optional,
|
|
13
|
+
withStatics: () => withStatics
|
|
14
|
+
});
|
|
15
|
+
/**
|
|
16
|
+
* Integer greater than zero.
|
|
17
|
+
*/
|
|
18
|
+
const PositiveInt = Schema.Int.check(Schema.isGreaterThan(0));
|
|
19
|
+
/**
|
|
20
|
+
* Integer greater than or equal to zero.
|
|
21
|
+
*/
|
|
22
|
+
const NonNegativeInt = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0));
|
|
23
|
+
/**
|
|
24
|
+
* Cost greater than or equal with finite value
|
|
25
|
+
*/
|
|
26
|
+
const NonNegativeCost = Schema.Finite.check(Schema.isGreaterThanOrEqualTo(0));
|
|
27
|
+
/**
|
|
28
|
+
* Absolute POSIX file path (e.g., `/home/user/projects/myapp/src/main.ts`).
|
|
29
|
+
*/
|
|
30
|
+
const AbsolutePath = Schema.String.check(Schema.isPattern(/^\//)).pipe(Schema.brand("AbsolutePath"));
|
|
31
|
+
/**
|
|
32
|
+
* Optional public JSON field that can hold explicit `undefined` on the type
|
|
33
|
+
* side but encodes it as an omitted key, matching legacy `JSON.stringify`.
|
|
34
|
+
*/
|
|
35
|
+
const optional = (schema) => Schema.optionalKey(schema).pipe(Schema.decodeTo(Schema.optional(Schema.toType(schema)), {
|
|
36
|
+
decode: SchemaGetter.passthrough({ strict: false }),
|
|
37
|
+
encode: SchemaGetter.transformOptional(Option.filter((value) => value !== void 0))
|
|
38
|
+
}));
|
|
39
|
+
/**
|
|
40
|
+
* Attach static methods to a schema object. Designed to be used with `.pipe()`:
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* export const Foo = fooSchema.pipe(
|
|
44
|
+
* withStatics((schema) => ({
|
|
45
|
+
* zero: schema.make(0),
|
|
46
|
+
* from: Schema.decodeUnknownOption(schema),
|
|
47
|
+
* }))
|
|
48
|
+
* )
|
|
49
|
+
*/
|
|
50
|
+
const withStatics = (methods) => (schema) => Object.assign(schema, methods(schema));
|
|
51
|
+
/**
|
|
52
|
+
* Nominal wrapper for scalar types. The class itself is a valid schema —
|
|
53
|
+
* pass it directly to `Schema.decode`, `Schema.decodeEffect`, etc.
|
|
54
|
+
*
|
|
55
|
+
* Overrides `~type.make` on the derived `Schema.Opaque` so `Schema.Schema.Type`
|
|
56
|
+
* of a field using this newtype resolves to `Self` rather than the underlying
|
|
57
|
+
* branded phantom. Without that override, passing a class instance to code
|
|
58
|
+
* typed against `Schema.Schema.Type<FieldSchema>` would require a cast even
|
|
59
|
+
* though the values are structurally equivalent at runtime.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* class QuestionID extends Newtype<QuestionID>()("QuestionID", Schema.String) {
|
|
63
|
+
* static make(id: string): QuestionID {
|
|
64
|
+
* return this.make(id)
|
|
65
|
+
* }
|
|
66
|
+
* }
|
|
67
|
+
*
|
|
68
|
+
* Schema.decodeEffect(QuestionID)(input)
|
|
69
|
+
*/
|
|
70
|
+
function Newtype() {
|
|
71
|
+
return (tag, schema) => {
|
|
72
|
+
class Base {
|
|
73
|
+
static make(value) {
|
|
74
|
+
return value;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
Object.setPrototypeOf(Base, schema);
|
|
78
|
+
return Base;
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
const DateTimeUtcFromMillis = Schema.Finite.pipe(Schema.decodeTo(Schema.DateTimeUtc, {
|
|
82
|
+
decode: SchemaGetter.transform((value) => DateTime.makeUnsafe(value)),
|
|
83
|
+
encode: SchemaGetter.transform((value) => DateTime.toEpochMillis(value))
|
|
84
|
+
}));
|
|
85
|
+
//#endregion
|
|
86
|
+
export { AbsolutePath, common_exports as Common, DateTimeUtcFromMillis, Newtype, NonNegativeCost, NonNegativeInt, PositiveInt, optional, withStatics };
|
|
87
|
+
|
|
88
|
+
//# sourceMappingURL=common.mjs.map
|
package/common.mjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"common.mjs","names":[],"sources":["../../src/common.ts"],"sourcesContent":["import { DateTime, Option, Schema, SchemaGetter } from \"effect\";\n\n/**\n * Integer greater than zero.\n */\nexport const PositiveInt = Schema.Int.check(Schema.isGreaterThan(0));\n\n/**\n * Integer greater than or equal to zero.\n */\nexport const NonNegativeInt = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0));\n\n/**\n * Cost greater than or equal with finite value\n */\nexport const NonNegativeCost = Schema.Finite.check(Schema.isGreaterThanOrEqualTo(0));\n\n/**\n * Absolute POSIX file path (e.g., `/home/user/projects/myapp/src/main.ts`).\n */\nexport const AbsolutePath = Schema.String.check(Schema.isPattern(/^\\//)).pipe(Schema.brand(\"AbsolutePath\"));\nexport type AbsolutePath = Schema.Schema.Type<typeof AbsolutePath>;\n\n/**\n * Optional public JSON field that can hold explicit `undefined` on the type\n * side but encodes it as an omitted key, matching legacy `JSON.stringify`.\n */\nexport const optional = <S extends Schema.Top>(schema: S) =>\n\tSchema.optionalKey(schema).pipe(\n\t\tSchema.decodeTo(Schema.optional(Schema.toType(schema)), {\n\t\t\tdecode: SchemaGetter.passthrough({ strict: false }),\n\t\t\tencode: SchemaGetter.transformOptional(Option.filter((value) => value !== undefined)),\n\t\t}),\n\t);\n\n/**\n * Strip `readonly` from a nested type. Stand-in for `effect`'s `Types.DeepMutable`\n * until `effect:core/x228my` (\"Types.DeepMutable widens unknown to `{}`\") lands.\n *\n * The upstream version falls through `unknown` into `{ -readonly [K in keyof T]: ... }`\n * where `keyof unknown = never`, so `unknown` collapses to `{}`. This local\n * version gates the object branch on `extends object` (which `unknown` does\n * not) so `unknown` passes through untouched.\n *\n * Primitive bailout matches upstream — without it, branded strings like\n * `string & Brand<\"SessionID\">` fall into the object branch and get their\n * prototype methods walked.\n *\n * Tuple branch preserves readonly tuples (e.g. `ConfigPlugin.Spec`'s\n * `readonly [string, Options]`); the general array branch would otherwise\n * widen them to unbounded arrays.\n */\nexport type DeepMutable<T> = T extends string | number | boolean | bigint | symbol | Function\n\t? T\n\t: T extends readonly [unknown, ...unknown[]]\n\t\t? { -readonly [K in keyof T]: DeepMutable<T[K]> }\n\t\t: T extends readonly (infer U)[]\n\t\t\t? DeepMutable<U>[]\n\t\t\t: T extends object\n\t\t\t\t? { -readonly [K in keyof T]: DeepMutable<T[K]> }\n\t\t\t\t: T;\n\n/**\n * Attach static methods to a schema object. Designed to be used with `.pipe()`:\n *\n * @example\n * export const Foo = fooSchema.pipe(\n * withStatics((schema) => ({\n * zero: schema.make(0),\n * from: Schema.decodeUnknownOption(schema),\n * }))\n * )\n */\nexport const withStatics =\n\t<S extends object, M extends Record<string, unknown>>(methods: (schema: S) => M) =>\n\t(schema: S): S & M =>\n\t\tObject.assign(schema, methods(schema));\n\n/**\n * Nominal wrapper for scalar types. The class itself is a valid schema —\n * pass it directly to `Schema.decode`, `Schema.decodeEffect`, etc.\n *\n * Overrides `~type.make` on the derived `Schema.Opaque` so `Schema.Schema.Type`\n * of a field using this newtype resolves to `Self` rather than the underlying\n * branded phantom. Without that override, passing a class instance to code\n * typed against `Schema.Schema.Type<FieldSchema>` would require a cast even\n * though the values are structurally equivalent at runtime.\n *\n * @example\n * class QuestionID extends Newtype<QuestionID>()(\"QuestionID\", Schema.String) {\n * static make(id: string): QuestionID {\n * return this.make(id)\n * }\n * }\n *\n * Schema.decodeEffect(QuestionID)(input)\n */\nexport function Newtype<Self>() {\n\treturn <const Tag extends string, S extends Schema.Top>(tag: Tag, schema: S) => {\n\t\tabstract class Base {\n\t\t\tdeclare readonly _newtype: Tag;\n\n\t\t\tstatic make(value: Schema.Schema.Type<S>): Self {\n\t\t\t\treturn value as unknown as Self;\n\t\t\t}\n\t\t}\n\n\t\tObject.setPrototypeOf(Base, schema);\n\n\t\treturn Base as unknown as (abstract new (_: never) => { readonly _newtype: Tag }) & {\n\t\t\treadonly make: (value: Schema.Schema.Type<S>) => Self;\n\t\t} & Omit<Schema.Opaque<Self, S, {}>, \"make\" | \"~type.make\"> & {\n\t\t\t\treadonly \"~type.make\": Self;\n\t\t\t};\n\t};\n}\n\nexport const DateTimeUtcFromMillis = Schema.Finite.pipe(\n\tSchema.decodeTo(Schema.DateTimeUtc, {\n\t\tdecode: SchemaGetter.transform((value) => DateTime.makeUnsafe(value)),\n\t\tencode: SchemaGetter.transform((value) => DateTime.toEpochMillis(value)),\n\t}),\n);\n\nexport * as Common from \"./common.ts\";\n"],"mappings":";;;;;;;;;;;;;;;;;AAKA,MAAa,cAAc,OAAO,IAAI,MAAM,OAAO,cAAc,CAAC,CAAC;;;;AAKnE,MAAa,iBAAiB,OAAO,IAAI,MAAM,OAAO,uBAAuB,CAAC,CAAC;;;;AAK/E,MAAa,kBAAkB,OAAO,OAAO,MAAM,OAAO,uBAAuB,CAAC,CAAC;;;;AAKnF,MAAa,eAAe,OAAO,OAAO,MAAM,OAAO,UAAU,KAAK,CAAC,CAAC,CAAC,KAAK,OAAO,MAAM,cAAc,CAAC;;;;;AAO1G,MAAa,YAAkC,WAC9C,OAAO,YAAY,MAAM,CAAC,CAAC,KAC1B,OAAO,SAAS,OAAO,SAAS,OAAO,OAAO,MAAM,CAAC,GAAG;CACvD,QAAQ,aAAa,YAAY,EAAE,QAAQ,MAAM,CAAC;CAClD,QAAQ,aAAa,kBAAkB,OAAO,QAAQ,UAAU,UAAU,KAAA,CAAS,CAAC;AACrF,CAAC,CACF;;;;;;;;;;;;AAwCD,MAAa,eAC0C,aACrD,WACA,OAAO,OAAO,QAAQ,QAAQ,MAAM,CAAC;;;;;;;;;;;;;;;;;;;;AAqBvC,SAAgB,UAAgB;CAC/B,QAAwD,KAAU,WAAc;EAC/E,MAAe,KAAK;GAGnB,OAAO,KAAK,OAAoC;IAC/C,OAAO;GACR;EACD;EAEA,OAAO,eAAe,MAAM,MAAM;EAElC,OAAO;CAKR;AACD;AAEA,MAAa,wBAAwB,OAAO,OAAO,KAClD,OAAO,SAAS,OAAO,aAAa;CACnC,QAAQ,aAAa,WAAW,UAAU,SAAS,WAAW,KAAK,CAAC;CACpE,QAAQ,aAAa,WAAW,UAAU,SAAS,cAAc,KAAK,CAAC;AACxE,CAAC,CACF"}
|
package/daemon.d.mts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
declare namespace daemon_d_exports {
|
|
3
|
+
export { DaemonCallError, daemon_d_exports as DaemonSchema };
|
|
4
|
+
}
|
|
5
|
+
declare const DaemonCallError_base: Schema.Class<DaemonCallError, Schema.TaggedStruct<"DaemonCallError", {
|
|
6
|
+
readonly reason: Schema.Literals<readonly ["unavailable", "detached", "method_not_found", "invalid_input", "invalid_output", "failed"]>;
|
|
7
|
+
readonly message: Schema.String;
|
|
8
|
+
}>, import("effect/Cause").YieldableError>;
|
|
9
|
+
export declare class DaemonCallError extends DaemonCallError_base {}
|
|
10
|
+
//#endregion
|
|
11
|
+
export { daemon_d_exports as DaemonSchema };
|
|
12
|
+
//# sourceMappingURL=daemon.d.mts.map
|
package/daemon.mjs
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
|
|
2
|
+
import { Schema } from "effect";
|
|
3
|
+
//#region src/daemon.ts
|
|
4
|
+
var daemon_exports = /* @__PURE__ */ __exportAll({
|
|
5
|
+
DaemonCallError: () => DaemonCallError,
|
|
6
|
+
DaemonSchema: () => daemon_exports
|
|
7
|
+
});
|
|
8
|
+
var DaemonCallError = class extends Schema.TaggedError()("DaemonCallError", {
|
|
9
|
+
reason: Schema.Literals([
|
|
10
|
+
"unavailable",
|
|
11
|
+
"detached",
|
|
12
|
+
"method_not_found",
|
|
13
|
+
"invalid_input",
|
|
14
|
+
"invalid_output",
|
|
15
|
+
"failed"
|
|
16
|
+
]),
|
|
17
|
+
message: Schema.String
|
|
18
|
+
}) {};
|
|
19
|
+
//#endregion
|
|
20
|
+
export { DaemonCallError, daemon_exports as DaemonSchema };
|
|
21
|
+
|
|
22
|
+
//# sourceMappingURL=daemon.mjs.map
|
package/daemon.mjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"daemon.mjs","names":[],"sources":["../../src/daemon.ts"],"sourcesContent":["import { Schema } from \"effect\";\n\nexport class DaemonCallError extends Schema.TaggedError<DaemonCallError>()(\"DaemonCallError\", {\n\treason: Schema.Literals([\n\t\t\"unavailable\",\n\t\t\"detached\",\n\t\t\"method_not_found\",\n\t\t\"invalid_input\",\n\t\t\"invalid_output\",\n\t\t\"failed\",\n\t]),\n\tmessage: Schema.String,\n}) {}\n\nexport * as DaemonSchema from \"./daemon.ts\";\n"],"mappings":";;;;;;;AAEA,IAAa,kBAAb,cAAqC,OAAO,YAA6B,CAAC,CAAC,mBAAmB;CAC7F,QAAQ,OAAO,SAAS;EACvB;EACA;EACA;EACA;EACA;EACA;CACD,CAAC;CACD,SAAS,OAAO;AACjB,CAAC,CAAC,CAAC,CAAC"}
|
package/door.d.mts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { HttpRouter, HttpServerRequest } from "effect/http";
|
|
3
|
+
declare namespace door_d_exports {
|
|
4
|
+
export { Admit, door_d_exports as Door, isToken, layer, make, matches, offered, protocols, token };
|
|
5
|
+
}
|
|
6
|
+
/** The subprotocols a client offers: the fixed name, then its token; without one it is refused. */
|
|
7
|
+
export declare const protocols: (token: string | undefined) => Array<string>;
|
|
8
|
+
/** A token as a subprotocol can carry it: base64url, which is what {@link make} gives. */
|
|
9
|
+
export declare const isToken: (value: string) => boolean;
|
|
10
|
+
/** A fresh token: 32 random bytes. */
|
|
11
|
+
export declare const make: Effect.Effect<string, never, never>;
|
|
12
|
+
/** The token a `Sec-WebSocket-Protocol` header offers after the fixed name, and nothing else; none otherwise. */
|
|
13
|
+
export declare const offered: (header: string | undefined) => string | undefined;
|
|
14
|
+
/** Whether `given` is `expected`, in a time that depends on `expected`'s length alone. */
|
|
15
|
+
export declare const matches: (given: string, expected: string) => boolean;
|
|
16
|
+
/**
|
|
17
|
+
* How a listener judges the credential a request offers, if any: it lets the request in as it came
|
|
18
|
+
* or changed for the routes behind (a header they read), or turns it away with 401 by answering none.
|
|
19
|
+
*/
|
|
20
|
+
export type Admit = (offered: string | undefined, request: HttpServerRequest.HttpServerRequest) => Effect.Effect<HttpServerRequest.HttpServerRequest | undefined>;
|
|
21
|
+
/** Admits a request that offers `expected`, and nothing else. */
|
|
22
|
+
export declare const token: (expected: string) => Admit;
|
|
23
|
+
/**
|
|
24
|
+
* The door on every request the listener serves, before a WebSocket upgrade: a refused one gets a
|
|
25
|
+
* plain HTTP status instead of 101, 403 for where it comes from and 401 for its credential.
|
|
26
|
+
*/
|
|
27
|
+
export declare const layer: (options: {
|
|
28
|
+
readonly admit: Admit;
|
|
29
|
+
readonly origins: ReadonlyArray<string>;
|
|
30
|
+
}) => import("effect/Layer").Layer<never, never, HttpRouter.HttpRouter>;
|
|
31
|
+
//#endregion
|
|
32
|
+
export { door_d_exports as Door };
|
|
33
|
+
//# sourceMappingURL=door.d.mts.map
|
package/door.mjs
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
|
|
2
|
+
import { Effect } from "effect";
|
|
3
|
+
import { Base64Url } from "effect/encoding";
|
|
4
|
+
import { HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/http";
|
|
5
|
+
//#region src/door.ts
|
|
6
|
+
var door_exports = /* @__PURE__ */ __exportAll({
|
|
7
|
+
Door: () => door_exports,
|
|
8
|
+
isToken: () => isToken,
|
|
9
|
+
layer: () => layer,
|
|
10
|
+
make: () => make,
|
|
11
|
+
matches: () => matches,
|
|
12
|
+
offered: () => offered,
|
|
13
|
+
protocols: () => protocols,
|
|
14
|
+
token: () => token
|
|
15
|
+
});
|
|
16
|
+
/** The subprotocol every client offers first, and the one a listener selects. */
|
|
17
|
+
const protocol = "codework";
|
|
18
|
+
/** The subprotocols a client offers: the fixed name, then its token; without one it is refused. */
|
|
19
|
+
const protocols = (token) => token === void 0 ? [protocol] : [protocol, token];
|
|
20
|
+
/** A token as a subprotocol can carry it: base64url, which is what {@link make} gives. */
|
|
21
|
+
const isToken = (value) => /^[A-Za-z0-9_-]+$/.test(value);
|
|
22
|
+
/** A fresh token: 32 random bytes. */
|
|
23
|
+
const make = Effect.sync(() => Base64Url.encode(crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(32))));
|
|
24
|
+
/** The token a `Sec-WebSocket-Protocol` header offers after the fixed name, and nothing else; none otherwise. */
|
|
25
|
+
const offered = (header) => {
|
|
26
|
+
const [name, token, ...rest] = (header ?? "").split(",").map((part) => part.trim());
|
|
27
|
+
return name === protocol && token !== void 0 && isToken(token) && rest.length === 0 ? token : void 0;
|
|
28
|
+
};
|
|
29
|
+
/** Whether `given` is `expected`, in a time that depends on `expected`'s length alone. */
|
|
30
|
+
const matches = (given, expected) => {
|
|
31
|
+
let difference = given.length ^ expected.length;
|
|
32
|
+
for (let index = 0; index < expected.length; index++) difference |= expected.charCodeAt(index) ^ (given.charCodeAt(index) | 0);
|
|
33
|
+
return difference === 0;
|
|
34
|
+
};
|
|
35
|
+
/** Admits a request that offers `expected`, and nothing else. */
|
|
36
|
+
const token = (expected) => (given, request) => Effect.succeed(given !== void 0 && matches(given, expected) ? request : void 0);
|
|
37
|
+
/**
|
|
38
|
+
* The door on every request the listener serves, before a WebSocket upgrade: a refused one gets a
|
|
39
|
+
* plain HTTP status instead of 101, 403 for where it comes from and 401 for its credential.
|
|
40
|
+
*/
|
|
41
|
+
const layer = (options) => HttpRouter.middleware((app) => Effect.gen(function* () {
|
|
42
|
+
const request = yield* HttpServerRequest.HttpServerRequest;
|
|
43
|
+
const origin = request.headers.origin;
|
|
44
|
+
if (origin !== void 0 && !options.origins.includes(origin)) return HttpServerResponse.empty({ status: 403 });
|
|
45
|
+
const admitted = yield* options.admit(offered(request.headers["sec-websocket-protocol"]), request);
|
|
46
|
+
if (admitted === void 0) return HttpServerResponse.empty({ status: 401 });
|
|
47
|
+
return yield* Effect.provideService(app, HttpServerRequest.HttpServerRequest, admitted);
|
|
48
|
+
}), { global: true });
|
|
49
|
+
//#endregion
|
|
50
|
+
export { door_exports as Door, isToken, layer, make, matches, offered, protocols, token };
|
|
51
|
+
|
|
52
|
+
//# sourceMappingURL=door.mjs.map
|
package/door.mjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"door.mjs","names":[],"sources":["../../src/door.ts"],"sourcesContent":["import { Effect } from \"effect\";\nimport { Base64Url } from \"effect/encoding\";\nimport { HttpRouter, HttpServerRequest, HttpServerResponse } from \"effect/http\";\n\n// Who may open a CodeWork listener's WebSocket. A browser WebSocket cannot set headers, so the token\n// travels as the second offered subprotocol, after the fixed name the listener selects: `ws` answers\n// with the first one offered, so the token is never echoed back.\n\n/** The subprotocol every client offers first, and the one a listener selects. */\nconst protocol = \"codework\";\n\n/** The subprotocols a client offers: the fixed name, then its token; without one it is refused. */\nexport const protocols = (token: string | undefined): Array<string> =>\n\ttoken === undefined ? [protocol] : [protocol, token];\n\n/** A token as a subprotocol can carry it: base64url, which is what {@link make} gives. */\nexport const isToken = (value: string) => /^[A-Za-z0-9_-]+$/.test(value);\n\n/** A fresh token: 32 random bytes. */\nexport const make = Effect.sync(() => Base64Url.encode(crypto.getRandomValues(new Uint8Array(32))));\n\n/** The token a `Sec-WebSocket-Protocol` header offers after the fixed name, and nothing else; none otherwise. */\nexport const offered = (header: string | undefined): string | undefined => {\n\tconst [name, token, ...rest] = (header ?? \"\").split(\",\").map((part) => part.trim());\n\treturn name === protocol && token !== undefined && isToken(token) && rest.length === 0 ? token : undefined;\n};\n\n/** Whether `given` is `expected`, in a time that depends on `expected`'s length alone. */\nexport const matches = (given: string, expected: string) => {\n\tlet difference = given.length ^ expected.length;\n\t// Past its end `given` reads NaN, which `| 0` turns into 0.\n\tfor (let index = 0; index < expected.length; index++)\n\t\tdifference |= expected.charCodeAt(index) ^ (given.charCodeAt(index) | 0);\n\treturn difference === 0;\n};\n\n/**\n * How a listener judges the credential a request offers, if any: it lets the request in as it came\n * or changed for the routes behind (a header they read), or turns it away with 401 by answering none.\n */\nexport type Admit = (\n\toffered: string | undefined,\n\trequest: HttpServerRequest.HttpServerRequest,\n) => Effect.Effect<HttpServerRequest.HttpServerRequest | undefined>;\n\n/** Admits a request that offers `expected`, and nothing else. */\nexport const token =\n\t(expected: string): Admit =>\n\t(given, request) =>\n\t\tEffect.succeed(given !== undefined && matches(given, expected) ? request : undefined);\n\n/**\n * The door on every request the listener serves, before a WebSocket upgrade: a refused one gets a\n * plain HTTP status instead of 101, 403 for where it comes from and 401 for its credential.\n */\nexport const layer = (options: { readonly admit: Admit; readonly origins: ReadonlyArray<string> }) =>\n\tHttpRouter.middleware(\n\t\t(app) =>\n\t\t\tEffect.gen(function* () {\n\t\t\t\tconst request = yield* HttpServerRequest.HttpServerRequest;\n\t\t\t\tconst origin = request.headers.origin;\n\t\t\t\tif (origin !== undefined && !options.origins.includes(origin))\n\t\t\t\t\treturn HttpServerResponse.empty({ status: 403 });\n\t\t\t\tconst admitted = yield* options.admit(offered(request.headers[\"sec-websocket-protocol\"]), request);\n\t\t\t\tif (admitted === undefined) return HttpServerResponse.empty({ status: 401 });\n\t\t\t\treturn yield* Effect.provideService(app, HttpServerRequest.HttpServerRequest, admitted);\n\t\t\t}),\n\t\t{ global: true },\n\t);\n\nexport * as Door from \"./door.ts\";\n"],"mappings":";;;;;;;;;;;;;;;;AASA,MAAM,WAAW;;AAGjB,MAAa,aAAa,UACzB,UAAU,KAAA,IAAY,CAAC,QAAQ,IAAI,CAAC,UAAU,KAAK;;AAGpD,MAAa,WAAW,UAAkB,mBAAmB,KAAK,KAAK;;AAGvE,MAAa,OAAO,OAAO,WAAW,UAAU,OAAO,OAAO,gCAAgB,IAAI,WAAW,EAAE,CAAC,CAAC,CAAC;;AAGlG,MAAa,WAAW,WAAmD;CAC1E,MAAM,CAAC,MAAM,OAAO,GAAG,SAAS,UAAU,GAAA,CAAI,MAAM,GAAG,CAAC,CAAC,KAAK,SAAS,KAAK,KAAK,CAAC;CAClF,OAAO,SAAS,YAAY,UAAU,KAAA,KAAa,QAAQ,KAAK,KAAK,KAAK,WAAW,IAAI,QAAQ,KAAA;AAClG;;AAGA,MAAa,WAAW,OAAe,aAAqB;CAC3D,IAAI,aAAa,MAAM,SAAS,SAAS;CAEzC,KAAK,IAAI,QAAQ,GAAG,QAAQ,SAAS,QAAQ,SAC5C,cAAc,SAAS,WAAW,KAAK,KAAK,MAAM,WAAW,KAAK,IAAI;CACvE,OAAO,eAAe;AACvB;;AAYA,MAAa,SACX,cACA,OAAO,YACP,OAAO,QAAQ,UAAU,KAAA,KAAa,QAAQ,OAAO,QAAQ,IAAI,UAAU,KAAA,CAAS;;;;;AAMtF,MAAa,SAAS,YACrB,WAAW,YACT,QACA,OAAO,IAAI,aAAa;CACvB,MAAM,UAAU,OAAO,kBAAkB;CACzC,MAAM,SAAS,QAAQ,QAAQ;CAC/B,IAAI,WAAW,KAAA,KAAa,CAAC,QAAQ,QAAQ,SAAS,MAAM,GAC3D,OAAO,mBAAmB,MAAM,EAAE,QAAQ,IAAI,CAAC;CAChD,MAAM,WAAW,OAAO,QAAQ,MAAM,QAAQ,QAAQ,QAAQ,yBAAyB,GAAG,OAAO;CACjG,IAAI,aAAa,KAAA,GAAW,OAAO,mBAAmB,MAAM,EAAE,QAAQ,IAAI,CAAC;CAC3E,OAAO,OAAO,OAAO,eAAe,KAAK,kBAAkB,mBAAmB,QAAQ;AACvF,CAAC,GACF,EAAE,QAAQ,KAAK,CAChB"}
|
package/event.d.mts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
declare namespace event_d_exports {
|
|
3
|
+
export { Data, Definition, event_d_exports as EventSchema, ID, Payload, PublishOptions, SubscriptionOverflowError, define };
|
|
4
|
+
}
|
|
5
|
+
export declare const ID: Schema.brand<Schema.String, "Event.ID"> & {
|
|
6
|
+
create: () => string & import("effect/Brand").Brand<"Event.ID">;
|
|
7
|
+
};
|
|
8
|
+
export type ID = typeof ID.Type;
|
|
9
|
+
export type Definition<Type extends string = string, DataSchema extends Schema.Codec<unknown, unknown> = Schema.Codec<unknown, unknown>> = Schema.Top & {
|
|
10
|
+
readonly type: Type;
|
|
11
|
+
readonly durable?: {
|
|
12
|
+
readonly version: number;
|
|
13
|
+
readonly aggregate: string;
|
|
14
|
+
};
|
|
15
|
+
readonly data: DataSchema;
|
|
16
|
+
};
|
|
17
|
+
export type Data<D extends Definition> = Schema.Schema.Type<D["data"]>;
|
|
18
|
+
/** Publish-time options. Lives beside the definition so the plugin-facing publisher can name it. */
|
|
19
|
+
export interface PublishOptions {
|
|
20
|
+
/** Caller-supplied event ID. Publishing the same ID twice is a defect, not an upsert. */
|
|
21
|
+
readonly id?: ID;
|
|
22
|
+
/**
|
|
23
|
+
* Publish-time context — correlation, tracing — carried to projectors on the
|
|
24
|
+
* in-memory payload and deliberately not stored in the event row. A projector
|
|
25
|
+
* persists it into its own projection if it means something there; the log
|
|
26
|
+
* records what happened, not who asked. It therefore does not survive a
|
|
27
|
+
* durable reread or a rebuild, which `event.test.ts` pins explicitly.
|
|
28
|
+
*/
|
|
29
|
+
readonly metadata?: Record<string, string>;
|
|
30
|
+
}
|
|
31
|
+
export type Payload<D extends Definition = Definition> = {
|
|
32
|
+
readonly id: ID;
|
|
33
|
+
readonly type: D["type"];
|
|
34
|
+
readonly data: Data<D>;
|
|
35
|
+
readonly durable?: {
|
|
36
|
+
readonly aggregateId: string;
|
|
37
|
+
readonly seq: number;
|
|
38
|
+
readonly version: number;
|
|
39
|
+
};
|
|
40
|
+
readonly metadata?: Record<string, string>;
|
|
41
|
+
};
|
|
42
|
+
export declare function define<const Type extends string, const Fields extends Readonly<Record<PropertyKey, Schema.Codec<unknown, unknown>>>>(input: {
|
|
43
|
+
readonly type: Type;
|
|
44
|
+
readonly durable?: {
|
|
45
|
+
readonly version: number;
|
|
46
|
+
readonly aggregate: string;
|
|
47
|
+
};
|
|
48
|
+
readonly schema: Fields;
|
|
49
|
+
}): Schema.Struct<{
|
|
50
|
+
readonly id: Schema.brand<Schema.String, "Event.ID"> & {
|
|
51
|
+
create: () => string & import("effect/Brand").Brand<"Event.ID">;
|
|
52
|
+
};
|
|
53
|
+
readonly metadata: Schema.decodeTo<Schema.optional<Schema.toType<Schema.$Record<Schema.String, Schema.String>>>, Schema.optionalKey<Schema.$Record<Schema.String, Schema.String>>, never, never>;
|
|
54
|
+
readonly type: Schema.Literal<Type>;
|
|
55
|
+
readonly durable: Schema.decodeTo<Schema.optional<Schema.toType<Schema.Struct<{
|
|
56
|
+
readonly aggregateId: Schema.String;
|
|
57
|
+
readonly seq: Schema.Int;
|
|
58
|
+
readonly version: Schema.Int;
|
|
59
|
+
}>>>, Schema.optionalKey<Schema.Struct<{
|
|
60
|
+
readonly aggregateId: Schema.String;
|
|
61
|
+
readonly seq: Schema.Int;
|
|
62
|
+
readonly version: Schema.Int;
|
|
63
|
+
}>>, never, never>;
|
|
64
|
+
readonly data: Schema.Struct<Fields>;
|
|
65
|
+
}> & {
|
|
66
|
+
type: Type;
|
|
67
|
+
durable?: {
|
|
68
|
+
readonly version: number;
|
|
69
|
+
readonly aggregate: string;
|
|
70
|
+
};
|
|
71
|
+
data: Schema.Struct<Fields>;
|
|
72
|
+
};
|
|
73
|
+
declare const SubscriptionOverflowError_base: Schema.Class<SubscriptionOverflowError, Schema.TaggedStruct<"SubscriptionOverflowError", {
|
|
74
|
+
readonly capacity: Schema.Int;
|
|
75
|
+
}>, import("effect/Cause").YieldableError>;
|
|
76
|
+
export declare class SubscriptionOverflowError extends SubscriptionOverflowError_base {}
|
|
77
|
+
//#endregion
|
|
78
|
+
export { event_d_exports as EventSchema };
|
|
79
|
+
//# sourceMappingURL=event.d.mts.map
|
package/event.mjs
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
|
|
2
|
+
import { optional, withStatics } from "./common.mjs";
|
|
3
|
+
import { Schema } from "effect";
|
|
4
|
+
import { uuidv7 } from "uuidv7";
|
|
5
|
+
//#region src/event.ts
|
|
6
|
+
var event_exports = /* @__PURE__ */ __exportAll({
|
|
7
|
+
EventSchema: () => event_exports,
|
|
8
|
+
ID: () => ID,
|
|
9
|
+
SubscriptionOverflowError: () => SubscriptionOverflowError,
|
|
10
|
+
define: () => define
|
|
11
|
+
});
|
|
12
|
+
const ID = Schema.String.pipe(Schema.brand("Event.ID"), withStatics((schema) => ({ create: () => schema.make(`evt_${uuidv7()}`) })));
|
|
13
|
+
function define(input) {
|
|
14
|
+
const data = Schema.Struct(input.schema);
|
|
15
|
+
return Schema.Struct({
|
|
16
|
+
id: ID,
|
|
17
|
+
metadata: optional(Schema.Record(Schema.String, Schema.String)),
|
|
18
|
+
type: Schema.Literal(input.type),
|
|
19
|
+
durable: optional(Schema.Struct({
|
|
20
|
+
aggregateId: Schema.String,
|
|
21
|
+
seq: Schema.Int,
|
|
22
|
+
version: Schema.Int
|
|
23
|
+
})),
|
|
24
|
+
data
|
|
25
|
+
}).annotate({ identifier: input.type }).pipe(withStatics(() => ({
|
|
26
|
+
type: input.type,
|
|
27
|
+
...input.durable === void 0 ? {} : { durable: input.durable },
|
|
28
|
+
data
|
|
29
|
+
})));
|
|
30
|
+
}
|
|
31
|
+
var SubscriptionOverflowError = class extends Schema.TaggedError()("SubscriptionOverflowError", { capacity: Schema.Int }) {};
|
|
32
|
+
//#endregion
|
|
33
|
+
export { event_exports as EventSchema, ID, SubscriptionOverflowError, define };
|
|
34
|
+
|
|
35
|
+
//# sourceMappingURL=event.mjs.map
|
package/event.mjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"event.mjs","names":[],"sources":["../../src/event.ts"],"sourcesContent":["import { Schema } from \"effect\";\nimport { uuidv7 } from \"uuidv7\";\nimport { optional, withStatics } from \"./common.ts\";\n\nexport const ID = Schema.String.pipe(\n\tSchema.brand(\"Event.ID\"),\n\twithStatics((schema) => ({\n\t\tcreate: () => schema.make(`evt_${uuidv7()}`),\n\t})),\n);\nexport type ID = typeof ID.Type;\n\nexport type Definition<\n\tType extends string = string,\n\tDataSchema extends Schema.Codec<unknown, unknown> = Schema.Codec<unknown, unknown>,\n> = Schema.Top & {\n\treadonly type: Type;\n\treadonly durable?: {\n\t\treadonly version: number; // event versioning for decoding in later time\n\t\treadonly aggregate: string;\n\t};\n\treadonly data: DataSchema;\n};\n\nexport type Data<D extends Definition> = Schema.Schema.Type<D[\"data\"]>;\n\n/** Publish-time options. Lives beside the definition so the plugin-facing publisher can name it. */\nexport interface PublishOptions {\n\t/** Caller-supplied event ID. Publishing the same ID twice is a defect, not an upsert. */\n\treadonly id?: ID;\n\t/**\n\t * Publish-time context — correlation, tracing — carried to projectors on the\n\t * in-memory payload and deliberately not stored in the event row. A projector\n\t * persists it into its own projection if it means something there; the log\n\t * records what happened, not who asked. It therefore does not survive a\n\t * durable reread or a rebuild, which `event.test.ts` pins explicitly.\n\t */\n\treadonly metadata?: Record<string, string>;\n}\n\nexport type Payload<D extends Definition = Definition> = {\n\treadonly id: ID;\n\treadonly type: D[\"type\"];\n\treadonly data: Data<D>;\n\treadonly durable?: {\n\t\treadonly aggregateId: string;\n\t\treadonly seq: number;\n\t\treadonly version: number;\n\t};\n\treadonly metadata?: Record<string, string>;\n};\n\nexport function define<\n\tconst Type extends string,\n\tconst Fields extends Readonly<Record<PropertyKey, Schema.Codec<unknown, unknown>>>,\n>(input: {\n\treadonly type: Type;\n\treadonly durable?: {\n\t\treadonly version: number;\n\t\treadonly aggregate: string;\n\t};\n\treadonly schema: Fields;\n}) {\n\tconst data = Schema.Struct(input.schema);\n\treturn Schema.Struct({\n\t\tid: ID,\n\t\tmetadata: optional(Schema.Record(Schema.String, Schema.String)),\n\t\ttype: Schema.Literal(input.type),\n\t\tdurable: optional(Schema.Struct({ aggregateId: Schema.String, seq: Schema.Int, version: Schema.Int })),\n\t\tdata,\n\t})\n\t\t.annotate({ identifier: input.type })\n\t\t.pipe(\n\t\t\twithStatics(() => ({\n\t\t\t\ttype: input.type,\n\t\t\t\t...(input.durable === undefined ? {} : { durable: input.durable }),\n\t\t\t\tdata,\n\t\t\t})),\n\t\t) satisfies Definition<Type, typeof data>;\n}\n\nexport class SubscriptionOverflowError extends Schema.TaggedError<SubscriptionOverflowError>()(\n\t\"SubscriptionOverflowError\",\n\t{ capacity: Schema.Int },\n) {}\n\nexport * as EventSchema from \"./event.ts\";\n"],"mappings":";;;;;;;;;;;AAIA,MAAa,KAAK,OAAO,OAAO,KAC/B,OAAO,MAAM,UAAU,GACvB,aAAa,YAAY,EACxB,cAAc,OAAO,KAAK,OAAO,OAAO,GAAG,EAC5C,EAAE,CACH;AA2CA,SAAgB,OAGd,OAOC;CACF,MAAM,OAAO,OAAO,OAAO,MAAM,MAAM;CACvC,OAAO,OAAO,OAAO;EACpB,IAAI;EACJ,UAAU,SAAS,OAAO,OAAO,OAAO,QAAQ,OAAO,MAAM,CAAC;EAC9D,MAAM,OAAO,QAAQ,MAAM,IAAI;EAC/B,SAAS,SAAS,OAAO,OAAO;GAAE,aAAa,OAAO;GAAQ,KAAK,OAAO;GAAK,SAAS,OAAO;EAAI,CAAC,CAAC;EACrG;CACD,CAAC,CAAC,CACA,SAAS,EAAE,YAAY,MAAM,KAAK,CAAC,CAAC,CACpC,KACA,mBAAmB;EAClB,MAAM,MAAM;EACZ,GAAI,MAAM,YAAY,KAAA,IAAY,CAAC,IAAI,EAAE,SAAS,MAAM,QAAQ;EAChE;CACD,EAAE,CACH;AACF;AAEA,IAAa,4BAAb,cAA+C,OAAO,YAAuC,CAAC,CAC7F,6BACA,EAAE,UAAU,OAAO,IAAI,CACxB,CAAC,CAAC,CAAC"}
|
package/forwarded.d.mts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { HttpServerRequest } from "effect/http";
|
|
2
|
+
import * as IpNetwork from "effect/net/IpNetwork";
|
|
3
|
+
import * as NetAddress from "effect/net/NetAddress";
|
|
4
|
+
declare namespace forwarded_d_exports {
|
|
5
|
+
export { forwarded_d_exports as Forwarded, encrypted, peer };
|
|
6
|
+
}
|
|
7
|
+
/** The address `request` came from, by its socket, canonical; none when it has none. */
|
|
8
|
+
export declare const peer: (request: HttpServerRequest.HttpServerRequest) => NetAddress.IpAddress | undefined;
|
|
9
|
+
/** Whether `request` reached the proxy in front of this listener over TLS, by a proxy on loopback or in `trusted`. */
|
|
10
|
+
export declare const encrypted: (request: HttpServerRequest.HttpServerRequest, trusted: ReadonlyArray<IpNetwork.IpNetwork>) => boolean;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { forwarded_d_exports as Forwarded };
|
|
13
|
+
//# sourceMappingURL=forwarded.d.mts.map
|
package/forwarded.mjs
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
|
|
2
|
+
import { Option, Result } from "effect";
|
|
3
|
+
import * as IpNetwork from "effect/net/IpNetwork";
|
|
4
|
+
import * as NetAddress from "effect/net/NetAddress";
|
|
5
|
+
//#region src/forwarded.ts
|
|
6
|
+
var forwarded_exports = /* @__PURE__ */ __exportAll({
|
|
7
|
+
Forwarded: () => forwarded_exports,
|
|
8
|
+
encrypted: () => encrypted,
|
|
9
|
+
peer: () => peer
|
|
10
|
+
});
|
|
11
|
+
/** The address `request` came from, by its socket, canonical; none when it has none. */
|
|
12
|
+
const peer = (request) => Option.match(request.remoteAddress, {
|
|
13
|
+
onNone: () => void 0,
|
|
14
|
+
onSome: (address) => Result.getOrUndefined(Result.map(NetAddress.ipFromString(address), (ip) => NetAddress.toCanonical(ip)))
|
|
15
|
+
});
|
|
16
|
+
/** Whether `request` reached the proxy in front of this listener over TLS, by a proxy on loopback or in `trusted`. */
|
|
17
|
+
const encrypted = (request, trusted) => {
|
|
18
|
+
const from = peer(request);
|
|
19
|
+
if (from === void 0 || !(NetAddress.isLoopback(from) || trusted.some((network) => IpNetwork.contains(network, from)))) return false;
|
|
20
|
+
return request.headers["x-forwarded-proto"]?.split(",")[0]?.trim().toLowerCase() === "https";
|
|
21
|
+
};
|
|
22
|
+
//#endregion
|
|
23
|
+
export { forwarded_exports as Forwarded, encrypted, peer };
|
|
24
|
+
|
|
25
|
+
//# sourceMappingURL=forwarded.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"forwarded.mjs","names":[],"sources":["../../src/forwarded.ts"],"sourcesContent":["import { Option, Result } from \"effect\";\nimport type { HttpServerRequest } from \"effect/http\";\nimport * as IpNetwork from \"effect/net/IpNetwork\";\nimport * as NetAddress from \"effect/net/NetAddress\";\n\n// A listener speaks plain HTTP; TLS ends at a proxy in front of it, which says so in\n// `X-Forwarded-Proto`. Any client can send that header, so only a proxy's is believed: a peer on\n// loopback, or in the trusted list.\n\n/** The address `request` came from, by its socket, canonical; none when it has none. */\nexport const peer = (request: HttpServerRequest.HttpServerRequest): NetAddress.IpAddress | undefined =>\n\tOption.match(request.remoteAddress, {\n\t\tonNone: () => undefined,\n\t\tonSome: (address) =>\n\t\t\tResult.getOrUndefined(Result.map(NetAddress.ipFromString(address), (ip) => NetAddress.toCanonical(ip))),\n\t});\n\n/** Whether `request` reached the proxy in front of this listener over TLS, by a proxy on loopback or in `trusted`. */\nexport const encrypted = (\n\trequest: HttpServerRequest.HttpServerRequest,\n\ttrusted: ReadonlyArray<IpNetwork.IpNetwork>,\n): boolean => {\n\tconst from = peer(request);\n\tif (\n\t\tfrom === undefined ||\n\t\t!(NetAddress.isLoopback(from) || trusted.some((network) => IpNetwork.contains(network, from)))\n\t)\n\t\treturn false;\n\t// A chain of proxies appends; the first is the one the client reached.\n\treturn request.headers[\"x-forwarded-proto\"]?.split(\",\")[0]?.trim().toLowerCase() === \"https\";\n};\n\nexport * as Forwarded from \"./forwarded.ts\";\n"],"mappings":";;;;;;;;;;;AAUA,MAAa,QAAQ,YACpB,OAAO,MAAM,QAAQ,eAAe;CACnC,cAAc,KAAA;CACd,SAAS,YACR,OAAO,eAAe,OAAO,IAAI,WAAW,aAAa,OAAO,IAAI,OAAO,WAAW,YAAY,EAAE,CAAC,CAAC;AACxG,CAAC;;AAGF,MAAa,aACZ,SACA,YACa;CACb,MAAM,OAAO,KAAK,OAAO;CACzB,IACC,SAAS,KAAA,KACT,EAAE,WAAW,WAAW,IAAI,KAAK,QAAQ,MAAM,YAAY,UAAU,SAAS,SAAS,IAAI,CAAC,IAE5F,OAAO;CAER,OAAO,QAAQ,QAAQ,oBAAoB,EAAE,MAAM,GAAG,CAAC,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC,YAAY,MAAM;AACtF"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
declare namespace envelope_d_exports {
|
|
3
|
+
export { EncodingError, EventEnvelope, envelope_d_exports as HostEnvelope };
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* The wire form of an event payload. `data` is carried in its encoded form — decoded payloads are
|
|
7
|
+
* not wire-safe (`Schema.Duration`, aikit class transforms) — so clients decode it lazily through
|
|
8
|
+
* their own copy of the registry and can skip types they do not know.
|
|
9
|
+
*/
|
|
10
|
+
export declare const EventEnvelope: Schema.Struct<{
|
|
11
|
+
readonly id: Schema.brand<Schema.String, "Event.ID"> & {
|
|
12
|
+
create: () => string & import("effect/Brand").Brand<"Event.ID">;
|
|
13
|
+
};
|
|
14
|
+
readonly type: Schema.String;
|
|
15
|
+
readonly durable: Schema.decodeTo<Schema.optional<Schema.toType<Schema.Struct<{
|
|
16
|
+
readonly aggregateId: Schema.String;
|
|
17
|
+
readonly seq: Schema.Int;
|
|
18
|
+
readonly version: Schema.Int;
|
|
19
|
+
}>>>, Schema.optionalKey<Schema.Struct<{
|
|
20
|
+
readonly aggregateId: Schema.String;
|
|
21
|
+
readonly seq: Schema.Int;
|
|
22
|
+
readonly version: Schema.Int;
|
|
23
|
+
}>>, never, never>;
|
|
24
|
+
readonly metadata: Schema.decodeTo<Schema.optional<Schema.toType<Schema.$Record<Schema.String, Schema.String>>>, Schema.optionalKey<Schema.$Record<Schema.String, Schema.String>>, never, never>;
|
|
25
|
+
readonly data: Schema.Unknown;
|
|
26
|
+
}>;
|
|
27
|
+
export type EventEnvelope = typeof EventEnvelope.Type;
|
|
28
|
+
declare const EncodingError_base: Schema.Class<EncodingError, Schema.TaggedStruct<"EventEncodingError", {
|
|
29
|
+
readonly type: Schema.String;
|
|
30
|
+
readonly cause: Schema.Defect;
|
|
31
|
+
}>, import("effect/Cause").YieldableError>;
|
|
32
|
+
/** The server could not encode an event for the wire; it disconnects every live subscriber. */
|
|
33
|
+
export declare class EncodingError extends EncodingError_base {}
|
|
34
|
+
//#endregion
|
|
35
|
+
export { envelope_d_exports as HostEnvelope };
|
|
36
|
+
//# sourceMappingURL=envelope.d.mts.map
|