@browserless.io/browserless 2.0.0-beta-5 → 2.0.0-beta-6

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 (240) hide show
  1. package/LICENSE +2 -0
  2. package/README.md +11 -11
  3. package/bin/browserless.js +155 -11
  4. package/bin/scaffold/README.md +415 -0
  5. package/bin/scaffold/package.json +21 -0
  6. package/bin/scaffold/src/hello-world.http.ts +25 -0
  7. package/bin/scaffold/tsconfig.json +4 -0
  8. package/build/browserless.js +18 -15
  9. package/build/browsers/index.d.ts +2 -18
  10. package/build/browsers/index.js +43 -14
  11. package/build/http.d.ts +3 -3
  12. package/build/http.js +3 -3
  13. package/build/router.js +2 -4
  14. package/build/routes/chromium/http/content-post.body.json +8 -8
  15. package/build/routes/chromium/http/content-post.d.ts +15 -3
  16. package/build/routes/chromium/http/content-post.js +14 -15
  17. package/build/routes/chromium/http/download-post.d.ts +16 -3
  18. package/build/routes/chromium/http/download-post.js +17 -22
  19. package/build/routes/chromium/http/function-post.d.ts +16 -3
  20. package/build/routes/chromium/http/function-post.js +17 -22
  21. package/build/routes/chromium/http/pdf-post.body.json +8 -8
  22. package/build/routes/chromium/http/pdf-post.d.ts +15 -3
  23. package/build/routes/chromium/http/pdf-post.js +19 -15
  24. package/build/routes/chromium/http/performance.d.ts +15 -3
  25. package/build/routes/chromium/http/performance.js +15 -23
  26. package/build/routes/chromium/http/scrape-post.body.json +8 -8
  27. package/build/routes/chromium/http/scrape-post.d.ts +15 -3
  28. package/build/routes/chromium/http/scrape-post.js +15 -16
  29. package/build/routes/chromium/http/screenshot-post.body.json +8 -8
  30. package/build/routes/chromium/http/screenshot-post.d.ts +15 -3
  31. package/build/routes/chromium/http/screenshot-post.js +18 -15
  32. package/build/routes/chromium/tests/websocket.spec.js +20 -1
  33. package/build/routes/chromium/utils/function/handler.js +2 -2
  34. package/build/routes/chromium/ws/browser.d.ts +13 -3
  35. package/build/routes/chromium/ws/browser.js +10 -11
  36. package/build/routes/chromium/ws/cdp-chromium.d.ts +13 -3
  37. package/build/routes/chromium/ws/cdp-chromium.js +10 -11
  38. package/build/routes/chromium/ws/page.d.ts +13 -3
  39. package/build/routes/chromium/ws/page.js +10 -11
  40. package/build/routes/chromium/ws/playwright-chromium.d.ts +13 -3
  41. package/build/routes/chromium/ws/playwright-chromium.js +11 -12
  42. package/build/routes/firefox/ws/playwright-firefox.d.ts +13 -3
  43. package/build/routes/firefox/ws/playwright-firefox.js +11 -12
  44. package/build/routes/management/http/config-get.d.ts +15 -3
  45. package/build/routes/management/http/config-get.js +15 -20
  46. package/build/routes/management/http/metrics-get.d.ts +15 -3
  47. package/build/routes/management/http/metrics-get.js +16 -21
  48. package/build/routes/management/http/metrics-total-get.d.ts +15 -3
  49. package/build/routes/management/http/metrics-total-get.js +16 -21
  50. package/build/routes/management/http/sessions-get.d.ts +15 -3
  51. package/build/routes/management/http/sessions-get.js +16 -20
  52. package/build/routes/management/http/static-get.d.ts +15 -3
  53. package/build/routes/management/http/static-get.js +15 -20
  54. package/build/routes/webkit/ws/playwright-webkit.d.ts +13 -3
  55. package/build/routes/webkit/ws/playwright-webkit.js +11 -12
  56. package/build/server.js +0 -1
  57. package/build/types.d.ts +48 -38
  58. package/build/types.js +135 -0
  59. package/extensions/ublock/1p-filters.html +0 -1
  60. package/extensions/ublock/3p-filters.html +0 -2
  61. package/extensions/ublock/_locales/bg/messages.json +6 -6
  62. package/extensions/ublock/_locales/br_FR/messages.json +14 -14
  63. package/extensions/ublock/_locales/bs/messages.json +8 -8
  64. package/extensions/ublock/_locales/ca/messages.json +1 -1
  65. package/extensions/ublock/_locales/da/messages.json +5 -5
  66. package/extensions/ublock/_locales/fa/messages.json +1 -1
  67. package/extensions/ublock/_locales/fi/messages.json +6 -6
  68. package/extensions/ublock/_locales/hr/messages.json +4 -4
  69. package/extensions/ublock/_locales/nb/messages.json +1 -1
  70. package/extensions/ublock/_locales/no/messages.json +1 -1
  71. package/extensions/ublock/_locales/ro/messages.json +2 -2
  72. package/extensions/ublock/_locales/ru/messages.json +1 -1
  73. package/extensions/ublock/_locales/sk/messages.json +1 -1
  74. package/extensions/ublock/_locales/sv/messages.json +2 -2
  75. package/extensions/ublock/_locales/te/messages.json +17 -17
  76. package/extensions/ublock/_locales/vi/messages.json +12 -12
  77. package/extensions/ublock/_locales/zh_TW/messages.json +13 -13
  78. package/extensions/ublock/assets/assets.json +3 -3
  79. package/extensions/ublock/assets/resources/scriptlets.js +218 -97
  80. package/extensions/ublock/assets/thirdparties/easylist/easylist.txt +3010 -2056
  81. package/extensions/ublock/assets/thirdparties/easylist/easyprivacy.txt +624 -433
  82. package/extensions/ublock/assets/thirdparties/pgl.yoyo.org/as/serverlist +93 -24
  83. package/extensions/ublock/assets/thirdparties/publicsuffix.org/list/effective_tld_names.dat +7 -15
  84. package/extensions/ublock/assets/thirdparties/urlhaus-filter/urlhaus-filter-online.txt +795 -777
  85. package/extensions/ublock/assets/ublock/badware.min.txt +138 -72
  86. package/extensions/ublock/assets/ublock/filters.min.txt +1929 -2735
  87. package/extensions/ublock/assets/ublock/privacy.min.txt +57 -26
  88. package/extensions/ublock/assets/ublock/quick-fixes.min.txt +125 -74
  89. package/extensions/ublock/assets/ublock/unbreak.min.txt +46 -53
  90. package/extensions/ublock/css/codemirror.css +8 -7
  91. package/extensions/ublock/css/dom-inspector.css +40 -0
  92. package/extensions/ublock/css/logger-ui-inspector.css +7 -1
  93. package/extensions/ublock/css/logger-ui.css +12 -5
  94. package/extensions/ublock/css/popup-fenix.css +1 -1
  95. package/extensions/ublock/devtools.html +1 -0
  96. package/extensions/ublock/js/1p-filters.js +4 -3
  97. package/extensions/ublock/js/3p-filters.js +25 -31
  98. package/extensions/ublock/js/about.js +1 -1
  99. package/extensions/ublock/js/advanced-settings.js +1 -1
  100. package/extensions/ublock/js/asset-viewer.js +1 -1
  101. package/extensions/ublock/js/assets.js +74 -44
  102. package/extensions/ublock/js/background.js +9 -3
  103. package/extensions/ublock/js/base64-custom.js +1 -1
  104. package/extensions/ublock/js/benchmarks.js +1 -1
  105. package/extensions/ublock/js/biditrie.js +1 -1
  106. package/extensions/ublock/js/broadcast.js +75 -0
  107. package/extensions/ublock/js/cachestorage.js +68 -45
  108. package/extensions/ublock/js/click2load.js +1 -1
  109. package/extensions/ublock/js/cloud-ui.js +1 -1
  110. package/extensions/ublock/js/code-viewer.js +1 -1
  111. package/extensions/ublock/js/codemirror/search-thread.js +1 -1
  112. package/extensions/ublock/js/codemirror/search.js +1 -1
  113. package/extensions/ublock/js/codemirror/ubo-dynamic-filtering.js +1 -1
  114. package/extensions/ublock/js/codemirror/ubo-static-filtering.js +98 -24
  115. package/extensions/ublock/js/commands.js +1 -1
  116. package/extensions/ublock/js/console.js +1 -1
  117. package/extensions/ublock/js/contentscript-extra.js +1 -1
  118. package/extensions/ublock/js/contentscript.js +1 -3
  119. package/extensions/ublock/js/contextmenu.js +1 -1
  120. package/extensions/ublock/js/cosmetic-filtering.js +4 -4
  121. package/extensions/ublock/js/dashboard-common.js +1 -1
  122. package/extensions/ublock/js/dashboard.js +1 -1
  123. package/extensions/ublock/js/devtools.js +23 -15
  124. package/extensions/ublock/js/diff-updater.js +3 -3
  125. package/extensions/ublock/js/document-blocked.js +1 -1
  126. package/extensions/ublock/js/dom-inspector.js +68 -0
  127. package/extensions/ublock/js/dom.js +1 -1
  128. package/extensions/ublock/js/dyna-rules.js +1 -1
  129. package/extensions/ublock/js/dynamic-net-filtering.js +1 -1
  130. package/extensions/ublock/js/epicker-ui.js +35 -59
  131. package/extensions/ublock/js/fa-icons.js +1 -1
  132. package/extensions/ublock/js/filtering-context.js +1 -1
  133. package/extensions/ublock/js/filtering-engines.js +1 -1
  134. package/extensions/ublock/js/hnswitches.js +1 -1
  135. package/extensions/ublock/js/hntrie.js +1 -1
  136. package/extensions/ublock/js/html-filtering.js +1 -1
  137. package/extensions/ublock/js/httpheader-filtering.js +1 -1
  138. package/extensions/ublock/js/i18n.js +1 -1
  139. package/extensions/ublock/js/is-webrtc-supported.js +1 -1
  140. package/extensions/ublock/js/logger-ui-inspector.js +203 -145
  141. package/extensions/ublock/js/logger-ui.js +21 -5
  142. package/extensions/ublock/js/logger.js +6 -2
  143. package/extensions/ublock/js/lz4.js +2 -2
  144. package/extensions/ublock/js/messaging.js +266 -166
  145. package/extensions/ublock/js/mrucache.js +58 -0
  146. package/extensions/ublock/js/pagestore.js +1 -1
  147. package/extensions/ublock/js/popup-fenix.js +2 -1
  148. package/extensions/ublock/js/redirect-engine.js +1 -1
  149. package/extensions/ublock/js/redirect-resources.js +1 -12
  150. package/extensions/ublock/js/reverselookup-worker.js +1 -1
  151. package/extensions/ublock/js/reverselookup.js +1 -1
  152. package/extensions/ublock/js/scriptlet-filtering-core.js +300 -0
  153. package/extensions/ublock/js/scriptlet-filtering.js +122 -350
  154. package/extensions/ublock/js/scriptlets/cosmetic-logger.js +36 -47
  155. package/extensions/ublock/js/scriptlets/cosmetic-off.js +1 -1
  156. package/extensions/ublock/js/scriptlets/cosmetic-on.js +1 -1
  157. package/extensions/ublock/js/scriptlets/cosmetic-report.js +1 -1
  158. package/extensions/ublock/js/scriptlets/dom-inspector.js +341 -323
  159. package/extensions/ublock/js/scriptlets/dom-survey-elements.js +1 -1
  160. package/extensions/ublock/js/scriptlets/dom-survey-scripts.js +1 -1
  161. package/extensions/ublock/js/scriptlets/epicker.js +80 -89
  162. package/extensions/ublock/js/scriptlets/load-3p-css.js +1 -1
  163. package/extensions/ublock/js/scriptlets/load-large-media-all.js +1 -1
  164. package/extensions/ublock/js/scriptlets/load-large-media-interactive.js +1 -1
  165. package/extensions/ublock/js/scriptlets/noscript-spoof.js +1 -1
  166. package/extensions/ublock/js/scriptlets/should-inject-contentscript.js +1 -1
  167. package/extensions/ublock/js/scriptlets/subscriber.js +1 -1
  168. package/extensions/ublock/js/scriptlets/updater.js +20 -3
  169. package/extensions/ublock/js/settings.js +1 -1
  170. package/extensions/ublock/js/start.js +19 -20
  171. package/extensions/ublock/js/static-dnr-filtering.js +1 -1
  172. package/extensions/ublock/js/static-ext-filtering-db.js +1 -1
  173. package/extensions/ublock/js/static-ext-filtering.js +1 -1
  174. package/extensions/ublock/js/static-filtering-io.js +1 -1
  175. package/extensions/ublock/js/static-filtering-parser.js +5 -3
  176. package/extensions/ublock/js/static-net-filtering.js +57 -37
  177. package/extensions/ublock/js/storage.js +49 -29
  178. package/extensions/ublock/js/support.js +4 -4
  179. package/extensions/ublock/js/tab.js +1 -1
  180. package/extensions/ublock/js/tasks.js +1 -1
  181. package/extensions/ublock/js/text-encode.js +1 -1
  182. package/extensions/ublock/js/text-utils.js +1 -1
  183. package/extensions/ublock/js/theme.js +1 -1
  184. package/extensions/ublock/js/traffic.js +2 -1
  185. package/extensions/ublock/js/ublock.js +15 -11
  186. package/extensions/ublock/js/uri-utils.js +1 -1
  187. package/extensions/ublock/js/url-net-filtering.js +1 -1
  188. package/extensions/ublock/js/utils.js +1 -73
  189. package/extensions/ublock/js/vapi-background-ext.js +1 -1
  190. package/extensions/ublock/js/vapi-background.js +92 -83
  191. package/extensions/ublock/js/vapi-client.js +4 -33
  192. package/extensions/ublock/js/vapi-common.js +16 -30
  193. package/extensions/ublock/js/vapi.js +1 -1
  194. package/extensions/ublock/js/wasm/biditrie.wat +1 -1
  195. package/extensions/ublock/js/wasm/hntrie.wat +1 -1
  196. package/extensions/ublock/js/webext.js +1 -1
  197. package/extensions/ublock/js/whitelist.js +1 -1
  198. package/extensions/ublock/logger-ui.html +2 -2
  199. package/extensions/ublock/manifest.json +1 -1
  200. package/extensions/ublock/support.html +0 -1
  201. package/extensions/ublock/web_accessible_resources/dom-inspector.html +25 -0
  202. package/extensions/ublock/web_accessible_resources/epicker-ui.html +0 -1
  203. package/extensions/ublock/web_accessible_resources/googletagservices_gpt.js +1 -0
  204. package/package.json +7 -19
  205. package/scripts/build-open-api.js +7 -4
  206. package/src/browserless.ts +42 -18
  207. package/src/browsers/index.ts +48 -20
  208. package/src/http.ts +3 -3
  209. package/src/router.ts +2 -6
  210. package/src/routes/chromium/http/content-post.ts +13 -16
  211. package/src/routes/chromium/http/download-post.ts +16 -27
  212. package/src/routes/chromium/http/function-post.ts +16 -25
  213. package/src/routes/chromium/http/pdf-post.ts +19 -15
  214. package/src/routes/chromium/http/performance.ts +14 -26
  215. package/src/routes/chromium/http/scrape-post.ts +14 -16
  216. package/src/routes/chromium/http/screenshot-post.ts +18 -15
  217. package/src/routes/chromium/tests/websocket.spec.ts +28 -1
  218. package/src/routes/chromium/utils/function/handler.ts +2 -1
  219. package/src/routes/chromium/ws/browser.ts +10 -12
  220. package/src/routes/chromium/ws/cdp-chromium.ts +10 -12
  221. package/src/routes/chromium/ws/page.ts +10 -12
  222. package/src/routes/chromium/ws/playwright-chromium.ts +10 -12
  223. package/src/routes/firefox/ws/playwright-firefox.ts +10 -12
  224. package/src/routes/management/http/config-get.ts +14 -23
  225. package/src/routes/management/http/metrics-get.ts +15 -24
  226. package/src/routes/management/http/metrics-total-get.ts +15 -26
  227. package/src/routes/management/http/sessions-get.ts +15 -23
  228. package/src/routes/management/http/static-get.ts +14 -22
  229. package/src/routes/webkit/ws/playwright-webkit.ts +10 -12
  230. package/src/server.ts +0 -1
  231. package/src/types.ts +59 -45
  232. package/static/docs/browserless-logo-inline.svg +1 -0
  233. package/static/docs/index.html +27 -0
  234. package/static/docs/swagger.json +33 -33
  235. package/static/function/client.js +626 -78
  236. package/extensions/ublock/js/vapi-client-extra.js +0 -312
  237. package/extensions/ublock/web_accessible_resources/addthis_widget.js +0 -39
  238. package/extensions/ublock/web_accessible_resources/ligatus_angular-tag.js +0 -29
  239. package/extensions/ublock/web_accessible_resources/monkeybroker.js +0 -43
  240. package/extensions/ublock/web_accessible_resources/mxpnl_mixpanel.js +0 -51
package/LICENSE CHANGED
@@ -3,4 +3,6 @@
3
3
  This work is dual-licensed under the MongoDB Server-Side Public License OR the browserless commercial license.
4
4
  You can choose between one of them if you use this work.
5
5
 
6
+ If you want to use browserless to build commercial sites, applications, or in a continuous-integration system that's closed-source then you'll need to purchase a commercial license. This allows you to keep your software proprietary whilst still using browserless. [You can purchase a commercial license here](https://www.browserless.io/contact).
7
+
6
8
  `SPDX-License-Identifier: SSPL-1.0 OR Browserless Commercial License`
package/README.md CHANGED
@@ -8,14 +8,14 @@
8
8
  ![Multi CI](https://github.com/browserless/chrome/actions/workflows/docker-multi.yml/badge.svg)
9
9
 
10
10
 
11
- > [Looking for v1.x.x of browserless? Check it out here](https://github.com/browserless/chrome/tree/v1).
12
- > NOTE: Version 1 is the version we currently still have running on browserless' hosted services.
11
+ > [Looking for v2.x.x of browserless? Check it out here](https://github.com/browserless/browserless).
12
+ > NOTE: Version 1 is the version we currently still have running on browserless' hosted services, we would recommend self-hosted users migrate to v2.
13
13
 
14
- browserless is a web-based service that allows for remote clients to connect and execute headless work; all inside of docker. It supports new libraries like Puppeteer and Playwright, aiming to replace antiquated or in-house systems. We also bundle numerous handy REST-based APIs for doing more common actions like data collection, PDF generation and more.
14
+ Browserless allows remote clients to connect and execute headless work, all inside of docker. It supports the standard, unforked Puppeteer and Playwright libraries, as well offering REST-based APIs for common actions like data collection, PDF generation and more.
15
15
 
16
- We also take care of other common issues such as missing system-fonts, missing external libraries, and performance improvements. We even handle edge-cases like downloading files, managing sessions, and have a full documentation site built into the project which includes Open API docs.
16
+ We take care of common issues such as missing system-fonts, missing external libraries, and performance improvements, along with edge-cases like downloading files and managing sessions. For details, check out the documentation site built into the project which includes Open API docs.
17
17
 
18
- If you've been struggling to get a browser up and running docker, or scaling out your headless workloads, then browserless was built for you.
18
+ If you've been struggling to deploy headless browsers without running into issues or bloated resource requirements, then Browserless was built for you. Run the browsers in [our cloud](https://browserless.io/) or your own, [free for non-commercial uses](https://github.com/browserless/browserless#licensing).
19
19
  # Table of Contents
20
20
 
21
21
  1. [Features](#features)
@@ -40,14 +40,14 @@ If you've been struggling to get a browser up and running docker, or scaling out
40
40
  - Fonts and emoji's working out-of-the-box.
41
41
  - Debug Viewer for actively viewing/debugging running sessions.
42
42
  - An interactive puppeteer debugger, so you can see what the headless browser is doing and use its DevTools.
43
- - Works with most headless libraries.
43
+ - Works with unforked Puppeteer and Playwright.
44
44
  - Configurable session timers and health-checks to keep things running smoothly.
45
45
  - Error tolerant: if Chrome dies it won't.
46
46
  - [Support for running and development on Apple's M1 machines](#building-for-arm64-apple-m1-machines)
47
47
 
48
48
  # How it works
49
49
 
50
- browserless listens for both incoming websocket requests, generally issued by most libraries, as well as pre-build REST APIs to do common functions (PDF generation, images and so on). When a websocket connects to browserless it starts Chrome and proxies your request into it. Once the session is done then it closes and awaits for more connections. Some libraries use Chrome's HTTP endpoints, like `/json` to inspect debug-able targets, which browserless also supports.
50
+ Browserless listens for both incoming websocket requests, generally issued by most libraries, as well as pre-build REST APIs to do common functions (PDF generation, images and so on). When a websocket connects to Browserless it starts Chrome and proxies your request into it. Once the session is done then it closes and awaits for more connections. Some libraries use Chrome's HTTP endpoints, like `/json` to inspect debug-able targets, which Browserless also supports.
51
51
 
52
52
  You still execute the script itself which gives you total control over what library you want to choose and when to do upgrades. This also comes with the benefit of keep your code proprietary and able to run on numerous platforms. We simply take care of all the browser-aspects and offer a management layer on top of the browser.
53
53
 
@@ -71,7 +71,7 @@ If you're interested in using this image for commercial aspects, then please rea
71
71
 
72
72
  # Puppeteer
73
73
 
74
- Puppeteer allows you to specify a remote location for chrome via the `browserWSEndpoint` option. Setting this for browserless is a single line of code change.
74
+ Puppeteer allows you to specify a remote location for chrome via the `browserWSEndpoint` option. Setting this for Browserless is a single line of code change.
75
75
 
76
76
  **Before**
77
77
  ```js
@@ -114,13 +114,13 @@ Running Chrome on lambda or on your own is a fantastic idea but in practice is q
114
114
 
115
115
  Getting Chrome running well in docker is also a challenge as there's quiet a few packages you need in order to get Chrome running. Once that's done then there's still missing fonts, getting libraries to work with it, and having limitations on service reliability. This is also ignoring CVEs, access-controls, and scaling strategies.
116
116
 
117
- All of these issues prompted us to build a first-class image and workflow for interacting with Chrome in a more streamlined way. With browserless you never have to worry about fonts, extra packages, library support, security, or anything else. It just works reliably like any other modern web service. On top of that it comes with a prescribed approach on how you interact with Chrome, which is through socket connections (similar to a database or any other external appliance). What this means is that you get the ability to drive Chrome remotely without having to do updates/releases to the thing that runs Chrome since it's divorced from your application.
117
+ All of these issues prompted us to build a first-class image and workflow for interacting with Chrome in a more streamlined way. With Browserless you never have to worry about fonts, extra packages, library support, security, or anything else. It just works reliably like any other modern web service. On top of that it comes with a prescribed approach on how you interact with Chrome, which is through socket connections (similar to a database or any other external appliance). What this means is that you get the ability to drive Chrome remotely without having to do updates/releases to the thing that runs Chrome since it's divorced from your application.
118
118
 
119
119
  # Licensing
120
120
 
121
121
  SPDX-License-Identifier: SSPL-1.0 OR Browserless Commercial License.
122
122
 
123
- If you want to use browserless to build commercial sites, applications, or in a continuous-integration system that's closed-source then you'll need to purchase a commercial license. This allows you to keep your software proprietary whilst still using browserless. [You can purchase a commercial license here](https://www.browserless.io/contact). A commercial license grants you:
123
+ If you want to use Browserless to build commercial sites, applications, or in a continuous-integration system that's closed-source then you'll need to purchase a commercial license. This allows you to keep your software proprietary whilst still using browserless. [You can purchase a commercial license here](https://www.browserless.io/contact). A commercial license grants you:
124
124
 
125
125
  - Priority support on issues and features.
126
126
  - On-premise running as well as running on public cloud providers for commercial/CI purposes for proprietary systems.
@@ -129,4 +129,4 @@ If you want to use browserless to build commercial sites, applications, or in a
129
129
 
130
130
  Not only does it grant you a license to run such a critical piece of infrastructure, but you are also supporting further innovation in this space and our ability to contribute to it.
131
131
 
132
- If you are creating an open source application under a license compatible with the Server Side License 1.0, you may use browserless under those terms.
132
+ If you are creating an open source application under a license compatible with the Server Side License 1.0, you may use Browserless under those terms.
@@ -1,12 +1,15 @@
1
1
  #!/usr/bin/env node
2
2
  /* eslint-disable no-undef */
3
3
  'use strict';
4
+ process.env.DEBUG = process.env.DEBUG || 'browserless*';
5
+
4
6
  import { Browserless } from '@browserless.io/browserless';
5
7
  import buildOpenAPI from '../scripts/build-open-api.js';
6
8
  import buildSchemas from '../scripts/build-schemas.js';
7
9
 
8
10
  import { createInterface } from 'readline';
9
11
  import debug from 'debug';
12
+ import { dedent } from '../build/utils.js';
10
13
  import { fileURLToPath } from 'url';
11
14
  import fs from 'fs/promises';
12
15
  import path from 'path';
@@ -17,8 +20,9 @@ const promptLog = debug('browserless:prompt');
17
20
 
18
21
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
19
22
  const cmd = process.argv[2];
23
+ const subCMD = process.argv[3];
20
24
  const cwd = process.cwd();
21
- const allowedCMDs = ['build', 'dev', 'docker', 'start'];
25
+ const allowedCMDs = ['build', 'dev', 'docker', 'start', 'create', 'help'];
22
26
  const srcDir = path.join(cwd, 'build');
23
27
 
24
28
  if (!allowedCMDs.includes(cmd)) {
@@ -60,6 +64,23 @@ const importClassOverride = async (files, className) => {
60
64
  return (await import(classModuleFullFilePath)).default;
61
65
  };
62
66
 
67
+ const installDependencies = async (workingDirectory) => {
68
+ new Promise((resolve, reject) => {
69
+ spawn('npm', ['i'], {
70
+ cwd: workingDirectory,
71
+ stdio: 'inherit',
72
+ }).once('close', (code) => {
73
+ if (code === 0) {
74
+ log(`Successfully installed Dependencies.`);
75
+ return resolve();
76
+ }
77
+ return reject(
78
+ `Error when installing dependencies, see output for more details`,
79
+ );
80
+ });
81
+ });
82
+ };
83
+
63
84
  const buildDockerImage = async (cmd) => {
64
85
  new Promise((resolve, reject) => {
65
86
  const [docker, ...args] = cmd.split(' ');
@@ -94,7 +115,7 @@ const buildTypeScript = async () =>
94
115
  });
95
116
 
96
117
  const getSourceFiles = async () => {
97
- const files = await fs.readdir(srcDir);
118
+ const files = await fs.readdir(srcDir, { recursive: true });
98
119
  const [httpRoutes, webSocketRoutes] = files.reduce(
99
120
  ([httpRoutes, webSocketRoutes], file) => {
100
121
  const parsed = path.parse(file);
@@ -210,25 +231,25 @@ const start = async (dev = false) => {
210
231
 
211
232
  log(`Starting Browserless`);
212
233
 
213
- const config = isConstructor(Config) ? new Config() : config;
214
- const metrics = isConstructor(Metrics) ? new Metrics() : metrics;
215
- const token = isConstructor(Token) ? new Token(config) : token;
216
- const webhooks = isConstructor(Webhooks) ? new Webhooks(config) : webhooks;
234
+ const config = isConstructor(Config) ? new Config() : Config;
235
+ const metrics = isConstructor(Metrics) ? new Metrics() : Metrics;
236
+ const token = isConstructor(Token) ? new Token(config) : Token;
237
+ const webhooks = isConstructor(Webhooks) ? new Webhooks(config) : Webhooks;
217
238
  const browserManager = isConstructor(BrowserManager)
218
239
  ? new BrowserManager(config)
219
- : browserManager;
240
+ : BrowserManager;
220
241
  const monitoring = isConstructor(Monitoring)
221
242
  ? new Monitoring(config)
222
- : monitoring;
243
+ : Monitoring;
223
244
  const fileSystem = isConstructor(FileSystem)
224
245
  ? new FileSystem(config)
225
- : fileSystem;
246
+ : FileSystem;
226
247
  const limiter = isConstructor(Limiter)
227
248
  ? new Limiter(config, metrics, monitoring, webhooks)
228
- : limiter;
249
+ : Limiter;
229
250
  const router = isConstructor(Router)
230
251
  ? new Router(config, browserManager, limiter)
231
- : router;
252
+ : Router;
232
253
 
233
254
  const browserless = new Browserless({
234
255
  browserManager,
@@ -350,6 +371,121 @@ const buildDocker = async () => {
350
371
  }
351
372
  };
352
373
 
374
+ const create = async () => {
375
+ const validNameRegex = /^[a-zA-Z0-9-_]+$/gi;
376
+ const directory = (
377
+ await prompt('What should we name this project (hyphens are ok)?')
378
+ ).trim();
379
+ const scaffoldLocation = path.join(__dirname, 'scaffold');
380
+
381
+ if (!directory) {
382
+ throw new Error(`A valid name is required.`);
383
+ }
384
+
385
+ const isValidDirectory = validNameRegex.test(directory);
386
+
387
+ if (!isValidDirectory) {
388
+ throw new Error(`Name must not include special characters.`);
389
+ }
390
+
391
+ const installPath = path.join(cwd, directory);
392
+ log(`Creating folder "${installPath}"...`);
393
+ await fs.mkdir(installPath);
394
+
395
+ log(`Copying Project Dependencies...`);
396
+ const sdkFiles = await fs.readdir(scaffoldLocation, { recursive: true });
397
+ for (const sdkFile of sdkFiles) {
398
+ const from = path.join(scaffoldLocation, sdkFile);
399
+ const to = path.join(installPath, sdkFile);
400
+
401
+ if ((await fs.lstat(from)).isDirectory()) {
402
+ await fs.mkdir(to);
403
+ } else {
404
+ await fs.copyFile(from, to);
405
+ }
406
+ }
407
+
408
+ log(`Installing npm modules...`);
409
+ await installDependencies(installPath);
410
+ };
411
+
412
+ const help = () => {
413
+ if (subCMD) {
414
+ if (!allowedCMDs.includes(subCMD)) {
415
+ throw new Error(`Unknown command of "${subCMD}" passed.`);
416
+ }
417
+
418
+ switch (subCMD) {
419
+ case 'start':
420
+ console.log(dedent`
421
+ Usage: npx @browserless.io/browserless start
422
+
423
+ Description: Starts the HTTP server without building source.
424
+ Useful for restarting a prior build, testing quickly, or
425
+ running without packaging into a docker image.
426
+ `);
427
+ break;
428
+
429
+ case 'dev':
430
+ console.log(dedent`
431
+ Usage: npx @browserless.io/browserless dev
432
+
433
+ Description: Builds the TypeScript files, compiles runtime
434
+ route validation, generates the OpenAPI JSON document,
435
+ and starts the development server at localhost:3000.
436
+ `);
437
+ break;
438
+
439
+ case 'build':
440
+ console.log(dedent`
441
+ Usage: npx @browserless.io/browserless build
442
+
443
+ Description: Builds the TypeScript files, compiles runtime
444
+ route validation, generates the OpenAPI JSON document,
445
+ and exits. Useful for testing full compilation.
446
+ `);
447
+ break;
448
+
449
+ case 'docker':
450
+ console.log(dedent`
451
+ Usage: npx @browserless.io/browserless docker
452
+
453
+ Description: Builds a docker image from source. This command is hybrid
454
+ in that it can be either interactive or use the switches listed below.
455
+
456
+ Options:
457
+ --from The Browserless docker image to extend from (ghcr.io/browserless/multi:latest).
458
+ --action One of "push" or "load" to load or push to a registry.
459
+ --tag The full tag, including version, to name the image (IE: my-bless/chrome:latest).
460
+ --platform A comma-separated list of platforms to build for.
461
+ --proceed Proceed with building the image without prompting.
462
+ `);
463
+ break;
464
+
465
+ case 'create':
466
+ console.log(dedent`
467
+ Usage: npx @browserless.io/browserless create
468
+
469
+ Description: Creates a new project with interactive prompts.
470
+ `);
471
+ break;
472
+ }
473
+
474
+ return;
475
+ }
476
+
477
+ console.log(dedent`
478
+ Usage: npx @browserless.io/browserless [command] [arguments]
479
+
480
+ Options:
481
+ create Creates a new scaffold project, installs dependencies, and exits.
482
+ dev Compiles TypeScript, generates build assets and starts the server.
483
+ build Compiles TypeScript, generates build assets and exits.
484
+ docker Generates a docker image.
485
+ start Starts the http server with already-built assets.
486
+ `);
487
+ };
488
+
353
489
  switch (cmd) {
354
490
  case 'start':
355
491
  start(false);
@@ -366,4 +502,12 @@ switch (cmd) {
366
502
  case 'docker':
367
503
  buildDocker();
368
504
  break;
505
+
506
+ case 'create':
507
+ create();
508
+ break;
509
+
510
+ default:
511
+ help();
512
+ break;
369
513
  }