@meshagent/meshagent-react 0.38.2 → 0.38.4
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/CHANGELOG.md +17 -1
- package/README.md +78 -41
- package/dist/cjs/client-toolkits.d.ts +2 -2
- package/dist/cjs/client-toolkits.js +143 -10
- package/dist/cjs/document-connection-scope.d.ts +7 -12
- package/dist/cjs/document-connection-scope.js +88 -82
- package/dist/cjs/index.d.ts +3 -2
- package/dist/cjs/index.js +3 -2
- package/dist/cjs/livekit-client.d.ts +23 -0
- package/dist/cjs/livekit-client.js +66 -0
- package/dist/cjs/livekit-protocol.d.ts +21 -0
- package/dist/cjs/livekit-protocol.js +97 -0
- package/dist/cjs/room-connection-scope.d.ts +16 -16
- package/dist/cjs/room-connection-scope.js +207 -90
- package/dist/cjs/room-participants.d.ts +2 -0
- package/dist/cjs/room-participants.js +46 -0
- package/dist/esm/client-toolkits.d.ts +2 -2
- package/dist/esm/client-toolkits.js +145 -12
- package/dist/esm/document-connection-scope.d.ts +7 -12
- package/dist/esm/document-connection-scope.js +88 -81
- package/dist/esm/index.d.ts +3 -2
- package/dist/esm/index.js +3 -2
- package/dist/esm/livekit-client.d.ts +23 -0
- package/dist/esm/livekit-client.js +61 -0
- package/dist/esm/livekit-protocol.d.ts +21 -0
- package/dist/esm/livekit-protocol.js +60 -0
- package/dist/esm/room-connection-scope.d.ts +16 -16
- package/dist/esm/room-connection-scope.js +205 -89
- package/dist/esm/room-participants.d.ts +2 -0
- package/dist/esm/room-participants.js +43 -0
- package/package.json +2 -2
- package/dist/cjs/chat.d.ts +0 -33
- package/dist/cjs/chat.js +0 -207
- package/dist/cjs/file-upload.d.ts +0 -43
- package/dist/cjs/file-upload.js +0 -168
- package/dist/esm/chat.d.ts +0 -33
- package/dist/esm/chat.js +0 -201
- package/dist/esm/file-upload.d.ts +0 -43
- package/dist/esm/file-upload.js +0 -163
package/dist/cjs/file-upload.js
DELETED
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MeshagentFileUpload = exports.FileUpload = exports.UploadStatus = void 0;
|
|
4
|
-
const meshagent_1 = require("@meshagent/meshagent");
|
|
5
|
-
var UploadStatus;
|
|
6
|
-
(function (UploadStatus) {
|
|
7
|
-
UploadStatus["Initial"] = "initial";
|
|
8
|
-
UploadStatus["Uploading"] = "uploading";
|
|
9
|
-
UploadStatus["Completed"] = "completed";
|
|
10
|
-
UploadStatus["Failed"] = "failed";
|
|
11
|
-
})(UploadStatus || (exports.UploadStatus = UploadStatus = {}));
|
|
12
|
-
class FileUpload extends meshagent_1.EventEmitter {
|
|
13
|
-
constructor(path, size = 0) {
|
|
14
|
-
super();
|
|
15
|
-
Object.defineProperty(this, "path", {
|
|
16
|
-
enumerable: true,
|
|
17
|
-
configurable: true,
|
|
18
|
-
writable: true,
|
|
19
|
-
value: path
|
|
20
|
-
});
|
|
21
|
-
Object.defineProperty(this, "size", {
|
|
22
|
-
enumerable: true,
|
|
23
|
-
configurable: true,
|
|
24
|
-
writable: true,
|
|
25
|
-
value: size
|
|
26
|
-
});
|
|
27
|
-
Object.defineProperty(this, "_status", {
|
|
28
|
-
enumerable: true,
|
|
29
|
-
configurable: true,
|
|
30
|
-
writable: true,
|
|
31
|
-
value: UploadStatus.Initial
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
get status() {
|
|
35
|
-
return this._status;
|
|
36
|
-
}
|
|
37
|
-
set status(value) {
|
|
38
|
-
if (this._status !== value) {
|
|
39
|
-
this._status = value;
|
|
40
|
-
this.emit("status", {
|
|
41
|
-
status: value,
|
|
42
|
-
progress: this.bytesUploaded,
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
get filename() {
|
|
47
|
-
return this.path.split("/").pop() ?? "";
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
exports.FileUpload = FileUpload;
|
|
51
|
-
class MeshagentFileUpload extends FileUpload {
|
|
52
|
-
constructor(room, path, dataStream, size = 0, autoStart = true) {
|
|
53
|
-
super(path, size);
|
|
54
|
-
Object.defineProperty(this, "room", {
|
|
55
|
-
enumerable: true,
|
|
56
|
-
configurable: true,
|
|
57
|
-
writable: true,
|
|
58
|
-
value: room
|
|
59
|
-
});
|
|
60
|
-
Object.defineProperty(this, "dataStream", {
|
|
61
|
-
enumerable: true,
|
|
62
|
-
configurable: true,
|
|
63
|
-
writable: true,
|
|
64
|
-
value: dataStream
|
|
65
|
-
});
|
|
66
|
-
Object.defineProperty(this, "_bytesUploaded", {
|
|
67
|
-
enumerable: true,
|
|
68
|
-
configurable: true,
|
|
69
|
-
writable: true,
|
|
70
|
-
value: 0
|
|
71
|
-
});
|
|
72
|
-
Object.defineProperty(this, "_done", {
|
|
73
|
-
enumerable: true,
|
|
74
|
-
configurable: true,
|
|
75
|
-
writable: true,
|
|
76
|
-
value: void 0
|
|
77
|
-
});
|
|
78
|
-
Object.defineProperty(this, "_resolveDone", {
|
|
79
|
-
enumerable: true,
|
|
80
|
-
configurable: true,
|
|
81
|
-
writable: true,
|
|
82
|
-
value: void 0
|
|
83
|
-
});
|
|
84
|
-
Object.defineProperty(this, "_rejectDone", {
|
|
85
|
-
enumerable: true,
|
|
86
|
-
configurable: true,
|
|
87
|
-
writable: true,
|
|
88
|
-
value: void 0
|
|
89
|
-
});
|
|
90
|
-
Object.defineProperty(this, "_downloadUrl", {
|
|
91
|
-
enumerable: true,
|
|
92
|
-
configurable: true,
|
|
93
|
-
writable: true,
|
|
94
|
-
value: void 0
|
|
95
|
-
});
|
|
96
|
-
Object.defineProperty(this, "_resolveUrl", {
|
|
97
|
-
enumerable: true,
|
|
98
|
-
configurable: true,
|
|
99
|
-
writable: true,
|
|
100
|
-
value: void 0
|
|
101
|
-
});
|
|
102
|
-
Object.defineProperty(this, "_rejectUrl", {
|
|
103
|
-
enumerable: true,
|
|
104
|
-
configurable: true,
|
|
105
|
-
writable: true,
|
|
106
|
-
value: void 0
|
|
107
|
-
});
|
|
108
|
-
this._done = new Promise((res, rej) => {
|
|
109
|
-
this._resolveDone = res;
|
|
110
|
-
this._rejectDone = rej;
|
|
111
|
-
});
|
|
112
|
-
this._downloadUrl = new Promise((res, rej) => {
|
|
113
|
-
this._resolveUrl = res;
|
|
114
|
-
this._rejectUrl = rej;
|
|
115
|
-
});
|
|
116
|
-
if (autoStart)
|
|
117
|
-
this._upload();
|
|
118
|
-
}
|
|
119
|
-
static deferred(room, path, dataStream, size = 0) {
|
|
120
|
-
return new MeshagentFileUpload(room, path, dataStream, size, false);
|
|
121
|
-
}
|
|
122
|
-
get bytesUploaded() {
|
|
123
|
-
return this._bytesUploaded;
|
|
124
|
-
}
|
|
125
|
-
get done() {
|
|
126
|
-
return this._done;
|
|
127
|
-
}
|
|
128
|
-
/** Resolves to the server’s public download URL – like Dart version. */
|
|
129
|
-
get downloadUrl() {
|
|
130
|
-
return this._downloadUrl;
|
|
131
|
-
}
|
|
132
|
-
startUpload() {
|
|
133
|
-
this._upload(); // idempotent guard inside _upload()
|
|
134
|
-
}
|
|
135
|
-
async _upload() {
|
|
136
|
-
if (this.status !== UploadStatus.Initial) {
|
|
137
|
-
throw new Error("upload already started or completed");
|
|
138
|
-
}
|
|
139
|
-
try {
|
|
140
|
-
this.status = UploadStatus.Uploading;
|
|
141
|
-
const self = this;
|
|
142
|
-
async function* trackedChunks() {
|
|
143
|
-
for await (const chunk of self.dataStream) {
|
|
144
|
-
self._bytesUploaded += chunk.length;
|
|
145
|
-
self.emit("progress", {
|
|
146
|
-
status: UploadStatus.Uploading,
|
|
147
|
-
progress: self.bytesUploaded / self.size,
|
|
148
|
-
});
|
|
149
|
-
yield chunk;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
await this.room.storage.uploadStream(this.path, trackedChunks(), {
|
|
153
|
-
overwrite: true,
|
|
154
|
-
size: this.size,
|
|
155
|
-
});
|
|
156
|
-
this._resolveDone();
|
|
157
|
-
this.status = UploadStatus.Completed;
|
|
158
|
-
const urlStr = await this.room.storage.downloadUrl(this.path);
|
|
159
|
-
this._resolveUrl(new URL(urlStr));
|
|
160
|
-
}
|
|
161
|
-
catch (err) {
|
|
162
|
-
this.status = UploadStatus.Failed;
|
|
163
|
-
this._rejectDone(err);
|
|
164
|
-
this._rejectUrl(err);
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
exports.MeshagentFileUpload = MeshagentFileUpload;
|
package/dist/esm/chat.d.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { RoomClient, Element, Participant } from "@meshagent/meshagent";
|
|
2
|
-
import { FileUpload } from "./file-upload";
|
|
3
|
-
export interface ChatMessageArgs {
|
|
4
|
-
id: string;
|
|
5
|
-
text: string;
|
|
6
|
-
attachments?: string[];
|
|
7
|
-
}
|
|
8
|
-
export declare class ChatMessage {
|
|
9
|
-
id: string;
|
|
10
|
-
text: string;
|
|
11
|
-
attachments: string[];
|
|
12
|
-
constructor({ id, text, attachments }: ChatMessageArgs);
|
|
13
|
-
}
|
|
14
|
-
export interface UseMessageChatProps {
|
|
15
|
-
room: RoomClient;
|
|
16
|
-
path: string;
|
|
17
|
-
participants?: Participant[];
|
|
18
|
-
participantNames?: string[];
|
|
19
|
-
includeLocalParticipant?: boolean;
|
|
20
|
-
initialMessage?: ChatMessage;
|
|
21
|
-
}
|
|
22
|
-
export interface UseMessageChatResult {
|
|
23
|
-
messages: Element[];
|
|
24
|
-
sendMessage: (message: ChatMessage) => void;
|
|
25
|
-
selectAttachments: (files: File[]) => void;
|
|
26
|
-
attachments: FileUpload[];
|
|
27
|
-
setAttachments: (attachments: FileUpload[]) => void;
|
|
28
|
-
schemaFileExists: boolean;
|
|
29
|
-
onlineParticipants: Participant[];
|
|
30
|
-
cancelRequest?: () => void;
|
|
31
|
-
}
|
|
32
|
-
export declare function fileToAsyncIterable(file: File): AsyncIterable<Uint8Array>;
|
|
33
|
-
export declare function useChat({ room, path, participants, participantNames, initialMessage, includeLocalParticipant }: UseMessageChatProps): UseMessageChatResult;
|
package/dist/esm/chat.js
DELETED
|
@@ -1,201 +0,0 @@
|
|
|
1
|
-
import { useCallback, useState, useMemo } from "react";
|
|
2
|
-
import { Element, RemoteParticipant } from "@meshagent/meshagent";
|
|
3
|
-
import { MeshagentFileUpload } from "./file-upload";
|
|
4
|
-
import { useRoomParticipants } from "./document-connection-scope";
|
|
5
|
-
import { useDocumentConnection, useDocumentChanged } from "./document-connection-scope";
|
|
6
|
-
export class ChatMessage {
|
|
7
|
-
constructor({ id, text, attachments }) {
|
|
8
|
-
Object.defineProperty(this, "id", {
|
|
9
|
-
enumerable: true,
|
|
10
|
-
configurable: true,
|
|
11
|
-
writable: true,
|
|
12
|
-
value: void 0
|
|
13
|
-
});
|
|
14
|
-
Object.defineProperty(this, "text", {
|
|
15
|
-
enumerable: true,
|
|
16
|
-
configurable: true,
|
|
17
|
-
writable: true,
|
|
18
|
-
value: void 0
|
|
19
|
-
});
|
|
20
|
-
Object.defineProperty(this, "attachments", {
|
|
21
|
-
enumerable: true,
|
|
22
|
-
configurable: true,
|
|
23
|
-
writable: true,
|
|
24
|
-
value: void 0
|
|
25
|
-
});
|
|
26
|
-
this.id = id;
|
|
27
|
-
this.text = text;
|
|
28
|
-
this.attachments = attachments ?? [];
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
function getParticipantName(participant) {
|
|
32
|
-
const name = participant.getAttribute("name");
|
|
33
|
-
return typeof name === "string" && name.length > 0 ? name : null;
|
|
34
|
-
}
|
|
35
|
-
function ensureParticipants(document, localParticipant, includeLocalParticipant, participants, participantNames) {
|
|
36
|
-
const retParticipants = [
|
|
37
|
-
...(participants ?? []),
|
|
38
|
-
...(includeLocalParticipant ? [localParticipant] : []),
|
|
39
|
-
];
|
|
40
|
-
const existing = new Set();
|
|
41
|
-
for (const child of document.root.getChildren()
|
|
42
|
-
.filter((c) => c instanceof Element)) {
|
|
43
|
-
if (child.tagName === "members") {
|
|
44
|
-
for (const member of child.getChildren()
|
|
45
|
-
.filter((c) => c instanceof Element)) {
|
|
46
|
-
const name = getParticipantName(member);
|
|
47
|
-
if (name)
|
|
48
|
-
existing.add(name);
|
|
49
|
-
}
|
|
50
|
-
for (const part of retParticipants) {
|
|
51
|
-
const name = getParticipantName(part);
|
|
52
|
-
if (name && !existing.has(name)) {
|
|
53
|
-
child.createChildElement("member", { name });
|
|
54
|
-
existing.add(name);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
if (participantNames != null) {
|
|
58
|
-
for (const name of participantNames) {
|
|
59
|
-
if (!existing.has(name)) {
|
|
60
|
-
child.createChildElement("member", { name });
|
|
61
|
-
existing.add(name);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
function mapMessages(doc) {
|
|
69
|
-
const children = doc.root.getChildren() || [];
|
|
70
|
-
const thread = children.find((c) => c.tagName === "messages");
|
|
71
|
-
const threadChildren = thread?.getChildren() || [];
|
|
72
|
-
return threadChildren.filter((el) => el.tagName === "message");
|
|
73
|
-
}
|
|
74
|
-
function* getParticipantNames(document) {
|
|
75
|
-
const children = document.root.getChildren() || [];
|
|
76
|
-
const memberNode = children.find((c) => c.tagName === "members");
|
|
77
|
-
const members = memberNode?.getChildren() || [];
|
|
78
|
-
for (const member of members) {
|
|
79
|
-
const name = getParticipantName(member);
|
|
80
|
-
if (name) {
|
|
81
|
-
yield name;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
function* getOnlineParticipants(roomParticipants, participantNames) {
|
|
86
|
-
for (const participantName of participantNames) {
|
|
87
|
-
for (const remoteParticipant of roomParticipants) {
|
|
88
|
-
if (getParticipantName(remoteParticipant) === participantName) {
|
|
89
|
-
yield remoteParticipant;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
const chunkSize = 64 * 1024; // 64 KB
|
|
95
|
-
export function fileToAsyncIterable(file) {
|
|
96
|
-
const hasNativeStream = typeof file.stream === 'function';
|
|
97
|
-
async function* nativeStream() {
|
|
98
|
-
const reader = file.stream().getReader();
|
|
99
|
-
try {
|
|
100
|
-
while (true) {
|
|
101
|
-
const { done, value } = await reader.read();
|
|
102
|
-
if (done)
|
|
103
|
-
break;
|
|
104
|
-
yield value;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
finally {
|
|
108
|
-
reader.releaseLock();
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
async function* sliceStream() {
|
|
112
|
-
let offset = 0;
|
|
113
|
-
while (offset < file.size) {
|
|
114
|
-
const blob = file.slice(offset, offset + chunkSize);
|
|
115
|
-
const buffer = await blob.arrayBuffer();
|
|
116
|
-
yield new Uint8Array(buffer);
|
|
117
|
-
offset += chunkSize;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
return (hasNativeStream ? nativeStream : sliceStream)();
|
|
121
|
-
}
|
|
122
|
-
export function useChat({ room, path, participants, participantNames, initialMessage, includeLocalParticipant }) {
|
|
123
|
-
const { document, schemaFileExists } = useDocumentConnection({
|
|
124
|
-
room,
|
|
125
|
-
path,
|
|
126
|
-
onConnected: (doc) => {
|
|
127
|
-
ensureParticipants(doc, room.localParticipant, includeLocalParticipant ?? true, participants ?? [], participantNames ?? []);
|
|
128
|
-
if (initialMessage) {
|
|
129
|
-
sendMessage(initialMessage);
|
|
130
|
-
}
|
|
131
|
-
},
|
|
132
|
-
onError: (error) => {
|
|
133
|
-
console.error("Failed to connect to document:", error);
|
|
134
|
-
}
|
|
135
|
-
});
|
|
136
|
-
const [messages, setMessages] = useState(() => document ? mapMessages(document) : []);
|
|
137
|
-
const [attachments, setAttachments] = useState([]);
|
|
138
|
-
const [documentMembers, setDocumentMembers] = useState(() => document ? getParticipantNames(document) : []);
|
|
139
|
-
useDocumentChanged({
|
|
140
|
-
document,
|
|
141
|
-
onChanged: (doc) => {
|
|
142
|
-
setMessages(mapMessages(doc));
|
|
143
|
-
setDocumentMembers(getParticipantNames(doc));
|
|
144
|
-
},
|
|
145
|
-
});
|
|
146
|
-
const selectAttachments = useCallback((files) => {
|
|
147
|
-
const attachmentsToUpload = files.map((file) => new MeshagentFileUpload(room, `uploaded-files/${file.name}`, fileToAsyncIterable(file), file.size));
|
|
148
|
-
setAttachments(attachmentsToUpload);
|
|
149
|
-
}, [room]);
|
|
150
|
-
const roomParticipants = useRoomParticipants(room);
|
|
151
|
-
const onlineParticipants = useMemo(() => Array.from(getOnlineParticipants(roomParticipants, documentMembers)), [roomParticipants, documentMembers]);
|
|
152
|
-
const sendMessage = useCallback((message) => {
|
|
153
|
-
const children = document?.root.getChildren() || [];
|
|
154
|
-
const thread = children.find((c) => c.tagName === "messages");
|
|
155
|
-
if (!thread) {
|
|
156
|
-
return;
|
|
157
|
-
}
|
|
158
|
-
const m = thread.createChildElement("message", {
|
|
159
|
-
id: message.id,
|
|
160
|
-
text: message.text,
|
|
161
|
-
created_at: new Date().toISOString(),
|
|
162
|
-
author_name: getParticipantName(room.localParticipant) ?? "",
|
|
163
|
-
author_ref: null,
|
|
164
|
-
});
|
|
165
|
-
for (const path of message.attachments) {
|
|
166
|
-
m.createChildElement("file", { path });
|
|
167
|
-
}
|
|
168
|
-
for (const participant of onlineParticipants) {
|
|
169
|
-
room.messaging.sendMessage({
|
|
170
|
-
to: participant,
|
|
171
|
-
type: "chat",
|
|
172
|
-
message: {
|
|
173
|
-
path,
|
|
174
|
-
text: message.text,
|
|
175
|
-
attachments: message.attachments.map(path => ({ path })),
|
|
176
|
-
},
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
}, [document, path, attachments, onlineParticipants, room]);
|
|
180
|
-
const cancelRequest = useCallback(() => {
|
|
181
|
-
for (const participant of onlineParticipants) {
|
|
182
|
-
if (participant instanceof RemoteParticipant && participant.role === 'agent') {
|
|
183
|
-
room.messaging.sendMessage({
|
|
184
|
-
to: participant,
|
|
185
|
-
type: "cancel",
|
|
186
|
-
message: { path },
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
}, [room, path, onlineParticipants]);
|
|
191
|
-
return {
|
|
192
|
-
messages,
|
|
193
|
-
sendMessage,
|
|
194
|
-
selectAttachments,
|
|
195
|
-
attachments,
|
|
196
|
-
setAttachments,
|
|
197
|
-
schemaFileExists,
|
|
198
|
-
onlineParticipants,
|
|
199
|
-
cancelRequest,
|
|
200
|
-
};
|
|
201
|
-
}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { EventEmitter, RoomClient } from "@meshagent/meshagent";
|
|
2
|
-
export declare enum UploadStatus {
|
|
3
|
-
Initial = "initial",
|
|
4
|
-
Uploading = "uploading",
|
|
5
|
-
Completed = "completed",
|
|
6
|
-
Failed = "failed"
|
|
7
|
-
}
|
|
8
|
-
interface UploadStatusEvent {
|
|
9
|
-
status: UploadStatus;
|
|
10
|
-
progress?: number;
|
|
11
|
-
}
|
|
12
|
-
export declare abstract class FileUpload extends EventEmitter<UploadStatusEvent> {
|
|
13
|
-
path: string;
|
|
14
|
-
size: number;
|
|
15
|
-
protected _status: UploadStatus;
|
|
16
|
-
protected constructor(path: string, size?: number);
|
|
17
|
-
get status(): UploadStatus;
|
|
18
|
-
protected set status(value: UploadStatus);
|
|
19
|
-
abstract get bytesUploaded(): number;
|
|
20
|
-
abstract get done(): Promise<void>;
|
|
21
|
-
get filename(): string;
|
|
22
|
-
abstract startUpload(): void;
|
|
23
|
-
}
|
|
24
|
-
export declare class MeshagentFileUpload extends FileUpload {
|
|
25
|
-
readonly room: RoomClient;
|
|
26
|
-
readonly dataStream: AsyncIterable<Uint8Array>;
|
|
27
|
-
private _bytesUploaded;
|
|
28
|
-
private _done;
|
|
29
|
-
private _resolveDone;
|
|
30
|
-
private _rejectDone;
|
|
31
|
-
private _downloadUrl;
|
|
32
|
-
private _resolveUrl;
|
|
33
|
-
private _rejectUrl;
|
|
34
|
-
constructor(room: RoomClient, path: string, dataStream: AsyncIterable<Uint8Array>, size?: number, autoStart?: boolean);
|
|
35
|
-
static deferred(room: RoomClient, path: string, dataStream: AsyncIterable<Uint8Array>, size?: number): MeshagentFileUpload;
|
|
36
|
-
get bytesUploaded(): number;
|
|
37
|
-
get done(): Promise<void>;
|
|
38
|
-
/** Resolves to the server’s public download URL – like Dart version. */
|
|
39
|
-
get downloadUrl(): Promise<URL>;
|
|
40
|
-
startUpload(): void;
|
|
41
|
-
private _upload;
|
|
42
|
-
}
|
|
43
|
-
export {};
|
package/dist/esm/file-upload.js
DELETED
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
import { EventEmitter } from "@meshagent/meshagent";
|
|
2
|
-
export var UploadStatus;
|
|
3
|
-
(function (UploadStatus) {
|
|
4
|
-
UploadStatus["Initial"] = "initial";
|
|
5
|
-
UploadStatus["Uploading"] = "uploading";
|
|
6
|
-
UploadStatus["Completed"] = "completed";
|
|
7
|
-
UploadStatus["Failed"] = "failed";
|
|
8
|
-
})(UploadStatus || (UploadStatus = {}));
|
|
9
|
-
export class FileUpload extends EventEmitter {
|
|
10
|
-
constructor(path, size = 0) {
|
|
11
|
-
super();
|
|
12
|
-
Object.defineProperty(this, "path", {
|
|
13
|
-
enumerable: true,
|
|
14
|
-
configurable: true,
|
|
15
|
-
writable: true,
|
|
16
|
-
value: path
|
|
17
|
-
});
|
|
18
|
-
Object.defineProperty(this, "size", {
|
|
19
|
-
enumerable: true,
|
|
20
|
-
configurable: true,
|
|
21
|
-
writable: true,
|
|
22
|
-
value: size
|
|
23
|
-
});
|
|
24
|
-
Object.defineProperty(this, "_status", {
|
|
25
|
-
enumerable: true,
|
|
26
|
-
configurable: true,
|
|
27
|
-
writable: true,
|
|
28
|
-
value: UploadStatus.Initial
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
get status() {
|
|
32
|
-
return this._status;
|
|
33
|
-
}
|
|
34
|
-
set status(value) {
|
|
35
|
-
if (this._status !== value) {
|
|
36
|
-
this._status = value;
|
|
37
|
-
this.emit("status", {
|
|
38
|
-
status: value,
|
|
39
|
-
progress: this.bytesUploaded,
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
get filename() {
|
|
44
|
-
return this.path.split("/").pop() ?? "";
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
export class MeshagentFileUpload extends FileUpload {
|
|
48
|
-
constructor(room, path, dataStream, size = 0, autoStart = true) {
|
|
49
|
-
super(path, size);
|
|
50
|
-
Object.defineProperty(this, "room", {
|
|
51
|
-
enumerable: true,
|
|
52
|
-
configurable: true,
|
|
53
|
-
writable: true,
|
|
54
|
-
value: room
|
|
55
|
-
});
|
|
56
|
-
Object.defineProperty(this, "dataStream", {
|
|
57
|
-
enumerable: true,
|
|
58
|
-
configurable: true,
|
|
59
|
-
writable: true,
|
|
60
|
-
value: dataStream
|
|
61
|
-
});
|
|
62
|
-
Object.defineProperty(this, "_bytesUploaded", {
|
|
63
|
-
enumerable: true,
|
|
64
|
-
configurable: true,
|
|
65
|
-
writable: true,
|
|
66
|
-
value: 0
|
|
67
|
-
});
|
|
68
|
-
Object.defineProperty(this, "_done", {
|
|
69
|
-
enumerable: true,
|
|
70
|
-
configurable: true,
|
|
71
|
-
writable: true,
|
|
72
|
-
value: void 0
|
|
73
|
-
});
|
|
74
|
-
Object.defineProperty(this, "_resolveDone", {
|
|
75
|
-
enumerable: true,
|
|
76
|
-
configurable: true,
|
|
77
|
-
writable: true,
|
|
78
|
-
value: void 0
|
|
79
|
-
});
|
|
80
|
-
Object.defineProperty(this, "_rejectDone", {
|
|
81
|
-
enumerable: true,
|
|
82
|
-
configurable: true,
|
|
83
|
-
writable: true,
|
|
84
|
-
value: void 0
|
|
85
|
-
});
|
|
86
|
-
Object.defineProperty(this, "_downloadUrl", {
|
|
87
|
-
enumerable: true,
|
|
88
|
-
configurable: true,
|
|
89
|
-
writable: true,
|
|
90
|
-
value: void 0
|
|
91
|
-
});
|
|
92
|
-
Object.defineProperty(this, "_resolveUrl", {
|
|
93
|
-
enumerable: true,
|
|
94
|
-
configurable: true,
|
|
95
|
-
writable: true,
|
|
96
|
-
value: void 0
|
|
97
|
-
});
|
|
98
|
-
Object.defineProperty(this, "_rejectUrl", {
|
|
99
|
-
enumerable: true,
|
|
100
|
-
configurable: true,
|
|
101
|
-
writable: true,
|
|
102
|
-
value: void 0
|
|
103
|
-
});
|
|
104
|
-
this._done = new Promise((res, rej) => {
|
|
105
|
-
this._resolveDone = res;
|
|
106
|
-
this._rejectDone = rej;
|
|
107
|
-
});
|
|
108
|
-
this._downloadUrl = new Promise((res, rej) => {
|
|
109
|
-
this._resolveUrl = res;
|
|
110
|
-
this._rejectUrl = rej;
|
|
111
|
-
});
|
|
112
|
-
if (autoStart)
|
|
113
|
-
this._upload();
|
|
114
|
-
}
|
|
115
|
-
static deferred(room, path, dataStream, size = 0) {
|
|
116
|
-
return new MeshagentFileUpload(room, path, dataStream, size, false);
|
|
117
|
-
}
|
|
118
|
-
get bytesUploaded() {
|
|
119
|
-
return this._bytesUploaded;
|
|
120
|
-
}
|
|
121
|
-
get done() {
|
|
122
|
-
return this._done;
|
|
123
|
-
}
|
|
124
|
-
/** Resolves to the server’s public download URL – like Dart version. */
|
|
125
|
-
get downloadUrl() {
|
|
126
|
-
return this._downloadUrl;
|
|
127
|
-
}
|
|
128
|
-
startUpload() {
|
|
129
|
-
this._upload(); // idempotent guard inside _upload()
|
|
130
|
-
}
|
|
131
|
-
async _upload() {
|
|
132
|
-
if (this.status !== UploadStatus.Initial) {
|
|
133
|
-
throw new Error("upload already started or completed");
|
|
134
|
-
}
|
|
135
|
-
try {
|
|
136
|
-
this.status = UploadStatus.Uploading;
|
|
137
|
-
const self = this;
|
|
138
|
-
async function* trackedChunks() {
|
|
139
|
-
for await (const chunk of self.dataStream) {
|
|
140
|
-
self._bytesUploaded += chunk.length;
|
|
141
|
-
self.emit("progress", {
|
|
142
|
-
status: UploadStatus.Uploading,
|
|
143
|
-
progress: self.bytesUploaded / self.size,
|
|
144
|
-
});
|
|
145
|
-
yield chunk;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
await this.room.storage.uploadStream(this.path, trackedChunks(), {
|
|
149
|
-
overwrite: true,
|
|
150
|
-
size: this.size,
|
|
151
|
-
});
|
|
152
|
-
this._resolveDone();
|
|
153
|
-
this.status = UploadStatus.Completed;
|
|
154
|
-
const urlStr = await this.room.storage.downloadUrl(this.path);
|
|
155
|
-
this._resolveUrl(new URL(urlStr));
|
|
156
|
-
}
|
|
157
|
-
catch (err) {
|
|
158
|
-
this.status = UploadStatus.Failed;
|
|
159
|
-
this._rejectDone(err);
|
|
160
|
-
this._rejectUrl(err);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
}
|