@auscera/design-labs 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/apps/workspace/.skills/design-labs/SKILL.md +63 -0
  2. package/apps/workspace/.skills/design-labs/references/commands.md +38 -0
  3. package/apps/workspace/AGENTS.md +9 -0
  4. package/apps/workspace/app/(lab)/compare/page.tsx +2 -0
  5. package/apps/workspace/app/(lab)/lab.css +412 -0
  6. package/apps/workspace/app/(lab)/layout.tsx +21 -0
  7. package/apps/workspace/app/(lab)/page.tsx +2 -0
  8. package/apps/workspace/app/api/annotations/route.ts +94 -0
  9. package/apps/workspace/app/api/versions/events/route.ts +63 -0
  10. package/apps/workspace/app/api/versions/route.ts +7 -0
  11. package/apps/workspace/components/animated-icons.tsx +72 -0
  12. package/apps/workspace/components/dotmatrix-loader.css +1228 -0
  13. package/apps/workspace/components/glass-controls.tsx +47 -0
  14. package/apps/workspace/components/lab-providers.tsx +17 -0
  15. package/apps/workspace/components/molecules/brand-lockup.tsx +13 -0
  16. package/apps/workspace/components/molecules/pane-url-popover.css +92 -0
  17. package/apps/workspace/components/molecules/pane-url-popover.tsx +191 -0
  18. package/apps/workspace/components/pane-preferences.tsx +75 -0
  19. package/apps/workspace/components/pane-viewport.css +34 -0
  20. package/apps/workspace/components/pane-viewport.tsx +199 -0
  21. package/apps/workspace/components/preview-pane.css +293 -0
  22. package/apps/workspace/components/preview-pane.tsx +662 -0
  23. package/apps/workspace/components/ui/dotm-triangle-11.tsx +170 -0
  24. package/apps/workspace/components/ui/dotmatrix-core.tsx +1649 -0
  25. package/apps/workspace/components/ui/dotmatrix-hooks.ts +224 -0
  26. package/apps/workspace/components/version-select.tsx +220 -0
  27. package/apps/workspace/components/workspace.tsx +313 -0
  28. package/apps/workspace/eslint.config.mjs +3 -0
  29. package/apps/workspace/lab.config.json +1 -0
  30. package/apps/workspace/lib/annotation-export.ts +28 -0
  31. package/apps/workspace/lib/annotation-types.ts +38 -0
  32. package/apps/workspace/lib/device-presets.ts +103 -0
  33. package/apps/workspace/lib/use-annotation-bridge.ts +130 -0
  34. package/apps/workspace/lib/version-types.ts +12 -0
  35. package/apps/workspace/lib/website-url.ts +78 -0
  36. package/apps/workspace/next-env.d.ts +6 -0
  37. package/apps/workspace/next.config.mjs +1 -0
  38. package/apps/workspace/package.json +35 -0
  39. package/apps/workspace/public/apple-touch-icon.png +0 -0
  40. package/apps/workspace/public/brand/design-labs-script.LICENSE.txt +93 -0
  41. package/apps/workspace/public/brand/design-labs-script.woff2 +0 -0
  42. package/apps/workspace/public/brand/logo.webp +0 -0
  43. package/apps/workspace/public/brand/wordmark.css +42 -0
  44. package/apps/workspace/public/favicon.ico +0 -0
  45. package/apps/workspace/public/icon-192.png +0 -0
  46. package/apps/workspace/public/icon-512.png +0 -0
  47. package/apps/workspace/public/preview-bridge.js +411 -0
  48. package/apps/workspace/public/site.webmanifest +1 -0
  49. package/apps/workspace/scripts/annotation-store.mjs +308 -0
  50. package/apps/workspace/scripts/lab.mjs +414 -0
  51. package/apps/workspace/scripts/migrate.mjs +2 -0
  52. package/apps/workspace/scripts/prepare-preview.mjs +203 -0
  53. package/apps/workspace/tsconfig.json +24 -0
  54. package/package.json +9 -3
  55. package/scripts/install.mjs +67 -0
  56. package/scripts/update.mjs +204 -0
  57. package/README.md +0 -3
@@ -0,0 +1,308 @@
1
+ import fs from "node:fs/promises";
2
+ import path from "node:path";
3
+ import crypto from "node:crypto";
4
+
5
+ export class AnnotationError extends Error {
6
+ constructor(message, status = 400) {
7
+ super(message);
8
+ this.name = "AnnotationError";
9
+ this.status = status;
10
+ }
11
+ }
12
+ const queues = new Map();
13
+ const uuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
14
+ const record = (value) =>
15
+ value !== null && typeof value === "object" && !Array.isArray(value);
16
+ function string(value, name, max, allowEmpty = false) {
17
+ if (
18
+ typeof value !== "string" ||
19
+ value.length > max ||
20
+ (!allowEmpty && !value.trim())
21
+ )
22
+ throw new AnnotationError(`Invalid ${name}`);
23
+ return value;
24
+ }
25
+ function number(value, name, min, max) {
26
+ if (
27
+ typeof value !== "number" ||
28
+ !Number.isFinite(value) ||
29
+ value < min ||
30
+ value > max
31
+ )
32
+ throw new AnnotationError(`Invalid ${name}`);
33
+ return value;
34
+ }
35
+ // HTML is exported as inert text, never rendered. Strip active markup and form data
36
+ // again on the server, even though the preview bridge already sanitizes its clone.
37
+ export function sanitizeHtml(value) {
38
+ return value
39
+ .replace(
40
+ /<(script|style|iframe|object|embed|template|textarea)\b[^>]*>[\s\S]*?<\/\1\s*>/gi,
41
+ "",
42
+ )
43
+ .replace(
44
+ /<(script|style|iframe|object|embed|template|textarea)\b[^>]*\/?\s*>/gi,
45
+ "",
46
+ )
47
+ .replace(
48
+ /(<([a-z][a-z0-9:-]*)\b[^>]*\bcontenteditable(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s>]+))?[^>]*>)[\s\S]*?(<\/\2\s*>)/gi,
49
+ "$1$3",
50
+ )
51
+ .replace(
52
+ /\s(?:on[^\s=/>]+|value|checked|selected|srcdoc|nonce|integrity|[^\s=/>]*(?:password|secret|token|authorization|credential)[^\s=/>]*)\s*(?:=\s*(?:"[^"]*"|'[^']*'|[^\s>]+))?/gi,
53
+ "",
54
+ )
55
+ .replace(
56
+ /\s(?:href|src|action|formaction)\s*=\s*(?:"\s*(?:javascript|data):[^"]*"|'\s*(?:javascript|data):[^']*'|(?:javascript|data):[^\s>]+)/gi,
57
+ "",
58
+ )
59
+ .replace(/(<select\b[^>]*>)[\s\S]*?(<\/select\s*>)/gi, "$1$2")
60
+ .slice(0, 16000);
61
+ }
62
+ export function validateTarget(value) {
63
+ if (
64
+ !record(value) ||
65
+ !record(value.bounds) ||
66
+ !record(value.viewport) ||
67
+ !record(value.styles)
68
+ )
69
+ throw new AnnotationError("Invalid target context");
70
+ const target = {
71
+ selector: string(value.selector, "selector", 2000),
72
+ tag: string(value.tag, "element tag", 64).toLowerCase(),
73
+ text: string(value.text, "element text", 2000, true),
74
+ html: sanitizeHtml(string(value.html, "element HTML", 20000, true)),
75
+ styles: {},
76
+ bounds: {
77
+ x: number(value.bounds.x, "bounds x", -10000000, 10000000),
78
+ y: number(value.bounds.y, "bounds y", -10000000, 10000000),
79
+ width: number(value.bounds.width, "bounds width", 0, 200000),
80
+ height: number(value.bounds.height, "bounds height", 0, 200000),
81
+ },
82
+ viewport: {
83
+ width: number(value.viewport.width, "viewport width", 1, 16384),
84
+ height: number(value.viewport.height, "viewport height", 1, 16384),
85
+ },
86
+ pathname: string(value.pathname, "pathname", 2000),
87
+ };
88
+ if (
89
+ !target.pathname.startsWith("/") ||
90
+ !/^[a-z][a-z0-9:-]*$/.test(target.tag)
91
+ )
92
+ throw new AnnotationError("Invalid target location");
93
+ const styleKeys = [
94
+ "display",
95
+ "position",
96
+ "color",
97
+ "backgroundColor",
98
+ "fontFamily",
99
+ "fontSize",
100
+ "fontWeight",
101
+ "lineHeight",
102
+ "padding",
103
+ "margin",
104
+ "borderRadius",
105
+ "width",
106
+ "height",
107
+ "gap",
108
+ "alignItems",
109
+ "justifyContent",
110
+ ];
111
+ for (const key of styleKeys)
112
+ if (Object.hasOwn(value.styles, key))
113
+ target.styles[key] = string(
114
+ value.styles[key],
115
+ "computed style",
116
+ 500,
117
+ true,
118
+ );
119
+ if (value.component !== undefined)
120
+ target.component = string(value.component, "component", 160);
121
+ if (value.sourceFile !== undefined)
122
+ target.sourceFile = string(value.sourceFile, "source file", 1000);
123
+ return target;
124
+ }
125
+ export async function registeredVersion(root, versionKey) {
126
+ string(versionKey, "version key", 300);
127
+ const config = JSON.parse(
128
+ await fs.readFile(path.join(root, "lab.config.json"), "utf8"),
129
+ );
130
+ if (
131
+ !Array.isArray(config.versions) ||
132
+ !config.versions.some((version) => version.key === versionKey)
133
+ )
134
+ throw new AnnotationError("Unknown website version", 404);
135
+ }
136
+ async function filename(root, versionKey, create = false) {
137
+ await registeredVersion(root, versionKey);
138
+ const directory = path.join(root, "annotations");
139
+ if (create) await fs.mkdir(directory, { recursive: true, mode: 0o700 });
140
+ try {
141
+ if ((await fs.lstat(directory)).isSymbolicLink())
142
+ throw new AnnotationError(
143
+ "Annotation directory cannot be a symlink",
144
+ 409,
145
+ );
146
+ } catch (error) {
147
+ if (error.code !== "ENOENT") throw error;
148
+ }
149
+ const file = path.join(
150
+ directory,
151
+ crypto.createHash("sha256").update(versionKey).digest("hex") + ".json",
152
+ );
153
+ try {
154
+ if ((await fs.lstat(file)).isSymbolicLink())
155
+ throw new AnnotationError("Annotation file cannot be a symlink", 409);
156
+ } catch (error) {
157
+ if (error.code !== "ENOENT") throw error;
158
+ }
159
+ return file;
160
+ }
161
+ async function readFile(file, versionKey) {
162
+ try {
163
+ const data = JSON.parse(await fs.readFile(file, "utf8"));
164
+ if (
165
+ data.schemaVersion !== 1 ||
166
+ data.versionKey !== versionKey ||
167
+ !Array.isArray(data.annotations) ||
168
+ data.annotations.length > 1000
169
+ )
170
+ throw new Error("Invalid annotation file");
171
+ const ids = new Set();
172
+ return data.annotations.map((annotation) => {
173
+ if (
174
+ !record(annotation) ||
175
+ typeof annotation.id !== "string" ||
176
+ !uuid.test(annotation.id) ||
177
+ ids.has(annotation.id) ||
178
+ annotation.versionKey !== versionKey ||
179
+ typeof annotation.createdAt !== "string" ||
180
+ !Number.isFinite(Date.parse(annotation.createdAt)) ||
181
+ typeof annotation.updatedAt !== "string" ||
182
+ !Number.isFinite(Date.parse(annotation.updatedAt))
183
+ )
184
+ throw new Error("Invalid annotation file");
185
+ ids.add(annotation.id);
186
+ return {
187
+ id: annotation.id,
188
+ versionKey,
189
+ comment: string(annotation.comment, "stored comment", 8000),
190
+ target: validateTarget(annotation.target),
191
+ createdAt: annotation.createdAt,
192
+ updatedAt: annotation.updatedAt,
193
+ };
194
+ });
195
+ } catch (error) {
196
+ if (error.code === "ENOENT") return [];
197
+ throw error;
198
+ }
199
+ }
200
+ export async function listAnnotations(root, versionKey) {
201
+ return readFile(await filename(root, versionKey), versionKey);
202
+ }
203
+ async function locked(root, versionKey, task) {
204
+ const key = path.resolve(root) + "\0" + versionKey;
205
+ const before = queues.get(key) || Promise.resolve();
206
+ const next = before.catch(() => {}).then(task);
207
+ queues.set(key, next);
208
+ try {
209
+ return await next;
210
+ } finally {
211
+ if (queues.get(key) === next) queues.delete(key);
212
+ }
213
+ }
214
+ async function persist(file, versionKey, annotations) {
215
+ const temporary = `${file}.${crypto.randomUUID()}.tmp`;
216
+ try {
217
+ await fs.writeFile(
218
+ temporary,
219
+ JSON.stringify({ schemaVersion: 1, versionKey, annotations }, null, 2) +
220
+ "\n",
221
+ { mode: 0o600, flag: "wx" },
222
+ );
223
+ await fs.rename(temporary, file);
224
+ } finally {
225
+ await fs.rm(temporary, { force: true });
226
+ }
227
+ }
228
+ export async function upsertAnnotation(root, input) {
229
+ if (!record(input)) throw new AnnotationError("Invalid annotation payload");
230
+ const versionKey = string(input.versionKey, "version key", 300);
231
+ const comment = string(input.comment, "comment", 8000).trim();
232
+ const target = validateTarget(input.target);
233
+ if (
234
+ input.id !== undefined &&
235
+ (typeof input.id !== "string" || !uuid.test(input.id))
236
+ )
237
+ throw new AnnotationError("Invalid annotation ID");
238
+ return locked(root, versionKey, async () => {
239
+ const file = await filename(root, versionKey, true);
240
+ const annotations = await readFile(file, versionKey);
241
+ const index = input.id
242
+ ? annotations.findIndex((annotation) => annotation.id === input.id)
243
+ : -1;
244
+ if (input.id && index < 0)
245
+ throw new AnnotationError("Annotation not found in this version", 404);
246
+ if (index < 0 && annotations.length >= 1000)
247
+ throw new AnnotationError("Annotation limit reached", 409);
248
+ const timestamp = new Date().toISOString();
249
+ const annotation = {
250
+ id: input.id || crypto.randomUUID(),
251
+ versionKey,
252
+ comment,
253
+ target,
254
+ createdAt: index >= 0 ? annotations[index].createdAt : timestamp,
255
+ updatedAt: timestamp,
256
+ };
257
+ if (index >= 0) annotations[index] = annotation;
258
+ else annotations.push(annotation);
259
+ await persist(file, versionKey, annotations);
260
+ return annotation;
261
+ });
262
+ }
263
+ export async function deleteAnnotations(root, input) {
264
+ if (!record(input)) throw new AnnotationError("Invalid deletion payload");
265
+ const versionKey = string(input.versionKey, "version key", 300);
266
+ if (
267
+ !Array.isArray(input.ids) ||
268
+ input.ids.length > 1000 ||
269
+ !input.ids.every((id) => typeof id === "string" && uuid.test(id))
270
+ )
271
+ throw new AnnotationError("Invalid annotation IDs");
272
+ return locked(root, versionKey, async () => {
273
+ const file = await filename(root, versionKey, true);
274
+ const annotations = await readFile(file, versionKey);
275
+ const ids = new Set(input.ids);
276
+ const kept = annotations.filter((annotation) => !ids.has(annotation.id));
277
+ await persist(file, versionKey, kept);
278
+ return { deleted: annotations.length - kept.length };
279
+ });
280
+ }
281
+ export function checkWriteOrigin(request) {
282
+ const origin = request.headers.get("origin");
283
+ // Next may expose its internal localhost URL even when the browser used
284
+ // 127.0.0.1. The incoming Host header is the browser-facing authority.
285
+ const url = new URL(request.url);
286
+ const host = request.headers.get("host") || url.host;
287
+ let expected;
288
+ try {
289
+ expected = new URL(`${url.protocol}//${host}`);
290
+ } catch {
291
+ throw new AnnotationError("Invalid annotation host", 403);
292
+ }
293
+ if (
294
+ !["localhost", "127.0.0.1", "[::1]"].includes(expected.hostname) ||
295
+ !origin ||
296
+ origin !== expected.origin
297
+ )
298
+ throw new AnnotationError(
299
+ "Cross-origin annotation writes are not allowed",
300
+ 403,
301
+ );
302
+ const site = request.headers.get("sec-fetch-site");
303
+ if (site && !["same-origin", "none"].includes(site))
304
+ throw new AnnotationError(
305
+ "Cross-origin annotation writes are not allowed",
306
+ 403,
307
+ );
308
+ }