UAParserSwift, a lightweight User-Agent parser for Swift Server Side
This is my first contribution to the Swift Server Side community; during these months I’m evaluating Swift as backend language for server side programming. In fact it was not my first attempt, but during this year I’ve seen many improvements in all main server projects like Kitura and Vapor.
Moving away from NodeJS is a tricky decision but the advantages of moving to a modern language like Swift are tempting me since the first release.
So while it’s still a long path to the goal I’m slowly moving to see how I can make this a valuable solution.
With this goal in mind I’ve tried to port a first small contribution to the community by porting of the most famous library used in NodeJS in Swift; UAParserSwift is a port of ua-parser-js made for Swift server side. This library aims to identify detailed type of web browser, layout engine, operating system, cpu architecture, and device type/model, entirely from user-agent string with a relatively small footprint.
Usage of UAParserSwift is pretty simple; just allocate an UAParser object along with the User-Agent string you want to parse:
let parser = UAParser(agent: "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7")
// Then call properties you want to read.
// Parsed data are cached and evaluated lazily in order to
// get the best performances.
let identified_browser = parser.browser
let identified_device = parser.device
let identified_engine = parser.engine
let identified_os = parser.os
let identified_cpu = parser.cpu
The object exposes several properties (os, device, engine, browser and cpu) which are evaluated lazily in order to improve the footprint of the library.
Each property also exposes some sub-properties which better aim to describe the object itself (for a browser you can get the engine, the name of the browser and the version).
Installation of the library can be accomplished via Swift Package Manager and its compatible with all the major Swift Server Side projects.
UAParserSwift is also listed on IBM’s Kitura Official Package Catalog.
Project page on GitHub: ➤ UAParserSwift
UAParserSwift is my first contribution to the growth of Swift in Server environment: it's a port of the popular ua-parser-js package for Node and it was also added to the IBM’s official package catalog for Swift.https://t.co/IqY0ffliHF#swiftserver #swiftlang #kitura #vapor pic.twitter.com/z59hRg0DTA
— daniele margutti (@danielemargutti) November 11, 2017