@session.js/types 1.0.0

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/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # @session.js/types
2
+
3
+ A special package that holds TypeScript definitions and enums shared internally for developing your own @session.js/client modular parts.
4
+
5
+ To build your own Storage adapter, use:
6
+
7
+ ```ts
8
+ import type { Storage } from '@session.js/types'
9
+
10
+ export class MyInMemoryStorage implements Storage {
11
+ storage: Map<string, string> = new Map()
12
+
13
+ get(key: string) {
14
+ return this.storage.get(key) ?? null
15
+ }
16
+
17
+ set(key: string, value: string) {
18
+ this.storage.set(key, value)
19
+ }
20
+
21
+ delete(key: string) {
22
+ this.storage.delete(key)
23
+ }
24
+
25
+ has(key: string) {
26
+ return this.storage.has(key)
27
+ }
28
+ }
29
+ ```
30
+
31
+ To build your own Network connecter, use:
32
+
33
+ ```ts
34
+ import type { Network } from '@session.js/types'
35
+ import {
36
+ RequestType,
37
+ type RequestGetSwarmsBody,
38
+ type RequestPollBody,
39
+ type RequestStoreBody,
40
+ type RequestUploadAttachment
41
+ } from '@session.js/types/network/request'
42
+
43
+ export class MyNetwork implements Network {
44
+ onRequest(type: RequestType, body: object): Promise<object> {
45
+ switch(type) {
46
+ case RequestType.Store:
47
+ return // typeof ResponseStore
48
+
49
+ case RequestType.GetSnodes:
50
+ return // typeof ResponseGetSnodes
51
+
52
+ case RequestType.GetSwarms:
53
+ return // typeof ResponseGetSwarms
54
+
55
+ case RequestType.Poll:
56
+ return // typeof ResponsePoll
57
+
58
+ case RequestType.UploadAttachment:
59
+ return // typeof ResponseUploadAttachment
60
+
61
+ default:
62
+ throw new Error('Invalid request type')
63
+ }
64
+ }
65
+ }
66
+ ```
67
+
68
+ ## Made for session.js
69
+
70
+ Use Session messenger programmatically with [Session.js](https://github.com/sessionjs/client): Session bots, custom Session clients, and more.
71
+
72
+ ## Donate
73
+
74
+ [hloth.dev/donate](https://hloth.dev/donate)
@@ -0,0 +1,2 @@
1
+ export type DisappearingMessageType = (typeof DisappearingMessageMode)[number];
2
+ export declare const DisappearingMessageMode: readonly ["unknown", "deleteAfterRead", "deleteAfterSend"];
@@ -0,0 +1 @@
1
+ export const DisappearingMessageMode = ['unknown', 'deleteAfterRead', 'deleteAfterSend'];
@@ -0,0 +1,3 @@
1
+ export type PickEnum<T, K extends T> = {
2
+ [P in keyof K]: P extends K ? P : never;
3
+ };
package/dist/enums.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,6 @@
1
+ import type { SignalService } from './signal-bindings/index.js';
2
+ export interface EnvelopePlus extends Omit<SignalService.Envelope, 'toJSON'> {
3
+ senderIdentity: string;
4
+ receivedAt: number;
5
+ id: string;
6
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ export { SnodeNamespaces } from './namespaces.js';
2
+ export type { Network } from './network/index.js';
3
+ export type { Storage } from './storage/index.js';
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export { SnodeNamespaces } from './namespaces.js';
@@ -0,0 +1,42 @@
1
+ import type { PickEnum } from './enums.js';
2
+ export declare enum SnodeNamespaces {
3
+ /**
4
+ * This is the namespace anyone can deposit a message for us
5
+ */
6
+ UserMessages = 0,
7
+ /**
8
+ * This is the namespace used to sync our profile
9
+ */
10
+ UserProfile = 2,
11
+ /**
12
+ * This is the namespace used to sync our contacts
13
+ */
14
+ UserContacts = 3,
15
+ /**
16
+ * This is the namespace used to sync our volatile info (currently read status only)
17
+ */
18
+ ConvoInfoVolatile = 4,
19
+ /**
20
+ * This is the namespace used to sync our user groups and communities
21
+ */
22
+ UserGroups = 5,
23
+ /** **THIS IS LEGACY AND SHOULD BE USED unless you know how to deal with legacy closed groups**. Use UserGroups instead.
24
+ * The messages sent to a closed group are sent and polled from this namespace
25
+ */
26
+ ClosedGroupMessage = -10
27
+ }
28
+ /**
29
+ * Returns true if that namespace is associated with the config of a user (not his messages, only configs)
30
+ */
31
+ declare function isUserConfigNamespace(namespace: SnodeNamespaces): boolean;
32
+ export type SnodeNamespacesGroup = PickEnum<SnodeNamespaces, SnodeNamespaces.ClosedGroupMessage>;
33
+ export type SnodeNamespacesUser = PickEnum<SnodeNamespaces, SnodeNamespaces.UserContacts | SnodeNamespaces.UserProfile | SnodeNamespaces.UserMessages>;
34
+ declare function maxSizeMap(namespaces: Array<SnodeNamespaces>): {
35
+ namespace: SnodeNamespaces;
36
+ maxSize: number;
37
+ }[];
38
+ export declare const SnodeNamespace: {
39
+ isUserConfigNamespace: typeof isUserConfigNamespace;
40
+ maxSizeMap: typeof maxSizeMap;
41
+ };
42
+ export {};
@@ -0,0 +1,99 @@
1
+ // CREDIT: OXEN, Session-Desktop
2
+ // github.com/oxen-io/session-desktop
3
+ import _ from 'lodash';
4
+ export var SnodeNamespaces;
5
+ (function (SnodeNamespaces) {
6
+ /**
7
+ * This is the namespace anyone can deposit a message for us
8
+ */
9
+ SnodeNamespaces[SnodeNamespaces["UserMessages"] = 0] = "UserMessages";
10
+ /**
11
+ * This is the namespace used to sync our profile
12
+ */
13
+ SnodeNamespaces[SnodeNamespaces["UserProfile"] = 2] = "UserProfile";
14
+ /**
15
+ * This is the namespace used to sync our contacts
16
+ */
17
+ SnodeNamespaces[SnodeNamespaces["UserContacts"] = 3] = "UserContacts";
18
+ /**
19
+ * This is the namespace used to sync our volatile info (currently read status only)
20
+ */
21
+ SnodeNamespaces[SnodeNamespaces["ConvoInfoVolatile"] = 4] = "ConvoInfoVolatile";
22
+ /**
23
+ * This is the namespace used to sync our user groups and communities
24
+ */
25
+ SnodeNamespaces[SnodeNamespaces["UserGroups"] = 5] = "UserGroups";
26
+ /** **THIS IS LEGACY AND SHOULD BE USED unless you know how to deal with legacy closed groups**. Use UserGroups instead.
27
+ * The messages sent to a closed group are sent and polled from this namespace
28
+ */
29
+ SnodeNamespaces[SnodeNamespaces["ClosedGroupMessage"] = -10] = "ClosedGroupMessage";
30
+ /**
31
+ * This is the namespace used to sync the closed group details for each of the closed groups we are polling
32
+ */
33
+ // ClosedGroupInfo = 1, // This is commented out in Session-desktop too
34
+ })(SnodeNamespaces || (SnodeNamespaces = {}));
35
+ /**
36
+ * Returns true if that namespace is associated with the config of a user (not his messages, only configs)
37
+ */
38
+ function isUserConfigNamespace(namespace) {
39
+ switch (namespace) {
40
+ case SnodeNamespaces.UserMessages:
41
+ // user messages is not hosting config based messages
42
+ return false;
43
+ case SnodeNamespaces.UserContacts:
44
+ case SnodeNamespaces.UserProfile:
45
+ case SnodeNamespaces.UserGroups:
46
+ case SnodeNamespaces.ConvoInfoVolatile:
47
+ return true;
48
+ // This is commented out in Session-desktop too
49
+ // case SnodeNamespaces.ClosedGroupInfo:
50
+ case SnodeNamespaces.ClosedGroupMessage:
51
+ return false;
52
+ default:
53
+ throw new Error(`isUserConfigNamespace case not handled: ${namespace}`);
54
+ }
55
+ }
56
+ function namespacePriority(namespace) {
57
+ switch (namespace) {
58
+ case SnodeNamespaces.UserMessages:
59
+ return 10;
60
+ case SnodeNamespaces.UserContacts:
61
+ return 1;
62
+ case SnodeNamespaces.UserProfile:
63
+ return 1;
64
+ case SnodeNamespaces.UserGroups:
65
+ return 1;
66
+ case SnodeNamespaces.ConvoInfoVolatile:
67
+ return 1;
68
+ case SnodeNamespaces.ClosedGroupMessage:
69
+ return 10;
70
+ default:
71
+ throw new Error(`namespacePriority case not handled: ${namespace}`);
72
+ }
73
+ }
74
+ function maxSizeMap(namespaces) {
75
+ let lastSplit = 1;
76
+ const withPriorities = namespaces.map(namespace => {
77
+ return { namespace, priority: namespacePriority(namespace) };
78
+ });
79
+ const groupedByPriorities = [];
80
+ withPriorities.forEach(item => {
81
+ if (!groupedByPriorities.find(p => p.priority === item.priority)) {
82
+ groupedByPriorities.push({ priority: item.priority, namespaces: [] });
83
+ }
84
+ groupedByPriorities.find(p => p.priority === item.priority)?.namespaces.push(item.namespace);
85
+ });
86
+ const sortedDescPriorities = _.orderBy(groupedByPriorities, ['priority'], ['desc']);
87
+ const lowestPriority = _.last(sortedDescPriorities)?.priority || 1;
88
+ const sizeMap = sortedDescPriorities.flatMap(m => {
89
+ const paddingForLowerPriority = m.priority === lowestPriority ? 0 : 1;
90
+ const splitsForPriority = paddingForLowerPriority + m.namespaces.length;
91
+ lastSplit *= splitsForPriority;
92
+ return m.namespaces.map(namespace => ({ namespace, maxSize: -lastSplit }));
93
+ });
94
+ return sizeMap;
95
+ }
96
+ export const SnodeNamespace = {
97
+ isUserConfigNamespace,
98
+ maxSizeMap,
99
+ };
@@ -0,0 +1,4 @@
1
+ import type { RequestType } from './request.js';
2
+ export interface Network {
3
+ onRequest(type: RequestType, body: unknown): Promise<unknown>;
4
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,29 @@
1
+ import type { Snode } from '../snode.js';
2
+ import type { RequestNamespace } from '../snode-retrieve.js';
3
+ import type { Swarm } from '../swarm.js';
4
+ export declare enum RequestType {
5
+ Store = "/store",
6
+ GetSnodes = "/get_snodes",
7
+ GetSwarms = "/get_swarms",
8
+ Poll = "/poll",
9
+ UploadAttachment = "/upload_attachment"
10
+ }
11
+ export type RequestStoreBody = {
12
+ destination: string;
13
+ data64: string;
14
+ ttl: number;
15
+ timestamp: number;
16
+ namespace: number;
17
+ swarm: Swarm;
18
+ };
19
+ export type RequestGetSwarmsBody = {
20
+ snode: Snode;
21
+ pubkey: string;
22
+ };
23
+ export type RequestPollBody = {
24
+ swarm: Swarm;
25
+ namespaces: RequestNamespace[];
26
+ };
27
+ export type RequestUploadAttachment = {
28
+ data: ArrayBuffer;
29
+ };
@@ -0,0 +1,8 @@
1
+ export var RequestType;
2
+ (function (RequestType) {
3
+ RequestType["Store"] = "/store";
4
+ RequestType["GetSnodes"] = "/get_snodes";
5
+ RequestType["GetSwarms"] = "/get_swarms";
6
+ RequestType["Poll"] = "/poll";
7
+ RequestType["UploadAttachment"] = "/upload_attachment";
8
+ })(RequestType || (RequestType = {}));
@@ -0,0 +1,23 @@
1
+ import type { SnodeNamespaces } from '../namespaces.js';
2
+ import type { Snode } from '../snode.js';
3
+ import type { RetrieveMessageItem } from '../snode-retrieve.js';
4
+ import type { Swarm } from '../swarm.js';
5
+ export type ResponseStore = {
6
+ hash: string;
7
+ };
8
+ export type ResponseGetSnodes = {
9
+ snodes: Snode[];
10
+ };
11
+ export type ResponseGetSwarms = {
12
+ swarms: Swarm[];
13
+ };
14
+ export type ResponsePoll = {
15
+ messages: {
16
+ namespace: SnodeNamespaces;
17
+ messages: RetrieveMessageItem[];
18
+ }[];
19
+ };
20
+ export type ResponseUploadAttachment = {
21
+ url: string;
22
+ id: number;
23
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,22 @@
1
+ export declare enum KeyPrefixType {
2
+ /**
3
+ * Used for keys which have the blinding update and aren't using blinding
4
+ */
5
+ unblinded = "00",
6
+ /**
7
+ * Used for identified users, open groups, etc
8
+ */
9
+ standard = "05",
10
+ /**
11
+ * used for participants in open groups (legacy blinding logic)
12
+ */
13
+ blinded15 = "15",
14
+ /**
15
+ * used for participants in open groups (new blinding logic)
16
+ */
17
+ blinded25 = "25",
18
+ /**
19
+ * used for participants in open groups
20
+ */
21
+ groupV3 = "03"
22
+ }
package/dist/pubkey.js ADDED
@@ -0,0 +1,23 @@
1
+ export var KeyPrefixType;
2
+ (function (KeyPrefixType) {
3
+ /**
4
+ * Used for keys which have the blinding update and aren't using blinding
5
+ */
6
+ KeyPrefixType["unblinded"] = "00";
7
+ /**
8
+ * Used for identified users, open groups, etc
9
+ */
10
+ KeyPrefixType["standard"] = "05";
11
+ /**
12
+ * used for participants in open groups (legacy blinding logic)
13
+ */
14
+ KeyPrefixType["blinded15"] = "15";
15
+ /**
16
+ * used for participants in open groups (new blinding logic)
17
+ */
18
+ KeyPrefixType["blinded25"] = "25";
19
+ /**
20
+ * used for participants in open groups
21
+ */
22
+ KeyPrefixType["groupV3"] = "03";
23
+ })(KeyPrefixType || (KeyPrefixType = {}));