@diory/client-js 0.2.0-rc2 → 0.2.0-rc20

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.
@@ -0,0 +1,44 @@
1
+ name: Publish to NPM
2
+
3
+ on:
4
+ workflow_dispatch:
5
+ inputs:
6
+ version:
7
+ description: 'Specify the version to publish'
8
+ required: true
9
+
10
+ jobs:
11
+ publish:
12
+ runs-on: ubuntu-latest
13
+ permissions:
14
+ contents: write
15
+
16
+ steps:
17
+ - name: Checkout code
18
+ uses: actions/checkout@v4
19
+
20
+ - name: Setup Node.js
21
+ uses: actions/setup-node@v4
22
+ with:
23
+ node-version-file: '.nvmrc'
24
+ registry-url: 'https://registry.npmjs.org'
25
+
26
+ - name: Install dependencies
27
+ run: yarn install
28
+
29
+ - name: Build
30
+ run: yarn build
31
+
32
+ - name: Update version using Yarn
33
+ run: |
34
+ git config user.email "<>"
35
+ git config user.name "GitHub Actions Bot"
36
+ yarn version --new-version ${{ github.event.inputs.version }}
37
+
38
+ - name: Push version update commit
39
+ run: git push origin HEAD --follow-tags
40
+
41
+ - name: Publish to NPM
42
+ run: npm publish --access=public --tag=next
43
+ env:
44
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
@@ -0,0 +1,23 @@
1
+ # This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
2
+ # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3
+
4
+ name: Test (yarn test)
5
+
6
+ on: [push]
7
+
8
+ jobs:
9
+ build:
10
+ runs-on: ubuntu-latest
11
+ env:
12
+ CI: false
13
+ steps:
14
+ - uses: actions/checkout@v4
15
+ with:
16
+ submodules: recursive
17
+ - name: Use Node.js
18
+ uses: actions/setup-node@v4
19
+ with:
20
+ node-version-file: '.nvmrc'
21
+ - run: yarn
22
+ - run: yarn build
23
+ - run: yarn test
package/.nvmrc ADDED
@@ -0,0 +1 @@
1
+ 20.10.0
@@ -0,0 +1 @@
1
+ export declare function debounce(func: any, wait: number, immediate?: boolean): () => typeof func;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.debounce = void 0;
4
+ function debounce(func, wait, immediate = false) {
5
+ let timeout;
6
+ // eslint-disable-next-line func-names
7
+ return () => {
8
+ // @ts-ignore
9
+ const context = this;
10
+ const args = arguments;
11
+ // eslint-disable-next-line func-names
12
+ const later = function () {
13
+ timeout = null;
14
+ if (!immediate)
15
+ func.apply(context, args);
16
+ };
17
+ const callNow = immediate && !timeout;
18
+ clearTimeout(timeout);
19
+ timeout = setTimeout(later, wait);
20
+ if (callNow)
21
+ func.apply(context, args);
22
+ };
23
+ }
24
+ exports.debounce = debounce;
@@ -0,0 +1,24 @@
1
+ {
2
+ "/": {
3
+ "id": "welcome-root-diory"
4
+ },
5
+ "welcome-root-diory": {
6
+ "id": "welcome-root-diory",
7
+ "text": "Room",
8
+ "image": "",
9
+ "links": [
10
+ {
11
+ "id": "welcome-diory"
12
+ }
13
+ ],
14
+ "modified": "2020-01-03T14:03:04.751Z"
15
+ },
16
+ "welcome-diory": {
17
+ "id": "welcome-diory",
18
+ "image": "",
19
+ "modified": "2020-01-03T14:03:04.751Z",
20
+ "text": "Welcome to Diory",
21
+ "latitude": 60.01366036242365,
22
+ "longitude": 20.007133483886722
23
+ }
24
+ }
@@ -0,0 +1,21 @@
1
+ export declare const diosphere: {
2
+ rooms: {
3
+ '/': {
4
+ id: string
5
+ created: string
6
+ modified: string
7
+ }
8
+ 'welcome-room': {
9
+ id: string
10
+ text: string
11
+ doors: never[]
12
+ connections: {
13
+ id: string
14
+ client: string
15
+ address: string
16
+ }[]
17
+ created: string
18
+ modified: string
19
+ }
20
+ }
21
+ }
@@ -0,0 +1,26 @@
1
+ 'use strict'
2
+ Object.defineProperty(exports, '__esModule', { value: true })
3
+ exports.diosphere = void 0
4
+ exports.diosphere = {
5
+ rooms: {
6
+ '/': {
7
+ id: 'welcome-room',
8
+ created: '2024-03-24T14:56:21.243Z',
9
+ modified: '2024-03-24T14:56:21.243Z',
10
+ },
11
+ 'welcome-room': {
12
+ id: 'welcome-room',
13
+ text: 'Wellcome',
14
+ doors: [],
15
+ connections: [
16
+ {
17
+ id: 'welcome-connection',
18
+ client: 'LocalClient',
19
+ address: 'public/welcome-room',
20
+ },
21
+ ],
22
+ created: '2024-03-24T14:56:21.243Z',
23
+ modified: '2024-03-24T14:56:21.243Z',
24
+ },
25
+ },
26
+ }
@@ -0,0 +1,23 @@
1
+ {
2
+ "rooms": {
3
+ "/": {
4
+ "id": "welcome-room",
5
+ "created": "2024-03-24T14:56:21.243Z",
6
+ "modified": "2024-03-24T14:56:21.243Z"
7
+ },
8
+ "welcome-room": {
9
+ "id": "welcome-room",
10
+ "text": "Wellcome room",
11
+ "doors": [],
12
+ "connections": [
13
+ {
14
+ "id": "welcome-room",
15
+ "client": "LocalClient",
16
+ "address": "public/welcome-room"
17
+ }
18
+ ],
19
+ "created": "2024-03-24T14:56:21.243Z",
20
+ "modified": "2024-03-24T14:56:21.243Z"
21
+ }
22
+ }
23
+ }
@@ -1,5 +1,5 @@
1
- import { IConnectionObject, IDiosphere, IRoom, IRoomObject } from '@diory/diosphere-js';
2
- import { IDiograph, IDiory, IDioryObject } from '@diograph/diograph';
1
+ import { IConnectionObject, IDiosphere, IDiosphereObject, IRoom, IRoomObject } from '@diory/diosphere-js';
2
+ import { IDiograph, IDiographObject, IDiory, IDioryObject } from '@diograph/diograph';
3
3
  import { IDioryClient, IDataClient } from '../types';
4
4
  declare class DioryClient implements IDioryClient {
5
5
  dataClients: IDataClient[];
@@ -9,13 +9,17 @@ declare class DioryClient implements IDioryClient {
9
9
  diograph: IDiograph;
10
10
  diory?: IDiory;
11
11
  constructor(dataClients: IDataClient[]);
12
- initialise: (connections: IConnectionObject[]) => Promise<IDioryClient>;
13
- enterRoom: (roomObject: IRoomObject) => Promise<IRoom>;
12
+ selectRoom: (roomObject: IRoomObject) => IRoom;
13
+ getDiosphereClients: (connections?: IConnectionObject[]) => import("../types").IConnectionClient[];
14
+ initialiseDiosphere: (connections: IConnectionObject[]) => Promise<IDiosphere>;
15
+ getDiosphere: (connections?: IConnectionObject[]) => Promise<IDiosphereObject | undefined>;
16
+ saveDiosphere: (connections?: IConnectionObject[]) => Promise<IDiosphereObject>;
17
+ getDiographClients: (connections?: IConnectionObject[]) => import("../types").IConnectionClient[];
14
18
  focusDiory: (dioryObject: IDioryObject) => IDiory;
15
- getDiosphere: () => Promise<IDiosphere>;
16
- saveDiosphere: () => Promise<IDiosphere>;
17
- getDiograph: () => Promise<IDiograph>;
18
- saveDiograph: () => Promise<IDiograph>;
19
- generateDiograph: () => Promise<IDiograph>;
19
+ initialiseDiograph: (roomObject: IRoomObject) => Promise<IDiograph>;
20
+ getDiograph: (connections?: IConnectionObject[]) => Promise<IDiographObject | undefined>;
21
+ saveDiograph: (connections?: IConnectionObject[]) => Promise<IDiographObject>;
22
+ mergeDiograph: (diographObject: IDiographObject) => Promise<void>;
23
+ generateDiograph: (connections?: IConnectionObject[]) => Promise<IDiographObject | undefined>;
20
24
  }
21
25
  export { DioryClient };
@@ -4,99 +4,127 @@ exports.DioryClient = void 0;
4
4
  const diosphere_js_1 = require("@diory/diosphere-js");
5
5
  const diograph_1 = require("@diograph/diograph");
6
6
  const getConnectionClients_1 = require("../utils/getConnectionClients");
7
+ const debounce_1 = require("../connectionClient/utils/debounce");
8
+ const getDefaultDiosphere_1 = require("./getDefaultDiosphere");
9
+ const getDefaultDiograph_1 = require("./getDefaultDiograph");
7
10
  class DioryClient {
8
11
  constructor(dataClients) {
9
12
  this.dataClients = [];
10
13
  this.connections = [];
11
- this.initialise = async (connections) => {
14
+ this.selectRoom = (roomObject) => {
15
+ return (this.room = this.diosphere.getRoom(roomObject));
16
+ };
17
+ this.getDiosphereClients = (connections) => {
18
+ return (0, getConnectionClients_1.getConnectionClients)(this.dataClients, connections !== null && connections !== void 0 ? connections : this.connections);
19
+ };
20
+ this.initialiseDiosphere = async (connections) => {
21
+ var _a;
22
+ console.info('initialiseDiosphere', connections);
12
23
  this.connections = connections;
13
24
  this.diosphere.resetRooms();
14
- await this.getDiosphere();
15
- await this.enterRoom({ id: '/' });
16
- return this;
25
+ const diosphereObject = (_a = (await this.getDiosphere())) !== null && _a !== void 0 ? _a : (0, getDefaultDiosphere_1.getDefaultDiosphere)(connections);
26
+ this.diosphere.addDiosphere(diosphereObject);
27
+ this.selectRoom({ id: '/' });
28
+ console.info(this.diosphere.toObject());
29
+ return this.diosphere;
17
30
  };
18
- this.enterRoom = async (roomObject) => {
19
- this.room = this.diosphere.getRoom(roomObject);
20
- this.diograph.resetDiograph();
21
- await this.getDiograph();
22
- this.focusDiory({ id: '/' });
23
- await this.generateDiograph();
24
- return this.room;
31
+ this.getDiosphere = async (connections) => {
32
+ console.info('getDiosphere', connections);
33
+ let diosphereObject;
34
+ await Promise.all(this.getDiosphereClients(connections).map(async (connectionClient) => {
35
+ try {
36
+ diosphereObject = await connectionClient.getDiosphere();
37
+ console.info(diosphereObject);
38
+ }
39
+ catch (error) {
40
+ console.error(error);
41
+ }
42
+ return;
43
+ }));
44
+ return diosphereObject;
45
+ };
46
+ this.saveDiosphere = async (connections) => {
47
+ console.info('saveDiosphere', connections);
48
+ const diosphereObject = this.diosphere.toObject();
49
+ await Promise.all(this.getDiosphereClients(connections).map(async (connectionClient) => {
50
+ await connectionClient.saveDiosphere(diosphereObject);
51
+ console.info(diosphereObject);
52
+ return;
53
+ }));
54
+ return diosphereObject;
55
+ };
56
+ this.getDiographClients = (connections) => {
57
+ var _a;
58
+ return (0, getConnectionClients_1.getConnectionClients)(this.dataClients, connections !== null && connections !== void 0 ? connections : (_a = this.room) === null || _a === void 0 ? void 0 : _a.connections);
25
59
  };
26
60
  this.focusDiory = (dioryObject) => {
27
61
  return (this.diory = this.diograph.getDiory(dioryObject));
28
62
  };
29
- this.getDiosphere = async () => {
30
- console.info('getDiosphere', this.connections);
31
- if (this.connections) {
32
- const connectionClients = (0, getConnectionClients_1.getConnectionClients)(this.dataClients, this.connections);
33
- await Promise.all(connectionClients.map(async (connectionClient) => {
34
- const diosphereObject = await connectionClient.getDiosphere();
35
- console.info(diosphereObject);
36
- return this.diosphere.addDiosphere(diosphereObject);
37
- }));
38
- }
39
- return this.diosphere;
40
- };
41
- this.saveDiosphere = async () => {
42
- console.info('saveDiosphere', this.connections);
43
- if (this.connections) {
44
- const connectionClients = (0, getConnectionClients_1.getConnectionClients)(this.dataClients, this.connections);
45
- await Promise.all(connectionClients.map(async (connectionClient) => {
46
- console.info(this.diosphere.toObject());
47
- await connectionClient.saveDiosphere(this.diosphere.toObject());
48
- return;
49
- }));
50
- }
51
- return this.diosphere;
63
+ this.initialiseDiograph = async (roomObject) => {
64
+ var _a;
65
+ console.info('initialiseDiograph', roomObject);
66
+ this.selectRoom(roomObject);
67
+ this.diograph.resetDiograph();
68
+ const diographObject = (_a = (await this.getDiograph())) !== null && _a !== void 0 ? _a : (0, getDefaultDiograph_1.getDefaultDiograph)();
69
+ this.diograph.addDiograph(diographObject);
70
+ this.focusDiory({ id: '/' });
71
+ console.info(this.diograph.toObject());
72
+ return this.diograph;
52
73
  };
53
- this.getDiograph = async () => {
54
- var _a, _b;
55
- console.info('getDiograph', (_a = this.room) === null || _a === void 0 ? void 0 : _a.connections);
56
- if ((_b = this.room) === null || _b === void 0 ? void 0 : _b.connections) {
57
- const connectionClients = (0, getConnectionClients_1.getConnectionClients)(this.dataClients, this.room.connections);
58
- await Promise.all(connectionClients.map(async (connectionClient) => {
59
- const diographObject = await connectionClient.getDiograph();
74
+ this.getDiograph = async (connections) => {
75
+ console.info('getDiograph', connections);
76
+ let diographObject;
77
+ await Promise.all(this.getDiographClients(connections).map(async (connectionClient) => {
78
+ try {
79
+ diographObject = await connectionClient.getDiograph();
60
80
  console.info(diographObject);
61
- this.diograph.addDiograph(diographObject);
62
- return;
63
- }));
64
- }
65
- return this.diograph;
81
+ }
82
+ catch (error) {
83
+ console.error(error);
84
+ }
85
+ return;
86
+ }));
87
+ return diographObject;
66
88
  };
67
- this.saveDiograph = async () => {
68
- var _a, _b;
69
- console.info('saveDiograph', (_a = this.room) === null || _a === void 0 ? void 0 : _a.connections);
70
- if ((_b = this.room) === null || _b === void 0 ? void 0 : _b.connections) {
71
- const connectionClients = (0, getConnectionClients_1.getConnectionClients)(this.dataClients, this.room.connections);
72
- await Promise.all(connectionClients.map(async (connectionClient) => {
73
- console.info(this.diograph.toObject());
74
- await connectionClient.saveDiograph(this.diograph.toObject());
75
- return;
76
- }));
77
- }
78
- return this.diograph;
89
+ this.saveDiograph = async (connections) => {
90
+ console.info('saveDiograph', connections);
91
+ const diographObject = this.diograph.toObject();
92
+ await Promise.all(this.getDiographClients(connections).map(async (connectionClient) => {
93
+ await connectionClient.saveDiograph(diographObject);
94
+ console.info(diographObject);
95
+ return;
96
+ }));
97
+ return diographObject;
79
98
  };
80
- this.generateDiograph = async () => {
81
- var _a, _b, _c;
82
- console.info('generateDiograph', (_a = this.room) === null || _a === void 0 ? void 0 : _a.connections);
83
- if ((_b = this.room) === null || _b === void 0 ? void 0 : _b.connections) {
84
- const connectionClients = (0, getConnectionClients_1.getConnectionClients)(this.dataClients, (_c = this.room) === null || _c === void 0 ? void 0 : _c.connections);
85
- await Promise.all(connectionClients.map(async (connectionClient) => {
86
- const diographObject = await connectionClient.generateDiograph();
87
- console.info(diographObject);
88
- this.diograph.addDiograph(diographObject);
89
- await connectionClient.saveDiograph(this.diograph.toObject());
90
- return;
91
- }));
92
- }
93
- return this.diograph;
99
+ this.mergeDiograph = async (diographObject) => {
100
+ Object.entries(diographObject).forEach(([key, dioryObject]) => {
101
+ try {
102
+ key === '/'
103
+ ? this.diograph.addDioryLink({ id: '/' }, diographObject['/'])
104
+ : this.diograph.addDiory(dioryObject);
105
+ }
106
+ catch (error) {
107
+ console.error(error);
108
+ }
109
+ });
110
+ await this.saveDiograph();
111
+ console.info(this.diograph.toObject());
112
+ };
113
+ this.generateDiograph = async (connections) => {
114
+ console.info('generateDiograph', connections);
115
+ let diographObject;
116
+ await Promise.all(this.getDiographClients(connections).map(async (connectionClient) => {
117
+ diographObject = await connectionClient.generateDiograph();
118
+ console.info(diographObject);
119
+ return;
120
+ }));
121
+ return diographObject;
94
122
  };
95
123
  this.dataClients = dataClients;
96
124
  this.diosphere = new diosphere_js_1.Diosphere();
97
125
  this.diograph = new diograph_1.Diograph();
98
- this.diosphere.saveDiosphere = this.saveDiosphere;
99
- this.diograph.saveDiograph = this.saveDiograph;
126
+ this.diosphere.saveDiosphere = (0, debounce_1.debounce)(this.saveDiosphere, 1000);
127
+ this.diograph.saveDiograph = (0, debounce_1.debounce)(this.saveDiograph, 1000);
100
128
  }
101
129
  }
102
130
  exports.DioryClient = DioryClient;
@@ -0,0 +1,2 @@
1
+ import { IDiographObject } from '@diograph/diograph';
2
+ export declare const getDefaultDiograph: () => IDiographObject;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getDefaultDiograph = void 0;
4
+ const diograph_1 = require("@diograph/diograph");
5
+ const getDefaultDiograph = () => {
6
+ const diograph = new diograph_1.Diograph();
7
+ const diory = diograph.addDiory({
8
+ text: 'Welcome to Diory',
9
+ latlng: '60.01366036242365, 20.007133483886722',
10
+ });
11
+ diograph.addDiory({
12
+ text: 'Room root',
13
+ links: [{ id: diory.id }],
14
+ }, '/');
15
+ return diograph.toObject();
16
+ };
17
+ exports.getDefaultDiograph = getDefaultDiograph;
@@ -0,0 +1,2 @@
1
+ import { IConnectionObject, IDiosphereObject } from '@diory/diosphere-js';
2
+ export declare const getDefaultDiosphere: (connections: IConnectionObject[]) => IDiosphereObject;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getDefaultDiosphere = void 0;
4
+ const diosphere_js_1 = require("@diory/diosphere-js");
5
+ const getDefaultDiosphere = (connections) => {
6
+ const diosphere = new diosphere_js_1.Diosphere();
7
+ diosphere.addRoom({
8
+ text: 'Welcome room',
9
+ connections,
10
+ }, '/');
11
+ return diosphere.toObject();
12
+ };
13
+ exports.getDefaultDiosphere = getDefaultDiosphere;
package/dist/types.d.ts CHANGED
@@ -5,6 +5,9 @@ export interface IMetadata {
5
5
  name: string;
6
6
  created?: string;
7
7
  modified?: string;
8
+ duration?: string;
9
+ thumbnail?: string;
10
+ latlng?: string;
8
11
  }
9
12
  export interface IFileType {
10
13
  ext?: string;
@@ -23,8 +26,10 @@ export interface IDataClient {
23
26
  list(url: string): Promise<string[]>;
24
27
  getFileNames(url: string): Promise<string[]>;
25
28
  getFolderNames(url: string): Promise<string[]>;
26
- getMetadata(url: string): IMetadata;
27
29
  getFileType(url: string): Promise<IFileType>;
30
+ getMetadata(url: string): IMetadata;
31
+ getThumbnail?(imageUrl: string): Promise<string | undefined>;
32
+ getVideoMetadata?(videoUrl: string): Promise<IMetadata>;
28
33
  }
29
34
  export interface IConnectionClient {
30
35
  type: string;
@@ -43,12 +48,13 @@ export interface IDioryClient {
43
48
  room?: IRoom;
44
49
  diograph: IDiograph;
45
50
  diory?: IDiory;
46
- initialise: (connections: IConnectionObject[]) => Promise<IDioryClient>;
47
- enterRoom: (roomObject: IRoomObject) => Promise<IRoom>;
51
+ selectRoom: (roomObject: IRoomObject) => IRoom;
52
+ getDiosphere: () => Promise<IDiosphereObject | undefined>;
53
+ saveDiosphere: () => Promise<IDiosphereObject>;
54
+ initialiseDiosphere: (connections: IConnectionObject[]) => Promise<IDiosphere>;
48
55
  focusDiory: (dioryObject: IDioryObject) => IDiory;
49
- getDiosphere: () => Promise<IDiosphere>;
50
- saveDiosphere: () => Promise<IDiosphere>;
51
- getDiograph: () => Promise<IDiograph>;
52
- saveDiograph: () => Promise<IDiograph>;
53
- generateDiograph: () => Promise<IDiograph>;
56
+ getDiograph: () => Promise<IDiographObject | undefined>;
57
+ saveDiograph: () => Promise<IDiographObject>;
58
+ initialiseDiograph: (roomObject: IRoomObject) => Promise<IDiograph>;
59
+ generateDiograph: () => Promise<IDiographObject | undefined>;
54
60
  }
@@ -1,3 +1,3 @@
1
1
  import { IConnectionObject } from '@diory/diosphere-js';
2
2
  import { IConnectionClient, IDataClient } from '../types';
3
- export declare function getConnectionClients(dataClients: IDataClient[], connections: IConnectionObject[]): IConnectionClient[];
3
+ export declare function getConnectionClients(dataClients: IDataClient[], connections?: IConnectionObject[]): IConnectionClient[];
@@ -6,9 +6,7 @@ function getDataClient(dataClients, { client }) {
6
6
  return dataClients.find(({ type }) => type === client);
7
7
  }
8
8
  function getConnectionClients(dataClients, connections) {
9
- return connections
10
- .filter(({ client }) => dataClients.some(({ type }) => type === client))
11
- .map((connection) => {
9
+ return connections === null || connections === void 0 ? void 0 : connections.filter(({ client }) => dataClients.some(({ type }) => type === client)).map((connection) => {
12
10
  const dataClient = getDataClient(dataClients, connection);
13
11
  return new connectionClient_1.ConnectionClient(dataClient, connection);
14
12
  });
package/jest.config.js ADDED
@@ -0,0 +1,6 @@
1
+ /** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
2
+ module.exports = {
3
+ preset: 'ts-jest',
4
+ testEnvironment: 'node',
5
+ testPathIgnorePatterns: ['dist', 'node_modules'],
6
+ }
package/package.json CHANGED
@@ -1,17 +1,14 @@
1
1
  {
2
2
  "name": "@diory/client-js",
3
- "version": "0.2.0-rc2",
3
+ "version": "0.2.0-rc20",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
- "files": [
7
- "dist"
8
- ],
9
6
  "author": "Olli-Pekka Pohjola <op@diory.me>, Jouni Alanen <jouni@diory.me>",
10
7
  "license": "MIT",
11
8
  "dependencies": {
12
- "@diograph/diograph": "0.3.0-rc14",
13
- "@diograph/folder-generator": "^0.3.0-rc1",
14
- "@diory/diosphere-js": "0.2.6-rc2",
9
+ "@diograph/diograph": "0.3.0-rc16",
10
+ "@diograph/folder-generator": "^0.3.0-rc3",
11
+ "@diory/diosphere-js": "0.2.6-rc4",
15
12
  "path-browserify": "^1.0.1",
16
13
  "uuid": "8.3.2"
17
14
  },
@@ -0,0 +1,19 @@
1
+ describe('connectionClient', () => {
2
+ describe('construct()', () => {
3
+ it('should construct with dataClient and connection', () => {
4
+ expect(true).toEqual(true)
5
+ })
6
+ })
7
+
8
+ describe('getDiosphere()', () => {
9
+ it('should get diosphere', () => {
10
+ expect(true).toEqual(true)
11
+ })
12
+ })
13
+
14
+ describe('saveDiosphere()', () => {
15
+ it('should save diosphere object', () => {
16
+ expect(true).toEqual(true)
17
+ })
18
+ })
19
+ })
@@ -0,0 +1,52 @@
1
+ import { join } from 'path-browserify'
2
+ import { generateDiograph } from '@diograph/folder-generator'
3
+
4
+ import { IDiographObject } from '@diograph/diograph'
5
+ import { IConnectionObject, IDiosphereObject } from '@diory/diosphere-js'
6
+ import { IConnectionClient, IDataClient } from '../types'
7
+
8
+ const DIOSPHERE_JSON = 'diosphere.json'
9
+ const DIOGRAPH_JSON = 'diograph.json'
10
+
11
+ class ConnectionClient implements IConnectionClient {
12
+ type: string
13
+ client: IDataClient
14
+ connection: IConnectionObject
15
+
16
+ constructor(dataClient: IDataClient, connection: IConnectionObject) {
17
+ this.type = dataClient.type
18
+ this.client = dataClient
19
+ this.connection = connection
20
+ }
21
+
22
+ getDiosphere = async () => {
23
+ const path = join(this.connection.address, DIOSPHERE_JSON)
24
+ const diosphereString = await this.client.readTextItem(path)
25
+ return JSON.parse(diosphereString)
26
+ }
27
+
28
+ saveDiosphere = async (diosphereObject: IDiosphereObject) => {
29
+ const path = join(this.connection.address, DIOSPHERE_JSON)
30
+ const diosphereString = JSON.stringify(diosphereObject, null, 2)
31
+ return this.client.writeItem(path, diosphereString)
32
+ }
33
+
34
+ getDiograph = async () => {
35
+ const path = join(this.connection.address, DIOGRAPH_JSON)
36
+ const diographString = await this.client.readTextItem(path)
37
+ return JSON.parse(diographString)
38
+ }
39
+
40
+ saveDiograph = async (diographObject: IDiographObject) => {
41
+ const path = join(this.connection.address, DIOGRAPH_JSON)
42
+ const diographString = JSON.stringify(diographObject, null, 2)
43
+ return this.client.writeItem(path, diographString)
44
+ }
45
+
46
+ generateDiograph = async (): Promise<IDiographObject> => {
47
+ const { diograph } = await generateDiograph(this.connection.address, this.client)
48
+ return diograph.toObject()
49
+ }
50
+ }
51
+
52
+ export { ConnectionClient }
@@ -0,0 +1,18 @@
1
+ export function debounce(func: any, wait: number, immediate = false) {
2
+ let timeout: any
3
+ // eslint-disable-next-line func-names
4
+ return (): typeof func => {
5
+ // @ts-ignore
6
+ const context = this
7
+ const args = arguments
8
+ // eslint-disable-next-line func-names
9
+ const later = function () {
10
+ timeout = null
11
+ if (!immediate) func.apply(context, args)
12
+ }
13
+ const callNow = immediate && !timeout
14
+ clearTimeout(timeout)
15
+ timeout = setTimeout(later, wait)
16
+ if (callNow) func.apply(context, args)
17
+ }
18
+ }
@@ -0,0 +1,19 @@
1
+ describe('dioryClient', () => {
2
+ describe('constructor()', () => {
3
+ it('should construct with dataClient', () => {
4
+ expect(true).toEqual(true)
5
+ })
6
+ })
7
+
8
+ describe('initialise()', () => {
9
+ it('should initialise with connections', () => {
10
+ expect(true).toEqual(true)
11
+ })
12
+ })
13
+
14
+ describe('enterRoom()', () => {
15
+ it('should initialise with connections', () => {
16
+ expect(true).toEqual(true)
17
+ })
18
+ })
19
+ })
@@ -0,0 +1,183 @@
1
+ import {
2
+ Diosphere,
3
+ IConnectionObject,
4
+ IDiosphere,
5
+ IDiosphereObject,
6
+ IRoom,
7
+ IRoomObject,
8
+ } from '@diory/diosphere-js'
9
+ import { Diograph, IDiograph, IDiographObject, IDiory, IDioryObject } from '@diograph/diograph'
10
+
11
+ import { IDioryClient, IDataClient } from '../types'
12
+ import { getConnectionClients } from '../utils/getConnectionClients'
13
+ import { debounce } from '../connectionClient/utils/debounce'
14
+
15
+ import { getDefaultDiosphere } from './getDefaultDiosphere'
16
+ import { getDefaultDiograph } from './getDefaultDiograph'
17
+
18
+ class DioryClient implements IDioryClient {
19
+ dataClients: IDataClient[] = []
20
+ connections: IConnectionObject[] = []
21
+ diosphere: IDiosphere
22
+ room?: IRoom
23
+ diograph: IDiograph
24
+ diory?: IDiory
25
+
26
+ constructor(dataClients: IDataClient[]) {
27
+ this.dataClients = dataClients
28
+
29
+ this.diosphere = new Diosphere()
30
+ this.diograph = new Diograph()
31
+
32
+ this.diosphere.saveDiosphere = debounce(this.saveDiosphere, 1000)
33
+ this.diograph.saveDiograph = debounce(this.saveDiograph, 1000)
34
+ }
35
+
36
+ selectRoom = (roomObject: IRoomObject): IRoom => {
37
+ return (this.room = this.diosphere.getRoom(roomObject))
38
+ }
39
+
40
+ getDiosphereClients = (connections?: IConnectionObject[]) => {
41
+ return getConnectionClients(this.dataClients, connections ?? this.connections)
42
+ }
43
+
44
+ initialiseDiosphere = async (connections: IConnectionObject[]): Promise<IDiosphere> => {
45
+ console.info('initialiseDiosphere', connections)
46
+ this.connections = connections
47
+
48
+ this.diosphere.resetRooms()
49
+ const diosphereObject = (await this.getDiosphere()) ?? getDefaultDiosphere(connections)
50
+ this.diosphere.addDiosphere(diosphereObject)
51
+
52
+ this.selectRoom({ id: '/' })
53
+
54
+ console.info(this.diosphere.toObject())
55
+ return this.diosphere
56
+ }
57
+
58
+ getDiosphere = async (
59
+ connections?: IConnectionObject[],
60
+ ): Promise<IDiosphereObject | undefined> => {
61
+ console.info('getDiosphere', connections)
62
+
63
+ let diosphereObject
64
+ await Promise.all(
65
+ this.getDiosphereClients(connections).map(async (connectionClient) => {
66
+ try {
67
+ diosphereObject = await connectionClient.getDiosphere()
68
+ console.info(diosphereObject)
69
+ } catch (error) {
70
+ console.error(error)
71
+ }
72
+ return
73
+ }),
74
+ )
75
+
76
+ return diosphereObject
77
+ }
78
+
79
+ saveDiosphere = async (connections?: IConnectionObject[]): Promise<IDiosphereObject> => {
80
+ console.info('saveDiosphere', connections)
81
+
82
+ const diosphereObject = this.diosphere.toObject()
83
+ await Promise.all(
84
+ this.getDiosphereClients(connections).map(async (connectionClient) => {
85
+ await connectionClient.saveDiosphere(diosphereObject)
86
+ console.info(diosphereObject)
87
+ return
88
+ }),
89
+ )
90
+
91
+ return diosphereObject
92
+ }
93
+
94
+ getDiographClients = (connections?: IConnectionObject[]) => {
95
+ return getConnectionClients(this.dataClients, connections ?? this.room?.connections)
96
+ }
97
+
98
+ focusDiory = (dioryObject: IDioryObject): IDiory => {
99
+ return (this.diory = this.diograph.getDiory(dioryObject))
100
+ }
101
+
102
+ initialiseDiograph = async (roomObject: IRoomObject): Promise<IDiograph> => {
103
+ console.info('initialiseDiograph', roomObject)
104
+ this.selectRoom(roomObject)
105
+
106
+ this.diograph.resetDiograph()
107
+ const diographObject = (await this.getDiograph()) ?? getDefaultDiograph()
108
+ this.diograph.addDiograph(diographObject)
109
+
110
+ this.focusDiory({ id: '/' })
111
+
112
+ console.info(this.diograph.toObject())
113
+ return this.diograph
114
+ }
115
+
116
+ getDiograph = async (connections?: IConnectionObject[]): Promise<IDiographObject | undefined> => {
117
+ console.info('getDiograph', connections)
118
+
119
+ let diographObject
120
+ await Promise.all(
121
+ this.getDiographClients(connections).map(async (connectionClient) => {
122
+ try {
123
+ diographObject = await connectionClient.getDiograph()
124
+ console.info(diographObject)
125
+ } catch (error) {
126
+ console.error(error)
127
+ }
128
+ return
129
+ }),
130
+ )
131
+
132
+ return diographObject
133
+ }
134
+
135
+ saveDiograph = async (connections?: IConnectionObject[]): Promise<IDiographObject> => {
136
+ console.info('saveDiograph', connections)
137
+
138
+ const diographObject = this.diograph.toObject()
139
+ await Promise.all(
140
+ this.getDiographClients(connections).map(async (connectionClient) => {
141
+ await connectionClient.saveDiograph(diographObject)
142
+ console.info(diographObject)
143
+ return
144
+ }),
145
+ )
146
+
147
+ return diographObject
148
+ }
149
+
150
+ mergeDiograph = async (diographObject: IDiographObject) => {
151
+ Object.entries(diographObject).forEach(([key, dioryObject]) => {
152
+ try {
153
+ key === '/'
154
+ ? this.diograph.addDioryLink({ id: '/' }, diographObject['/'])
155
+ : this.diograph.addDiory(dioryObject)
156
+ } catch (error) {
157
+ console.error(error)
158
+ }
159
+ })
160
+
161
+ await this.saveDiograph()
162
+ console.info(this.diograph.toObject())
163
+ }
164
+
165
+ generateDiograph = async (
166
+ connections?: IConnectionObject[],
167
+ ): Promise<IDiographObject | undefined> => {
168
+ console.info('generateDiograph', connections)
169
+
170
+ let diographObject
171
+ await Promise.all(
172
+ this.getDiographClients(connections).map(async (connectionClient) => {
173
+ diographObject = await connectionClient.generateDiograph()
174
+ console.info(diographObject)
175
+ return
176
+ }),
177
+ )
178
+
179
+ return diographObject
180
+ }
181
+ }
182
+
183
+ export { DioryClient }
@@ -0,0 +1,18 @@
1
+ import { Diograph, IDiographObject } from '@diograph/diograph'
2
+
3
+ export const getDefaultDiograph = (): IDiographObject => {
4
+ const diograph = new Diograph()
5
+ const diory = diograph.addDiory({
6
+ text: 'Welcome to Diory',
7
+ latlng: '60.01366036242365, 20.007133483886722',
8
+ })
9
+ diograph.addDiory(
10
+ {
11
+ text: 'Room root',
12
+ links: [{ id: diory.id }],
13
+ },
14
+ '/',
15
+ )
16
+
17
+ return diograph.toObject()
18
+ }
@@ -0,0 +1,14 @@
1
+ import { Diosphere, IConnectionObject, IDiosphereObject } from '@diory/diosphere-js'
2
+
3
+ export const getDefaultDiosphere = (connections: IConnectionObject[]): IDiosphereObject => {
4
+ const diosphere = new Diosphere()
5
+ diosphere.addRoom(
6
+ {
7
+ text: 'Welcome room',
8
+ connections,
9
+ },
10
+ '/',
11
+ )
12
+
13
+ return diosphere.toObject()
14
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export { DioryClient } from './dioryClient/dioryClient'
2
+ export { ConnectionClient } from './connectionClient/connectionClient'
3
+ export { IDioryClient, IConnectionClient, IDataClient, IFileType, IMetadata } from './types'
package/src/types.ts ADDED
@@ -0,0 +1,70 @@
1
+ import {
2
+ IConnectionObject,
3
+ IDiosphere,
4
+ IDiosphereObject,
5
+ IRoom,
6
+ IRoomObject,
7
+ } from '@diory/diosphere-js'
8
+ import { IDiograph, IDiographObject, IDiory, IDioryObject } from '@diograph/diograph'
9
+
10
+ export interface IMetadata {
11
+ name: string
12
+ created?: string
13
+ modified?: string
14
+ duration?: string
15
+ thumbnail?: string
16
+ latlng?: string
17
+ }
18
+
19
+ export interface IFileType {
20
+ ext?: string
21
+ mime?: string
22
+ }
23
+
24
+ export interface IDataClient {
25
+ type: string
26
+ readTextItem(url: string): Promise<string>
27
+ readItem(url: string): Promise<Buffer>
28
+ readToStream(url: string): any
29
+ exists(url: string): Promise<boolean>
30
+ writeTextItem(url: string, fileContent: string): Promise<boolean>
31
+ writeItem(url: string, fileContent: Buffer | string): Promise<boolean>
32
+ deleteItem(url: string): Promise<boolean>
33
+ deleteFolder(url: string): Promise<void>
34
+ list(url: string): Promise<string[]>
35
+ getFileNames(url: string): Promise<string[]>
36
+ getFolderNames(url: string): Promise<string[]>
37
+ getFileType(url: string): Promise<IFileType>
38
+ getMetadata(url: string): IMetadata
39
+ getThumbnail?(imageUrl: string): Promise<string | undefined>
40
+ getVideoMetadata?(videoUrl: string): Promise<IMetadata>
41
+ }
42
+
43
+ export interface IConnectionClient {
44
+ type: string
45
+ client: IDataClient
46
+ connection: IConnectionObject
47
+ getDiosphere: () => Promise<IDiosphereObject>
48
+ saveDiosphere: (diosphereObject: IDiosphereObject) => void
49
+ getDiograph: () => Promise<IDiographObject>
50
+ saveDiograph: (diographObject: IDiographObject) => void
51
+ generateDiograph: () => Promise<IDiographObject>
52
+ }
53
+
54
+ export interface IDioryClient {
55
+ dataClients: IDataClient[]
56
+ connections: IConnectionObject[]
57
+ diosphere: IDiosphere
58
+ room?: IRoom
59
+ diograph: IDiograph
60
+ diory?: IDiory
61
+ selectRoom: (roomObject: IRoomObject) => IRoom
62
+ getDiosphere: () => Promise<IDiosphereObject | undefined>
63
+ saveDiosphere: () => Promise<IDiosphereObject>
64
+ initialiseDiosphere: (connections: IConnectionObject[]) => Promise<IDiosphere>
65
+ focusDiory: (dioryObject: IDioryObject) => IDiory
66
+ getDiograph: () => Promise<IDiographObject | undefined>
67
+ saveDiograph: () => Promise<IDiographObject>
68
+ initialiseDiograph: (roomObject: IRoomObject) => Promise<IDiograph>
69
+ generateDiograph: () => Promise<IDiographObject | undefined>
70
+ }
@@ -0,0 +1,23 @@
1
+ import { IConnectionObject } from '@diory/diosphere-js'
2
+
3
+ import { ConnectionClient } from '../connectionClient/connectionClient'
4
+ import { IConnectionClient, IDataClient } from '../types'
5
+
6
+ function getDataClient(
7
+ dataClients: IDataClient[],
8
+ { client }: IConnectionObject,
9
+ ): IDataClient | undefined {
10
+ return dataClients.find(({ type }) => type === client)
11
+ }
12
+
13
+ export function getConnectionClients(
14
+ dataClients: IDataClient[],
15
+ connections?: IConnectionObject[],
16
+ ): IConnectionClient[] {
17
+ return connections
18
+ ?.filter(({ client }) => dataClients.some(({ type }) => type === client))
19
+ .map((connection) => {
20
+ const dataClient = getDataClient(dataClients, connection)
21
+ return new ConnectionClient(dataClient!, connection)
22
+ }) as IConnectionClient[]
23
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "compilerOptions": {
3
+ "outDir": "dist",
4
+ "declaration": true,
5
+ "target": "es2017",
6
+ "module": "commonjs",
7
+ "strict": true,
8
+ "forceConsistentCasingInFileNames": true,
9
+ "resolveJsonModule": true,
10
+ "esModuleInterop": true
11
+ },
12
+ "exclude": ["node_modules", "dist"]
13
+ }