@pixeyedev/storybook 0.4.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.
@@ -0,0 +1,6 @@
1
+ export interface BundleResult {
2
+ hash: string;
3
+ buffer: Buffer;
4
+ sizeBytes: number;
5
+ }
6
+ export declare const createBundle: (dir: string) => Promise<BundleResult>;
package/dist/bundle.js ADDED
@@ -0,0 +1,34 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { readFile } from 'node:fs/promises';
3
+ import { join } from 'node:path';
4
+ import { Readable } from 'node:stream';
5
+ import { createGzip } from 'node:zlib';
6
+ import { glob } from 'glob';
7
+ export const createBundle = async (dir) => {
8
+ const files = await glob('**/*', { cwd: dir, nodir: true });
9
+ files.sort(); // Deterministic ordering
10
+ // Build a content map
11
+ const contentMap = {};
12
+ for (const file of files) {
13
+ const content = await readFile(join(dir, file));
14
+ contentMap[file] = content.toString('base64');
15
+ }
16
+ // Serialize deterministically (sorted keys)
17
+ const json = JSON.stringify(contentMap, Object.keys(contentMap).sort());
18
+ // Gzip compress
19
+ const compressed = await new Promise((resolve, reject) => {
20
+ const chunks = [];
21
+ const gzip = createGzip({ level: 9 });
22
+ gzip.on('data', (chunk) => chunks.push(chunk));
23
+ gzip.on('end', () => resolve(Buffer.concat(chunks)));
24
+ gzip.on('error', reject);
25
+ Readable.from(json).pipe(gzip);
26
+ });
27
+ // Content hash for dedup
28
+ const hash = createHash('sha256').update(compressed).digest('hex');
29
+ return {
30
+ hash,
31
+ buffer: compressed,
32
+ sizeBytes: compressed.length
33
+ };
34
+ };
@@ -0,0 +1,6 @@
1
+ export type { BundleResult } from './bundle.js';
2
+ export { createBundle } from './bundle.js';
3
+ export type { UploadBundleOptions, UploadBundleResult } from './upload.js';
4
+ export { uploadBundle } from './upload.js';
5
+ export type { BundleValidation } from './validate.js';
6
+ export { validateBundle } from './validate.js';
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { createBundle } from './bundle.js';
2
+ export { uploadBundle } from './upload.js';
3
+ export { validateBundle } from './validate.js';
@@ -0,0 +1,27 @@
1
+ export interface UploadBundleOptions {
2
+ serverUrl: string;
3
+ token: string;
4
+ commitSha: string;
5
+ branch: string;
6
+ hash: string;
7
+ buffer: Buffer;
8
+ storyCount: number;
9
+ componentCount: number;
10
+ }
11
+ export interface UploadBundleResult {
12
+ buildUrl: string;
13
+ storyCount: number;
14
+ skipped: boolean;
15
+ }
16
+ /**
17
+ * Upload a Storybook bundle to Pixeye for server-side rendering.
18
+ *
19
+ * Uses the existing storybook build registration endpoint. The full
20
+ * presigned bundle upload path (POST /storybook/uploads -> presigned PUT
21
+ * -> POST /storybook/uploads/:id/finalize) will be implemented when the
22
+ * storybook-renderer worker is built (Task 15.1).
23
+ *
24
+ * For now, this registers the build metadata so it appears in the
25
+ * Pixeye dashboard. Server-side rendering is not yet available.
26
+ */
27
+ export declare const uploadBundle: (opts: UploadBundleOptions) => Promise<UploadBundleResult>;
package/dist/upload.js ADDED
@@ -0,0 +1,41 @@
1
+ import { PixeyeError } from '@pixeyedev/core';
2
+ /**
3
+ * Upload a Storybook bundle to Pixeye for server-side rendering.
4
+ *
5
+ * Uses the existing storybook build registration endpoint. The full
6
+ * presigned bundle upload path (POST /storybook/uploads -> presigned PUT
7
+ * -> POST /storybook/uploads/:id/finalize) will be implemented when the
8
+ * storybook-renderer worker is built (Task 15.1).
9
+ *
10
+ * For now, this registers the build metadata so it appears in the
11
+ * Pixeye dashboard. Server-side rendering is not yet available.
12
+ */
13
+ export const uploadBundle = async (opts) => {
14
+ const baseUrl = opts.serverUrl.replace(/\/$/, '');
15
+ // Register the storybook build
16
+ const res = await fetch(`${baseUrl}/api/v1/storybook/builds`, {
17
+ method: 'POST',
18
+ headers: {
19
+ 'Content-Type': 'application/json',
20
+ Authorization: `Bearer ${opts.token}`
21
+ },
22
+ body: JSON.stringify({
23
+ projectToken: opts.token,
24
+ commitSha: opts.commitSha,
25
+ branch: opts.branch,
26
+ fileCount: opts.storyCount,
27
+ totalBytes: opts.buffer.length,
28
+ storagePrefix: `storybook-bundle/${opts.hash}/`
29
+ })
30
+ });
31
+ if (!res.ok) {
32
+ const text = await res.text();
33
+ throw new PixeyeError(res.status, '/api/v1/storybook/builds', text);
34
+ }
35
+ const data = (await res.json());
36
+ return {
37
+ buildUrl: data.storybookUrl ?? `${baseUrl}/storybook/${data.build.id}`,
38
+ storyCount: opts.storyCount,
39
+ skipped: false
40
+ };
41
+ };
@@ -0,0 +1,6 @@
1
+ export interface BundleValidation {
2
+ storyCount: number;
3
+ componentCount: number;
4
+ totalSizeBytes: number;
5
+ }
6
+ export declare const validateBundle: (dir: string) => Promise<BundleValidation>;
@@ -0,0 +1,53 @@
1
+ import { readFile, stat } from 'node:fs/promises';
2
+ import { join } from 'node:path';
3
+ import { glob } from 'glob';
4
+ export const validateBundle = async (dir) => {
5
+ // Check required files
6
+ const indexHtml = join(dir, 'index.html');
7
+ const iframeHtml = join(dir, 'iframe.html');
8
+ try {
9
+ await stat(indexHtml);
10
+ }
11
+ catch {
12
+ throw new Error('Not a valid Storybook build. Missing index.html. Run "build-storybook" first.');
13
+ }
14
+ try {
15
+ await stat(iframeHtml);
16
+ }
17
+ catch {
18
+ throw new Error('Not a valid Storybook build. Missing iframe.html.');
19
+ }
20
+ // Find story manifest (Storybook 7+)
21
+ const indexJsonPath = join(dir, 'index.json');
22
+ const storiesJsonPath = join(dir, 'stories.json');
23
+ let manifestContent;
24
+ try {
25
+ manifestContent = await readFile(indexJsonPath, 'utf-8');
26
+ }
27
+ catch {
28
+ try {
29
+ manifestContent = await readFile(storiesJsonPath, 'utf-8');
30
+ }
31
+ catch {
32
+ throw new Error('Storybook 7+ required (index.json not found). Upgrade Storybook or use @pixeyedev/playwright for manual capture.');
33
+ }
34
+ }
35
+ // Parse manifest to count stories and components
36
+ const manifest = JSON.parse(manifestContent);
37
+ // Storybook 7 index.json format: { v: 5, entries: { [storyId]: { type, title, name, ... } } }
38
+ // Storybook 6 stories.json format: { v: 3, stories: { [storyId]: { ... } } }
39
+ const entries = manifest.entries ?? manifest.stories ?? {};
40
+ const storyEntries = Object.values(entries).filter((entry) => !entry.type || entry.type === 'story');
41
+ const storyCount = storyEntries.length;
42
+ // Components are unique titles (e.g., "Design System/Button")
43
+ const componentTitles = new Set(storyEntries.map((entry) => entry.title).filter(Boolean));
44
+ const componentCount = componentTitles.size;
45
+ // Calculate total size
46
+ const allFiles = await glob('**/*', { cwd: dir, nodir: true });
47
+ let totalSizeBytes = 0;
48
+ for (const file of allFiles) {
49
+ const fileStat = await stat(join(dir, file));
50
+ totalSizeBytes += fileStat.size;
51
+ }
52
+ return { storyCount, componentCount, totalSizeBytes };
53
+ };
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@pixeyedev/storybook",
3
+ "version": "0.4.0",
4
+ "private": false,
5
+ "type": "module",
6
+ "files": [
7
+ "dist"
8
+ ],
9
+ "main": "dist/index.js",
10
+ "types": "dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "types": "./src/index.ts",
14
+ "import": "./dist/index.js"
15
+ }
16
+ },
17
+ "scripts": {
18
+ "build": "tsc",
19
+ "typecheck": "tsc --noEmit",
20
+ "test": "vitest run",
21
+ "lint": "echo 'ok'",
22
+ "ai-check": "exit 0"
23
+ },
24
+ "dependencies": {
25
+ "@pixeyedev/core": "^0.4.0",
26
+ "@pixeyedev/shared": "^0.4.0",
27
+ "glob": "^11.0.0"
28
+ },
29
+ "devDependencies": {
30
+ "@types/node": "^22.0.0",
31
+ "typescript": "^5.7.0"
32
+ }
33
+ }