Last active
January 2, 2024 00:27
-
-
Save hfossli/c0818637d67beda27f9767a811dc76df to your computer and use it in GitHub Desktop.
Start a node.js server using NSTask in Swift.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let mainBundle = NSBundle.mainBundle() | |
let pathToNode = mainBundle.pathForResource("node", ofType: "")! as String | |
let pathToNodeApp = mainBundle.pathForResource("server", ofType: "js", inDirectory: "Server")! as String | |
let pathToAppFolder = (pathToNodeApp as NSString).stringByDeletingLastPathComponent | |
task = NodeTask(nodeJSPath: pathToNode, appPath: pathToNodeApp, currentDirectoryPath: pathToAppFolder) | |
task.launch() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import Cocoa | |
class NodeTask: NSObject { | |
private let processIdentifier = NSProcessInfo.processInfo().processIdentifier | |
private let nodeTask = NSTask() | |
private let readPipe = NSPipe() | |
private let errorPipe = NSPipe() | |
private let queue = dispatch_queue_create("NodeTask.output.queue", DISPATCH_QUEUE_SERIAL) | |
private var running = false | |
init(nodeJSPath: String, appPath: String, currentDirectoryPath: String) { | |
super.init() | |
readPipe.fileHandleForReading.readabilityHandler = { [unowned self] (handler: NSFileHandle!) in | |
dispatch_async(self.queue) { | |
if self.running { | |
let data = handler.readDataToEndOfFile() | |
self.onRead(data) | |
} | |
} | |
} | |
errorPipe.fileHandleForReading.readabilityHandler = { [unowned self] (handler: NSFileHandle!) in | |
dispatch_async(self.queue) { | |
if self.running { | |
let data = handler.readDataToEndOfFile() | |
self.onError(data) | |
} | |
} | |
} | |
nodeTask.currentDirectoryPath = currentDirectoryPath | |
nodeTask.launchPath = nodeJSPath | |
nodeTask.arguments = [appPath, "\(processIdentifier)"] | |
nodeTask.qualityOfService = .UserInitiated | |
nodeTask.standardOutput = readPipe | |
nodeTask.standardError = errorPipe | |
} | |
deinit { | |
self.quit() | |
} | |
func launch() { | |
if !running { | |
print("------------------------------ Node launch ------------------------------") | |
running = true | |
nodeTask.launch() | |
} | |
} | |
func quit() { | |
dispatch_sync(self.queue) { | |
self.terminate() | |
} | |
} | |
private func terminate() { | |
if running { | |
print("------------------------------ Node quit ------------------------------") | |
running = false | |
readPipe.fileHandleForReading.closeFile() | |
errorPipe.fileHandleForReading.closeFile() | |
nodeTask.terminate() | |
} | |
} | |
private func stringFromData(data: NSData) -> String { | |
return NSString(data: data, encoding: NSUTF8StringEncoding)!.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) as String | |
} | |
private func onRead(data: NSData) { | |
let text = stringFromData(data) | |
if text != "" { | |
print("Node: \(text)") | |
} | |
} | |
private func onError(data: NSData) { | |
let text = stringFromData(data) | |
if text != "" { | |
print("------------------------------ Node fatal error ------------------------------") | |
print(text) | |
self.terminate() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No