mirror of
https://github.com/tloncorp/landscape.git
synced 2025-01-05 07:13:24 +03:00
devex: move to ui/, remove graph and lib
also uninstall @urbit/api
This commit is contained in:
parent
24691c9136
commit
62833d42ee
@ -1,534 +0,0 @@
|
||||
import { GroupPolicy, makeResource, Resource, resourceFromPath } from '../groups';
|
||||
|
||||
import { decToUd, deSig, unixToDa, Scry } from '../lib';
|
||||
import { Enc, Path, Patp, PatpNoSig, Poke, Thread } from '../lib/types';
|
||||
import { Content, GraphChildrenPoke, GraphNode, GraphNodePoke, Post } from './types';
|
||||
import { patp2dec } from 'urbit-ob';
|
||||
|
||||
export const GRAPH_UPDATE_VERSION = 3;
|
||||
|
||||
export const createBlankNodeWithChildPost = (
|
||||
ship: PatpNoSig,
|
||||
parentIndex = '',
|
||||
childIndex = '',
|
||||
contents: Content[]
|
||||
): GraphNodePoke => {
|
||||
const date = unixToDa(Date.now()).toString();
|
||||
const nodeIndex = parentIndex + '/' + date;
|
||||
|
||||
const childGraph: GraphChildrenPoke = {};
|
||||
childGraph[childIndex] = {
|
||||
post: {
|
||||
author: `~${ship}`,
|
||||
index: nodeIndex + '/' + childIndex,
|
||||
'time-sent': Date.now(),
|
||||
contents,
|
||||
hash: null,
|
||||
signatures: []
|
||||
},
|
||||
children: null
|
||||
};
|
||||
|
||||
return {
|
||||
post: {
|
||||
author: `~${ship}`,
|
||||
index: nodeIndex,
|
||||
'time-sent': Date.now(),
|
||||
contents: [],
|
||||
hash: null,
|
||||
signatures: []
|
||||
},
|
||||
children: childGraph
|
||||
};
|
||||
};
|
||||
|
||||
export const markPending = (nodes: any): any => {
|
||||
Object.keys(nodes).forEach((key) => {
|
||||
nodes[key].post.author = deSig(nodes[key].post.author);
|
||||
nodes[key].post.pending = true;
|
||||
if (nodes[key].children) {
|
||||
nodes[key].children = markPending(nodes[key].children);
|
||||
}
|
||||
});
|
||||
return nodes;
|
||||
};
|
||||
|
||||
export const createPost = (
|
||||
ship: PatpNoSig,
|
||||
contents: Content[],
|
||||
parentIndex = '',
|
||||
childIndex = 'DATE_PLACEHOLDER'
|
||||
): Post => {
|
||||
if (childIndex === 'DATE_PLACEHOLDER') {
|
||||
childIndex = unixToDa(Date.now()).toString();
|
||||
}
|
||||
return {
|
||||
author: `~${ship}`,
|
||||
index: parentIndex + '/' + childIndex,
|
||||
'time-sent': Date.now(),
|
||||
contents,
|
||||
hash: null,
|
||||
signatures: []
|
||||
};
|
||||
};
|
||||
|
||||
function moduleToMark(mod: string): string | undefined {
|
||||
if(mod === 'link') {
|
||||
return 'graph-validator-link';
|
||||
}
|
||||
if(mod === 'publish') {
|
||||
return 'graph-validator-publish';
|
||||
}
|
||||
if(mod === 'chat') {
|
||||
return 'graph-validator-chat';
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const storeAction = <T>(data: T, version: number = GRAPH_UPDATE_VERSION): Poke<T> => ({
|
||||
app: 'graph-store',
|
||||
mark: `graph-update-${version}`,
|
||||
json: data
|
||||
});
|
||||
|
||||
export { storeAction as graphStoreAction };
|
||||
|
||||
const viewAction = <T>(threadName: string, action: T): Thread<T> => ({
|
||||
inputMark: 'graph-view-action',
|
||||
outputMark: 'json',
|
||||
threadName,
|
||||
body: action
|
||||
});
|
||||
|
||||
export { viewAction as graphViewAction };
|
||||
|
||||
const hookAction = <T>(data: T, version: number = GRAPH_UPDATE_VERSION): Poke<T> => ({
|
||||
app: 'graph-push-hook',
|
||||
mark: `graph-update-${version}`,
|
||||
json: data
|
||||
});
|
||||
|
||||
const dmAction = <T>(data: T): Poke<T> => ({
|
||||
app: 'dm-hook',
|
||||
mark: 'dm-hook-action',
|
||||
json: data
|
||||
});
|
||||
|
||||
export { hookAction as graphHookAction };
|
||||
|
||||
export const createManagedGraph = (
|
||||
ship: PatpNoSig,
|
||||
name: string,
|
||||
title: string,
|
||||
description: string,
|
||||
group: Path,
|
||||
mod: string
|
||||
): Thread<any> => {
|
||||
const associated = { group: resourceFromPath(group) };
|
||||
const resource = makeResource(`~${ship}`, name);
|
||||
|
||||
return viewAction('graph-create', {
|
||||
create: {
|
||||
resource,
|
||||
title,
|
||||
description,
|
||||
associated,
|
||||
module: mod,
|
||||
mark: moduleToMark(mod)
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export const createUnmanagedGraph = (
|
||||
ship: PatpNoSig,
|
||||
name: string,
|
||||
title: string,
|
||||
description: string,
|
||||
policy: Enc<GroupPolicy>,
|
||||
mod: string
|
||||
): Thread<any> => viewAction('graph-create', {
|
||||
create: {
|
||||
resource: makeResource(`~${ship}`, name),
|
||||
title,
|
||||
description,
|
||||
associated: { policy },
|
||||
module: mod,
|
||||
mark: moduleToMark(mod)
|
||||
}
|
||||
});
|
||||
|
||||
export const joinGraph = (
|
||||
ship: Patp,
|
||||
name: string
|
||||
): Thread<any> => viewAction('graph-join', {
|
||||
join: {
|
||||
resource: makeResource(ship, name),
|
||||
ship
|
||||
}
|
||||
});
|
||||
|
||||
export const deleteGraph = (
|
||||
ship: PatpNoSig,
|
||||
name: string
|
||||
): Thread<any> => viewAction('graph-delete', {
|
||||
'delete': {
|
||||
resource: makeResource(`~${ship}`, name)
|
||||
}
|
||||
});
|
||||
|
||||
export const leaveGraph = (
|
||||
ship: Patp,
|
||||
name: string
|
||||
): Thread<any> => viewAction('graph-leave', {
|
||||
'leave': {
|
||||
resource: makeResource(ship, name)
|
||||
}
|
||||
});
|
||||
|
||||
export const groupifyGraph = (
|
||||
ship: Patp,
|
||||
name: string,
|
||||
toPath?: string
|
||||
): Thread<any> => {
|
||||
const resource = makeResource(ship, name);
|
||||
const to = toPath && resourceFromPath(toPath);
|
||||
|
||||
return viewAction('graph-groupify', {
|
||||
groupify: {
|
||||
resource,
|
||||
to
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export const evalCord = (
|
||||
cord: string
|
||||
): Thread<any> => {
|
||||
return ({
|
||||
inputMark: 'graph-view-action',
|
||||
outputMark: 'tang',
|
||||
threadName: 'graph-eval',
|
||||
body: {
|
||||
eval: cord
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export const addGraph = (
|
||||
ship: Patp,
|
||||
name: string,
|
||||
graph: any,
|
||||
mark: any
|
||||
): Poke<any> => {
|
||||
return storeAction({
|
||||
'add-graph': {
|
||||
resource: { ship, name },
|
||||
graph,
|
||||
mark
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export const addNodes = (
|
||||
ship: Patp,
|
||||
name: string,
|
||||
nodes: Object
|
||||
): Thread<any> => ({
|
||||
inputMark: `graph-update-${GRAPH_UPDATE_VERSION}`,
|
||||
outputMark: 'graph-view-action',
|
||||
threadName: 'graph-add-nodes',
|
||||
body: {
|
||||
'add-nodes': {
|
||||
resource: { ship, name },
|
||||
nodes
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export const addPost = (
|
||||
ship: Patp,
|
||||
name: string,
|
||||
post: Post
|
||||
): Thread<any> => {
|
||||
const nodes: Record<string, GraphNode> = {};
|
||||
nodes[post.index] = {
|
||||
post,
|
||||
children: null
|
||||
};
|
||||
return addNodes(ship, name, nodes);
|
||||
};
|
||||
|
||||
export const addNode = (
|
||||
ship: Patp,
|
||||
name: string,
|
||||
node: GraphNodePoke
|
||||
): Thread<any> => {
|
||||
const nodes: Record<string, GraphNodePoke> = {};
|
||||
nodes[node.post.index] = node;
|
||||
|
||||
return addNodes(ship, name, nodes);
|
||||
};
|
||||
|
||||
export const createGroupFeed = (
|
||||
group: Resource,
|
||||
vip: any = ''
|
||||
): Thread<any> => ({
|
||||
inputMark: 'graph-view-action',
|
||||
outputMark: 'resource',
|
||||
threadName: 'graph-create-group-feed',
|
||||
body: {
|
||||
'create-group-feed': {
|
||||
resource: group,
|
||||
vip
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export const disableGroupFeed = (
|
||||
group: Resource
|
||||
): Thread<any> => ({
|
||||
inputMark: 'graph-view-action',
|
||||
outputMark: 'json',
|
||||
threadName: 'graph-disable-group-feed',
|
||||
body: {
|
||||
'disable-group-feed': {
|
||||
resource: group
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Set dm-hook to screen new DMs or not
|
||||
*
|
||||
*/
|
||||
export const setScreen = (screen: boolean): Poke<any> => dmAction({ screen });
|
||||
|
||||
/**
|
||||
* Accept a pending DM request
|
||||
*
|
||||
* @param ship the ship to accept
|
||||
*/
|
||||
export const acceptDm = (ship: string) => dmAction({
|
||||
accept: ship
|
||||
});
|
||||
|
||||
/**
|
||||
* Decline a pending DM request
|
||||
*
|
||||
* @param ship the ship to accept
|
||||
*/
|
||||
export const declineDm = (ship: string) => dmAction({
|
||||
decline: ship
|
||||
});
|
||||
|
||||
/**
|
||||
* Remove posts from a set of indices
|
||||
*
|
||||
*/
|
||||
export const removePosts = (
|
||||
ship: Patp,
|
||||
name: string,
|
||||
indices: string[]
|
||||
): Poke<any> => hookAction({
|
||||
'remove-posts': {
|
||||
resource: { ship, name },
|
||||
indices
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Remove a DM message from our inbox
|
||||
*
|
||||
* @remarks
|
||||
* This does not remove the message from the recipients inbox
|
||||
*/
|
||||
export const removeDmMessage = (
|
||||
our: Patp,
|
||||
index: string
|
||||
): Poke<any> => ({
|
||||
app: 'graph-store',
|
||||
mark: `graph-update-${GRAPH_UPDATE_VERSION}`,
|
||||
json: {
|
||||
'remove-posts': {
|
||||
resource: { ship: our, name: 'dm-inbox' },
|
||||
indices: [index]
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Send a DM to a ship
|
||||
*
|
||||
* @param our sender
|
||||
* @param ship recipient
|
||||
* @param contents contents of message
|
||||
*/
|
||||
export const addDmMessage = (our: PatpNoSig, ship: Patp, contents: Content[]): Poke<any> => {
|
||||
const post = createPost(our, contents, `/${patp2dec(ship)}`);
|
||||
const node: GraphNode = {
|
||||
post,
|
||||
children: null
|
||||
};
|
||||
return {
|
||||
app: 'dm-hook',
|
||||
mark: `graph-update-${GRAPH_UPDATE_VERSION}`,
|
||||
json: {
|
||||
'add-nodes': {
|
||||
resource: { ship: `~${our}`, name: 'dm-inbox' },
|
||||
nodes: {
|
||||
[post.index]: node
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
const encodeIndex = (idx: string) => idx.split('/').map(decToUd).join('/');
|
||||
|
||||
/**
|
||||
* Fetch all graph keys
|
||||
*/
|
||||
export const getKeys = (): Scry => ({
|
||||
app: 'graph-store',
|
||||
path: '/keys'
|
||||
});
|
||||
|
||||
/**
|
||||
* Fetch newest (larger keys) nodes in a graph under some index
|
||||
*
|
||||
* @param ship ship of graph
|
||||
* @param name name of graph
|
||||
* @param count number of nodes to load
|
||||
* @param index index to query
|
||||
*/
|
||||
export const getNewest = (
|
||||
ship: string,
|
||||
name: string,
|
||||
count: number,
|
||||
index = ''
|
||||
): Scry => ({
|
||||
app: 'graph-store',
|
||||
path: `/graph/${ship}/${name}/node/siblings` +
|
||||
`/newest/lone/${count}${encodeIndex(index)}`
|
||||
});
|
||||
|
||||
/**
|
||||
* Fetch nodes in a graph that are older (key is smaller) and direct
|
||||
* siblings of some index
|
||||
*
|
||||
* @param ship ship of graph
|
||||
* @param name name of graph
|
||||
* @param count number of nodes to load
|
||||
* @param index index to query
|
||||
*/
|
||||
export const getOlderSiblings = (
|
||||
ship: string,
|
||||
name: string,
|
||||
count: number,
|
||||
index: string
|
||||
): Scry => ({
|
||||
app: 'graph-store',
|
||||
path: `/graph/${ship}/${name}/node/siblings/older/lone/${count}${encodeIndex(index)}`
|
||||
});
|
||||
|
||||
/**
|
||||
* Fetch nodes in a graph that are younger (key is larger) and direct
|
||||
* siblings of some index
|
||||
*
|
||||
* @param ship ship of graph
|
||||
* @param name name of graph
|
||||
* @param count number of nodes to load
|
||||
* @param index index to query
|
||||
*/
|
||||
export const getYoungerSiblings = (
|
||||
ship: string,
|
||||
name: string,
|
||||
count: number,
|
||||
index: string
|
||||
): Scry => ({
|
||||
app: 'graph-store',
|
||||
path: `/graph/${ship}/${name}/node/siblings/newer/lone/${count}${encodeIndex(index)}`
|
||||
});
|
||||
|
||||
/**
|
||||
* Fetch all nodes in a graph under some index, without loading children
|
||||
*
|
||||
* @param ship ship of graph
|
||||
* @param name name of graph
|
||||
* @param index index to query
|
||||
*/
|
||||
export const getShallowChildren = (ship: string, name: string, index = '') => ({
|
||||
app: 'graph-store',
|
||||
path: `/graph/${ship}/${name}/node/children/lone/~/~${encodeIndex(index)}`
|
||||
});
|
||||
|
||||
/**
|
||||
* Fetch newest nodes in a graph as a flat map, including children,
|
||||
* optionally starting at a specified key
|
||||
*
|
||||
* @param ship ship of graph
|
||||
* @param name name of graph
|
||||
* @param count number of nodes to load
|
||||
* @param start key to start at
|
||||
*
|
||||
*/
|
||||
export const getDeepOlderThan = (
|
||||
ship: string,
|
||||
name: string,
|
||||
count: number,
|
||||
start = ''
|
||||
) => ({
|
||||
app: 'graph-store',
|
||||
path: `/graph/${ship}/${name}/node/siblings` +
|
||||
`/${start.length > 0 ? 'older' : 'newest'}` +
|
||||
`/kith/${count}${encodeIndex(start)}`
|
||||
});
|
||||
|
||||
/**
|
||||
* Fetch a flat map of a nodes ancestors and firstborn children
|
||||
*
|
||||
* @param ship ship of graph
|
||||
* @param name name of graph
|
||||
* @param index index to query
|
||||
*
|
||||
*/
|
||||
export const getFirstborn = (
|
||||
ship: string,
|
||||
name: string,
|
||||
index: string
|
||||
): Scry => ({
|
||||
app: 'graph-store',
|
||||
path: `/graph/${ship}/${name}/node/firstborn${encodeIndex(index)}`
|
||||
});
|
||||
|
||||
/**
|
||||
* Fetch a single node, and all it's children
|
||||
*
|
||||
* @param ship ship of graph
|
||||
* @param name name of graph
|
||||
* @param index index to query
|
||||
*
|
||||
*/
|
||||
export const getNode = (
|
||||
ship: string,
|
||||
name: string,
|
||||
index: string
|
||||
): Scry => ({
|
||||
app: 'graph-store',
|
||||
path: `/graph/${ship}/${name}/node/index/kith${encodeIndex(index)}`
|
||||
});
|
||||
|
||||
/**
|
||||
* Fetch entire graph
|
||||
*
|
||||
* @param ship ship of graph
|
||||
* @param name name of graph
|
||||
*
|
||||
*/
|
||||
export const getGraph = (
|
||||
ship: string,
|
||||
name: string
|
||||
): Scry => ({
|
||||
app: 'graph-store',
|
||||
path: `/graph/${ship}/${name}`
|
||||
});
|
@ -1,94 +0,0 @@
|
||||
import { Patp } from '../lib';
|
||||
import { BigIntOrderedMap } from '../lib/BigIntOrderedMap';
|
||||
import { BigIntArrayOrderedMap } from '../lib/BigIntArrayOrderedMap';
|
||||
|
||||
export interface TextContent {
|
||||
text: string;
|
||||
}
|
||||
export interface UrlContent {
|
||||
url: string;
|
||||
}
|
||||
export interface CodeContent {
|
||||
code: {
|
||||
expression: string;
|
||||
output: string[] | undefined;
|
||||
}
|
||||
}
|
||||
|
||||
export interface ReferenceContent {
|
||||
reference: AppReference | GraphReference | GroupReference;
|
||||
}
|
||||
|
||||
export interface GraphReference {
|
||||
graph: {
|
||||
graph: string;
|
||||
group: string;
|
||||
index: string;
|
||||
}
|
||||
}
|
||||
|
||||
export interface GroupReference {
|
||||
group: string;
|
||||
}
|
||||
|
||||
export interface AppReference {
|
||||
app: {
|
||||
ship: string;
|
||||
desk: string;
|
||||
path: string;
|
||||
}
|
||||
}
|
||||
|
||||
export interface MentionContent {
|
||||
mention: string;
|
||||
emphasis?: 'bold' | 'italic';
|
||||
}
|
||||
export type Content =
|
||||
| TextContent
|
||||
| UrlContent
|
||||
| CodeContent
|
||||
| ReferenceContent
|
||||
| MentionContent;
|
||||
|
||||
export interface Post {
|
||||
author: Patp;
|
||||
contents: Content[];
|
||||
hash: string | null;
|
||||
index: string;
|
||||
pending?: boolean;
|
||||
signatures: string[];
|
||||
'time-sent': number;
|
||||
}
|
||||
|
||||
export interface GraphNodePoke {
|
||||
post: Post;
|
||||
children: GraphChildrenPoke | null;
|
||||
}
|
||||
|
||||
export interface GraphChildrenPoke {
|
||||
[k: string]: GraphNodePoke;
|
||||
}
|
||||
|
||||
export interface GraphNode {
|
||||
children: Graph | null;
|
||||
post: Post;
|
||||
}
|
||||
|
||||
export interface FlatGraphNode {
|
||||
children: null;
|
||||
post: Post;
|
||||
}
|
||||
|
||||
export type Graph = BigIntOrderedMap<GraphNode>;
|
||||
|
||||
export type Graphs = { [rid: string]: Graph };
|
||||
|
||||
export type FlatGraph = BigIntArrayOrderedMap<FlatGraphNode>;
|
||||
|
||||
export type FlatGraphs = { [rid: string]: FlatGraph };
|
||||
|
||||
export type ThreadGraphs = {
|
||||
[rid: string]: {
|
||||
[index: string]: FlatGraph;
|
||||
}
|
||||
};
|
@ -1,152 +0,0 @@
|
||||
import produce, { immerable, castDraft, setAutoFreeze, enablePatches } from 'immer';
|
||||
import bigInt, { BigInteger } from 'big-integer';
|
||||
|
||||
setAutoFreeze(false);
|
||||
|
||||
enablePatches();
|
||||
|
||||
export function stringToArr(str: string) {
|
||||
return str.split('/').slice(1).map((ind) => {
|
||||
return bigInt(ind);
|
||||
});
|
||||
}
|
||||
|
||||
export function arrToString(arr: BigInteger[]) {
|
||||
let string = '';
|
||||
arr.forEach((key) => {
|
||||
string = string + `/${key.toString()}`;
|
||||
});
|
||||
return string;
|
||||
}
|
||||
|
||||
function sorted(a: BigInteger[], b: BigInteger[], reversed = false) {
|
||||
const getSort = sortBigIntArr(a, b);
|
||||
if (reversed) {
|
||||
return getSort * -1;
|
||||
} else {
|
||||
return getSort;
|
||||
}
|
||||
}
|
||||
|
||||
export function sortBigIntArr(a: BigInteger[], b: BigInteger[]) {
|
||||
const aLen = a.length;
|
||||
const bLen = b.length;
|
||||
|
||||
const aCop = a.slice(0);
|
||||
const bCop = b.slice(0);
|
||||
aCop.reverse();
|
||||
bCop.reverse();
|
||||
|
||||
let i = 0;
|
||||
while (i < aLen && i < bLen) {
|
||||
if (aCop[i].lt(bCop[i])) {
|
||||
return 1;
|
||||
} else if (aCop[i].gt(bCop[i])) {
|
||||
return -1;
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
return bLen - aLen;
|
||||
}
|
||||
|
||||
export class BigIntArrayOrderedMap<V> implements Iterable<[BigInteger[], V]> {
|
||||
root: Record<string, V> = {}
|
||||
cachedIter: [BigInteger[], V][] | null = null;
|
||||
[immerable] = true;
|
||||
reversed = false;
|
||||
|
||||
constructor(items: [BigInteger[], V][] = [], reversed = false) {
|
||||
items.forEach(([key, val]) => {
|
||||
this.set(key, val);
|
||||
});
|
||||
this.reversed = reversed;
|
||||
}
|
||||
|
||||
get size() {
|
||||
return Object.keys(this.root).length;
|
||||
}
|
||||
|
||||
get(key: BigInteger[]) {
|
||||
return this.root[arrToString(key)] ?? null;
|
||||
}
|
||||
|
||||
gas(items: [BigInteger[], V][]) {
|
||||
return produce(this, (draft) => {
|
||||
items.forEach(([key, value]) => {
|
||||
draft.root[arrToString(key)] = castDraft(value);
|
||||
});
|
||||
draft.generateCachedIter();
|
||||
},
|
||||
(patches) => {
|
||||
// console.log(`gassed with ${JSON.stringify(patches, null, 2)}`);
|
||||
});
|
||||
}
|
||||
|
||||
set(key: BigInteger[], value: V) {
|
||||
return produce(this, (draft) => {
|
||||
draft.root[arrToString(key)] = castDraft(value);
|
||||
draft.cachedIter = null;
|
||||
});
|
||||
}
|
||||
|
||||
clear() {
|
||||
return produce(this, (draft) => {
|
||||
draft.cachedIter = [];
|
||||
draft.root = {};
|
||||
});
|
||||
}
|
||||
|
||||
has(key: BigInteger[]) {
|
||||
return arrToString(key) in this.root;
|
||||
}
|
||||
|
||||
delete(key: BigInteger[]) {
|
||||
const result = produce(this, (draft) => {
|
||||
delete draft.root[arrToString(key)];
|
||||
draft.cachedIter = null;
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
[Symbol.iterator](): IterableIterator<[BigInteger[], V]> {
|
||||
let idx = 0;
|
||||
const result = this.generateCachedIter();
|
||||
return {
|
||||
[Symbol.iterator]: this[Symbol.iterator],
|
||||
next: (): IteratorResult<[BigInteger[], V]> => {
|
||||
if (idx < result.length) {
|
||||
return { value: result[idx++], done: false };
|
||||
}
|
||||
return { done: true, value: null };
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
peekLargest() {
|
||||
const sorted = Array.from(this);
|
||||
return sorted[0] as [BigInteger[], V] | null;
|
||||
}
|
||||
|
||||
peekSmallest() {
|
||||
const sorted = Array.from(this);
|
||||
return sorted[sorted.length - 1] as [BigInteger[], V] | null;
|
||||
}
|
||||
|
||||
keys() {
|
||||
return Array.from(this).map(([k,v]) => k);
|
||||
}
|
||||
|
||||
generateCachedIter() {
|
||||
if(this.cachedIter) {
|
||||
return [...this.cachedIter];
|
||||
}
|
||||
const result = Object.keys(this.root).map((key) => {
|
||||
return [stringToArr(key), this.root[key]] as [BigInteger[], V];
|
||||
}).sort(([a], [b]) => sorted(a, b, this.reversed));
|
||||
this.cachedIter = result;
|
||||
return [...result];
|
||||
}
|
||||
}
|
||||
|
@ -1,117 +0,0 @@
|
||||
import produce, { immerable, castDraft, setAutoFreeze, enablePatches } from 'immer';
|
||||
import bigInt, { BigInteger } from 'big-integer';
|
||||
|
||||
setAutoFreeze(false);
|
||||
|
||||
enablePatches();
|
||||
|
||||
function sortBigInt(a: BigInteger, b: BigInteger) {
|
||||
if (a.lt(b)) {
|
||||
return 1;
|
||||
} else if (a.eq(b)) {
|
||||
return 0;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
export class BigIntOrderedMap<V> implements Iterable<[BigInteger, V]> {
|
||||
root: Record<string, V> = {}
|
||||
cachedIter: [BigInteger, V][] | null = null;
|
||||
[immerable] = true;
|
||||
|
||||
constructor(items: [BigInteger, V][] = []) {
|
||||
items.forEach(([key, val]) => {
|
||||
this.set(key, val);
|
||||
});
|
||||
}
|
||||
|
||||
get size() {
|
||||
if(this.cachedIter) {
|
||||
return this.cachedIter.length;
|
||||
}
|
||||
return this.generateCachedIter().length;
|
||||
}
|
||||
|
||||
get(key: BigInteger) {
|
||||
return this.root[key.toString()] ?? null;
|
||||
}
|
||||
|
||||
gas(items: [BigInteger, V][]) {
|
||||
return produce(this, (draft) => {
|
||||
items.forEach(([key, value]) => {
|
||||
draft.root[key.toString()] = castDraft(value);
|
||||
});
|
||||
draft.cachedIter = null;
|
||||
},
|
||||
(patches) => {
|
||||
// console.log(`gassed with ${JSON.stringify(patches, null, 2)}`);
|
||||
});
|
||||
}
|
||||
|
||||
set(key: BigInteger, value: V) {
|
||||
return produce(this, (draft) => {
|
||||
draft.root[key.toString()] = castDraft(value);
|
||||
draft.cachedIter = null;
|
||||
});
|
||||
}
|
||||
|
||||
clear() {
|
||||
return produce(this, (draft) => {
|
||||
draft.cachedIter = [];
|
||||
draft.root = {};
|
||||
});
|
||||
}
|
||||
|
||||
has(key: BigInteger) {
|
||||
return key.toString() in this.root;
|
||||
}
|
||||
|
||||
delete(key: BigInteger) {
|
||||
const result = produce(this, (draft) => {
|
||||
delete draft.root[key.toString()];
|
||||
draft.cachedIter = null;
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
[Symbol.iterator](): IterableIterator<[BigInteger, V]> {
|
||||
let idx = 0;
|
||||
const result = this.generateCachedIter();
|
||||
return {
|
||||
[Symbol.iterator]: this[Symbol.iterator],
|
||||
next: (): IteratorResult<[BigInteger, V]> => {
|
||||
if (idx < result.length) {
|
||||
return { value: result[idx++], done: false };
|
||||
}
|
||||
return { done: true, value: null };
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
peekLargest() {
|
||||
const sorted = Array.from(this);
|
||||
return sorted[0] as [BigInteger, V] | null;
|
||||
}
|
||||
|
||||
peekSmallest() {
|
||||
const sorted = Array.from(this);
|
||||
return sorted[sorted.length - 1] as [BigInteger, V] | null;
|
||||
}
|
||||
|
||||
keys() {
|
||||
return Array.from(this).map(([k,v]) => k);
|
||||
}
|
||||
|
||||
generateCachedIter() {
|
||||
if(this.cachedIter) {
|
||||
return [...this.cachedIter];
|
||||
}
|
||||
const result = Object.keys(this.root).map((key) => {
|
||||
const num = bigInt(key);
|
||||
return [num, this.root[key]] as [BigInteger, V];
|
||||
}).sort(([a], [b]) => sortBigInt(a,b));
|
||||
this.cachedIter = result;
|
||||
return [...result];
|
||||
}
|
||||
}
|
||||
|
@ -1,2 +0,0 @@
|
||||
export * from './lib';
|
||||
export * from './types';
|
259
gear/lib/lib.ts
259
gear/lib/lib.ts
@ -1,259 +0,0 @@
|
||||
import bigInt, { BigInteger } from "big-integer";
|
||||
|
||||
import { Resource } from "../groups/types";
|
||||
import { Post, GraphNode } from "../graph/types";
|
||||
|
||||
const DA_UNIX_EPOCH = bigInt("170141184475152167957503069145530368000"); // `@ud` ~1970.1.1
|
||||
|
||||
const DA_SECOND = bigInt("18446744073709551616"); // `@ud` ~s1
|
||||
|
||||
function chunk<T>(arr: T[], size: number): T[][] {
|
||||
let chunk: T[] = [];
|
||||
let newArray = [chunk];
|
||||
|
||||
for (let i = 0;i < arr.length;i++) {
|
||||
if (chunk.length < size) {
|
||||
chunk.push(arr[i])
|
||||
} else {
|
||||
chunk = [arr[i]]
|
||||
newArray.push(chunk)
|
||||
}
|
||||
}
|
||||
|
||||
return newArray;
|
||||
}
|
||||
|
||||
function dropWhile<T>(arr: T[], pred: (x: T) => boolean): T[] {
|
||||
const newArray = arr.slice();
|
||||
|
||||
for (const item of arr) {
|
||||
if (pred(item)) {
|
||||
newArray.shift();
|
||||
} else {
|
||||
return newArray;
|
||||
}
|
||||
}
|
||||
|
||||
return newArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a bigint representing an urbit date, returns a unix timestamp.
|
||||
*
|
||||
* @param {BigInteger} da The urbit date
|
||||
*
|
||||
* @return {number} The unix timestamp
|
||||
*/
|
||||
export function daToUnix(da: BigInteger): number {
|
||||
// ported from +time:enjs:format in hoon.hoon
|
||||
const offset = DA_SECOND.divide(bigInt(2000));
|
||||
const epochAdjusted = offset.add(da.subtract(DA_UNIX_EPOCH));
|
||||
|
||||
return Math.round(
|
||||
epochAdjusted.multiply(bigInt(1000)).divide(DA_SECOND).toJSNumber()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a unix timestamp, returns a bigint representing an urbit date
|
||||
*
|
||||
* @param {number} unix The unix timestamp
|
||||
*
|
||||
* @return {BigInteger} The urbit date
|
||||
*/
|
||||
export function unixToDa(unix: number): BigInteger {
|
||||
const timeSinceEpoch = bigInt(unix).multiply(DA_SECOND).divide(bigInt(1000));
|
||||
return DA_UNIX_EPOCH.add(timeSinceEpoch);
|
||||
}
|
||||
|
||||
|
||||
export function makePatDa(patda: string): BigInteger {
|
||||
return bigInt(udToDec(patda));
|
||||
}
|
||||
|
||||
export function udToDec(ud: string): string {
|
||||
return ud.replace(/\./g, "");
|
||||
}
|
||||
|
||||
export function decToUd(str: string): string {
|
||||
const transform = chunk(str.split('').reverse(), 3)
|
||||
.map(group => group.reverse().join(''))
|
||||
.reverse()
|
||||
.join('.')
|
||||
return transform.replace(/^[0\.]+/g, '');
|
||||
}
|
||||
|
||||
export function resourceAsPath(resource: Resource): string {
|
||||
const { name, ship } = resource;
|
||||
return `/ship/~${ship}/${name}`;
|
||||
}
|
||||
|
||||
export function uuid(): string {
|
||||
let str = "0v";
|
||||
str += Math.ceil(Math.random() * 8) + ".";
|
||||
for (let i = 0; i < 5; i++) {
|
||||
let _str = Math.ceil(Math.random() * 10000000).toString(32);
|
||||
_str = ("00000" + _str).substr(-5, 5);
|
||||
str += _str + ".";
|
||||
}
|
||||
|
||||
return str.slice(0, -1);
|
||||
}
|
||||
|
||||
/*
|
||||
Goes from:
|
||||
~2018.7.17..23.15.09..5be5 // urbit @da
|
||||
To:
|
||||
(javascript Date object)
|
||||
*/
|
||||
export function daToDate(st: string): Date {
|
||||
const dub = function (n: string) {
|
||||
return parseInt(n) < 10 ? "0" + parseInt(n) : n.toString();
|
||||
};
|
||||
const da = st.split("..");
|
||||
const bigEnd = da[0].split(".");
|
||||
const lilEnd = da[1].split(".");
|
||||
const ds = `${bigEnd[0].slice(1)}-${dub(bigEnd[1])}-${dub(bigEnd[2])}T${dub(
|
||||
lilEnd[0]
|
||||
)}:${dub(lilEnd[1])}:${dub(lilEnd[2])}Z`;
|
||||
return new Date(ds);
|
||||
}
|
||||
|
||||
/*
|
||||
Goes from:
|
||||
(javascript Date object)
|
||||
To:
|
||||
~2018.7.17..23.15.09..5be5 // urbit @da
|
||||
*/
|
||||
|
||||
export function dateToDa(d: Date, mil: boolean = false): string {
|
||||
const fil = function (n: number) {
|
||||
return n >= 10 ? n : "0" + n;
|
||||
};
|
||||
return (
|
||||
`~${d.getUTCFullYear()}.` +
|
||||
`${d.getUTCMonth() + 1}.` +
|
||||
`${fil(d.getUTCDate())}..` +
|
||||
`${fil(d.getUTCHours())}.` +
|
||||
`${fil(d.getUTCMinutes())}.` +
|
||||
`${fil(d.getUTCSeconds())}` +
|
||||
`${mil ? "..0000" : ""}`
|
||||
);
|
||||
}
|
||||
|
||||
export function preSig(ship: string): string {
|
||||
if (!ship) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (ship.trim().startsWith('~')) {
|
||||
return ship.trim();
|
||||
}
|
||||
|
||||
return '~'.concat(ship.trim());
|
||||
}
|
||||
|
||||
export function deSig(ship: string): string | null {
|
||||
if (!ship) {
|
||||
return null;
|
||||
}
|
||||
return ship.replace("~", "");
|
||||
}
|
||||
|
||||
// trim patps to match dojo, chat-cli
|
||||
export function cite(ship: string) {
|
||||
let patp = ship,
|
||||
shortened = '';
|
||||
if (patp === null || patp === '') {
|
||||
return null;
|
||||
}
|
||||
if (patp.startsWith('~')) {
|
||||
patp = patp.substr(1);
|
||||
}
|
||||
// comet
|
||||
if (patp.length === 56) {
|
||||
shortened = '~' + patp.slice(0, 6) + '_' + patp.slice(50, 56);
|
||||
return shortened;
|
||||
}
|
||||
// moon
|
||||
if (patp.length === 27) {
|
||||
shortened = '~' + patp.slice(14, 20) + '^' + patp.slice(21, 27);
|
||||
return shortened;
|
||||
}
|
||||
return `~${patp}`;
|
||||
}
|
||||
|
||||
|
||||
export function uxToHex(ux: string) {
|
||||
if (ux.length > 2 && ux.substr(0, 2) === '0x') {
|
||||
const value = ux.substr(2).replace('.', '').padStart(6, '0');
|
||||
return value;
|
||||
}
|
||||
|
||||
const value = ux.replace('.', '').padStart(6, '0');
|
||||
return value;
|
||||
}
|
||||
|
||||
export const hexToUx = (hex: string): string => {
|
||||
const nonZeroChars = dropWhile(hex.split(''), y => y === '0');
|
||||
const ux = chunk(nonZeroChars.reverse(), 4).map(x => {
|
||||
return x.reverse().join('');
|
||||
}).reverse().join('.') || '0';
|
||||
|
||||
return `0x${ux}`;
|
||||
};
|
||||
|
||||
|
||||
// encode the string into @ta-safe format, using logic from +wood.
|
||||
// for example, 'some Chars!' becomes '~.some.~43.hars~21.'
|
||||
//
|
||||
export function stringToTa(str: string): string {
|
||||
let out = "";
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
const char = str[i];
|
||||
let add = "";
|
||||
switch (char) {
|
||||
case " ":
|
||||
add = ".";
|
||||
break;
|
||||
case ".":
|
||||
add = "~.";
|
||||
break;
|
||||
case "~":
|
||||
add = "~~";
|
||||
break;
|
||||
default:
|
||||
const charCode = str.charCodeAt(i);
|
||||
if (
|
||||
(charCode >= 97 && charCode <= 122) || // a-z
|
||||
(charCode >= 48 && charCode <= 57) || // 0-9
|
||||
char === "-"
|
||||
) {
|
||||
add = char;
|
||||
} else {
|
||||
// TODO behavior for unicode doesn't match +wood's,
|
||||
// but we can probably get away with that for now.
|
||||
add = "~" + charCode.toString(16) + ".";
|
||||
}
|
||||
}
|
||||
out = out + add;
|
||||
}
|
||||
return "~." + out;
|
||||
}
|
||||
|
||||
export const buntPost = (): Post => ({
|
||||
author: '',
|
||||
contents: [],
|
||||
hash: null,
|
||||
index: '',
|
||||
signatures: [],
|
||||
'time-sent': 0
|
||||
});
|
||||
|
||||
export function makeNodeMap(posts: Post[]): Record<string, GraphNode> {
|
||||
const nodes: Record<string, GraphNode> = {};
|
||||
posts.forEach((p: Post) => {
|
||||
nodes[String(p.index)] = { children: null, post: p };
|
||||
});
|
||||
return nodes;
|
||||
}
|
@ -1,67 +0,0 @@
|
||||
/**
|
||||
* Martian embassy
|
||||
*/
|
||||
|
||||
import { BigIntOrderedMap } from "./BigIntOrderedMap";
|
||||
|
||||
// an urbit style path rendered as string
|
||||
export type Path = string;
|
||||
|
||||
// patp including leading sig
|
||||
export type Patp = string;
|
||||
|
||||
// patp excluding leading sig
|
||||
export type PatpNoSig = string;
|
||||
|
||||
// @uvH encoded string
|
||||
export type Serial = string;
|
||||
|
||||
// jug from hoon
|
||||
export type Jug<K,V> = Map<K,Set<V>>;
|
||||
|
||||
// name of app
|
||||
export type AppName = 'chat' | 'link' | 'contacts' | 'publish' | 'graph' | 'groups';
|
||||
|
||||
export type ShipRank = 'czar' | 'king' | 'duke' | 'earl' | 'pawn';
|
||||
|
||||
export type Action = 'poke' | 'subscribe' | 'ack' | 'unsubscribe' | 'delete';
|
||||
|
||||
|
||||
export type SetElement<S> = S extends Set<(infer T)> ? T : never;
|
||||
export type MapKey<M> = M extends Map<(infer K), any> ? K : never;
|
||||
export type MapValue<M> = M extends Map<any, (infer V)> ? V : never;
|
||||
|
||||
/**
|
||||
* Turns sets into arrays and maps into objects so we can send them over the wire
|
||||
*/
|
||||
export type Enc<S> =
|
||||
S extends Set<any> ?
|
||||
Enc<SetElement<S>>[] :
|
||||
S extends Map<string, any> ?
|
||||
{ [s: string]: Enc<MapValue<S>> } :
|
||||
S extends object ?
|
||||
{ [K in keyof S]: Enc<S[K]> } :
|
||||
S extends BigIntOrderedMap<infer T> ?
|
||||
{ [index: string]: T } :
|
||||
S;
|
||||
|
||||
export type Mark = string;
|
||||
|
||||
export interface Poke<Action> {
|
||||
ship?: string; // This should be handled by the http library, but is part of the spec
|
||||
app: string;
|
||||
mark: Mark;
|
||||
json: Action;
|
||||
}
|
||||
|
||||
export interface Scry {
|
||||
app: string;
|
||||
path: string;
|
||||
}
|
||||
|
||||
export interface Thread<Action> {
|
||||
inputMark: string;
|
||||
outputMark: string;
|
||||
threadName: string;
|
||||
body: Action;
|
||||
}
|
@ -1,2 +0,0 @@
|
||||
export * from './lib';
|
||||
export * from './types';
|
0
gear/deps.d.ts → ui/gear/deps.d.ts
vendored
0
gear/deps.d.ts → ui/gear/deps.d.ts
vendored
@ -1,7 +1,5 @@
|
||||
export * from './contacts';
|
||||
export * as contacts from './contacts';
|
||||
export * from './graph';
|
||||
export * as graph from './graph';
|
||||
export * from './groups';
|
||||
export * as groups from './groups';
|
||||
export * from './hark';
|
||||
@ -13,8 +11,8 @@ export * from './metadata';
|
||||
export * as metadata from './metadata';
|
||||
export * from './settings';
|
||||
export * as settings from './settings';
|
||||
export * from './s3';
|
||||
export * as s3 from './s3';
|
||||
export * from './storage';
|
||||
export * as storage from './storage';
|
||||
export * from './lib';
|
||||
export * from './lib/BigIntOrderedMap';
|
||||
export * from './lib/BigIntArrayOrderedMap';
|
25
ui/package-lock.json
generated
25
ui/package-lock.json
generated
@ -25,7 +25,6 @@
|
||||
"@tlon/sigil-js": "^1.4.4",
|
||||
"@tloncorp/mock-http-api": "^1.2.0",
|
||||
"@types/lodash": "^4.14.172",
|
||||
"@urbit/api": "^2.2.0",
|
||||
"@urbit/http-api": "^2.4.5-debug",
|
||||
"big-integer": "^1.6.48",
|
||||
"browser-cookies": "^1.2.0",
|
||||
@ -2922,18 +2921,6 @@
|
||||
"url": "https://opencollective.com/typescript-eslint"
|
||||
}
|
||||
},
|
||||
"node_modules/@urbit/api": {
|
||||
"version": "2.2.0",
|
||||
"resolved": "https://registry.npmjs.org/@urbit/api/-/api-2.2.0.tgz",
|
||||
"integrity": "sha512-W8kP9OT6yOK62n+4yCPO3i9QqU5xriLvZQ9WYW4SAV7ktbSrGuf2kmYbnoqfA/NybIs9Q/MbFkPewrz4XJ96Ag==",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.16.0",
|
||||
"big-integer": "^1.6.48",
|
||||
"core-js": "^3.19.1",
|
||||
"immer": "^9.0.1",
|
||||
"urbit-ob": "^5.0.1"
|
||||
}
|
||||
},
|
||||
"node_modules/@urbit/http-api": {
|
||||
"version": "2.4.5-debug",
|
||||
"resolved": "https://registry.npmjs.org/@urbit/http-api/-/http-api-2.4.5-debug.tgz",
|
||||
@ -11349,18 +11336,6 @@
|
||||
"eslint-visitor-keys": "^2.0.0"
|
||||
}
|
||||
},
|
||||
"@urbit/api": {
|
||||
"version": "2.2.0",
|
||||
"resolved": "https://registry.npmjs.org/@urbit/api/-/api-2.2.0.tgz",
|
||||
"integrity": "sha512-W8kP9OT6yOK62n+4yCPO3i9QqU5xriLvZQ9WYW4SAV7ktbSrGuf2kmYbnoqfA/NybIs9Q/MbFkPewrz4XJ96Ag==",
|
||||
"requires": {
|
||||
"@babel/runtime": "^7.16.0",
|
||||
"big-integer": "^1.6.48",
|
||||
"core-js": "^3.19.1",
|
||||
"immer": "^9.0.1",
|
||||
"urbit-ob": "^5.0.1"
|
||||
}
|
||||
},
|
||||
"@urbit/http-api": {
|
||||
"version": "2.4.5-debug",
|
||||
"resolved": "https://registry.npmjs.org/@urbit/http-api/-/http-api-2.4.5-debug.tgz",
|
||||
|
@ -35,7 +35,6 @@
|
||||
"@tlon/sigil-js": "^1.4.4",
|
||||
"@tloncorp/mock-http-api": "^1.2.0",
|
||||
"@types/lodash": "^4.14.172",
|
||||
"@urbit/api": "^2.2.0",
|
||||
"@urbit/http-api": "^2.4.5-debug",
|
||||
"big-integer": "^1.6.48",
|
||||
"browser-cookies": "^1.2.0",
|
||||
|
@ -7,7 +7,7 @@ import {
|
||||
reduceStateN,
|
||||
BaseState,
|
||||
} from '../base';
|
||||
import { S3Credentials } from '@urbit/api';
|
||||
import { StorageCredentials } from '../../../';
|
||||
|
||||
enableMapSet();
|
||||
|
||||
@ -22,7 +22,7 @@ export interface BaseStorageState {
|
||||
currentBucket: string;
|
||||
region: string;
|
||||
};
|
||||
credentials: S3Credentials | null;
|
||||
credentials: StorageCredentials | null;
|
||||
};
|
||||
[ref: string]: unknown;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user