nitrogen 0.2.24 → 0.29.5

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 (241) hide show
  1. package/README.md +18 -108
  2. package/lib/Logger.js +56 -0
  3. package/lib/autolinking/Autolinking.js +1 -0
  4. package/lib/autolinking/android/createCMakeExtension.js +109 -0
  5. package/lib/autolinking/android/createGradleExtension.js +36 -0
  6. package/lib/autolinking/android/createHybridObjectInitializer.js +159 -0
  7. package/lib/autolinking/createAndroidAutolinking.js +13 -0
  8. package/lib/autolinking/createIOSAutolinking.js +19 -0
  9. package/lib/autolinking/ios/createHybridObjectInitializer.js +97 -0
  10. package/lib/autolinking/ios/createPodspecRubyExtension.js +69 -0
  11. package/lib/autolinking/ios/createSwiftCxxBridge.js +117 -0
  12. package/lib/autolinking/ios/createSwiftUmbrellaHeader.js +74 -0
  13. package/lib/config/NitroConfig.js +112 -0
  14. package/lib/config/NitroUserConfig.js +88 -0
  15. package/lib/config/getConfig.js +84 -0
  16. package/lib/createGitAttributes.js +11 -0
  17. package/lib/createPlatformSpec.js +127 -0
  18. package/lib/getFiles.js +28 -0
  19. package/lib/getPlatformSpecs.js +153 -0
  20. package/lib/index.js +113 -10
  21. package/lib/init.js +123 -0
  22. package/lib/nitrogen.js +165 -0
  23. package/lib/prettifyDirectory.js +27 -0
  24. package/lib/syntax/BridgedType.js +1 -0
  25. package/lib/syntax/CodeNode.js +1 -0
  26. package/lib/syntax/HybridObjectSpec.js +1 -0
  27. package/lib/syntax/Method.js +108 -0
  28. package/lib/syntax/Parameter.js +65 -0
  29. package/lib/syntax/Property.js +147 -0
  30. package/lib/syntax/SourceFile.js +7 -0
  31. package/lib/syntax/c++/CppEnum.js +110 -0
  32. package/lib/syntax/c++/CppHybridObject.js +146 -0
  33. package/lib/syntax/c++/CppHybridObjectRegistration.js +18 -0
  34. package/lib/syntax/c++/CppStruct.js +108 -0
  35. package/lib/syntax/c++/CppUnion.js +88 -0
  36. package/lib/syntax/c++/getForwardDeclaration.js +14 -0
  37. package/lib/syntax/c++/includeNitroHeader.js +34 -0
  38. package/lib/syntax/createType.js +303 -0
  39. package/lib/syntax/getAllTypes.js +11 -0
  40. package/lib/syntax/getCustomTypeConfig.js +53 -0
  41. package/lib/syntax/getHybridObjectName.d.ts +36 -0
  42. package/lib/syntax/getHybridObjectName.js +10 -0
  43. package/lib/syntax/getInterfaceProperties.js +9 -0
  44. package/lib/syntax/getReferencedTypes.js +47 -0
  45. package/lib/syntax/helpers.js +53 -0
  46. package/lib/syntax/isCoreType.js +47 -0
  47. package/lib/syntax/kotlin/FbjniHybridObject.js +261 -0
  48. package/lib/syntax/kotlin/JNINativeRegistrations.js +7 -0
  49. package/lib/syntax/kotlin/KotlinBoxedPrimitive.js +17 -0
  50. package/lib/syntax/kotlin/KotlinCxxBridgedType.js +893 -0
  51. package/lib/syntax/kotlin/KotlinEnum.js +113 -0
  52. package/lib/syntax/kotlin/KotlinFunction.js +256 -0
  53. package/lib/syntax/kotlin/KotlinHybridObject.js +177 -0
  54. package/lib/syntax/kotlin/KotlinHybridObjectRegistration.js +26 -0
  55. package/lib/syntax/kotlin/KotlinStruct.js +172 -0
  56. package/lib/syntax/kotlin/KotlinVariant.js +191 -0
  57. package/lib/syntax/swift/SwiftCxxBridgedType.js +819 -0
  58. package/lib/syntax/swift/SwiftCxxTypeHelper.js +613 -0
  59. package/lib/syntax/swift/SwiftEnum.js +52 -0
  60. package/lib/syntax/swift/SwiftFunction.js +83 -0
  61. package/lib/syntax/swift/SwiftHybridObject.js +103 -0
  62. package/lib/syntax/swift/SwiftHybridObjectBridge.js +451 -0
  63. package/lib/syntax/swift/SwiftHybridObjectRegistration.js +42 -0
  64. package/lib/syntax/swift/SwiftStruct.js +75 -0
  65. package/lib/syntax/swift/SwiftVariant.js +58 -0
  66. package/lib/syntax/types/ArrayBufferType.js +37 -0
  67. package/lib/syntax/types/ArrayType.d.ts +12 -0
  68. package/lib/syntax/types/ArrayType.js +52 -0
  69. package/lib/syntax/types/BigIntType.js +27 -0
  70. package/lib/syntax/types/BooleanType.js +27 -0
  71. package/lib/syntax/types/CustomType.d.ts +14 -0
  72. package/lib/syntax/types/CustomType.js +36 -0
  73. package/lib/syntax/types/DateType.js +35 -0
  74. package/lib/syntax/types/EnumType.js +101 -0
  75. package/lib/syntax/types/ErrorType.js +37 -0
  76. package/lib/syntax/types/FunctionType.js +147 -0
  77. package/lib/syntax/types/HybridObjectBaseType.js +38 -0
  78. package/lib/syntax/types/HybridObjectType.js +131 -0
  79. package/lib/syntax/types/MapType.js +37 -0
  80. package/lib/syntax/types/NamedWrappingType.js +27 -0
  81. package/lib/syntax/types/NullType.js +23 -0
  82. package/lib/syntax/types/NumberType.js +27 -0
  83. package/lib/syntax/types/OptionalType.js +59 -0
  84. package/lib/syntax/types/PromiseType.js +62 -0
  85. package/lib/syntax/types/RecordType.js +47 -0
  86. package/lib/syntax/types/ResultWrappingType.js +44 -0
  87. package/lib/syntax/types/StringType.js +35 -0
  88. package/lib/syntax/types/StructType.js +61 -0
  89. package/lib/syntax/types/TupleType.js +39 -0
  90. package/lib/syntax/types/Type.js +1 -0
  91. package/lib/syntax/types/VariantType.js +75 -0
  92. package/lib/syntax/types/VoidType.js +27 -0
  93. package/lib/syntax/types/getTypeAs.js +12 -0
  94. package/lib/utils.js +126 -0
  95. package/lib/views/CppHybridViewComponent.js +256 -0
  96. package/lib/views/createHostComponentJs.js +27 -0
  97. package/lib/views/kotlin/KotlinHybridViewManager.js +229 -0
  98. package/lib/views/swift/SwiftHybridViewManager.js +131 -0
  99. package/lib/writeFile.js +19 -0
  100. package/package.json +58 -29
  101. package/src/Logger.ts +63 -0
  102. package/src/autolinking/Autolinking.ts +9 -0
  103. package/src/autolinking/android/createCMakeExtension.ts +126 -0
  104. package/src/autolinking/android/createGradleExtension.ts +43 -0
  105. package/src/autolinking/android/createHybridObjectInitializer.ts +174 -0
  106. package/src/autolinking/createAndroidAutolinking.ts +28 -0
  107. package/src/autolinking/createIOSAutolinking.ts +24 -0
  108. package/src/autolinking/ios/createHybridObjectInitializer.ts +112 -0
  109. package/src/autolinking/ios/createPodspecRubyExtension.ts +76 -0
  110. package/src/autolinking/ios/createSwiftCxxBridge.ts +137 -0
  111. package/src/autolinking/ios/createSwiftUmbrellaHeader.ts +90 -0
  112. package/src/config/NitroConfig.ts +139 -0
  113. package/src/config/NitroUserConfig.ts +105 -0
  114. package/src/config/getConfig.ts +91 -0
  115. package/src/createGitAttributes.ts +15 -0
  116. package/src/createPlatformSpec.ts +176 -0
  117. package/src/getFiles.ts +31 -0
  118. package/src/getPlatformSpecs.ts +202 -0
  119. package/src/index.ts +146 -0
  120. package/src/init.ts +186 -0
  121. package/src/nitrogen.ts +246 -0
  122. package/src/prettifyDirectory.ts +32 -0
  123. package/src/syntax/BridgedType.ts +59 -0
  124. package/src/syntax/CodeNode.ts +24 -0
  125. package/src/syntax/HybridObjectSpec.ts +14 -0
  126. package/src/syntax/Method.ts +154 -0
  127. package/src/syntax/Parameter.ts +81 -0
  128. package/src/syntax/Property.ts +203 -0
  129. package/src/syntax/SourceFile.ts +80 -0
  130. package/src/syntax/c++/CppEnum.ts +128 -0
  131. package/src/syntax/c++/CppHybridObject.ts +165 -0
  132. package/src/syntax/c++/CppHybridObjectRegistration.ts +39 -0
  133. package/src/syntax/c++/CppStruct.ts +129 -0
  134. package/src/syntax/c++/CppUnion.ts +105 -0
  135. package/src/syntax/c++/getForwardDeclaration.ts +19 -0
  136. package/src/syntax/c++/includeNitroHeader.ts +40 -0
  137. package/src/syntax/createType.ts +365 -0
  138. package/src/syntax/getAllTypes.ts +18 -0
  139. package/src/syntax/getCustomTypeConfig.ts +71 -0
  140. package/src/syntax/getHybridObjectName.ts +48 -0
  141. package/src/syntax/getInterfaceProperties.ts +21 -0
  142. package/src/syntax/getReferencedTypes.ts +57 -0
  143. package/src/syntax/helpers.ts +79 -0
  144. package/src/syntax/isCoreType.ts +60 -0
  145. package/src/syntax/kotlin/FbjniHybridObject.ts +313 -0
  146. package/src/syntax/kotlin/JNINativeRegistrations.ts +19 -0
  147. package/src/syntax/kotlin/KotlinBoxedPrimitive.ts +19 -0
  148. package/src/syntax/kotlin/KotlinCxxBridgedType.ts +942 -0
  149. package/src/syntax/kotlin/KotlinEnum.ts +130 -0
  150. package/src/syntax/kotlin/KotlinFunction.ts +277 -0
  151. package/src/syntax/kotlin/KotlinHybridObject.ts +205 -0
  152. package/src/syntax/kotlin/KotlinHybridObjectRegistration.ts +51 -0
  153. package/src/syntax/kotlin/KotlinStruct.ts +198 -0
  154. package/src/syntax/kotlin/KotlinVariant.ts +212 -0
  155. package/src/syntax/swift/SwiftCxxBridgedType.ts +874 -0
  156. package/src/syntax/swift/SwiftCxxTypeHelper.ts +674 -0
  157. package/src/syntax/swift/SwiftEnum.ts +65 -0
  158. package/src/syntax/swift/SwiftFunction.ts +91 -0
  159. package/src/syntax/swift/SwiftHybridObject.ts +121 -0
  160. package/src/syntax/swift/SwiftHybridObjectBridge.ts +522 -0
  161. package/src/syntax/swift/SwiftHybridObjectRegistration.ts +75 -0
  162. package/src/syntax/swift/SwiftStruct.ts +85 -0
  163. package/src/syntax/swift/SwiftVariant.ts +67 -0
  164. package/src/syntax/types/ArrayBufferType.ts +49 -0
  165. package/src/syntax/types/ArrayType.ts +62 -0
  166. package/src/syntax/types/BigIntType.ts +35 -0
  167. package/src/syntax/types/BooleanType.ts +35 -0
  168. package/src/syntax/types/CustomType.ts +47 -0
  169. package/src/syntax/types/DateType.ts +43 -0
  170. package/src/syntax/types/EnumType.ts +130 -0
  171. package/src/syntax/types/ErrorType.ts +44 -0
  172. package/src/syntax/types/FunctionType.ts +167 -0
  173. package/src/syntax/types/HybridObjectBaseType.ts +54 -0
  174. package/src/syntax/types/HybridObjectType.ts +198 -0
  175. package/src/syntax/types/MapType.ts +49 -0
  176. package/src/syntax/types/NamedWrappingType.ts +33 -0
  177. package/src/syntax/types/NullType.ts +30 -0
  178. package/src/syntax/types/NumberType.ts +34 -0
  179. package/src/syntax/types/OptionalType.ts +66 -0
  180. package/src/syntax/types/PromiseType.ts +72 -0
  181. package/src/syntax/types/RecordType.ts +56 -0
  182. package/src/syntax/types/ResultWrappingType.ts +53 -0
  183. package/src/syntax/types/StringType.ts +44 -0
  184. package/src/syntax/types/StructType.ts +83 -0
  185. package/src/syntax/types/TupleType.ts +53 -0
  186. package/src/syntax/types/Type.ts +82 -0
  187. package/src/syntax/types/VariantType.ts +92 -0
  188. package/src/syntax/types/VoidType.ts +34 -0
  189. package/src/syntax/types/getTypeAs.ts +15 -0
  190. package/src/utils.ts +162 -0
  191. package/src/views/CppHybridViewComponent.ts +304 -0
  192. package/src/views/createHostComponentJs.ts +34 -0
  193. package/src/views/kotlin/KotlinHybridViewManager.ts +258 -0
  194. package/src/views/swift/SwiftHybridViewManager.ts +153 -0
  195. package/src/writeFile.ts +27 -0
  196. package/.jshintignore +0 -6
  197. package/.jshintrc +0 -3
  198. package/.npmignore +0 -3
  199. package/.travis.yml +0 -13
  200. package/LICENSE +0 -13
  201. package/browser/nitrogen-min.js +0 -3
  202. package/browser/nitrogen.js +0 -6369
  203. package/lib/apiKey.js +0 -67
  204. package/lib/blob.js +0 -57
  205. package/lib/commandManager.js +0 -350
  206. package/lib/device.js +0 -19
  207. package/lib/memoryStore.js +0 -24
  208. package/lib/message.js +0 -298
  209. package/lib/permission.js +0 -121
  210. package/lib/principal.js +0 -330
  211. package/lib/service.js +0 -347
  212. package/lib/session.js +0 -494
  213. package/lib/user.js +0 -20
  214. package/publish +0 -2
  215. package/scripts/build-documentation +0 -4
  216. package/scripts/build-module +0 -27
  217. package/scripts/module.js +0 -12
  218. package/scripts/postamble.js +0 -1
  219. package/scripts/preamble.js +0 -2
  220. package/scripts/run-test-server +0 -9
  221. package/test/config.js +0 -12
  222. package/test/fixtures/images/image.jpg +0 -0
  223. package/test/fixtures/images/motion0.jpg +0 -0
  224. package/test/fixtures/images/motion1.jpg +0 -0
  225. package/test/fixtures/images/motion2.jpg +0 -0
  226. package/test/fixtures/index.js +0 -76
  227. package/test/main.js +0 -5
  228. package/test/memoryStore.js +0 -22
  229. package/test/mocha.opts +0 -3
  230. package/test/units/apiKey.js +0 -46
  231. package/test/units/blob.js +0 -35
  232. package/test/units/commandManager.js +0 -67
  233. package/test/units/device.js +0 -26
  234. package/test/units/heartbeat.js +0 -28
  235. package/test/units/message.js +0 -79
  236. package/test/units/permissions.js +0 -43
  237. package/test/units/principal.js +0 -116
  238. package/test/units/service.js +0 -92
  239. package/test/units/session.js +0 -97
  240. package/test/units/user.js +0 -48
  241. package/yuidoc.json +0 -8
package/README.md CHANGED
@@ -1,118 +1,28 @@
1
- # Nitrogen Client
1
+ <a href="https://margelo.com">
2
+ <picture>
3
+ <source media="(prefers-color-scheme: dark)" srcset="../../docs/static/img/banner-nitrogen-dark.png" />
4
+ <source media="(prefers-color-scheme: light)" srcset="../../docs/static/img/banner-nitrogen-light.png" />
5
+ <img alt="Nitrogen" src="../../docs/static/img/banner-nitrogen-light.png" />
6
+ </picture>
7
+ </a>
2
8
 
3
- Nitrogen is a platform for building connected devices. Nitrogen provides the authentication, authorization, and real time message passing framework so that you can focus on your device and application. All with a consistent development platform that leverages the ubiquity of Javascript.
9
+ <br />
4
10
 
5
- This is the client library for developing applications and devices that communicate to the Nitrogen service.
11
+ **Nitrogen** is a code-generator that takes TypeScript interfaces and generates C++, Swift and Kotlin code and native bindings built on top of the [**react-native-nitro-modules**](../react-native-nitro-modules/) core APIs.
6
12
 
7
- ## Device Development Model
13
+ ## Installation
8
14
 
9
- Nitrogen at its heart uses messaging between principals (devices and users). Principals in the system can create and consume messages. Messages can follow a well known schema to enable interoperability between applications or use their own private custom message types.
10
-
11
- For example, a thermometer that measures temperature once every 15 minutes could be implemented in Nitrogen like this:
12
-
13
- ``` javascript
14
- var thermometer = new nitrogen.Device({
15
- tags: ['sends:temperature'],
16
- nickname: 'thermometer'
17
- });
18
-
19
- var service = new nitrogen.Service(config);
20
- service.connect(thermometer, function(err, session, thermometer) {
21
-
22
- // take temperature every 15 minutes.
23
-
24
- setInterval(function() {
25
- var message = new Nitrogen.Message({
26
- type: 'temperature',
27
- body: {
28
- temperature: getTemp()
29
- }
30
- });
31
-
32
- message.save(session);
33
- }, 15 * 60 * 1000);
34
-
35
- });
15
+ Install [nitrogen](https://npmjs.org/nitrogen) as a `devDependency` in your Nitro Module:
16
+ ```sh
17
+ npm i nitrogen -D
36
18
  ```
37
19
 
38
- You can find a complete example for a device application of Nitrogen in the [camera](https://github.com/nitrogenjs/camera) project.
20
+ Then, generate your specs;
39
21
 
40
- ## Listening to a device's message stream
41
-
42
- An application that displays these temperatures in real time as they are received would look like this. In this case,
43
- we're using a user principal, and a filter with onMessage to only notify us of temperature updates.
44
-
45
- ``` javascript
46
- var user = new nitrogen.User({...});
47
-
48
- var service = new nitrogen.Service(config);
49
- service.connect(user, function(err, session, user) {
50
- session.onMessage({ type: 'temperature' }, function(message) {
51
- console.log("The temperature is now: " + message.body.temperature);
52
-
53
- // update the UI
54
- });
55
- });
22
+ ```sh
23
+ npx nitrogen
56
24
  ```
57
25
 
58
- ## Getting Started
59
-
60
- To get started with a Nitrogen client:
61
-
62
- ### Node.js application
63
-
64
- 1. `npm install nitrogen`
65
-
66
- ### Browser application
67
-
68
- 1. Add `<script src="https://api.nitrogen.io/client/nitrogen-min.js" />` to your application.
69
-
70
- ### Documentation
71
-
72
- Documentation for the Nitrogen client library can be found online at the [Nitrogen project](http://nitrogen.io).
73
-
74
- ### Contributing to the project.
75
-
76
- 1. Clone or fork this repo: `https://github.com/nitrogenjs/client`
77
- 2. If you are building on Windows, make sure to fetch all of the node-gyp dependencies as explained here: https://github.com/TooTallNate/node-gyp#installation
78
- 2. Fetch and install its node.js dependencies: `npm install`
79
- 3. Run a [Nitrogen server](https://github.com/nitrogenjs/service) locally that the tests can run against in test mode (NODE_ENV=test).
80
- 4. Run the tests to make sure everything is setup correctly: `npm test`
81
- 5. Make your change as a clean commit for a pull request.
82
- 6. Make sure there is a test to cover new functionality so nobody can break it in the future without us knowing.
83
- 7. Submit it as a pull request to the project.
84
-
85
- ## How to contribute
86
-
87
- 1. Feedback: We'd love feedback on what problems you are using Nitrogen to solve. Obviously, we'd also like to hear about where you ran into sharp edges and dead ends. Drop me a message at timfpark@gmail.com or file an issue with us above.
88
- 2. Pull requests: If you'd like to tackle an issue, fork the repo, create a clean commit for the fix or enhancement with tests if necessary, and send us a pull request. This is also the path to becoming a core committer for the project for folks that are interested in contributing in more depth.
89
- 3. Documentation: Better technical documentation is key to broadening the use of the platform. We'd love to have more help and this is one of the most valuable contributions you can make.
90
-
91
- ## Running on Windows
92
-
93
- On Windows, you'll need to install some dependencies first:
94
- - [node-gyp](https://github.com/TooTallNate/node-gyp/) (`npm install -g node-gyp`)
95
- - [Python 2.7](http://www.python.org/download/releases/2.7.3#download) (not 3.3)
96
- - Visual Studio 2010 or higher (including Express editions)
97
- - Windows XP/Vista/7:
98
- - Microsoft Visual Studio C++ 2010 ([Express](http://go.microsoft.com/?linkid=9709949) version works well)
99
- - Also install [Microsoft Visual Studio 2010 Service Pack 1](http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=23691)
100
- - For 64-bit builds of node and native modules you will _**also**_ need the [Windows 7 64-bit SDK](http://www.microsoft.com/en-us/download/details.aspx?id=8279)
101
- - If you get errors that the 64-bit compilers are not installed you may also need the [compiler update for the Windows SDK 7.1](http://www.microsoft.com/en-us/download/details.aspx?id=4422)
102
- - Windows 8:
103
- - Microsoft Visual Studio C++ 2012 for Windows Desktop ([Express](http://go.microsoft.com/?linkid=9816758) version works well)
104
- - [OpenSSL](http://slproweb.com/products/Win32OpenSSL.html) (normal, not light)
105
- in the same bitness as your Node.js installation.
106
- - The build script looks for OpenSSL in the default install directory (`C:\OpenSSL-Win32` or `C:\OpenSSL-Win64`)
107
- - If you get `Error: The specified module could not be found.`, copy `libeay32.dll` from the OpenSSL bin directory to this module's bin directory, or to Windows\System3.
108
-
109
- ## Nitrogen Project
110
-
111
- The Nitrogen project is housed in a set of GitHub projects:
26
+ ## Usage
112
27
 
113
- 1. [service](https://github.com/nitrogenjs/service): Core platform responsible for managing principals, security, and messaging.
114
- 2. [client](https://github.com/nitrogenjs/client): JavaScript client library for building Nitrogen devices and applications.
115
- 3. [admin](https://github.com/nitrogenjs/admin): Administrative tool for managing the Nitrogen service.
116
- 4. [device](https://github.com/nitrogenjs/devices): Adaptors for common pieces of hardware.
117
- 5. [commands](https://github.com/nitrogenjs/commands): CommandManagers and schemas for well known command types.
118
- 6. [cli](https://github.com/nitrogenjs/cli): Command line interface for working with a Nitrogen service.
28
+ See the [Nitrogen documentation](https://nitro.margelo.com/docs/nitrogen) for more information.
package/lib/Logger.js ADDED
@@ -0,0 +1,56 @@
1
+ const levelMap = {
2
+ debug: 0,
3
+ info: 1,
4
+ warning: 2,
5
+ error: 3,
6
+ };
7
+ let currentLogLevel = 'info';
8
+ export function isValidLogLevel(level) {
9
+ // @ts-expect-error
10
+ return typeof levelMap[level] === 'number';
11
+ }
12
+ export function setLogLevel(level) {
13
+ currentLogLevel = level;
14
+ }
15
+ function isAtLeast(level) {
16
+ return levelMap[level] >= levelMap[currentLogLevel];
17
+ }
18
+ let indentation = 0;
19
+ function getIndentation() {
20
+ let string = '';
21
+ for (let i = 0; i < indentation; i++) {
22
+ string += ' ';
23
+ }
24
+ return string;
25
+ }
26
+ export const Logger = {
27
+ withIndented(callback) {
28
+ try {
29
+ indentation++;
30
+ callback();
31
+ }
32
+ finally {
33
+ indentation--;
34
+ }
35
+ },
36
+ debug(message, ...extra) {
37
+ if (isAtLeast('debug')) {
38
+ console.debug(getIndentation() + message, ...extra);
39
+ }
40
+ },
41
+ info(message, ...extra) {
42
+ if (isAtLeast('info')) {
43
+ console.info(getIndentation() + message, ...extra);
44
+ }
45
+ },
46
+ warn(message, ...extra) {
47
+ if (isAtLeast('warning')) {
48
+ console.warn(getIndentation() + message, ...extra);
49
+ }
50
+ },
51
+ error(message, ...extra) {
52
+ if (isAtLeast('error')) {
53
+ console.error(getIndentation() + message, ...extra);
54
+ }
55
+ },
56
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,109 @@
1
+ import { NitroConfig } from '../../config/NitroConfig.js';
2
+ import { indent, toLowerCamelCase, toUnixPath } from '../../utils.js';
3
+ import { createFileMetadataString, getRelativeDirectory, getRelativeDirectoryGenerated, isCppFile, isNotDuplicate, } from '../../syntax/helpers.js';
4
+ export function getBuildingWithGeneratedCmakeDefinition() {
5
+ const moduleName = NitroConfig.current.getAndroidCxxLibName();
6
+ const upper = toLowerCamelCase(moduleName).toUpperCase();
7
+ return `BUILDING_${upper}_WITH_GENERATED_CMAKE_PROJECT`;
8
+ }
9
+ export function createCMakeExtension(files) {
10
+ const name = NitroConfig.current.getAndroidCxxLibName();
11
+ const sharedFiles = files
12
+ .filter((f) => f.platform === 'shared' && isCppFile(f))
13
+ .map((f) => getRelativeDirectory(f))
14
+ .map((p) => toUnixPath(p))
15
+ .filter(isNotDuplicate);
16
+ const androidFiles = files
17
+ .filter((f) => f.platform === 'android' && isCppFile(f))
18
+ .map((f) => getRelativeDirectory(f))
19
+ .map((p) => toUnixPath(p))
20
+ .filter(isNotDuplicate);
21
+ const autolinkingFilePath = getRelativeDirectoryGenerated('android', `${name}OnLoad.cpp`);
22
+ const autolinkingFile = toUnixPath(autolinkingFilePath);
23
+ const buildingWithDefinition = getBuildingWithGeneratedCmakeDefinition();
24
+ const code = `
25
+ ${createFileMetadataString(`${name}+autolinking.cmake`, '#')}
26
+
27
+ # This is a CMake file that adds all files generated by Nitrogen
28
+ # to the current CMake project.
29
+ #
30
+ # To use it, add this to your CMakeLists.txt:
31
+ # \`\`\`cmake
32
+ # include(\${CMAKE_SOURCE_DIR}/../nitrogen/generated/android/${name}+autolinking.cmake)
33
+ # \`\`\`
34
+
35
+ # Add all headers that were generated by Nitrogen
36
+ include_directories(
37
+ "../nitrogen/generated/shared/c++"
38
+ "../nitrogen/generated/android/c++"
39
+ "../nitrogen/generated/android/"
40
+ )
41
+
42
+ # Add all .cpp sources that were generated by Nitrogen
43
+ target_sources(
44
+ # CMake project name (Android C++ library name)
45
+ ${name} PRIVATE
46
+ # Autolinking Setup
47
+ ${autolinkingFile}
48
+ # Shared Nitrogen C++ sources
49
+ ${indent(sharedFiles.join('\n'), ' ')}
50
+ # Android-specific Nitrogen C++ sources
51
+ ${indent(androidFiles.join('\n'), ' ')}
52
+ )
53
+
54
+ # Define a flag to check if we are building properly
55
+ add_definitions(-D${buildingWithDefinition})
56
+
57
+ # Enable Raw Props parsing in react-native (for Nitro Views)
58
+ add_compile_options(-DRN_SERIALIZABLE_STATE=1)
59
+
60
+ # From node_modules/react-native/ReactAndroid/cmake-utils/folly-flags.cmake
61
+ # Used in node_modules/react-native/ReactAndroid/cmake-utils/ReactNative-application.cmake
62
+ target_compile_definitions(
63
+ ${name} PRIVATE
64
+ -DFOLLY_NO_CONFIG=1
65
+ -DFOLLY_HAVE_CLOCK_GETTIME=1
66
+ -DFOLLY_USE_LIBCPP=1
67
+ -DFOLLY_CFG_NO_COROUTINES=1
68
+ -DFOLLY_MOBILE=1
69
+ -DFOLLY_HAVE_RECVMMSG=1
70
+ -DFOLLY_HAVE_PTHREAD=1
71
+ # Once we target android-23 above, we can comment
72
+ # the following line. NDK uses GNU style stderror_r() after API 23.
73
+ -DFOLLY_HAVE_XSI_STRERROR_R=1
74
+ )
75
+
76
+ # Add all libraries required by the generated specs
77
+ find_package(fbjni REQUIRED) # <-- Used for communication between Java <-> C++
78
+ find_package(ReactAndroid REQUIRED) # <-- Used to set up React Native bindings (e.g. CallInvoker/TurboModule)
79
+ find_package(react-native-nitro-modules REQUIRED) # <-- Used to create all HybridObjects and use the Nitro core library
80
+
81
+ # Link all libraries together
82
+ target_link_libraries(
83
+ ${name}
84
+ fbjni::fbjni # <-- Facebook C++ JNI helpers
85
+ ReactAndroid::jsi # <-- RN: JSI
86
+ react-native-nitro-modules::NitroModules # <-- NitroModules Core :)
87
+ )
88
+
89
+ # Link react-native (different prefab between RN 0.75 and RN 0.76)
90
+ if(ReactAndroid_VERSION_MINOR GREATER_EQUAL 76)
91
+ target_link_libraries(
92
+ ${name}
93
+ ReactAndroid::reactnative # <-- RN: Native Modules umbrella prefab
94
+ )
95
+ else()
96
+ target_link_libraries(
97
+ ${name}
98
+ ReactAndroid::react_nativemodule_core # <-- RN: TurboModules Core
99
+ )
100
+ endif()
101
+ `.trim();
102
+ return {
103
+ content: code,
104
+ language: 'cmake',
105
+ name: `${name}+autolinking.cmake`,
106
+ platform: 'android',
107
+ subdirectory: [],
108
+ };
109
+ }
@@ -0,0 +1,36 @@
1
+ import { NitroConfig } from '../../config/NitroConfig.js';
2
+ import { createFileMetadataString } from '../../syntax/helpers.js';
3
+ export function createGradleExtension() {
4
+ const name = NitroConfig.current.getAndroidCxxLibName();
5
+ const code = `
6
+ ${createFileMetadataString(`${name}+autolinking.gradle`)}
7
+
8
+ /// This is a Gradle file that adds all files generated by Nitrogen
9
+ /// to the current Gradle project.
10
+ ///
11
+ /// To use it, add this to your build.gradle:
12
+ /// \`\`\`gradle
13
+ /// apply from: '../nitrogen/generated/android/${name}+autolinking.gradle'
14
+ /// \`\`\`
15
+
16
+ logger.warn("[NitroModules] 🔥 ${name} is boosted by nitro!")
17
+
18
+ android {
19
+ sourceSets {
20
+ main {
21
+ java.srcDirs += [
22
+ // Nitrogen files
23
+ "\${project.projectDir}/../nitrogen/generated/android/kotlin"
24
+ ]
25
+ }
26
+ }
27
+ }
28
+ `.trim();
29
+ return {
30
+ content: code,
31
+ language: 'gradle',
32
+ name: `${name}+autolinking.gradle`,
33
+ platform: 'android',
34
+ subdirectory: [],
35
+ };
36
+ }
@@ -0,0 +1,159 @@
1
+ import { NitroConfig } from '../../config/NitroConfig.js';
2
+ import { createCppHybridObjectRegistration } from '../../syntax/c++/CppHybridObjectRegistration.js';
3
+ import { includeHeader } from '../../syntax/c++/includeNitroHeader.js';
4
+ import { createFileMetadataString, isNotDuplicate, } from '../../syntax/helpers.js';
5
+ import { getJNINativeRegistrations } from '../../syntax/kotlin/JNINativeRegistrations.js';
6
+ import { createJNIHybridObjectRegistration } from '../../syntax/kotlin/KotlinHybridObjectRegistration.js';
7
+ import { indent } from '../../utils.js';
8
+ import { getBuildingWithGeneratedCmakeDefinition } from './createCMakeExtension.js';
9
+ export function createHybridObjectIntializer() {
10
+ const cxxNamespace = NitroConfig.current.getCxxNamespace('c++');
11
+ const cppLibName = NitroConfig.current.getAndroidCxxLibName();
12
+ const javaNamespace = NitroConfig.current.getAndroidPackage('java/kotlin');
13
+ const autolinkingClassName = `${NitroConfig.current.getAndroidCxxLibName()}OnLoad`;
14
+ const jniRegistrations = getJNINativeRegistrations()
15
+ .map((r) => `${r.namespace}::${r.className}::registerNatives();`)
16
+ .filter(isNotDuplicate);
17
+ const autolinkedHybridObjects = NitroConfig.current.getAutolinkedHybridObjects();
18
+ const cppHybridObjectImports = [];
19
+ const cppRegistrations = [];
20
+ for (const hybridObjectName of Object.keys(autolinkedHybridObjects)) {
21
+ const config = autolinkedHybridObjects[hybridObjectName];
22
+ if (config?.cpp != null) {
23
+ // Autolink a C++ HybridObject!
24
+ const { cppCode, requiredImports } = createCppHybridObjectRegistration({
25
+ hybridObjectName: hybridObjectName,
26
+ cppClassName: config.cpp,
27
+ });
28
+ cppHybridObjectImports.push(...requiredImports);
29
+ cppRegistrations.push(cppCode);
30
+ }
31
+ if (config?.kotlin != null) {
32
+ // Autolink a Kotlin HybridObject through JNI/C++!
33
+ const { cppCode, requiredImports } = createJNIHybridObjectRegistration({
34
+ hybridObjectName: hybridObjectName,
35
+ jniClassName: config.kotlin,
36
+ });
37
+ cppHybridObjectImports.push(...requiredImports);
38
+ cppRegistrations.push(cppCode);
39
+ }
40
+ }
41
+ const buildingWithDefinition = getBuildingWithGeneratedCmakeDefinition();
42
+ const includes = [
43
+ ...getJNINativeRegistrations().map((r) => includeHeader(r.import)),
44
+ ...cppHybridObjectImports.map((i) => includeHeader(i)),
45
+ ]
46
+ .filter(isNotDuplicate)
47
+ .join('\n');
48
+ const hppCode = `
49
+ ${createFileMetadataString(`${autolinkingClassName}.hpp`)}
50
+
51
+ #include <jni.h>
52
+ #include <NitroModules/NitroDefines.hpp>
53
+
54
+ namespace ${cxxNamespace} {
55
+
56
+ /**
57
+ * Initializes the native (C++) part of ${cppLibName}, and autolinks all Hybrid Objects.
58
+ * Call this in your \`JNI_OnLoad\` function (probably inside \`cpp-adapter.cpp\`).
59
+ * Example:
60
+ * \`\`\`cpp (cpp-adapter.cpp)
61
+ * JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*) {
62
+ * return ${cxxNamespace}::initialize(vm);
63
+ * }
64
+ * \`\`\`
65
+ */
66
+ int initialize(JavaVM* vm);
67
+
68
+ } // namespace ${cxxNamespace}
69
+
70
+ `;
71
+ const cppCode = `
72
+ ${createFileMetadataString(`${autolinkingClassName}.cpp`)}
73
+
74
+ #ifndef ${buildingWithDefinition}
75
+ #error ${autolinkingClassName}.cpp is not being built with the autogenerated CMakeLists.txt project. Is a different CMakeLists.txt building this?
76
+ #endif
77
+
78
+ #include "${autolinkingClassName}.hpp"
79
+
80
+ #include <jni.h>
81
+ #include <fbjni/fbjni.h>
82
+ #include <NitroModules/HybridObjectRegistry.hpp>
83
+
84
+ ${includes}
85
+
86
+ namespace ${cxxNamespace} {
87
+
88
+ int initialize(JavaVM* vm) {
89
+ using namespace margelo::nitro;
90
+ using namespace ${cxxNamespace};
91
+ using namespace facebook;
92
+
93
+ return facebook::jni::initialize(vm, [] {
94
+ // Register native JNI methods
95
+ ${indent(jniRegistrations.join('\n'), ' ')}
96
+
97
+ // Register Nitro Hybrid Objects
98
+ ${indent(cppRegistrations.join('\n'), ' ')}
99
+ });
100
+ }
101
+
102
+ } // namespace ${cxxNamespace}
103
+ `.trim();
104
+ const kotlinCode = `
105
+ ${createFileMetadataString(`${autolinkingClassName}.kt`)}
106
+
107
+ package ${javaNamespace}
108
+
109
+ import android.util.Log
110
+
111
+ internal class ${autolinkingClassName} {
112
+ companion object {
113
+ private const val TAG = "${autolinkingClassName}"
114
+ private var didLoad = false
115
+ /**
116
+ * Initializes the native part of "${cppLibName}".
117
+ * This method is idempotent and can be called more than once.
118
+ */
119
+ @JvmStatic
120
+ fun initializeNative() {
121
+ if (didLoad) return
122
+ try {
123
+ Log.i(TAG, "Loading ${cppLibName} C++ library...")
124
+ System.loadLibrary("${cppLibName}")
125
+ Log.i(TAG, "Successfully loaded ${cppLibName} C++ library!")
126
+ didLoad = true
127
+ } catch (e: Error) {
128
+ Log.e(TAG, "Failed to load ${cppLibName} C++ library! Is it properly installed and linked? " +
129
+ "Is the name correct? (see \`CMakeLists.txt\`, at \`add_library(...)\`)", e)
130
+ throw e
131
+ }
132
+ }
133
+ }
134
+ }
135
+ `.trim();
136
+ return [
137
+ {
138
+ content: hppCode,
139
+ language: 'c++',
140
+ name: `${autolinkingClassName}.hpp`,
141
+ platform: 'android',
142
+ subdirectory: [],
143
+ },
144
+ {
145
+ content: cppCode,
146
+ language: 'c++',
147
+ name: `${autolinkingClassName}.cpp`,
148
+ platform: 'android',
149
+ subdirectory: [],
150
+ },
151
+ {
152
+ content: kotlinCode,
153
+ language: 'kotlin',
154
+ name: `${autolinkingClassName}.kt`,
155
+ platform: 'android',
156
+ subdirectory: ['kotlin', ...javaNamespace.split('.')],
157
+ },
158
+ ];
159
+ }
@@ -0,0 +1,13 @@
1
+ import { createCMakeExtension } from './android/createCMakeExtension.js';
2
+ import { createGradleExtension } from './android/createGradleExtension.js';
3
+ import { createHybridObjectIntializer } from './android/createHybridObjectInitializer.js';
4
+ export function createAndroidAutolinking(allFiles) {
5
+ const cmakeExtension = createCMakeExtension(allFiles);
6
+ const gradleExtension = createGradleExtension();
7
+ const hybridObjectInitializer = createHybridObjectIntializer();
8
+ return {
9
+ platform: 'android',
10
+ jniHybridRegistrations: [],
11
+ sourceFiles: [cmakeExtension, gradleExtension, ...hybridObjectInitializer],
12
+ };
13
+ }
@@ -0,0 +1,19 @@
1
+ import { createHybridObjectIntializer } from './ios/createHybridObjectInitializer.js';
2
+ import { createPodspecRubyExtension } from './ios/createPodspecRubyExtension.js';
3
+ import { createSwiftCxxBridge } from './ios/createSwiftCxxBridge.js';
4
+ import { createSwiftUmbrellaHeader } from './ios/createSwiftUmbrellaHeader.js';
5
+ export function createIOSAutolinking() {
6
+ const podspecExtension = createPodspecRubyExtension();
7
+ const swiftCxxBridge = createSwiftCxxBridge();
8
+ const swiftUmbrellaHeader = createSwiftUmbrellaHeader();
9
+ const hybridObjectInitializer = createHybridObjectIntializer();
10
+ return {
11
+ platform: 'ios',
12
+ sourceFiles: [
13
+ podspecExtension,
14
+ ...swiftCxxBridge,
15
+ swiftUmbrellaHeader,
16
+ ...hybridObjectInitializer,
17
+ ],
18
+ };
19
+ }
@@ -0,0 +1,97 @@
1
+ import { NitroConfig } from '../../config/NitroConfig.js';
2
+ import { createCppHybridObjectRegistration } from '../../syntax/c++/CppHybridObjectRegistration.js';
3
+ import { includeHeader } from '../../syntax/c++/includeNitroHeader.js';
4
+ import { createFileMetadataString } from '../../syntax/helpers.js';
5
+ import { createSwiftHybridObjectRegistration } from '../../syntax/swift/SwiftHybridObjectRegistration.js';
6
+ import { indent } from '../../utils.js';
7
+ import { getUmbrellaHeaderName } from './createSwiftUmbrellaHeader.js';
8
+ export function createHybridObjectIntializer() {
9
+ const autolinkingClassName = `${NitroConfig.current.getIosModuleName()}Autolinking`;
10
+ const umbrellaHeaderName = getUmbrellaHeaderName();
11
+ const bridgeNamespace = NitroConfig.current.getSwiftBridgeNamespace('swift');
12
+ const autolinkedHybridObjects = NitroConfig.current.getAutolinkedHybridObjects();
13
+ const swiftFunctions = [];
14
+ const cppRegistrations = [];
15
+ const cppImports = [];
16
+ let containsSwiftObjects = false;
17
+ for (const hybridObjectName of Object.keys(autolinkedHybridObjects)) {
18
+ const config = autolinkedHybridObjects[hybridObjectName];
19
+ if (config?.cpp != null) {
20
+ // Autolink a C++ HybridObject!
21
+ const { cppCode, requiredImports } = createCppHybridObjectRegistration({
22
+ hybridObjectName: hybridObjectName,
23
+ cppClassName: config.cpp,
24
+ });
25
+ cppImports.push(...requiredImports);
26
+ cppRegistrations.push(cppCode);
27
+ }
28
+ if (config?.swift != null) {
29
+ // Autolink a Swift HybridObject!
30
+ containsSwiftObjects = true;
31
+ const { cppCode, requiredImports, swiftFunction } = createSwiftHybridObjectRegistration({
32
+ hybridObjectName: hybridObjectName,
33
+ swiftClassName: config.swift,
34
+ });
35
+ cppImports.push(...requiredImports);
36
+ cppRegistrations.push(cppCode);
37
+ swiftFunctions.push(swiftFunction);
38
+ }
39
+ }
40
+ if (cppRegistrations.length === 0) {
41
+ // Nothing to autolink!
42
+ return [];
43
+ }
44
+ const umbrellaImport = containsSwiftObjects
45
+ ? `#import "${umbrellaHeaderName}"`
46
+ : '';
47
+ const imports = cppImports.map((i) => includeHeader(i, true)).join('\n');
48
+ const objcCode = `
49
+ ${createFileMetadataString(`${autolinkingClassName}.mm`)}
50
+
51
+ #import <Foundation/Foundation.h>
52
+ #import <NitroModules/HybridObjectRegistry.hpp>
53
+ ${umbrellaImport}
54
+ #import <type_traits>
55
+
56
+ ${imports}
57
+
58
+ @interface ${autolinkingClassName} : NSObject
59
+ @end
60
+
61
+ @implementation ${autolinkingClassName}
62
+
63
+ + (void) load {
64
+ using namespace margelo::nitro;
65
+ using namespace ${NitroConfig.current.getCxxNamespace('c++')};
66
+
67
+ ${indent(cppRegistrations.join('\n'), ' ')}
68
+ }
69
+
70
+ @end
71
+ `.trim();
72
+ const swiftCode = `
73
+ ${createFileMetadataString(`${autolinkingClassName}.swift`)}
74
+
75
+ public final class ${autolinkingClassName} {
76
+ public typealias bridge = ${bridgeNamespace}
77
+
78
+ ${indent(swiftFunctions.join('\n\n'), ' ')}
79
+ }
80
+ `.trim();
81
+ return [
82
+ {
83
+ content: objcCode,
84
+ language: 'objective-c++',
85
+ name: `${autolinkingClassName}.mm`,
86
+ platform: 'ios',
87
+ subdirectory: [],
88
+ },
89
+ {
90
+ content: swiftCode,
91
+ language: 'swift',
92
+ name: `${autolinkingClassName}.swift`,
93
+ platform: 'ios',
94
+ subdirectory: [],
95
+ },
96
+ ];
97
+ }