@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.
Files changed (108) hide show
  1. package/README.md +20 -2
  2. package/bin/buerli-classcad.cjs +23 -0
  3. package/build/cjs/BuerliCadFacade.js +718 -0
  4. package/build/cjs/cclass/utils/assembly.js +687 -0
  5. package/build/cjs/cclass/utils/base.js +136 -0
  6. package/build/cjs/cclass/utils/index.js +99 -0
  7. package/build/cjs/cclass/utils/part.js +153 -0
  8. package/build/cjs/classcadEngine.js +33 -0
  9. package/build/cjs/createApi.js +71 -0
  10. package/build/cjs/createUnwrappedApi.js +43 -0
  11. package/build/cjs/facade.js +103 -0
  12. package/build/cjs/globals.js +157 -0
  13. package/build/cjs/index.js +102 -0
  14. package/build/cjs/io/AwvNodeClient.js +290 -0
  15. package/build/cjs/io/SocketIOClient.js +339 -0
  16. package/build/cjs/io/Transaction.js +134 -0
  17. package/build/cjs/io/WASMClient.js +433 -0
  18. package/build/cjs/io/WSClient.js +646 -0
  19. package/build/cjs/io/engineKey.js +202 -0
  20. package/build/cjs/re-export.js +35 -0
  21. package/build/cjs/session/host.js +479 -0
  22. package/build/cjs/session/identity.js +38 -0
  23. package/build/cjs/session/selection.js +283 -0
  24. package/build/cjs/session/share.js +229 -0
  25. package/build/cjs/session/types.js +23 -0
  26. package/build/cjs/test/SocketIOClient.spec.js +178 -0
  27. package/build/cjs/types/ComClient.js +10 -0
  28. package/build/cjs/utils/createRecursiveBufferGeometry.js +55 -0
  29. package/build/cjs/utils/createRecursiveScene.js +140 -0
  30. package/build/cjs/utils/getDifferentColoredMeshesFromEntity.js +24 -0
  31. package/build/cjs/utils/index.js +20 -0
  32. package/build/cjs/utils/polyline.js +65 -0
  33. package/build/cjs/utils/waitForSelect.js +29 -0
  34. package/build/esm/BuerliCadFacade.js +714 -0
  35. package/build/esm/cclass/utils/assembly.js +665 -0
  36. package/build/esm/cclass/utils/base.js +127 -0
  37. package/build/esm/cclass/utils/index.js +62 -0
  38. package/build/esm/cclass/utils/part.js +144 -0
  39. package/build/esm/classcadEngine.js +30 -0
  40. package/build/esm/createApi.js +66 -0
  41. package/build/esm/createUnwrappedApi.js +39 -0
  42. package/build/esm/facade.js +98 -0
  43. package/build/esm/globals.js +150 -0
  44. package/build/esm/index.js +80 -0
  45. package/build/esm/io/AwvNodeClient.js +286 -0
  46. package/build/esm/io/SocketIOClient.js +332 -0
  47. package/build/esm/io/Transaction.js +130 -0
  48. package/build/esm/io/WASMClient.js +429 -0
  49. package/build/esm/io/WSClient.js +642 -0
  50. package/build/esm/io/engineKey.js +196 -0
  51. package/build/esm/re-export.js +3 -0
  52. package/build/esm/session/host.js +474 -0
  53. package/build/esm/session/identity.js +34 -0
  54. package/build/esm/session/selection.js +277 -0
  55. package/build/esm/session/share.js +221 -0
  56. package/build/esm/session/types.js +20 -0
  57. package/build/esm/test/SocketIOClient.spec.js +178 -0
  58. package/build/esm/types/ComClient.js +7 -0
  59. package/build/esm/utils/createRecursiveBufferGeometry.js +51 -0
  60. package/build/esm/utils/createRecursiveScene.js +136 -0
  61. package/build/esm/utils/getDifferentColoredMeshesFromEntity.js +20 -0
  62. package/build/esm/utils/index.js +4 -0
  63. package/build/esm/utils/polyline.js +60 -0
  64. package/build/esm/utils/waitForSelect.js +23 -0
  65. package/build/types/BuerliCadFacade.d.ts +410 -0
  66. package/build/types/cclass/utils/assembly.d.ts +180 -0
  67. package/build/types/cclass/utils/base.d.ts +55 -0
  68. package/build/types/cclass/utils/index.d.ts +46 -0
  69. package/build/types/cclass/utils/part.d.ts +46 -0
  70. package/build/types/classcadEngine.d.ts +40 -0
  71. package/build/types/createApi.d.ts +65 -0
  72. package/build/types/createUnwrappedApi.d.ts +36 -0
  73. package/build/types/facade.d.ts +9 -0
  74. package/build/types/globals.d.ts +79 -0
  75. package/build/types/index.d.ts +82 -0
  76. package/build/types/io/AwvNodeClient.d.ts +182 -0
  77. package/build/types/io/SocketIOClient.d.ts +84 -0
  78. package/build/types/io/Transaction.d.ts +56 -0
  79. package/build/types/io/WASMClient.d.ts +239 -0
  80. package/build/types/io/WSClient.d.ts +242 -0
  81. package/build/types/io/engineKey.d.ts +137 -0
  82. package/build/types/re-export.d.ts +6 -0
  83. package/build/types/session/host.d.ts +108 -0
  84. package/build/types/session/identity.d.ts +46 -0
  85. package/build/types/session/selection.d.ts +164 -0
  86. package/build/types/session/share.d.ts +131 -0
  87. package/build/types/session/types.d.ts +113 -0
  88. package/build/types/test/SocketIOClient.spec.d.ts +0 -0
  89. package/build/types/types/ComClient.d.ts +307 -0
  90. package/build/types/utils/createRecursiveBufferGeometry.d.ts +8 -0
  91. package/build/types/utils/createRecursiveScene.d.ts +24 -0
  92. package/build/types/utils/getDifferentColoredMeshesFromEntity.d.ts +14 -0
  93. package/build/types/utils/index.d.ts +4 -0
  94. package/build/types/utils/polyline.d.ts +43 -0
  95. package/build/types/utils/waitForSelect.d.ts +4 -0
  96. package/doc/api/advanced/connection.md +488 -0
  97. package/doc/api/clients.md +2045 -0
  98. package/doc/api/engine-keys.md +230 -0
  99. package/doc/api/index.md +58 -0
  100. package/doc/api/modeling.md +682 -0
  101. package/doc/api/other.md +33 -0
  102. package/doc/api/session-sharing.md +706 -0
  103. package/package.json +59 -4
  104. package/test/classcadEngine.test.cjs +44 -0
  105. package/test/connect.test.cjs +81 -0
  106. package/test/engineKey.test.cjs +80 -0
  107. package/test/onClient.test.cjs +82 -0
  108. package/typedoc.json +8 -0
@@ -0,0 +1,202 @@
1
+ "use strict";
2
+ // Engine keys from an access token or the app's own sign-in, so that an app carries no key.
3
+ //
4
+ // A ClassCAD account makes access tokens on its account page (classcad.ch/account):
5
+ // ccpk_… public: may sit in a web page; yields keys for the account's registered domains and localhost
6
+ // ccsk_… secret: for servers; never put it into a web page
7
+ // The token exchange turns a token into an engine key that the plan allows, with its expiry.
8
+ // Keys are cached until shortly before they expire and renewed in the background, and a cached
9
+ // key that has not expired keeps working when the service cannot be reached.
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ exports.EngineKeyError = exports.DEFAULT_KEY_URL = void 0;
12
+ exports.requestEngineKey = requestEngineKey;
13
+ exports.resolveEngineKey = resolveEngineKey;
14
+ /**
15
+ * The token exchange of the ClassCAD backend.
16
+ *
17
+ * @advanced
18
+ * @category Engine Keys
19
+ */
20
+ exports.DEFAULT_KEY_URL = 'https://europe-west1-classcad-app.cloudfunctions.net/api/v1/key';
21
+ /**
22
+ * The error when there is no key for an engine: no token, the key service cannot be reached, or it refused.
23
+ *
24
+ * {@link WASMClient.connect} rejects with it.
25
+ *
26
+ * @example
27
+ * ```ts
28
+ * import { EngineKeyError, resolveEngineKey } from '@classcad/client'
29
+ *
30
+ * // Checks a page's token before the app starts, and says why it fails
31
+ * try {
32
+ * await resolveEngineKey({ token: 'ccpk_…' })
33
+ * } catch (error) {
34
+ * if (error instanceof EngineKeyError && error.status && error.status < 500) {
35
+ * showError(`ClassCAD refused the key: ${error.message}`) // e.g. a domain the account has not registered
36
+ * } else {
37
+ * showError('The ClassCAD key service cannot be reached')
38
+ * }
39
+ * }
40
+ * ```
41
+ * @category Engine Keys
42
+ */
43
+ class EngineKeyError extends Error {
44
+ /**
45
+ * @param message - What went wrong; the key service's own words when it refused.
46
+ * @param status - The HTTP status of the key service's answer, when it answered.
47
+ * @param code - The key service's error code, when it gave one.
48
+ */
49
+ constructor(message, status, code) {
50
+ super(message);
51
+ this.status = status;
52
+ this.code = code;
53
+ }
54
+ }
55
+ exports.EngineKeyError = EngineKeyError;
56
+ // A cached key is renewed once less than a quarter of its life, or less than a day, is left
57
+ const RENEW_SHARE = 0.25;
58
+ const MIN_LEFT = 24 * 3600 * 1000;
59
+ const storage = () => {
60
+ try {
61
+ return typeof localStorage !== 'undefined' ? localStorage : null;
62
+ }
63
+ catch (error) {
64
+ return null;
65
+ }
66
+ };
67
+ // One cache entry per token, page origin and service; the token itself is not stored as a name
68
+ const cacheName = async (token, keyUrl) => {
69
+ const origin = typeof location !== 'undefined' ? location.origin : '';
70
+ const subtle = typeof crypto !== 'undefined' ? crypto.subtle : undefined;
71
+ if (!subtle)
72
+ return null;
73
+ const hash = await subtle.digest('SHA-256', new TextEncoder().encode(`${keyUrl}|${origin}|${token}`));
74
+ return ('classcad.key.' +
75
+ Array.from(new Uint8Array(hash))
76
+ .slice(0, 16)
77
+ .map(b => b.toString(16).padStart(2, '0'))
78
+ .join(''));
79
+ };
80
+ /**
81
+ * Asks the token exchange for a key.
82
+ *
83
+ * Sends `source.token` to `source.keyUrl` (default {@link DEFAULT_KEY_URL}) every time; {@link resolveEngineKey}
84
+ * caches the key. Throws an {@link EngineKeyError} without a token, when the service cannot be reached, and with
85
+ * its reason when it refuses.
86
+ *
87
+ * @param source - The access token, and optionally `keyUrl`, `appType` and `client`.
88
+ * @example
89
+ * ```ts
90
+ * import { requestEngineKey } from '@classcad/client'
91
+ *
92
+ * // The app's server: hands its signed-in users engine keys, with the secret token only it knows
93
+ * export async function engineKeyRoute(request: Request): Promise<Response> {
94
+ * if (!(await isSignedIn(request))) return new Response('Sign in first', { status: 401 })
95
+ * const key = await requestEngineKey({ token: process.env.CLASSCAD_SECRET_TOKEN, client: 'my-app server' })
96
+ * return Response.json(key) // { key, exp }, what a WASMClient's getKey returns
97
+ * }
98
+ * ```
99
+ * @advanced
100
+ * @category Engine Keys
101
+ */
102
+ async function requestEngineKey(source) {
103
+ if (!source.token)
104
+ throw new EngineKeyError('No access token');
105
+ let res;
106
+ try {
107
+ res = await fetch(source.keyUrl || exports.DEFAULT_KEY_URL, {
108
+ method: 'POST',
109
+ headers: { 'content-type': 'application/json', authorization: `Bearer ${source.token}` },
110
+ body: JSON.stringify({ appType: source.appType || 'wasm', client: source.client }),
111
+ });
112
+ }
113
+ catch (error) {
114
+ throw new EngineKeyError(`The ClassCAD key service cannot be reached: ${error.message}`);
115
+ }
116
+ const body = await res.json().catch(() => null);
117
+ if (!res.ok || !body || typeof body.key !== 'string') {
118
+ throw new EngineKeyError((body && body.message) || `The ClassCAD key service answered ${res.status}`, res.status, body === null || body === void 0 ? void 0 : body.code);
119
+ }
120
+ return { key: body.key, exp: body.exp };
121
+ }
122
+ /**
123
+ * The key for an engine start: the given key, the app's own, or one for the access token.
124
+ *
125
+ * Throws EngineKeyError when there is none, with the service's reason (an unregistered
126
+ * domain, a revoked token, a plan without the native engine).
127
+ *
128
+ * A key for a token is cached in `localStorage` (where there is one), per token, page origin and key service. It is
129
+ * used until a minute before it expires and renewed in the background once less than a quarter of its life, or less
130
+ * than a day, is left. When the service cannot be reached, a cached key that has not expired is used; a refusal
131
+ * (an HTTP 4xx) is final. {@link WASMClient} calls it on `connect`.
132
+ *
133
+ * @param source - The key, the app's `getKey`, or the access token to get one with.
134
+ * @returns The key.
135
+ * @example
136
+ * ```ts
137
+ * import { resolveEngineKey } from '@classcad/client'
138
+ *
139
+ * // A server that starts a native engine of its own, with a secret access token
140
+ * const key = await resolveEngineKey({
141
+ * token: process.env.CLASSCAD_SECRET_TOKEN,
142
+ * appType: 'native',
143
+ * client: 'render farm',
144
+ * })
145
+ * await startEngine(key)
146
+ * ```
147
+ * @category Engine Keys
148
+ */
149
+ async function resolveEngineKey(source) {
150
+ if (source.classcadKey)
151
+ return source.classcadKey;
152
+ if (source.getKey) {
153
+ const key = await source.getKey();
154
+ return typeof key === 'string' ? key : key.key;
155
+ }
156
+ if (!source.token)
157
+ throw new EngineKeyError('An engine needs a token, a getKey function or a classcadKey');
158
+ const keyUrl = source.keyUrl || exports.DEFAULT_KEY_URL;
159
+ const store = storage();
160
+ const name = store ? await cacheName(source.token, keyUrl) : null;
161
+ let cached = null;
162
+ if (store && name) {
163
+ try {
164
+ cached = JSON.parse(store.getItem(name) || 'null');
165
+ }
166
+ catch (error) {
167
+ cached = null;
168
+ }
169
+ }
170
+ const fetchAndStore = async () => {
171
+ const fresh = await requestEngineKey(Object.assign(Object.assign({}, source), { keyUrl }));
172
+ if (store && name) {
173
+ try {
174
+ store.setItem(name, JSON.stringify({ key: fresh.key, exp: fresh.exp, iat: Date.now() }));
175
+ }
176
+ catch (error) {
177
+ // a full or blocked storage only costs a fetch next time
178
+ }
179
+ }
180
+ return fresh.key;
181
+ };
182
+ const now = Date.now();
183
+ if (cached && cached.exp > now + 60 * 1000) {
184
+ const left = cached.exp - now;
185
+ const life = cached.exp - (cached.iat || now);
186
+ // The engine checks its key at start only: renewing now readies the next start
187
+ if (left < MIN_LEFT || left < life * RENEW_SHARE)
188
+ fetchAndStore().catch(() => undefined);
189
+ return cached.key;
190
+ }
191
+ try {
192
+ return await fetchAndStore();
193
+ }
194
+ catch (error) {
195
+ // Our service down, or no network: a key that has not expired still starts the engine.
196
+ // A refusal (4xx) is final: the plan or the token does not allow it any more.
197
+ const refused = error instanceof EngineKeyError && !!error.status && error.status < 500;
198
+ if (cached && cached.exp > now && !refused)
199
+ return cached.key;
200
+ throw error;
201
+ }
202
+ }
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PointTypes = exports.MeshTypes = exports.MemberType = exports.GraphicType = exports.EntityTypes = exports.EdgeTypes = exports.CurveTypes = exports.ContainerType = exports.classMap = exports.CCClasses = exports.ScgGraphicType = exports.ScgClassType = exports.MessageLevel = exports.WorkPointType = exports.WorkPlaneType = exports.WorkCoordSystemType = exports.WorkAxisType = exports.ViewType = exports.SVGModusType = exports.ReorientedType = exports.OrientationType = exports.NOID = exports.FlipType = exports.ExtrusionType = exports.DataType = exports.CLASSCAD_VERSION_QUALITY = exports.CLASSCAD_VERSION = exports.ChamferType = exports.BooleanOperationType = void 0;
4
+ var api_js_1 = require("@classcad/api-js");
5
+ Object.defineProperty(exports, "BooleanOperationType", { enumerable: true, get: function () { return api_js_1.BooleanOperationType; } });
6
+ Object.defineProperty(exports, "ChamferType", { enumerable: true, get: function () { return api_js_1.ChamferType; } });
7
+ Object.defineProperty(exports, "CLASSCAD_VERSION", { enumerable: true, get: function () { return api_js_1.CLASSCAD_VERSION; } });
8
+ Object.defineProperty(exports, "CLASSCAD_VERSION_QUALITY", { enumerable: true, get: function () { return api_js_1.CLASSCAD_VERSION_QUALITY; } });
9
+ Object.defineProperty(exports, "DataType", { enumerable: true, get: function () { return api_js_1.DataType; } });
10
+ Object.defineProperty(exports, "ExtrusionType", { enumerable: true, get: function () { return api_js_1.ExtrusionType; } });
11
+ Object.defineProperty(exports, "FlipType", { enumerable: true, get: function () { return api_js_1.FlipType; } });
12
+ Object.defineProperty(exports, "NOID", { enumerable: true, get: function () { return api_js_1.NOID; } });
13
+ Object.defineProperty(exports, "OrientationType", { enumerable: true, get: function () { return api_js_1.OrientationType; } });
14
+ Object.defineProperty(exports, "ReorientedType", { enumerable: true, get: function () { return api_js_1.ReorientedType; } });
15
+ Object.defineProperty(exports, "SVGModusType", { enumerable: true, get: function () { return api_js_1.SVGModusType; } });
16
+ Object.defineProperty(exports, "ViewType", { enumerable: true, get: function () { return api_js_1.ViewType; } });
17
+ Object.defineProperty(exports, "WorkAxisType", { enumerable: true, get: function () { return api_js_1.WorkAxisType; } });
18
+ Object.defineProperty(exports, "WorkCoordSystemType", { enumerable: true, get: function () { return api_js_1.WorkCoordSystemType; } });
19
+ Object.defineProperty(exports, "WorkPlaneType", { enumerable: true, get: function () { return api_js_1.WorkPlaneType; } });
20
+ Object.defineProperty(exports, "WorkPointType", { enumerable: true, get: function () { return api_js_1.WorkPointType; } });
21
+ var api_js_2 = require("@classcad/api-js");
22
+ Object.defineProperty(exports, "MessageLevel", { enumerable: true, get: function () { return api_js_2.MessageLevel; } });
23
+ Object.defineProperty(exports, "ScgClassType", { enumerable: true, get: function () { return api_js_2.ScgClassType; } });
24
+ Object.defineProperty(exports, "ScgGraphicType", { enumerable: true, get: function () { return api_js_2.ScgGraphicType; } });
25
+ var sdkjs_1 = require("@classcad/sdkjs");
26
+ Object.defineProperty(exports, "CCClasses", { enumerable: true, get: function () { return sdkjs_1.CCClasses; } });
27
+ Object.defineProperty(exports, "classMap", { enumerable: true, get: function () { return sdkjs_1.classMap; } });
28
+ Object.defineProperty(exports, "ContainerType", { enumerable: true, get: function () { return sdkjs_1.ContainerType; } });
29
+ Object.defineProperty(exports, "CurveTypes", { enumerable: true, get: function () { return sdkjs_1.CurveTypes; } });
30
+ Object.defineProperty(exports, "EdgeTypes", { enumerable: true, get: function () { return sdkjs_1.EdgeTypes; } });
31
+ Object.defineProperty(exports, "EntityTypes", { enumerable: true, get: function () { return sdkjs_1.EntityTypes; } });
32
+ Object.defineProperty(exports, "GraphicType", { enumerable: true, get: function () { return sdkjs_1.GraphicType; } });
33
+ Object.defineProperty(exports, "MemberType", { enumerable: true, get: function () { return sdkjs_1.MemberType; } });
34
+ Object.defineProperty(exports, "MeshTypes", { enumerable: true, get: function () { return sdkjs_1.MeshTypes; } });
35
+ Object.defineProperty(exports, "PointTypes", { enumerable: true, get: function () { return sdkjs_1.PointTypes; } });