@obra-studio/figma-console-mcp 1.32.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/LICENSE +21 -0
- package/README.md +879 -0
- package/dist/apps/design-system-dashboard/scoring/accessibility.d.ts +14 -0
- package/dist/apps/design-system-dashboard/scoring/accessibility.d.ts.map +1 -0
- package/dist/apps/design-system-dashboard/scoring/accessibility.js +278 -0
- package/dist/apps/design-system-dashboard/scoring/accessibility.js.map +1 -0
- package/dist/apps/design-system-dashboard/scoring/component-metadata.d.ts +29 -0
- package/dist/apps/design-system-dashboard/scoring/component-metadata.d.ts.map +1 -0
- package/dist/apps/design-system-dashboard/scoring/component-metadata.js +358 -0
- package/dist/apps/design-system-dashboard/scoring/component-metadata.js.map +1 -0
- package/dist/apps/design-system-dashboard/scoring/consistency.d.ts +14 -0
- package/dist/apps/design-system-dashboard/scoring/consistency.d.ts.map +1 -0
- package/dist/apps/design-system-dashboard/scoring/consistency.js +342 -0
- package/dist/apps/design-system-dashboard/scoring/consistency.js.map +1 -0
- package/dist/apps/design-system-dashboard/scoring/coverage.d.ts +14 -0
- package/dist/apps/design-system-dashboard/scoring/coverage.d.ts.map +1 -0
- package/dist/apps/design-system-dashboard/scoring/coverage.js +231 -0
- package/dist/apps/design-system-dashboard/scoring/coverage.js.map +1 -0
- package/dist/apps/design-system-dashboard/scoring/engine.d.ts +27 -0
- package/dist/apps/design-system-dashboard/scoring/engine.d.ts.map +1 -0
- package/dist/apps/design-system-dashboard/scoring/engine.js +93 -0
- package/dist/apps/design-system-dashboard/scoring/engine.js.map +1 -0
- package/dist/apps/design-system-dashboard/scoring/naming-semantics.d.ts +14 -0
- package/dist/apps/design-system-dashboard/scoring/naming-semantics.d.ts.map +1 -0
- package/dist/apps/design-system-dashboard/scoring/naming-semantics.js +309 -0
- package/dist/apps/design-system-dashboard/scoring/naming-semantics.js.map +1 -0
- package/dist/apps/design-system-dashboard/scoring/token-architecture.d.ts +14 -0
- package/dist/apps/design-system-dashboard/scoring/token-architecture.d.ts.map +1 -0
- package/dist/apps/design-system-dashboard/scoring/token-architecture.js +350 -0
- package/dist/apps/design-system-dashboard/scoring/token-architecture.js.map +1 -0
- package/dist/apps/design-system-dashboard/scoring/types.d.ts +89 -0
- package/dist/apps/design-system-dashboard/scoring/types.d.ts.map +1 -0
- package/dist/apps/design-system-dashboard/scoring/types.js +41 -0
- package/dist/apps/design-system-dashboard/scoring/types.js.map +1 -0
- package/dist/apps/design-system-dashboard/server.d.ts +24 -0
- package/dist/apps/design-system-dashboard/server.d.ts.map +1 -0
- package/dist/apps/design-system-dashboard/server.js +160 -0
- package/dist/apps/design-system-dashboard/server.js.map +1 -0
- package/dist/apps/token-browser/server.d.ts +26 -0
- package/dist/apps/token-browser/server.d.ts.map +1 -0
- package/dist/apps/token-browser/server.js +137 -0
- package/dist/apps/token-browser/server.js.map +1 -0
- package/dist/browser/base.d.ts +58 -0
- package/dist/browser/base.d.ts.map +1 -0
- package/dist/browser/base.js +6 -0
- package/dist/browser/base.js.map +1 -0
- package/dist/browser/local.d.ts +87 -0
- package/dist/browser/local.d.ts.map +1 -0
- package/dist/browser/local.js +318 -0
- package/dist/browser/local.js.map +1 -0
- package/dist/core/accessibility-tools.d.ts +21 -0
- package/dist/core/accessibility-tools.d.ts.map +1 -0
- package/dist/core/accessibility-tools.js +307 -0
- package/dist/core/accessibility-tools.js.map +1 -0
- package/dist/core/annotation-tools.d.ts +14 -0
- package/dist/core/annotation-tools.d.ts.map +1 -0
- package/dist/core/annotation-tools.js +231 -0
- package/dist/core/annotation-tools.js.map +1 -0
- package/dist/core/autodocs-tools.d.ts +7 -0
- package/dist/core/autodocs-tools.d.ts.map +1 -0
- package/dist/core/autodocs-tools.js +195 -0
- package/dist/core/autodocs-tools.js.map +1 -0
- package/dist/core/comment-tools.d.ts +11 -0
- package/dist/core/comment-tools.d.ts.map +1 -0
- package/dist/core/comment-tools.js +293 -0
- package/dist/core/comment-tools.js.map +1 -0
- package/dist/core/config.d.ts +17 -0
- package/dist/core/config.d.ts.map +1 -0
- package/dist/core/config.js +154 -0
- package/dist/core/config.js.map +1 -0
- package/dist/core/console-monitor.d.ts +82 -0
- package/dist/core/console-monitor.d.ts.map +1 -0
- package/dist/core/console-monitor.js +428 -0
- package/dist/core/console-monitor.js.map +1 -0
- package/dist/core/deep-component-tools.d.ts +14 -0
- package/dist/core/deep-component-tools.d.ts.map +1 -0
- package/dist/core/deep-component-tools.js +129 -0
- package/dist/core/deep-component-tools.js.map +1 -0
- package/dist/core/design-code-tools.d.ts +116 -0
- package/dist/core/design-code-tools.d.ts.map +1 -0
- package/dist/core/design-code-tools.js +2751 -0
- package/dist/core/design-code-tools.js.map +1 -0
- package/dist/core/design-system-manifest.d.ts +272 -0
- package/dist/core/design-system-manifest.d.ts.map +1 -0
- package/dist/core/design-system-manifest.js +261 -0
- package/dist/core/design-system-manifest.js.map +1 -0
- package/dist/core/design-system-tools.d.ts +67 -0
- package/dist/core/design-system-tools.d.ts.map +1 -0
- package/dist/core/design-system-tools.js +874 -0
- package/dist/core/design-system-tools.js.map +1 -0
- package/dist/core/diagnose-tool.d.ts +33 -0
- package/dist/core/diagnose-tool.d.ts.map +1 -0
- package/dist/core/diagnose-tool.js +97 -0
- package/dist/core/diagnose-tool.js.map +1 -0
- package/dist/core/diff/changelog-formatter.d.ts +35 -0
- package/dist/core/diff/changelog-formatter.d.ts.map +1 -0
- package/dist/core/diff/changelog-formatter.js +276 -0
- package/dist/core/diff/changelog-formatter.js.map +1 -0
- package/dist/core/diff/diff-engine.d.ts +127 -0
- package/dist/core/diff/diff-engine.d.ts.map +1 -0
- package/dist/core/diff/diff-engine.js +335 -0
- package/dist/core/diff/diff-engine.js.map +1 -0
- package/dist/core/diff/property-compare.d.ts +19 -0
- package/dist/core/diff/property-compare.d.ts.map +1 -0
- package/dist/core/diff/property-compare.js +37 -0
- package/dist/core/diff/property-compare.js.map +1 -0
- package/dist/core/diff/version-cache.d.ts +40 -0
- package/dist/core/diff/version-cache.d.ts.map +1 -0
- package/dist/core/diff/version-cache.js +75 -0
- package/dist/core/diff/version-cache.js.map +1 -0
- package/dist/core/enrichment/enrichment-service.d.ts +52 -0
- package/dist/core/enrichment/enrichment-service.d.ts.map +1 -0
- package/dist/core/enrichment/enrichment-service.js +369 -0
- package/dist/core/enrichment/enrichment-service.js.map +1 -0
- package/dist/core/enrichment/index.d.ts +8 -0
- package/dist/core/enrichment/index.d.ts.map +1 -0
- package/dist/core/enrichment/index.js +8 -0
- package/dist/core/enrichment/index.js.map +1 -0
- package/dist/core/enrichment/relationship-mapper.d.ts +106 -0
- package/dist/core/enrichment/relationship-mapper.d.ts.map +1 -0
- package/dist/core/enrichment/relationship-mapper.js +352 -0
- package/dist/core/enrichment/relationship-mapper.js.map +1 -0
- package/dist/core/enrichment/style-resolver.d.ts +80 -0
- package/dist/core/enrichment/style-resolver.d.ts.map +1 -0
- package/dist/core/enrichment/style-resolver.js +327 -0
- package/dist/core/enrichment/style-resolver.js.map +1 -0
- package/dist/core/figjam-tools.d.ts +8 -0
- package/dist/core/figjam-tools.d.ts.map +1 -0
- package/dist/core/figjam-tools.js +548 -0
- package/dist/core/figjam-tools.js.map +1 -0
- package/dist/core/figma-api.d.ts +245 -0
- package/dist/core/figma-api.d.ts.map +1 -0
- package/dist/core/figma-api.js +446 -0
- package/dist/core/figma-api.js.map +1 -0
- package/dist/core/figma-connector.d.ts +180 -0
- package/dist/core/figma-connector.d.ts.map +1 -0
- package/dist/core/figma-connector.js +8 -0
- package/dist/core/figma-connector.js.map +1 -0
- package/dist/core/figma-desktop-connector.d.ts +312 -0
- package/dist/core/figma-desktop-connector.d.ts.map +1 -0
- package/dist/core/figma-desktop-connector.js +1298 -0
- package/dist/core/figma-desktop-connector.js.map +1 -0
- package/dist/core/figma-reconstruction-spec.d.ts +166 -0
- package/dist/core/figma-reconstruction-spec.d.ts.map +1 -0
- package/dist/core/figma-reconstruction-spec.js +403 -0
- package/dist/core/figma-reconstruction-spec.js.map +1 -0
- package/dist/core/figma-style-extractor.d.ts +76 -0
- package/dist/core/figma-style-extractor.d.ts.map +1 -0
- package/dist/core/figma-style-extractor.js +312 -0
- package/dist/core/figma-style-extractor.js.map +1 -0
- package/dist/core/figma-tools.d.ts +22 -0
- package/dist/core/figma-tools.d.ts.map +1 -0
- package/dist/core/figma-tools.js +3187 -0
- package/dist/core/figma-tools.js.map +1 -0
- package/dist/core/identity.d.ts +41 -0
- package/dist/core/identity.d.ts.map +1 -0
- package/dist/core/identity.js +97 -0
- package/dist/core/identity.js.map +1 -0
- package/dist/core/library-tools.d.ts +17 -0
- package/dist/core/library-tools.d.ts.map +1 -0
- package/dist/core/library-tools.js +581 -0
- package/dist/core/library-tools.js.map +1 -0
- package/dist/core/logger.d.ts +22 -0
- package/dist/core/logger.d.ts.map +1 -0
- package/dist/core/logger.js +54 -0
- package/dist/core/logger.js.map +1 -0
- package/dist/core/port-discovery.d.ts +171 -0
- package/dist/core/port-discovery.d.ts.map +1 -0
- package/dist/core/port-discovery.js +563 -0
- package/dist/core/port-discovery.js.map +1 -0
- package/dist/core/resolve-package-root.d.ts +2 -0
- package/dist/core/resolve-package-root.d.ts.map +1 -0
- package/dist/core/resolve-package-root.js +12 -0
- package/dist/core/resolve-package-root.js.map +1 -0
- package/dist/core/slides-tools.d.ts +8 -0
- package/dist/core/slides-tools.d.ts.map +1 -0
- package/dist/core/slides-tools.js +715 -0
- package/dist/core/slides-tools.js.map +1 -0
- package/dist/core/snippet-injector.d.ts +24 -0
- package/dist/core/snippet-injector.d.ts.map +1 -0
- package/dist/core/snippet-injector.js +97 -0
- package/dist/core/snippet-injector.js.map +1 -0
- package/dist/core/tokens/alias-resolver.d.ts +55 -0
- package/dist/core/tokens/alias-resolver.d.ts.map +1 -0
- package/dist/core/tokens/alias-resolver.js +136 -0
- package/dist/core/tokens/alias-resolver.js.map +1 -0
- package/dist/core/tokens/config.d.ts +87 -0
- package/dist/core/tokens/config.d.ts.map +1 -0
- package/dist/core/tokens/config.js +285 -0
- package/dist/core/tokens/config.js.map +1 -0
- package/dist/core/tokens/figma-converter.d.ts +81 -0
- package/dist/core/tokens/figma-converter.d.ts.map +1 -0
- package/dist/core/tokens/figma-converter.js +196 -0
- package/dist/core/tokens/figma-converter.js.map +1 -0
- package/dist/core/tokens/formatters/css-vars.d.ts +24 -0
- package/dist/core/tokens/formatters/css-vars.d.ts.map +1 -0
- package/dist/core/tokens/formatters/css-vars.js +330 -0
- package/dist/core/tokens/formatters/css-vars.js.map +1 -0
- package/dist/core/tokens/formatters/dtcg.d.ts +28 -0
- package/dist/core/tokens/formatters/dtcg.d.ts.map +1 -0
- package/dist/core/tokens/formatters/dtcg.js +301 -0
- package/dist/core/tokens/formatters/dtcg.js.map +1 -0
- package/dist/core/tokens/formatters/index.d.ts +30 -0
- package/dist/core/tokens/formatters/index.d.ts.map +1 -0
- package/dist/core/tokens/formatters/index.js +46 -0
- package/dist/core/tokens/formatters/index.js.map +1 -0
- package/dist/core/tokens/formatters/json.d.ts +37 -0
- package/dist/core/tokens/formatters/json.d.ts.map +1 -0
- package/dist/core/tokens/formatters/json.js +188 -0
- package/dist/core/tokens/formatters/json.js.map +1 -0
- package/dist/core/tokens/formatters/less.d.ts +4 -0
- package/dist/core/tokens/formatters/less.d.ts.map +1 -0
- package/dist/core/tokens/formatters/less.js +5 -0
- package/dist/core/tokens/formatters/less.js.map +1 -0
- package/dist/core/tokens/formatters/scss.d.ts +26 -0
- package/dist/core/tokens/formatters/scss.d.ts.map +1 -0
- package/dist/core/tokens/formatters/scss.js +253 -0
- package/dist/core/tokens/formatters/scss.js.map +1 -0
- package/dist/core/tokens/formatters/stubs.d.ts +9 -0
- package/dist/core/tokens/formatters/stubs.d.ts.map +1 -0
- package/dist/core/tokens/formatters/stubs.js +14 -0
- package/dist/core/tokens/formatters/stubs.js.map +1 -0
- package/dist/core/tokens/formatters/style-dictionary-v3.d.ts +45 -0
- package/dist/core/tokens/formatters/style-dictionary-v3.d.ts.map +1 -0
- package/dist/core/tokens/formatters/style-dictionary-v3.js +208 -0
- package/dist/core/tokens/formatters/style-dictionary-v3.js.map +1 -0
- package/dist/core/tokens/formatters/tailwind-v3.d.ts +37 -0
- package/dist/core/tokens/formatters/tailwind-v3.d.ts.map +1 -0
- package/dist/core/tokens/formatters/tailwind-v3.js +238 -0
- package/dist/core/tokens/formatters/tailwind-v3.js.map +1 -0
- package/dist/core/tokens/formatters/tailwind-v4.d.ts +41 -0
- package/dist/core/tokens/formatters/tailwind-v4.d.ts.map +1 -0
- package/dist/core/tokens/formatters/tailwind-v4.js +331 -0
- package/dist/core/tokens/formatters/tailwind-v4.js.map +1 -0
- package/dist/core/tokens/formatters/tokens-studio.d.ts +44 -0
- package/dist/core/tokens/formatters/tokens-studio.d.ts.map +1 -0
- package/dist/core/tokens/formatters/tokens-studio.js +251 -0
- package/dist/core/tokens/formatters/tokens-studio.js.map +1 -0
- package/dist/core/tokens/formatters/ts-module.d.ts +35 -0
- package/dist/core/tokens/formatters/ts-module.d.ts.map +1 -0
- package/dist/core/tokens/formatters/ts-module.js +199 -0
- package/dist/core/tokens/formatters/ts-module.js.map +1 -0
- package/dist/core/tokens/index.d.ts +17 -0
- package/dist/core/tokens/index.d.ts.map +1 -0
- package/dist/core/tokens/index.js +16 -0
- package/dist/core/tokens/index.js.map +1 -0
- package/dist/core/tokens/parsers/css-vars.d.ts +3 -0
- package/dist/core/tokens/parsers/css-vars.d.ts.map +1 -0
- package/dist/core/tokens/parsers/css-vars.js +5 -0
- package/dist/core/tokens/parsers/css-vars.js.map +1 -0
- package/dist/core/tokens/parsers/dtcg.d.ts +21 -0
- package/dist/core/tokens/parsers/dtcg.d.ts.map +1 -0
- package/dist/core/tokens/parsers/dtcg.js +254 -0
- package/dist/core/tokens/parsers/dtcg.js.map +1 -0
- package/dist/core/tokens/parsers/index.d.ts +37 -0
- package/dist/core/tokens/parsers/index.d.ts.map +1 -0
- package/dist/core/tokens/parsers/index.js +139 -0
- package/dist/core/tokens/parsers/index.js.map +1 -0
- package/dist/core/tokens/parsers/json.d.ts +4 -0
- package/dist/core/tokens/parsers/json.d.ts.map +1 -0
- package/dist/core/tokens/parsers/json.js +8 -0
- package/dist/core/tokens/parsers/json.js.map +1 -0
- package/dist/core/tokens/parsers/scss.d.ts +3 -0
- package/dist/core/tokens/parsers/scss.d.ts.map +1 -0
- package/dist/core/tokens/parsers/scss.js +5 -0
- package/dist/core/tokens/parsers/scss.js.map +1 -0
- package/dist/core/tokens/parsers/stubs.d.ts +15 -0
- package/dist/core/tokens/parsers/stubs.d.ts.map +1 -0
- package/dist/core/tokens/parsers/stubs.js +21 -0
- package/dist/core/tokens/parsers/stubs.js.map +1 -0
- package/dist/core/tokens/parsers/style-dictionary-v3.d.ts +3 -0
- package/dist/core/tokens/parsers/style-dictionary-v3.d.ts.map +1 -0
- package/dist/core/tokens/parsers/style-dictionary-v3.js +5 -0
- package/dist/core/tokens/parsers/style-dictionary-v3.js.map +1 -0
- package/dist/core/tokens/parsers/tailwind-v3.d.ts +3 -0
- package/dist/core/tokens/parsers/tailwind-v3.d.ts.map +1 -0
- package/dist/core/tokens/parsers/tailwind-v3.js +5 -0
- package/dist/core/tokens/parsers/tailwind-v3.js.map +1 -0
- package/dist/core/tokens/parsers/tailwind-v4.d.ts +3 -0
- package/dist/core/tokens/parsers/tailwind-v4.d.ts.map +1 -0
- package/dist/core/tokens/parsers/tailwind-v4.js +5 -0
- package/dist/core/tokens/parsers/tailwind-v4.js.map +1 -0
- package/dist/core/tokens/parsers/tokens-studio.d.ts +3 -0
- package/dist/core/tokens/parsers/tokens-studio.d.ts.map +1 -0
- package/dist/core/tokens/parsers/tokens-studio.js +5 -0
- package/dist/core/tokens/parsers/tokens-studio.js.map +1 -0
- package/dist/core/tokens/schemas.d.ts +31 -0
- package/dist/core/tokens/schemas.d.ts.map +1 -0
- package/dist/core/tokens/schemas.js +149 -0
- package/dist/core/tokens/schemas.js.map +1 -0
- package/dist/core/tokens/transforms/color.d.ts +9 -0
- package/dist/core/tokens/transforms/color.d.ts.map +1 -0
- package/dist/core/tokens/transforms/color.js +13 -0
- package/dist/core/tokens/transforms/color.js.map +1 -0
- package/dist/core/tokens/transforms/index.d.ts +36 -0
- package/dist/core/tokens/transforms/index.d.ts.map +1 -0
- package/dist/core/tokens/transforms/index.js +30 -0
- package/dist/core/tokens/transforms/index.js.map +1 -0
- package/dist/core/tokens/transforms/size.d.ts +7 -0
- package/dist/core/tokens/transforms/size.d.ts.map +1 -0
- package/dist/core/tokens/transforms/size.js +8 -0
- package/dist/core/tokens/transforms/size.js.map +1 -0
- package/dist/core/tokens/types.d.ts +228 -0
- package/dist/core/tokens/types.d.ts.map +1 -0
- package/dist/core/tokens/types.js +19 -0
- package/dist/core/tokens/types.js.map +1 -0
- package/dist/core/tokens-tools.d.ts +42 -0
- package/dist/core/tokens-tools.d.ts.map +1 -0
- package/dist/core/tokens-tools.js +860 -0
- package/dist/core/tokens-tools.js.map +1 -0
- package/dist/core/types/design-code.d.ts +271 -0
- package/dist/core/types/design-code.d.ts.map +1 -0
- package/dist/core/types/design-code.js +5 -0
- package/dist/core/types/design-code.js.map +1 -0
- package/dist/core/types/enriched.d.ts +213 -0
- package/dist/core/types/enriched.d.ts.map +1 -0
- package/dist/core/types/enriched.js +6 -0
- package/dist/core/types/enriched.js.map +1 -0
- package/dist/core/types/index.d.ts +104 -0
- package/dist/core/types/index.d.ts.map +1 -0
- package/dist/core/types/index.js +5 -0
- package/dist/core/types/index.js.map +1 -0
- package/dist/core/variable-resolver.d.ts +45 -0
- package/dist/core/variable-resolver.d.ts.map +1 -0
- package/dist/core/variable-resolver.js +86 -0
- package/dist/core/variable-resolver.js.map +1 -0
- package/dist/core/version-tools.d.ts +59 -0
- package/dist/core/version-tools.d.ts.map +1 -0
- package/dist/core/version-tools.js +1159 -0
- package/dist/core/version-tools.js.map +1 -0
- package/dist/core/websocket-connector.d.ts +187 -0
- package/dist/core/websocket-connector.d.ts.map +1 -0
- package/dist/core/websocket-connector.js +378 -0
- package/dist/core/websocket-connector.js.map +1 -0
- package/dist/core/websocket-server.js +866 -0
- package/dist/core/websocket-server.js.map +1 -0
- package/dist/core/write-tools.d.ts +7 -0
- package/dist/core/write-tools.d.ts.map +1 -0
- package/dist/core/write-tools.js +2172 -0
- package/dist/core/write-tools.js.map +1 -0
- package/dist/local.d.ts +95 -0
- package/dist/local.d.ts.map +1 -0
- package/dist/local.js +3036 -0
- package/dist/local.js.map +1 -0
- package/dist/vendor/obra-autodocs/autodocs-body.generated.d.ts +2 -0
- package/dist/vendor/obra-autodocs/autodocs-body.generated.d.ts.map +1 -0
- package/dist/vendor/obra-autodocs/autodocs-body.generated.js +6 -0
- package/dist/vendor/obra-autodocs/autodocs-body.generated.js.map +1 -0
- package/figma-desktop-bridge/README.md +365 -0
- package/figma-desktop-bridge/code.js +6504 -0
- package/figma-desktop-bridge/icon.png +0 -0
- package/figma-desktop-bridge/manifest.json +67 -0
- package/figma-desktop-bridge/ui.html +2441 -0
- package/package.json +98 -0
|
@@ -0,0 +1,866 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebSocket Bridge Server (Multi-Client)
|
|
3
|
+
*
|
|
4
|
+
* Creates a WebSocket server that multiple Desktop Bridge plugin instances connect to.
|
|
5
|
+
* Each instance represents a different Figma file and is identified by its fileKey
|
|
6
|
+
* (sent via FILE_INFO on connection). Per-file state (selection, document changes,
|
|
7
|
+
* console logs) is maintained independently.
|
|
8
|
+
*
|
|
9
|
+
* Active file tracking: The "active" file is automatically switched when the user
|
|
10
|
+
* interacts with a file (selection/page changes) or can be set explicitly via
|
|
11
|
+
* setActiveFile(). All backward-compatible getters return data from the active file.
|
|
12
|
+
*
|
|
13
|
+
* Data flow: MCP Server ←WebSocket→ ui.html ←postMessage→ code.js ←figma.*→ Figma
|
|
14
|
+
*/
|
|
15
|
+
import { WebSocketServer as WSServer, WebSocket } from 'ws';
|
|
16
|
+
import { EventEmitter } from 'events';
|
|
17
|
+
import { createServer as createHttpServer } from 'http';
|
|
18
|
+
import { readFileSync } from 'fs';
|
|
19
|
+
import { join } from 'path';
|
|
20
|
+
import { createChildLogger } from './logger.js';
|
|
21
|
+
import { PACKAGE_ROOT } from './resolve-package-root.js';
|
|
22
|
+
// Read version from package.json using the resolved package root.
|
|
23
|
+
// PACKAGE_ROOT uses import.meta.url in ESM (production) and __dirname in CJS (Jest).
|
|
24
|
+
let SERVER_VERSION = '0.0.0';
|
|
25
|
+
try {
|
|
26
|
+
SERVER_VERSION = JSON.parse(readFileSync(join(PACKAGE_ROOT, 'package.json'), 'utf-8')).version;
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
// Non-critical — version will show as 0.0.0
|
|
30
|
+
}
|
|
31
|
+
const logger = createChildLogger({ component: 'websocket-server' });
|
|
32
|
+
export class FigmaWebSocketServer extends EventEmitter {
|
|
33
|
+
constructor(options) {
|
|
34
|
+
super();
|
|
35
|
+
this.wss = null;
|
|
36
|
+
this.httpServer = null;
|
|
37
|
+
/** Named clients indexed by fileKey — each represents a connected Figma file */
|
|
38
|
+
this.clients = new Map();
|
|
39
|
+
/** Clients awaiting FILE_INFO identification, mapped to their pending timeout */
|
|
40
|
+
this._pendingClients = new Map();
|
|
41
|
+
/** The fileKey of the currently active (targeted) file */
|
|
42
|
+
this._activeFileKey = null;
|
|
43
|
+
this.pendingRequests = new Map();
|
|
44
|
+
this.requestIdCounter = 0;
|
|
45
|
+
this._isStarted = false;
|
|
46
|
+
this._startedAt = Date.now();
|
|
47
|
+
this.consoleBufferSize = 1000;
|
|
48
|
+
this.documentChangeBufferSize = 200;
|
|
49
|
+
/** Heartbeat interval for detecting dead connections via ping/pong */
|
|
50
|
+
this._heartbeatInterval = null;
|
|
51
|
+
this.options = options;
|
|
52
|
+
this._startedAt = Date.now();
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Handle HTTP requests on the same port as WebSocket.
|
|
56
|
+
* Serves a JSON `/health` (also at `/`) endpoint with version and connected-file
|
|
57
|
+
* info. No plugin-UI route exists — the plugin loads its own `ui.html` from disk
|
|
58
|
+
* via the Figma plugin runtime; the legacy `/plugin/ui` bootloader endpoint was
|
|
59
|
+
* removed in the Phase 3 cleanup.
|
|
60
|
+
*/
|
|
61
|
+
handleHttpRequest(req, res) {
|
|
62
|
+
// CORS headers for Figma plugin iframe (sandboxed, origin: null)
|
|
63
|
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
64
|
+
res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
|
|
65
|
+
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
|
|
66
|
+
if (req.method === 'OPTIONS') {
|
|
67
|
+
res.writeHead(204);
|
|
68
|
+
res.end();
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const url = req.url || '/';
|
|
72
|
+
// Health/version endpoint
|
|
73
|
+
if (url === '/health' || url === '/') {
|
|
74
|
+
const now = Date.now();
|
|
75
|
+
const connectedClients = Array.from(this.clients.values()).filter(c => c.ws.readyState === WebSocket.OPEN && (now - c.lastPongAt) < 90000).length;
|
|
76
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
77
|
+
res.end(JSON.stringify({
|
|
78
|
+
status: 'ok',
|
|
79
|
+
version: SERVER_VERSION,
|
|
80
|
+
clients: this.clients.size,
|
|
81
|
+
connectedClients,
|
|
82
|
+
uptime: Math.floor((now - this._startedAt) / 1000),
|
|
83
|
+
}));
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
// 404 for anything else
|
|
87
|
+
res.writeHead(404, { 'Content-Type': 'text/plain' });
|
|
88
|
+
res.end('Not Found');
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Start the HTTP + WebSocket server.
|
|
92
|
+
* HTTP serves the plugin UI content; WebSocket handles plugin communication.
|
|
93
|
+
*/
|
|
94
|
+
async start() {
|
|
95
|
+
if (this._isStarted)
|
|
96
|
+
return;
|
|
97
|
+
return new Promise((resolve, reject) => {
|
|
98
|
+
let rejected = false;
|
|
99
|
+
const rejectOnce = (error) => {
|
|
100
|
+
if (!rejected) {
|
|
101
|
+
rejected = true;
|
|
102
|
+
reject(error);
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
try {
|
|
106
|
+
// Create HTTP server first — handles plugin UI requests
|
|
107
|
+
this.httpServer = createHttpServer((req, res) => this.handleHttpRequest(req, res));
|
|
108
|
+
// Attach WebSocket server to the HTTP server (shares the same port)
|
|
109
|
+
this.wss = new WSServer({
|
|
110
|
+
server: this.httpServer,
|
|
111
|
+
maxPayload: 100 * 1024 * 1024,
|
|
112
|
+
verifyClient: (info, callback) => {
|
|
113
|
+
// Mitigate Cross-Site WebSocket Hijacking (CSWSH):
|
|
114
|
+
// Reject connections from unexpected browser origins.
|
|
115
|
+
const origin = info.origin;
|
|
116
|
+
const allowed = !origin || // No origin — local process (e.g. Node.js client)
|
|
117
|
+
origin === 'null' || // Sandboxed iframe / Figma Desktop plugin UI
|
|
118
|
+
origin.startsWith('https://www.figma.com') ||
|
|
119
|
+
origin.startsWith('https://figma.com');
|
|
120
|
+
if (allowed) {
|
|
121
|
+
callback(true);
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
logger.warn({ origin }, 'Rejected WebSocket connection from unauthorized origin');
|
|
125
|
+
callback(false, 403, 'Unauthorized Origin');
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
// Error handler for startup failures (EADDRINUSE, etc.)
|
|
130
|
+
// Must be on BOTH httpServer and wss — the WSS re-emits HTTP server errors
|
|
131
|
+
// and throws if no listener is attached.
|
|
132
|
+
const onStartupError = (error) => {
|
|
133
|
+
if (!this._isStarted) {
|
|
134
|
+
try {
|
|
135
|
+
if (this.wss) {
|
|
136
|
+
this.wss.close();
|
|
137
|
+
this.wss = null;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
catch { /* ignore */ }
|
|
141
|
+
try {
|
|
142
|
+
if (this.httpServer) {
|
|
143
|
+
this.httpServer.close();
|
|
144
|
+
this.httpServer = null;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
catch { /* ignore */ }
|
|
148
|
+
rejectOnce(error);
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
logger.error({ error }, 'HTTP/WebSocket server error');
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
this.httpServer.on('error', onStartupError);
|
|
155
|
+
this.wss.on('error', onStartupError);
|
|
156
|
+
// Start listening on the HTTP server (which also handles WS upgrades)
|
|
157
|
+
this.httpServer.listen(this.options.port, this.options.host || 'localhost', () => {
|
|
158
|
+
this._isStarted = true;
|
|
159
|
+
this.startHeartbeat();
|
|
160
|
+
logger.info({ port: this.options.port, host: this.options.host || 'localhost' }, 'WebSocket bridge server started (with HTTP plugin UI endpoint)');
|
|
161
|
+
resolve();
|
|
162
|
+
});
|
|
163
|
+
this.wss.on('connection', (ws) => {
|
|
164
|
+
// Add to pending until FILE_INFO identifies the file
|
|
165
|
+
const pendingTimeout = setTimeout(() => {
|
|
166
|
+
if (this._pendingClients.has(ws)) {
|
|
167
|
+
this._pendingClients.delete(ws);
|
|
168
|
+
logger.warn('Pending WebSocket client timed out without sending FILE_INFO');
|
|
169
|
+
ws.close(1000, 'File identification timeout');
|
|
170
|
+
}
|
|
171
|
+
}, 30000);
|
|
172
|
+
this._pendingClients.set(ws, pendingTimeout);
|
|
173
|
+
// Send server identity to the client for debugging and logging
|
|
174
|
+
try {
|
|
175
|
+
ws.send(JSON.stringify({
|
|
176
|
+
type: 'SERVER_HELLO',
|
|
177
|
+
data: {
|
|
178
|
+
port: this.wss.address()?.port,
|
|
179
|
+
pid: process.pid,
|
|
180
|
+
serverVersion: SERVER_VERSION,
|
|
181
|
+
startedAt: this._startedAt,
|
|
182
|
+
},
|
|
183
|
+
}));
|
|
184
|
+
}
|
|
185
|
+
catch {
|
|
186
|
+
// Non-critical — client will still work without SERVER_HELLO
|
|
187
|
+
}
|
|
188
|
+
logger.info({ totalClients: this.clients.size, pendingClients: this._pendingClients.size }, 'New WebSocket connection (pending file identification)');
|
|
189
|
+
ws.on('message', (data) => {
|
|
190
|
+
try {
|
|
191
|
+
let text;
|
|
192
|
+
if (typeof data === 'string') {
|
|
193
|
+
text = data;
|
|
194
|
+
}
|
|
195
|
+
else if (Buffer.isBuffer(data)) {
|
|
196
|
+
text = data.toString();
|
|
197
|
+
}
|
|
198
|
+
else if (Array.isArray(data)) {
|
|
199
|
+
text = Buffer.concat(data).toString();
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
text = Buffer.from(data).toString();
|
|
203
|
+
}
|
|
204
|
+
const message = JSON.parse(text);
|
|
205
|
+
this.handleMessage(message, ws);
|
|
206
|
+
}
|
|
207
|
+
catch (error) {
|
|
208
|
+
logger.error({ error }, 'Failed to parse WebSocket message');
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
ws.on('close', (code, reason) => {
|
|
212
|
+
this.handleClientDisconnect(ws, code, reason.toString());
|
|
213
|
+
});
|
|
214
|
+
ws.on('error', (error) => {
|
|
215
|
+
logger.error({ error }, 'WebSocket client error');
|
|
216
|
+
});
|
|
217
|
+
// Track pong responses for heartbeat-based liveness detection.
|
|
218
|
+
// Browser WebSocket clients auto-respond to pings per RFC 6455 —
|
|
219
|
+
// no plugin-side code changes needed.
|
|
220
|
+
ws.isAlive = true;
|
|
221
|
+
ws.on('pong', () => {
|
|
222
|
+
ws.isAlive = true;
|
|
223
|
+
// Update lastPongAt on the named client if identified
|
|
224
|
+
const found = this.findClientByWs(ws);
|
|
225
|
+
if (found) {
|
|
226
|
+
found.client.lastPongAt = Date.now();
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
catch (error) {
|
|
232
|
+
rejectOnce(error);
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Find a named client connection by its WebSocket reference
|
|
238
|
+
*/
|
|
239
|
+
findClientByWs(ws) {
|
|
240
|
+
for (const [fileKey, client] of this.clients) {
|
|
241
|
+
if (client.ws === ws)
|
|
242
|
+
return { fileKey, client };
|
|
243
|
+
}
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Handle incoming message from a plugin UI WebSocket connection
|
|
248
|
+
*/
|
|
249
|
+
handleMessage(message, ws) {
|
|
250
|
+
// Response to a command we sent
|
|
251
|
+
if (message.id && this.pendingRequests.has(message.id)) {
|
|
252
|
+
const pending = this.pendingRequests.get(message.id);
|
|
253
|
+
clearTimeout(pending.timeoutId);
|
|
254
|
+
this.pendingRequests.delete(message.id);
|
|
255
|
+
if (message.error) {
|
|
256
|
+
pending.reject(new Error(message.error));
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
pending.resolve(message.result);
|
|
260
|
+
}
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
// Unsolicited data from plugin (FILE_INFO, events, forwarded data)
|
|
264
|
+
if (message.type) {
|
|
265
|
+
// FILE_INFO promotes pending clients to named clients
|
|
266
|
+
if (message.type === 'FILE_INFO' && message.data) {
|
|
267
|
+
this.handleFileInfo(message.data, ws);
|
|
268
|
+
}
|
|
269
|
+
// Buffer document changes for the specific file
|
|
270
|
+
if (message.type === 'DOCUMENT_CHANGE' && message.data) {
|
|
271
|
+
const found = this.findClientByWs(ws);
|
|
272
|
+
if (found) {
|
|
273
|
+
const entry = {
|
|
274
|
+
hasStyleChanges: message.data.hasStyleChanges,
|
|
275
|
+
hasNodeChanges: message.data.hasNodeChanges,
|
|
276
|
+
changedNodeIds: message.data.changedNodeIds || [],
|
|
277
|
+
changeCount: message.data.changeCount || 0,
|
|
278
|
+
timestamp: message.data.timestamp || Date.now(),
|
|
279
|
+
};
|
|
280
|
+
found.client.documentChanges.push(entry);
|
|
281
|
+
if (found.client.documentChanges.length > this.documentChangeBufferSize) {
|
|
282
|
+
found.client.documentChanges.shift();
|
|
283
|
+
}
|
|
284
|
+
found.client.lastActivity = Date.now();
|
|
285
|
+
}
|
|
286
|
+
this.emit('documentChange', { fileKey: found?.fileKey ?? null, ...message.data });
|
|
287
|
+
}
|
|
288
|
+
// v1.25.0: buffer description/annotation changes for the specific file.
|
|
289
|
+
// These are the diff-engine blind spots that Figma REST doesn't expose;
|
|
290
|
+
// the diff engine consults this buffer when correlating to a version
|
|
291
|
+
// range, so changes made while the plugin was connected become visible.
|
|
292
|
+
if (message.type === 'METADATA_CHANGE' && message.data) {
|
|
293
|
+
const found = this.findClientByWs(ws);
|
|
294
|
+
if (found) {
|
|
295
|
+
const changes = Array.isArray(message.data.changes) ? message.data.changes : [];
|
|
296
|
+
for (const c of changes) {
|
|
297
|
+
if (!c || typeof c.node_id !== 'string' || !c.field)
|
|
298
|
+
continue;
|
|
299
|
+
const entry = {
|
|
300
|
+
node_id: c.node_id,
|
|
301
|
+
node_name: c.node_name ?? null,
|
|
302
|
+
node_type: c.node_type ?? null,
|
|
303
|
+
field: c.field === 'annotations' ? 'annotations' : 'description',
|
|
304
|
+
new_value: c.new_value,
|
|
305
|
+
timestamp: typeof c.timestamp === 'number' ? c.timestamp : Date.now(),
|
|
306
|
+
};
|
|
307
|
+
found.client.metadataChanges.push(entry);
|
|
308
|
+
if (found.client.metadataChanges.length > this.documentChangeBufferSize) {
|
|
309
|
+
found.client.metadataChanges.shift();
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
found.client.lastActivity = Date.now();
|
|
313
|
+
}
|
|
314
|
+
this.emit('metadataChange', { fileKey: found?.fileKey ?? null, changes: message.data.changes });
|
|
315
|
+
}
|
|
316
|
+
// Track selection changes — user interaction makes this the active file
|
|
317
|
+
if (message.type === 'SELECTION_CHANGE' && message.data) {
|
|
318
|
+
const found = this.findClientByWs(ws);
|
|
319
|
+
if (found) {
|
|
320
|
+
found.client.selection = message.data;
|
|
321
|
+
found.client.lastActivity = Date.now();
|
|
322
|
+
this._activeFileKey = found.fileKey;
|
|
323
|
+
}
|
|
324
|
+
this.emit('selectionChange', { fileKey: found?.fileKey ?? null, ...message.data });
|
|
325
|
+
}
|
|
326
|
+
// Track page changes — user interaction makes this the active file
|
|
327
|
+
if (message.type === 'PAGE_CHANGE' && message.data) {
|
|
328
|
+
const found = this.findClientByWs(ws);
|
|
329
|
+
if (found) {
|
|
330
|
+
found.client.fileInfo.currentPage = message.data.pageName;
|
|
331
|
+
found.client.fileInfo.currentPageId = message.data.pageId || null;
|
|
332
|
+
found.client.lastActivity = Date.now();
|
|
333
|
+
this._activeFileKey = found.fileKey;
|
|
334
|
+
}
|
|
335
|
+
this.emit('pageChange', { fileKey: found?.fileKey ?? null, ...message.data });
|
|
336
|
+
}
|
|
337
|
+
// Capture console logs for the specific file
|
|
338
|
+
if (message.type === 'CONSOLE_CAPTURE' && message.data) {
|
|
339
|
+
const found = this.findClientByWs(ws);
|
|
340
|
+
const data = message.data;
|
|
341
|
+
const entry = {
|
|
342
|
+
timestamp: data.timestamp || Date.now(),
|
|
343
|
+
level: data.level || 'log',
|
|
344
|
+
message: typeof data.message === 'string' ? data.message.substring(0, 1000) : String(data.message),
|
|
345
|
+
args: Array.isArray(data.args) ? data.args.slice(0, 10) : [],
|
|
346
|
+
source: 'plugin',
|
|
347
|
+
};
|
|
348
|
+
if (found) {
|
|
349
|
+
found.client.consoleLogs.push(entry);
|
|
350
|
+
if (found.client.consoleLogs.length > this.consoleBufferSize) {
|
|
351
|
+
found.client.consoleLogs.shift();
|
|
352
|
+
}
|
|
353
|
+
found.client.lastActivity = Date.now();
|
|
354
|
+
}
|
|
355
|
+
this.emit('consoleLog', entry);
|
|
356
|
+
}
|
|
357
|
+
this.emit('pluginMessage', message);
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
logger.debug({ message }, 'Unhandled WebSocket message');
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Handle FILE_INFO message — promotes pending clients to named clients.
|
|
364
|
+
* This is the critical multi-client identification step: each plugin reports
|
|
365
|
+
* its fileKey on connect, allowing the server to track multiple files.
|
|
366
|
+
*/
|
|
367
|
+
handleFileInfo(data, ws) {
|
|
368
|
+
const fileKey = data.fileKey || null;
|
|
369
|
+
if (!fileKey) {
|
|
370
|
+
logger.warn('FILE_INFO received without fileKey — client remains pending');
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
// Remove from pending clients (cancel identification timeout)
|
|
374
|
+
const pendingTimeout = this._pendingClients.get(ws);
|
|
375
|
+
if (pendingTimeout) {
|
|
376
|
+
clearTimeout(pendingTimeout);
|
|
377
|
+
this._pendingClients.delete(ws);
|
|
378
|
+
}
|
|
379
|
+
// Check if this ws was already registered under a different fileKey
|
|
380
|
+
// (shouldn't happen in practice — each plugin instance is per-file)
|
|
381
|
+
const previousEntry = this.findClientByWs(ws);
|
|
382
|
+
if (previousEntry && previousEntry.fileKey !== fileKey) {
|
|
383
|
+
this.clients.delete(previousEntry.fileKey);
|
|
384
|
+
if (this._activeFileKey === previousEntry.fileKey) {
|
|
385
|
+
this._activeFileKey = null;
|
|
386
|
+
}
|
|
387
|
+
logger.info({ oldFileKey: previousEntry.fileKey, newFileKey: fileKey }, 'WebSocket client switched files');
|
|
388
|
+
}
|
|
389
|
+
// If same fileKey already connected with a DIFFERENT ws, clean up old connection
|
|
390
|
+
const existing = this.clients.get(fileKey);
|
|
391
|
+
if (existing && existing.ws !== ws) {
|
|
392
|
+
logger.info({ fileKey }, 'Replacing existing connection for same file');
|
|
393
|
+
if (existing.gracePeriodTimer) {
|
|
394
|
+
clearTimeout(existing.gracePeriodTimer);
|
|
395
|
+
}
|
|
396
|
+
// Reject any in-flight commands before replacing — the old ws close event
|
|
397
|
+
// won't find this fileKey in the map after overwrite, so pending requests
|
|
398
|
+
// would hang until timeout otherwise.
|
|
399
|
+
this.rejectPendingRequestsForFile(fileKey, 'Connection replaced by same file reconnection');
|
|
400
|
+
if (existing.ws.readyState === WebSocket.OPEN || existing.ws.readyState === WebSocket.CONNECTING) {
|
|
401
|
+
existing.ws.close(1000, 'Replaced by same file reconnection');
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
// Create client connection (preserve per-file state from previous connection of same file)
|
|
405
|
+
this.clients.set(fileKey, {
|
|
406
|
+
ws,
|
|
407
|
+
fileInfo: {
|
|
408
|
+
fileName: data.fileName,
|
|
409
|
+
fileKey,
|
|
410
|
+
currentPage: data.currentPage,
|
|
411
|
+
currentPageId: data.currentPageId || null,
|
|
412
|
+
editorType: data.editorType || 'figma',
|
|
413
|
+
connectedAt: Date.now(),
|
|
414
|
+
},
|
|
415
|
+
selection: existing?.selection || null,
|
|
416
|
+
documentChanges: existing?.documentChanges || [],
|
|
417
|
+
metadataChanges: existing?.metadataChanges || [],
|
|
418
|
+
consoleLogs: existing?.consoleLogs || [],
|
|
419
|
+
lastActivity: Date.now(),
|
|
420
|
+
lastPongAt: Date.now(),
|
|
421
|
+
gracePeriodTimer: null,
|
|
422
|
+
});
|
|
423
|
+
// Most recently connected file becomes active (user just opened the plugin there).
|
|
424
|
+
// On bulk reconnect the order is non-deterministic, but the first user interaction
|
|
425
|
+
// (SELECTION_CHANGE or PAGE_CHANGE) will correct the active file immediately.
|
|
426
|
+
this._activeFileKey = fileKey;
|
|
427
|
+
logger.info({
|
|
428
|
+
fileName: data.fileName,
|
|
429
|
+
fileKey,
|
|
430
|
+
totalClients: this.clients.size,
|
|
431
|
+
isActive: this._activeFileKey === fileKey,
|
|
432
|
+
}, 'File connected via WebSocket');
|
|
433
|
+
// Emit both events for backward compat and new features
|
|
434
|
+
this.emit('connected');
|
|
435
|
+
this.emit('fileConnected', { fileKey, fileName: data.fileName });
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* Handle a client WebSocket disconnecting.
|
|
439
|
+
* Starts a grace period before removing the client to allow reconnection.
|
|
440
|
+
*/
|
|
441
|
+
handleClientDisconnect(ws, code, reason) {
|
|
442
|
+
// Check if it was a pending client (never identified itself)
|
|
443
|
+
const pendingTimeout = this._pendingClients.get(ws);
|
|
444
|
+
if (pendingTimeout) {
|
|
445
|
+
clearTimeout(pendingTimeout);
|
|
446
|
+
this._pendingClients.delete(ws);
|
|
447
|
+
logger.info('Pending WebSocket client disconnected before file identification');
|
|
448
|
+
this.emit('disconnected');
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
// Find which named client this belongs to
|
|
452
|
+
const found = this.findClientByWs(ws);
|
|
453
|
+
if (!found) {
|
|
454
|
+
logger.debug('Unknown WebSocket client disconnected');
|
|
455
|
+
this.emit('disconnected');
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
const { fileKey, client } = found;
|
|
459
|
+
logger.info({ fileKey, fileName: client.fileInfo.fileName, code, reason }, 'File disconnected from WebSocket');
|
|
460
|
+
// Start grace period — keep state but clean up if not reconnected
|
|
461
|
+
client.gracePeriodTimer = setTimeout(() => {
|
|
462
|
+
client.gracePeriodTimer = null;
|
|
463
|
+
// Only remove if the client in the map is still the disconnected one
|
|
464
|
+
const current = this.clients.get(fileKey);
|
|
465
|
+
if (current && current.ws === ws) {
|
|
466
|
+
this.clients.delete(fileKey);
|
|
467
|
+
this.rejectPendingRequestsForFile(fileKey, 'WebSocket client disconnected');
|
|
468
|
+
// If active file disconnected, switch to another connected file
|
|
469
|
+
if (this._activeFileKey === fileKey) {
|
|
470
|
+
this._activeFileKey = null;
|
|
471
|
+
for (const [fk, c] of this.clients) {
|
|
472
|
+
if (c.ws.readyState === WebSocket.OPEN) {
|
|
473
|
+
this._activeFileKey = fk;
|
|
474
|
+
break;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
this.emit('fileDisconnected', { fileKey, fileName: client.fileInfo.fileName });
|
|
479
|
+
}
|
|
480
|
+
}, 5000);
|
|
481
|
+
this.emit('disconnected');
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* Send a command to a plugin UI and wait for the response.
|
|
485
|
+
* By default targets the active file. Pass targetFileKey to target a specific file.
|
|
486
|
+
*/
|
|
487
|
+
sendCommand(method, params = {}, timeoutMs = 15000, targetFileKey) {
|
|
488
|
+
return new Promise((resolve, reject) => {
|
|
489
|
+
const fileKey = targetFileKey || this._activeFileKey;
|
|
490
|
+
if (!fileKey) {
|
|
491
|
+
reject(new Error('No active file connected. Make sure the Desktop Bridge plugin is open in Figma.'));
|
|
492
|
+
return;
|
|
493
|
+
}
|
|
494
|
+
const client = this.clients.get(fileKey);
|
|
495
|
+
if (!client || client.ws.readyState !== WebSocket.OPEN) {
|
|
496
|
+
reject(new Error('No WebSocket client connected. Make sure the Desktop Bridge plugin is open in Figma.'));
|
|
497
|
+
return;
|
|
498
|
+
}
|
|
499
|
+
const id = `ws_${++this.requestIdCounter}_${Date.now()}`;
|
|
500
|
+
const timeoutId = setTimeout(() => {
|
|
501
|
+
if (this.pendingRequests.has(id)) {
|
|
502
|
+
this.pendingRequests.delete(id);
|
|
503
|
+
reject(new Error(`WebSocket command ${method} timed out after ${timeoutMs}ms`));
|
|
504
|
+
}
|
|
505
|
+
}, timeoutMs);
|
|
506
|
+
this.pendingRequests.set(id, {
|
|
507
|
+
resolve,
|
|
508
|
+
reject,
|
|
509
|
+
method,
|
|
510
|
+
timeoutId,
|
|
511
|
+
createdAt: Date.now(),
|
|
512
|
+
targetFileKey: fileKey,
|
|
513
|
+
});
|
|
514
|
+
const message = JSON.stringify({ id, method, params });
|
|
515
|
+
try {
|
|
516
|
+
client.ws.send(message);
|
|
517
|
+
}
|
|
518
|
+
catch (sendError) {
|
|
519
|
+
this.pendingRequests.delete(id);
|
|
520
|
+
clearTimeout(timeoutId);
|
|
521
|
+
reject(new Error(`Failed to send WebSocket command ${method}: ${sendError instanceof Error ? sendError.message : String(sendError)}`));
|
|
522
|
+
return;
|
|
523
|
+
}
|
|
524
|
+
client.lastActivity = Date.now();
|
|
525
|
+
logger.debug({ id, method, fileKey }, 'Sent WebSocket command');
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* Start the heartbeat interval that pings all connected clients every 30s.
|
|
530
|
+
* Detects silently dropped connections (e.g., macOS sleep, network change)
|
|
531
|
+
* that the OS TCP keepalive would take 30-120s to catch.
|
|
532
|
+
* Browser WebSocket clients auto-respond to pings per RFC 6455.
|
|
533
|
+
*/
|
|
534
|
+
startHeartbeat() {
|
|
535
|
+
this._heartbeatInterval = setInterval(() => {
|
|
536
|
+
if (!this.wss)
|
|
537
|
+
return;
|
|
538
|
+
for (const ws of this.wss.clients) {
|
|
539
|
+
if (ws.isAlive === false) {
|
|
540
|
+
logger.info('Terminating unresponsive WebSocket client (missed heartbeat pong)');
|
|
541
|
+
ws.terminate();
|
|
542
|
+
continue;
|
|
543
|
+
}
|
|
544
|
+
ws.isAlive = false;
|
|
545
|
+
ws.ping();
|
|
546
|
+
}
|
|
547
|
+
}, 30000);
|
|
548
|
+
}
|
|
549
|
+
/**
|
|
550
|
+
* Check if any named client is connected (transport availability check).
|
|
551
|
+
* Checks both socket readyState and heartbeat pong freshness to avoid
|
|
552
|
+
* reporting phantom-connected state on silently dropped connections.
|
|
553
|
+
*/
|
|
554
|
+
isClientConnected() {
|
|
555
|
+
const now = Date.now();
|
|
556
|
+
for (const [, client] of this.clients) {
|
|
557
|
+
if (client.ws.readyState === WebSocket.OPEN && (now - client.lastPongAt) < 90000) {
|
|
558
|
+
return true;
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
return false;
|
|
562
|
+
}
|
|
563
|
+
/**
|
|
564
|
+
* Whether the server has been started
|
|
565
|
+
*/
|
|
566
|
+
isStarted() {
|
|
567
|
+
return this._isStarted;
|
|
568
|
+
}
|
|
569
|
+
/**
|
|
570
|
+
* Get the bound address info (port, host, family).
|
|
571
|
+
* Only available after the server has started listening.
|
|
572
|
+
* Returns the actual port — critical when using port 0 for OS-assigned ports.
|
|
573
|
+
*/
|
|
574
|
+
address() {
|
|
575
|
+
// Use the HTTP server's address (which is the actual listening socket)
|
|
576
|
+
if (this.httpServer) {
|
|
577
|
+
const addr = this.httpServer.address();
|
|
578
|
+
if (typeof addr === 'string' || !addr)
|
|
579
|
+
return null;
|
|
580
|
+
return addr;
|
|
581
|
+
}
|
|
582
|
+
// Fallback for backward compat
|
|
583
|
+
if (!this.wss)
|
|
584
|
+
return null;
|
|
585
|
+
const addr = this.wss.address();
|
|
586
|
+
if (typeof addr === 'string')
|
|
587
|
+
return null;
|
|
588
|
+
return addr;
|
|
589
|
+
}
|
|
590
|
+
// ============================================================================
|
|
591
|
+
// Active file getters (backward compatible — return active file's state)
|
|
592
|
+
// ============================================================================
|
|
593
|
+
/**
|
|
594
|
+
* Get info about the currently active Figma file.
|
|
595
|
+
* Returns null if no file is active or connected.
|
|
596
|
+
*/
|
|
597
|
+
getConnectedFileInfo() {
|
|
598
|
+
if (!this._activeFileKey)
|
|
599
|
+
return null;
|
|
600
|
+
const client = this.clients.get(this._activeFileKey);
|
|
601
|
+
return client?.fileInfo || null;
|
|
602
|
+
}
|
|
603
|
+
/**
|
|
604
|
+
* Get the current user selection in the active Figma file
|
|
605
|
+
*/
|
|
606
|
+
getCurrentSelection() {
|
|
607
|
+
if (!this._activeFileKey)
|
|
608
|
+
return null;
|
|
609
|
+
const client = this.clients.get(this._activeFileKey);
|
|
610
|
+
return client?.selection || null;
|
|
611
|
+
}
|
|
612
|
+
/**
|
|
613
|
+
* Get buffered document change events from the active file
|
|
614
|
+
*/
|
|
615
|
+
getDocumentChanges(options) {
|
|
616
|
+
if (!this._activeFileKey)
|
|
617
|
+
return [];
|
|
618
|
+
const client = this.clients.get(this._activeFileKey);
|
|
619
|
+
if (!client)
|
|
620
|
+
return [];
|
|
621
|
+
let filtered = [...client.documentChanges];
|
|
622
|
+
if (options?.since !== undefined) {
|
|
623
|
+
filtered = filtered.filter((e) => e.timestamp >= options.since);
|
|
624
|
+
}
|
|
625
|
+
if (options?.count !== undefined && options.count > 0) {
|
|
626
|
+
filtered = filtered.slice(-options.count);
|
|
627
|
+
}
|
|
628
|
+
return filtered;
|
|
629
|
+
}
|
|
630
|
+
/**
|
|
631
|
+
* Clear document change buffer for the active file
|
|
632
|
+
*/
|
|
633
|
+
clearDocumentChanges() {
|
|
634
|
+
if (!this._activeFileKey)
|
|
635
|
+
return 0;
|
|
636
|
+
const client = this.clients.get(this._activeFileKey);
|
|
637
|
+
if (!client)
|
|
638
|
+
return 0;
|
|
639
|
+
const count = client.documentChanges.length;
|
|
640
|
+
client.documentChanges = [];
|
|
641
|
+
return count;
|
|
642
|
+
}
|
|
643
|
+
/**
|
|
644
|
+
* v1.25.0: Get buffered description/annotation change events for a file.
|
|
645
|
+
*
|
|
646
|
+
* Used by `figma_diff_versions` to surface changes that Figma REST doesn't
|
|
647
|
+
* expose. The diff engine passes a time window (from-version → to-version
|
|
648
|
+
* `last_modified` timestamps converted to Unix ms) and optional scoping by
|
|
649
|
+
* node IDs.
|
|
650
|
+
*
|
|
651
|
+
* Defaults to the active file if `fileKey` is omitted.
|
|
652
|
+
*/
|
|
653
|
+
getMetadataChanges(options) {
|
|
654
|
+
const fileKey = options?.fileKey ?? this._activeFileKey;
|
|
655
|
+
if (!fileKey)
|
|
656
|
+
return [];
|
|
657
|
+
const client = this.clients.get(fileKey);
|
|
658
|
+
if (!client)
|
|
659
|
+
return [];
|
|
660
|
+
let filtered = [...client.metadataChanges];
|
|
661
|
+
if (options?.since !== undefined) {
|
|
662
|
+
filtered = filtered.filter((e) => e.timestamp >= options.since);
|
|
663
|
+
}
|
|
664
|
+
if (options?.until !== undefined) {
|
|
665
|
+
filtered = filtered.filter((e) => e.timestamp <= options.until);
|
|
666
|
+
}
|
|
667
|
+
if (options?.nodeIds && options.nodeIds.length > 0) {
|
|
668
|
+
const set = new Set(options.nodeIds);
|
|
669
|
+
filtered = filtered.filter((e) => set.has(e.node_id));
|
|
670
|
+
}
|
|
671
|
+
return filtered;
|
|
672
|
+
}
|
|
673
|
+
/**
|
|
674
|
+
* v1.25.0: Clear metadata-change buffer for the active file.
|
|
675
|
+
*/
|
|
676
|
+
clearMetadataChanges() {
|
|
677
|
+
if (!this._activeFileKey)
|
|
678
|
+
return 0;
|
|
679
|
+
const client = this.clients.get(this._activeFileKey);
|
|
680
|
+
if (!client)
|
|
681
|
+
return 0;
|
|
682
|
+
const count = client.metadataChanges.length;
|
|
683
|
+
client.metadataChanges = [];
|
|
684
|
+
return count;
|
|
685
|
+
}
|
|
686
|
+
/**
|
|
687
|
+
* Get console logs from the active file with optional filtering
|
|
688
|
+
*/
|
|
689
|
+
getConsoleLogs(options) {
|
|
690
|
+
if (!this._activeFileKey)
|
|
691
|
+
return [];
|
|
692
|
+
const client = this.clients.get(this._activeFileKey);
|
|
693
|
+
if (!client)
|
|
694
|
+
return [];
|
|
695
|
+
let filtered = [...client.consoleLogs];
|
|
696
|
+
if (options?.since !== undefined) {
|
|
697
|
+
filtered = filtered.filter((log) => log.timestamp >= options.since);
|
|
698
|
+
}
|
|
699
|
+
if (options?.level && options.level !== 'all') {
|
|
700
|
+
filtered = filtered.filter((log) => log.level === options.level);
|
|
701
|
+
}
|
|
702
|
+
if (options?.count !== undefined && options.count > 0) {
|
|
703
|
+
filtered = filtered.slice(-options.count);
|
|
704
|
+
}
|
|
705
|
+
return filtered;
|
|
706
|
+
}
|
|
707
|
+
/**
|
|
708
|
+
* Clear console log buffer for the active file
|
|
709
|
+
*/
|
|
710
|
+
clearConsoleLogs() {
|
|
711
|
+
if (!this._activeFileKey)
|
|
712
|
+
return 0;
|
|
713
|
+
const client = this.clients.get(this._activeFileKey);
|
|
714
|
+
if (!client)
|
|
715
|
+
return 0;
|
|
716
|
+
const count = client.consoleLogs.length;
|
|
717
|
+
client.consoleLogs = [];
|
|
718
|
+
return count;
|
|
719
|
+
}
|
|
720
|
+
/**
|
|
721
|
+
* Get console monitoring status for the active file
|
|
722
|
+
*/
|
|
723
|
+
getConsoleStatus() {
|
|
724
|
+
const client = this._activeFileKey ? this.clients.get(this._activeFileKey) : null;
|
|
725
|
+
const logs = client?.consoleLogs || [];
|
|
726
|
+
return {
|
|
727
|
+
isMonitoring: !!(client && client.ws.readyState === WebSocket.OPEN),
|
|
728
|
+
anyClientConnected: this.isClientConnected(),
|
|
729
|
+
logCount: logs.length,
|
|
730
|
+
bufferSize: this.consoleBufferSize,
|
|
731
|
+
workerCount: 0,
|
|
732
|
+
oldestTimestamp: logs[0]?.timestamp,
|
|
733
|
+
newestTimestamp: logs[logs.length - 1]?.timestamp,
|
|
734
|
+
};
|
|
735
|
+
}
|
|
736
|
+
// ============================================================================
|
|
737
|
+
// Multi-client methods
|
|
738
|
+
// ============================================================================
|
|
739
|
+
/**
|
|
740
|
+
* Get info about all connected Figma files.
|
|
741
|
+
* Returns an array of ConnectedFileInfo for each file with an active WebSocket.
|
|
742
|
+
*/
|
|
743
|
+
getConnectedFiles() {
|
|
744
|
+
const files = [];
|
|
745
|
+
for (const [fileKey, client] of this.clients) {
|
|
746
|
+
if (client.ws.readyState === WebSocket.OPEN) {
|
|
747
|
+
files.push({
|
|
748
|
+
...client.fileInfo,
|
|
749
|
+
isActive: fileKey === this._activeFileKey,
|
|
750
|
+
});
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
return files;
|
|
754
|
+
}
|
|
755
|
+
/**
|
|
756
|
+
* Get the last pong timestamp for the active client.
|
|
757
|
+
* Returns null if no active client or no pong received yet.
|
|
758
|
+
*/
|
|
759
|
+
getActiveClientLastPongAt() {
|
|
760
|
+
if (!this._activeFileKey)
|
|
761
|
+
return null;
|
|
762
|
+
const client = this.clients.get(this._activeFileKey);
|
|
763
|
+
return client?.lastPongAt ?? null;
|
|
764
|
+
}
|
|
765
|
+
/**
|
|
766
|
+
* Set the active file by fileKey. Returns true if the file is connected.
|
|
767
|
+
*/
|
|
768
|
+
setActiveFile(fileKey) {
|
|
769
|
+
const client = this.clients.get(fileKey);
|
|
770
|
+
if (client && client.ws.readyState === WebSocket.OPEN) {
|
|
771
|
+
this._activeFileKey = fileKey;
|
|
772
|
+
logger.info({ fileKey, fileName: client.fileInfo.fileName }, 'Active file switched');
|
|
773
|
+
this.emit('activeFileChanged', { fileKey, fileName: client.fileInfo.fileName });
|
|
774
|
+
return true;
|
|
775
|
+
}
|
|
776
|
+
return false;
|
|
777
|
+
}
|
|
778
|
+
/**
|
|
779
|
+
* Get the currently active file's key
|
|
780
|
+
*/
|
|
781
|
+
getActiveFileKey() {
|
|
782
|
+
return this._activeFileKey;
|
|
783
|
+
}
|
|
784
|
+
/**
|
|
785
|
+
* Get the editor type of the currently active file.
|
|
786
|
+
* Returns 'figma' if no file is connected or editorType wasn't reported.
|
|
787
|
+
*/
|
|
788
|
+
getEditorType() {
|
|
789
|
+
if (!this._activeFileKey)
|
|
790
|
+
return 'figma';
|
|
791
|
+
const client = this.clients.get(this._activeFileKey);
|
|
792
|
+
return client?.fileInfo?.editorType || 'figma';
|
|
793
|
+
}
|
|
794
|
+
// ============================================================================
|
|
795
|
+
// Cleanup
|
|
796
|
+
// ============================================================================
|
|
797
|
+
/**
|
|
798
|
+
* Reject pending requests that were sent to a specific file
|
|
799
|
+
*/
|
|
800
|
+
rejectPendingRequestsForFile(fileKey, reason) {
|
|
801
|
+
for (const [id, pending] of this.pendingRequests) {
|
|
802
|
+
if (pending.targetFileKey === fileKey) {
|
|
803
|
+
clearTimeout(pending.timeoutId);
|
|
804
|
+
pending.reject(new Error(reason));
|
|
805
|
+
this.pendingRequests.delete(id);
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
/**
|
|
810
|
+
* Reject all pending requests (used during shutdown)
|
|
811
|
+
*/
|
|
812
|
+
rejectPendingRequests(reason) {
|
|
813
|
+
for (const [, pending] of this.pendingRequests) {
|
|
814
|
+
clearTimeout(pending.timeoutId);
|
|
815
|
+
pending.reject(new Error(reason));
|
|
816
|
+
}
|
|
817
|
+
this.pendingRequests.clear();
|
|
818
|
+
}
|
|
819
|
+
/**
|
|
820
|
+
* Stop the server and clean up all connections
|
|
821
|
+
*/
|
|
822
|
+
async stop() {
|
|
823
|
+
// Clear heartbeat interval
|
|
824
|
+
if (this._heartbeatInterval) {
|
|
825
|
+
clearInterval(this._heartbeatInterval);
|
|
826
|
+
this._heartbeatInterval = null;
|
|
827
|
+
}
|
|
828
|
+
// Clear all per-client grace period timers
|
|
829
|
+
for (const [, client] of this.clients) {
|
|
830
|
+
if (client.gracePeriodTimer) {
|
|
831
|
+
clearTimeout(client.gracePeriodTimer);
|
|
832
|
+
client.gracePeriodTimer = null;
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
// Clear pending client identification timeouts
|
|
836
|
+
for (const [, timeout] of this._pendingClients) {
|
|
837
|
+
clearTimeout(timeout);
|
|
838
|
+
}
|
|
839
|
+
this._pendingClients.clear();
|
|
840
|
+
this.rejectPendingRequests('WebSocket server shutting down');
|
|
841
|
+
// Terminate all connected clients so wss.close() resolves promptly
|
|
842
|
+
if (this.wss) {
|
|
843
|
+
for (const ws of this.wss.clients) {
|
|
844
|
+
ws.terminate();
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
this.clients.clear();
|
|
848
|
+
this._activeFileKey = null;
|
|
849
|
+
// Close WS server first (handles WebSocket connections)
|
|
850
|
+
if (this.wss) {
|
|
851
|
+
await new Promise((resolve) => {
|
|
852
|
+
this.wss.close(() => resolve());
|
|
853
|
+
});
|
|
854
|
+
}
|
|
855
|
+
// Then close HTTP server (releases the port)
|
|
856
|
+
if (this.httpServer) {
|
|
857
|
+
await new Promise((resolve) => {
|
|
858
|
+
this.httpServer.close(() => resolve());
|
|
859
|
+
});
|
|
860
|
+
this.httpServer = null;
|
|
861
|
+
}
|
|
862
|
+
this._isStarted = false;
|
|
863
|
+
logger.info('WebSocket bridge server stopped');
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
//# sourceMappingURL=websocket-server.js.map
|