sparkbun 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 (131) hide show
  1. package/bin/sparkbun.cjs +18 -0
  2. package/dist-linux-arm64/bsdiff +0 -0
  3. package/dist-linux-arm64/bspatch +0 -0
  4. package/dist-linux-arm64/libElectrobunCore.so +0 -0
  5. package/dist-linux-arm64/libNativeWrapper.so +0 -0
  6. package/dist-linux-arm64/libasar.so +0 -0
  7. package/dist-linux-x64/bsdiff +0 -0
  8. package/dist-linux-x64/bspatch +0 -0
  9. package/dist-linux-x64/libElectrobunCore.so +0 -0
  10. package/dist-linux-x64/libNativeWrapper.so +0 -0
  11. package/dist-linux-x64/libasar.so +0 -0
  12. package/dist-macos-arm64/bsdiff +0 -0
  13. package/dist-macos-arm64/bspatch +0 -0
  14. package/dist-macos-arm64/libElectrobunCore.dylib +0 -0
  15. package/dist-macos-arm64/libNativeWrapper.dylib +0 -0
  16. package/dist-macos-arm64/libasar.dylib +0 -0
  17. package/dist-macos-arm64/libwebgpu_dawn.dylib +0 -0
  18. package/dist-macos-arm64/preload-full.js +885 -0
  19. package/dist-macos-arm64/preload-sandboxed.js +111 -0
  20. package/dist-macos-arm64/process_helper +0 -0
  21. package/dist-win-x64/ElectrobunCore.dll +0 -0
  22. package/dist-win-x64/WebView2Loader.dll +0 -0
  23. package/dist-win-x64/bsdiff.exe +0 -0
  24. package/dist-win-x64/bspatch.exe +0 -0
  25. package/dist-win-x64/libNativeWrapper.dll +0 -0
  26. package/dist-win-x64/zig-asar/arm64/libasar.dll +0 -0
  27. package/dist-win-x64/zig-asar/x64/libasar.dll +0 -0
  28. package/package.json +47 -0
  29. package/scripts/build-and-upload-artifacts.js +207 -0
  30. package/scripts/gen-webgpu-ffi.mjs +162 -0
  31. package/scripts/install-windows-deps.ps1 +80 -0
  32. package/scripts/package-release.js +237 -0
  33. package/scripts/push-version.js +84 -0
  34. package/scripts/update-bun-version.ts +122 -0
  35. package/scripts/update-cef-version.ts +145 -0
  36. package/src/browser/builtinrpcSchema.ts +19 -0
  37. package/src/browser/global.d.ts +36 -0
  38. package/src/browser/index.ts +234 -0
  39. package/src/browser/webviewtag.ts +88 -0
  40. package/src/browser/wgputag.ts +48 -0
  41. package/src/bun/SparkBunConfig.ts +497 -0
  42. package/src/bun/__tests__/ffi-contract.test.ts +105 -0
  43. package/src/bun/core/ApplicationMenu.ts +70 -0
  44. package/src/bun/core/BrowserView.ts +416 -0
  45. package/src/bun/core/BrowserWindow.ts +396 -0
  46. package/src/bun/core/BuildConfig.ts +71 -0
  47. package/src/bun/core/ContextMenu.ts +75 -0
  48. package/src/bun/core/GpuWindow.ts +289 -0
  49. package/src/bun/core/Paths.ts +5 -0
  50. package/src/bun/core/Socket.ts +22 -0
  51. package/src/bun/core/Tray.ts +197 -0
  52. package/src/bun/core/Updater.ts +1131 -0
  53. package/src/bun/core/Utils.ts +487 -0
  54. package/src/bun/core/WGPUView.ts +167 -0
  55. package/src/bun/core/menuRoles.ts +181 -0
  56. package/src/bun/events/ApplicationEvents.ts +22 -0
  57. package/src/bun/events/event.ts +27 -0
  58. package/src/bun/events/eventEmitter.ts +45 -0
  59. package/src/bun/events/trayEvents.ts +11 -0
  60. package/src/bun/events/webviewEvents.ts +39 -0
  61. package/src/bun/events/windowEvents.ts +23 -0
  62. package/src/bun/index.ts +120 -0
  63. package/src/bun/preload/.generated/compiled.ts +2 -0
  64. package/src/bun/preload/build.ts +65 -0
  65. package/src/bun/preload/dragRegions.ts +41 -0
  66. package/src/bun/preload/encryption.ts +86 -0
  67. package/src/bun/preload/events.ts +171 -0
  68. package/src/bun/preload/globals.d.ts +45 -0
  69. package/src/bun/preload/index-sandboxed.ts +28 -0
  70. package/src/bun/preload/index.ts +77 -0
  71. package/src/bun/preload/internalRpc.ts +80 -0
  72. package/src/bun/preload/overlaySync.ts +107 -0
  73. package/src/bun/preload/webviewTag.ts +451 -0
  74. package/src/bun/preload/wgpuTag.ts +246 -0
  75. package/src/bun/proc/linux.md +43 -0
  76. package/src/bun/proc/native.ts +3253 -0
  77. package/src/bun/webGPU.ts +346 -0
  78. package/src/bun/webgpuAdapter.ts +3011 -0
  79. package/src/cli/bun.lockb +0 -0
  80. package/src/cli/index.ts +4653 -0
  81. package/src/cli/package-lock.json +81 -0
  82. package/src/cli/package.json +11 -0
  83. package/src/cli/templates/embedded.ts +2 -0
  84. package/src/core/build.zig +16 -0
  85. package/src/core/main.zig +3378 -0
  86. package/src/extractor/build.zig +22 -0
  87. package/src/installer/installer-template.ts +216 -0
  88. package/src/launcher/main.ts +221 -0
  89. package/src/native/build/libNativeWrapper.so +0 -0
  90. package/src/native/linux/build/nativeWrapper.o +0 -0
  91. package/src/native/linux/cef_loader.cpp +110 -0
  92. package/src/native/linux/cef_loader.h +28 -0
  93. package/src/native/linux/cef_process_helper_linux.cpp +160 -0
  94. package/src/native/linux/nativeWrapper.cpp +11768 -0
  95. package/src/native/macos/cef_process_helper_mac.cc +160 -0
  96. package/src/native/macos/nativeWrapper.mm +9172 -0
  97. package/src/native/shared/accelerator_parser.h +72 -0
  98. package/src/native/shared/app_paths.h +110 -0
  99. package/src/native/shared/asar.h +35 -0
  100. package/src/native/shared/cache_migration.h +244 -0
  101. package/src/native/shared/callbacks.h +57 -0
  102. package/src/native/shared/cef_response_filter.h +189 -0
  103. package/src/native/shared/chromium_flags.h +181 -0
  104. package/src/native/shared/config.h +66 -0
  105. package/src/native/shared/download_event.h +197 -0
  106. package/src/native/shared/ffi_helpers.h +139 -0
  107. package/src/native/shared/glob_match.h +59 -0
  108. package/src/native/shared/json_menu_parser.h +223 -0
  109. package/src/native/shared/mime_types.h +101 -0
  110. package/src/native/shared/navigation_rules.h +98 -0
  111. package/src/native/shared/partition_context.h +137 -0
  112. package/src/native/shared/pending_resize_queue.h +45 -0
  113. package/src/native/shared/permissions.h +118 -0
  114. package/src/native/shared/permissions_cef.h +74 -0
  115. package/src/native/shared/preload_script.h +71 -0
  116. package/src/native/shared/shutdown_guard.h +134 -0
  117. package/src/native/shared/thread_safe_map.h +138 -0
  118. package/src/native/shared/webview_storage.h +91 -0
  119. package/src/native/win/cef_process_helper_win.cpp +143 -0
  120. package/src/native/win/dcomp_compositor.h +352 -0
  121. package/src/native/win/nativeWrapper.cpp +12434 -0
  122. package/src/npmbin/index.js +34 -0
  123. package/src/shared/bun-version.ts +3 -0
  124. package/src/shared/cef-version.ts +5 -0
  125. package/src/shared/naming.test.ts +327 -0
  126. package/src/shared/naming.ts +188 -0
  127. package/src/shared/platform.ts +48 -0
  128. package/src/shared/rpc.ts +541 -0
  129. package/src/shared/sparkbun-version.ts +2 -0
  130. package/src/types/three.d.ts +1 -0
  131. package/tsconfig.json +31 -0
@@ -0,0 +1,352 @@
1
+ // dcomp_compositor.h — DirectComposition GPU surface compositor for Windows
2
+ //
3
+ // Provides zero-copy GPU compositing using DirectComposition + DXGI swap chain.
4
+ // This matches the macOS CAMetalLayer compositing pattern used in Electrobun
5
+ // for native GPU rendering.
6
+ //
7
+ // Usage: initMinimal() to create DComp visual tree, then initSwapChainFromDevice()
8
+ // once Dawn's D3D11On12 device is available. Frames are presented via
9
+ // zeroCopyPresent() or presentOnly().
10
+
11
+ #pragma once
12
+
13
+ #include <dcomp.h>
14
+ #include <dxgi1_2.h>
15
+ #include <dxgi1_4.h>
16
+ #include <d3d11.h>
17
+ #include <d3d11_4.h>
18
+ #include <d3d11on12.h>
19
+ #include <d3d12.h>
20
+ #include <wrl.h>
21
+ #include <cstdio>
22
+
23
+ #pragma comment(lib, "dxgi.lib")
24
+ #pragma comment(lib, "d3d11.lib")
25
+ #pragma comment(lib, "d3d12.lib")
26
+
27
+ #include <versionhelpers.h>
28
+
29
+ using Microsoft::WRL::ComPtr;
30
+
31
+ // Feature gate: DirectComposition requires Windows 8.1+.
32
+ // Note: IsWindows8Point1OrGreater() requires an app manifest to report correctly.
33
+ // Without a manifest, Windows lies about the version. We use RtlGetVersion instead
34
+ // which always returns the true OS version.
35
+ static bool isDCompAvailable() {
36
+ static int cached = -1;
37
+ if (cached >= 0) return cached == 1;
38
+
39
+ // RtlGetVersion is not affected by manifests — always returns the real version.
40
+ typedef LONG (WINAPI *RtlGetVersionPtr)(OSVERSIONINFOEXW*);
41
+ HMODULE ntdll = GetModuleHandleW(L"ntdll.dll");
42
+ if (ntdll) {
43
+ auto fn = (RtlGetVersionPtr)GetProcAddress(ntdll, "RtlGetVersion");
44
+ if (fn) {
45
+ OSVERSIONINFOEXW osInfo = {};
46
+ osInfo.dwOSVersionInfoSize = sizeof(osInfo);
47
+ if (fn(&osInfo) == 0) {
48
+ // Windows 8.1 = 6.3, Windows 10/11 = 10.0
49
+ bool ok = (osInfo.dwMajorVersion > 6) ||
50
+ (osInfo.dwMajorVersion == 6 && osInfo.dwMinorVersion >= 3);
51
+ // OS version check done
52
+ cached = ok ? 1 : 0;
53
+ return ok;
54
+ }
55
+ }
56
+ }
57
+
58
+ // Fallback: assume available on modern Windows
59
+ // Could not detect OS version, assuming DComp available
60
+ cached = 1;
61
+ return true;
62
+ }
63
+
64
+ class DCompCompositor {
65
+ public:
66
+ // Minimal init: DComp visual tree only, no rendering device or swap chain.
67
+ // The swap chain is created later via initSwapChainFromDevice() once Dawn's
68
+ // D3D11On12 device is available.
69
+ bool initMinimal(HWND hwnd, int width, int height) {
70
+ if (!isDCompAvailable()) return false;
71
+
72
+ this->targetHwnd = hwnd;
73
+ this->surfaceWidth = width;
74
+ this->surfaceHeight = height;
75
+
76
+ // DCompositionCreateDevice2 with a DXGI device creates a DComp device
77
+ // that can hold visuals/targets. We pass nullptr to create a device-less
78
+ // compositor — swap chain content is attached later.
79
+ //
80
+ // Fallback: if nullptr doesn't work, we create a temporary D3D11 device
81
+ // just for DComp init. The swap chain content can be replaced later.
82
+ HRESULT hr = DCompositionCreateDevice2(nullptr, IID_PPV_ARGS(&dcompDevice));
83
+ if (FAILED(hr)) {
84
+ printf("[DComp] DCompositionCreateDevice2(null) failed: 0x%08lx, trying fallback\n", hr);
85
+
86
+ // Fallback: create a lightweight D3D11 device just for DComp
87
+ D3D_FEATURE_LEVEL featureLevel;
88
+ hr = D3D11CreateDevice(
89
+ nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr,
90
+ D3D11_CREATE_DEVICE_BGRA_SUPPORT,
91
+ nullptr, 0, D3D11_SDK_VERSION,
92
+ &d3dDevice, &featureLevel, &d3dContext);
93
+ if (FAILED(hr)) {
94
+ printf("[DComp] Fallback D3D11CreateDevice failed: 0x%08lx\n", hr);
95
+ return false;
96
+ }
97
+
98
+ ComPtr<IDXGIDevice> dxgiDevice;
99
+ hr = d3dDevice.As(&dxgiDevice);
100
+ if (FAILED(hr)) return false;
101
+
102
+ hr = DCompositionCreateDevice(dxgiDevice.Get(), IID_PPV_ARGS(&dcompDevice));
103
+ if (FAILED(hr)) {
104
+ printf("[DComp] Fallback DCompositionCreateDevice failed: 0x%08lx\n", hr);
105
+ return false;
106
+ }
107
+ }
108
+
109
+ hr = dcompDevice->CreateTargetForHwnd(hwnd, FALSE, &dcompTarget);
110
+ if (FAILED(hr)) {
111
+ printf("[DComp] CreateTargetForHwnd failed: 0x%08lx\n", hr);
112
+ return false;
113
+ }
114
+
115
+ hr = dcompDevice->CreateVisual(&rootVisual);
116
+ if (FAILED(hr)) {
117
+ printf("[DComp] CreateVisual failed: 0x%08lx\n", hr);
118
+ return false;
119
+ }
120
+
121
+ hr = dcompTarget->SetRoot(rootVisual.Get());
122
+ if (FAILED(hr)) return false;
123
+
124
+ hr = dcompDevice->Commit();
125
+ if (FAILED(hr)) return false;
126
+
127
+ // Minimal init done
128
+ initialized = true;
129
+ return true;
130
+ }
131
+
132
+ // Create a swap chain on an external D3D11 device (e.g., Dawn's D3D11On12).
133
+ // Call after initMinimal() + Dawn device creation.
134
+ bool initSwapChainFromDevice(ID3D11Device* externalDevice, int width, int height) {
135
+ if (!initialized || !dcompDevice || !rootVisual) return false;
136
+ if (!externalDevice) return false;
137
+
138
+ // Store the external device for blit/present operations
139
+ externalD3dDevice = externalDevice;
140
+ externalDevice->GetImmediateContext(&externalD3dContext);
141
+
142
+ // Get DXGI factory from external device
143
+ ComPtr<IDXGIDevice> dxgiDevice;
144
+ HRESULT hr = externalDevice->QueryInterface(IID_PPV_ARGS(&dxgiDevice));
145
+ if (FAILED(hr)) {
146
+ printf("[DComp] External device QI for IDXGIDevice failed: 0x%08lx\n", hr);
147
+ return false;
148
+ }
149
+
150
+ ComPtr<IDXGIAdapter> dxgiAdapter;
151
+ hr = dxgiDevice->GetAdapter(&dxgiAdapter);
152
+ if (FAILED(hr)) return false;
153
+
154
+ ComPtr<IDXGIFactory2> factory;
155
+ hr = dxgiAdapter->GetParent(IID_PPV_ARGS(&factory));
156
+ if (FAILED(hr)) return false;
157
+
158
+ // Create swap chain for composition on the external device
159
+ DXGI_SWAP_CHAIN_DESC1 scDesc = {};
160
+ scDesc.Width = width;
161
+ scDesc.Height = height;
162
+ scDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
163
+ scDesc.SampleDesc.Count = 1;
164
+ scDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
165
+ scDesc.BufferCount = 2;
166
+ scDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
167
+ scDesc.AlphaMode = DXGI_ALPHA_MODE_PREMULTIPLIED;
168
+
169
+ hr = factory->CreateSwapChainForComposition(
170
+ externalDevice, &scDesc, nullptr, &swapChain);
171
+ if (FAILED(hr)) {
172
+ printf("[DComp] CreateSwapChainForComposition (external) failed: 0x%08lx\n", hr);
173
+ return false;
174
+ }
175
+
176
+ // Set as root visual content
177
+ hr = rootVisual->SetContent(swapChain.Get());
178
+ if (FAILED(hr)) return false;
179
+
180
+ hr = dcompDevice->Commit();
181
+ if (FAILED(hr)) return false;
182
+
183
+ surfaceWidth = width;
184
+ surfaceHeight = height;
185
+ zeroCopyMode = true;
186
+ // Swap chain created
187
+ return true;
188
+ }
189
+
190
+ // Zero-copy present: copy source D3D11 texture to swap chain back buffer.
191
+ // sourceTexture must be on the same D3D11 device as the swap chain.
192
+ bool zeroCopyPresent(ID3D11Texture2D* sourceTexture) {
193
+ if (!swapChain || !externalD3dContext) return false;
194
+
195
+ ComPtr<ID3D11Texture2D> backBuffer;
196
+ HRESULT hr = swapChain->GetBuffer(0, IID_PPV_ARGS(&backBuffer));
197
+ if (FAILED(hr)) {
198
+ printf("[DComp] zeroCopyPresent: GetBuffer failed: 0x%08lx\n", hr);
199
+ return false;
200
+ }
201
+
202
+ externalD3dContext->CopyResource(backBuffer.Get(), sourceTexture);
203
+
204
+ hr = swapChain->Present(0, 0);
205
+ if (FAILED(hr)) {
206
+ printf("[DComp] zeroCopyPresent: Present failed: 0x%08lx\n", hr);
207
+ return false;
208
+ }
209
+
210
+ return SUCCEEDED(dcompDevice->Commit());
211
+ }
212
+
213
+ // Just Present + Commit (when the copy was already done externally).
214
+ bool presentOnly() {
215
+ if (!swapChain || !dcompDevice) return false;
216
+
217
+ HRESULT hr = swapChain->Present(0, 0);
218
+ if (FAILED(hr)) return false;
219
+
220
+ return SUCCEEDED(dcompDevice->Commit());
221
+ }
222
+
223
+ bool resize(int newWidth, int newHeight) {
224
+ if (!initialized || !swapChain) return false;
225
+ if (newWidth <= 0 || newHeight <= 0) return false;
226
+ if (newWidth == surfaceWidth && newHeight == surfaceHeight) return true;
227
+
228
+ surfaceWidth = newWidth;
229
+ surfaceHeight = newHeight;
230
+
231
+ auto* ctx = externalD3dContext ? externalD3dContext.Get() : d3dContext.Get();
232
+ if (ctx) {
233
+ ctx->ClearState();
234
+ ctx->Flush();
235
+ }
236
+
237
+ HRESULT hr = swapChain->ResizeBuffers(
238
+ 2, surfaceWidth, surfaceHeight,
239
+ DXGI_FORMAT_B8G8R8A8_UNORM, 0);
240
+ if (FAILED(hr)) {
241
+ printf("[DComp] ResizeBuffers failed: 0x%08lx\n", hr);
242
+ return false;
243
+ }
244
+
245
+ dcompDevice->Commit();
246
+ return true;
247
+ }
248
+
249
+ // Enable native resize tracking: subclass the target HWND to intercept
250
+ // WM_SIZE and auto-resize the swap chain without TS FFI round-trip.
251
+ void enableNativeResize() {
252
+ if (!initialized || !targetHwnd || nativeResizeHooked) return;
253
+
254
+ // Store this pointer for the subclass callback
255
+ SetPropA(targetHwnd, "DCompCompositor", (HANDLE)this);
256
+
257
+ SetWindowSubclass(targetHwnd, resizeSubclassProc, 1, (DWORD_PTR)this);
258
+ nativeResizeHooked = true;
259
+ // Native resize hook installed
260
+ }
261
+
262
+ void disableNativeResize() {
263
+ if (!nativeResizeHooked || !targetHwnd) return;
264
+ RemoveWindowSubclass(targetHwnd, resizeSubclassProc, 1);
265
+ RemovePropA(targetHwnd, "DCompCompositor");
266
+ nativeResizeHooked = false;
267
+ }
268
+
269
+ void shutdown() {
270
+ if (!initialized) return;
271
+
272
+ // Remove native resize hook
273
+ disableNativeResize();
274
+
275
+ // Release DComp
276
+ if (dcompTarget) dcompTarget->SetRoot(nullptr);
277
+ if (dcompDevice) dcompDevice->Commit();
278
+
279
+ rootVisual.Reset();
280
+ dcompTarget.Reset();
281
+ dcompDevice.Reset();
282
+ swapChain.Reset();
283
+
284
+ // Release external device (zero-copy bridge)
285
+ externalD3dContext.Reset();
286
+ externalD3dDevice.Reset();
287
+ zeroCopyMode = false;
288
+
289
+ d3dContext.Reset();
290
+ d3dDevice.Reset();
291
+ dxgiFactory.Reset();
292
+
293
+ initialized = false;
294
+ // Shutdown complete
295
+ }
296
+
297
+ // Accessors
298
+ bool isInitialized() const { return initialized; }
299
+ bool isZeroCopyMode() const { return zeroCopyMode; }
300
+ IDXGISwapChain1* getSwapChain() const { return swapChain.Get(); }
301
+ IDCompositionDevice* getDCompDevice() const { return dcompDevice.Get(); }
302
+ HWND getTargetHwnd() const { return targetHwnd; }
303
+ ID3D11DeviceContext* getExternalD3dContext() const { return externalD3dContext.Get(); }
304
+ ID3D11Device* getExternalD3dDevice() const { return externalD3dDevice.Get(); }
305
+
306
+ private:
307
+ HWND targetHwnd = NULL;
308
+ int surfaceWidth = 0;
309
+ int surfaceHeight = 0;
310
+ bool initialized = false;
311
+
312
+ // D3D11 (fallback device for DComp init)
313
+ ComPtr<ID3D11Device> d3dDevice;
314
+ ComPtr<ID3D11DeviceContext> d3dContext;
315
+
316
+ // DXGI
317
+ ComPtr<IDXGIFactory2> dxgiFactory;
318
+ ComPtr<IDXGISwapChain1> swapChain;
319
+
320
+ // DirectComposition visual tree
321
+ ComPtr<IDCompositionDevice> dcompDevice;
322
+ ComPtr<IDCompositionTarget> dcompTarget;
323
+ ComPtr<IDCompositionVisual> rootVisual;
324
+
325
+ // External D3D11 device (D3D11On12 from Dawn, for zero-copy bridge)
326
+ ComPtr<ID3D11Device> externalD3dDevice;
327
+ ComPtr<ID3D11DeviceContext> externalD3dContext;
328
+
329
+ // Zero-copy mode: swap chain created on Dawn's D3D11On12 device
330
+ bool zeroCopyMode = false;
331
+
332
+ // Native resize hook
333
+ bool nativeResizeHooked = false;
334
+
335
+ // Native subclass — handles WM_SIZE for auto-resize of swap chain.
336
+ static LRESULT CALLBACK resizeSubclassProc(
337
+ HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
338
+ UINT_PTR subclassId, DWORD_PTR refData
339
+ ) {
340
+ auto* self = (DCompCompositor*)refData;
341
+
342
+ if (msg == WM_SIZE && wParam != SIZE_MINIMIZED) {
343
+ int w = LOWORD(lParam);
344
+ int h = HIWORD(lParam);
345
+ if (self && self->isInitialized() && !self->isZeroCopyMode() && w > 0 && h > 0) {
346
+ self->resize(w, h);
347
+ }
348
+ }
349
+
350
+ return DefSubclassProc(hwnd, msg, wParam, lParam);
351
+ }
352
+ };