@yaks/rtc 0.0.0-stage → 0.2.36
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 +202 -0
- package/README.md +117 -2
- package/_dist/door.d.ts +74 -0
- package/_dist/door.d.ts.map +1 -0
- package/_dist/effects.d.ts +8 -0
- package/_dist/effects.d.ts.map +1 -0
- package/_dist/join.d.ts +66 -0
- package/_dist/join.d.ts.map +1 -0
- package/_dist/loop.d.ts +11 -0
- package/_dist/loop.d.ts.map +1 -0
- package/_dist/mod.d.ts +30 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/vocab.d.ts +6 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/deno.json +1 -0
- package/door.js +349 -0
- package/door.js.map +1 -0
- package/door.ts +417 -0
- package/effects.js +23 -0
- package/effects.js.map +1 -0
- package/effects.ts +27 -0
- package/join.js +515 -0
- package/join.js.map +1 -0
- package/join.ts +564 -0
- package/loop.js +64 -0
- package/loop.js.map +1 -0
- package/loop.ts +73 -0
- package/mod.js +30 -0
- package/mod.js.map +1 -0
- package/mod.ts +45 -0
- package/package.json +30 -4
- package/vocab.js +16 -0
- package/vocab.js.map +1 -0
- package/vocab.json +68 -0
- package/vocab.ts +17 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 Jeff Peterson
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,118 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @yaks/rtc
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Voice between pages over
|
|
4
|
+
[Cloudflare Realtime](https://developers.cloudflare.com/realtime/): a page joins
|
|
5
|
+
a call, publishes its microphone, and hears the tracks it asks for. The bytes go
|
|
6
|
+
through Realtime's SFU and never touch the graph. What a page says is named on
|
|
7
|
+
the entity it speaks as, in the relayed `rtc` component, so a listener finds a
|
|
8
|
+
voice where it finds everything else about that entity.
|
|
9
|
+
|
|
10
|
+
There are no rooms. A page hears the voices it asks for, and a voice goes quiet
|
|
11
|
+
for everyone when its tab closes, because the relay clears `rtc` with the
|
|
12
|
+
connection that wrote it ([@yaks/sync](../sync)).
|
|
13
|
+
|
|
14
|
+
## A page
|
|
15
|
+
|
|
16
|
+
```ts ignore
|
|
17
|
+
import { join, microphone } from '@yaks/rtc'
|
|
18
|
+
|
|
19
|
+
let call = await join({ entity: hero, write: (b) => client.mutate(b) })
|
|
20
|
+
let voice = await call.publish(await microphone())
|
|
21
|
+
call.mute(true)
|
|
22
|
+
|
|
23
|
+
// a peer wearing rtc{session, tracks: ['voice']}
|
|
24
|
+
let heard = await call.hear(peer.rtc.session, 'voice')
|
|
25
|
+
audio.createMediaStreamSource(heard!.stream).connect(panner)
|
|
26
|
+
await heard!.stop()
|
|
27
|
+
await voice.stop()
|
|
28
|
+
await call.leave()
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
- `join({entity, write, door?, change?})` opens a session through the app's door
|
|
32
|
+
(`./api/rtc/` beside the page) and wears `rtc` on the entity: its session, the
|
|
33
|
+
tracks it publishes, and whether it is muted or talking. `change` hears the
|
|
34
|
+
call's state: `live`, `limit` (the space's allowance is used up, and voices go
|
|
35
|
+
quiet until it lifts), `refused`, `lost` while it reconnects, `left`.
|
|
36
|
+
- `publish(track, name = 'voice')` sends a track, capped at 32 kbit/s, and names
|
|
37
|
+
it in `rtc.tracks`. `talking` follows the microphone's level, read off the
|
|
38
|
+
sender's own statistics.
|
|
39
|
+
- `hear(session, track)` subscribes to another session's track and returns its
|
|
40
|
+
stream, or null for a track that is gone. The stream already plays through a
|
|
41
|
+
muted `<audio>`: Chrome plays a remote track that feeds only Web Audio as
|
|
42
|
+
silence.
|
|
43
|
+
- The call renews its lease about every 30 seconds and re-writes `rtc` with it.
|
|
44
|
+
A failed connection, or a lease the door no longer holds, is a new session: it
|
|
45
|
+
publishes again, and every `Heard` it had goes `live: false`, for the page to
|
|
46
|
+
ask again.
|
|
47
|
+
|
|
48
|
+
Chrome's echo canceller does not hear what Web Audio plays, so a voice played
|
|
49
|
+
through a `PannerNode` to the speakers reaches the microphone and returns to its
|
|
50
|
+
speaker. `loopback(ctx)` is the way round it: a Web Audio mix connected into
|
|
51
|
+
`loop.into` plays through a local peer connection into an `<audio>`, which is
|
|
52
|
+
cancelled, and stays stereo. Move the mix over once `loop.ready` resolves, only
|
|
53
|
+
while a microphone is open (it adds some 40 ms), and `loop.stop()` it after:
|
|
54
|
+
|
|
55
|
+
```ts ignore
|
|
56
|
+
import { loopback } from '@yaks/rtc'
|
|
57
|
+
|
|
58
|
+
let loop = loopback(ctx)
|
|
59
|
+
await loop.ready
|
|
60
|
+
mix.disconnect(ctx.destination)
|
|
61
|
+
mix.connect(loop.into)
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## A host
|
|
65
|
+
|
|
66
|
+
`@yaks/rtc/door` answers Realtime's own paths and bodies for one app
|
|
67
|
+
(`sessions/new`, `sessions/<id>/tracks/new`, `renegotiate`, `tracks/close`,
|
|
68
|
+
`tracks/update`, `datachannels/new`, `datachannels/close`, and a `GET` of a
|
|
69
|
+
session), so Cloudflare's pages apply word for word, plus `ice` (TURN's ICE
|
|
70
|
+
servers) and `sessions/<id>/renew`. The host hands `answer` each call with
|
|
71
|
+
everything it knows about the caller's world:
|
|
72
|
+
|
|
73
|
+
```ts ignore
|
|
74
|
+
import { answer } from '@yaks/rtc/door'
|
|
75
|
+
|
|
76
|
+
let res = await answer({
|
|
77
|
+
req,
|
|
78
|
+
path, // within the door: '/sessions/new'
|
|
79
|
+
store, // the app's store: read(query), write(bundles)
|
|
80
|
+
meter, // refused() -> sentence | null; reserve(dollars) -> {settle(cost)} | sentence
|
|
81
|
+
realtime: { app, token, turn: { key, token } },
|
|
82
|
+
rates: { track, channel }, // dollars a second received
|
|
83
|
+
})
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
- Every session the door opens is an `sfu{session, key}` row in the store, with
|
|
87
|
+
a `wake` when its lease ends a minute out. A session a call names, the
|
|
88
|
+
caller's own or one it subscribes to, must be one of them, so one app never
|
|
89
|
+
reaches another's sessions.
|
|
90
|
+
- A change to a session carries the key `sessions/new` handed back
|
|
91
|
+
(`x-yak-rtc-key`); the row keeps its SHA-256. The session id is no proof:
|
|
92
|
+
`rtc` relays it to every listener.
|
|
93
|
+
- The allowance is asked before a session opens. A subscription reserves its
|
|
94
|
+
remote tracks' and channels' rates over the remaining lease before reaching
|
|
95
|
+
Realtime. A renewal reserves the active subscriptions' rates over the lease
|
|
96
|
+
extension before granting it. The host reserves and settles these dollars
|
|
97
|
+
atomically against the same balance its other consumers use; a refused
|
|
98
|
+
subscription releases its reservation. Concurrent renewals charge each
|
|
99
|
+
extension once.
|
|
100
|
+
- Voice dollars are an egress estimate. Realtime reports no bytes per session
|
|
101
|
+
and does not enforce the host's lease; its wake closes the tracks later. These
|
|
102
|
+
reservations bound the shared dollar ledger, rather than guaranteeing a
|
|
103
|
+
maximum provider egress invoice. Model providers have separate request limits
|
|
104
|
+
and priced maximums for their strict next-call caps.
|
|
105
|
+
- When a lease lapses, its wake fires, and `rtc_lapse` closes the session's open
|
|
106
|
+
tracks at Realtime and drops the row. The host composes `@yaks/rtc/effects`
|
|
107
|
+
with `with: {realtime: {app, token}}`, using the same account as the door. In
|
|
108
|
+
a JSON config, `token` is a secret reference such as
|
|
109
|
+
`{"secret": "REALTIME_TOKEN"}`. Provider failures leave the row for the effect
|
|
110
|
+
pool to retry and report.
|
|
111
|
+
- A refusal is `{error: {code, message}}`: `limit` (429), `not_found` (404),
|
|
112
|
+
`not_yours` (403), `unavailable` (502, reported through `report`).
|
|
113
|
+
|
|
114
|
+
## The components
|
|
115
|
+
|
|
116
|
+
`@yaks/rtc/vocab` declares `rtc` (relayed, gone with its connection, sent at
|
|
117
|
+
most every 200 ms) and `sfu` (`wire: false`: only the door writes it). It also
|
|
118
|
+
declares `rtc_lapse`, triggered by `fired` while the store has an `sfu`.
|
package/_dist/door.d.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { type Bundle } from "@yaks/graph";
|
|
2
|
+
/** Cloudflare Realtime's API, as documented at
|
|
3
|
+
* developers.cloudflare.com/realtime/sfu/https-api/. */ export declare let API: string;
|
|
4
|
+
/** How long a lease runs past its last renewal, in milliseconds. */ export declare let LEASE: number;
|
|
5
|
+
/** The header a change to a session carries its opener's key in. */ export declare let KEY: string;
|
|
6
|
+
/** The account the door speaks for: an SFU app and its secret, and a TURN key
|
|
7
|
+
* and its token where the host has one. */ export type Realtime = {
|
|
8
|
+
app: string;
|
|
9
|
+
token: string;
|
|
10
|
+
turn?: {
|
|
11
|
+
key: string;
|
|
12
|
+
token: string;
|
|
13
|
+
};
|
|
14
|
+
/** Realtime's address, for a test; {@link API} otherwise */ api?: string;
|
|
15
|
+
fetch?: typeof fetch;
|
|
16
|
+
};
|
|
17
|
+
/** What a session costs a second, in dollars, per remote track and per remote
|
|
18
|
+
* DataChannel it receives. */ export type Rates = {
|
|
19
|
+
track: number;
|
|
20
|
+
channel: number;
|
|
21
|
+
};
|
|
22
|
+
/** The app's store, as far as the door reads and writes it. */ export type Store = {
|
|
23
|
+
read: (query: string) => Promise<Bundle[]>;
|
|
24
|
+
write: (bundles: Bundle[]) => Promise<unknown>;
|
|
25
|
+
};
|
|
26
|
+
/** The space's allowance, with dollars held before a lease can spend them. */ export type Meter = {
|
|
27
|
+
refused: () => Promise<string | null>;
|
|
28
|
+
reserve: (dollars: number) => Promise<string | {
|
|
29
|
+
settle: (dollars: number) => Promise<unknown>;
|
|
30
|
+
}>;
|
|
31
|
+
};
|
|
32
|
+
/** One call to the door. */ export type Ask = {
|
|
33
|
+
req: Request;
|
|
34
|
+
/** the path within the door: `/sessions/new`, `/ice` */ path: string;
|
|
35
|
+
store: Store;
|
|
36
|
+
meter: Meter;
|
|
37
|
+
realtime: Realtime;
|
|
38
|
+
rates: Rates;
|
|
39
|
+
/** a label TURN reports usage under (its customIdentifier) */ label?: string;
|
|
40
|
+
/** what went wrong that the caller could not have caused */ report?: (e: unknown) => void;
|
|
41
|
+
now?: () => number;
|
|
42
|
+
};
|
|
43
|
+
/** A refusal, the way every yaks door says one. */ export declare let refusal: (status: number, code: string, message: string) => Response;
|
|
44
|
+
/** What the row keeps of a key: its SHA-256. */ export declare let hashed: (key: string) => Promise<string>;
|
|
45
|
+
/** The sessions a request body subscribes to: its remote tracks and remote
|
|
46
|
+
* DataChannels. */ export declare let remotes: (body: unknown) => string[];
|
|
47
|
+
/**
|
|
48
|
+
* What a session receives costs a second: its active remote tracks and
|
|
49
|
+
* channels, each at its rate, as Realtime's own reading of the session says
|
|
50
|
+
* them.
|
|
51
|
+
*
|
|
52
|
+
* ```ts
|
|
53
|
+
* import { assertEquals } from '@std/assert'
|
|
54
|
+
* import { rate } from './door.ts'
|
|
55
|
+
*
|
|
56
|
+
* let state = {
|
|
57
|
+
* tracks: [
|
|
58
|
+
* { location: 'local', status: 'active' },
|
|
59
|
+
* { location: 'remote', status: 'active' },
|
|
60
|
+
* { location: 'remote', status: 'inactive' },
|
|
61
|
+
* ],
|
|
62
|
+
* dataChannels: [{ location: 'remote', status: 'active' }],
|
|
63
|
+
* }
|
|
64
|
+
* assertEquals(rate(state, { track: 3, channel: 1 }), 4)
|
|
65
|
+
* ```
|
|
66
|
+
*/ export declare let rate: (state: unknown, rates: Rates) => number;
|
|
67
|
+
/** Answer one call to the door. */ export declare let answer: (ask: Ask) => Promise<Response>;
|
|
68
|
+
/**
|
|
69
|
+
* A lease that lapsed: every track and channel still open on the session
|
|
70
|
+
* closed at Realtime, so nothing more is received on it or sent from it, and
|
|
71
|
+
* the row dropped. Run when the row's wake fires. A session Realtime no
|
|
72
|
+
* longer knows has nothing to close.
|
|
73
|
+
*/ export declare let lapse: (r: Realtime, store: Pick<Store, 'write'>, row: Bundle) => Promise<void>;
|
|
74
|
+
//# sourceMappingURL=door.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"door.d.ts","sources":["../door.ts"],"names":[],"mappings":"AA6BA,SAAS,KAAK,MAAM,2BAAkD;AAGtE;sDACsD,GACtD,OAAO,YAAI,YAA0C;AAErD,kEAAkE,GAClE,OAAO,YAAI,cAAc;AAEzB,kEAAkE,GAClE,OAAO,YAAI,YAAqB;AAEhC;yCACyC,GACzC,YAAY;EACV,KAAK,MAAM;EACX,OAAO,MAAM;EACb;IAAS,KAAK,MAAM;IAAE,OAAO,MAAM;;EACnC,0DAA0D,GAC1D,MAAM,MAAM;EACZ,eAAe;;AAGjB;4BAC4B,GAC5B,YAAY;EAAU,OAAO,MAAM;EAAE,SAAS,MAAM;;AAEpD,6DAA6D,GAC7D,YAAY;EACV,OAAO,OAAO,MAAM,KAAK,QAAQ;EACjC,QAAQ,SAAS,aAAa,QAAQ,OAAO;;AAG/C,4EAA4E,GAC5E,YAAY;EACV,eAAe,QAAQ,MAAM,GAAG,IAAI;EACpC,UAAU,SAAS,MAAM,KAAK,QAC5B,MAAM;IAAK,SAAS,SAAS,MAAM,KAAK,QAAQ,OAAO;;;AAI3D,0BAA0B,GAC1B,YAAY;EACV,KAAK;EACL,sDAAsD,GACtD,MAAM,MAAM;EACZ,OAAO;EACP,OAAO;EACP,UAAU;EACV,OAAO;EACP,4DAA4D,GAC5D,QAAQ,MAAM;EACd,0DAA0D,GAC1D,UAAU,GAAG,OAAO,KAAK,IAAI;EAC7B,YAAY,MAAM;;AAGpB,iDAAiD,GACjD,OAAO,YAAI,UACT,QAAQ,MAAM,EACd,MAAM,MAAM,EACZ,SAAS,MAAM,KACd,SAIC;AAQJ,8CAA8C,GAC9C,OAAO,YAAI,SAAgB,KAAK,MAAM,KAAG,QAAQ,MAAM,EACoB;AAa3E;iBACiB,GACjB,OAAO,YAAI,UAAW,MAAM,OAAO,KAAG,MAAM,GAM3C;AAED;;;;;;;;;;;;;;;;;;;CAmBC,GACD,OAAO,YAAI,OAAQ,OAAO,OAAO,EAAE,OAAO,UAAQ,MAAM,CAQvD;AAgBD,iCAAiC,GACjC,OAAO,YAAI,SAAgB,KAAK,QAAM,QAAQ,UA2B7C;AAkLD;;;;;CAKC,GACD,OAAO,YAAI,QACT,GAAG,UACH,OAAO,KAAK,OAAO,UACnB,KAAK,WACJ,QAAQ,IAAI,EA2Bd"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Handlers } from "@yaks/effects";
|
|
2
|
+
import { type Realtime } from "./door.js";
|
|
3
|
+
/** The same Realtime account the host lends the door. Credentials are read
|
|
4
|
+
* on each attempt, so a config's secret references can rotate. */ export type Options = {
|
|
5
|
+
realtime?: Realtime;
|
|
6
|
+
};
|
|
7
|
+
/** A host composes this facet beside the door's vocabulary. */ export declare let effects: (_host: unknown, options?: Options) => Handlers;
|
|
8
|
+
//# sourceMappingURL=effects.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"effects.d.ts","sources":["../effects.ts"],"names":[],"mappings":"AAIA,cAAc,QAAQ,6BAAkC;AACxD,SAAgB,KAAK,QAAQ,oBAAmB;AAEhD;gEACgE,GAChE,YAAY;EAAY,WAAW;;AAEnC,6DAA6D,GAC7D,OAAO,YAAI,UAAW,OAAO,OAAO,EAAE,UAAS,YAAe,SAc5D"}
|
package/_dist/join.d.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { Bundle } from "@yaks/graph";
|
|
2
|
+
/** How a call stands. `limit` is the space's allowance used up: the lease
|
|
3
|
+
* runs out and the voices go quiet until it lifts. `refused` is a caller the
|
|
4
|
+
* app does not let speak. */ export type State = 'joining' | 'live' | 'limit' | 'refused' | 'lost' | 'left';
|
|
5
|
+
export type Opts = {
|
|
6
|
+
/** the entity the page speaks as, which wears `rtc` */ entity: string;
|
|
7
|
+
/** how the page writes a bundle into its graph */ write: (bundles: Bundle[]) => unknown;
|
|
8
|
+
/** the app's Realtime door: `./api/rtc/` beside the page unless named */ door?: string | URL;
|
|
9
|
+
/** told whenever the call's state moves */ change?: (state: State) => void;
|
|
10
|
+
fetch?: typeof fetch;
|
|
11
|
+
};
|
|
12
|
+
/** A snapshot of the actual microphone sender, not a promise that a listener
|
|
13
|
+
* hears it. A null statistic means this browser does not expose that field. */ export type MicSignal = {
|
|
14
|
+
ready: boolean;
|
|
15
|
+
enabled: boolean;
|
|
16
|
+
connected: boolean;
|
|
17
|
+
level: number | null;
|
|
18
|
+
energy: number | null;
|
|
19
|
+
duration: number | null;
|
|
20
|
+
packets: number | null;
|
|
21
|
+
};
|
|
22
|
+
/** A track the call publishes. */ export type Published = {
|
|
23
|
+
name: string;
|
|
24
|
+
stop: () => Promise<void>;
|
|
25
|
+
};
|
|
26
|
+
/** A track the call hears: its stream, while `live`. */ export type Heard = {
|
|
27
|
+
stream: MediaStream;
|
|
28
|
+
readonly live: boolean;
|
|
29
|
+
stop: () => Promise<void>;
|
|
30
|
+
};
|
|
31
|
+
export type Call = {
|
|
32
|
+
readonly session: string;
|
|
33
|
+
readonly state: State;
|
|
34
|
+
publish: (track: MediaStreamTrack, name?: string) => Promise<Published>;
|
|
35
|
+
mute: (muted: boolean) => void;
|
|
36
|
+
/** Inspect the published sender, for local input and transport guidance. */ diagnose: (name?: string) => Promise<MicSignal | null>;
|
|
37
|
+
hear: (session: string, track: string) => Promise<Heard | null>;
|
|
38
|
+
leave: () => Promise<void>;
|
|
39
|
+
};
|
|
40
|
+
/** A door's refusal, with its code: `limit`, `not_found`, `not_yours`. */ export declare class Refused extends Error {
|
|
41
|
+
code: string;
|
|
42
|
+
status: number;
|
|
43
|
+
constructor(code: string, message: string, status?: number);
|
|
44
|
+
}
|
|
45
|
+
/** What Opus is capped at, in bits a second: a voice, not a song. */ export declare let BITRATE: number;
|
|
46
|
+
/** The track a call opens with, silent until a microphone is published into
|
|
47
|
+
* it: the name `publish` takes unless told another. */ export declare let VOICE: string;
|
|
48
|
+
/** How often the lease is renewed, in milliseconds: twice a lease. */ export declare let RENEW: number;
|
|
49
|
+
/** A microphone as a voice wants it. */ export declare let microphone: () => Promise<MediaStreamTrack>;
|
|
50
|
+
/**
|
|
51
|
+
* Whether a microphone is hearing a voice, from its level (0 to 1) and
|
|
52
|
+
* whether it was a moment ago: it starts above one line and stops below a
|
|
53
|
+
* lower one, so a voice between the two does not flicker.
|
|
54
|
+
*
|
|
55
|
+
* ```ts
|
|
56
|
+
* import { assertEquals } from '@std/assert'
|
|
57
|
+
* import { voiced } from './join.ts'
|
|
58
|
+
*
|
|
59
|
+
* assertEquals(voiced(0.2, false), true)
|
|
60
|
+
* assertEquals(voiced(0.03, false), false)
|
|
61
|
+
* assertEquals(voiced(0.03, true), true)
|
|
62
|
+
* assertEquals(voiced(0.005, true), false)
|
|
63
|
+
* ```
|
|
64
|
+
*/ export declare let voiced: (level: number, was: boolean) => any;
|
|
65
|
+
/** Open a call: a session at Realtime, through the app's door. */ export declare let join: (opts: Opts) => Promise<Call>;
|
|
66
|
+
//# sourceMappingURL=join.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"join.d.ts","sources":["../join.ts"],"names":[],"mappings":"AAgCA,cAAc,MAAM,2BAAgC;AAGpD;;2BAE2B,GAC3B,YAAY,QAAQ,YAAY,SAAS,UAAU,YAAY,SAAS;AAExE,YAAY;EACV,qDAAqD,GACrD,QAAQ,MAAM;EACd,gDAAgD,GAChD,QAAQ,SAAS,aAAa,OAAO;EACrC,uEAAuE,GACvE,OAAO,MAAM,GAAG;EAChB,yCAAyC,GACzC,UAAU,OAAO,UAAU,IAAI;EAC/B,eAAe;;AAGjB;6EAC6E,GAC7E,YAAY;EACV,OAAO,OAAO;EACd,SAAS,OAAO;EAChB,WAAW,OAAO;EAClB,OAAO,MAAM,GAAG,IAAI;EACpB,QAAQ,MAAM,GAAG,IAAI;EACrB,UAAU,MAAM,GAAG,IAAI;EACvB,SAAS,MAAM,GAAG,IAAI;;AAGxB,gCAAgC,GAChC,YAAY;EAAc,MAAM,MAAM;EAAE,YAAY,QAAQ,IAAI;;AAEhE,sDAAsD,GACtD,YAAY;EACV,QAAQ;WACC,MAAM,OAAO;EACtB,YAAY,QAAQ,IAAI;;AAG1B,YAAY;WACD,SAAS,MAAM;WACf,OAAO;EAChB,UAAU,OAAO,kBAAkB,OAAO,MAAM,KAAK,QAAQ;EAC7D,OAAO,OAAO,OAAO,KAAK,IAAI;EAC9B,0EAA0E,GAC1E,WAAW,OAAO,MAAM,KAAK,QAAQ,YAAY,IAAI;EACrD,OAAO,SAAS,MAAM,EAAE,OAAO,MAAM,KAAK,QAAQ,QAAQ,IAAI;EAC9D,aAAa,QAAQ,IAAI;;AAG3B,wEAAwE,GACxE,OAAO,cAAM,gBAAgB;EAC3B,MAAM,MAAM,CAAA;EACZ,QAAQ,MAAM,CAAA;EACd,YAAY,MAAM,MAAM,EAAE,SAAS,MAAM,EAAE,eAAU;AAMvD;AAEA,mEAAmE,GACnE,OAAO,YAAI,gBAAgB;AAE3B;qDACqD,GACrD,OAAO,YAAI,cAAe;AAE1B,oEAAoE,GACpE,OAAO,YAAI,cAAc;AAEzB,sCAAsC,GACtC,OAAO,YAAI,kBAAuB,QAAQ,kBAczC;AAED;;;;;;;;;;;;;;CAcC,GACD,OAAO,YAAI,SAAU,OAAO,MAAM,EAAE,KAAK,OAAO,SAClB;AAE9B,gEAAgE,GAChE,OAAO,YAAI,OAAc,MAAM,SAAO,QAAQ,MAoa7C"}
|
package/_dist/loop.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** A loop: connect sound `into` it and it plays through an <audio> element
|
|
2
|
+
* once `ready`; `stop` ends it. */ export type Loop = {
|
|
3
|
+
into: AudioNode;
|
|
4
|
+
ready: Promise<void>;
|
|
5
|
+
stop: () => void;
|
|
6
|
+
};
|
|
7
|
+
/** A loop for Web Audio on `ctx` that the echo canceller hears. It is
|
|
8
|
+
* `ready` once the two ends have met and the element plays, a moment after it
|
|
9
|
+
* is made; until then, and if it never is, sound connected into it is not
|
|
10
|
+
* heard, so a page moves its mix over once it is. */ export declare let loopback: (ctx: AudioContext) => Loop;
|
|
11
|
+
//# sourceMappingURL=loop.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loop.d.ts","sources":["../loop.ts"],"names":[],"mappings":"AAiBA;iCACiC,GACjC,YAAY;EAAS,MAAM;EAAW,OAAO,QAAQ,IAAI;EAAG,YAAY,IAAI;;AAiB5E;;;mDAGmD,GACnD,OAAO,YAAI,WAAY,KAAK,iBAAe,KAgC1C"}
|
package/_dist/mod.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @yaks/rtc — voice between pages over Cloudflare Realtime.
|
|
3
|
+
*
|
|
4
|
+
* A page {@link join}s a call through its app's Realtime door, publishes a
|
|
5
|
+
* microphone, and hears the tracks it asks for. What it says is named on the
|
|
6
|
+
* entity it speaks as, in the relayed `rtc` component, so a listener finds a
|
|
7
|
+
* voice where it finds everything else about that entity:
|
|
8
|
+
*
|
|
9
|
+
* ```ts ignore
|
|
10
|
+
* import { join, microphone } from '@yaks/rtc'
|
|
11
|
+
*
|
|
12
|
+
* let call = await join({ entity: hero, write: (b) => client.mutate(b) })
|
|
13
|
+
* await call.publish(await microphone())
|
|
14
|
+
* // a peer wearing rtc{session, tracks: ['voice']}:
|
|
15
|
+
* let voice = await call.hear(peer.rtc.session, 'voice')
|
|
16
|
+
* audio.createMediaStreamSource(voice!.stream).connect(panner)
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* A page that places voices in space with Web Audio plays its mix through
|
|
20
|
+
* {@link loopback} while a microphone is open, so the browser's echo canceller
|
|
21
|
+
* hears it, which in Chrome it does not otherwise.
|
|
22
|
+
*
|
|
23
|
+
* The host half, which keeps the account's secret, the sessions and the
|
|
24
|
+
* meter, is `@yaks/rtc/door`; the components are `@yaks/rtc/vocab`.
|
|
25
|
+
*
|
|
26
|
+
* @module
|
|
27
|
+
*/ export { BITRATE, type Call, type Heard, join, microphone, type MicSignal, type Opts, type Published, Refused, RENEW, type State, VOICE, voiced } from "./join.js";
|
|
28
|
+
export { type Loop, loopback } from "./loop.js";
|
|
29
|
+
export { docs, rtcDoc } from "./vocab.js";
|
|
30
|
+
//# sourceMappingURL=mod.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.d.ts","sources":["../mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;CA0BC,GAED,SACE,OAAO,EACP,KAAK,IAAI,EACT,KAAK,KAAK,EACV,IAAI,EACJ,UAAU,EACV,KAAK,SAAS,EACd,KAAK,IAAI,EACT,KAAK,SAAS,EACd,OAAO,EACP,KAAK,EACL,KAAK,KAAK,EACV,KAAK,EACL,MAAM,oBACU;AAClB,SAAS,KAAK,IAAI,EAAE,QAAQ,oBAAmB;AAC/C,SAAS,IAAI,EAAE,MAAM,qBAAoB"}
|
package/_dist/vocab.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { VocabDoc } from "@yaks/vocab";
|
|
2
|
+
/** The `rtc` and `sfu` components. */ export declare let rtcDoc: VocabDoc;
|
|
3
|
+
/** What this package is: its manifest's description, the one place it is
|
|
4
|
+
* written, which a host stamps on each document here. */ export declare let description: string;
|
|
5
|
+
/** Every document this package declares. */ export declare let docs: VocabDoc[];
|
|
6
|
+
//# sourceMappingURL=vocab.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vocab.d.ts","sources":["../vocab.ts"],"names":[],"mappings":"AAIA,cAAc,QAAQ,2BAAgC;AAItD,oCAAoC,GACpC,OAAO,YAAI,QAAQ,SAA0B;AAE7C;uDACuD,GACvD,OAAO,YAAI,aAAa,MAAM,CAAuB;AAErD,0CAA0C,GAC1C,OAAO,YAAI,MAAM,WAAqB"}
|
package/deno.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"compilerOptions":{"lib":["dom","esnext"]},"publish":{"exclude":["**/*_test.ts"]},"name":"@yaks/rtc","version":"0.2.36","license":"Apache-2.0","description":"Voice between pages over Cloudflare Realtime: a page joins a session, publishes its microphone and hears the tracks it asks for, and a host answers the door that keeps the secret, the sessions and the meter.","exports":{".":"./mod.ts","./door":"./door.ts","./effects":"./effects.ts","./vocab":"./vocab.ts"},"imports":{"@yaks/rtc":"./mod.ts","@yaks/rtc/door":"./door.ts","@yaks/rtc/effects":"./effects.ts","@yaks/rtc/vocab":"./vocab.ts"}}
|