effect 4.0.0-beta.44 → 4.0.0-beta.45
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/dist/Equal.d.ts.map +1 -1
- package/dist/Equal.js +16 -0
- package/dist/Equal.js.map +1 -1
- package/dist/Hash.js +1 -1
- package/dist/Hash.js.map +1 -1
- package/dist/Semaphore.d.ts +1 -1
- package/dist/Semaphore.d.ts.map +1 -1
- package/dist/Semaphore.js +1 -3
- package/dist/Semaphore.js.map +1 -1
- package/dist/unstable/ai/McpServer.d.ts.map +1 -1
- package/dist/unstable/ai/McpServer.js +24 -21
- package/dist/unstable/ai/McpServer.js.map +1 -1
- package/dist/unstable/eventlog/Event.d.ts +0 -6
- package/dist/unstable/eventlog/Event.d.ts.map +1 -1
- package/dist/unstable/eventlog/Event.js +0 -5
- package/dist/unstable/eventlog/Event.js.map +1 -1
- package/dist/unstable/eventlog/EventGroup.d.ts +0 -2
- package/dist/unstable/eventlog/EventGroup.d.ts.map +1 -1
- package/dist/unstable/eventlog/EventGroup.js +0 -2
- package/dist/unstable/eventlog/EventGroup.js.map +1 -1
- package/dist/unstable/eventlog/EventJournal.d.ts +22 -5
- package/dist/unstable/eventlog/EventJournal.d.ts.map +1 -1
- package/dist/unstable/eventlog/EventJournal.js +126 -67
- package/dist/unstable/eventlog/EventJournal.js.map +1 -1
- package/dist/unstable/eventlog/EventLog.d.ts +88 -34
- package/dist/unstable/eventlog/EventLog.d.ts.map +1 -1
- package/dist/unstable/eventlog/EventLog.js +215 -141
- package/dist/unstable/eventlog/EventLog.js.map +1 -1
- package/dist/unstable/eventlog/EventLogEncryption.d.ts +9 -7
- package/dist/unstable/eventlog/EventLogEncryption.d.ts.map +1 -1
- package/dist/unstable/eventlog/EventLogEncryption.js +13 -15
- package/dist/unstable/eventlog/EventLogEncryption.js.map +1 -1
- package/dist/unstable/eventlog/EventLogMessage.d.ts +228 -0
- package/dist/unstable/eventlog/EventLogMessage.d.ts.map +1 -0
- package/dist/unstable/eventlog/EventLogMessage.js +214 -0
- package/dist/unstable/eventlog/EventLogMessage.js.map +1 -0
- package/dist/unstable/eventlog/EventLogRemote.d.ts +109 -194
- package/dist/unstable/eventlog/EventLogRemote.d.ts.map +1 -1
- package/dist/unstable/eventlog/EventLogRemote.js +165 -320
- package/dist/unstable/eventlog/EventLogRemote.js.map +1 -1
- package/dist/unstable/eventlog/EventLogServer.d.ts +25 -47
- package/dist/unstable/eventlog/EventLogServer.d.ts.map +1 -1
- package/dist/unstable/eventlog/EventLogServer.js +127 -198
- package/dist/unstable/eventlog/EventLogServer.js.map +1 -1
- package/dist/unstable/eventlog/EventLogServerEncrypted.d.ts +60 -0
- package/dist/unstable/eventlog/EventLogServerEncrypted.d.ts.map +1 -0
- package/dist/unstable/eventlog/EventLogServerEncrypted.js +166 -0
- package/dist/unstable/eventlog/EventLogServerEncrypted.js.map +1 -0
- package/dist/unstable/eventlog/EventLogServerUnencrypted.d.ts +183 -0
- package/dist/unstable/eventlog/EventLogServerUnencrypted.d.ts.map +1 -0
- package/dist/unstable/eventlog/EventLogServerUnencrypted.js +461 -0
- package/dist/unstable/eventlog/EventLogServerUnencrypted.js.map +1 -0
- package/dist/unstable/eventlog/EventLogSessionAuth.d.ts +117 -0
- package/dist/unstable/eventlog/EventLogSessionAuth.d.ts.map +1 -0
- package/dist/unstable/eventlog/EventLogSessionAuth.js +284 -0
- package/dist/unstable/eventlog/EventLogSessionAuth.js.map +1 -0
- package/dist/unstable/eventlog/{SqlEventLogJournal.d.ts → SqlEventJournal.d.ts} +2 -2
- package/dist/unstable/eventlog/SqlEventJournal.d.ts.map +1 -0
- package/dist/unstable/eventlog/{SqlEventLogJournal.js → SqlEventJournal.js} +20 -14
- package/dist/unstable/eventlog/SqlEventJournal.js.map +1 -0
- package/dist/unstable/eventlog/{SqlEventLogServer.d.ts → SqlEventLogServerEncrypted.d.ts} +5 -5
- package/dist/unstable/eventlog/SqlEventLogServerEncrypted.d.ts.map +1 -0
- package/dist/unstable/eventlog/{SqlEventLogServer.js → SqlEventLogServerEncrypted.js} +65 -24
- package/dist/unstable/eventlog/SqlEventLogServerEncrypted.js.map +1 -0
- package/dist/unstable/eventlog/SqlEventLogServerUnencrypted.d.ts +25 -0
- package/dist/unstable/eventlog/SqlEventLogServerUnencrypted.d.ts.map +1 -0
- package/dist/unstable/eventlog/SqlEventLogServerUnencrypted.js +354 -0
- package/dist/unstable/eventlog/SqlEventLogServerUnencrypted.js.map +1 -0
- package/dist/unstable/eventlog/index.d.ts +22 -2
- package/dist/unstable/eventlog/index.d.ts.map +1 -1
- package/dist/unstable/eventlog/index.js +22 -2
- package/dist/unstable/eventlog/index.js.map +1 -1
- package/dist/unstable/eventlog/internal/identityRootSecretDerivation.d.ts +2 -0
- package/dist/unstable/eventlog/internal/identityRootSecretDerivation.d.ts.map +1 -0
- package/dist/unstable/eventlog/internal/identityRootSecretDerivation.js +89 -0
- package/dist/unstable/eventlog/internal/identityRootSecretDerivation.js.map +1 -0
- package/dist/unstable/reactivity/AtomHttpApi.d.ts +1 -2
- package/dist/unstable/reactivity/AtomHttpApi.d.ts.map +1 -1
- package/dist/unstable/reactivity/AtomHttpApi.js +2 -2
- package/dist/unstable/reactivity/AtomHttpApi.js.map +1 -1
- package/dist/unstable/reactivity/AtomRpc.d.ts +1 -2
- package/dist/unstable/reactivity/AtomRpc.d.ts.map +1 -1
- package/dist/unstable/reactivity/AtomRpc.js +3 -3
- package/dist/unstable/reactivity/AtomRpc.js.map +1 -1
- package/dist/unstable/rpc/Rpc.d.ts +25 -4
- package/dist/unstable/rpc/Rpc.d.ts.map +1 -1
- package/dist/unstable/rpc/Rpc.js +26 -0
- package/dist/unstable/rpc/Rpc.js.map +1 -1
- package/dist/unstable/rpc/RpcClient.d.ts +3 -13
- package/dist/unstable/rpc/RpcClient.d.ts.map +1 -1
- package/dist/unstable/rpc/RpcClient.js +47 -23
- package/dist/unstable/rpc/RpcClient.js.map +1 -1
- package/dist/unstable/rpc/RpcGroup.d.ts +1 -1
- package/dist/unstable/rpc/RpcGroup.d.ts.map +1 -1
- package/dist/unstable/rpc/RpcMiddleware.d.ts +2 -2
- package/dist/unstable/rpc/RpcMiddleware.d.ts.map +1 -1
- package/dist/unstable/rpc/RpcServer.d.ts.map +1 -1
- package/dist/unstable/rpc/RpcServer.js +3 -2
- package/dist/unstable/rpc/RpcServer.js.map +1 -1
- package/dist/unstable/rpc/Utils.d.ts +6 -0
- package/dist/unstable/rpc/Utils.d.ts.map +1 -1
- package/dist/unstable/rpc/Utils.js +44 -0
- package/dist/unstable/rpc/Utils.js.map +1 -1
- package/dist/unstable/schema/Model.d.ts +2 -2
- package/dist/unstable/schema/Model.d.ts.map +1 -1
- package/dist/unstable/schema/Model.js +2 -4
- package/dist/unstable/schema/Model.js.map +1 -1
- package/dist/unstable/schema/VariantSchema.d.ts +1 -1
- package/dist/unstable/schema/VariantSchema.d.ts.map +1 -1
- package/dist/unstable/schema/VariantSchema.js +1 -12
- package/dist/unstable/schema/VariantSchema.js.map +1 -1
- package/dist/unstable/workers/Transferable.d.ts +1 -1
- package/dist/unstable/workers/Transferable.d.ts.map +1 -1
- package/dist/unstable/workers/Transferable.js +1 -1
- package/dist/unstable/workers/Transferable.js.map +1 -1
- package/package.json +1 -1
- package/src/Equal.ts +17 -0
- package/src/Hash.ts +2 -2
- package/src/Semaphore.ts +2 -4
- package/src/unstable/ai/McpServer.ts +24 -22
- package/src/unstable/eventlog/Event.ts +0 -8
- package/src/unstable/eventlog/EventGroup.ts +0 -4
- package/src/unstable/eventlog/EventJournal.ts +144 -76
- package/src/unstable/eventlog/EventLog.ts +342 -221
- package/src/unstable/eventlog/EventLogEncryption.ts +16 -30
- package/src/unstable/eventlog/EventLogMessage.ts +277 -0
- package/src/unstable/eventlog/EventLogRemote.ts +261 -408
- package/src/unstable/eventlog/EventLogServer.ts +182 -274
- package/src/unstable/eventlog/EventLogServerEncrypted.ts +206 -0
- package/src/unstable/eventlog/EventLogServerUnencrypted.ts +749 -0
- package/src/unstable/eventlog/EventLogSessionAuth.ts +437 -0
- package/src/unstable/eventlog/{SqlEventLogJournal.ts → SqlEventJournal.ts} +26 -18
- package/src/unstable/eventlog/{SqlEventLogServer.ts → SqlEventLogServerEncrypted.ts} +102 -40
- package/src/unstable/eventlog/SqlEventLogServerUnencrypted.ts +500 -0
- package/src/unstable/eventlog/index.ts +27 -2
- package/src/unstable/eventlog/internal/identityRootSecretDerivation.ts +153 -0
- package/src/unstable/reactivity/AtomHttpApi.ts +23 -8
- package/src/unstable/reactivity/AtomRpc.ts +16 -5
- package/src/unstable/rpc/Rpc.ts +42 -4
- package/src/unstable/rpc/RpcClient.ts +59 -24
- package/src/unstable/rpc/RpcGroup.ts +1 -1
- package/src/unstable/rpc/RpcMiddleware.ts +2 -2
- package/src/unstable/rpc/RpcServer.ts +5 -3
- package/src/unstable/rpc/Utils.ts +59 -0
- package/src/unstable/schema/Model.ts +4 -6
- package/src/unstable/schema/VariantSchema.ts +4 -17
- package/src/unstable/workers/Transferable.ts +9 -11
- package/dist/unstable/eventlog/SqlEventLogJournal.d.ts.map +0 -1
- package/dist/unstable/eventlog/SqlEventLogJournal.js.map +0 -1
- package/dist/unstable/eventlog/SqlEventLogServer.d.ts.map +0 -1
- package/dist/unstable/eventlog/SqlEventLogServer.js.map +0 -1
|
@@ -6,8 +6,10 @@ import * as Effect from "../../Effect.ts"
|
|
|
6
6
|
import * as Layer from "../../Layer.ts"
|
|
7
7
|
import * as Redacted from "../../Redacted.ts"
|
|
8
8
|
import * as Schema from "../../Schema.ts"
|
|
9
|
+
import * as Transferable from "../workers/Transferable.ts"
|
|
9
10
|
import { Entry, EntryId, RemoteEntry } from "./EventJournal.ts"
|
|
10
11
|
import type { Identity } from "./EventLog.ts"
|
|
12
|
+
import { makeGetIdentityRootSecretMaterial } from "./internal/identityRootSecretDerivation.ts"
|
|
11
13
|
|
|
12
14
|
/**
|
|
13
15
|
* @since 4.0.0
|
|
@@ -15,7 +17,7 @@ import type { Identity } from "./EventLog.ts"
|
|
|
15
17
|
*/
|
|
16
18
|
export const EncryptedEntry = Schema.Struct({
|
|
17
19
|
entryId: EntryId,
|
|
18
|
-
encryptedEntry:
|
|
20
|
+
encryptedEntry: Transferable.Uint8Array
|
|
19
21
|
})
|
|
20
22
|
|
|
21
23
|
/**
|
|
@@ -30,9 +32,9 @@ export interface EncryptedRemoteEntry extends Schema.Schema.Type<typeof Encrypte
|
|
|
30
32
|
*/
|
|
31
33
|
export const EncryptedRemoteEntry = Schema.Struct({
|
|
32
34
|
sequence: Schema.Number,
|
|
33
|
-
iv:
|
|
35
|
+
iv: Transferable.Uint8Array,
|
|
34
36
|
entryId: EntryId,
|
|
35
|
-
encryptedEntry:
|
|
37
|
+
encryptedEntry: Transferable.Uint8Array
|
|
36
38
|
})
|
|
37
39
|
|
|
38
40
|
const toArrayBuffer = (data: Uint8Array): ArrayBuffer => {
|
|
@@ -52,8 +54,8 @@ export class EventLogEncryption extends Context.Service<EventLogEncryption, {
|
|
|
52
54
|
identity: Identity["Service"],
|
|
53
55
|
entries: ReadonlyArray<Entry>
|
|
54
56
|
) => Effect.Effect<{
|
|
55
|
-
readonly iv: Uint8Array
|
|
56
|
-
readonly encryptedEntries: ReadonlyArray<Uint8Array
|
|
57
|
+
readonly iv: Uint8Array<ArrayBuffer>
|
|
58
|
+
readonly encryptedEntries: ReadonlyArray<Uint8Array<ArrayBuffer>>
|
|
57
59
|
}>
|
|
58
60
|
readonly decrypt: (
|
|
59
61
|
identity: Identity["Service"],
|
|
@@ -61,6 +63,7 @@ export class EventLogEncryption extends Context.Service<EventLogEncryption, {
|
|
|
61
63
|
) => Effect.Effect<Array<RemoteEntry>>
|
|
62
64
|
readonly sha256String: (data: Uint8Array) => Effect.Effect<string>
|
|
63
65
|
readonly sha256: (data: Uint8Array) => Effect.Effect<Uint8Array>
|
|
66
|
+
readonly generateIdentity: Effect.Effect<Identity["Service"]>
|
|
64
67
|
}>()("effect/eventlog/EventLogEncryption") {}
|
|
65
68
|
|
|
66
69
|
/**
|
|
@@ -69,33 +72,12 @@ export class EventLogEncryption extends Context.Service<EventLogEncryption, {
|
|
|
69
72
|
*/
|
|
70
73
|
export const makeEncryptionSubtle = (crypto: Crypto): Effect.Effect<EventLogEncryption["Service"]> =>
|
|
71
74
|
Effect.sync(() => {
|
|
72
|
-
const
|
|
73
|
-
const getKey = (identity: Identity["Service"]) =>
|
|
74
|
-
Effect.suspend(() => {
|
|
75
|
-
if (keyCache.has(identity)) {
|
|
76
|
-
return Effect.succeed(keyCache.get(identity)!)
|
|
77
|
-
}
|
|
78
|
-
return Effect.promise(() =>
|
|
79
|
-
crypto.subtle.importKey(
|
|
80
|
-
"raw",
|
|
81
|
-
toArrayBuffer(Redacted.value(identity.privateKey)),
|
|
82
|
-
"AES-GCM",
|
|
83
|
-
true,
|
|
84
|
-
["encrypt", "decrypt"]
|
|
85
|
-
)
|
|
86
|
-
).pipe(
|
|
87
|
-
Effect.tap((key) =>
|
|
88
|
-
Effect.sync(() => {
|
|
89
|
-
keyCache.set(identity, key)
|
|
90
|
-
})
|
|
91
|
-
)
|
|
92
|
-
)
|
|
93
|
-
})
|
|
75
|
+
const getIdentityRootSecretMaterial = makeGetIdentityRootSecretMaterial(crypto)
|
|
94
76
|
|
|
95
77
|
return EventLogEncryption.of({
|
|
96
78
|
encrypt: Effect.fnUntraced(function*(identity, entries) {
|
|
97
79
|
const data = yield* Effect.orDie(Entry.encodeArray(entries))
|
|
98
|
-
const key = yield*
|
|
80
|
+
const key = (yield* getIdentityRootSecretMaterial(identity)).encryptionKey
|
|
99
81
|
const iv = crypto.getRandomValues(new Uint8Array(12))
|
|
100
82
|
const encryptedEntries = yield* Effect.promise(() =>
|
|
101
83
|
Promise.all(
|
|
@@ -114,7 +96,7 @@ export const makeEncryptionSubtle = (crypto: Crypto): Effect.Effect<EventLogEncr
|
|
|
114
96
|
}
|
|
115
97
|
}),
|
|
116
98
|
decrypt: Effect.fnUntraced(function*(identity, entries) {
|
|
117
|
-
const key = yield*
|
|
99
|
+
const key = (yield* getIdentityRootSecretMaterial(identity)).encryptionKey
|
|
118
100
|
const decryptedData = (yield* Effect.promise(() =>
|
|
119
101
|
Promise.all(entries.map((data) =>
|
|
120
102
|
crypto.subtle.decrypt(
|
|
@@ -141,7 +123,11 @@ export const makeEncryptionSubtle = (crypto: Crypto): Effect.Effect<EventLogEncr
|
|
|
141
123
|
.join("")
|
|
142
124
|
return hashHex
|
|
143
125
|
}
|
|
144
|
-
)
|
|
126
|
+
),
|
|
127
|
+
generateIdentity: Effect.sync(() => ({
|
|
128
|
+
publicKey: crypto.randomUUID(),
|
|
129
|
+
privateKey: Redacted.make(crypto.getRandomValues(new Uint8Array(32)))
|
|
130
|
+
}))
|
|
145
131
|
})
|
|
146
132
|
})
|
|
147
133
|
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 4.0.0
|
|
3
|
+
*/
|
|
4
|
+
import type { NonEmptyArray, NonEmptyReadonlyArray } from "../../Array.ts"
|
|
5
|
+
import type { Brand } from "../../Brand.ts"
|
|
6
|
+
import * as Schema from "../../Schema.ts"
|
|
7
|
+
import * as Msgpack from "../encoding/Msgpack.ts"
|
|
8
|
+
import * as Rpc from "../rpc/Rpc.ts"
|
|
9
|
+
import * as RpcGroup from "../rpc/RpcGroup.ts"
|
|
10
|
+
import * as RpcMiddleware from "../rpc/RpcMiddleware.ts"
|
|
11
|
+
import * as Transferable from "../workers/Transferable.ts"
|
|
12
|
+
import { Entry, RemoteEntry, RemoteId } from "./EventJournal.ts"
|
|
13
|
+
import type { Identity } from "./EventLog.ts"
|
|
14
|
+
import { EncryptedEntry, EncryptedRemoteEntry } from "./EventLogEncryption.ts"
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @since 4.0.0
|
|
18
|
+
* @category StoreId
|
|
19
|
+
*/
|
|
20
|
+
export type StoreIdTypeId = "effect/eventlog/EventLog/StoreId"
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @since 4.0.0
|
|
24
|
+
* @category StoreId
|
|
25
|
+
*/
|
|
26
|
+
export const StoreIdTypeId: StoreIdTypeId = "effect/eventlog/EventLog/StoreId"
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @since 4.0.0
|
|
30
|
+
* @category StoreId
|
|
31
|
+
*/
|
|
32
|
+
export type StoreId = string & Brand<StoreIdTypeId>
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @since 4.0.0
|
|
36
|
+
* @category StoreId
|
|
37
|
+
*/
|
|
38
|
+
export const StoreId = Schema.String.pipe(Schema.brand(StoreIdTypeId))
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @since 4.0.0
|
|
42
|
+
* @category protocol
|
|
43
|
+
*/
|
|
44
|
+
export class EventLogProtocolError extends Schema.TaggedErrorClass<EventLogProtocolError>(
|
|
45
|
+
"effect/eventlog/EventLogRemote/ProtocolError"
|
|
46
|
+
)("EventLogProtocolError", {
|
|
47
|
+
requestTag: Schema.String,
|
|
48
|
+
publicKey: Schema.optional(Schema.String),
|
|
49
|
+
storeId: Schema.optional(StoreId),
|
|
50
|
+
code: Schema.Literals(["Unauthorized", "Forbidden", "NotFound", "InvalidRequest", "InternalServerError"]),
|
|
51
|
+
message: Schema.String
|
|
52
|
+
}) {}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @since 4.0.0
|
|
56
|
+
* @category Middleware
|
|
57
|
+
*/
|
|
58
|
+
export class EventLogAuthentication extends RpcMiddleware.Service<EventLogAuthentication, {
|
|
59
|
+
provides: Identity
|
|
60
|
+
}>()("effect/eventlog/EventLogMessage/EventLogAuthentication", {
|
|
61
|
+
error: EventLogProtocolError
|
|
62
|
+
}) {}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* @since 4.0.0
|
|
66
|
+
* @category protocol
|
|
67
|
+
*/
|
|
68
|
+
export class HelloResponse extends Schema.Class<HelloResponse>("effect/eventlog/EventLogRemote/HelloResponse")({
|
|
69
|
+
remoteId: RemoteId,
|
|
70
|
+
challenge: Transferable.Uint8Array
|
|
71
|
+
}) {}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* @since 4.0.0
|
|
75
|
+
* @category protocol
|
|
76
|
+
*/
|
|
77
|
+
export class HelloRpc extends Rpc.make("EventLog.Hello", {
|
|
78
|
+
success: HelloResponse
|
|
79
|
+
}) {}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* @since 4.0.0
|
|
83
|
+
* @category protocol
|
|
84
|
+
*/
|
|
85
|
+
export class Authenticate extends Schema.Class<Authenticate>("effect/eventlog/EventLogRemote/Authenticate")({
|
|
86
|
+
publicKey: Schema.String,
|
|
87
|
+
signingPublicKey: Transferable.Uint8Array,
|
|
88
|
+
signature: Transferable.Uint8Array,
|
|
89
|
+
algorithm: Schema.Literal("Ed25519")
|
|
90
|
+
}) {}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* @since 4.0.0
|
|
94
|
+
* @category protocol
|
|
95
|
+
*/
|
|
96
|
+
export class AuthenticateRpc extends Rpc.make("EventLog.Authenticate", {
|
|
97
|
+
payload: Authenticate,
|
|
98
|
+
error: EventLogProtocolError
|
|
99
|
+
}) {}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* @since 4.0.0
|
|
103
|
+
* @category protocol
|
|
104
|
+
*/
|
|
105
|
+
export class SingleMessage
|
|
106
|
+
extends Schema.TaggedClass<SingleMessage>("effect/eventlog/EventLogRemote/SingleMessage")("Single", {
|
|
107
|
+
data: Transferable.Uint8Array
|
|
108
|
+
})
|
|
109
|
+
{}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* @since 4.0.0
|
|
113
|
+
* @category protocol
|
|
114
|
+
*/
|
|
115
|
+
export class ChunkedMessage
|
|
116
|
+
extends Schema.TaggedClass<ChunkedMessage>("effect/eventlog/EventLogRemote/ChunkedMessage")("Chunked", {
|
|
117
|
+
id: Schema.Number,
|
|
118
|
+
part: Schema.Tuple([Schema.Number, Schema.Number]),
|
|
119
|
+
data: Transferable.Uint8Array
|
|
120
|
+
})
|
|
121
|
+
{
|
|
122
|
+
static chunkSize = 512_000
|
|
123
|
+
|
|
124
|
+
static initialJoinState() {
|
|
125
|
+
return new Map<number, {
|
|
126
|
+
readonly parts: Array<Uint8Array>
|
|
127
|
+
count: number
|
|
128
|
+
bytes: number
|
|
129
|
+
}>()
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* @since 4.0.0
|
|
134
|
+
*/
|
|
135
|
+
static split(id: number, data: Uint8Array): NonEmptyReadonlyArray<ChunkedMessage> {
|
|
136
|
+
const parts = Math.ceil(data.byteLength / ChunkedMessage.chunkSize)
|
|
137
|
+
const result: NonEmptyArray<ChunkedMessage> = new Array(parts) as any
|
|
138
|
+
for (let i = 0; i < parts; i++) {
|
|
139
|
+
const start = i * ChunkedMessage.chunkSize
|
|
140
|
+
const end = Math.min((i + 1) * ChunkedMessage.chunkSize, data.byteLength)
|
|
141
|
+
result[i] = new ChunkedMessage({
|
|
142
|
+
id,
|
|
143
|
+
part: [i, parts],
|
|
144
|
+
data: data.subarray(start, end) as any
|
|
145
|
+
})
|
|
146
|
+
}
|
|
147
|
+
return result
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* @since 4.0.0
|
|
152
|
+
*/
|
|
153
|
+
static join(
|
|
154
|
+
map: Map<number, {
|
|
155
|
+
readonly parts: Array<Uint8Array>
|
|
156
|
+
count: number
|
|
157
|
+
bytes: number
|
|
158
|
+
}>,
|
|
159
|
+
part: ChunkedMessage
|
|
160
|
+
): Uint8Array<ArrayBuffer> | undefined {
|
|
161
|
+
const [index, total] = part.part
|
|
162
|
+
let entry = map.get(part.id)
|
|
163
|
+
if (!entry) {
|
|
164
|
+
entry = {
|
|
165
|
+
parts: new Array(total),
|
|
166
|
+
count: 0,
|
|
167
|
+
bytes: 0
|
|
168
|
+
}
|
|
169
|
+
map.set(part.id, entry)
|
|
170
|
+
}
|
|
171
|
+
entry.parts[index] = part.data
|
|
172
|
+
entry.count++
|
|
173
|
+
entry.bytes += part.data.byteLength
|
|
174
|
+
if (entry.count !== total) {
|
|
175
|
+
return
|
|
176
|
+
}
|
|
177
|
+
const data = new Uint8Array(entry.bytes)
|
|
178
|
+
let offset = 0
|
|
179
|
+
for (const part of entry.parts) {
|
|
180
|
+
data.set(part, offset)
|
|
181
|
+
offset += part.byteLength
|
|
182
|
+
}
|
|
183
|
+
map.delete(part.id)
|
|
184
|
+
return data
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* @since 4.0.0
|
|
190
|
+
* @category protocol
|
|
191
|
+
*/
|
|
192
|
+
export class WriteChunkedRpc extends Rpc.make("EventLog.WriteChunked", {
|
|
193
|
+
payload: ChunkedMessage,
|
|
194
|
+
error: EventLogProtocolError
|
|
195
|
+
}).middleware(EventLogAuthentication) {}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* @since 4.0.0
|
|
199
|
+
* @category protocol
|
|
200
|
+
*/
|
|
201
|
+
export class WriteEntries extends Schema.Class<WriteEntries>("effect/eventlog/EventLogRemote/WriteEntries")({
|
|
202
|
+
publicKey: Schema.String,
|
|
203
|
+
storeId: StoreId,
|
|
204
|
+
iv: Transferable.Uint8Array,
|
|
205
|
+
encryptedEntries: Schema.Array(EncryptedEntry)
|
|
206
|
+
}) {
|
|
207
|
+
static FromMsgpack = Msgpack.schema(WriteEntries)
|
|
208
|
+
static encode = Schema.encodeEffect(this.FromMsgpack)
|
|
209
|
+
static decode = Schema.decodeEffect(this.FromMsgpack)
|
|
210
|
+
get encoded() {
|
|
211
|
+
return WriteEntries.encode(this)
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* @since 4.0.0
|
|
217
|
+
* @category protocol
|
|
218
|
+
*/
|
|
219
|
+
export class WriteEntriesUnencrypted
|
|
220
|
+
extends Schema.Class<WriteEntriesUnencrypted>("effect/eventlog/EventLogRemote/WriteEntriesUnencrypted")({
|
|
221
|
+
publicKey: Schema.String,
|
|
222
|
+
storeId: StoreId,
|
|
223
|
+
entries: Schema.Array(Entry)
|
|
224
|
+
})
|
|
225
|
+
{
|
|
226
|
+
static FromMsgpack = Msgpack.schema(WriteEntriesUnencrypted)
|
|
227
|
+
static encode = Schema.encodeEffect(this.FromMsgpack)
|
|
228
|
+
static decode = Schema.decodeEffect(this.FromMsgpack)
|
|
229
|
+
get encoded() {
|
|
230
|
+
return WriteEntriesUnencrypted.encode(this)
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* @since 4.0.0
|
|
236
|
+
* @category protocol
|
|
237
|
+
*/
|
|
238
|
+
export class WriteSingleRpc extends Rpc.make("EventLog.WriteSingle", {
|
|
239
|
+
payload: {
|
|
240
|
+
data: Transferable.Uint8Array
|
|
241
|
+
},
|
|
242
|
+
error: EventLogProtocolError
|
|
243
|
+
}).middleware(EventLogAuthentication) {}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* @since 4.0.0
|
|
247
|
+
* @category protocol
|
|
248
|
+
*/
|
|
249
|
+
export class ChangesRpc extends Rpc.make("EventLog.Changes", {
|
|
250
|
+
payload: {
|
|
251
|
+
publicKey: Schema.String,
|
|
252
|
+
storeId: StoreId,
|
|
253
|
+
startSequence: Schema.Number
|
|
254
|
+
},
|
|
255
|
+
success: Schema.Union([SingleMessage, ChunkedMessage]),
|
|
256
|
+
error: EventLogProtocolError,
|
|
257
|
+
stream: true
|
|
258
|
+
}).middleware(EventLogAuthentication) {
|
|
259
|
+
static EncryptedFromMsgpack = Msgpack.schema(Schema.NonEmptyArray(EncryptedRemoteEntry))
|
|
260
|
+
static UnencryptedFromMsgpack = Msgpack.schema(Schema.NonEmptyArray(RemoteEntry))
|
|
261
|
+
static encodeEncrypted = Schema.encodeEffect(ChangesRpc.EncryptedFromMsgpack)
|
|
262
|
+
static decodeEncrypted = Schema.decodeEffect(ChangesRpc.EncryptedFromMsgpack)
|
|
263
|
+
static encodeUnencrypted = Schema.encodeEffect(ChangesRpc.UnencryptedFromMsgpack)
|
|
264
|
+
static decodeUnencrypted = Schema.decodeEffect(ChangesRpc.UnencryptedFromMsgpack)
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* @since 4.0.0
|
|
269
|
+
* @category protocol
|
|
270
|
+
*/
|
|
271
|
+
export class EventLogRemoteRpcs extends RpcGroup.make(
|
|
272
|
+
HelloRpc,
|
|
273
|
+
AuthenticateRpc,
|
|
274
|
+
WriteChunkedRpc,
|
|
275
|
+
WriteSingleRpc,
|
|
276
|
+
ChangesRpc
|
|
277
|
+
) {}
|