Skip to content

Instantly share code, notes, and snippets.

@Fumesover
Last active January 8, 2019 19:53
Show Gist options
  • Save Fumesover/3e5c5529a493635126a264878c2a1fa0 to your computer and use it in GitHub Desktop.
Save Fumesover/3e5c5529a493635126a264878c2a1fa0 to your computer and use it in GitHub Desktop.
CodiMD Edit test
#!/bin/bash
# Variables for tests
SERVER= # codimd instance
NOTEID= # a note id
# Authentification is handeled by https://github.com/hackmdio/codimd-cli/pull/9
# The socket url
URL=$SERVER/socket.io/?noteId=$NOTEID\&EIO=3\&transport=polling
# get the 'io' token
curl -c /tmp/codimd-cookies.txt \
-b /tmp/codimd-cookies.txt \
$URL\&t=$(python yeast.py) \
&> /dev/null
TOKEN=$(cat /tmp/codimd-cookies.txt | grep io | awk '{print $7}')
# Now, send some payloads to the socket.io Websocket
curl -b /tmp/codimd-cookies.txt \
$URL\&t\=$(python yeast.py)\&sid\=$TOKEN \
-H "Content-Type: text/plain;charset=UTF-8" \
-d @msg_0
curl -b /tmp/codimd-cookies.txt \
$URL\&t\=$(python yeast.py)\&sid\=$TOKEN \
-H "Content-Type: text/plain;charset=UTF-8" \
-d @msg_1
curl -b /tmp/codimd-cookies.txt \
$URL\&t\=$(python yeast.py)\&sid\=$TOKEN \
-H "Content-Type: text/plain;charset=UTF-8" \
-d @msg_2
curl -b /tmp/codimd-cookies.txt \
$URL\&t\=$(python yeast.py)\&sid\=$TOKEN \
-H "Content-Type: text/plain;charset=UTF-8" \
-d @msg_3
20:42["selection",null]
50:42["cursor focus",{"line":0,"ch":8,"sticky":null}]
50:42["selection",{"ranges":[{"anchor":8,"head":8}]}]
59:42["operation",0,[7,-1],{"ranges":[{"anchor":7,"head":7}]}]
#!/usr/bin/env python
# yeast.py: Tiny script to generate the timestampParam required by the socket.io server
# Author: https://gist.github.com/avramit/271d1cd1465022ba17218609343ae5a7
import math
from datetime import datetime
import http.client, urllib.parse
class Yeast:
def __init__(self):
self.alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_'
self.length = 64
self.seed = 0
self.prev = None
def encode(self, num):
encoded = ''
while num > 0:
encoded = self.alphabet[num % self.length] + encoded
num = math.floor(num / self.length)
return encoded
def decode(self, encoded):
decoded = 0
for i in range(len(encoded)):
decoded = decoded * self.length + self.alphabet.index(encoded[i])
return decoded
def __call__(self):
now = self.encode(int(datetime.now().timestamp() * 1000))
if now != self.prev:
self.seed = 0
self.prev = now
return now
self.seed += 1
return f'{now}.{self.encode(self.seed)}'
yeast = Yeast()
print(yeast())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment