• Stars
    star
    109
  • Rank 323,694 (Top 7 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated 17 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A JavaScript client that speaks Replit's container protocol

Installation

yarn add @replit/crosis @replit/protocol

Crosis relies on the @replit/protocol package as a peer dependency.

Prerequisites

You should probably familiarize yourself with the protocol before trying to use it. Crosis is just a client that helps you connect and communicate with the container using the protocol.

Read about the protocol here https://crosis-doc.util.repl.co/

Usage and concepts

The central concept is a "channel" that you can send commands to and receive commands from. Communicating with channels requires a network connection. The goal of this client is to provide an API to manage the connection (including disconnects and reconnects), opening channels, and a way to send a receive messages/commands on channels. How you handle this is up to you and depends on the desired UX. In some cases you'll want to disable UI to prevent any new messages from being sent when offline and then re-enable once connected again. In other cases you might want to give the user the illusion that they are connected and queue messages locally while disconnected and send them once reconnected.

Here is an example usage, for more details on usage please refer to the API docs at https://crosisdoc.util.repl.co/

import { Client } from '@replit/crosis';

const client = new Client<{ user: { name: string }; repl: { id: string } }>();

const repl = { id: 'someuuid' };

async function fetchConnectionMetadata(
  signal: AbortSignal,
): Promise<FetchConnectionMetadataResult> {
  let res: Response;
  try {
    res = await fetch(CONNECTION_METADATA_URL + repl.id, { signal });
  } catch (error) {
    if (error.name === 'AbortError') {
      return {
        error: FetchConnectionMetadataError.Aborted,
      };
    }

    throw error;
  }

  if (!res.ok) {
    if (res.status > 500) {
      // Network or server error, try again
      return {
        error: FetchConnectionMetadataError.Retriable,
      };
    }

    const errorText = await res.text();
    throw new Error(errorText || res.statusText);
  }

  const connectionMetadata = await res.json();

  return {
    token: connectionMetadata.token,
    gurl: connectionMetadata.gurl,
    conmanURL: connectionMetadata.conmanURL,
    error: null,
  };
}

const user = { name: 'tim' };

const context = { user, repl };

client.open({ context, fetchConnectionMetadata }, function onOpen({ channel, context }) {
  //  The client is now connected (or reconnected in the event that it encountered an unexpected disconnect)
  // `channel` here is channel0 (more info at https://crosis-doc.util.repl.co/protov2)
  // - send commands using `channel.send`
  // - listen for commands using `channel.onCommand(cmd => ...)`

  return function cleanup({ willReconnect }) {
    // The client was closed and might reconnect if it was closed unexpectedly
  };
});

// See docs for exec service here https://crosis-doc.util.repl.co/services#exec
const closeChannel = client.openChannel({ service: 'exec' }, function open({ channel, context }) {
  channel.onCommand((cmd) => {
    if (cmd.output) {
      terminal.write(cmd.output);
    }
  });

  const intervalId = setInterval(() => {
    channel.send({
      exec: { args: ['echo', 'hello', context.user.name] },
      blocking: true,
    });
  }, 100);

  return function cleanup({ willReconnect }) {
    clearInterval(intervalId);
  };
});

Developing

To run tests run

USER_KEY_ID=XXXX USER_PRIVATE_KEY=XXXX yarn test

To interact with a connected client in the browser run

USER_KEY_ID=XXXX USER_PRIVATE_KEY=XXXX yarn debug

You can then access the client from the console an send messages like:

window.client.send({ exec: { args: ['kill', '1'] } });

Releasing

To release, just run USER_KEY_ID=XXXX USER_PRIVATE_KEY=XXXX yarn version, it will prompt you for a version, then it will push to github and release to npm.

To update documentation, go to https://crosisdoc.util.repl.co/__repl and run . ./updatedocs.sh

More Repositories

1

kaboom

πŸ’₯ JavaScript game library
TypeScript
2,376
star
2

clui

CLUI is a collection of JavaScript libraries for building command-driven interfaces with context-aware autocomplete.
TypeScript
1,233
star
3

upm

β • Universal Package Manager - Python, Node.js, Ruby, Emacs Lisp.
Go
1,021
star
4

ReplitLM

Inference code and configs for the ReplitLM model family
Python
900
star
5

polygott

Base Docker image for the Repl.it evaluation server
Shell
386
star
6

codemirror-vim

Vim keybindings for CM6
JavaScript
244
star
7

prybar

Pry open those interpreters.
Go
242
star
8

play

The easiest way to start coding games and graphics projects in Python
Python
182
star
9

replit-py

A helpful Python package that helps you build excellent things inside Repls! πŸ’»
Python
151
star
10

codemirror-interact

TypeScript
92
star
11

ReplitClient.js

A JavaScript client library used to connect to the server-side code execution service
67
star
12

replit-vscode

Replit in vscode
TypeScript
54
star
13

evalbot

A bot that speaks code
JavaScript
48
star
14

codemirror-indentation-markers

A CodeMirror extension that renders indentation markers
TypeScript
44
star
15

repl.sh

Sometimes you need a shell (alpha release)
JavaScript
37
star
16

extensions

A client library for the Replit Extensions API
TypeScript
33
star
17

Codemirror-CSS-color-picker

A codemirror extension that adds a color picker input next to CSS color values. Development happens on Replit, just fork and hit run! https://replit.com/@util/Codemirror-CSS-color-picker
TypeScript
30
star
18

codemirror-vscode-keymap

VSCode keyboard shortcuts for CodeMirror 6
TypeScript
29
star
19

nixmodules

Nix
27
star
20

codemirror-lang-svelte

Svelte language support for CodeMirror 6
TypeScript
26
star
21

codemirror-minimap

Minimap extension for Codemirror 6
TypeScript
25
star
22

river

Long-lived Streaming Remote Procedure Calls
TypeScript
24
star
23

database-go

Go client for Repl.it Database
Go
22
star
24

pyright-extended

pyright with yapf and ruff
Python
20
star
25

nixpkgs-replit

Nix
19
star
26

rfbproxy

An RFB proxy that enables WebSockets and audio.
Nix
19
star
27

blog

the code behind https://blog.replit.com/
EJS
19
star
28

replit-code-exec

A library that allows interacting with Replit's code-exec API
Python
18
star
29

protocol

The Protocol definition file and language bindings
JavaScript
17
star
30

codemirror-emacs

Emacs keybindings for CM6
TypeScript
14
star
31

express-router-async

Creates an express router augmented with promise handling routes
JavaScript
13
star
32

replbox

JavaScript
12
star
33

codemirror-lang-nix

A Nix language mode for CodeMirror 6
TypeScript
11
star
34

kaboomware

TypeScript
9
star
35

go-replidentity

Go implementation of Repl Identity
Go
9
star
36

extensions-react

Replit Extensions React Client
TypeScript
9
star
37

nix-editor

an automatic editor for replit.nix files
Rust
8
star
38

replit-ai-python

The library for Replit AI
Python
7
star
39

repl-auth

Allow your users to sign in with Replit!
JavaScript
7
star
40

audio-libs

Audio bindings for javascript
Python
6
star
41

toml-editor

Rust
6
star
42

codemirror-lang-csharp

TypeScript
6
star
43

homebrew-tap

Homebrew tap for Repl.it open-source.
Ruby
5
star
44

replit-node

A node.js library that helps you build excellent things inside Repls!
TypeScript
5
star
45

replit-ai-modelfarm-typescript

TypeScript
4
star
46

nixsysdeps

A mapping from language packages to nixpkgs system dependencies
Shell
4
star
47

codemirror-lang-solidity

CodeMirror 6 extension for Solidity language support and syntax highlighting
TypeScript
4
star
48

ztoc-rs

SOCI ztoc index builder
Rust
4
star
49

alcor

Serialize and hydrate complex javascript objects
TypeScript
4
star
50

replkit

TypeScript
3
star
51

kaboomsite

Website for Kaboom.js
JavaScript
2
star
52

example-repltweet

An example of using replit-py for exploring the world of web applications! πŸ—Ί
JavaScript
2
star
53

abcd-amasad

abcd-amasad created by GitHub Classroom
HTML
2
star
54

scoop-bucket

Scoop bucket for Repl.it open-source.
2
star
55

heroku-bot

A bot that responds to a slack slash command with a diff of commits going out to heroku
JavaScript
1
star
56

ruspty

PTY for Bun (and Node) through Rust FFI
JavaScript
1
star
57

repl-auth-node

Nodejs client for Repl Auth functions!
JavaScript
1
star
58

protocol-go

Mirror for Go package for repl.it protocol. Actual package here https://github.com/replit/protocol/
1
star