@kokimoki/app 0.0.9 → 0.1.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/dist/index.d.ts CHANGED
@@ -1,6 +1,11 @@
1
+ import { KokimokiClient } from "./kokimoki-client";
2
+ import { UserMediaHandler } from "./user-media-handler";
1
3
  export * from "./types/api";
2
4
  export * from "./types/tasks";
5
+ export * from "./types/common";
6
+ export * from "./types/events";
3
7
  export * from "./fields";
4
8
  export * from "./kokimoki-client";
5
9
  export * from "./synced-store";
6
- export * from "@syncedstore/core";
10
+ export declare const kmClient: KokimokiClient<any>;
11
+ export declare const userMediaHandler: UserMediaHandler;
package/dist/index.js CHANGED
@@ -1,6 +1,11 @@
1
+ import { KokimokiClient } from "./kokimoki-client";
2
+ import { UserMediaHandler } from "./user-media-handler";
1
3
  export * from "./types/api";
2
4
  export * from "./types/tasks";
5
+ export * from "./types/common";
6
+ export * from "./types/events";
3
7
  export * from "./fields";
4
8
  export * from "./kokimoki-client";
5
9
  export * from "./synced-store";
6
- export * from "@syncedstore/core";
10
+ export const kmClient = new KokimokiClient();
11
+ export const userMediaHandler = new UserMediaHandler();
@@ -6,17 +6,17 @@ import type { UploadedMedia } from "./types/api";
6
6
  import type { KokimokiClientEvents } from "./types/events";
7
7
  declare const KokimokiClient_base: new <T>() => TypedEmitter<KokimokiClientEvents<T>>;
8
8
  export declare class KokimokiClient<StatelessDataT = any> extends KokimokiClient_base<StatelessDataT> {
9
- readonly appId: string;
10
9
  serverTimeOffset: number;
11
- wsUrl: string;
12
- apiUrl: string;
10
+ private _appId?;
11
+ private _wsUrl?;
12
+ private _apiUrl?;
13
13
  private _id;
14
14
  private _token;
15
15
  private _providers;
16
- constructor(host: string, appId: string);
16
+ constructor();
17
17
  get id(): string;
18
18
  get token(): string;
19
- connect(): Promise<void>;
19
+ connect(host: string, appId: string): Promise<void>;
20
20
  setProvider<T extends DocTypeDescription>(name: string, store: SyncedStore<T>): Promise<void>;
21
21
  removeProvider(name: string): void;
22
22
  getProvider(name: string): HocuspocusProvider | undefined;
@@ -24,14 +24,5 @@ export declare class KokimokiClient<StatelessDataT = any> extends KokimokiClient
24
24
  sendStatelessToClient(room: string, clientId: string, data: StatelessDataT): void;
25
25
  sendStatelessToRoom(room: string, data: StatelessDataT, self?: boolean): void;
26
26
  mediaUpload(blob: Blob): Promise<UploadedMedia>;
27
- private _facingMode;
28
- private _stream;
29
- private _mediaRequests;
30
- requestMediaStream(): void;
31
- releaseMediaStream(): void;
32
- private closeMediaStream;
33
- private updateMediaStream;
34
- setFacingMode(facingMode: "user" | "environment"): void;
35
- toggleFacingMode(): void;
36
27
  }
37
28
  export {};
@@ -6,19 +6,15 @@ import { uploadMedia } from "./api/uploadMedia";
6
6
  import { errorMessage } from "./utils";
7
7
  import { fetchAuth } from "./api/fetchAuth";
8
8
  export class KokimokiClient extends EventEmitter {
9
- appId;
10
9
  serverTimeOffset = 0;
11
- wsUrl;
12
- apiUrl;
10
+ _appId;
11
+ _wsUrl;
12
+ _apiUrl;
13
13
  _id = "";
14
14
  _token = "";
15
15
  _providers = new Map();
16
- constructor(host, appId) {
16
+ constructor() {
17
17
  super();
18
- this.appId = appId;
19
- const secure = host.indexOf(":") === -1;
20
- this.wsUrl = `ws${secure ? "s" : ""}://${host}`;
21
- this.apiUrl = `http${secure ? "s" : ""}://${host}`;
22
18
  }
23
19
  get id() {
24
20
  if (!this._id) {
@@ -32,13 +28,19 @@ export class KokimokiClient extends EventEmitter {
32
28
  }
33
29
  return this._token;
34
30
  }
35
- async connect() {
31
+ async connect(host, appId) {
32
+ this._appId = appId;
33
+ // Set up the URLs
34
+ const secure = host.indexOf(":") === -1;
35
+ this._wsUrl = `ws${secure ? "s" : ""}://${host}`;
36
+ this._apiUrl = `http${secure ? "s" : ""}://${host}`;
37
+ // Fetch the auth token
36
38
  let clientToken = localStorage.getItem("KM_TOKEN");
37
39
  const startTime = Date.now();
38
40
  const { clientId, appToken, serverTime, token } = await fetchAuth({
39
- appId: this.appId,
41
+ appId,
40
42
  clientToken,
41
- apiUrl: this.apiUrl,
43
+ apiUrl: this._apiUrl,
42
44
  });
43
45
  const endTime = Date.now();
44
46
  const ping = Math.round((endTime - startTime) / 2);
@@ -48,9 +50,12 @@ export class KokimokiClient extends EventEmitter {
48
50
  localStorage.setItem("KM_TOKEN", token);
49
51
  }
50
52
  async setProvider(name, store) {
53
+ if (!this._wsUrl || !this._appId) {
54
+ throw new Error("Client not connected");
55
+ }
51
56
  const provider = new HocuspocusProvider({
52
- url: `${this.wsUrl}/connection`,
53
- name: `${this.appId}/${name}`,
57
+ url: `${this._wsUrl}/connection`,
58
+ name: `${this._appId}/${name}`,
54
59
  document: store.doc,
55
60
  token: this.token,
56
61
  });
@@ -98,77 +103,21 @@ export class KokimokiClient extends EventEmitter {
98
103
  provider.sendStateless(JSON.stringify({ data, self }));
99
104
  }
100
105
  async mediaUpload(blob) {
106
+ if (!this._apiUrl) {
107
+ throw new Error("Client not connected");
108
+ }
101
109
  try {
102
110
  const uploadData = await sendMediaUpload(blob, {
103
- apiUrl: this.apiUrl,
111
+ apiUrl: this._apiUrl,
104
112
  appToken: this.token,
105
113
  });
106
114
  const { id } = uploadData;
107
115
  const etags = await getETags(blob, uploadData);
108
- const { url } = await uploadMedia({ id, etags }, { apiUrl: this.apiUrl, appToken: this.token });
116
+ const { url } = await uploadMedia({ id, etags }, { apiUrl: this._apiUrl, appToken: this.token });
109
117
  return { url, id };
110
118
  }
111
119
  catch (error) {
112
120
  throw new Error(`Error while uploading media: ${errorMessage(error)}`);
113
121
  }
114
122
  }
115
- // User media
116
- _facingMode = "environment";
117
- _stream = null;
118
- _mediaRequests = 0;
119
- requestMediaStream() {
120
- this._mediaRequests++;
121
- this.updateMediaStream();
122
- }
123
- releaseMediaStream() {
124
- this._mediaRequests--;
125
- if (this._mediaRequests <= 0) {
126
- this.closeMediaStream();
127
- this._mediaRequests = 0;
128
- }
129
- }
130
- closeMediaStream() {
131
- if (!this._stream) {
132
- return;
133
- }
134
- this.emit("media-stream", null, this._facingMode, null);
135
- this._stream.getTracks().forEach((track) => track.stop());
136
- this._stream = null;
137
- }
138
- async updateMediaStream() {
139
- this.closeMediaStream();
140
- try {
141
- this._stream = await navigator.mediaDevices.getUserMedia({
142
- video: {
143
- facingMode: this._facingMode,
144
- frameRate: { ideal: 24, max: 30 },
145
- },
146
- audio: true,
147
- });
148
- this.emit("media-stream", null, this._facingMode, this._stream);
149
- }
150
- catch (err) {
151
- this._stream = null;
152
- if ((err + "").match(/^NotReadableError/)) {
153
- await new Promise((resolve) => {
154
- setTimeout(async () => {
155
- await this.updateMediaStream();
156
- resolve();
157
- }, 1000);
158
- });
159
- return;
160
- }
161
- this.emit("media-stream", err + "", this._facingMode, null);
162
- }
163
- }
164
- setFacingMode(facingMode) {
165
- if (this._facingMode === facingMode) {
166
- return;
167
- }
168
- this._facingMode = facingMode;
169
- this.updateMediaStream();
170
- }
171
- toggleFacingMode() {
172
- this.setFacingMode(this._facingMode === "user" ? "environment" : "user");
173
- }
174
123
  }
@@ -0,0 +1 @@
1
+ export type FacingMode = "user" | "environment";
@@ -0,0 +1 @@
1
+ export {};
@@ -1,4 +1,6 @@
1
1
  export type KokimokiClientEvents<T> = {
2
2
  stateless: (room: string, from: string, data: T) => void;
3
- "media-stream": (error: string | null, facingMode: "user" | "environment", stream: MediaStream | null) => void;
3
+ };
4
+ export type UserMediaHandlerEvents = {
5
+ stream: (error: string | null, facingMode: "user" | "environment", stream: MediaStream | null) => void;
4
6
  };
@@ -0,0 +1,15 @@
1
+ import type TypedEmitter from "typed-emitter";
2
+ import type { UserMediaHandlerEvents } from "./types/events";
3
+ declare const UserMediaHandler_base: new () => TypedEmitter<UserMediaHandlerEvents>;
4
+ export declare class UserMediaHandler extends UserMediaHandler_base {
5
+ private _facingMode;
6
+ private _stream;
7
+ private _mediaRequests;
8
+ requestMediaStream(): void;
9
+ releaseMediaStream(): void;
10
+ private closeMediaStream;
11
+ private updateMediaStream;
12
+ setFacingMode(facingMode: "user" | "environment"): void;
13
+ toggleFacingMode(): void;
14
+ }
15
+ export {};
@@ -0,0 +1,61 @@
1
+ import EventEmitter from "events";
2
+ export class UserMediaHandler extends EventEmitter {
3
+ _facingMode = "environment";
4
+ _stream = null;
5
+ _mediaRequests = 0;
6
+ requestMediaStream() {
7
+ this._mediaRequests++;
8
+ this.updateMediaStream();
9
+ }
10
+ releaseMediaStream() {
11
+ this._mediaRequests--;
12
+ if (this._mediaRequests <= 0) {
13
+ this.closeMediaStream();
14
+ this._mediaRequests = 0;
15
+ }
16
+ }
17
+ closeMediaStream() {
18
+ if (!this._stream) {
19
+ return;
20
+ }
21
+ this.emit("stream", null, this._facingMode, null);
22
+ this._stream.getTracks().forEach((track) => track.stop());
23
+ this._stream = null;
24
+ }
25
+ async updateMediaStream() {
26
+ this.closeMediaStream();
27
+ try {
28
+ this._stream = await navigator.mediaDevices.getUserMedia({
29
+ video: {
30
+ facingMode: this._facingMode,
31
+ frameRate: { ideal: 24, max: 30 },
32
+ },
33
+ audio: true,
34
+ });
35
+ this.emit("stream", null, this._facingMode, this._stream);
36
+ }
37
+ catch (err) {
38
+ this._stream = null;
39
+ if ((err + "").match(/^NotReadableError/)) {
40
+ await new Promise((resolve) => {
41
+ setTimeout(async () => {
42
+ await this.updateMediaStream();
43
+ resolve();
44
+ }, 1000);
45
+ });
46
+ return;
47
+ }
48
+ this.emit("stream", err + "", this._facingMode, null);
49
+ }
50
+ }
51
+ setFacingMode(facingMode) {
52
+ if (this._facingMode === facingMode) {
53
+ return;
54
+ }
55
+ this._facingMode = facingMode;
56
+ this.updateMediaStream();
57
+ }
58
+ toggleFacingMode() {
59
+ this.setFacingMode(this._facingMode === "user" ? "environment" : "user");
60
+ }
61
+ }
package/dist/utils.d.ts CHANGED
@@ -1,6 +1,2 @@
1
- export declare function wait(msDelay: number): Promise<unknown>;
2
- export declare function hasOwnProperty<P extends PropertyKey>(obj: object, property: P): obj is {
3
- [p in P]: unknown;
4
- };
5
1
  export declare function getHeaders(token: string | null): Headers;
6
2
  export declare function errorMessage(error: unknown): string;
package/dist/utils.js CHANGED
@@ -1,9 +1,3 @@
1
- export function wait(msDelay) {
2
- return new Promise((resolve) => setTimeout(resolve, msDelay));
3
- }
4
- export function hasOwnProperty(obj, property) {
5
- return Object.prototype.hasOwnProperty.call(obj, property);
6
- }
7
1
  export function getHeaders(token) {
8
2
  return token
9
3
  ? new Headers({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kokimoki/app",
3
- "version": "0.0.9",
3
+ "version": "0.1.0",
4
4
  "type": "module",
5
5
  "description": "Kokimoki app",
6
6
  "main": "dist/index.js",
@@ -11,12 +11,14 @@
11
11
  "scripts": {
12
12
  "test": "echo \"Error: no test specified\" && exit 1",
13
13
  "build": "tsc",
14
- "dev": "tsc -w"
14
+ "dev": "tsc -w",
15
+ "docs": "typedoc src/index.ts"
15
16
  },
16
17
  "author": "Loquiz OÜ",
17
18
  "license": "Apache-2.0",
18
19
  "devDependencies": {
19
20
  "@types/ws": "^8.5.5",
21
+ "typedoc": "^0.24.8",
20
22
  "typescript": "^5.1.6"
21
23
  },
22
24
  "dependencies": {