@classcad/client 0.0.0-stage → 3.0.0-beta.20261119090857
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -2
- package/bin/buerli-classcad.cjs +23 -0
- package/build/cjs/BuerliCadFacade.js +718 -0
- package/build/cjs/cclass/utils/assembly.js +687 -0
- package/build/cjs/cclass/utils/base.js +136 -0
- package/build/cjs/cclass/utils/index.js +99 -0
- package/build/cjs/cclass/utils/part.js +153 -0
- package/build/cjs/classcadEngine.js +33 -0
- package/build/cjs/createApi.js +71 -0
- package/build/cjs/createUnwrappedApi.js +43 -0
- package/build/cjs/facade.js +103 -0
- package/build/cjs/globals.js +157 -0
- package/build/cjs/index.js +102 -0
- package/build/cjs/io/AwvNodeClient.js +290 -0
- package/build/cjs/io/SocketIOClient.js +339 -0
- package/build/cjs/io/Transaction.js +134 -0
- package/build/cjs/io/WASMClient.js +433 -0
- package/build/cjs/io/WSClient.js +646 -0
- package/build/cjs/io/engineKey.js +202 -0
- package/build/cjs/re-export.js +35 -0
- package/build/cjs/session/host.js +479 -0
- package/build/cjs/session/identity.js +38 -0
- package/build/cjs/session/selection.js +283 -0
- package/build/cjs/session/share.js +229 -0
- package/build/cjs/session/types.js +23 -0
- package/build/cjs/test/SocketIOClient.spec.js +178 -0
- package/build/cjs/types/ComClient.js +10 -0
- package/build/cjs/utils/createRecursiveBufferGeometry.js +55 -0
- package/build/cjs/utils/createRecursiveScene.js +140 -0
- package/build/cjs/utils/getDifferentColoredMeshesFromEntity.js +24 -0
- package/build/cjs/utils/index.js +20 -0
- package/build/cjs/utils/polyline.js +65 -0
- package/build/cjs/utils/waitForSelect.js +29 -0
- package/build/esm/BuerliCadFacade.js +714 -0
- package/build/esm/cclass/utils/assembly.js +665 -0
- package/build/esm/cclass/utils/base.js +127 -0
- package/build/esm/cclass/utils/index.js +62 -0
- package/build/esm/cclass/utils/part.js +144 -0
- package/build/esm/classcadEngine.js +30 -0
- package/build/esm/createApi.js +66 -0
- package/build/esm/createUnwrappedApi.js +39 -0
- package/build/esm/facade.js +98 -0
- package/build/esm/globals.js +150 -0
- package/build/esm/index.js +80 -0
- package/build/esm/io/AwvNodeClient.js +286 -0
- package/build/esm/io/SocketIOClient.js +332 -0
- package/build/esm/io/Transaction.js +130 -0
- package/build/esm/io/WASMClient.js +429 -0
- package/build/esm/io/WSClient.js +642 -0
- package/build/esm/io/engineKey.js +196 -0
- package/build/esm/re-export.js +3 -0
- package/build/esm/session/host.js +474 -0
- package/build/esm/session/identity.js +34 -0
- package/build/esm/session/selection.js +277 -0
- package/build/esm/session/share.js +221 -0
- package/build/esm/session/types.js +20 -0
- package/build/esm/test/SocketIOClient.spec.js +178 -0
- package/build/esm/types/ComClient.js +7 -0
- package/build/esm/utils/createRecursiveBufferGeometry.js +51 -0
- package/build/esm/utils/createRecursiveScene.js +136 -0
- package/build/esm/utils/getDifferentColoredMeshesFromEntity.js +20 -0
- package/build/esm/utils/index.js +4 -0
- package/build/esm/utils/polyline.js +60 -0
- package/build/esm/utils/waitForSelect.js +23 -0
- package/build/types/BuerliCadFacade.d.ts +410 -0
- package/build/types/cclass/utils/assembly.d.ts +180 -0
- package/build/types/cclass/utils/base.d.ts +55 -0
- package/build/types/cclass/utils/index.d.ts +46 -0
- package/build/types/cclass/utils/part.d.ts +46 -0
- package/build/types/classcadEngine.d.ts +40 -0
- package/build/types/createApi.d.ts +65 -0
- package/build/types/createUnwrappedApi.d.ts +36 -0
- package/build/types/facade.d.ts +9 -0
- package/build/types/globals.d.ts +79 -0
- package/build/types/index.d.ts +82 -0
- package/build/types/io/AwvNodeClient.d.ts +182 -0
- package/build/types/io/SocketIOClient.d.ts +84 -0
- package/build/types/io/Transaction.d.ts +56 -0
- package/build/types/io/WASMClient.d.ts +239 -0
- package/build/types/io/WSClient.d.ts +242 -0
- package/build/types/io/engineKey.d.ts +137 -0
- package/build/types/re-export.d.ts +6 -0
- package/build/types/session/host.d.ts +108 -0
- package/build/types/session/identity.d.ts +46 -0
- package/build/types/session/selection.d.ts +164 -0
- package/build/types/session/share.d.ts +131 -0
- package/build/types/session/types.d.ts +113 -0
- package/build/types/test/SocketIOClient.spec.d.ts +0 -0
- package/build/types/types/ComClient.d.ts +307 -0
- package/build/types/utils/createRecursiveBufferGeometry.d.ts +8 -0
- package/build/types/utils/createRecursiveScene.d.ts +24 -0
- package/build/types/utils/getDifferentColoredMeshesFromEntity.d.ts +14 -0
- package/build/types/utils/index.d.ts +4 -0
- package/build/types/utils/polyline.d.ts +43 -0
- package/build/types/utils/waitForSelect.d.ts +4 -0
- package/doc/api/advanced/connection.md +488 -0
- package/doc/api/clients.md +2045 -0
- package/doc/api/engine-keys.md +230 -0
- package/doc/api/index.md +58 -0
- package/doc/api/modeling.md +682 -0
- package/doc/api/other.md +33 -0
- package/doc/api/session-sharing.md +706 -0
- package/package.json +59 -4
- package/test/classcadEngine.test.cjs +44 -0
- package/test/connect.test.cjs +81 -0
- package/test/engineKey.test.cjs +80 -0
- package/test/onClient.test.cjs +82 -0
- package/typedoc.json +8 -0
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.clientMade = exports.onClient = exports.init = exports.Globals = void 0;
|
|
4
|
+
const sdkjs_1 = require("@classcad/sdkjs");
|
|
5
|
+
const createApi_1 = require("./createApi");
|
|
6
|
+
class Globals {
|
|
7
|
+
}
|
|
8
|
+
exports.Globals = Globals;
|
|
9
|
+
Globals.clientCache = {};
|
|
10
|
+
/**
|
|
11
|
+
* Sets up buerli: tells it how to reach ClassCAD, with a factory that makes the client for each drawing.
|
|
12
|
+
*
|
|
13
|
+
* Call it once, before the first drawing connects. Every drawing that connects afterwards
|
|
14
|
+
* ({@link BuerliCadFacade.connect}, `BuerliCadFacade.utils.connect`, react-cad, Buerligons) gets its client from
|
|
15
|
+
* `source`; the client connects right after it is made. Calling `init` again replaces the factory for the drawings
|
|
16
|
+
* that connect from then on. `options` go to the buerli store (`init` of `@classcad/sdkjs`).
|
|
17
|
+
*
|
|
18
|
+
* @param source - Makes the client for a drawing: called with the new drawing's id, returns a client that is not
|
|
19
|
+
* connected yet.
|
|
20
|
+
* @param options - Options of the buerli store: theme colors, how geometry is shown (`config.geometry`), plugins.
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { init, WASMClient } from '@classcad/client'
|
|
24
|
+
*
|
|
25
|
+
* // The engine in the browser, with a public access token from classcad.ch/account
|
|
26
|
+
* init(drawingId => new WASMClient(drawingId, { token: 'ccpk_…' }))
|
|
27
|
+
* ```
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* import { init, SocketIOClient, WSClient } from '@classcad/client'
|
|
31
|
+
*
|
|
32
|
+
* // A ClassCAD server (classcad-socketio)
|
|
33
|
+
* init(drawingId => new SocketIOClient('ws://localhost:9091', drawingId))
|
|
34
|
+
* // Or the native engine's own WebSocket server (classcad-cli worker)
|
|
35
|
+
* init(drawingId => new WSClient('ws://localhost:9094', drawingId))
|
|
36
|
+
* ```
|
|
37
|
+
* @category Clients
|
|
38
|
+
*/
|
|
39
|
+
const init = (source, options) => {
|
|
40
|
+
// Make sure, the buerli core is initialized by calling initCore
|
|
41
|
+
(0, sdkjs_1.init)(options);
|
|
42
|
+
// Set the factory to the Globals object
|
|
43
|
+
Globals.factory = source;
|
|
44
|
+
};
|
|
45
|
+
exports.init = init;
|
|
46
|
+
const clientCallbacks = new Set();
|
|
47
|
+
/**
|
|
48
|
+
* Calls back with every drawing's client: the ones there are, and each new one before it connects.
|
|
49
|
+
*
|
|
50
|
+
* A new client is passed as soon as the factory has made it. An app that prepares the clients a host makes
|
|
51
|
+
* (session sharing, engine settings) registers here instead of wrapping the host's factory. Returns a function
|
|
52
|
+
* that unregisters.
|
|
53
|
+
*
|
|
54
|
+
* A callback that throws is logged; the drawing connects all the same.
|
|
55
|
+
*
|
|
56
|
+
* @param callback - Called with each client and the id of its drawing.
|
|
57
|
+
* @returns A function that unregisters `callback`.
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* import { onClient, syncSelection, WASMClient, WSClient } from '@classcad/client'
|
|
61
|
+
*
|
|
62
|
+
* // Prepares every client a host makes (react-cad, Buerligons, a facade), before it connects
|
|
63
|
+
* const unregister = onClient((client, drawingId) => {
|
|
64
|
+
* // Shares the selection where the session can be shared
|
|
65
|
+
* if (client instanceof WSClient || client instanceof WASMClient) syncSelection(client)
|
|
66
|
+
* // Hidden objects are not needed in this app
|
|
67
|
+
* client.on('connected', () => client.setEmissionConfig({ sendGraphic_Invisible: false }).catch(console.warn))
|
|
68
|
+
* client.on('conn_error', (_client, reason) => console.error(`Drawing ${drawingId} lost ClassCAD:`, reason))
|
|
69
|
+
* })
|
|
70
|
+
* ```
|
|
71
|
+
* @category Clients
|
|
72
|
+
*/
|
|
73
|
+
const onClient = (callback) => {
|
|
74
|
+
clientCallbacks.add(callback);
|
|
75
|
+
for (const [drawingId, client] of Object.entries(Globals.clientCache))
|
|
76
|
+
callback(client, drawingId);
|
|
77
|
+
return () => {
|
|
78
|
+
clientCallbacks.delete(callback);
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
exports.onClient = onClient;
|
|
82
|
+
/** Hands a client the factory has just made to the onClient callbacks (BuerliCadFacade.utils.connect). */
|
|
83
|
+
const clientMade = (client, drawingId) => {
|
|
84
|
+
// (a callback that fails costs the drawing nothing: it connects all the same)
|
|
85
|
+
clientCallbacks.forEach(callback => {
|
|
86
|
+
try {
|
|
87
|
+
callback(client, drawingId);
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
console.error(error);
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
};
|
|
94
|
+
exports.clientMade = clientMade;
|
|
95
|
+
//#region buerli store subscription
|
|
96
|
+
/**
|
|
97
|
+
* Subscribe to the buerli store and observe the drawing id's.
|
|
98
|
+
*/
|
|
99
|
+
sdkjs_1.api.subscribe((state, prev) => {
|
|
100
|
+
if (state.drawing.ids !== prev.drawing.ids && state.drawing.ids) {
|
|
101
|
+
const ids = Object.getOwnPropertyNames(Globals.clientCache);
|
|
102
|
+
ids.forEach(id => {
|
|
103
|
+
if (state.drawing.ids.indexOf(id) < 0 && Globals.clientCache[id]) {
|
|
104
|
+
Globals.clientCache[id].disconnect().catch(e => console.error(e));
|
|
105
|
+
delete Globals.clientCache[id];
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
const drawingSubscriptions = {};
|
|
111
|
+
/**
|
|
112
|
+
* Observe the drawing ids in the buerli state.
|
|
113
|
+
* The callback will be called each time a drawing is added or removed from the buerli state.
|
|
114
|
+
* Goal of this code is to call 'openFeature' and 'closeFeature' automatically if the active feature changes.
|
|
115
|
+
*/
|
|
116
|
+
sdkjs_1.api.subscribe((state, prev) => {
|
|
117
|
+
if (state.drawing.ids !== prev.drawing.ids && state.drawing.ids) {
|
|
118
|
+
for (const drawingId of state.drawing.ids) {
|
|
119
|
+
if (!drawingSubscriptions[drawingId]) {
|
|
120
|
+
// Observe the active feature for each drawing and keep the unsubscribe function.
|
|
121
|
+
// The callback will be called as soon as the active feature of 'drawingId' changes.
|
|
122
|
+
const unsubcribe = sdkjs_1.api.subscribe((stateB, prevB) => {
|
|
123
|
+
var _a, _b, _c, _d;
|
|
124
|
+
const featureId = (_b = (_a = stateB.drawing.refs[drawingId]) === null || _a === void 0 ? void 0 : _a.plugin.active.feature) !== null && _b !== void 0 ? _b : null;
|
|
125
|
+
const prevFeatureId = (_d = (_c = prevB.drawing.refs[drawingId]) === null || _c === void 0 ? void 0 : _c.plugin.active.feature) !== null && _d !== void 0 ? _d : null;
|
|
126
|
+
if (featureId !== prevFeatureId) {
|
|
127
|
+
// Close the previous active feature
|
|
128
|
+
if (prevFeatureId !== null) {
|
|
129
|
+
(0, createApi_1.createApi)(drawingId)
|
|
130
|
+
.v1.part.closeFeature({ id: prevFeatureId })
|
|
131
|
+
.catch(console.error);
|
|
132
|
+
}
|
|
133
|
+
// Open the active feature
|
|
134
|
+
if (featureId !== null) {
|
|
135
|
+
(0, createApi_1.createApi)(drawingId)
|
|
136
|
+
.v1.part.openFeature({ id: featureId })
|
|
137
|
+
.catch(console.error);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
// Add the unsubcribe function to the drawing subscribtions record.
|
|
142
|
+
drawingSubscriptions[drawingId] = unsubcribe;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
// Unsubscribe the drawing listeners if a drawing does not exist anymore
|
|
146
|
+
// and remove the listeners from the drawingSubscriptions record.
|
|
147
|
+
const subIds = Object.getOwnPropertyNames(drawingSubscriptions);
|
|
148
|
+
for (const id of subIds) {
|
|
149
|
+
if (!state.drawing.ids.includes(id)) {
|
|
150
|
+
const unsubcribe = drawingSubscriptions[id];
|
|
151
|
+
unsubcribe();
|
|
152
|
+
delete drawingSubscriptions[id];
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
//#endregion buerli store subscription
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.onClient = exports.init = exports.getApiFacade = exports.createUnwrappedApi = exports.createApi = exports.SessionHost = void 0;
|
|
18
|
+
/**
|
|
19
|
+
* Connects an app to ClassCAD, the CAD engine, and runs the ClassCAD API on its drawings: the package every buerli
|
|
20
|
+
* app starts with. The engine runs in the browser as WebAssembly, or natively behind a server; a session can be
|
|
21
|
+
* shared with other apps and with AI agents.
|
|
22
|
+
*
|
|
23
|
+
* ## What it exposes
|
|
24
|
+
*
|
|
25
|
+
* - {@link init}: sets buerli up with a factory that makes the client of each drawing.
|
|
26
|
+
* - {@link WASMClient}: runs the engine in the browser, with a public access token (`ccpk_…`).
|
|
27
|
+
* - {@link WSClient}: connects to a native engine's own WebSocket server (`classcad-cli worker`).
|
|
28
|
+
* - {@link SocketIOClient}: connects to the ClassCAD server (`classcad-socketio`).
|
|
29
|
+
* - {@link onClient}: prepares each client a host makes, before it connects.
|
|
30
|
+
* - {@link BuerliCadFacade}: connects a drawing, holds its ClassCAD API (`api.v1.*`) and makes three.js geometry of
|
|
31
|
+
* the model.
|
|
32
|
+
* - {@link resolveEngineKey}: an engine key from an access token, for engines an app starts itself.
|
|
33
|
+
* - {@link classcadEngine}: the ClassCAD engine this package is built against, and where its bundle is.
|
|
34
|
+
* - Session sharing: {@link WSClient.createInvite | createInvite} on a client, {@link buildShareUrl} and
|
|
35
|
+
* {@link getShareTokenFromUrl}, {@link getSessionShareState}, {@link syncSelection}, and the participants'
|
|
36
|
+
* {@link ClientIdentity}.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* import * as THREE from 'three'
|
|
41
|
+
* import { init, BuerliCadFacade, WASMClient } from '@classcad/client'
|
|
42
|
+
*
|
|
43
|
+
* // The engine in the browser; a public access token from classcad.ch/account
|
|
44
|
+
* init(drawingId => new WASMClient(drawingId, { token: 'ccpk_…' }))
|
|
45
|
+
*
|
|
46
|
+
* const facade = new BuerliCadFacade()
|
|
47
|
+
* await facade.connect()
|
|
48
|
+
* const part = (await facade.api.v1.part.create({ name: 'Part' }))!
|
|
49
|
+
* await facade.api.v1.part.box({ id: part, length: 100, width: 60, height: 20 })
|
|
50
|
+
* await facade.api.v1.part.cylinder({ id: part, diameter: 30, height: 50 })
|
|
51
|
+
*
|
|
52
|
+
* // The model as three.js geometry
|
|
53
|
+
* const geometries = await facade.createBufferGeometry(part)
|
|
54
|
+
* const meshes = geometries?.map(geometry => new THREE.Mesh(geometry, new THREE.MeshStandardMaterial()))
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
* @categoryDescription Clients
|
|
58
|
+
* Start here: `init` takes a factory that makes the client of each drawing, which connects it to ClassCAD: the engine
|
|
59
|
+
* in the page (`WASMClient`), a native engine (`WSClient`) or the ClassCAD server (`SocketIOClient`).
|
|
60
|
+
*
|
|
61
|
+
* @categoryDescription Modeling
|
|
62
|
+
* `BuerliCadFacade` connects a drawing, runs the ClassCAD API on it (`api.v1.*`) and turns the model into three.js
|
|
63
|
+
* geometry.
|
|
64
|
+
*
|
|
65
|
+
* @categoryDescription Engine Keys
|
|
66
|
+
* The key an engine starts with. A public access token on the `WASMClient` is all most apps need; the rest is for
|
|
67
|
+
* apps with a sign-in of their own, and for servers.
|
|
68
|
+
*
|
|
69
|
+
* @categoryDescription Session Sharing
|
|
70
|
+
* Shares a drawing's session with other apps and AI agents: invites and share links, who is in the session, what
|
|
71
|
+
* they publish, and what they have selected.
|
|
72
|
+
*
|
|
73
|
+
* @categoryDescription Connection
|
|
74
|
+
* The protocol between a client and its engine: requests, replies, and what the engine sends back after a command
|
|
75
|
+
* (`EmissionConfig`). For apps that talk to the engine directly.
|
|
76
|
+
*
|
|
77
|
+
* @packageDocumentation
|
|
78
|
+
*/
|
|
79
|
+
const createApi_1 = require("./createApi");
|
|
80
|
+
Object.defineProperty(exports, "createApi", { enumerable: true, get: function () { return createApi_1.createApi; } });
|
|
81
|
+
Object.defineProperty(exports, "getApiFacade", { enumerable: true, get: function () { return createApi_1.getApiFacade; } });
|
|
82
|
+
const createUnwrappedApi_1 = require("./createUnwrappedApi");
|
|
83
|
+
Object.defineProperty(exports, "createUnwrappedApi", { enumerable: true, get: function () { return createUnwrappedApi_1.createUnwrappedApi; } });
|
|
84
|
+
const globals_1 = require("./globals");
|
|
85
|
+
Object.defineProperty(exports, "init", { enumerable: true, get: function () { return globals_1.init; } });
|
|
86
|
+
Object.defineProperty(exports, "onClient", { enumerable: true, get: function () { return globals_1.onClient; } });
|
|
87
|
+
__exportStar(require("./re-export"), exports);
|
|
88
|
+
__exportStar(require("./cclass/utils"), exports);
|
|
89
|
+
__exportStar(require("./classcadEngine"), exports);
|
|
90
|
+
__exportStar(require("./BuerliCadFacade"), exports);
|
|
91
|
+
__exportStar(require("./io/AwvNodeClient"), exports);
|
|
92
|
+
__exportStar(require("./io/SocketIOClient"), exports);
|
|
93
|
+
__exportStar(require("./io/WASMClient"), exports);
|
|
94
|
+
__exportStar(require("./io/engineKey"), exports);
|
|
95
|
+
__exportStar(require("./io/WSClient"), exports);
|
|
96
|
+
__exportStar(require("./session/types"), exports);
|
|
97
|
+
__exportStar(require("./session/share"), exports);
|
|
98
|
+
__exportStar(require("./session/identity"), exports);
|
|
99
|
+
__exportStar(require("./session/selection"), exports);
|
|
100
|
+
var host_1 = require("./session/host");
|
|
101
|
+
Object.defineProperty(exports, "SessionHost", { enumerable: true, get: function () { return host_1.SessionHost; } });
|
|
102
|
+
__exportStar(require("./types/ComClient"), exports);
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AwvNodeClient = void 0;
|
|
4
|
+
const sdkjs_1 = require("@classcad/sdkjs");
|
|
5
|
+
const base64_arraybuffer_1 = require("base64-arraybuffer");
|
|
6
|
+
const events_1 = require("events");
|
|
7
|
+
const pako_1 = require("pako");
|
|
8
|
+
const ComClient_1 = require("../types/ComClient");
|
|
9
|
+
const types_1 = require("../session/types");
|
|
10
|
+
const identity_1 = require("../session/identity");
|
|
11
|
+
/**
|
|
12
|
+
* Turns the reply of a GetEmissionConfig/SetEmissionConfig request into an EmissionConfig, or
|
|
13
|
+
* throws EMISSION_CONFIG_UNSUPPORTED when the engine did not understand the
|
|
14
|
+
* command. Old engines answer in two shapes: the CommandFactory path rejects
|
|
15
|
+
* with an "Unknown command" error and no `result` (caught by the callers), the
|
|
16
|
+
* old ClassCADInstance path (published WASM) answers `result: ''` without any
|
|
17
|
+
* error - hence the shape check here.
|
|
18
|
+
*/
|
|
19
|
+
const toEmissionConfig = (result) => {
|
|
20
|
+
if (!result || typeof result !== 'object' || typeof result.sendStructure !== 'boolean') {
|
|
21
|
+
const err = new Error('This engine does not support GetEmissionConfig/SetEmissionConfig (emission config is fixed).');
|
|
22
|
+
err.name = ComClient_1.EMISSION_CONFIG_UNSUPPORTED;
|
|
23
|
+
throw err;
|
|
24
|
+
}
|
|
25
|
+
return result;
|
|
26
|
+
};
|
|
27
|
+
const unsupportedSharing = () => {
|
|
28
|
+
const err = new Error('This client cannot share its session.');
|
|
29
|
+
err.name = types_1.SESSION_SHARING_UNSUPPORTED;
|
|
30
|
+
return err;
|
|
31
|
+
};
|
|
32
|
+
const unsupportedFromRejection = (e) => {
|
|
33
|
+
// A response envelope (maxLevel/messages) without a result is the
|
|
34
|
+
// "Unknown command" reply of an engine that predates the config commands.
|
|
35
|
+
if (e && typeof e === 'object' && 'maxLevel' in e && typeof e.result === 'undefined') {
|
|
36
|
+
const err = new Error('This engine does not support GetEmissionConfig/SetEmissionConfig (emission config is fixed).');
|
|
37
|
+
err.name = ComClient_1.EMISSION_CONFIG_UNSUPPORTED;
|
|
38
|
+
throw err;
|
|
39
|
+
}
|
|
40
|
+
throw e;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* The base of every client: what buerli asks of a connection to ClassCAD, and the session-sharing surface.
|
|
44
|
+
*
|
|
45
|
+
* An app makes a {@link WASMClient}, {@link WSClient} or {@link SocketIOClient} in the factory it gives {@link init};
|
|
46
|
+
* buerli connects it and sends the drawing's API calls through it. A client that cannot share its session (the base
|
|
47
|
+
* class, {@link SocketIOClient}) has `shareKind` null and rejects `createInvite` with
|
|
48
|
+
* {@link SESSION_SHARING_UNSUPPORTED}.
|
|
49
|
+
*
|
|
50
|
+
* A client is an `EventEmitter`; every event passes the client first:
|
|
51
|
+
*
|
|
52
|
+
* - `connected` (client, true): the client has connected.
|
|
53
|
+
* - `conn_error` (client, reason): the connection failed or broke off.
|
|
54
|
+
* - `conn_timeout` (client, reason): connecting timed out ({@link SocketIOClient}).
|
|
55
|
+
* - `reconnect` (client): the connection is back, and the drawing reconnected ({@link SocketIOClient}).
|
|
56
|
+
* - `session_joined` (client, role, inviteName): the client joined a shared session with an invite ({@link WSClient}).
|
|
57
|
+
* - `peer_joined` / `peer_left` (client, {@link SessionPeer}): a guest joined or left this client's session.
|
|
58
|
+
* - `peers_changed` (client, {@link SessionPeer}[]): the guests in the session changed.
|
|
59
|
+
* - `invites_changed` (client, {@link SessionInvite}[]): an invite was made or taken back.
|
|
60
|
+
* - `identities_changed` (client, identities): a participant said who it is, or left.
|
|
61
|
+
* - `presence` (client, {@link PresenceMessage}): another participant published something.
|
|
62
|
+
* - `invite_refused` (client, {@link SessionInvite}, {@link OfferRefusal}): the share relay would not take an invite
|
|
63
|
+
* ({@link WASMClient}).
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* import { onClient } from '@classcad/client'
|
|
68
|
+
* import type { PresenceMessage, SessionPeer } from '@classcad/client'
|
|
69
|
+
*
|
|
70
|
+
* // A status line (an element of the app's page) for every drawing's connection and session
|
|
71
|
+
* onClient(client => {
|
|
72
|
+
* client.on('connected', () => (statusLine.textContent = 'Connected'))
|
|
73
|
+
* client.on('conn_error', (_client, reason) => (statusLine.textContent = `No connection: ${reason}`))
|
|
74
|
+
* client.on('peers_changed', (_client, peers: SessionPeer[]) => (statusLine.textContent = `${peers.length} guests`))
|
|
75
|
+
* client.on('presence', (_client, msg: PresenceMessage) => {
|
|
76
|
+
* if (msg.channel === 'leave') console.log(`${client.identities[msg.peerId]?.name ?? msg.peerId} left`)
|
|
77
|
+
* })
|
|
78
|
+
* })
|
|
79
|
+
* ```
|
|
80
|
+
* @advanced
|
|
81
|
+
* @category Clients
|
|
82
|
+
*/
|
|
83
|
+
class AwvNodeClient extends events_1.EventEmitter {
|
|
84
|
+
/** Guests currently sharing this session (host side). */
|
|
85
|
+
get peers() {
|
|
86
|
+
return [];
|
|
87
|
+
}
|
|
88
|
+
/** The invites this client minted and has not revoked. */
|
|
89
|
+
get invites() {
|
|
90
|
+
return this._invites;
|
|
91
|
+
}
|
|
92
|
+
trackInvite(invite) {
|
|
93
|
+
this._invites = [...this._invites, invite];
|
|
94
|
+
this.emit('invites_changed', this, this._invites);
|
|
95
|
+
}
|
|
96
|
+
untrackInvite(token) {
|
|
97
|
+
this._invites = this._invites.filter(i => i.invite !== token);
|
|
98
|
+
this.emit('invites_changed', this, this._invites);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Mints an invite token guests (apps, an MCP) can join with. Rejects with SESSION_SHARING_UNSUPPORTED on non-sharing transports.
|
|
102
|
+
*
|
|
103
|
+
* @param role - What guests with the invite may do: `'edit'` (default) or `'view'`.
|
|
104
|
+
* @param name - A name to show for the invite, e.g. "workgroup"; it has no effect.
|
|
105
|
+
*/
|
|
106
|
+
createInvite(role = 'edit', name = '') {
|
|
107
|
+
void role;
|
|
108
|
+
void name;
|
|
109
|
+
return Promise.reject(unsupportedSharing());
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Revokes a token and kicks the guests using it.
|
|
113
|
+
*
|
|
114
|
+
* @param invite - The invite token.
|
|
115
|
+
* @returns The token, whether it was revoked, and how many guests were cut off.
|
|
116
|
+
*/
|
|
117
|
+
revokeInvite(invite) {
|
|
118
|
+
void invite;
|
|
119
|
+
return Promise.reject(unsupportedSharing());
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Fire-and-forget presence broadcast to session siblings (camera, cursor …). No-op on non-sharing transports.
|
|
123
|
+
*
|
|
124
|
+
* @param channel - The channel, e.g. `'view'` for a camera viewpoint; others get it in their `presence` event.
|
|
125
|
+
* @param data - The payload, sent as it is. A frame larger than 8 KB is dropped.
|
|
126
|
+
*/
|
|
127
|
+
sendPresence(channel, data) {
|
|
128
|
+
void channel;
|
|
129
|
+
void data;
|
|
130
|
+
}
|
|
131
|
+
/** What the other participants said about themselves ({@link ClientIdentity}), by peer id. */
|
|
132
|
+
get identities() {
|
|
133
|
+
return this._identities;
|
|
134
|
+
}
|
|
135
|
+
/** Transports pass every presence frame through here: a 'client' frame says who a peer is, 'leave' that it is gone. */
|
|
136
|
+
noteIdentity(msg) {
|
|
137
|
+
if (msg.channel === identity_1.CLIENT_CHANNEL && msg.peerId) {
|
|
138
|
+
const identity = (0, identity_1.sanitizeIdentity)(msg.data);
|
|
139
|
+
if (!identity)
|
|
140
|
+
return;
|
|
141
|
+
this._identities = Object.assign(Object.assign({}, this._identities), { [msg.peerId]: identity });
|
|
142
|
+
this.emit('identities_changed', this, this._identities);
|
|
143
|
+
}
|
|
144
|
+
else if (msg.channel === 'leave' && msg.peerId in this._identities) {
|
|
145
|
+
const next = Object.assign({}, this._identities);
|
|
146
|
+
delete next[msg.peerId];
|
|
147
|
+
this._identities = next;
|
|
148
|
+
this.emit('identities_changed', this, this._identities);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/** The session is gone (disconnect): so is everyone in it. */
|
|
152
|
+
clearIdentities() {
|
|
153
|
+
if (Object.keys(this._identities).length === 0)
|
|
154
|
+
return;
|
|
155
|
+
this._identities = {};
|
|
156
|
+
this.emit('identities_changed', this, this._identities);
|
|
157
|
+
}
|
|
158
|
+
constructor() {
|
|
159
|
+
super();
|
|
160
|
+
// ── Session sharing (transport-independent surface, see session/types.ts) ──
|
|
161
|
+
// The base class answers "cannot share" so every client has the same
|
|
162
|
+
// shape; transports override: WSClient (a ClassCAD server owns the session)
|
|
163
|
+
// and WASMClient (the page hosts it itself, see session/host.ts).
|
|
164
|
+
/** How this client's invites are honoured; null = this client cannot share. */
|
|
165
|
+
this.shareKind = null;
|
|
166
|
+
/** This connection's role within a shared session ('edit' unless a guest with a view invite). */
|
|
167
|
+
this.role = 'edit';
|
|
168
|
+
/** Name of the invite this connection joined with ('' for the host or unnamed tokens). */
|
|
169
|
+
this.inviteName = '';
|
|
170
|
+
/** The invites this client minted (the server keeps no queryable list). */
|
|
171
|
+
this._invites = [];
|
|
172
|
+
/** What the other participants said about themselves ('client' presence, see session/identity.ts), by peer id. */
|
|
173
|
+
this._identities = {};
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Sets the drawing this client serves; the constructor does it.
|
|
177
|
+
*
|
|
178
|
+
* @internal
|
|
179
|
+
*/
|
|
180
|
+
setDrawingId(drawingId) {
|
|
181
|
+
this._drawingId = drawingId;
|
|
182
|
+
}
|
|
183
|
+
/** The drawing this client serves. */
|
|
184
|
+
get drawingId() {
|
|
185
|
+
return this._drawingId;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Sends a command of the server's own (a middleware command), with the fields of `data`.
|
|
189
|
+
*
|
|
190
|
+
* @param command - The command's name.
|
|
191
|
+
* @param data - Fields sent along with the command.
|
|
192
|
+
*/
|
|
193
|
+
executeMiddleware(command, data) {
|
|
194
|
+
return this.request(Object.assign({ command }, data));
|
|
195
|
+
}
|
|
196
|
+
// TODO: should it be possible the task to be an string?
|
|
197
|
+
/**
|
|
198
|
+
* Runs `task` (API calls) on the engine with an `Execute` request. Rejects with the reply when it carries an error.
|
|
199
|
+
*
|
|
200
|
+
* The drawing's API ({@link createApi}) calls through here; the streams of `streamMap` are deflated and sent as
|
|
201
|
+
* base64.
|
|
202
|
+
*
|
|
203
|
+
* @param task - The API calls to run, e.g. `[{ 'v1.part.create': [{ name: 'Part' }] }]`.
|
|
204
|
+
* @param streamMap - Data the calls read, by stream key (a file to import, for example).
|
|
205
|
+
* @param options - Options of the request, such as `undoable`.
|
|
206
|
+
*/
|
|
207
|
+
executeRequest(task, streamMap, options) {
|
|
208
|
+
if (streamMap) {
|
|
209
|
+
const streams = {};
|
|
210
|
+
Object.getOwnPropertyNames(streamMap).forEach(streamKey => {
|
|
211
|
+
const streamData = streamMap[streamKey];
|
|
212
|
+
const binaryContent = (0, pako_1.deflate)(streamData, { raw: true });
|
|
213
|
+
streams[streamKey] = (0, base64_arraybuffer_1.encode)(binaryContent.slice().buffer);
|
|
214
|
+
});
|
|
215
|
+
return this.request({ command: 'Execute', task, streamData: streams, options: options });
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
return this.request({ command: 'Execute', task, options: options });
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* The effective emission config of this connection ({@link EmissionConfig}): what the engine sends back after a
|
|
223
|
+
* command.
|
|
224
|
+
*
|
|
225
|
+
* Rejects with an Error named {@link EMISSION_CONFIG_UNSUPPORTED} on engines that predate the command.
|
|
226
|
+
*/
|
|
227
|
+
// Implemented once here for every transport: the commands travel like any other request, the engine answers with
|
|
228
|
+
// the effective flags in `result`. Transports that need to route the reply differently (WASMClient) only adapt
|
|
229
|
+
// their requestByName.
|
|
230
|
+
async getEmissionConfig() {
|
|
231
|
+
try {
|
|
232
|
+
const res = await this.request({ command: 'GetEmissionConfig' });
|
|
233
|
+
return toEmissionConfig(res.result);
|
|
234
|
+
}
|
|
235
|
+
catch (e) {
|
|
236
|
+
return unsupportedFromRejection(e);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Merges `partial` into the emission config of this connection and resolves to the effective flags.
|
|
241
|
+
*
|
|
242
|
+
* The engine keeps the config for every command that follows, until it is changed again. Rejects with an Error
|
|
243
|
+
* named {@link EMISSION_CONFIG_UNSUPPORTED} on engines that predate the command. {@link EmissionConfig} has an
|
|
244
|
+
* example, and {@link onClient} sets flags as every client connects.
|
|
245
|
+
*
|
|
246
|
+
* @param partial - The flags to change; the others stay as they are.
|
|
247
|
+
*/
|
|
248
|
+
async setEmissionConfig(partial) {
|
|
249
|
+
try {
|
|
250
|
+
const res = await this.request({ command: 'SetEmissionConfig', config: partial });
|
|
251
|
+
return toEmissionConfig(res.result);
|
|
252
|
+
}
|
|
253
|
+
catch (e) {
|
|
254
|
+
return unsupportedFromRejection(e);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Takes the undo history the engine reported (UndoStack frame) into the drawing's
|
|
259
|
+
* `cad.states`, in the form SocketIOClient fills from the node server's 'undo' event: the
|
|
260
|
+
* state ids, the current one, and per state its caption. The engine records a state only for
|
|
261
|
+
* an undoable command, so every state is undoable.
|
|
262
|
+
*/
|
|
263
|
+
applyUndoStack(frame) {
|
|
264
|
+
var _a, _b, _c, _d;
|
|
265
|
+
const drawing = (0, sdkjs_1.getDrawing)(this._drawingId);
|
|
266
|
+
if (!drawing || !Array.isArray(frame.stack))
|
|
267
|
+
return;
|
|
268
|
+
const previous = Object.values(drawing.cad.states.captionMap);
|
|
269
|
+
const captionMap = {};
|
|
270
|
+
for (const state of frame.stack) {
|
|
271
|
+
const caption = (_d = (_b = (_a = frame.captions) === null || _a === void 0 ? void 0 : _a[state]) !== null && _b !== void 0 ? _b : (_c = previous.find(entry => entry.stateName === state)) === null || _c === void 0 ? void 0 : _c.caption) !== null && _d !== void 0 ? _d : '';
|
|
272
|
+
captionMap[state] = { stateName: state, caption, undoable: true };
|
|
273
|
+
}
|
|
274
|
+
drawing.api.cad.setStates(frame.current ? Number.parseInt(frame.current, 10) : 0, frame.stack, captionMap);
|
|
275
|
+
}
|
|
276
|
+
/** Loads the drawing's structure tree from the engine and sets it in the buerli store. */
|
|
277
|
+
requestTree() {
|
|
278
|
+
return new Promise(async (resolve, reject) => {
|
|
279
|
+
try {
|
|
280
|
+
const response = await this.request({ command: 'GetTree' });
|
|
281
|
+
(0, sdkjs_1.getDrawing)(this._drawingId).api.structure.set(response.result);
|
|
282
|
+
resolve(response);
|
|
283
|
+
}
|
|
284
|
+
catch (error) {
|
|
285
|
+
reject(error);
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
exports.AwvNodeClient = AwvNodeClient;
|