@fairfox/polly 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/LICENSE +21 -0
  2. package/README.md +322 -0
  3. package/cli/polly.ts +564 -0
  4. package/dist/background/api-client.d.ts +7 -0
  5. package/dist/background/context-menu.d.ts +7 -0
  6. package/dist/background/index.d.ts +31 -0
  7. package/dist/background/index.js +1309 -0
  8. package/dist/background/index.js.map +25 -0
  9. package/dist/background/log-store.d.ts +22 -0
  10. package/dist/background/message-router.d.ts +30 -0
  11. package/dist/background/message-router.js +1300 -0
  12. package/dist/background/message-router.js.map +24 -0
  13. package/dist/background/offscreen-manager.d.ts +10 -0
  14. package/dist/index.d.ts +18 -0
  15. package/dist/index.js +1471 -0
  16. package/dist/index.js.map +27 -0
  17. package/dist/shared/adapters/chrome/context-menus.chrome.d.ts +8 -0
  18. package/dist/shared/adapters/chrome/offscreen.chrome.d.ts +6 -0
  19. package/dist/shared/adapters/chrome/runtime.chrome.d.ts +13 -0
  20. package/dist/shared/adapters/chrome/storage.chrome.d.ts +8 -0
  21. package/dist/shared/adapters/chrome/tabs.chrome.d.ts +16 -0
  22. package/dist/shared/adapters/chrome/window.chrome.d.ts +6 -0
  23. package/dist/shared/adapters/context-menus.adapter.d.ts +22 -0
  24. package/dist/shared/adapters/fetch.adapter.d.ts +6 -0
  25. package/dist/shared/adapters/index.d.ts +34 -0
  26. package/dist/shared/adapters/index.js +298 -0
  27. package/dist/shared/adapters/index.js.map +18 -0
  28. package/dist/shared/adapters/logger.adapter.d.ts +44 -0
  29. package/dist/shared/adapters/offscreen.adapter.d.ts +20 -0
  30. package/dist/shared/adapters/runtime.adapter.d.ts +66 -0
  31. package/dist/shared/adapters/storage.adapter.d.ts +29 -0
  32. package/dist/shared/adapters/tabs.adapter.d.ts +39 -0
  33. package/dist/shared/adapters/window.adapter.d.ts +14 -0
  34. package/dist/shared/lib/context-helpers.d.ts +64 -0
  35. package/dist/shared/lib/context-helpers.js +1086 -0
  36. package/dist/shared/lib/context-helpers.js.map +24 -0
  37. package/dist/shared/lib/context-specific-helpers.d.ts +160 -0
  38. package/dist/shared/lib/errors.d.ts +67 -0
  39. package/dist/shared/lib/errors.js +94 -0
  40. package/dist/shared/lib/errors.js.map +10 -0
  41. package/dist/shared/lib/handler-execution-tracker.d.ts +24 -0
  42. package/dist/shared/lib/message-bus.d.ts +233 -0
  43. package/dist/shared/lib/message-bus.js +1033 -0
  44. package/dist/shared/lib/message-bus.js.map +23 -0
  45. package/dist/shared/lib/state.d.ts +102 -0
  46. package/dist/shared/lib/state.js +1265 -0
  47. package/dist/shared/lib/state.js.map +24 -0
  48. package/dist/shared/lib/test-helpers.d.ts +133 -0
  49. package/dist/shared/lib/test-helpers.js +136 -0
  50. package/dist/shared/lib/test-helpers.js.map +10 -0
  51. package/dist/shared/state/app-state.d.ts +8 -0
  52. package/dist/shared/state/app-state.js +1272 -0
  53. package/dist/shared/state/app-state.js.map +25 -0
  54. package/dist/shared/types/messages.d.ts +341 -0
  55. package/dist/shared/types/messages.js +25 -0
  56. package/dist/shared/types/messages.js.map +10 -0
  57. package/package.json +110 -0
@@ -0,0 +1,25 @@
1
+ // src/shared/types/messages.ts
2
+ var ALL_CONTEXTS = [
3
+ "background",
4
+ "content",
5
+ "page",
6
+ "devtools",
7
+ "popup",
8
+ "options",
9
+ "sidepanel",
10
+ "offscreen"
11
+ ];
12
+ var defaultSettings = {
13
+ theme: "auto",
14
+ autoSync: true,
15
+ debugMode: false,
16
+ notifications: true,
17
+ apiEndpoint: "https://api.example.com",
18
+ refreshInterval: 60000
19
+ };
20
+ export {
21
+ defaultSettings,
22
+ ALL_CONTEXTS
23
+ };
24
+
25
+ //# debugId=CD4C96B7C2FB31BC64756E2164756E21
@@ -0,0 +1,10 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/shared/types/messages.ts"],
4
+ "sourcesContent": [
5
+ "// Type definitions for all messages in the extension\n\n/**\n * Base message interface that all messages must satisfy.\n * This allows users to define custom messages alongside framework messages.\n */\nexport interface BaseMessage {\n type: string;\n}\n\nexport type Context =\n | \"background\"\n | \"content\"\n | \"page\"\n | \"devtools\"\n | \"popup\"\n | \"options\"\n | \"sidepanel\"\n | \"offscreen\";\n\n// All contexts (useful for broadcast)\nexport const ALL_CONTEXTS: Context[] = [\n \"background\",\n \"content\",\n \"page\",\n \"devtools\",\n \"popup\",\n \"options\",\n \"sidepanel\",\n \"offscreen\",\n] as const;\n\n// Settings schema\nexport type Settings = {\n theme: \"light\" | \"dark\" | \"auto\";\n autoSync: boolean;\n debugMode: boolean;\n notifications: boolean;\n apiEndpoint: string;\n refreshInterval: number;\n};\n\nexport const defaultSettings: Settings = {\n theme: \"auto\",\n autoSync: true,\n debugMode: false,\n notifications: true,\n apiEndpoint: \"https://api.example.com\",\n refreshInterval: 60000,\n};\n\n// Logging types\nexport type LogLevel = \"debug\" | \"info\" | \"warn\" | \"error\";\n\nexport type LogEntry = {\n id: string;\n level: LogLevel;\n message: string;\n context?: Record<string, unknown>;\n error?: string;\n stack?: string;\n source: Context;\n timestamp: number;\n};\n\n// All possible messages (discriminated union)\nexport type ExtensionMessage =\n // DOM Operations (handled by Content Script)\n | { type: \"DOM_QUERY\"; selector: string }\n | { type: \"DOM_UPDATE\"; selector: string; content: string }\n | {\n type: \"DOM_INSERT\";\n position: \"beforebegin\" | \"afterbegin\" | \"beforeend\" | \"afterend\";\n html: string;\n }\n | { type: \"DOM_REMOVE\"; selector: string }\n\n // Page Script Operations (handled by Page Script)\n | { type: \"PAGE_EVAL\"; code: string }\n | { type: \"PAGE_GET_VAR\"; varName: string }\n | { type: \"PAGE_CALL_FN\"; fnName: string; args: unknown[] }\n | { type: \"PAGE_SET_VAR\"; varName: string; value: unknown }\n\n // API Operations (handled by Background)\n | {\n type: \"API_REQUEST\";\n endpoint: string;\n method: \"GET\" | \"POST\" | \"PUT\" | \"DELETE\";\n body?: unknown;\n headers?: Record<string, string>;\n }\n | {\n type: \"API_BATCH\";\n requests: Array<{ endpoint: string; method: string; body?: unknown }>;\n }\n\n // Clipboard Operations (handled by Offscreen)\n | { type: \"CLIPBOARD_WRITE\"; text: string }\n | { type: \"CLIPBOARD_WRITE_HTML\"; html: string }\n | { type: \"CLIPBOARD_WRITE_RICH\"; data: { text: string; html: string } }\n | { type: \"CLIPBOARD_READ\" }\n\n // Context Menu (handled by Background)\n | {\n type: \"CONTEXT_MENU_CLICKED\";\n menuId: string;\n info: chrome.contextMenus.OnClickData;\n tabId: number;\n }\n | {\n type: \"CONTEXT_MENU_CREATE\";\n id: string;\n title: string;\n contexts: chrome.contextMenus.ContextType[];\n }\n | { type: \"CONTEXT_MENU_REMOVE\"; id: string }\n\n // State Sync (broadcast) - Internal only, handled by state primitives\n | {\n type: \"STATE_SYNC\";\n key: string;\n value: unknown;\n clock: number;\n }\n\n // Tab Operations (handled by Background)\n | { type: \"TAB_QUERY\"; queryInfo: chrome.tabs.QueryInfo }\n | { type: \"TAB_GET_CURRENT\" }\n | { type: \"TAB_RELOAD\"; tabId: number }\n\n // DevTools Operations\n | { type: \"DEVTOOLS_INSPECT_ELEMENT\"; selector: string }\n | {\n type: \"DEVTOOLS_LOG\";\n level: \"log\" | \"warn\" | \"error\";\n message: string;\n data?: unknown;\n }\n\n // Logging (handled by Background LogStore)\n | {\n type: \"LOG\";\n level: LogLevel;\n message: string;\n context?: Record<string, unknown>;\n error?: string;\n stack?: string;\n source: Context;\n timestamp: number;\n }\n | {\n type: \"LOGS_GET\";\n filters?: {\n level?: LogLevel;\n source?: Context;\n since?: number;\n limit?: number;\n };\n }\n | { type: \"LOGS_CLEAR\" }\n | { type: \"LOGS_EXPORT\" }\n\n // Test Messages (only used in tests)\n | { type: \"TEST_MESSAGE\"; data?: unknown }\n | { type: \"TEST\"; iteration?: number }\n | { type: \"CUSTOM_MESSAGE\"; data?: unknown }\n | { type: \"SETTINGS_GET\" }\n | {\n type: \"SIGNAL_UPDATE\";\n key?: string;\n value?: unknown;\n signalId?: string;\n source?: Context;\n }\n | {\n type: \"USER_DATA\";\n password?: string;\n apiKey?: string;\n [key: string]: unknown;\n };\n\n// Helper: Look up the full message type from a union based on the 'type' discriminator\ntype LookupMessage<TUnion, TType extends string> = TUnion extends { type: TType } ? TUnion : never;\n\n// Extract response type from message using phantom type\n// If message has __response field (phantom type), use it; otherwise infer from framework messages\nexport type MessageResponse<T extends BaseMessage> =\n // First, try to find the matching message in the union by type discriminator\n T extends { type: infer TType extends string }\n ? LookupMessage<T, TType> extends { readonly __response?: infer R }\n ? R // Found phantom type, use it\n : T extends ExtensionMessage\n ? // Framework message - infer from type\n // DOM Operations\n T extends { type: \"DOM_QUERY\" }\n ? {\n elements: Array<{\n tag: string;\n text: string;\n html: string;\n attrs: Record<string, string>;\n rect?: DOMRect;\n }>;\n }\n : T extends { type: \"DOM_UPDATE\" }\n ? { success: boolean }\n : T extends { type: \"DOM_INSERT\" }\n ? { success: boolean }\n : T extends { type: \"DOM_REMOVE\" }\n ? { success: boolean; count: number }\n : // Page Script Operations\n T extends { type: \"PAGE_EVAL\" }\n ? { result: unknown; error?: string }\n : T extends { type: \"PAGE_GET_VAR\" }\n ? { value: unknown; exists: boolean }\n : T extends { type: \"PAGE_CALL_FN\" }\n ? { result: unknown; error?: string }\n : T extends { type: \"PAGE_SET_VAR\" }\n ? { success: boolean }\n : // API Operations\n T extends { type: \"API_REQUEST\" }\n ? {\n data: unknown;\n status: number;\n statusText: string;\n headers: Record<string, string>;\n error?: string;\n }\n : T extends { type: \"API_BATCH\" }\n ? {\n results: Array<{\n data: unknown;\n status: number;\n error?: string;\n }>;\n }\n : // Clipboard Operations\n T extends { type: \"CLIPBOARD_WRITE\" }\n ? { success: boolean }\n : T extends { type: \"CLIPBOARD_WRITE_HTML\" }\n ? { success: boolean }\n : T extends { type: \"CLIPBOARD_WRITE_RICH\" }\n ? { success: boolean }\n : T extends { type: \"CLIPBOARD_READ\" }\n ? { text: string }\n : // Context Menu\n T extends { type: \"CONTEXT_MENU_CLICKED\" }\n ? undefined\n : T extends { type: \"CONTEXT_MENU_CREATE\" }\n ? { success: boolean }\n : T extends { type: \"CONTEXT_MENU_REMOVE\" }\n ? { success: boolean }\n : // State Sync\n T extends { type: \"STATE_SYNC\" }\n ? undefined\n : // Tab Operations\n T extends { type: \"TAB_QUERY\" }\n ? { tabs: chrome.tabs.Tab[] }\n : T extends {\n type: \"TAB_GET_CURRENT\";\n }\n ? { tab: chrome.tabs.Tab }\n : T extends {\n type: \"TAB_RELOAD\";\n }\n ? { success: boolean }\n : // DevTools Operations\n T extends {\n type: \"DEVTOOLS_INSPECT_ELEMENT\";\n }\n ? {\n success: boolean;\n }\n : T extends {\n type: \"DEVTOOLS_LOG\";\n }\n ? undefined\n : // Logging Operations\n T extends {\n type: \"LOG\";\n }\n ? {\n success: boolean;\n }\n : T extends {\n type: \"LOGS_GET\";\n }\n ? {\n logs: LogEntry[];\n }\n : T extends {\n type: \"LOGS_CLEAR\";\n }\n ? {\n success: boolean;\n count: number;\n }\n : T extends {\n type: \"LOGS_EXPORT\";\n }\n ? {\n json: string;\n count: number;\n }\n : T extends {\n type: \"SETTINGS_GET\";\n }\n ? {\n settings: unknown;\n }\n : undefined\n : unknown // For custom messages outside ExtensionMessage, require phantom type\n : unknown; // Fallback for messages without type field\n\n// Message handler mapping (which context handles which message)\n// Can be a single context or an array for multi-target routing\nexport type MessageHandler = {\n DOM_QUERY: \"content\";\n DOM_UPDATE: \"content\";\n DOM_INSERT: \"content\";\n DOM_REMOVE: \"content\";\n\n PAGE_EVAL: \"page\";\n PAGE_GET_VAR: \"page\";\n PAGE_CALL_FN: \"page\";\n PAGE_SET_VAR: \"page\";\n\n API_REQUEST: \"background\";\n API_BATCH: \"background\";\n\n CLIPBOARD_WRITE: \"offscreen\";\n CLIPBOARD_WRITE_HTML: \"offscreen\";\n CLIPBOARD_WRITE_RICH: \"offscreen\";\n CLIPBOARD_READ: \"offscreen\";\n\n CONTEXT_MENU_CLICKED: \"background\";\n CONTEXT_MENU_CREATE: \"background\";\n CONTEXT_MENU_REMOVE: \"background\";\n\n STATE_SYNC: Context[]; // Broadcast to all contexts\n\n TAB_QUERY: \"background\";\n TAB_GET_CURRENT: \"background\";\n TAB_RELOAD: \"background\";\n\n DEVTOOLS_INSPECT_ELEMENT: \"content\";\n DEVTOOLS_LOG: \"background\";\n\n LOG: \"background\";\n LOGS_GET: \"background\";\n LOGS_CLEAR: \"background\";\n LOGS_EXPORT: \"background\";\n};\n\n// Routed message envelope\nexport type RoutedMessage<T extends BaseMessage = ExtensionMessage> = {\n id: string; // Correlation ID (UUID)\n source: Context; // Which context sent it\n targets: Context[]; // Which contexts should receive this (can be multiple)\n tabId?: number; // Required for per-tab contexts\n timestamp: number; // When it was sent\n payload: T; // The actual message\n};\n\n// Routed response envelope\nexport type RoutedResponse<T extends BaseMessage = ExtensionMessage> = {\n id: string; // Matches request ID\n success: boolean; // Whether operation succeeded\n data?: MessageResponse<T>; // Response data\n error?: string; // Error message if failed\n timestamp: number; // When response was sent\n};\n"
6
+ ],
7
+ "mappings": ";AAqBO,IAAM,eAA0B;AAAA,EACrC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAYO,IAAM,kBAA4B;AAAA,EACvC,OAAO;AAAA,EACP,UAAU;AAAA,EACV,WAAW;AAAA,EACX,eAAe;AAAA,EACf,aAAa;AAAA,EACb,iBAAiB;AACnB;",
8
+ "debugId": "CD4C96B7C2FB31BC64756E2164756E21",
9
+ "names": []
10
+ }
package/package.json ADDED
@@ -0,0 +1,110 @@
1
+ {
2
+ "name": "@fairfox/polly",
3
+ "version": "0.1.0",
4
+ "private": false,
5
+ "type": "module",
6
+ "description": "Multi-execution-context framework with reactive state and cross-context messaging for Chrome extensions, PWAs, and worker-based applications",
7
+ "main": "dist/index.js",
8
+ "module": "dist/index.js",
9
+ "types": "dist/index.d.ts",
10
+ "bin": {
11
+ "polly": "./cli/polly.ts"
12
+ },
13
+ "exports": {
14
+ ".": {
15
+ "import": "./dist/index.js",
16
+ "types": "./dist/index.d.ts"
17
+ },
18
+ "./state": {
19
+ "import": "./dist/shared/lib/state.js",
20
+ "types": "./dist/shared/lib/state.d.ts"
21
+ },
22
+ "./message-bus": {
23
+ "import": "./dist/shared/lib/message-bus.js",
24
+ "types": "./dist/shared/lib/message-bus.d.ts"
25
+ },
26
+ "./background": {
27
+ "import": "./dist/background/index.js",
28
+ "types": "./dist/background/index.d.ts"
29
+ },
30
+ "./message-router": {
31
+ "import": "./dist/background/message-router.js",
32
+ "types": "./dist/background/message-router.d.ts"
33
+ },
34
+ "./adapters": {
35
+ "import": "./dist/shared/adapters/index.js",
36
+ "types": "./dist/shared/adapters/index.d.ts"
37
+ },
38
+ "./errors": {
39
+ "import": "./dist/shared/lib/errors.js",
40
+ "types": "./dist/shared/lib/errors.d.ts"
41
+ },
42
+ "./types": {
43
+ "import": "./dist/shared/types/messages.js",
44
+ "types": "./dist/shared/types/messages.d.ts"
45
+ }
46
+ },
47
+ "files": ["dist", "README.md", "LICENSE"],
48
+ "scripts": {
49
+ "dev": "bun run build.ts --watch",
50
+ "build": "bun run build.ts",
51
+ "build:prod": "bun run build.ts --prod",
52
+ "build:lib": "bun run build-lib.ts",
53
+ "build:full-featured": "bun run scripts/build-user-extension.ts examples/full-featured",
54
+ "build:full-featured:prod": "bun run scripts/build-user-extension.ts examples/full-featured --prod",
55
+ "typecheck": "bunx tsc --noEmit",
56
+ "lint": "biome check .",
57
+ "lint:fix": "biome check --write .",
58
+ "format": "biome format --write .",
59
+ "prepublishOnly": "bun run typecheck && bun run lint && bun test && bun run build:lib",
60
+ "publish:public": "npm publish --access public"
61
+ },
62
+ "devDependencies": {
63
+ "@biomejs/biome": "^1.8.3",
64
+ "@types/bun": "^1.2.19",
65
+ "@types/chrome": "^0.0.268",
66
+ "typescript": "^5.5.4"
67
+ },
68
+ "engines": {
69
+ "bun": ">=1.0.0"
70
+ },
71
+ "dependencies": {
72
+ "@preact/signals": "^2.3.1",
73
+ "preact": "^10.27.2",
74
+ "ts-morph": "^21.0.1"
75
+ },
76
+ "peerDependencies": {
77
+ "typescript": "^5",
78
+ "preact": "^10",
79
+ "@preact/signals": "^2"
80
+ },
81
+ "keywords": [
82
+ "multi-context",
83
+ "chrome-extension",
84
+ "browser-extension",
85
+ "web-extension",
86
+ "pwa",
87
+ "web-workers",
88
+ "service-workers",
89
+ "framework",
90
+ "reactive",
91
+ "state-management",
92
+ "messaging",
93
+ "cross-context",
94
+ "preact",
95
+ "signals",
96
+ "typescript",
97
+ "distributed",
98
+ "concurrent"
99
+ ],
100
+ "author": "Alex Jeffcott",
101
+ "license": "MIT",
102
+ "repository": {
103
+ "type": "git",
104
+ "url": "https://github.com/AlexJeffcott/polly"
105
+ },
106
+ "bugs": {
107
+ "url": "https://github.com/AlexJeffcott/polly/issues"
108
+ },
109
+ "homepage": "https://github.com/AlexJeffcott/polly#readme"
110
+ }