@legendapp/spark 0.0.1-next.7 → 0.0.1-next.9
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/package.json +8 -8
- package/schema.json +9 -0
- package/skills/spark/SKILL.md +1 -1
- package/skills/spark/UPGRADING.md +34 -0
- package/vendor/node_modules/@legendapp/spark-ai/package.json +3 -3
- package/vendor/node_modules/@legendapp/spark-audio/package.json +2 -2
- package/vendor/node_modules/@legendapp/spark-auth-session/package.json +3 -3
- package/vendor/node_modules/@legendapp/spark-cli/dist/add-desktop.js +1 -1
- package/vendor/node_modules/@legendapp/spark-cli/dist/build.js +16 -4
- package/vendor/node_modules/@legendapp/spark-cli/dist/create.js +16 -4
- package/vendor/node_modules/@legendapp/spark-cli/dist/init-template.cjs +2 -1
- package/vendor/node_modules/@legendapp/spark-cli/dist/macos-release.js +4 -0
- package/vendor/node_modules/@legendapp/spark-cli/dist/package-manager-config.js +3 -1
- package/vendor/node_modules/@legendapp/spark-cli/dist/project.js +10 -4
- package/vendor/node_modules/@legendapp/spark-cli/dist/release-driver.js +35 -0
- package/vendor/node_modules/@legendapp/spark-cli/dist/release.json +18 -18
- package/vendor/node_modules/@legendapp/spark-cli/dist/updates.js +18 -8
- package/vendor/node_modules/@legendapp/spark-cli/dist/windows.js +1 -1
- package/vendor/node_modules/@legendapp/spark-cli/package.json +4 -4
- package/vendor/node_modules/@legendapp/spark-cli/templates/blank-typescript/app.json +5 -1
- package/vendor/node_modules/@legendapp/spark-cli/templates/blank-typescript/gitignore +2 -1
- package/vendor/node_modules/@legendapp/spark-cli/templates/blank-typescript/package.json +8 -7
- package/vendor/node_modules/@legendapp/spark-cli/templates/universal/app.json +5 -1
- package/vendor/node_modules/@legendapp/spark-cli/templates/universal/gitignore +2 -1
- package/vendor/node_modules/@legendapp/spark-cli/templates/universal/package.json +9 -8
- package/vendor/node_modules/@legendapp/spark-cli/templates/windows/app.json +5 -1
- package/vendor/node_modules/@legendapp/spark-cli/templates/windows/gitignore +2 -1
- package/vendor/node_modules/@legendapp/spark-cli/templates/windows/package.json +9 -8
- package/vendor/node_modules/@legendapp/spark-clipboard/package.json +2 -2
- package/vendor/node_modules/@legendapp/spark-codex/cpp/HybridCodexAppServer.mm +88 -76
- package/vendor/node_modules/@legendapp/spark-codex/package.json +2 -2
- package/vendor/node_modules/@legendapp/spark-commands/package.json +5 -5
- package/vendor/node_modules/@legendapp/spark-context-menu/package.json +2 -2
- package/vendor/node_modules/@legendapp/spark-desktop-app/RNDesktopApp.podspec +12 -1
- package/vendor/node_modules/@legendapp/spark-desktop-app/macos/RNDesktopApp.mm +8 -1
- package/vendor/node_modules/@legendapp/spark-desktop-app/macos/SparkDesktop.h +8 -0
- package/vendor/node_modules/@legendapp/spark-desktop-app/macos/SparkDesktop.mm +84 -21
- package/vendor/node_modules/@legendapp/spark-desktop-app/macos/SparkInstance.h +27 -0
- package/vendor/node_modules/@legendapp/spark-desktop-app/macos/SparkInstance.mm +207 -0
- package/vendor/node_modules/@legendapp/spark-desktop-app/macos/SparkTestDriver.h +44 -0
- package/vendor/node_modules/@legendapp/spark-desktop-app/macos/SparkTestDriver.mm +349 -0
- package/vendor/node_modules/@legendapp/spark-desktop-app/macos/SparkWindow.mm +3 -1
- package/vendor/node_modules/@legendapp/spark-desktop-app/package.json +1 -1
- package/vendor/node_modules/@legendapp/spark-desktop-app/src/api.ts +39 -6
- package/vendor/node_modules/@legendapp/spark-desktop-app/src/contracts/menu.ts +21 -7
- package/vendor/node_modules/@legendapp/spark-desktop-config/accessibility-state.cjs +50 -0
- package/vendor/node_modules/@legendapp/spark-desktop-config/app.plugin.cjs +9 -1
- package/vendor/node_modules/@legendapp/spark-desktop-config/autolinking.cjs +8 -1
- package/vendor/node_modules/@legendapp/spark-desktop-config/config.cjs +1 -1
- package/vendor/node_modules/@legendapp/spark-desktop-config/config.d.cts +7 -1
- package/vendor/node_modules/@legendapp/spark-desktop-config/package.json +4 -4
- package/vendor/node_modules/@legendapp/spark-desktop-config/rtl-scroll-view.cjs +144 -0
- package/vendor/node_modules/@legendapp/spark-desktop-config/rtl-text-alignment.cjs +60 -0
- package/vendor/node_modules/@legendapp/spark-desktop-config/rtl-text-input-alignment.cjs +112 -0
- package/vendor/node_modules/@legendapp/spark-desktop-config/schema.json +9 -0
- package/vendor/node_modules/@legendapp/spark-desktop-config/test-driver.cjs +11 -0
- package/vendor/node_modules/@legendapp/spark-desktop-config/test-driver.d.cts +2 -0
- package/vendor/node_modules/@legendapp/spark-desktop-config/text-input-accessibility.cjs +47 -0
- package/vendor/node_modules/@legendapp/spark-desktop-config/updates.cjs +4 -2
- package/vendor/node_modules/@legendapp/spark-desktop-config/validation.cjs +2 -1
- package/vendor/node_modules/@legendapp/spark-desktop-config/windows.plugin.cjs +1 -1
- package/vendor/node_modules/@legendapp/spark-desktop-host/AppDelegate.mm +18 -6
- package/vendor/node_modules/@legendapp/spark-desktop-host/package.json +2 -2
- package/vendor/node_modules/@legendapp/spark-desktop-host/windows/application.inc +61 -15
- package/vendor/node_modules/@legendapp/spark-desktop-host/windows/launch.inc +67 -0
- package/vendor/node_modules/@legendapp/spark-desktop-links/package.json +2 -2
- package/vendor/node_modules/@legendapp/spark-desktop-shortcuts/package.json +2 -2
- package/vendor/node_modules/@legendapp/spark-desktop-windows/RNDesktopWindows.podspec +3 -0
- package/vendor/node_modules/@legendapp/spark-desktop-windows/macos/RNDesktopWindows.mm +5 -2
- package/vendor/node_modules/@legendapp/spark-desktop-windows/macos/window-manager/RNWindowManager.mm +22 -3
- package/vendor/node_modules/@legendapp/spark-desktop-windows/package.json +3 -3
- package/vendor/node_modules/@legendapp/spark-documents/package.json +7 -7
- package/vendor/node_modules/@legendapp/spark-drag-drop/package.json +2 -2
- package/vendor/node_modules/@legendapp/spark-file-dialog/RNFileDialog.podspec +1 -0
- package/vendor/node_modules/@legendapp/spark-file-dialog/ios/RNFileDialog.mm +153 -64
- package/vendor/node_modules/@legendapp/spark-file-dialog/package.json +2 -2
- package/vendor/node_modules/@legendapp/spark-file-dialog/src/index.ts +19 -7
- package/vendor/node_modules/@legendapp/spark-file-dialog/src/native.ts +5 -1
- package/vendor/node_modules/@legendapp/spark-file-dialog/src/options.ts +42 -4
- package/vendor/node_modules/@legendapp/spark-file-dialog/src/types.ts +18 -2
- package/vendor/node_modules/@legendapp/spark-file-system/package.json +2 -2
- package/vendor/node_modules/@legendapp/spark-global-shortcuts/package.json +2 -2
- package/vendor/node_modules/@legendapp/spark-html/package.json +2 -2
- package/vendor/node_modules/@legendapp/spark-markdown/package.json +2 -2
- package/vendor/node_modules/@legendapp/spark-markdown/src/Markdown.tsx +2 -1
- package/vendor/node_modules/@legendapp/spark-message-dialog/macos/RNDesktopMessageDialog.mm +113 -12
- package/vendor/node_modules/@legendapp/spark-message-dialog/package.json +2 -2
- package/vendor/node_modules/@legendapp/spark-message-dialog/src/index.ts +110 -14
- package/vendor/node_modules/@legendapp/spark-native-menu/ios/RNNativeMenu.mm +116 -8
- package/vendor/node_modules/@legendapp/spark-native-menu/package.json +2 -2
- package/vendor/node_modules/@legendapp/spark-native-menu/src/api.ts +63 -18
- package/vendor/node_modules/@legendapp/spark-native-menu/src/hooks.ts +9 -3
- package/vendor/node_modules/@legendapp/spark-notifications/package.json +2 -2
- package/vendor/node_modules/@legendapp/spark-processes/package.json +2 -2
- package/vendor/node_modules/@legendapp/spark-rich-text/package.json +2 -2
- package/vendor/node_modules/@legendapp/spark-secure-storage/package.json +2 -2
- package/vendor/node_modules/@legendapp/spark-settings/package.json +3 -3
- package/vendor/node_modules/@legendapp/spark-settings-ui/package.json +4 -4
- package/vendor/node_modules/@legendapp/spark-settings-ui/src/index.tsx +5 -2
- package/vendor/node_modules/@legendapp/spark-sqlite/package.json +3 -3
- package/vendor/node_modules/@legendapp/spark-system/macos/RNDesktopSystem.mm +13 -3
- package/vendor/node_modules/@legendapp/spark-system/macos/SparkAppearancePreferences.h +9 -0
- package/vendor/node_modules/@legendapp/spark-system/macos/SparkAppearancePreferences.mm +31 -0
- package/vendor/node_modules/@legendapp/spark-system/macos/SparkDockTile.h +24 -0
- package/vendor/node_modules/@legendapp/spark-system/macos/SparkDockTile.mm +101 -0
- package/vendor/node_modules/@legendapp/spark-system/package.json +2 -2
- package/vendor/node_modules/@legendapp/spark-system/src/index.ts +73 -7
- package/vendor/node_modules/@legendapp/spark-tray/package.json +2 -2
- package/vendor/node_modules/@legendapp/spark-ui/macos/RNSparkButton.mm +2 -0
- package/vendor/node_modules/@legendapp/spark-ui/macos/RNSparkLayoutDirection.h +17 -0
- package/vendor/node_modules/@legendapp/spark-ui/macos/RNSparkTextInput.mm +6 -1
- package/vendor/node_modules/@legendapp/spark-ui/macos/appkit-split-view/RNSidebarSplitViewComponent.mm +55 -8
- package/vendor/node_modules/@legendapp/spark-ui/macos/glass-effect-view/RNGlassEffectView.mm +11 -7
- package/vendor/node_modules/@legendapp/spark-ui/macos/native-select/RNNativeSegmentedControl.mm +4 -0
- package/vendor/node_modules/@legendapp/spark-ui/macos/native-select/RNNativeSelect.mm +4 -0
- package/vendor/node_modules/@legendapp/spark-ui/macos/sf-symbol/RNSFSymbol.mm +4 -0
- package/vendor/node_modules/@legendapp/spark-ui/macos/sidebar/RNSidebar.mm +77 -14
- package/vendor/node_modules/@legendapp/spark-ui/macos/swipe-actions/RNSwipeActions.mm +31 -7
- package/vendor/node_modules/@legendapp/spark-ui/macos/text-input-search/RNTextInputSearch.mm +25 -1
- package/vendor/node_modules/@legendapp/spark-ui/package.json +2 -2
- package/vendor/node_modules/@legendapp/spark-ui/src/sidebar/SidebarNativeComponent.ts +5 -0
- package/vendor/node_modules/@legendapp/spark-ui/src/sidebar/index.tsx +19 -8
- package/vendor/node_modules/@legendapp/spark-updates/macos/RNDesktopUpdates.mm +25 -13
- package/vendor/node_modules/@legendapp/spark-updates/macos/SparkUpdateState.h +39 -0
- package/vendor/node_modules/@legendapp/spark-updates/package.json +2 -2
- package/vendor/node_modules/@legendapp/spark-updates/src/index.ts +45 -9
- package/vendor/node_modules/@legendapp/spark-webview/package.json +2 -2
- package/vendor/node_modules/@legendapp/spark-window-options/package.json +1 -1
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
#import "SparkDesktop.h"
|
|
2
2
|
#import "SparkDesktopError.h"
|
|
3
3
|
#import "SparkQuitCoordinator.h"
|
|
4
|
+
#import "SparkInstance.h"
|
|
5
|
+
#import "SparkTestDriver.h"
|
|
6
|
+
#import <sys/wait.h>
|
|
4
7
|
#import <CommonCrypto/CommonDigest.h>
|
|
5
|
-
#import <sys/file.h>
|
|
6
|
-
#import <fcntl.h>
|
|
7
|
-
#import <unistd.h>
|
|
8
8
|
NSString * const SparkDesktopEvent = @"SparkDesktopEvent";
|
|
9
9
|
static NSMutableDictionary<NSString *, NSDictionary *> *jsQuitReplies;
|
|
10
10
|
static NSUInteger quitGeneration = 0;
|
|
11
11
|
static NSMutableArray *pendingURLs;
|
|
12
12
|
static NSString *initialURL;
|
|
13
13
|
static BOOL launchComplete = NO;
|
|
14
|
+
// Non-nil only in a second process, which forwards its open requests instead of handling them.
|
|
15
|
+
static NSMutableArray<NSURL *> *forwardedURLs;
|
|
14
16
|
|
|
15
17
|
NSString *SparkJSON(id value) {
|
|
16
18
|
NSData *data = [NSJSONSerialization dataWithJSONObject:value ?: NSNull.null options:NSJSONWritingFragmentsAllowed error:nil];
|
|
@@ -72,6 +74,7 @@ void SparkEmit(NSDictionary *event) {
|
|
|
72
74
|
void SparkMarkLaunchComplete(void) { launchComplete = YES; }
|
|
73
75
|
NSString *SparkInitialURL(void) { return initialURL; }
|
|
74
76
|
void SparkOpenURLs(NSArray<NSURL *> *urls) {
|
|
77
|
+
if (forwardedURLs) { [forwardedURLs addObjectsFromArray:urls]; return; }
|
|
75
78
|
if (!pendingURLs) pendingURLs = [NSMutableArray new];
|
|
76
79
|
for (NSURL *url in urls) {
|
|
77
80
|
if (!launchComplete && !url.isFileURL && !initialURL) initialURL = url.absoluteString;
|
|
@@ -111,25 +114,85 @@ void SparkReplyQuit(NSString *identifier, BOOL allow, NSUInteger generation) {
|
|
|
111
114
|
void SparkObserveQuitDecision(SparkQuitReply observer) { [SparkQuitCoordinatorInstance() observeDecision:observer]; }
|
|
112
115
|
NSApplicationTerminateReply SparkShouldQuit(void) { return [SparkQuitCoordinatorInstance() requestQuit]; }
|
|
113
116
|
|
|
117
|
+
// A forwarded launch reaches the owner as both a secondInstance event and, for its
|
|
118
|
+
// files and URLs, the same replayable open requests as a direct launch.
|
|
119
|
+
static void SparkReceiveLaunch(NSDictionary *launch) {
|
|
120
|
+
[NSApp activateIgnoringOtherApps:YES];
|
|
121
|
+
BOOL visible = NO;
|
|
122
|
+
for (NSWindow *window in NSApp.orderedWindows) if (window.isVisible && ![window isKindOfClass:NSPanel.class]) { visible = YES; break; }
|
|
123
|
+
[NSApp.delegate applicationShouldHandleReopen:NSApp hasVisibleWindows:visible];
|
|
124
|
+
SparkEmit(@{ @"type": @"secondInstance", @"arguments": launch[@"arguments"], @"workingDirectory": launch[@"workingDirectory"], @"files": launch[@"files"], @"urls": launch[@"urls"] });
|
|
125
|
+
NSMutableArray<NSURL *> *urls = [NSMutableArray new];
|
|
126
|
+
for (NSString *path in launch[@"files"]) [urls addObject:[NSURL fileURLWithPath:path]];
|
|
127
|
+
for (NSString *value in launch[@"urls"]) { NSURL *url = [NSURL URLWithString:value]; if (url) [urls addObject:url]; }
|
|
128
|
+
if (urls.count) SparkOpenURLs(urls);
|
|
129
|
+
}
|
|
130
|
+
[[noreturn]] static void SparkLaunchFailed(NSString *message, NSError *error) {
|
|
131
|
+
NSLog(@"%@: %@", message, error.localizedDescription);
|
|
132
|
+
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
|
|
133
|
+
[NSApp activateIgnoringOtherApps:YES];
|
|
134
|
+
NSAlert *alert = [NSAlert new];
|
|
135
|
+
alert.messageText = message;
|
|
136
|
+
alert.informativeText = error.localizedDescription;
|
|
137
|
+
[alert runModal];
|
|
138
|
+
exit(1);
|
|
139
|
+
}
|
|
140
|
+
// The second process stays invisible until AppKit has delivered the open requests it
|
|
141
|
+
// was launched with (they arrive before didFinishLaunching), then forwards and exits.
|
|
142
|
+
static void SparkForwardLaunch(void) {
|
|
143
|
+
forwardedURLs = [NSMutableArray new];
|
|
144
|
+
[NSApp setActivationPolicy:NSApplicationActivationPolicyProhibited];
|
|
145
|
+
__block id observer = [NSNotificationCenter.defaultCenter addObserverForName:NSApplicationDidFinishLaunchingNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
|
|
146
|
+
[NSNotificationCenter.defaultCenter removeObserver:observer];
|
|
147
|
+
NSMutableArray<NSString *> *files = [NSMutableArray new], *urls = [NSMutableArray new];
|
|
148
|
+
for (NSURL *url in forwardedURLs) { if (url.isFileURL) [files addObject:url.path]; else [urls addObject:url.absoluteString]; }
|
|
149
|
+
NSDictionary *launch = @{ @"arguments": NSProcessInfo.processInfo.arguments, @"workingDirectory": NSFileManager.defaultManager.currentDirectoryPath ?: @"/", @"files": files, @"urls": urls };
|
|
150
|
+
NSError *error;
|
|
151
|
+
if (!SparkInstanceForward(NSTemporaryDirectory(), SparkNamespace(), launch, 10, &error)) SparkLaunchFailed(@"The running app did not accept this launch", error);
|
|
152
|
+
exit(0);
|
|
153
|
+
}];
|
|
154
|
+
}
|
|
155
|
+
static NSString *safeModeTrigger;
|
|
156
|
+
static BOOL relaunched = NO, instanceOwner = NO;
|
|
157
|
+
BOOL SparkSafeMode(void) {
|
|
158
|
+
static dispatch_once_t once;
|
|
159
|
+
dispatch_once(&once, ^{
|
|
160
|
+
if ([NSProcessInfo.processInfo.arguments containsObject:@"--spark-safe-mode"]) safeModeTrigger = @"flag";
|
|
161
|
+
// A test-driver session never reads the keyboard: someone typing while it launches must not change the run.
|
|
162
|
+
else if (!SparkTestDriverActive() && (NSEvent.modifierFlags & NSEventModifierFlagShift)) safeModeTrigger = @"modifier";
|
|
163
|
+
});
|
|
164
|
+
return safeModeTrigger != nil;
|
|
165
|
+
}
|
|
166
|
+
NSDictionary *SparkLaunchState(void) {
|
|
167
|
+
return @{ @"safeMode": SparkSafeMode() ? safeModeTrigger : NSNull.null, @"relaunched": @(relaunched),
|
|
168
|
+
@"instance": instanceOwner ? @"primary" : @"test-driver" };
|
|
169
|
+
}
|
|
114
170
|
BOOL SparkAcquireInstance(void) {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
[NSDistributedNotificationCenter.defaultCenter postNotificationName:name object:nil userInfo:@{ @"arguments": NSProcessInfo.processInfo.arguments } deliverImmediately:YES];
|
|
124
|
-
return NO;
|
|
171
|
+
SparkSafeMode();
|
|
172
|
+
NSString *parent = NSProcessInfo.processInfo.environment[@"SPARK_RELAUNCH_PARENT"];
|
|
173
|
+
NSError *error;
|
|
174
|
+
if (parent) {
|
|
175
|
+
unsetenv("SPARK_RELAUNCH_PARENT");
|
|
176
|
+
relaunched = YES;
|
|
177
|
+
// The previous instance holds the lock until it exits; it kills this process if its quit is vetoed.
|
|
178
|
+
if (!SparkInstanceWaitForExit(parent.intValue, &error)) SparkLaunchFailed(@"Unable to relaunch", error);
|
|
125
179
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
180
|
+
instanceOwner = SparkInstanceAcquire(NSTemporaryDirectory(), SparkNamespace(), ^(NSDictionary *launch) { SparkReceiveLaunch(launch); }, &error);
|
|
181
|
+
if (error) SparkLaunchFailed(@"Unable to establish the app instance lock", error);
|
|
182
|
+
if (!instanceOwner) SparkForwardLaunch();
|
|
183
|
+
return instanceOwner;
|
|
184
|
+
}
|
|
185
|
+
static pid_t relaunchChild = 0;
|
|
186
|
+
void SparkRelaunch(NSArray<NSString *> *arguments, void (^completion)(NSDictionary *result, NSError *error)) {
|
|
187
|
+
if (!relaunchChild) {
|
|
188
|
+
NSError *error;
|
|
189
|
+
relaunchChild = SparkInstanceSpawnRelaunch(arguments, &error);
|
|
190
|
+
if (!relaunchChild) { completion(nil, error); return; }
|
|
191
|
+
}
|
|
192
|
+
SparkObserveQuitDecision(^(BOOL allow) {
|
|
193
|
+
if (!allow && relaunchChild) { kill(relaunchChild, SIGKILL); waitpid(relaunchChild, NULL, 0); relaunchChild = 0; }
|
|
194
|
+
completion(allow ? @{ @"quitRequested": @YES } : @{ @"quitRequested": @NO, @"reason": @"vetoed" }, nil);
|
|
195
|
+
});
|
|
196
|
+
[NSRunLoop.mainRunLoop performBlock:^{ [NSApp terminate:nil]; }];
|
|
134
197
|
}
|
|
135
198
|
NSMenu *SparkDockMenu = nil;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
#import <Foundation/Foundation.h>
|
|
2
|
+
|
|
3
|
+
// Per-namespace single-instance ownership and launch forwarding. Foundation and POSIX
|
|
4
|
+
// only, so tests/app-instance.native.mm can exercise it across real processes.
|
|
5
|
+
//
|
|
6
|
+
// `directory` holds three files named after `name`: `.lock` (held for the owner's
|
|
7
|
+
// lifetime; the kernel releases it on exit or crash), `.setup` (held only while an
|
|
8
|
+
// owner starts listening, so a launch that loses the race always finds a listening
|
|
9
|
+
// owner) and `.sock` (the owner's Unix-domain handoff socket).
|
|
10
|
+
|
|
11
|
+
// A forwarded launch: { arguments: [string], workingDirectory: string, files: [string], urls: [string] }.
|
|
12
|
+
typedef void (^SparkInstanceReceiver)(NSDictionary *launch);
|
|
13
|
+
|
|
14
|
+
// YES: this process owns `name` and `receiver` runs on the main queue for each
|
|
15
|
+
// forwarded launch. NO with *error nil: another process owns it. NO with *error: failure.
|
|
16
|
+
FOUNDATION_EXPORT BOOL SparkInstanceAcquire(NSString *directory, NSString *name, SparkInstanceReceiver receiver, NSError **error);
|
|
17
|
+
// Sends `launch` to the owner. YES only after the owner's receiver has run.
|
|
18
|
+
FOUNDATION_EXPORT BOOL SparkInstanceForward(NSString *directory, NSString *name, NSDictionary *launch, NSTimeInterval timeout, NSError **error);
|
|
19
|
+
// Validates a decoded launch payload.
|
|
20
|
+
FOUNDATION_EXPORT BOOL SparkInstanceValidLaunch(id launch);
|
|
21
|
+
// Reuses arguments after the executable, excluding the safe-mode launch flag.
|
|
22
|
+
FOUNDATION_EXPORT NSArray<NSString *> *SparkInstanceRelaunchArguments(NSArray<NSString *> *process);
|
|
23
|
+
// Starts this executable with `arguments` and SPARK_RELAUNCH_PARENT set to this process.
|
|
24
|
+
// Returns the child pid, or 0 with *error.
|
|
25
|
+
FOUNDATION_EXPORT pid_t SparkInstanceSpawnRelaunch(NSArray<NSString *> *arguments, NSError **error);
|
|
26
|
+
// Blocks until `pid` has exited (immediately when it already has).
|
|
27
|
+
FOUNDATION_EXPORT BOOL SparkInstanceWaitForExit(pid_t pid, NSError **error);
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
#import "SparkInstance.h"
|
|
2
|
+
#import <fcntl.h>
|
|
3
|
+
#import <signal.h>
|
|
4
|
+
#import <spawn.h>
|
|
5
|
+
#import <sys/event.h>
|
|
6
|
+
#import <sys/file.h>
|
|
7
|
+
#import <sys/socket.h>
|
|
8
|
+
#import <unistd.h>
|
|
9
|
+
#include <vector>
|
|
10
|
+
|
|
11
|
+
extern char **environ;
|
|
12
|
+
static const uint32_t SparkInstanceMaxPayload = 1 << 20;
|
|
13
|
+
|
|
14
|
+
static NSError *SparkPOSIXError(NSString *operation, int code) {
|
|
15
|
+
return [NSError errorWithDomain:NSPOSIXErrorDomain code:code userInfo:@{
|
|
16
|
+
NSLocalizedDescriptionKey: [NSString stringWithFormat:@"%@: %s", operation, strerror(code)] }];
|
|
17
|
+
}
|
|
18
|
+
static BOOL SparkFail(NSError **error, NSString *operation, int code) {
|
|
19
|
+
if (error) *error = SparkPOSIXError(operation, code);
|
|
20
|
+
return NO;
|
|
21
|
+
}
|
|
22
|
+
static int SparkOpenAt(int directory, NSString *name, NSString *suffix) {
|
|
23
|
+
const char *path = [name stringByAppendingString:suffix].fileSystemRepresentation;
|
|
24
|
+
int fd;
|
|
25
|
+
// XNU can fail O_CREAT with ENOENT while another process creates the same file;
|
|
26
|
+
// tests/app-instance-native.test.ts launches processes simultaneously to cover it.
|
|
27
|
+
for (int attempt = 0; (fd = openat(directory, path, O_CREAT | O_RDWR | O_NOFOLLOW | O_CLOEXEC, 0600)) < 0 && errno == ENOENT && attempt < 16; attempt++) {}
|
|
28
|
+
return fd;
|
|
29
|
+
}
|
|
30
|
+
// XNU accepts Unix-domain addresses up to SOCK_MAXADDRLEN, longer than the nominal
|
|
31
|
+
// 104-byte sun_path; sandbox container temporary paths need that room.
|
|
32
|
+
struct SparkSocketPath { uint8_t length; sa_family_t family; char path[SOCK_MAXADDRLEN - 2]; };
|
|
33
|
+
static BOOL SparkSocketAddress(NSString *directory, NSString *name, struct SparkSocketPath *address) {
|
|
34
|
+
memset(address, 0, sizeof(*address));
|
|
35
|
+
address->family = AF_UNIX;
|
|
36
|
+
const char *path = [directory stringByAppendingPathComponent:[name stringByAppendingString:@".sock"]].fileSystemRepresentation;
|
|
37
|
+
if (strlcpy(address->path, path, sizeof(address->path)) >= sizeof(address->path)) { errno = ENAMETOOLONG; return NO; }
|
|
38
|
+
address->length = (uint8_t)(offsetof(struct SparkSocketPath, path) + strlen(path) + 1);
|
|
39
|
+
return YES;
|
|
40
|
+
}
|
|
41
|
+
static int SparkSocket(NSTimeInterval timeout) {
|
|
42
|
+
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
43
|
+
if (fd < 0) return -1;
|
|
44
|
+
int one = 1;
|
|
45
|
+
struct timeval limit = { (time_t)timeout, (suseconds_t)((timeout - (time_t)timeout) * 1e6) };
|
|
46
|
+
if (fcntl(fd, F_SETFD, FD_CLOEXEC) || setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one))
|
|
47
|
+
|| (timeout > 0 && (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &limit, sizeof(limit)) || setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &limit, sizeof(limit))))) {
|
|
48
|
+
int code = errno; close(fd); errno = code; return -1;
|
|
49
|
+
}
|
|
50
|
+
return fd;
|
|
51
|
+
}
|
|
52
|
+
static BOOL SparkTransfer(int fd, void *bytes, size_t length, BOOL writing) {
|
|
53
|
+
for (size_t done = 0; done < length;) {
|
|
54
|
+
ssize_t count = writing ? write(fd, (char *)bytes + done, length - done) : read(fd, (char *)bytes + done, length - done);
|
|
55
|
+
if (count > 0) done += (size_t)count;
|
|
56
|
+
else if (count < 0 && errno == EINTR) continue;
|
|
57
|
+
else { if (count == 0) errno = ECONNRESET; return NO; }
|
|
58
|
+
}
|
|
59
|
+
return YES;
|
|
60
|
+
}
|
|
61
|
+
static BOOL SparkStrings(id value) {
|
|
62
|
+
if (![value isKindOfClass:NSArray.class]) return NO;
|
|
63
|
+
for (id item in value) if (![item isKindOfClass:NSString.class]) return NO;
|
|
64
|
+
return YES;
|
|
65
|
+
}
|
|
66
|
+
BOOL SparkInstanceValidLaunch(id launch) {
|
|
67
|
+
return [launch isKindOfClass:NSDictionary.class] && SparkStrings(launch[@"arguments"]) && [launch[@"workingDirectory"] isKindOfClass:NSString.class]
|
|
68
|
+
&& SparkStrings(launch[@"files"]) && SparkStrings(launch[@"urls"]);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Reads one length-prefixed launch, runs the receiver on the main queue, then acknowledges.
|
|
72
|
+
static void SparkServeConnection(int fd, SparkInstanceReceiver receiver) {
|
|
73
|
+
uint32_t length = 0;
|
|
74
|
+
NSMutableData *body = nil;
|
|
75
|
+
if (SparkTransfer(fd, &length, sizeof(length), NO) && (length = ntohl(length)) > 0 && length <= SparkInstanceMaxPayload) {
|
|
76
|
+
body = [NSMutableData dataWithLength:length];
|
|
77
|
+
if (!SparkTransfer(fd, body.mutableBytes, length, NO)) body = nil;
|
|
78
|
+
}
|
|
79
|
+
id launch = body ? [NSJSONSerialization JSONObjectWithData:body options:0 error:nil] : nil;
|
|
80
|
+
if (!SparkInstanceValidLaunch(launch)) { NSLog(@"Spark rejected a malformed forwarded launch"); close(fd); return; }
|
|
81
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
82
|
+
receiver(launch);
|
|
83
|
+
uint8_t accepted = 1;
|
|
84
|
+
SparkTransfer(fd, &accepted, 1, YES);
|
|
85
|
+
close(fd);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
BOOL SparkInstanceAcquire(NSString *directory, NSString *name, SparkInstanceReceiver receiver, NSError **error) {
|
|
90
|
+
if (error) *error = nil;
|
|
91
|
+
int directoryFD = open(directory.fileSystemRepresentation, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
|
|
92
|
+
if (directoryFD < 0) return SparkFail(error, @"Unable to open the instance directory", errno);
|
|
93
|
+
int setupFD = SparkOpenAt(directoryFD, name, @".setup");
|
|
94
|
+
if (setupFD < 0 || flock(setupFD, LOCK_EX)) {
|
|
95
|
+
int code = errno; if (setupFD >= 0) close(setupFD); close(directoryFD);
|
|
96
|
+
return SparkFail(error, @"Unable to lock instance setup", code);
|
|
97
|
+
}
|
|
98
|
+
// Closing setupFD releases the setup lock on every path below.
|
|
99
|
+
int lockFD = SparkOpenAt(directoryFD, name, @".lock");
|
|
100
|
+
if (lockFD < 0) { int code = errno; close(setupFD); close(directoryFD); return SparkFail(error, @"Unable to open the instance lock", code); }
|
|
101
|
+
if (flock(lockFD, LOCK_EX | LOCK_NB)) {
|
|
102
|
+
int code = errno; close(lockFD); close(setupFD); close(directoryFD);
|
|
103
|
+
return code == EWOULDBLOCK ? NO : SparkFail(error, @"Unable to take the instance lock", code);
|
|
104
|
+
}
|
|
105
|
+
struct SparkSocketPath address;
|
|
106
|
+
int listenFD = -1;
|
|
107
|
+
BOOL addressed = SparkSocketAddress(directory, name, &address);
|
|
108
|
+
// Only the lock owner touches the socket path, so an existing one was left by a crashed owner.
|
|
109
|
+
if (addressed) unlink(address.path);
|
|
110
|
+
if (!addressed || (listenFD = SparkSocket(0)) < 0 || bind(listenFD, (struct sockaddr *)&address, address.length) || listen(listenFD, 16) || fcntl(listenFD, F_SETFL, O_NONBLOCK)) {
|
|
111
|
+
int code = errno; if (listenFD >= 0) close(listenFD); close(lockFD); close(setupFD); close(directoryFD);
|
|
112
|
+
return SparkFail(error, @"Unable to listen for forwarded launches", code);
|
|
113
|
+
}
|
|
114
|
+
close(directoryFD);
|
|
115
|
+
dispatch_queue_t queue = dispatch_queue_create("spark.instance", DISPATCH_QUEUE_SERIAL);
|
|
116
|
+
// Retained with the lock for the process lifetime.
|
|
117
|
+
static dispatch_source_t source;
|
|
118
|
+
source = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, (uintptr_t)listenFD, 0, queue);
|
|
119
|
+
SparkInstanceReceiver handler = [receiver copy];
|
|
120
|
+
dispatch_source_set_event_handler(source, ^{
|
|
121
|
+
for (int client; (client = accept(listenFD, NULL, NULL)) >= 0;) {
|
|
122
|
+
// The socket sits in the per-user temporary directory; also refuse peers running as another user.
|
|
123
|
+
uid_t uid; gid_t gid;
|
|
124
|
+
if (getpeereid(client, &uid, &gid) || uid != getuid()) { close(client); continue; }
|
|
125
|
+
int one = 1; struct timeval limit = { 5, 0 };
|
|
126
|
+
fcntl(client, F_SETFD, FD_CLOEXEC); fcntl(client, F_SETFL, 0);
|
|
127
|
+
setsockopt(client, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one));
|
|
128
|
+
setsockopt(client, SOL_SOCKET, SO_RCVTIMEO, &limit, sizeof(limit));
|
|
129
|
+
setsockopt(client, SOL_SOCKET, SO_SNDTIMEO, &limit, sizeof(limit));
|
|
130
|
+
SparkServeConnection(client, handler);
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
dispatch_resume(source);
|
|
134
|
+
close(setupFD);
|
|
135
|
+
return YES;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
BOOL SparkInstanceForward(NSString *directory, NSString *name, NSDictionary *launch, NSTimeInterval timeout, NSError **error) {
|
|
139
|
+
if (error) *error = nil;
|
|
140
|
+
NSData *body = SparkInstanceValidLaunch(launch) ? [NSJSONSerialization dataWithJSONObject:launch options:0 error:nil] : nil;
|
|
141
|
+
if (!body || body.length > SparkInstanceMaxPayload) return SparkFail(error, @"Invalid forwarded launch", EINVAL);
|
|
142
|
+
struct SparkSocketPath address;
|
|
143
|
+
int fd = -1;
|
|
144
|
+
if (!SparkSocketAddress(directory, name, &address) || (fd = SparkSocket(timeout)) < 0 || connect(fd, (struct sockaddr *)&address, address.length)) {
|
|
145
|
+
int code = errno; if (fd >= 0) close(fd);
|
|
146
|
+
return SparkFail(error, @"The running instance is not accepting launches", code);
|
|
147
|
+
}
|
|
148
|
+
uint32_t size = htonl((uint32_t)body.length);
|
|
149
|
+
uint8_t accepted = 0;
|
|
150
|
+
BOOL sent = SparkTransfer(fd, &size, sizeof(size), YES) && SparkTransfer(fd, (void *)body.bytes, body.length, YES);
|
|
151
|
+
BOOL acknowledged = sent && SparkTransfer(fd, &accepted, 1, NO) && accepted == 1;
|
|
152
|
+
int code = errno;
|
|
153
|
+
close(fd);
|
|
154
|
+
return acknowledged ? YES : SparkFail(error, @"The running instance did not accept this launch", sent ? (code ?: EPROTO) : code);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
NSArray<NSString *> *SparkInstanceRelaunchArguments(NSArray<NSString *> *process) {
|
|
158
|
+
NSMutableArray<NSString *> *arguments = [NSMutableArray new];
|
|
159
|
+
for (NSUInteger i = 1; i < process.count; ++i) {
|
|
160
|
+
if (![process[i] isEqual:@"--spark-safe-mode"]) [arguments addObject:process[i]];
|
|
161
|
+
}
|
|
162
|
+
return arguments;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
pid_t SparkInstanceSpawnRelaunch(NSArray<NSString *> *arguments, NSError **error) {
|
|
166
|
+
NSString *executable = NSBundle.mainBundle.executablePath;
|
|
167
|
+
if (!executable) { SparkFail(error, @"Unable to find this executable", ENOENT); return 0; }
|
|
168
|
+
NSMutableArray<NSString *> *argv = [NSMutableArray arrayWithObject:executable];
|
|
169
|
+
[argv addObjectsFromArray:arguments];
|
|
170
|
+
NSString *parent = [NSString stringWithFormat:@"SPARK_RELAUNCH_PARENT=%d", getpid()];
|
|
171
|
+
NSMutableArray<NSString *> *environment = [NSMutableArray arrayWithObject:parent];
|
|
172
|
+
for (char **item = environ; *item; item++) if (strncmp(*item, "SPARK_RELAUNCH_PARENT=", 22)) [environment addObject:@(*item)];
|
|
173
|
+
std::vector<char *> args, env;
|
|
174
|
+
for (NSString *value in argv) args.push_back(strdup(value.UTF8String));
|
|
175
|
+
for (NSString *value in environment) env.push_back(strdup(value.UTF8String));
|
|
176
|
+
args.push_back(nullptr); env.push_back(nullptr);
|
|
177
|
+
posix_spawnattr_t attributes; posix_spawn_file_actions_t actions;
|
|
178
|
+
posix_spawnattr_init(&attributes); posix_spawn_file_actions_init(&actions);
|
|
179
|
+
sigset_t none, all; sigemptyset(&none); sigfillset(&all);
|
|
180
|
+
// Only stdio is inherited: the instance lock and handoff socket stay with this process.
|
|
181
|
+
posix_spawnattr_setflags(&attributes, POSIX_SPAWN_CLOEXEC_DEFAULT | POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF);
|
|
182
|
+
posix_spawnattr_setsigmask(&attributes, &none); posix_spawnattr_setsigdefault(&attributes, &all);
|
|
183
|
+
for (int fd = 0; fd <= 2; fd++) posix_spawn_file_actions_addinherit_np(&actions, fd);
|
|
184
|
+
pid_t child = 0;
|
|
185
|
+
int code = posix_spawn(&child, args[0], &actions, &attributes, args.data(), env.data());
|
|
186
|
+
posix_spawn_file_actions_destroy(&actions); posix_spawnattr_destroy(&attributes);
|
|
187
|
+
for (char *value : args) free(value);
|
|
188
|
+
for (char *value : env) free(value);
|
|
189
|
+
if (code) { SparkFail(error, @"Unable to start the relaunched instance", code); return 0; }
|
|
190
|
+
return child;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
BOOL SparkInstanceWaitForExit(pid_t pid, NSError **error) {
|
|
194
|
+
int queue = kqueue();
|
|
195
|
+
if (queue < 0) return SparkFail(error, @"Unable to wait for the previous instance", errno);
|
|
196
|
+
struct kevent change, event;
|
|
197
|
+
EV_SET(&change, pid, EVFILT_PROC, EV_ADD | EV_ONESHOT | EV_RECEIPT, NOTE_EXIT, 0, NULL);
|
|
198
|
+
int count;
|
|
199
|
+
while ((count = kevent(queue, &change, 1, &event, 1, NULL)) < 0 && errno == EINTR) {}
|
|
200
|
+
if (count < 0) { int code = errno; close(queue); return SparkFail(error, @"Unable to wait for the previous instance", code); }
|
|
201
|
+
// EV_RECEIPT reports registration; ESRCH means the process has already exited.
|
|
202
|
+
if ((event.flags & EV_ERROR) && event.data) { close(queue); return event.data == ESRCH ? YES : SparkFail(error, @"Unable to wait for the previous instance", (int)event.data); }
|
|
203
|
+
while ((count = kevent(queue, NULL, 0, &event, 1, NULL)) < 0 && errno == EINTR) {}
|
|
204
|
+
int code = errno;
|
|
205
|
+
close(queue);
|
|
206
|
+
return count == 1 ? YES : SparkFail(error, @"Unable to wait for the previous instance", code);
|
|
207
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
#import <AppKit/AppKit.h>
|
|
2
|
+
|
|
3
|
+
// In-app test driver for the release gate (docs/test-driver.md). Inert unless the
|
|
4
|
+
// gate runner launches the app with SPARK_TEST_DRIVER_DIR naming a private run
|
|
5
|
+
// directory that holds a one-time token. Main-thread confined.
|
|
6
|
+
//
|
|
7
|
+
// The RNDesktopApp, RNSparkUI, RNSparkMotion, RNDesktopSystem and RNDesktopWindows podspecs set SPARK_TEST_DRIVER: 1 in
|
|
8
|
+
// Debug builds, and in other builds only for apps that set macos.testDriverInRelease (docs/test-driver.md#builds).
|
|
9
|
+
// At 0 the driver's sources are not compiled, these functions are inline no-ops, and the hooks below are undeclared,
|
|
10
|
+
// so code that registers driver commands must sit inside #if SPARK_TEST_DRIVER. A file compiled without the setting
|
|
11
|
+
// gets the driver declarations; in a build without the driver it then fails to link instead of shipping driver code.
|
|
12
|
+
#ifndef SPARK_TEST_DRIVER
|
|
13
|
+
#define SPARK_TEST_DRIVER 1
|
|
14
|
+
#endif
|
|
15
|
+
|
|
16
|
+
#if !SPARK_TEST_DRIVER
|
|
17
|
+
static inline BOOL SparkTestDriverStart(void)
|
|
18
|
+
{
|
|
19
|
+
if (getenv("SPARK_TEST_DRIVER_DIR")) NSLog(@"Spark: this build has no test driver; SPARK_TEST_DRIVER_DIR is ignored");
|
|
20
|
+
return NO;
|
|
21
|
+
}
|
|
22
|
+
static inline BOOL SparkTestDriverActive(void) { return NO; }
|
|
23
|
+
static inline void SparkTestDriverDidMount(void) {}
|
|
24
|
+
static inline BOOL SparkTestDriverEnding(void) { return NO; }
|
|
25
|
+
#else
|
|
26
|
+
|
|
27
|
+
// Call first in applicationWillFinishLaunching:. Returns YES in driver mode, where
|
|
28
|
+
// the app never activates and its windows are rendered without being shown.
|
|
29
|
+
FOUNDATION_EXPORT BOOL SparkTestDriverStart(void);
|
|
30
|
+
FOUNDATION_EXPORT BOOL SparkTestDriverActive(void);
|
|
31
|
+
// The host reports each mounted UI transaction; captures wait until mounts settle.
|
|
32
|
+
FOUNDATION_EXPORT void SparkTestDriverDidMount(void);
|
|
33
|
+
// YES once the runner's session has ended: that termination is teardown, not a user quit,
|
|
34
|
+
// so the host terminates without running the app's quit guards.
|
|
35
|
+
FOUNDATION_EXPORT BOOL SparkTestDriverEnding(void);
|
|
36
|
+
|
|
37
|
+
// Driver commands owned by optional modules, which the driver itself cannot link. Handlers run on the main thread
|
|
38
|
+
// and must call `reply` exactly once with {"ok": true, ...} or
|
|
39
|
+
// {"ok": false, "error": {"code", "message"}}. Built-in commands cannot be replaced.
|
|
40
|
+
typedef void (^SparkTestDriverReply)(NSDictionary *reply);
|
|
41
|
+
typedef void (^SparkTestDriverHandler)(NSDictionary *request, SparkTestDriverReply reply);
|
|
42
|
+
FOUNDATION_EXPORT void SparkTestDriverRegisterCommand(NSString *name, SparkTestDriverHandler handler);
|
|
43
|
+
|
|
44
|
+
#endif
|