@capacitor/background-runner 1.0.0-rc.1

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 (61) hide show
  1. package/CapacitorBackgroundRunner.podspec +17 -0
  2. package/README.md +310 -0
  3. package/android/build.gradle +67 -0
  4. package/android/src/main/AndroidManifest.xml +12 -0
  5. package/android/src/main/java/io/ionic/backgroundrunner/plugin/BackgroundRunner.kt +166 -0
  6. package/android/src/main/java/io/ionic/backgroundrunner/plugin/BackgroundRunnerPlugin.kt +102 -0
  7. package/android/src/main/java/io/ionic/backgroundrunner/plugin/CoroutineHelper.kt +21 -0
  8. package/android/src/main/java/io/ionic/backgroundrunner/plugin/RunnerConfig.kt +33 -0
  9. package/android/src/main/java/io/ionic/backgroundrunner/plugin/RunnerWorker.kt +46 -0
  10. package/android/src/main/java/io/ionic/backgroundrunner/plugin/api/Device.kt +99 -0
  11. package/android/src/main/java/io/ionic/backgroundrunner/plugin/api/Geolocation.kt +55 -0
  12. package/android/src/main/java/io/ionic/backgroundrunner/plugin/api/KV.kt +29 -0
  13. package/android/src/main/java/io/ionic/backgroundrunner/plugin/api/NetworkStatus.kt +10 -0
  14. package/android/src/main/java/io/ionic/backgroundrunner/plugin/api/Notification.kt +59 -0
  15. package/android/src/main/java/io/ionic/backgroundrunner/plugin/api/Notifications.kt +158 -0
  16. package/android/src/main/java/io/ionic/backgroundrunner/plugin/api/TimedNotificationPublisher.kt +40 -0
  17. package/android/src/main/libs/android-js-engine-release.aar +0 -0
  18. package/android/src/main/res/.gitkeep +0 -0
  19. package/dist/docs.json +208 -0
  20. package/dist/esm/apis.d.ts +52 -0
  21. package/dist/esm/apis.js +2 -0
  22. package/dist/esm/apis.js.map +1 -0
  23. package/dist/esm/definitions.d.ts +82 -0
  24. package/dist/esm/definitions.js +3 -0
  25. package/dist/esm/definitions.js.map +1 -0
  26. package/dist/esm/index.d.ts +5 -0
  27. package/dist/esm/index.js +8 -0
  28. package/dist/esm/index.js.map +1 -0
  29. package/dist/esm/web.d.ts +8 -0
  30. package/dist/esm/web.js +16 -0
  31. package/dist/esm/web.js.map +1 -0
  32. package/dist/plugin.cjs.js +32 -0
  33. package/dist/plugin.cjs.js.map +1 -0
  34. package/dist/plugin.js +35 -0
  35. package/dist/plugin.js.map +1 -0
  36. package/ios/Plugin/BackgroundRunner.swift +223 -0
  37. package/ios/Plugin/BackgroundRunnerErrors.swift +8 -0
  38. package/ios/Plugin/BackgroundRunnerPlugin.h +10 -0
  39. package/ios/Plugin/BackgroundRunnerPlugin.m +11 -0
  40. package/ios/Plugin/BackgroundRunnerPlugin.swift +161 -0
  41. package/ios/Plugin/CapacitorAPI/Device.swift +116 -0
  42. package/ios/Plugin/CapacitorAPI/Geolocation.swift +190 -0
  43. package/ios/Plugin/CapacitorAPI/KV.swift +22 -0
  44. package/ios/Plugin/CapacitorAPI/Notifications.swift +165 -0
  45. package/ios/Plugin/CapacitorAPI/Reachability.swift +395 -0
  46. package/ios/Plugin/CapacitorAPI/Wearable.swift +48 -0
  47. package/ios/Plugin/Context+CapacitorAPI.swift +11 -0
  48. package/ios/Plugin/Info.plist +24 -0
  49. package/ios/Plugin/RunnerConfig.swift +36 -0
  50. package/ios/Plugin/RunnerEngine/Context.swift +151 -0
  51. package/ios/Plugin/RunnerEngine/Errors.swift +8 -0
  52. package/ios/Plugin/RunnerEngine/JSConsole.swift +38 -0
  53. package/ios/Plugin/RunnerEngine/JSCrypto.swift +38 -0
  54. package/ios/Plugin/RunnerEngine/JSError.swift +33 -0
  55. package/ios/Plugin/RunnerEngine/JSFetch.swift +50 -0
  56. package/ios/Plugin/RunnerEngine/JSResponse.swift +69 -0
  57. package/ios/Plugin/RunnerEngine/JSTextDecoder.swift +60 -0
  58. package/ios/Plugin/RunnerEngine/JSTextEncoder.swift +12 -0
  59. package/ios/Plugin/RunnerEngine/Runner.swift +23 -0
  60. package/ios/Plugin/WatchConnectivtySessionDelegate.swift +34 -0
  61. package/package.json +81 -0
@@ -0,0 +1,38 @@
1
+ import Foundation
2
+ import JavaScriptCore
3
+
4
+ @objc protocol JSConsoleExports: JSExport {
5
+ func log (_ msg: String)
6
+ func info(_ msg: String)
7
+ func warn(_ msg: String)
8
+ func error(_ msg: String)
9
+ func debug(_ msg: String)
10
+ }
11
+
12
+ class JSConsole: NSObject, JSConsoleExports {
13
+ let contextName: String
14
+
15
+ init(name: String) {
16
+ contextName = name
17
+ }
18
+
19
+ func log(_ msg: String) {
20
+ print("[\(contextName)] [LOG] \(msg)")
21
+ }
22
+
23
+ func info(_ msg: String) {
24
+ print("[\(contextName)] [INFO] \(msg)")
25
+ }
26
+
27
+ func warn(_ msg: String) {
28
+ print("[\(contextName)] [WARN] \(msg)")
29
+ }
30
+
31
+ func error(_ msg: String) {
32
+ print("[\(contextName)] [ERR] \(msg)")
33
+ }
34
+
35
+ func debug(_ msg: String) {
36
+ debugPrint("[\(contextName)] [DEBUG] \(msg)")
37
+ }
38
+ }
@@ -0,0 +1,38 @@
1
+ import Foundation
2
+ import JavaScriptCore
3
+
4
+ @objc protocol JSCryptoExports: JSExport {
5
+ static func getRandomValues(_ arr: JSValue) -> JSValue
6
+ static func randomUUID() -> String
7
+ }
8
+
9
+ class JSCrypto: NSObject, JSCryptoExports {
10
+ static func getRandomValues(_ arr: JSValue) -> JSValue {
11
+ let size = arr.toArray().count
12
+
13
+ var rnd = [UInt8](repeating: 0, count: size)
14
+ _ = SecRandomCopyBytes(kSecRandomDefault, size, &rnd)
15
+
16
+ if arr.toArray() as? [UInt8] != nil {
17
+ for i in 0...(size - 1) {
18
+ arr.setObject(rnd[i], atIndexedSubscript: i)
19
+ }
20
+ }
21
+ if arr.toArray() as? [UInt16] != nil {
22
+ for i in 0...(size - 1) {
23
+ arr.setObject(UInt16(rnd[i]), atIndexedSubscript: i)
24
+ }
25
+ }
26
+ if arr.toArray() as? [UInt32] != nil {
27
+ for i in 0...(size - 1) {
28
+ arr.setObject(UInt32(rnd[i]), forKeyedSubscript: i)
29
+ }
30
+ }
31
+
32
+ return arr
33
+ }
34
+
35
+ static func randomUUID() -> String {
36
+ return UUID().uuidString
37
+ }
38
+ }
@@ -0,0 +1,33 @@
1
+ import Foundation
2
+ import JavaScriptCore
3
+
4
+ // This is needed to do a problem with JavaScriptCore
5
+ // JSValue(newErrorFromMessage:in:) always returns a nil object
6
+ //
7
+ // https://github.com/mihaibabusca/jscore-behaviour-mismatch
8
+
9
+ @objc protocol JSErrorExports: JSExport {
10
+ var cause: Any? { get set }
11
+ var message: String { get set }
12
+
13
+ func toString() -> String
14
+ }
15
+
16
+ @objc public class JSError: NSObject, JSErrorExports {
17
+ dynamic var cause: Any?
18
+ dynamic var message: String
19
+
20
+ override public init() {
21
+ message = ""
22
+ cause = nil
23
+ }
24
+
25
+ public init(message: String) {
26
+ self.message = message
27
+ self.cause = nil
28
+ }
29
+
30
+ func toString() -> String {
31
+ return message
32
+ }
33
+ }
@@ -0,0 +1,50 @@
1
+ import JavaScriptCore
2
+
3
+ func fetch(resource: JSValue, options: JSValue) -> JSValue {
4
+ let session = URLSession.shared
5
+
6
+ var url: URL?
7
+
8
+ if resource.isString {
9
+ url = URL(string: resource.toString())
10
+ }
11
+
12
+ guard let url = url else {
13
+ return JSValue.init(newErrorFromMessage: "invalid url", in: JSContext.current())
14
+ }
15
+
16
+ var request = URLRequest(url: url)
17
+
18
+ if options.isObject, let optionsDict = options.toDictionary() as? [String: Any] {
19
+ request.httpMethod = (optionsDict["method"] as? String)?.uppercased()
20
+ request.allHTTPHeaderFields = optionsDict["headers"] as? [String: String]
21
+
22
+ if let bodyString = optionsDict["body"] as? String {
23
+ request.httpBody = bodyString.data(using: .utf8)
24
+ }
25
+ }
26
+
27
+ return JSValue(newPromiseIn: JSContext.current()) { resolve, reject in
28
+ let task = session.dataTask(with: request) { data, response, err in
29
+ if let err = err {
30
+ print("fetch failed: \(err.localizedDescription)")
31
+ let jsErr = JSError(message: err.localizedDescription)
32
+ reject?.call(withArguments: [jsErr as Any])
33
+ return
34
+ }
35
+
36
+ guard let httpResponse = response as? HTTPURLResponse else {
37
+ print("HTTPURLResponse was nil")
38
+ let jsErr = JSError(message: "native response was nil")
39
+ reject?.call(withArguments: [jsErr as Any])
40
+ return
41
+ }
42
+
43
+ let res = JSResponse(from: httpResponse, responseData: data)
44
+ resolve?.call(withArguments: [res])
45
+ return
46
+ }
47
+
48
+ task.resume()
49
+ }
50
+ }
@@ -0,0 +1,69 @@
1
+ import JavaScriptCore
2
+
3
+ @objc protocol JSResponseExports: JSExport {
4
+ // swiftlint:disable:next identifier_name
5
+ var ok: Bool { get }
6
+ var status: Int { get }
7
+ var url: String { get }
8
+
9
+ func text() -> JSValue
10
+ func json() -> JSValue
11
+ }
12
+
13
+ @objc public class JSResponse: NSObject, JSResponseExports {
14
+ dynamic var url: String
15
+ dynamic var status: Int
16
+ // swiftlint:disable:next identifier_name
17
+ dynamic var ok: Bool {
18
+ return status > 200 && status <= 299
19
+ }
20
+
21
+ private let data: Data?
22
+
23
+ public init(status: Int, responseData: Data?) {
24
+ self.status = status
25
+ self.data = responseData
26
+ self.url = ""
27
+ }
28
+
29
+ public init(from httpResponse: HTTPURLResponse, responseData: Data?) {
30
+ self.data = responseData
31
+ self.status = httpResponse.statusCode
32
+ self.url = httpResponse.url?.absoluteString ?? ""
33
+ }
34
+
35
+ func text() -> JSValue {
36
+ return JSValue(newPromiseIn: JSContext.current()) { resolve, _ in
37
+ guard let data = self.data else {
38
+ resolve?.call(withArguments: [""])
39
+ return
40
+ }
41
+
42
+ if let str = String(bytes: data, encoding: .utf8) {
43
+ resolve?.call(withArguments: [str])
44
+ return
45
+ }
46
+
47
+ resolve?.call(withArguments: [""])
48
+ }
49
+ }
50
+
51
+ func json() -> JSValue {
52
+ return JSValue(newPromiseIn: JSContext.current()) { resolve, reject in
53
+ do {
54
+ guard let data = self.data else {
55
+ resolve?.call(withArguments: [JSValue(nullIn: JSContext.current()) as Any])
56
+ return
57
+ }
58
+
59
+ let anyObj = try JSONSerialization.jsonObject(with: data)
60
+ let jsonObj = JSValue(object: anyObj, in: JSContext.current())
61
+ resolve?.call(withArguments: [jsonObj as Any])
62
+
63
+ } catch {
64
+ let err = JSError(message: error.localizedDescription)
65
+ reject?.call(withArguments: [err as Any])
66
+ }
67
+ }
68
+ }
69
+ }
@@ -0,0 +1,60 @@
1
+ import JavaScriptCore
2
+
3
+ @objc protocol JSTextDecoderExports: JSExport {
4
+ var encoding: String { get }
5
+ func decode(_ buffer: JSValue) -> String?
6
+ }
7
+
8
+ class JSTextDecoder: NSObject, JSTextDecoderExports {
9
+ dynamic var encoding: String = "utf-8"
10
+
11
+ required init(encoding: String?, options: [AnyHashable: Any]?) {
12
+ if let enc = encoding {
13
+ self.encoding = enc
14
+ }
15
+ }
16
+
17
+ func decode(_ buffer: JSValue) -> String? {
18
+ let useEncoding = setEncoding(enc: encoding)
19
+
20
+ if let arr = buffer.toArray() as? [UInt8] {
21
+ return String(bytes: arr, encoding: useEncoding)
22
+ }
23
+
24
+ if let arr = buffer.toArray() as? [Int8] {
25
+ let data = Data(bytes: arr, count: arr.count)
26
+ return String(data: data, encoding: useEncoding)
27
+ }
28
+
29
+ return nil
30
+ }
31
+
32
+ func setEncoding(enc: String) -> String.Encoding {
33
+ switch enc {
34
+ case "windows-1250":
35
+ return .windowsCP1250
36
+ case "windows-1251":
37
+ return .windowsCP1251
38
+ case "windows-1252":
39
+ return .windowsCP1252
40
+ case "windows-1253":
41
+ return .windowsCP1253
42
+ case "windows-1254":
43
+ return .windowsCP1254
44
+ case "iso-2022-jp":
45
+ return .iso2022JP
46
+ case "euc-jp":
47
+ return .japaneseEUC
48
+ case "macintosh":
49
+ return .macOSRoman
50
+ case "iso-8859-2":
51
+ return .isoLatin2
52
+ case "utf-16be":
53
+ return .utf16BigEndian
54
+ case "utf-16le":
55
+ return .utf16LittleEndian
56
+ default:
57
+ return .utf8
58
+ }
59
+ }
60
+ }
@@ -0,0 +1,12 @@
1
+ import JavaScriptCore
2
+
3
+ @objc protocol JSTextEncoderExports: JSExport {
4
+ func encode(_ str: String) -> [UInt8]
5
+ // func encodeInto(_ str: String, _ arr: JSValue) -> [AnyHashable: Any]
6
+ }
7
+
8
+ class JSTextEncoder: NSObject, JSTextEncoderExports {
9
+ func encode(_ str: String) -> [UInt8] {
10
+ return Array(str.utf8)
11
+ }
12
+ }
@@ -0,0 +1,23 @@
1
+ import Foundation
2
+ import JavaScriptCore
3
+
4
+ public class Runner {
5
+ var machine: JSVirtualMachine
6
+ var contexts: [String: Context]
7
+ public init() {
8
+ machine = JSVirtualMachine()
9
+ contexts = [:]
10
+ }
11
+
12
+ func createContext(name: String) throws -> Context {
13
+ let ctx = try Context(vm: machine, ctxName: name)
14
+
15
+ contexts[name] = ctx
16
+
17
+ return ctx
18
+ }
19
+
20
+ func destroyContext(name: String) {
21
+ contexts.removeValue(forKey: name)
22
+ }
23
+ }
@@ -0,0 +1,34 @@
1
+ import Foundation
2
+ import WatchConnectivity
3
+
4
+ extension BackgroundRunnerPlugin: WCSessionDelegate {
5
+ public func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
6
+ var args: [String: Any] = [:]
7
+ args["activationState"] = activationState.rawValue
8
+ args["error"] = error
9
+
10
+ try? BackgroundRunner.shared.dispatchEvent(event: "WatchConnectivity_activationDidCompleteWith", inputArgs: args)
11
+ }
12
+
13
+ public func sessionDidBecomeInactive(_ session: WCSession) {
14
+ try? BackgroundRunner.shared.dispatchEvent(event: "WatchConnectivity_sessionDidBecomeInactive", inputArgs: nil)
15
+ }
16
+
17
+ public func sessionDidDeactivate(_ session: WCSession) {
18
+ try? BackgroundRunner.shared.dispatchEvent(event: "WatchConnectivity_sessionDidDeactivate", inputArgs: nil)
19
+ }
20
+
21
+ public func session(_ session: WCSession, didReceiveUserInfo userInfo: [String: Any] = [:]) {
22
+ var args: [String: Any] = [:]
23
+ args["userInfo"] = userInfo
24
+
25
+ try? BackgroundRunner.shared.dispatchEvent(event: "WatchConnectivity_didReceiveUserInfo", inputArgs: args)
26
+ }
27
+
28
+ public func session(_ session: WCSession, didReceiveMessage message: [String: Any]) {
29
+ var args: [String: Any] = [:]
30
+ args["message"] = message
31
+
32
+ try? BackgroundRunner.shared.dispatchEvent(event: "WatchConnectivity_didReceiveMessage", inputArgs: args)
33
+ }
34
+ }
package/package.json ADDED
@@ -0,0 +1,81 @@
1
+ {
2
+ "name": "@capacitor/background-runner",
3
+ "version": "1.0.0-rc.1",
4
+ "description": "Capacitor Background Runner",
5
+ "main": "dist/plugin.cjs.js",
6
+ "module": "dist/esm/index.js",
7
+ "types": "dist/esm/index.d.ts",
8
+ "unpkg": "dist/plugin.js",
9
+ "files": [
10
+ "android/src/main/",
11
+ "android/build.gradle",
12
+ "dist/",
13
+ "ios/Plugin/",
14
+ "CapacitorBackgroundRunner.podspec"
15
+ ],
16
+ "author": "Ionic Team",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "https://github.com/ionic-team/capacitor-background-runner.git"
20
+ },
21
+ "bugs": {
22
+ "url": "https://ionic.io/issues"
23
+ },
24
+ "keywords": [
25
+ "capacitor",
26
+ "plugin",
27
+ "native"
28
+ ],
29
+ "scripts": {
30
+ "verify": "npm run verify:ios && npm run verify:android && npm run verify:web",
31
+ "verify:ios": "cd ios && pod install && xcodebuild -workspace Plugin.xcworkspace -scheme Plugin -destination generic/platform=iOS && cd ..",
32
+ "verify:android": "cd android && ./gradlew clean build test && cd ..",
33
+ "verify:web": "npm run build",
34
+ "lint": "npm run eslint && npm run prettier -- --check && npm run swiftlint -- lint",
35
+ "fmt": "npm run eslint -- --fix && npm run prettier -- --write && npm run swiftlint -- --fix --format",
36
+ "eslint": "eslint . --ext ts",
37
+ "prettier": "prettier \"**/*.{css,html,ts,js,java}\"",
38
+ "swiftlint": "node-swiftlint",
39
+ "docgen": "docgen --api BackgroundRunnerPlugin --output-readme README.md --output-json dist/docs.json",
40
+ "build-sdk": "./scripts/build-native-sdks.sh",
41
+ "build": "npm run clean && npm run docgen && tsc && rollup -c rollup.config.js && npm run build-sdk && ./scripts/copy-native-sdks.sh && npm pack && mv ./capacitor-background-runner-*.tgz ../../",
42
+ "pack": "npm run build && && ./scripts/copy-native-sdks.sh && npm pack && mv ./capacitor-background-runner-*.tgz ../../",
43
+ "clean": "rimraf ./dist",
44
+ "watch": "tsc --watch",
45
+ "prepublishOnly": "npm run build",
46
+ "postinstall": "mkdir -p $INIT_CWD/android/app/libs/ && cp -f android/src/main/libs/android-js-engine-release.aar $INIT_CWD/android/app/libs/"
47
+ },
48
+ "devDependencies": {
49
+ "@capacitor/android": "^5.2.1",
50
+ "@capacitor/cli": "^5.2.1",
51
+ "@capacitor/core": "^5.2.1",
52
+ "@capacitor/docgen": "^0.0.18",
53
+ "@capacitor/ios": "^5.2.1",
54
+ "@ionic/eslint-config": "^0.3.0",
55
+ "@ionic/prettier-config": "^1.0.1",
56
+ "@ionic/swiftlint-config": "^1.1.2",
57
+ "eslint": "^7.11.0",
58
+ "prettier": "~2.3.0",
59
+ "prettier-plugin-java": "~2.1.0",
60
+ "rimraf": "^3.0.2",
61
+ "rollup": "^2.32.0",
62
+ "swiftlint": "^1.0.2",
63
+ "typescript": "^5.1.6"
64
+ },
65
+ "peerDependencies": {
66
+ "@capacitor/core": "^5.0.0"
67
+ },
68
+ "prettier": "@ionic/prettier-config",
69
+ "swiftlint": "@ionic/swiftlint-config",
70
+ "eslintConfig": {
71
+ "extends": "@ionic/eslint-config/recommended"
72
+ },
73
+ "capacitor": {
74
+ "ios": {
75
+ "src": "ios"
76
+ },
77
+ "android": {
78
+ "src": "android"
79
+ }
80
+ }
81
+ }