PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/phridge/README.md

https://gitlab.com/danieldanielecki/ditectrev_v2
Markdown | 389 lines | 272 code | 117 blank | 0 comment | 0 complexity | 46a67066f3471b56c87ee7d4cb185138 MD5 | raw file
  1. phridge
  2. =======
  3. **A bridge between [node](http://nodejs.org/) and [PhantomJS](http://phantomjs.org/).**
  4. [![](https://img.shields.io/npm/v/phridge.svg)](https://www.npmjs.com/package/phridge)
  5. [![](https://img.shields.io/npm/dm/phridge.svg)](https://www.npmjs.com/package/phridge)
  6. [![Dependency Status](https://david-dm.org/peerigon/phridge.svg)](https://david-dm.org/peerigon/phridge)
  7. [![Build Status](https://travis-ci.org/peerigon/phridge.svg?branch=master)](https://travis-ci.org/peerigon/phridge)
  8. [![Coverage Status](https://img.shields.io/coveralls/peerigon/phridge.svg)](https://Coveralls.io/r/peerigon/phridge?branch=master)
  9. Working with PhantomJS in node is a bit cumbersome since you need to spawn a new PhantomJS process for every single task. However, spawning a new process is quite expensive and thus can slow down your application significantly.
  10. phridge provides an api to easily
  11. - spawn new PhantomJS processes
  12. - run functions with arguments inside PhantomJS
  13. - return results from PhantomJS to node
  14. - manage long-running PhantomJS instances
  15. Unlike other node-PhantomJS bridges phridge provides a way to run code directly inside PhantomJS instead of turning every call and assignment into an async operation.
  16. phridge uses PhantomJS' stdin and stdout for [inter-process communication](http://en.wikipedia.org/wiki/Inter-process_communication). It stringifies the given function, passes it to PhantomJS via stdin, executes it in the PhantomJS environment and passes back the results via stdout. Thus you can write your PhantomJS scripts inside your node modules in a clean and synchronous way.
  17. Instead of ...
  18. ```javascript
  19. phantom.addCookie("cookie_name", "cookie_value", "localhost", function () {
  20. phantom.createPage(function (page) {
  21. page.set("customHeaders.Referer", "http://google.com", function () {
  22. page.set(
  23. "settings.userAgent",
  24. "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5)",
  25. function () {
  26. page.open("http://localhost:9901/cookie", function (status) {
  27. page.evaluate(function (selector) {
  28. return document.querySelector(selector).innerText;
  29. }, function (text) {
  30. console.log("The element contains the following text: "+ text)
  31. }, "h1");
  32. });
  33. }
  34. );
  35. });
  36. });
  37. });
  38. ```
  39. ... you can write ...
  40. ```javascript
  41. // node
  42. phantom.run("h1", function (selector, resolve) {
  43. // this code runs inside PhantomJS
  44. phantom.addCookie("cookie_name", "cookie_value", "localhost");
  45. var page = webpage.create();
  46. page.customHeaders = {
  47. Referer: "http://google.com"
  48. };
  49. page.settings = {
  50. userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5)"
  51. };
  52. page.open("http://www.google.com", function () {
  53. var text = page.evaluate(function (selector) {
  54. return document.querySelector(selector).innerText;
  55. }, selector);
  56. // resolve the promise and pass 'text' back to node
  57. resolve(text);
  58. });
  59. }).then(function (text) {
  60. // inside node again
  61. console.log("The element contains the following text: " + text);
  62. });
  63. ```
  64. Please note that the `phantom`-object provided by phridge is completely different to the `phantom`-object inside PhantomJS. So is the `page`-object. [Check out the api](#api-phantom) for further information.
  65. <br />
  66. Installation
  67. ------------------------------------------------------------------------
  68. `npm install phridge`
  69. <br />
  70. Examples
  71. ------------------------------------------------------------------------
  72. ### Spawn a new PhantomJS process
  73. ```javascript
  74. phridge.spawn({
  75. proxyAuth: "john:1234",
  76. loadImages: false,
  77. // passing CLI-style options does also work
  78. "--remote-debugger-port": 8888
  79. }).then(function (phantom) {
  80. // phantom is now a reference to a specific PhantomJS process
  81. });
  82. ```
  83. `phridge.spawn()` takes an object which will be passed as config to PhantomJS. Check out [their documentation](http://phantomjs.org/api/command-line.html) for a detailed overview of options. CLI-style options are added as they are, so be sure to escape the space character.
  84. *Please note: There are [known issues](https://github.com/peerigon/phridge/issues/31) of PhantomJS that some config options are only supported in CLI-style.*
  85. ### Run any function inside PhantomJS
  86. ```javascript
  87. phantom.run(function () {
  88. console.log("Hi from PhantomJS");
  89. });
  90. ```
  91. phridge stringifies the given function, sends it to PhantomJS and evals it again. Hence you can't use scope variables:
  92. ```javascript
  93. var someVar = "hi";
  94. phantom.run(function () {
  95. console.log(someVar); // throws a ReferenceError
  96. });
  97. ```
  98. ### Passing arguments
  99. You can also pass arguments to the PhantomJS process:
  100. ```javascript
  101. phantom.run("hi", 2, {}, function (string, number, object) {
  102. console.log(string, number, object); // 'hi', 2, [object Object]
  103. });
  104. ```
  105. Arguments are stringified by `JSON.stringify()`, so be sure to use JSON-valid objects.
  106. ### Returning results
  107. The given function can run sync and async. However, the `run()` method itself will always run async as it needs to wait for the process to respond.
  108. **Sync**
  109. ```javascript
  110. phantom.run(function () {
  111. return Math.PI;
  112. }).then(function (pi) {
  113. console.log(pi === Math.PI); // true
  114. });
  115. ```
  116. **Async**
  117. ```javascript
  118. phantom.run(function (resolve) {
  119. setTimeout(function () {
  120. resolve("after 500 ms");
  121. }, 500);
  122. }).then(function (msg) {
  123. console.log(msg); // 'after 500 ms'
  124. });
  125. ```
  126. Results are also stringified by `JSON.stringify()`, so returning application objects with functions won't work.
  127. ```javascript
  128. phantom.run(function () {
  129. ...
  130. // doesn't work because page is not a JSON-valid object
  131. return page;
  132. });
  133. ```
  134. ### Returning errors
  135. Errors can be returned by using the `throw` keyword or by calling the `reject` function. Both ways will reject the promise returned by `run()`.
  136. **Sync**
  137. ```javascript
  138. phantom.run(function () {
  139. throw new Error("An unknown error occured");
  140. }).catch(function (err) {
  141. console.log(err); // 'An unknown error occured'
  142. });
  143. ```
  144. **Async**
  145. ```javascript
  146. phantom.run(function (resolve, reject) {
  147. setTimeout(function () {
  148. reject(new Error("An unknown error occured"));
  149. }, 500);
  150. }).catch(function (err) {
  151. console.log(err); // 'An unknown error occured'
  152. });
  153. ```
  154. ### Async methods with arguments
  155. `resolve` and `reject` are just appended to the regular arguments:
  156. ```javascript
  157. phantom.run(1, 2, 3, function (one, two, three, resolve, reject) {
  158. });
  159. ```
  160. ### Persisting states inside PhantomJS
  161. Since the function passed to `phantom.run()` can't declare variables in the global scope, it is impossible to maintain state in PhantomJS. That's why `phantom.run()` calls all functions on the same context object. Thus you can easily store state variables.
  162. ```javascript
  163. phantom.run(function () {
  164. this.message = "Hello from the first call";
  165. }).then(function () {
  166. phantom.run(function () {
  167. console.log(this.message); // 'Hello from the first call'
  168. });
  169. });
  170. ```
  171. For further convenience all PhantomJS modules are already available in the global scope.
  172. ```javascript
  173. phantom.run(function () {
  174. console.log(webpage); // [object Object]
  175. console.log(system); // [object Object]
  176. console.log(fs); // [object Object]
  177. console.log(webserver); // [object Object]
  178. console.log(child_process); // [object Object]
  179. });
  180. ```
  181. ### Working in a page context
  182. Most of the time its more useful to work in a specific webpage context. This is done by creating a Page via `phantom.createPage()` which calls internally `require("webpage").create()`. The returned page wrapper will then execute all functions bound to a PhantomJS [webpage instance](http://phantomjs.org/api/webpage/).
  183. ```javascript
  184. var page = phantom.createPage();
  185. page.run(function (resolve, reject) {
  186. // `this` is now a webpage instance
  187. this.open("http://example.com", function (status) {
  188. if (status !== "success") {
  189. return reject(new Error("Cannot load " + this.url));
  190. }
  191. resolve();
  192. });
  193. });
  194. ```
  195. And for the busy ones: You can just call `phantom.openPage(url)` which is basically the same as above:
  196. ```javascript
  197. phantom.openPage("http://example.com").then(function (page) {
  198. console.log("Example loaded");
  199. });
  200. ```
  201. ### Cleaning up
  202. If you don't need a particular page anymore, just call:
  203. ```javascript
  204. page.dispose().then(function () {
  205. console.log("page disposed");
  206. });
  207. ```
  208. This will clean up all page references inside PhantomJS.
  209. If you don't need the whole process anymore call
  210. ```javascript
  211. phantom.dispose().then(function () {
  212. console.log("process terminated");
  213. });
  214. ```
  215. which will terminate the process cleanly by calling `phantom.exit(0)` internally. You don't need to dispose all pages manuallly when you call `phantom.dispose()`.
  216. However, calling
  217. ```javascript
  218. phridge.disposeAll().then(function () {
  219. console.log("All processes created by phridge.spawn() have been terminated");
  220. });
  221. ```
  222. will terminate all processes.
  223. **I strongly recommend to call** `phridge.disposeAll()` **when the node process exits as this is the only way to ensure that all child processes terminate as well.** Since `disposeAll()` is async it is not safe to call it on `process.on("exit")`. It is better to call it on `SIGINT`, `SIGTERM` and within your regular exit flow.
  224. <br />
  225. API
  226. ----
  227. ### phridge
  228. #### .spawn(config?): Promise Phantom
  229. Spawns a new PhantomJS process with the given config. [Read the PhantomJS documentation](http://phantomjs.org/api/command-line.html) for all available config options. Use camelCase style for option names. The promise will be fulfilled with an instance of `Phantom`.
  230. #### .disposeAll(): Promise
  231. Terminates all PhantomJS processes that have been spawned. The promise will be fulfilled when all child processes emitted an `exit`-event.
  232. #### .config.stdout: Stream = process.stdout
  233. Destination stream where PhantomJS' [clean stdout](#phantom-childprocess-cleanstdout) will be piped to. Set it `null` if you don't want it. Changing the value does not affect processes that have already been spawned.
  234. #### .config.stderr: Stream = process.stderr
  235. Destination stream where PhantomJS' stderr will be piped to. Set it `null` if you don't want it. Changing the value does not affect processes that have already been spawned.
  236. ----
  237. ### <a name="api-phantom"></a>Phantom.prototype
  238. #### .childProcess: ChildProcess
  239. A reference to the [ChildProcess](http://nodejs.org/api/child_process.html#child_process_class_childprocess)-instance.
  240. #### <a name="phantom-childprocess-cleanstdout"></a> .childProcess.cleanStdout: ReadableStream
  241. phridge extends the [ChildProcess](http://nodejs.org/api/child_process.html#child_process_class_childprocess)-instance by a new stream called `cleanStdout`. This stream is piped to `process.stdout` by default. It provides all data not dedicated to phridge. Streaming data is considered to be dedicated to phridge when the new line is preceded by the classifier string `"message to node: "`.
  242. #### <a name="phantom-run"></a>.run(args..., fn): Promise *
  243. Stringifies `fn`, sends it to PhantomJS and executes it there again. `args...` are stringified using `JSON.stringify()` and passed to `fn` again. `fn` may simply `return` a result or `throw` an error or call `resolve()` or `reject()` respectively if it is asynchronous. phridge compares `fn.length` with the given number of arguments to determine whether `fn` is sync or async. The returned promise will be resolved with the result or rejected with the error.
  244. #### .createPage(): Page
  245. Creates a wrapper to execute code in the context of a specific [PhantomJS webpage](http://phantomjs.org/api/webpage/).
  246. #### .openPage(url): Promise Page
  247. Calls `phantom.createPage()`, then `page.open(url, cb)` inside PhantomJS and resolves when `cb` is called. If the returned `status` is not `"success"` the promise will be rejected.
  248. #### .dispose(): Promise
  249. Calls `phantom.exit(0)` inside PhantomJS and resolves when the child process emits an `exit`-event.
  250. ### Events
  251. #### unexpectedExit
  252. Will be emitted when PhantomJS exited without a call to `phantom.dispose()` or one of its std streams emitted an `error` event. This event may be fired on some OS when the process group receives a `SIGINT` or `SIGTERM` (see [#35](https://github.com/peerigon/phridge/pull/35)).
  253. When an `unexpectedExit` event is encountered, the `phantom` instance will be unusable and therefore automatically disposed. Usually you don't need to listen for this event.
  254. ---
  255. ### Page.prototype
  256. #### .phantom: Phantom
  257. A reference to the parent [`Phantom`](#api-phantom) instance.
  258. #### .run(args..., fn): Promise *
  259. Calls `fn` on the context of a PhantomJS page object. See [`phantom.run()`](#phantom-run) for further information.
  260. #### .dispose(): Promise
  261. Cleans up this page instance by calling `page.close()`
  262. <br />
  263. Contributing
  264. ------------------------------------------------------------------------
  265. From opening a bug report to creating a pull request: **every contribution is appreciated and welcome**. If you're planing to implement a new feature or change the api please create an issue first. This way we can ensure that your precious work is not in vain.
  266. All pull requests should have 100% test coverage (with notable exceptions) and need to pass all tests.
  267. - Call `npm test` to run the unit tests
  268. - Call `npm run coverage` to check the test coverage (using [istanbul](https://github.com/gotwarlost/istanbul))
  269. <br />
  270. License
  271. ------------------------------------------------------------------------
  272. Unlicense