@atom.io/template-react-realtime-text-editor 0.0.1
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/.turbo/turbo-build.log +10 -0
- package/CHANGELOG.md +5 -0
- package/INNOVATIONS.md +113 -0
- package/LICENSE +201 -0
- package/README.md +48 -0
- package/_gitignore +5 -0
- package/dist/assets/index-DGIOrh2O.css +1 -0
- package/dist/index.html +17 -0
- package/eslint.config.ts +39 -0
- package/index.html +16 -0
- package/mise.toml +1 -0
- package/node/mosaic-atom.ts +88 -0
- package/node/server.ts +67 -0
- package/package.json +53 -0
- package/src/MarkdownWorkspace.module.css +539 -0
- package/src/MarkdownWorkspace.tsx +424 -0
- package/src/collaboration/mosaic.ts +46 -0
- package/src/globals.css +38 -0
- package/src/identities.ts +23 -0
- package/src/initial-markdown.ts +22 -0
- package/src/main.tsx +24 -0
- package/src/session.ts +29 -0
- package/src/vite-env.d.ts +4 -0
- package/tests/collaboration.test.tsx +251 -0
- package/tsconfig.app.json +24 -0
- package/tsconfig.json +8 -0
- package/tsconfig.node.json +22 -0
- package/tsconfig.test.json +13 -0
- package/vite.config.ts +22 -0
- package/vitest.config.ts +10 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
[2m$ tsc -b && vite build[22m
|
|
2
|
+
[36mvite v8.2.1 [32mbuilding client environment for production...[36m[39m
|
|
3
|
+
[2K
|
|
4
|
+
[2K
|
|
5
|
+
[2K
|
|
6
|
+
[2mdist/[0m[2massets/[0m[35mindex-DGIOrh2O.css [0m[1;2m 11.67 kB[0m[2m │ gzip: 2.61 kB[0m
|
|
7
|
+
[2mdist/[0m[2massets/[0m[36mweb-BP4kJorM.js [0m[1;2m 0.22 kB[0m[2m │ gzip: 0.17 kB[0m
|
|
8
|
+
[2mdist/[0m[2massets/[0m[36mindex-jw83brgG.js [0m[1;2m318.26 kB[0m[2m │ gzip: 98.11 kB[0m
|
|
9
|
+
|
|
10
|
+
[32m✓ built in 120ms[39m
|
package/CHANGELOG.md
ADDED
package/INNOVATIONS.md
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# Mosaic Reconciliation Notes
|
|
2
|
+
|
|
3
|
+
This template began as an application-local experiment. The collaboration
|
|
4
|
+
machinery now lives in atom.io's Mosaic module, but Mosaic is not a parallel
|
|
5
|
+
state system: the document is a transceiver held by an ordinary mutable atom.
|
|
6
|
+
That atom token is its network address, its read-only view feeds ordinary
|
|
7
|
+
selectors and renderer hooks, and each Store owns the optimistic replica bound
|
|
8
|
+
to it. The server registers the same transceiver class as the authority for that
|
|
9
|
+
address. The decisions below are the places where this transceiver deliberately
|
|
10
|
+
takes a different path from rigid state proxying and whole-value time travel.
|
|
11
|
+
|
|
12
|
+
## Bounded Sequence Intervals
|
|
13
|
+
|
|
14
|
+
A shared string is not a useful conflict unit. Mosaic Text represents Unicode
|
|
15
|
+
graphemes as stable nodes and edits as insert/delete operations. Each insertion
|
|
16
|
+
records both its retained left boundary and its retained right boundary. The
|
|
17
|
+
second boundary matters: a predecessor-only graph can move a middle replacement
|
|
18
|
+
after the old suffix merely because one author's operation ID sorts later.
|
|
19
|
+
Bounded intervals preserve the user's edit location while deterministic,
|
|
20
|
+
code-unit ID ordering resolves genuinely concurrent insertions.
|
|
21
|
+
|
|
22
|
+
Deletes are edit-owned visibility marks. Hidden nodes remain as anchor stubs, so
|
|
23
|
+
foreign descendants, late operations, and relative selections do not lose their
|
|
24
|
+
position when an author undoes the edit that introduced an ancestor.
|
|
25
|
+
|
|
26
|
+
The model uses the host's Unicode grapheme segmenter. A deployment with
|
|
27
|
+
heterogeneous ICU versions should pin runtimes together or ship a versioned
|
|
28
|
+
segmenter before claiming cross-runtime model-version equivalence.
|
|
29
|
+
|
|
30
|
+
## Selective, Per-Identity History — Not a Timeline
|
|
31
|
+
|
|
32
|
+
Whole-document time travel is unsafe in a shared editor: restoring yesterday's
|
|
33
|
+
string can erase another person's accepted work. Mosaic records edit ownership
|
|
34
|
+
and represents undo and redo as durable operations that deactivate or reactivate
|
|
35
|
+
only one authenticated actor's current edit group. A foreign insertion anchored
|
|
36
|
+
inside hidden local work remains visible.
|
|
37
|
+
|
|
38
|
+
Atom.io's native timelines still mean local graph time travel. The Mosaic text
|
|
39
|
+
transceiver is marked append-only and cannot be attached to one: rewinding its
|
|
40
|
+
object would bypass the accepted operation stream and clobber collaboration.
|
|
41
|
+
Its actor-scoped capability is named `historyFor`, and undo/redo append normal
|
|
42
|
+
operations that every replica reduces in the same order.
|
|
43
|
+
|
|
44
|
+
The server validates the actor's current history cursor through the text
|
|
45
|
+
transceiver immediately before the append. A stale tab therefore fails closed
|
|
46
|
+
and resnapshots. Accepted revisions
|
|
47
|
+
are durable reduction metadata, so concurrent history operations have one
|
|
48
|
+
canonical order; provisional client projections explicitly use no revision.
|
|
49
|
+
This is exact at operation ownership and intentionally best-effort at recovering
|
|
50
|
+
the prose-level intention behind arbitrary concurrent rewrites.
|
|
51
|
+
|
|
52
|
+
## Relative, Ephemeral Presence
|
|
53
|
+
|
|
54
|
+
Numeric caret offsets become stale after any preceding edit. Mosaic presence
|
|
55
|
+
uses left/right node anchors plus an explicit affinity. The text model resolves
|
|
56
|
+
those anchors against each local projection, including hidden anchors. Presence
|
|
57
|
+
is schema-checked and model-checked, but never enters the durable operation log
|
|
58
|
+
or a user's history. Explicit departure and disconnect both remove the exact
|
|
59
|
+
actor/session record.
|
|
60
|
+
|
|
61
|
+
## Durable Stream Before Fan-Out
|
|
62
|
+
|
|
63
|
+
The server never acknowledges or broadcasts an operation before persistence.
|
|
64
|
+
Its adapter contract atomically compares the expected revision and reserves the
|
|
65
|
+
atom-stream/operation-ID receipt. A reused ID with different normalized content is
|
|
66
|
+
a collision, not an idempotent retry. Receipts survive checkpoint compaction.
|
|
67
|
+
|
|
68
|
+
Horizontal notifications are wake-up hints only. Every server drains a checked,
|
|
69
|
+
contiguous tail from the shared linearizable store. Recovery hydrates one
|
|
70
|
+
consistent checkpoint and then applies every later revision. Checkpoint
|
|
71
|
+
installation, tail pruning, session watermarks, and a retention epoch form the
|
|
72
|
+
compaction fence. Text model version 1 retains stable node stubs; a future model
|
|
73
|
+
that removes them will need an explicit anchor-translation protocol.
|
|
74
|
+
|
|
75
|
+
The template uses the in-memory adapter so it runs without infrastructure. It is
|
|
76
|
+
restart-safe only while that adapter instance survives. Production should supply
|
|
77
|
+
a transactional database implementation and define an offline-session retention
|
|
78
|
+
policy before pruning operation bodies.
|
|
79
|
+
|
|
80
|
+
## Optimistic Reconciliation and Recovery
|
|
81
|
+
|
|
82
|
+
Each Atom.io Store creates stable session-scoped operation IDs, applies locally,
|
|
83
|
+
and retains one causal outbox for the mutable atom token. Multiple components in
|
|
84
|
+
that Store share the same replica; a second Silo gets a genuinely independent
|
|
85
|
+
replica. Reconnect snapshots report which pending IDs were already accepted and
|
|
86
|
+
the authoritative causal frontier; the client hydrates the transceiver
|
|
87
|
+
checkpoint, removes those proposals, and replays the rest from that frontier.
|
|
88
|
+
Duplicate delivery is harmless. A revision gap resnapshots.
|
|
89
|
+
Structured rejection policies distinguish retryable work from stale history or
|
|
90
|
+
invalid dependency chains, which are quarantined rather than left as impossible
|
|
91
|
+
optimistic state.
|
|
92
|
+
|
|
93
|
+
Local wall-clock time may group typing gestures. It is deliberately absent from
|
|
94
|
+
accepted reduction semantics, fingerprints, and convergence decisions. Local
|
|
95
|
+
signals created inside an Atom.io transaction are observed only after commit;
|
|
96
|
+
aborted transaction clones are discarded before they can enter the network
|
|
97
|
+
outbox.
|
|
98
|
+
|
|
99
|
+
## Testing Arbitrary Protocols
|
|
100
|
+
|
|
101
|
+
Mosaic is not implemented as push/pull state proxying, yet atom.io's realtime
|
|
102
|
+
test harness can exercise it because the harness exposes the transport and
|
|
103
|
+
lifecycle rather than assuming a particular protocol. The template's
|
|
104
|
+
multi-client scenarios run independent React stores and Socket.IO sessions,
|
|
105
|
+
disconnect both editors, accept simultaneous offline work, verify selective
|
|
106
|
+
history, and verify presence removal. Core suites add deterministic drop,
|
|
107
|
+
duplicate, reorder, restart, multi-node, checkpoint, and session-correlation
|
|
108
|
+
schedules.
|
|
109
|
+
|
|
110
|
+
The remaining seam is application work tracking: integrations that schedule
|
|
111
|
+
work beyond transport callbacks should register that work with the harness when
|
|
112
|
+
they need barrier-based quiescence. Mosaic's conformance suite otherwise uses
|
|
113
|
+
the same public server, client, and renderer APIs as this application.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# React Realtime Text Editor
|
|
2
|
+
|
|
3
|
+
Mosaic is a React + atom.io multiplayer Markdown editor with a small Node and
|
|
4
|
+
Socket.IO collaboration server. It ships simulated identities so the complete
|
|
5
|
+
shared-editing experience can be explored locally without provisioning auth.
|
|
6
|
+
|
|
7
|
+
## Run It
|
|
8
|
+
|
|
9
|
+
Run `npm run dev`, then open the displayed Vite URL. Open a second tab and use
|
|
10
|
+
the avatar menu to switch identities. The Vite app runs on port 5173 and the
|
|
11
|
+
collaboration service runs on port 3000. The browser always connects to its
|
|
12
|
+
current origin; Vite proxies `/socket.io` and `/health` to the collaboration
|
|
13
|
+
service in both development and preview modes.
|
|
14
|
+
|
|
15
|
+
## What It Shows
|
|
16
|
+
|
|
17
|
+
- optimistic shared text that converges after simultaneous and offline edits;
|
|
18
|
+
- live identity, connection, caret, and line-level presence;
|
|
19
|
+
- selection anchors that survive edits around them;
|
|
20
|
+
- per-identity undo and redo that preserve other authors' work;
|
|
21
|
+
- durable-stream snapshots, idempotent operation replay, and server validation;
|
|
22
|
+
- a safe, dependency-free Markdown preview rendered as React nodes; and
|
|
23
|
+
- structural component styling checked by Lasertag.
|
|
24
|
+
|
|
25
|
+
Run `npm test` for the multi-client collaboration scenarios. Run `npm run lint`
|
|
26
|
+
to check TypeScript, ESLint, and the Lasertag render-story/CSS relationship.
|
|
27
|
+
|
|
28
|
+
## Start Reading
|
|
29
|
+
|
|
30
|
+
- `src/collaboration/mosaic.ts` is the browser collaboration contract: one
|
|
31
|
+
mutable atom, graph-derived selectors, selective history, and model-relative
|
|
32
|
+
presence.
|
|
33
|
+
- `node/mosaic-atom.ts` registers the mutable atom with Zod runtime schemas,
|
|
34
|
+
presence validation, and checkpoint cadence without entering the browser
|
|
35
|
+
bundle.
|
|
36
|
+
- atom.io's Mosaic client owns optimistic edits, reconnect rebasing, causal
|
|
37
|
+
retries, and structured recovery.
|
|
38
|
+
- atom.io's Mosaic server authenticates authorship, durably orders revisions,
|
|
39
|
+
checkpoints streams, and publishes ephemeral presence.
|
|
40
|
+
- `src/MarkdownWorkspace.tsx` is the React editing experience.
|
|
41
|
+
- `INNOVATIONS.md` records the reconciliation decisions that became Mosaic and
|
|
42
|
+
the remaining production boundaries.
|
|
43
|
+
|
|
44
|
+
The template selects Mosaic's in-memory storage adapter for a zero-setup demo.
|
|
45
|
+
Production deployments should provide a linearizable storage adapter, document
|
|
46
|
+
ACLs, rate limits, and metrics as described in `INNOVATIONS.md`. Keep the
|
|
47
|
+
same-origin contract in your deployment by forwarding `/socket.io` to the
|
|
48
|
+
collaboration service at the edge.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
@import "https://fonts.googleapis.com/css2?family=DM+Mono:wght@400;500&family=Manrope:wght@400;500;600;700&family=Newsreader:opsz,wght@6..72,500;6..72,650&display=swap";:root{color:#292936;font-synthesis:none;text-rendering:optimizelegibility;background:#f3f2f7;font-family:Manrope,system-ui,sans-serif}*{box-sizing:border-box}html,body,#root{min-width:320px;min-height:100%;margin:0}button,input,select,textarea{font:inherit}button,select{cursor:pointer}button:disabled,textarea:disabled{cursor:not-allowed}markdown-workspace._class_ioksj_1{--accent:#6d50ea;--border:#dfdce8;--ink:#292936;--muted:#817e90;--paper:#fffefa;--panel:#faf9fc;background:radial-gradient(circle at 80% 0,#eee9ff 0,#0000 28rem),#f3f2f7;grid-template-rows:auto minmax(0,1fr) auto;min-height:100svh;display:grid}markdown-workspace._class_ioksj_1>header{z-index:3;-webkit-backdrop-filter:blur(1.5rem);backdrop-filter:blur(1.5rem);background:#fbfaffe6;border-bottom:1px solid #e5e2ec;grid-template-columns:minmax(11rem,1fr) minmax(15rem,1.5fr) minmax(18rem,1fr);align-items:center;gap:1.25rem;min-height:5.4rem;padding:.75rem 1.5rem;display:grid;box-shadow:0 .25rem 1.8rem #3023510a}markdown-workspace._class_ioksj_1>header>brand-lockup{align-items:center;gap:.75rem;display:flex}markdown-workspace._class_ioksj_1>header>brand-lockup>mark{aspect-ratio:1;color:#fff;background:linear-gradient(145deg,#8c70ff,#5f3edf);border-radius:.9rem;place-items:center;width:2.65rem;font-family:Newsreader,serif;font-size:1.45rem;font-weight:650;display:grid;box-shadow:0 .5rem 1rem #6749dc38}markdown-workspace._class_ioksj_1>header>brand-lockup>label-set{flex-flow:column;line-height:1.15;display:flex}markdown-workspace._class_ioksj_1>header>brand-lockup>label-set>strong{letter-spacing:-.02em;font-family:Newsreader,serif;font-size:1.35rem}markdown-workspace._class_ioksj_1>header>brand-lockup>label-set>span{color:var(--muted);letter-spacing:.08em;text-transform:uppercase;margin-top:.2rem;font-size:.67rem;font-weight:700}markdown-workspace._class_ioksj_1>header>document-title{flex-flow:column;align-items:center;min-width:0;display:flex}markdown-workspace._class_ioksj_1>header>document-title>strong{max-width:100%;color:var(--ink);text-align:center;font-family:Newsreader,serif;font-size:1.2rem;font-weight:650}markdown-workspace._class_ioksj_1>header>document-title>span{color:var(--muted);margin-top:.16rem;font-size:.69rem}markdown-workspace._class_ioksj_1>header>toolbar-actions{justify-content:flex-end;align-items:center;gap:.45rem;display:flex}markdown-workspace._class_ioksj_1>header>toolbar-actions>button{border:1px solid var(--border);color:#565363;background:#fff;border-radius:.65rem;align-items:center;gap:.35rem;min-height:2.25rem;padding:.4rem .68rem;font-size:.73rem;font-weight:700;display:inline-flex}markdown-workspace._class_ioksj_1>header>toolbar-actions>button:hover:not(:disabled){color:var(--accent);background:#f8f6ff;border-color:#bdb4df}markdown-workspace._class_ioksj_1>header>toolbar-actions>button:disabled{opacity:.42}markdown-workspace._class_ioksj_1>header>toolbar-actions>label{border:1px solid var(--border);background:#fff;border-radius:99rem;align-items:center;gap:.45rem;min-height:2.25rem;padding:.27rem .35rem;display:flex}markdown-workspace._class_ioksj_1>header>toolbar-actions>label>avatar-dot{aspect-ratio:1;color:#fff;background:var(--person-color);border-radius:50%;place-items:center;width:1.65rem;font-size:.7rem;font-weight:800;display:grid}markdown-workspace._class_ioksj_1>header>toolbar-actions>label>select{color:#4a4755;background:0 0;border:0;outline:0;max-width:8rem;font-size:.72rem;font-weight:700}markdown-workspace._class_ioksj_1>main{grid-template-columns:minmax(21rem,.95fr) minmax(24rem,1.05fr) 15rem;gap:1rem;min-height:0;padding:1.15rem;display:grid}markdown-workspace._class_ioksj_1>main>editor-pane,markdown-workspace._class_ioksj_1>main>preview-pane,markdown-workspace._class_ioksj_1>main>presence-rail{border:1px solid var(--border);background:var(--paper);border-radius:1.1rem;min-width:0;min-height:0;box-shadow:0 .9rem 2.4rem #2b21450c}markdown-workspace._class_ioksj_1>main>editor-pane,markdown-workspace._class_ioksj_1>main>preview-pane{grid-template-rows:auto minmax(0,1fr);display:grid;overflow:hidden}:is(markdown-workspace._class_ioksj_1>main>editor-pane,markdown-workspace._class_ioksj_1>main>preview-pane)>pane-heading{background:#fcfbfd;border-bottom:1px solid #ebe8ef;justify-content:space-between;align-items:center;min-height:3.15rem;padding:.75rem 1rem;display:flex}:is(markdown-workspace._class_ioksj_1>main>editor-pane,markdown-workspace._class_ioksj_1>main>preview-pane)>pane-heading>span{color:var(--muted);font-size:.68rem}markdown-workspace._class_ioksj_1>main>editor-pane>pane-heading>label{letter-spacing:.055em;text-transform:uppercase;font-size:.75rem;font-weight:800}markdown-workspace._class_ioksj_1>main>editor-pane>editor-surface{background:linear-gradient(90deg, #f8f6fb 2.75rem, transparent 2.75rem), var(--panel);min-height:0;position:relative}markdown-workspace._class_ioksj_1>main>editor-pane>editor-surface>textarea{z-index:1;resize:none;color:#383542;width:100%;height:100%;caret-color:var(--accent);tab-size:2;background:0 0;border:0;outline:0;padding:1.45rem 1.45rem 8rem 3.7rem;font-family:DM Mono,monospace;font-size:.83rem;line-height:1.72rem;position:absolute;inset:0}markdown-workspace._class_ioksj_1>main>editor-pane>editor-surface>textarea:disabled{opacity:.58}markdown-workspace._class_ioksj_1>main>editor-pane>editor-surface>caret-layer{z-index:2;pointer-events:none;position:absolute;inset:0;overflow:hidden}markdown-workspace._class_ioksj_1>main>editor-pane>editor-surface>caret-layer>remote-caret{top:calc(1.45rem + var(--caret-line) * 1.72rem);left:calc(3.7rem + var(--caret-column) * .5rem);background:var(--person-color);width:2px;height:1.25rem;position:absolute}markdown-workspace._class_ioksj_1>main>editor-pane>editor-surface>caret-layer>remote-caret>span{color:#fff;background:var(--person-color);white-space:nowrap;border-radius:.3rem .3rem .3rem 0;padding:.14rem .35rem;font-size:.56rem;font-weight:800;position:absolute;top:-1.05rem;left:0}markdown-workspace._class_ioksj_1>main>preview-pane>pane-heading>strong{letter-spacing:.055em;text-transform:uppercase;font-size:.75rem;font-weight:800}markdown-workspace._class_ioksj_1>main>preview-pane>article{color:#37333d;padding:clamp(1.6rem,4vw,4rem);font-family:Newsreader,Georgia,serif;font-size:1.05rem;line-height:1.72;overflow:auto}markdown-workspace._class_ioksj_1>main>preview-pane>article>h1,markdown-workspace._class_ioksj_1>main>preview-pane>article>h2,markdown-workspace._class_ioksj_1>main>preview-pane>article>h3,markdown-workspace._class_ioksj_1>main>preview-pane>article>h4,markdown-workspace._class_ioksj_1>main>preview-pane>article>h5,markdown-workspace._class_ioksj_1>main>preview-pane>article>h6{color:#282430;letter-spacing:-.025em;margin:1.8em 0 .55em;font-weight:650;line-height:1.1}markdown-workspace._class_ioksj_1>main>preview-pane>article>h1:first-child{margin-top:0;font-size:clamp(2.15rem,5vw,3.5rem)}markdown-workspace._class_ioksj_1>main>preview-pane>article>h2{font-size:1.55rem}markdown-workspace._class_ioksj_1>main>preview-pane>article>p,markdown-workspace._class_ioksj_1>main>preview-pane>article>ul,markdown-workspace._class_ioksj_1>main>preview-pane>article>ol,markdown-workspace._class_ioksj_1>main>preview-pane>article>blockquote,markdown-workspace._class_ioksj_1>main>preview-pane>article>pre{margin:0 0 1.25rem}markdown-workspace._class_ioksj_1>main>preview-pane>article>ul,markdown-workspace._class_ioksj_1>main>preview-pane>article>ol{padding-left:1.35rem}markdown-workspace._class_ioksj_1>main>preview-pane>article>blockquote{color:#5d566b;background:#f7f4ff;border-left:3px solid #b5a5f6;padding:.75rem 1.1rem;font-style:italic}markdown-workspace._class_ioksj_1>main>preview-pane>article>pre{color:#ede9ff;background:#302c3a;border:1px solid #e2deea;border-radius:.7rem;padding:1rem 1.15rem;font-family:DM Mono,monospace;font-size:.76rem;line-height:1.65;overflow:auto}markdown-workspace._class_ioksj_1>main>preview-pane>article strong{color:#332d42;font-weight:650}markdown-workspace._class_ioksj_1>main>preview-pane>article code:not(pre code){color:#6547d6;background:#f0ecff;border-radius:.25rem;padding:.15em .35em;font-family:DM Mono,monospace;font-size:.8em}markdown-workspace._class_ioksj_1>main>preview-pane>article a{color:#6547d6;text-underline-offset:.15em;text-decoration-color:#b9abf2}markdown-workspace._class_ioksj_1>main>presence-rail{flex-flow:column;padding:1.15rem;display:flex;overflow:auto}markdown-workspace._class_ioksj_1>main>presence-rail>strong{letter-spacing:.055em;text-transform:uppercase;font-size:.72rem;font-weight:800}markdown-workspace._class_ioksj_1>main>presence-rail>ul{flex-flow:column;gap:.85rem;margin:1.1rem 0 0;padding:0;list-style:none;display:flex}markdown-workspace._class_ioksj_1>main>presence-rail>ul>li{grid-template-columns:auto minmax(0,1fr);align-items:center;gap:.65rem;display:grid}markdown-workspace._class_ioksj_1>main>presence-rail>ul>li>avatar-dot{aspect-ratio:1;color:#fff;background:var(--person-color);width:2rem;box-shadow:0 0 0 1px var(--person-color);border:2px solid #fff;border-radius:50%;place-items:center;font-size:.72rem;font-weight:800;display:grid}markdown-workspace._class_ioksj_1>main>presence-rail>ul>li>person-label{flex-flow:column;min-width:0;display:flex}markdown-workspace._class_ioksj_1>main>presence-rail>ul>li>person-label>strong{text-overflow:ellipsis;white-space:nowrap;font-size:.76rem;overflow:hidden}markdown-workspace._class_ioksj_1>main>presence-rail>ul>li>person-label>span{color:var(--muted);margin-top:.12rem;font-size:.62rem}markdown-workspace._class_ioksj_1>main>presence-rail>aside{background:linear-gradient(145deg,#f9f7ff,#f1edff);border:1px solid #e3dcff;border-radius:.8rem;margin-top:auto;padding:.9rem}markdown-workspace._class_ioksj_1>main>presence-rail>aside>strong{color:#5941b4;font-size:.7rem}markdown-workspace._class_ioksj_1>main>presence-rail>aside>p{color:#6d6680;margin:.4rem 0 0;font-size:.65rem;line-height:1.55}markdown-workspace._class_ioksj_1>footer{min-height:2.6rem;color:var(--muted);background:#faf9fc;border-top:1px solid #e2dfe8;align-items:center;gap:.45rem;padding:.55rem 1.5rem;font-size:.66rem;display:flex}markdown-workspace._class_ioksj_1>footer>span{aspect-ratio:1;background:#aaa5b3;border-radius:50%;width:.48rem}markdown-workspace._class_ioksj_1>footer>span[data-status=live]{background:#21a879;box-shadow:0 0 0 .22rem #21a8791c}markdown-workspace._class_ioksj_1>footer>span[data-status=offline]{background:#df8c35}markdown-workspace._class_ioksj_1>footer>strong{color:#5f5b69;font-weight:700}markdown-workspace._class_ioksj_1>footer>small{margin-left:auto}markdown-workspace._class_ioksj_1>footer>output{color:#a5435e}@media (width<=1080px){markdown-workspace._class_ioksj_1>header{grid-template-columns:1fr 1fr}markdown-workspace._class_ioksj_1>header>document-title{display:none}markdown-workspace._class_ioksj_1>main{grid-template-columns:minmax(20rem,1fr) minmax(22rem,1fr)}markdown-workspace._class_ioksj_1>main>presence-rail{display:none}}@media (width<=720px){markdown-workspace._class_ioksj_1>header{grid-template-columns:1fr auto;padding-inline:.85rem}markdown-workspace._class_ioksj_1>header>toolbar-actions>button>span{display:none}markdown-workspace._class_ioksj_1>header>toolbar-actions>label>select{display:none}markdown-workspace._class_ioksj_1>main{flex-flow:column;min-height:auto;padding:.7rem;display:flex}markdown-workspace._class_ioksj_1>main>editor-pane,markdown-workspace._class_ioksj_1>main>preview-pane{min-height:62svh}}
|
package/dist/index.html
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
|
+
<meta
|
|
7
|
+
name="description"
|
|
8
|
+
content="Mosaic, a realtime collaborative Markdown editor built with atom.io."
|
|
9
|
+
/>
|
|
10
|
+
<title>Mosaic · Shared Markdown</title>
|
|
11
|
+
<script type="module" crossorigin src="/assets/index-jw83brgG.js"></script>
|
|
12
|
+
<link rel="stylesheet" crossorigin href="/assets/index-DGIOrh2O.css">
|
|
13
|
+
</head>
|
|
14
|
+
<body>
|
|
15
|
+
<main id="root"></main>
|
|
16
|
+
</body>
|
|
17
|
+
</html>
|
package/eslint.config.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import * as tsParser from "@typescript-eslint/parser"
|
|
2
|
+
import AtomIOPlugin from "atom.io/eslint-plugin"
|
|
3
|
+
import type { Linter } from "eslint"
|
|
4
|
+
import LasertagPlugin from "lasertag/eslint-plugin"
|
|
5
|
+
|
|
6
|
+
const ERROR = 2
|
|
7
|
+
|
|
8
|
+
export default [
|
|
9
|
+
{
|
|
10
|
+
ignores: [`**/dist/**`, `**/node_modules/**`],
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
files: [`**/*.ts`, `**/*.tsx`],
|
|
14
|
+
languageOptions: {
|
|
15
|
+
parser: tsParser,
|
|
16
|
+
parserOptions: { projectService: true, sourceType: `module` },
|
|
17
|
+
},
|
|
18
|
+
plugins: { "atom.io": AtomIOPlugin },
|
|
19
|
+
rules: {
|
|
20
|
+
"atom.io/exact-catch-types": ERROR,
|
|
21
|
+
"atom.io/explicit-state-types": [ERROR, { permitAnnotation: true }],
|
|
22
|
+
"atom.io/naming-convention": ERROR,
|
|
23
|
+
quotes: [ERROR, `backtick`],
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
files: [`src/**/*.tsx`],
|
|
28
|
+
plugins: { lasertag: LasertagPlugin },
|
|
29
|
+
rules: {
|
|
30
|
+
"lasertag/access-css-module-class-only": ERROR,
|
|
31
|
+
"lasertag/ban-div": ERROR,
|
|
32
|
+
"lasertag/export-own-component-only": ERROR,
|
|
33
|
+
"lasertag/header-main-footer-as-group": ERROR,
|
|
34
|
+
"lasertag/import-own-css-module-only": ERROR,
|
|
35
|
+
"lasertag/name-imported-css-module-as-css": ERROR,
|
|
36
|
+
"lasertag/render-tag-with-own-name": ERROR,
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
] satisfies Linter.Config[]
|
package/index.html
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
|
+
<meta
|
|
7
|
+
name="description"
|
|
8
|
+
content="Mosaic, a realtime collaborative Markdown editor built with atom.io."
|
|
9
|
+
/>
|
|
10
|
+
<title>Mosaic · Shared Markdown</title>
|
|
11
|
+
</head>
|
|
12
|
+
<body>
|
|
13
|
+
<main id="root"></main>
|
|
14
|
+
<script type="module" src="/src/main.tsx"></script>
|
|
15
|
+
</body>
|
|
16
|
+
</html>
|
package/mise.toml
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
[tools]
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
MosaicTextOperation,
|
|
3
|
+
MosaicTextSelection,
|
|
4
|
+
MosaicTextView,
|
|
5
|
+
} from "atom.io/realtime"
|
|
6
|
+
import { defineMosaicAtomRegistration } from "atom.io/realtime-server"
|
|
7
|
+
import { z } from "zod"
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
Markdown,
|
|
11
|
+
markdownAtom,
|
|
12
|
+
type MarkdownPresence,
|
|
13
|
+
} from "../src/collaboration/mosaic.ts"
|
|
14
|
+
|
|
15
|
+
const idSchema = z.string().min(1).max(512)
|
|
16
|
+
const nullableIdSchema = idSchema.nullable()
|
|
17
|
+
|
|
18
|
+
const relativePositionSchema = z
|
|
19
|
+
.object({
|
|
20
|
+
affinity: z.enum([`left`, `right`]),
|
|
21
|
+
leftId: nullableIdSchema,
|
|
22
|
+
rightId: nullableIdSchema,
|
|
23
|
+
})
|
|
24
|
+
.strict()
|
|
25
|
+
|
|
26
|
+
const selectionSchema = z
|
|
27
|
+
.object({
|
|
28
|
+
anchor: relativePositionSchema,
|
|
29
|
+
head: relativePositionSchema,
|
|
30
|
+
})
|
|
31
|
+
.strict()
|
|
32
|
+
|
|
33
|
+
const operationSchema = z.discriminatedUnion(`type`, [
|
|
34
|
+
z
|
|
35
|
+
.object({
|
|
36
|
+
deletedIds: z.array(idSchema).max(200_000),
|
|
37
|
+
inserted: z
|
|
38
|
+
.array(
|
|
39
|
+
z
|
|
40
|
+
.object({
|
|
41
|
+
after: nullableIdSchema,
|
|
42
|
+
before: nullableIdSchema,
|
|
43
|
+
id: idSchema,
|
|
44
|
+
value: z.string().max(1_024),
|
|
45
|
+
})
|
|
46
|
+
.strict(),
|
|
47
|
+
)
|
|
48
|
+
.max(200_000),
|
|
49
|
+
type: z.literal(`edit`),
|
|
50
|
+
})
|
|
51
|
+
.strict(),
|
|
52
|
+
z
|
|
53
|
+
.object({
|
|
54
|
+
mode: z.enum([`redo`, `undo`]),
|
|
55
|
+
targetOperationIds: z.array(idSchema).min(1).max(10_000),
|
|
56
|
+
type: z.literal(`history`),
|
|
57
|
+
})
|
|
58
|
+
.strict(),
|
|
59
|
+
]) satisfies z.ZodType<MosaicTextOperation>
|
|
60
|
+
|
|
61
|
+
const presenceSchema = z
|
|
62
|
+
.object({
|
|
63
|
+
color: z.string().min(1).max(64),
|
|
64
|
+
lastActiveAt: z.number().finite().nonnegative(),
|
|
65
|
+
name: z.string().min(1).max(128),
|
|
66
|
+
selection: selectionSchema.nullable(),
|
|
67
|
+
})
|
|
68
|
+
.strict() satisfies z.ZodType<MarkdownPresence>
|
|
69
|
+
|
|
70
|
+
const selectionResolvesWithin = (
|
|
71
|
+
document: MosaicTextView,
|
|
72
|
+
selection: MosaicTextSelection,
|
|
73
|
+
): boolean =>
|
|
74
|
+
[selection.anchor, selection.head].every((position) => {
|
|
75
|
+
const offset = document.resolvePosition(position)
|
|
76
|
+
return offset >= 0 && offset <= document.length
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
export const markdownAtomRegistration = defineMosaicAtomRegistration({
|
|
80
|
+
checkpointEvery: 128,
|
|
81
|
+
class: Markdown,
|
|
82
|
+
operationSchema,
|
|
83
|
+
presenceSchema,
|
|
84
|
+
target: markdownAtom,
|
|
85
|
+
validatePresence: (presence, { view }) =>
|
|
86
|
+
presence.selection === null ||
|
|
87
|
+
selectionResolvesWithin(view, presence.selection),
|
|
88
|
+
})
|
package/node/server.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { createServer } from "node:http"
|
|
2
|
+
|
|
3
|
+
import { Silo } from "atom.io"
|
|
4
|
+
import type { UserKey } from "atom.io/realtime"
|
|
5
|
+
import { createMosaicServer, realtime } from "atom.io/realtime-server"
|
|
6
|
+
import { Server } from "socket.io"
|
|
7
|
+
|
|
8
|
+
import { identityById } from "../src/identities.ts"
|
|
9
|
+
import { markdownAtomRegistration } from "./mosaic-atom.ts"
|
|
10
|
+
|
|
11
|
+
const PORT = 3000
|
|
12
|
+
const mosaic = createMosaicServer({ registrations: [markdownAtomRegistration] })
|
|
13
|
+
const silo = new Silo({
|
|
14
|
+
name: `markdown-collaboration-server`,
|
|
15
|
+
lifespan: `immortal`,
|
|
16
|
+
isProduction: process.env.NODE_ENV === `production`,
|
|
17
|
+
})
|
|
18
|
+
const httpServer = createServer((request, response) => {
|
|
19
|
+
if (request.url === `/health`) {
|
|
20
|
+
response.writeHead(200, { "content-type": `application/json` })
|
|
21
|
+
response.end(JSON.stringify({ ok: true }))
|
|
22
|
+
return
|
|
23
|
+
}
|
|
24
|
+
response.writeHead(404)
|
|
25
|
+
response.end()
|
|
26
|
+
})
|
|
27
|
+
const socketServer = new Server(httpServer)
|
|
28
|
+
|
|
29
|
+
const disposeRealtime = realtime(
|
|
30
|
+
socketServer,
|
|
31
|
+
(handshake) => {
|
|
32
|
+
const requestedId =
|
|
33
|
+
typeof handshake.auth.userId === `string`
|
|
34
|
+
? handshake.auth.userId
|
|
35
|
+
: undefined
|
|
36
|
+
const sessionId =
|
|
37
|
+
typeof handshake.auth.sessionId === `string`
|
|
38
|
+
? handshake.auth.sessionId
|
|
39
|
+
: undefined
|
|
40
|
+
if (!sessionId || sessionId.includes(`::`)) {
|
|
41
|
+
return new Error(`A valid Mosaic session is required.`)
|
|
42
|
+
}
|
|
43
|
+
const identity = identityById(requestedId)
|
|
44
|
+
return `user::${identity.id}::${sessionId}` satisfies UserKey
|
|
45
|
+
},
|
|
46
|
+
({ socket, consumer }) => {
|
|
47
|
+
const [, actor, session] = consumer.split(`::`)
|
|
48
|
+
if (!actor || !session) {
|
|
49
|
+
throw new Error(`Malformed Mosaic user session.`)
|
|
50
|
+
}
|
|
51
|
+
return mosaic.connect({ actor, session, socket })
|
|
52
|
+
},
|
|
53
|
+
silo.store,
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
httpServer.listen(PORT, () => {
|
|
57
|
+
console.log(
|
|
58
|
+
`Markdown collaboration server listening on http://localhost:${PORT}`,
|
|
59
|
+
)
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
const shutdown = async (): Promise<void> => {
|
|
63
|
+
await disposeRealtime()
|
|
64
|
+
await mosaic.dispose()
|
|
65
|
+
}
|
|
66
|
+
process.once(`SIGINT`, () => void shutdown())
|
|
67
|
+
process.once(`SIGTERM`, () => void shutdown())
|