@matreshka/bff 0.39.0 → 0.40.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/components/file-picker.d.ts +37 -0
- package/dist/components/file-picker.js +78 -0
- package/dist/components/index.d.ts +1 -1
- package/dist/components/index.js +1 -1
- package/dist/platforms/client-platform.d.ts +16 -0
- package/dist/platforms/client-platform.js +24 -0
- package/dist/platforms/index.d.ts +3 -0
- package/dist/platforms/index.js +3 -0
- package/dist/platforms/local-file.d.ts +54 -0
- package/dist/platforms/local-file.js +106 -0
- package/dist/platforms/media-stream-ref.d.ts +29 -0
- package/dist/platforms/media-stream-ref.js +78 -0
- package/dist/platforms/recording.d.ts +58 -0
- package/dist/platforms/recording.js +203 -0
- package/package.json +2 -2
- package/dist/components/file-upload-area.d.ts +0 -79
- package/dist/components/file-upload-area.js +0 -135
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { ServerComponentClass } from "@matreshka/client-bff-protocol/enums/server-component-class";
|
|
2
|
+
import type { FilePickerConfig } from "@matreshka/client-bff-protocol/types/file-picker-config";
|
|
3
|
+
import { Componentable, ComponentTreeNode, MixedWithCallbacksArray, StandaloneComponent } from "../core";
|
|
4
|
+
import { LocalFile } from "../platforms/local-file";
|
|
5
|
+
import { Component, ComponentInitConfig, ComponentProperties } from "./component";
|
|
6
|
+
/**
|
|
7
|
+
* Свойства компонента выбора файла.
|
|
8
|
+
*/
|
|
9
|
+
export type FilePickerProperties = {
|
|
10
|
+
multiple: boolean;
|
|
11
|
+
content: ComponentTreeNode[];
|
|
12
|
+
accept?: string;
|
|
13
|
+
} & ComponentProperties;
|
|
14
|
+
/**
|
|
15
|
+
* Конфигурация инициализации компонента выбора файла.
|
|
16
|
+
*/
|
|
17
|
+
export type FilePickerInitConfig<ComponentType extends Componentable = FilePicker, PropertiesType extends FilePickerProperties = FilePickerProperties> = {
|
|
18
|
+
multiple: boolean;
|
|
19
|
+
content: MixedWithCallbacksArray<ComponentTreeNode>;
|
|
20
|
+
accept?: string;
|
|
21
|
+
onSelect?: (files: LocalFile[]) => void;
|
|
22
|
+
} & ComponentInitConfig<ComponentType, PropertiesType>;
|
|
23
|
+
type DefaultInitConfigType = FilePickerInitConfig<FilePicker>;
|
|
24
|
+
export declare function filePicker(options: Omit<DefaultInitConfigType, "content">, content: DefaultInitConfigType["content"]): FilePicker;
|
|
25
|
+
export declare function filePicker(config: DefaultInitConfigType): FilePicker;
|
|
26
|
+
/**
|
|
27
|
+
* Компонент выбора файлов (без авто-upload).
|
|
28
|
+
*/
|
|
29
|
+
export declare class FilePicker<InitConfigType extends FilePickerInitConfig<any> = DefaultInitConfigType, PropertiesType extends FilePickerProperties = FilePickerProperties> extends Component<InitConfigType, PropertiesType> implements StandaloneComponent {
|
|
30
|
+
constructor(config: InitConfigType);
|
|
31
|
+
standalone(): true;
|
|
32
|
+
protected class(): ServerComponentClass;
|
|
33
|
+
protected initProperties(initValues: InitConfigType): PropertiesType;
|
|
34
|
+
protected serializeRuleOverrides(overrides: Partial<PropertiesType>): Record<string, unknown>;
|
|
35
|
+
serialize(): FilePickerConfig;
|
|
36
|
+
}
|
|
37
|
+
export {};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FilePicker = void 0;
|
|
4
|
+
exports.filePicker = filePicker;
|
|
5
|
+
const server_component_class_1 = require("@matreshka/client-bff-protocol/enums/server-component-class");
|
|
6
|
+
const zod_1 = require("zod");
|
|
7
|
+
const core_1 = require("../core");
|
|
8
|
+
const client_context_1 = require("../core/client-context");
|
|
9
|
+
const local_file_1 = require("../platforms/local-file");
|
|
10
|
+
const component_1 = require("./component");
|
|
11
|
+
function filePicker(arg0, arg1) {
|
|
12
|
+
if (arg1 !== undefined) {
|
|
13
|
+
return new FilePicker({
|
|
14
|
+
...arg0,
|
|
15
|
+
content: arg1,
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
return new FilePicker(arg0);
|
|
19
|
+
}
|
|
20
|
+
const filePayloadSchema = zod_1.z.strictObject({
|
|
21
|
+
id: zod_1.z.string(),
|
|
22
|
+
type: zod_1.z.string(),
|
|
23
|
+
size: zod_1.z.number(),
|
|
24
|
+
name: zod_1.z.string(),
|
|
25
|
+
lastModified: zod_1.z.number(),
|
|
26
|
+
url: zod_1.z.string().refine((val) => val.startsWith("blob:"), {
|
|
27
|
+
message: "url must be a blob",
|
|
28
|
+
}),
|
|
29
|
+
});
|
|
30
|
+
/**
|
|
31
|
+
* Компонент выбора файлов (без авто-upload).
|
|
32
|
+
*/
|
|
33
|
+
class FilePicker extends component_1.Component {
|
|
34
|
+
constructor(config) {
|
|
35
|
+
super(config);
|
|
36
|
+
this.onInteraction("select", ({ payload }) => {
|
|
37
|
+
const schema = zod_1.z.strictObject({
|
|
38
|
+
files: zod_1.z.array(filePayloadSchema).min(1),
|
|
39
|
+
});
|
|
40
|
+
const safePayload = schema.parse(payload);
|
|
41
|
+
const client = (0, client_context_1.currentClient)();
|
|
42
|
+
const files = safePayload.files.map((file) => new local_file_1.LocalFile(client, file));
|
|
43
|
+
this.config.onSelect?.(files);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
standalone() {
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
class() {
|
|
50
|
+
return server_component_class_1.ServerComponentClass.FilePicker;
|
|
51
|
+
}
|
|
52
|
+
initProperties(initValues) {
|
|
53
|
+
return {
|
|
54
|
+
...super.initProperties(initValues),
|
|
55
|
+
content: (0, core_1.calculateComponents)(initValues.content),
|
|
56
|
+
accept: initValues.accept,
|
|
57
|
+
multiple: initValues.multiple,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
serializeRuleOverrides(overrides) {
|
|
61
|
+
const out = super.serializeRuleOverrides(overrides);
|
|
62
|
+
if (overrides.content !== undefined) {
|
|
63
|
+
out.content = (0, core_1.serializeComponentList)(overrides.content);
|
|
64
|
+
}
|
|
65
|
+
return out;
|
|
66
|
+
}
|
|
67
|
+
serialize() {
|
|
68
|
+
const result = super.serialize();
|
|
69
|
+
return {
|
|
70
|
+
...result,
|
|
71
|
+
class: server_component_class_1.ServerComponentClass.FilePicker,
|
|
72
|
+
properties: this.buildPropertiesForClient({
|
|
73
|
+
content: (0, core_1.serializeComponentList)(this.properties.content),
|
|
74
|
+
}),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
exports.FilePicker = FilePicker;
|
|
@@ -19,7 +19,7 @@ export * from "./charts/polar-area-chart";
|
|
|
19
19
|
export * from "./charts/radar-chart";
|
|
20
20
|
export * from "./page";
|
|
21
21
|
export * from "./dialog";
|
|
22
|
-
export * from "./file-
|
|
22
|
+
export * from "./file-picker";
|
|
23
23
|
export * from "./for-each";
|
|
24
24
|
export * from "./inputs/input-component";
|
|
25
25
|
export * from "./inputs/number-input";
|
package/dist/components/index.js
CHANGED
|
@@ -37,7 +37,7 @@ __exportStar(require("./charts/polar-area-chart"), exports);
|
|
|
37
37
|
__exportStar(require("./charts/radar-chart"), exports);
|
|
38
38
|
__exportStar(require("./page"), exports);
|
|
39
39
|
__exportStar(require("./dialog"), exports);
|
|
40
|
-
__exportStar(require("./file-
|
|
40
|
+
__exportStar(require("./file-picker"), exports);
|
|
41
41
|
__exportStar(require("./for-each"), exports);
|
|
42
42
|
__exportStar(require("./inputs/input-component"), exports);
|
|
43
43
|
__exportStar(require("./inputs/number-input"), exports);
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
import type { DisplayMediaStreamOptions, MediaStreamConstraints } from "@matreshka/client-bff-protocol/types/media-stream-constraints";
|
|
1
2
|
import { Observable } from "rxjs";
|
|
2
3
|
import { Audio, MediaConfig, Video } from "./media";
|
|
4
|
+
import { MediaStreamRef } from "./media-stream-ref";
|
|
3
5
|
import { Platform } from "./platform";
|
|
6
|
+
import { Recording, type RecordingConfig } from "./recording";
|
|
4
7
|
/**
|
|
5
8
|
* Абстрактный класс, реализующий любую клиентскую платформу.
|
|
6
9
|
*/
|
|
@@ -15,4 +18,17 @@ export declare abstract class ClientPlatform extends Platform {
|
|
|
15
18
|
* `Video.id` — ключ реестра, который позже сможет забрать видеокомпонент.
|
|
16
19
|
*/
|
|
17
20
|
createVideo(config?: MediaConfig): Observable<Video>;
|
|
21
|
+
/**
|
|
22
|
+
* Запрашивает `MediaStream` через `navigator.mediaDevices.getUserMedia`.
|
|
23
|
+
* Поток живёт на клиенте; BFF получает {@link MediaStreamRef}.
|
|
24
|
+
*/
|
|
25
|
+
getUserMedia(constraints?: MediaStreamConstraints): Observable<MediaStreamRef>;
|
|
26
|
+
/**
|
|
27
|
+
* Запрашивает захват экрана через `navigator.mediaDevices.getDisplayMedia`.
|
|
28
|
+
*/
|
|
29
|
+
getDisplayMedia(options?: DisplayMediaStreamOptions): Observable<MediaStreamRef>;
|
|
30
|
+
/**
|
|
31
|
+
* Создаёт сессию `MediaRecorder` на клиенте поверх одного или нескольких потоков.
|
|
32
|
+
*/
|
|
33
|
+
createRecording(config: RecordingConfig): Observable<Recording>;
|
|
18
34
|
}
|
|
@@ -3,7 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.ClientPlatform = void 0;
|
|
4
4
|
const rxjs_1 = require("rxjs");
|
|
5
5
|
const media_1 = require("./media");
|
|
6
|
+
const media_stream_ref_1 = require("./media-stream-ref");
|
|
6
7
|
const platform_1 = require("./platform");
|
|
8
|
+
const recording_1 = require("./recording");
|
|
7
9
|
/**
|
|
8
10
|
* Абстрактный класс, реализующий любую клиентскую платформу.
|
|
9
11
|
*/
|
|
@@ -24,5 +26,27 @@ class ClientPlatform extends platform_1.Platform {
|
|
|
24
26
|
const video = new media_1.Video(this.client, config);
|
|
25
27
|
return video.open().pipe((0, rxjs_1.map)(() => video));
|
|
26
28
|
}
|
|
29
|
+
/**
|
|
30
|
+
* Запрашивает `MediaStream` через `navigator.mediaDevices.getUserMedia`.
|
|
31
|
+
* Поток живёт на клиенте; BFF получает {@link MediaStreamRef}.
|
|
32
|
+
*/
|
|
33
|
+
getUserMedia(constraints) {
|
|
34
|
+
const stream = new media_stream_ref_1.MediaStreamRef(this.client);
|
|
35
|
+
return stream.openUserMedia(constraints);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Запрашивает захват экрана через `navigator.mediaDevices.getDisplayMedia`.
|
|
39
|
+
*/
|
|
40
|
+
getDisplayMedia(options) {
|
|
41
|
+
const stream = new media_stream_ref_1.MediaStreamRef(this.client);
|
|
42
|
+
return stream.openDisplayMedia(options);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Создаёт сессию `MediaRecorder` на клиенте поверх одного или нескольких потоков.
|
|
46
|
+
*/
|
|
47
|
+
createRecording(config) {
|
|
48
|
+
const recording = new recording_1.Recording(this.client, config);
|
|
49
|
+
return recording.open().pipe((0, rxjs_1.map)(() => recording));
|
|
50
|
+
}
|
|
27
51
|
}
|
|
28
52
|
exports.ClientPlatform = ClientPlatform;
|
|
@@ -2,12 +2,15 @@ export * from "./android-platform";
|
|
|
2
2
|
export * from "./browser-platform";
|
|
3
3
|
export * from "./client-platform";
|
|
4
4
|
export * from "./ios-platform";
|
|
5
|
+
export * from "./local-file";
|
|
5
6
|
export * from "./max-mini-app-platform";
|
|
6
7
|
export * from "./media";
|
|
8
|
+
export * from "./media-stream-ref";
|
|
7
9
|
export * from "./mobile-platform";
|
|
8
10
|
export * from "./platform";
|
|
9
11
|
export * from "./popover-show";
|
|
10
12
|
export * from "./pwa-platform";
|
|
13
|
+
export * from "./recording";
|
|
11
14
|
export * from "./server-platform";
|
|
12
15
|
export * from "./telegram-mini-app-platform";
|
|
13
16
|
export * from "./utils/client-platform";
|
package/dist/platforms/index.js
CHANGED
|
@@ -18,12 +18,15 @@ __exportStar(require("./android-platform"), exports);
|
|
|
18
18
|
__exportStar(require("./browser-platform"), exports);
|
|
19
19
|
__exportStar(require("./client-platform"), exports);
|
|
20
20
|
__exportStar(require("./ios-platform"), exports);
|
|
21
|
+
__exportStar(require("./local-file"), exports);
|
|
21
22
|
__exportStar(require("./max-mini-app-platform"), exports);
|
|
22
23
|
__exportStar(require("./media"), exports);
|
|
24
|
+
__exportStar(require("./media-stream-ref"), exports);
|
|
23
25
|
__exportStar(require("./mobile-platform"), exports);
|
|
24
26
|
__exportStar(require("./platform"), exports);
|
|
25
27
|
__exportStar(require("./popover-show"), exports);
|
|
26
28
|
__exportStar(require("./pwa-platform"), exports);
|
|
29
|
+
__exportStar(require("./recording"), exports);
|
|
27
30
|
__exportStar(require("./server-platform"), exports);
|
|
28
31
|
__exportStar(require("./telegram-mini-app-platform"), exports);
|
|
29
32
|
__exportStar(require("./utils/client-platform"), exports);
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { MediaKind } from "@matreshka/client-bff-protocol/enums/media-kind";
|
|
2
|
+
import { Observable } from "rxjs";
|
|
3
|
+
import type { Client } from "../core/client";
|
|
4
|
+
export { MediaKind };
|
|
5
|
+
export declare class LocalFileUploadProgress {
|
|
6
|
+
readonly progress: number;
|
|
7
|
+
constructor(progress: number);
|
|
8
|
+
}
|
|
9
|
+
export declare class LocalFileUploadResponse {
|
|
10
|
+
readonly response: unknown;
|
|
11
|
+
constructor(response: unknown);
|
|
12
|
+
}
|
|
13
|
+
export type LocalFileUploadEvent = LocalFileUploadProgress | LocalFileUploadResponse;
|
|
14
|
+
export type LocalFileUploadOptions = {
|
|
15
|
+
url: string;
|
|
16
|
+
fieldName?: string;
|
|
17
|
+
headers?: Record<string, string>;
|
|
18
|
+
fields?: Record<string, string>;
|
|
19
|
+
};
|
|
20
|
+
export type LocalFileInit = {
|
|
21
|
+
id: string;
|
|
22
|
+
url: string;
|
|
23
|
+
type: string;
|
|
24
|
+
size: number;
|
|
25
|
+
name: string;
|
|
26
|
+
lastModified: number;
|
|
27
|
+
duration?: number;
|
|
28
|
+
kind?: MediaKind;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Handle на клиентский blob/File: upload и destroy с BFF.
|
|
32
|
+
*/
|
|
33
|
+
export declare class LocalFile {
|
|
34
|
+
private readonly client;
|
|
35
|
+
readonly id: string;
|
|
36
|
+
readonly name: string;
|
|
37
|
+
readonly type: string;
|
|
38
|
+
readonly size: number;
|
|
39
|
+
readonly lastModified: number;
|
|
40
|
+
readonly url: string;
|
|
41
|
+
readonly duration?: number;
|
|
42
|
+
readonly kind?: MediaKind;
|
|
43
|
+
private destroyed;
|
|
44
|
+
constructor(client: Client, init: LocalFileInit);
|
|
45
|
+
/**
|
|
46
|
+
* POST multipart на endpoint. Эмитит progress, затем response, затем complete.
|
|
47
|
+
* Отписка всех observers abort'ит загрузку на клиенте.
|
|
48
|
+
*/
|
|
49
|
+
upload(options: LocalFileUploadOptions): Observable<LocalFileUploadEvent>;
|
|
50
|
+
/** Освобождает blob на клиенте. */
|
|
51
|
+
destroy(): Observable<void>;
|
|
52
|
+
private assertAlive;
|
|
53
|
+
private markDestroyed;
|
|
54
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LocalFile = exports.LocalFileUploadResponse = exports.LocalFileUploadProgress = exports.MediaKind = void 0;
|
|
4
|
+
const media_kind_1 = require("@matreshka/client-bff-protocol/enums/media-kind");
|
|
5
|
+
Object.defineProperty(exports, "MediaKind", { enumerable: true, get: function () { return media_kind_1.MediaKind; } });
|
|
6
|
+
const local_file_destroy_message_1 = require("@matreshka/client-bff-protocol/messages/bff-to-client/platforms/local-file-destroy-message");
|
|
7
|
+
const local_file_upload_abort_message_1 = require("@matreshka/client-bff-protocol/messages/bff-to-client/platforms/local-file-upload-abort-message");
|
|
8
|
+
const local_file_upload_message_1 = require("@matreshka/client-bff-protocol/messages/bff-to-client/platforms/local-file-upload-message");
|
|
9
|
+
const local_file_upload_progress_message_1 = require("@matreshka/client-bff-protocol/messages/client-to-bff/platforms/local-file-upload-progress-message");
|
|
10
|
+
const local_file_upload_success_message_1 = require("@matreshka/client-bff-protocol/messages/client-to-bff/platforms/local-file-upload-success-message");
|
|
11
|
+
const rxjs_1 = require("rxjs");
|
|
12
|
+
const zod_1 = require("zod");
|
|
13
|
+
const targeted_client_platform_action_1 = require("./utils/targeted-client-platform-action");
|
|
14
|
+
const targeted_client_platform_stream_1 = require("./utils/targeted-client-platform-stream");
|
|
15
|
+
class LocalFileUploadProgress {
|
|
16
|
+
constructor(progress) {
|
|
17
|
+
this.progress = progress;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.LocalFileUploadProgress = LocalFileUploadProgress;
|
|
21
|
+
class LocalFileUploadResponse {
|
|
22
|
+
constructor(response) {
|
|
23
|
+
this.response = response;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
exports.LocalFileUploadResponse = LocalFileUploadResponse;
|
|
27
|
+
const localFileInitSchema = zod_1.z.strictObject({
|
|
28
|
+
id: zod_1.z.string(),
|
|
29
|
+
url: zod_1.z.string().refine((val) => val.startsWith("blob:"), {
|
|
30
|
+
message: "url must be a blob",
|
|
31
|
+
}),
|
|
32
|
+
type: zod_1.z.string(),
|
|
33
|
+
size: zod_1.z.number(),
|
|
34
|
+
name: zod_1.z.string(),
|
|
35
|
+
lastModified: zod_1.z.number(),
|
|
36
|
+
duration: zod_1.z.number().optional(),
|
|
37
|
+
kind: zod_1.z.enum(media_kind_1.MediaKind).optional(),
|
|
38
|
+
});
|
|
39
|
+
/**
|
|
40
|
+
* Handle на клиентский blob/File: upload и destroy с BFF.
|
|
41
|
+
*/
|
|
42
|
+
class LocalFile {
|
|
43
|
+
constructor(client, init) {
|
|
44
|
+
this.client = client;
|
|
45
|
+
this.destroyed = false;
|
|
46
|
+
const safe = localFileInitSchema.parse(init);
|
|
47
|
+
this.id = safe.id;
|
|
48
|
+
this.name = safe.name;
|
|
49
|
+
this.type = safe.type;
|
|
50
|
+
this.size = safe.size;
|
|
51
|
+
this.lastModified = safe.lastModified;
|
|
52
|
+
this.url = safe.url;
|
|
53
|
+
this.duration = safe.duration;
|
|
54
|
+
this.kind = safe.kind;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* POST multipart на endpoint. Эмитит progress, затем response, затем complete.
|
|
58
|
+
* Отписка всех observers abort'ит загрузку на клиенте.
|
|
59
|
+
*/
|
|
60
|
+
upload(options) {
|
|
61
|
+
this.assertAlive();
|
|
62
|
+
return (0, targeted_client_platform_stream_1.targetedClientPlatformStream)(this.client, {
|
|
63
|
+
outgoingMessage: (scope) => new local_file_upload_message_1.LocalFileUploadMessage(scope, {
|
|
64
|
+
fileId: this.id,
|
|
65
|
+
url: options.url,
|
|
66
|
+
fieldName: options.fieldName,
|
|
67
|
+
headers: options.headers,
|
|
68
|
+
fields: options.fields,
|
|
69
|
+
}),
|
|
70
|
+
parseItem: (unsafeMessage) => {
|
|
71
|
+
if (unsafeMessage instanceof local_file_upload_progress_message_1.LocalFileUploadProgressMessage) {
|
|
72
|
+
const progress = zod_1.z
|
|
73
|
+
.number()
|
|
74
|
+
.min(0)
|
|
75
|
+
.max(100)
|
|
76
|
+
.parse(unsafeMessage.payload.progress);
|
|
77
|
+
return new LocalFileUploadProgress(progress);
|
|
78
|
+
}
|
|
79
|
+
if (unsafeMessage instanceof local_file_upload_success_message_1.LocalFileUploadSuccessMessage) {
|
|
80
|
+
return new LocalFileUploadResponse(unsafeMessage.payload.response);
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
onTeardown: (scope, client) => {
|
|
84
|
+
client.outcomingMessage$.next(new local_file_upload_abort_message_1.LocalFileUploadAbortMessage(scope));
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
/** Освобождает blob на клиенте. */
|
|
89
|
+
destroy() {
|
|
90
|
+
if (this.destroyed) {
|
|
91
|
+
return (0, rxjs_1.of)(undefined);
|
|
92
|
+
}
|
|
93
|
+
return (0, targeted_client_platform_action_1.targetedClientPlatformAction)(this.client, {
|
|
94
|
+
outgoingMessage: (scope) => new local_file_destroy_message_1.LocalFileDestroyMessage(scope, { fileId: this.id }),
|
|
95
|
+
}).pipe((0, rxjs_1.tap)(() => this.markDestroyed()));
|
|
96
|
+
}
|
|
97
|
+
assertAlive() {
|
|
98
|
+
if (this.destroyed) {
|
|
99
|
+
throw new Error(`LocalFile ${this.id} has been destroyed`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
markDestroyed() {
|
|
103
|
+
this.destroyed = true;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
exports.LocalFile = LocalFile;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { MediaKind } from "@matreshka/client-bff-protocol/enums/media-kind";
|
|
2
|
+
import type { DisplayMediaStreamOptions, MediaStreamConstraints } from "@matreshka/client-bff-protocol/types/media-stream-constraints";
|
|
3
|
+
import { Observable } from "rxjs";
|
|
4
|
+
import type { Client } from "../core/client";
|
|
5
|
+
export type { DisplayMediaStreamOptions, MediaStreamConstraints };
|
|
6
|
+
export type MediaStreamTrackInfo = {
|
|
7
|
+
kind: MediaKind;
|
|
8
|
+
label?: string;
|
|
9
|
+
settings?: Record<string, unknown>;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Ссылка на `MediaStream` на клиенте. Сам поток не сериализуется.
|
|
13
|
+
*/
|
|
14
|
+
export declare class MediaStreamRef {
|
|
15
|
+
private readonly client;
|
|
16
|
+
readonly id: string;
|
|
17
|
+
private stopped;
|
|
18
|
+
private trackInfos;
|
|
19
|
+
constructor(client: Client);
|
|
20
|
+
get tracks(): readonly MediaStreamTrackInfo[];
|
|
21
|
+
/** Останавливает треки на клиенте и снимает поток из реестра. */
|
|
22
|
+
stop(): Observable<void>;
|
|
23
|
+
/** @internal */
|
|
24
|
+
openUserMedia(constraints?: MediaStreamConstraints): Observable<this>;
|
|
25
|
+
/** @internal */
|
|
26
|
+
openDisplayMedia(options?: DisplayMediaStreamOptions): Observable<this>;
|
|
27
|
+
private open;
|
|
28
|
+
private markStopped;
|
|
29
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MediaStreamRef = void 0;
|
|
4
|
+
const media_kind_1 = require("@matreshka/client-bff-protocol/enums/media-kind");
|
|
5
|
+
const platform_get_display_media_message_1 = require("@matreshka/client-bff-protocol/messages/bff-to-client/platforms/platform-get-display-media-message");
|
|
6
|
+
const platform_get_user_media_message_1 = require("@matreshka/client-bff-protocol/messages/bff-to-client/platforms/platform-get-user-media-message");
|
|
7
|
+
const platform_media_stream_stop_message_1 = require("@matreshka/client-bff-protocol/messages/bff-to-client/platforms/platform-media-stream-stop-message");
|
|
8
|
+
const platform_media_stream_success_message_1 = require("@matreshka/client-bff-protocol/messages/client-to-bff/platforms/platform-media-stream-success-message");
|
|
9
|
+
const rxjs_1 = require("rxjs");
|
|
10
|
+
const zod_1 = require("zod");
|
|
11
|
+
const targeted_client_platform_action_1 = require("./utils/targeted-client-platform-action");
|
|
12
|
+
const trackInfoSchema = zod_1.z.strictObject({
|
|
13
|
+
kind: zod_1.z.enum(media_kind_1.MediaKind),
|
|
14
|
+
label: zod_1.z.string().optional(),
|
|
15
|
+
settings: zod_1.z.record(zod_1.z.string(), zod_1.z.unknown()).optional(),
|
|
16
|
+
});
|
|
17
|
+
const mediaStreamSuccessSchema = zod_1.z.strictObject({
|
|
18
|
+
streamId: zod_1.z.string(),
|
|
19
|
+
tracks: zod_1.z.array(trackInfoSchema),
|
|
20
|
+
});
|
|
21
|
+
/**
|
|
22
|
+
* Ссылка на `MediaStream` на клиенте. Сам поток не сериализуется.
|
|
23
|
+
*/
|
|
24
|
+
class MediaStreamRef {
|
|
25
|
+
constructor(client) {
|
|
26
|
+
this.client = client;
|
|
27
|
+
this.id = "media-stream-" + crypto.randomUUID();
|
|
28
|
+
this.stopped = false;
|
|
29
|
+
this.trackInfos = [];
|
|
30
|
+
}
|
|
31
|
+
get tracks() {
|
|
32
|
+
return this.trackInfos;
|
|
33
|
+
}
|
|
34
|
+
/** Останавливает треки на клиенте и снимает поток из реестра. */
|
|
35
|
+
stop() {
|
|
36
|
+
if (this.stopped) {
|
|
37
|
+
return (0, rxjs_1.of)(undefined);
|
|
38
|
+
}
|
|
39
|
+
return (0, targeted_client_platform_action_1.targetedClientPlatformAction)(this.client, {
|
|
40
|
+
outgoingMessage: (scope) => new platform_media_stream_stop_message_1.PlatformMediaStreamStopMessage(scope, { streamId: this.id }),
|
|
41
|
+
}).pipe((0, rxjs_1.tap)(() => this.markStopped()));
|
|
42
|
+
}
|
|
43
|
+
/** @internal */
|
|
44
|
+
openUserMedia(constraints) {
|
|
45
|
+
return this.open((scope) => new platform_get_user_media_message_1.PlatformGetUserMediaMessage(scope, {
|
|
46
|
+
streamId: this.id,
|
|
47
|
+
constraints,
|
|
48
|
+
}));
|
|
49
|
+
}
|
|
50
|
+
/** @internal */
|
|
51
|
+
openDisplayMedia(options) {
|
|
52
|
+
return this.open((scope) => new platform_get_display_media_message_1.PlatformGetDisplayMediaMessage(scope, {
|
|
53
|
+
streamId: this.id,
|
|
54
|
+
options,
|
|
55
|
+
}));
|
|
56
|
+
}
|
|
57
|
+
open(outgoingMessage) {
|
|
58
|
+
return (0, targeted_client_platform_action_1.targetedClientPlatformAction)(this.client, {
|
|
59
|
+
outgoingMessage,
|
|
60
|
+
parseSuccess: (unsafeMessage) => {
|
|
61
|
+
if (!(unsafeMessage instanceof platform_media_stream_success_message_1.PlatformMediaStreamSuccessMessage)) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
const payload = mediaStreamSuccessSchema.parse(unsafeMessage.payload);
|
|
65
|
+
if (payload.streamId !== this.id) {
|
|
66
|
+
throw new Error(`Unexpected streamId ${payload.streamId}, expected ${this.id}`);
|
|
67
|
+
}
|
|
68
|
+
this.trackInfos = payload.tracks;
|
|
69
|
+
return this;
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
markStopped() {
|
|
74
|
+
this.stopped = true;
|
|
75
|
+
this.trackInfos = [];
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
exports.MediaStreamRef = MediaStreamRef;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { MediaKind } from "@matreshka/client-bff-protocol/enums/media-kind";
|
|
2
|
+
import { Observable } from "rxjs";
|
|
3
|
+
import { ServerAction, type ServerActionHandler } from "../components/actions/server-action";
|
|
4
|
+
import { LocalAction } from "../core/actions/local-action";
|
|
5
|
+
import type { Client } from "../core/client";
|
|
6
|
+
import type { Componentable } from "../core/types/componentable";
|
|
7
|
+
import { LocalFile } from "./local-file";
|
|
8
|
+
import type { MediaStreamRef } from "./media-stream-ref";
|
|
9
|
+
export { MediaKind };
|
|
10
|
+
export type RecordingProgressPayload = {
|
|
11
|
+
duration: number;
|
|
12
|
+
};
|
|
13
|
+
export type RecordingErrorPayload = {
|
|
14
|
+
message: string;
|
|
15
|
+
};
|
|
16
|
+
/** Одно действие или массив локальных и серверных, как у Media. */
|
|
17
|
+
export type RecordingAction<Payload> = ServerAction<Componentable, Payload> | ServerActionHandler<Componentable, Payload> | LocalAction;
|
|
18
|
+
export type RecordingActionList<Payload> = RecordingAction<Payload> | RecordingAction<Payload>[];
|
|
19
|
+
export type RecordingEventConfig = {
|
|
20
|
+
onStart?: RecordingActionList<RecordingProgressPayload>;
|
|
21
|
+
onPause?: RecordingActionList<RecordingProgressPayload>;
|
|
22
|
+
onResume?: RecordingActionList<RecordingProgressPayload>;
|
|
23
|
+
onStop?: RecordingActionList<RecordingProgressPayload>;
|
|
24
|
+
onError?: RecordingActionList<RecordingErrorPayload>;
|
|
25
|
+
onTimeUpdate?: RecordingActionList<RecordingProgressPayload>;
|
|
26
|
+
};
|
|
27
|
+
export type RecordingConfig = RecordingEventConfig & {
|
|
28
|
+
streams: MediaStreamRef | MediaStreamRef[];
|
|
29
|
+
mimeType?: string;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Сессия `MediaRecorder` на клиенте поверх одного или нескольких `MediaStreamRef`.
|
|
33
|
+
*/
|
|
34
|
+
export declare class Recording {
|
|
35
|
+
private readonly client;
|
|
36
|
+
readonly id: string;
|
|
37
|
+
private readonly handlers;
|
|
38
|
+
private readonly streamIds;
|
|
39
|
+
private readonly mimeType?;
|
|
40
|
+
private sessionSubscription?;
|
|
41
|
+
private clientDestroySubscription?;
|
|
42
|
+
private ended;
|
|
43
|
+
constructor(client: Client, config: RecordingConfig);
|
|
44
|
+
start(): Observable<void>;
|
|
45
|
+
pause(): Observable<void>;
|
|
46
|
+
resume(): Observable<void>;
|
|
47
|
+
stop(): Observable<LocalFile>;
|
|
48
|
+
destroy(): Observable<void>;
|
|
49
|
+
/** @internal */
|
|
50
|
+
open(): Observable<void>;
|
|
51
|
+
private send;
|
|
52
|
+
private bindEvents;
|
|
53
|
+
private parseAction;
|
|
54
|
+
private serializeInteractions;
|
|
55
|
+
private startSession;
|
|
56
|
+
private emit;
|
|
57
|
+
private endSession;
|
|
58
|
+
}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Recording = exports.MediaKind = void 0;
|
|
4
|
+
const media_kind_1 = require("@matreshka/client-bff-protocol/enums/media-kind");
|
|
5
|
+
Object.defineProperty(exports, "MediaKind", { enumerable: true, get: function () { return media_kind_1.MediaKind; } });
|
|
6
|
+
const recording_create_message_1 = require("@matreshka/client-bff-protocol/messages/bff-to-client/platforms/recording-create-message");
|
|
7
|
+
const recording_destroy_message_1 = require("@matreshka/client-bff-protocol/messages/bff-to-client/platforms/recording-destroy-message");
|
|
8
|
+
const recording_pause_message_1 = require("@matreshka/client-bff-protocol/messages/bff-to-client/platforms/recording-pause-message");
|
|
9
|
+
const recording_resume_message_1 = require("@matreshka/client-bff-protocol/messages/bff-to-client/platforms/recording-resume-message");
|
|
10
|
+
const recording_start_message_1 = require("@matreshka/client-bff-protocol/messages/bff-to-client/platforms/recording-start-message");
|
|
11
|
+
const recording_stop_message_1 = require("@matreshka/client-bff-protocol/messages/bff-to-client/platforms/recording-stop-message");
|
|
12
|
+
const recording_event_messages_1 = require("@matreshka/client-bff-protocol/messages/client-to-bff/platforms/recording-event-messages");
|
|
13
|
+
const recording_stop_success_message_1 = require("@matreshka/client-bff-protocol/messages/client-to-bff/platforms/recording-stop-success-message");
|
|
14
|
+
const rxjs_1 = require("rxjs");
|
|
15
|
+
const zod_1 = require("zod");
|
|
16
|
+
const server_action_1 = require("../components/actions/server-action");
|
|
17
|
+
const action_1 = require("../core/actions/action");
|
|
18
|
+
const local_action_1 = require("../core/actions/local-action");
|
|
19
|
+
const client_context_1 = require("../core/client-context");
|
|
20
|
+
const incoming_message_observer_1 = require("../core/utils/incoming-message-observer");
|
|
21
|
+
const local_file_1 = require("./local-file");
|
|
22
|
+
const targeted_client_platform_action_1 = require("./utils/targeted-client-platform-action");
|
|
23
|
+
function isRecordingIncomingEvent(message) {
|
|
24
|
+
return (message instanceof recording_event_messages_1.RecordingStartEventMessage ||
|
|
25
|
+
message instanceof recording_event_messages_1.RecordingPauseEventMessage ||
|
|
26
|
+
message instanceof recording_event_messages_1.RecordingResumeEventMessage ||
|
|
27
|
+
message instanceof recording_event_messages_1.RecordingStopEventMessage ||
|
|
28
|
+
message instanceof recording_event_messages_1.RecordingTimeUpdateEventMessage ||
|
|
29
|
+
message instanceof recording_event_messages_1.RecordingErrorEventMessage);
|
|
30
|
+
}
|
|
31
|
+
const recordingHandlersSchema = zod_1.z.array(zod_1.z.number().int().nonnegative());
|
|
32
|
+
const recordingProgressEventSchema = zod_1.z.strictObject({
|
|
33
|
+
duration: zod_1.z.number(),
|
|
34
|
+
handlers: recordingHandlersSchema,
|
|
35
|
+
});
|
|
36
|
+
const recordingErrorEventSchema = zod_1.z.strictObject({
|
|
37
|
+
message: zod_1.z.string(),
|
|
38
|
+
handlers: recordingHandlersSchema,
|
|
39
|
+
});
|
|
40
|
+
function recordingEventSchema(message) {
|
|
41
|
+
if (message instanceof recording_event_messages_1.RecordingErrorEventMessage) {
|
|
42
|
+
return recordingErrorEventSchema;
|
|
43
|
+
}
|
|
44
|
+
return recordingProgressEventSchema;
|
|
45
|
+
}
|
|
46
|
+
function recordingEventName(message) {
|
|
47
|
+
if (message instanceof recording_event_messages_1.RecordingStartEventMessage) {
|
|
48
|
+
return "start";
|
|
49
|
+
}
|
|
50
|
+
if (message instanceof recording_event_messages_1.RecordingPauseEventMessage) {
|
|
51
|
+
return "pause";
|
|
52
|
+
}
|
|
53
|
+
if (message instanceof recording_event_messages_1.RecordingResumeEventMessage) {
|
|
54
|
+
return "resume";
|
|
55
|
+
}
|
|
56
|
+
if (message instanceof recording_event_messages_1.RecordingStopEventMessage) {
|
|
57
|
+
return "stop";
|
|
58
|
+
}
|
|
59
|
+
if (message instanceof recording_event_messages_1.RecordingTimeUpdateEventMessage) {
|
|
60
|
+
return "timeupdate";
|
|
61
|
+
}
|
|
62
|
+
return "error";
|
|
63
|
+
}
|
|
64
|
+
const EVENT_FIELDS = {
|
|
65
|
+
onStart: "start",
|
|
66
|
+
onPause: "pause",
|
|
67
|
+
onResume: "resume",
|
|
68
|
+
onStop: "stop",
|
|
69
|
+
onError: "error",
|
|
70
|
+
onTimeUpdate: "timeupdate",
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Сессия `MediaRecorder` на клиенте поверх одного или нескольких `MediaStreamRef`.
|
|
74
|
+
*/
|
|
75
|
+
class Recording {
|
|
76
|
+
constructor(client, config) {
|
|
77
|
+
this.client = client;
|
|
78
|
+
this.id = "recording-" + crypto.randomUUID();
|
|
79
|
+
this.handlers = new Map();
|
|
80
|
+
this.ended = false;
|
|
81
|
+
const streams = Array.isArray(config.streams)
|
|
82
|
+
? config.streams
|
|
83
|
+
: [config.streams];
|
|
84
|
+
if (streams.length === 0) {
|
|
85
|
+
throw new Error("Recording requires at least one MediaStreamRef");
|
|
86
|
+
}
|
|
87
|
+
this.streamIds = streams.map((stream) => stream.id);
|
|
88
|
+
this.mimeType = config.mimeType;
|
|
89
|
+
this.bindEvents(config);
|
|
90
|
+
}
|
|
91
|
+
start() {
|
|
92
|
+
return this.send((scope) => new recording_start_message_1.RecordingStartMessage(scope, { recordingId: this.id }));
|
|
93
|
+
}
|
|
94
|
+
pause() {
|
|
95
|
+
return this.send((scope) => new recording_pause_message_1.RecordingPauseMessage(scope, { recordingId: this.id }));
|
|
96
|
+
}
|
|
97
|
+
resume() {
|
|
98
|
+
return this.send((scope) => new recording_resume_message_1.RecordingResumeMessage(scope, { recordingId: this.id }));
|
|
99
|
+
}
|
|
100
|
+
stop() {
|
|
101
|
+
return (0, targeted_client_platform_action_1.targetedClientPlatformAction)(this.client, {
|
|
102
|
+
outgoingMessage: (scope) => new recording_stop_message_1.RecordingStopMessage(scope, { recordingId: this.id }),
|
|
103
|
+
parseSuccess: (unsafeMessage) => {
|
|
104
|
+
if (!(unsafeMessage instanceof recording_stop_success_message_1.RecordingStopSuccessMessage)) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
return new local_file_1.LocalFile(this.client, unsafeMessage.payload);
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
destroy() {
|
|
112
|
+
if (this.ended) {
|
|
113
|
+
return (0, rxjs_1.of)(undefined);
|
|
114
|
+
}
|
|
115
|
+
return this.send((scope) => new recording_destroy_message_1.RecordingDestroyMessage(scope, { recordingId: this.id })).pipe((0, rxjs_1.tap)(() => this.endSession()));
|
|
116
|
+
}
|
|
117
|
+
/** @internal */
|
|
118
|
+
open() {
|
|
119
|
+
return (0, targeted_client_platform_action_1.targetedClientPlatformAction)(this.client, {
|
|
120
|
+
outgoingMessage: (scope) => (0, client_context_1.runWithClient)(this.client, () => new recording_create_message_1.RecordingCreateMessage(scope, {
|
|
121
|
+
recordingId: this.id,
|
|
122
|
+
streamIds: this.streamIds,
|
|
123
|
+
mimeType: this.mimeType,
|
|
124
|
+
interactions: this.serializeInteractions(),
|
|
125
|
+
})),
|
|
126
|
+
}).pipe((0, rxjs_1.tap)(() => this.startSession()));
|
|
127
|
+
}
|
|
128
|
+
send(outgoingMessage) {
|
|
129
|
+
return (0, targeted_client_platform_action_1.targetedClientPlatformAction)(this.client, {
|
|
130
|
+
outgoingMessage,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
bindEvents(config) {
|
|
134
|
+
Object.keys(EVENT_FIELDS).forEach((field) => {
|
|
135
|
+
const actions = config[field];
|
|
136
|
+
if (actions === undefined) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const list = (Array.isArray(actions) ? actions : [actions]).map((action) => this.parseAction(action));
|
|
140
|
+
this.handlers.set(EVENT_FIELDS[field], list);
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
parseAction(action) {
|
|
144
|
+
if (action instanceof local_action_1.LocalAction || action instanceof server_action_1.ServerAction) {
|
|
145
|
+
return action;
|
|
146
|
+
}
|
|
147
|
+
if (action instanceof action_1.Action) {
|
|
148
|
+
throw new Error("Recording events accept LocalAction, ServerAction, or a server function");
|
|
149
|
+
}
|
|
150
|
+
return new server_action_1.ServerAction(action);
|
|
151
|
+
}
|
|
152
|
+
serializeInteractions() {
|
|
153
|
+
const interactions = {};
|
|
154
|
+
this.handlers.forEach((actions, event) => {
|
|
155
|
+
interactions[event] = actions.map((action) => action.toJSON());
|
|
156
|
+
});
|
|
157
|
+
return interactions;
|
|
158
|
+
}
|
|
159
|
+
startSession() {
|
|
160
|
+
if (this.sessionSubscription || this.ended) {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
this.sessionSubscription = this.client.incomingMessage$
|
|
164
|
+
.pipe((0, rxjs_1.filter)((message) => isRecordingIncomingEvent(message) && message.target === this.id))
|
|
165
|
+
.subscribe((0, incoming_message_observer_1.incomingMessageObserver)(this.client, (message) => {
|
|
166
|
+
const parsed = recordingEventSchema(message).safeParse(message.payload);
|
|
167
|
+
if (!parsed.success) {
|
|
168
|
+
this.client.error$.next(`Invalid recording event payload: ${message.type}`);
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
const { handlers, ...payload } = parsed.data;
|
|
172
|
+
this.emit(recordingEventName(message), payload, handlers);
|
|
173
|
+
}));
|
|
174
|
+
this.clientDestroySubscription = this.client.destroy$
|
|
175
|
+
.pipe((0, rxjs_1.take)(1))
|
|
176
|
+
.subscribe(() => this.endSession());
|
|
177
|
+
}
|
|
178
|
+
emit(event, payload, handlers) {
|
|
179
|
+
const actions = this.handlers.get(event);
|
|
180
|
+
if (!actions) {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
for (const index of handlers) {
|
|
184
|
+
const action = actions[index];
|
|
185
|
+
if (!(action instanceof server_action_1.ServerAction)) {
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
action.execute({
|
|
189
|
+
payload,
|
|
190
|
+
instance: { client: this.client },
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
endSession() {
|
|
195
|
+
if (this.ended) {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
this.ended = true;
|
|
199
|
+
this.sessionSubscription?.unsubscribe();
|
|
200
|
+
this.clientDestroySubscription?.unsubscribe();
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
exports.Recording = Recording;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@matreshka/bff",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.40.0",
|
|
4
4
|
"description": "Matreshka BFF: server-driven UI runtime — routing, pages, context, component DSL, and platform APIs",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -62,7 +62,7 @@
|
|
|
62
62
|
"vitest": "^4.1.0"
|
|
63
63
|
},
|
|
64
64
|
"dependencies": {
|
|
65
|
-
"@matreshka/client-bff-protocol": "^0.
|
|
65
|
+
"@matreshka/client-bff-protocol": "^0.8.0",
|
|
66
66
|
"@matreshka/gateway-bff-protocol": "^0.1.0",
|
|
67
67
|
"@matreshka/shared": "^0.26.0",
|
|
68
68
|
"@simplewebauthn/server": "^13.1.1",
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import { ServerComponentClass } from "@matreshka/client-bff-protocol/enums/server-component-class";
|
|
2
|
-
import type { FileUploadAreaConfig } from "@matreshka/client-bff-protocol/types/file-upload-area-config";
|
|
3
|
-
import { Componentable, ComponentTreeNode, MixedWithCallbacksArray, StandaloneComponent } from "../core";
|
|
4
|
-
import { Component, ComponentInitConfig, ComponentProperties } from "./component";
|
|
5
|
-
/**
|
|
6
|
-
* Свойства компонента загрузки файла.
|
|
7
|
-
*
|
|
8
|
-
* @property upload Конфигурация загрузки.
|
|
9
|
-
* @property multiple Можно ли загружать несколько файлов.
|
|
10
|
-
* @property label Метка для поля загрузки.
|
|
11
|
-
* @property url Ссылка для загрузки файлов (POST запрос).
|
|
12
|
-
* @property accept MIME типы файлов, а также расширения файлов, которые можно загружать.
|
|
13
|
-
*/
|
|
14
|
-
export type FileUploadAreaProperties = {
|
|
15
|
-
multiple: boolean;
|
|
16
|
-
content: ComponentTreeNode[];
|
|
17
|
-
url: string;
|
|
18
|
-
accept?: string;
|
|
19
|
-
} & ComponentProperties;
|
|
20
|
-
/**
|
|
21
|
-
* Конфигурация инициализации компонента загрузки файла.
|
|
22
|
-
*
|
|
23
|
-
* @property upload Настройки загрузки
|
|
24
|
-
*/
|
|
25
|
-
export type FileUploadAreaInitConfig<ComponentType extends Componentable = FileUploadArea, PropertiesType extends FileUploadAreaProperties = FileUploadAreaProperties> = {
|
|
26
|
-
multiple: boolean;
|
|
27
|
-
content: MixedWithCallbacksArray<ComponentTreeNode>;
|
|
28
|
-
url: string;
|
|
29
|
-
errorKey?: string;
|
|
30
|
-
accept?: string;
|
|
31
|
-
onStart?: (file: FileInfo) => void;
|
|
32
|
-
onComplete?: (config: {
|
|
33
|
-
file: FileInfo;
|
|
34
|
-
response: unknown;
|
|
35
|
-
}) => void;
|
|
36
|
-
onError?: (config: {
|
|
37
|
-
file: FileInfo;
|
|
38
|
-
response: unknown;
|
|
39
|
-
code: number;
|
|
40
|
-
}) => void;
|
|
41
|
-
onProgress?: (config: {
|
|
42
|
-
file: FileInfo;
|
|
43
|
-
progress: number;
|
|
44
|
-
}) => void;
|
|
45
|
-
} & ComponentInitConfig<ComponentType, PropertiesType>;
|
|
46
|
-
type DefaultInitConfigType = FileUploadAreaInitConfig<FileUploadArea>;
|
|
47
|
-
export declare function fileUploadArea(options: Omit<DefaultInitConfigType, "content">, content: DefaultInitConfigType["content"]): FileUploadArea;
|
|
48
|
-
export declare function fileUploadArea(config: DefaultInitConfigType): FileUploadArea;
|
|
49
|
-
export type FileInfo = {
|
|
50
|
-
id: string;
|
|
51
|
-
type: string;
|
|
52
|
-
size: number;
|
|
53
|
-
name: string;
|
|
54
|
-
lastModified: number;
|
|
55
|
-
url: string;
|
|
56
|
-
};
|
|
57
|
-
/**
|
|
58
|
-
* Компонент загрузки файлов.
|
|
59
|
-
*/
|
|
60
|
-
export declare class FileUploadArea<InitConfigType extends FileUploadAreaInitConfig<any> = DefaultInitConfigType, PropertiesType extends FileUploadAreaProperties = FileUploadAreaProperties> extends Component<InitConfigType, PropertiesType> implements StandaloneComponent {
|
|
61
|
-
private uploadMap;
|
|
62
|
-
constructor(config: InitConfigType);
|
|
63
|
-
standalone(): true;
|
|
64
|
-
/**
|
|
65
|
-
* Возвращает уникальный идентификатор класса компонента.
|
|
66
|
-
* @returns Строка "file-upload-area".
|
|
67
|
-
*/
|
|
68
|
-
protected class(): ServerComponentClass;
|
|
69
|
-
/**
|
|
70
|
-
* Инициализирует свойства компонента загрузки.
|
|
71
|
-
*
|
|
72
|
-
* @param initValues Конфигурация инициализации компонента.
|
|
73
|
-
* @returns Объект свойств компонента.
|
|
74
|
-
*/
|
|
75
|
-
protected initProperties(initValues: InitConfigType): PropertiesType;
|
|
76
|
-
protected serializeRuleOverrides(overrides: Partial<PropertiesType>): Record<string, unknown>;
|
|
77
|
-
serialize(): FileUploadAreaConfig;
|
|
78
|
-
}
|
|
79
|
-
export {};
|
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.FileUploadArea = void 0;
|
|
4
|
-
exports.fileUploadArea = fileUploadArea;
|
|
5
|
-
const server_component_class_1 = require("@matreshka/client-bff-protocol/enums/server-component-class");
|
|
6
|
-
const zod_1 = require("zod");
|
|
7
|
-
const core_1 = require("../core");
|
|
8
|
-
const component_1 = require("./component");
|
|
9
|
-
function fileUploadArea(arg0, arg1) {
|
|
10
|
-
if (arg1 !== undefined) {
|
|
11
|
-
return new FileUploadArea({
|
|
12
|
-
...arg0,
|
|
13
|
-
content: arg1,
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
return new FileUploadArea(arg0);
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Компонент загрузки файлов.
|
|
20
|
-
*/
|
|
21
|
-
class FileUploadArea extends component_1.Component {
|
|
22
|
-
constructor(config) {
|
|
23
|
-
super(config);
|
|
24
|
-
this.uploadMap = new Map();
|
|
25
|
-
this.onInteraction("start", ({ payload }) => {
|
|
26
|
-
const schema = zod_1.z.strictObject({
|
|
27
|
-
id: zod_1.z.string(),
|
|
28
|
-
type: zod_1.z.string(),
|
|
29
|
-
size: zod_1.z.number(),
|
|
30
|
-
name: zod_1.z.string(),
|
|
31
|
-
lastModified: zod_1.z.number(),
|
|
32
|
-
url: zod_1.z.string().refine((val) => val.startsWith("blob:"), {
|
|
33
|
-
message: "url must be a blob",
|
|
34
|
-
}),
|
|
35
|
-
});
|
|
36
|
-
const safePayload = schema.parse(payload);
|
|
37
|
-
this.uploadMap.set(safePayload.id, safePayload);
|
|
38
|
-
this.config.onStart?.(safePayload);
|
|
39
|
-
});
|
|
40
|
-
this.onInteraction("progress", ({ payload }) => {
|
|
41
|
-
const schema = zod_1.z
|
|
42
|
-
.strictObject({
|
|
43
|
-
id: zod_1.z.string(),
|
|
44
|
-
progress: zod_1.z.number().min(0).max(100),
|
|
45
|
-
})
|
|
46
|
-
.refine((data) => this.uploadMap.has(data.id), {
|
|
47
|
-
message: "Unknown upload id",
|
|
48
|
-
path: ["id"],
|
|
49
|
-
});
|
|
50
|
-
const safePayload = schema.parse(payload);
|
|
51
|
-
this.config.onProgress?.({
|
|
52
|
-
file: this.uploadMap.get(safePayload.id),
|
|
53
|
-
progress: safePayload.progress,
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
this.onInteraction("complete", ({ payload }) => {
|
|
57
|
-
const schema = zod_1.z
|
|
58
|
-
.strictObject({
|
|
59
|
-
id: zod_1.z.string(),
|
|
60
|
-
response: zod_1.z.unknown(),
|
|
61
|
-
})
|
|
62
|
-
.refine((data) => this.uploadMap.has(data.id), {
|
|
63
|
-
message: "Unknown upload id",
|
|
64
|
-
path: ["id"],
|
|
65
|
-
});
|
|
66
|
-
const safePayload = schema.parse(payload);
|
|
67
|
-
this.config.onComplete?.({
|
|
68
|
-
file: this.uploadMap.get(safePayload.id),
|
|
69
|
-
response: safePayload.response,
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
this.onInteraction("error", ({ payload }) => {
|
|
73
|
-
const schema = zod_1.z
|
|
74
|
-
.strictObject({
|
|
75
|
-
id: zod_1.z.string(),
|
|
76
|
-
response: zod_1.z.unknown(),
|
|
77
|
-
code: zod_1.z.number(), // Не StatusCode, потому что может быть 0, который не содержится в словаре
|
|
78
|
-
})
|
|
79
|
-
.refine((data) => this.uploadMap.has(data.id), {
|
|
80
|
-
message: "Unknown upload id",
|
|
81
|
-
path: ["id"],
|
|
82
|
-
});
|
|
83
|
-
const safePayload = schema.parse(payload);
|
|
84
|
-
this.config.onError?.({
|
|
85
|
-
file: this.uploadMap.get(safePayload.id),
|
|
86
|
-
response: safePayload.response,
|
|
87
|
-
code: safePayload.code,
|
|
88
|
-
});
|
|
89
|
-
this.uploadMap.delete(safePayload.id);
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
standalone() {
|
|
93
|
-
return true;
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Возвращает уникальный идентификатор класса компонента.
|
|
97
|
-
* @returns Строка "file-upload-area".
|
|
98
|
-
*/
|
|
99
|
-
class() {
|
|
100
|
-
return server_component_class_1.ServerComponentClass.FileUploadArea;
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Инициализирует свойства компонента загрузки.
|
|
104
|
-
*
|
|
105
|
-
* @param initValues Конфигурация инициализации компонента.
|
|
106
|
-
* @returns Объект свойств компонента.
|
|
107
|
-
*/
|
|
108
|
-
initProperties(initValues) {
|
|
109
|
-
return {
|
|
110
|
-
...super.initProperties(initValues),
|
|
111
|
-
content: (0, core_1.calculateComponents)(initValues.content),
|
|
112
|
-
url: initValues.url,
|
|
113
|
-
accept: initValues.accept,
|
|
114
|
-
multiple: initValues.multiple,
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
serializeRuleOverrides(overrides) {
|
|
118
|
-
const out = super.serializeRuleOverrides(overrides);
|
|
119
|
-
if (overrides.content !== undefined) {
|
|
120
|
-
out.content = (0, core_1.serializeComponentList)(overrides.content);
|
|
121
|
-
}
|
|
122
|
-
return out;
|
|
123
|
-
}
|
|
124
|
-
serialize() {
|
|
125
|
-
const result = super.serialize();
|
|
126
|
-
return {
|
|
127
|
-
...result,
|
|
128
|
-
class: server_component_class_1.ServerComponentClass.FileUploadArea,
|
|
129
|
-
properties: this.buildPropertiesForClient({
|
|
130
|
-
content: (0, core_1.serializeComponentList)(this.properties.content),
|
|
131
|
-
}),
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
exports.FileUploadArea = FileUploadArea;
|