@browserless.io/browserless 2.25.0-beta-2 → 2.25.0

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 (224) hide show
  1. package/CHANGELOG.md +39 -3
  2. package/README.md +4 -5
  3. package/build/browserless.js +3 -1
  4. package/build/browsers/browsers.cdp.d.ts +3 -0
  5. package/build/browsers/browsers.cdp.js +4 -1
  6. package/build/browsers/browsers.playwright.d.ts +4 -0
  7. package/build/browsers/browsers.playwright.js +11 -2
  8. package/build/browsers/index.js +4 -3
  9. package/build/http.d.ts +9 -0
  10. package/build/http.js +9 -0
  11. package/build/routes/chrome/http/content.post.body.json +8 -8
  12. package/build/routes/chrome/http/pdf.post.body.json +8 -8
  13. package/build/routes/chrome/http/scrape.post.body.json +8 -8
  14. package/build/routes/chrome/http/screenshot.post.body.json +8 -8
  15. package/build/routes/chrome/tests/kill-sessions.spec.js +1 -1
  16. package/build/routes/chromium/http/content.post.body.json +8 -8
  17. package/build/routes/chromium/http/pdf.post.body.json +8 -8
  18. package/build/routes/chromium/http/scrape.post.body.json +8 -8
  19. package/build/routes/chromium/http/screenshot.post.body.json +8 -8
  20. package/build/routes/chromium/tests/kill-sessions.spec.js +1 -1
  21. package/build/routes/edge/http/content.post.body.json +579 -0
  22. package/build/routes/edge/http/content.post.d.ts +8 -0
  23. package/build/routes/edge/http/content.post.js +7 -0
  24. package/build/routes/edge/http/content.post.query.json +183 -0
  25. package/build/routes/edge/http/content.post.response.json +5 -0
  26. package/build/routes/edge/http/download.post.body.json +32 -0
  27. package/build/routes/edge/http/download.post.d.ts +8 -0
  28. package/build/routes/edge/http/download.post.js +7 -0
  29. package/build/routes/edge/http/download.post.query.json +120 -0
  30. package/build/routes/edge/http/download.post.response.json +4 -0
  31. package/build/routes/edge/http/function.post.body.json +32 -0
  32. package/build/routes/edge/http/function.post.d.ts +8 -0
  33. package/build/routes/edge/http/function.post.js +7 -0
  34. package/build/routes/edge/http/function.post.query.json +120 -0
  35. package/build/routes/edge/http/function.post.response.json +4 -0
  36. package/build/routes/edge/http/json-list.get.d.ts +5 -0
  37. package/build/routes/edge/http/json-list.get.js +5 -0
  38. package/build/routes/edge/http/json-list.get.response.json +52 -0
  39. package/build/routes/edge/http/json-new.put.d.ts +5 -0
  40. package/build/routes/edge/http/json-new.put.js +5 -0
  41. package/build/routes/edge/http/json-new.put.response.json +44 -0
  42. package/build/routes/edge/http/json-protocol.get.d.ts +5 -0
  43. package/build/routes/edge/http/json-protocol.get.js +5 -0
  44. package/build/routes/edge/http/json-protocol.get.response.json +6 -0
  45. package/build/routes/edge/http/json-version.get.d.ts +5 -0
  46. package/build/routes/edge/http/json-version.get.js +5 -0
  47. package/build/routes/edge/http/json-version.get.response.json +44 -0
  48. package/build/routes/edge/http/pdf.post.body.json +724 -0
  49. package/build/routes/edge/http/pdf.post.d.ts +8 -0
  50. package/build/routes/edge/http/pdf.post.js +7 -0
  51. package/build/routes/edge/http/pdf.post.query.json +120 -0
  52. package/build/routes/edge/http/pdf.post.response.json +5 -0
  53. package/build/routes/edge/http/performance.post.body.json +26 -0
  54. package/build/routes/edge/http/performance.post.d.ts +8 -0
  55. package/build/routes/edge/http/performance.post.js +7 -0
  56. package/build/routes/edge/http/performance.post.query.json +120 -0
  57. package/build/routes/edge/http/performance.post.response.json +7 -0
  58. package/build/routes/edge/http/scrape.post.body.json +626 -0
  59. package/build/routes/edge/http/scrape.post.d.ts +8 -0
  60. package/build/routes/edge/http/scrape.post.js +7 -0
  61. package/build/routes/edge/http/scrape.post.query.json +183 -0
  62. package/build/routes/edge/http/scrape.post.response.json +334 -0
  63. package/build/routes/edge/http/screenshot.post.body.json +669 -0
  64. package/build/routes/edge/http/screenshot.post.d.ts +8 -0
  65. package/build/routes/edge/http/screenshot.post.js +7 -0
  66. package/build/routes/edge/http/screenshot.post.query.json +120 -0
  67. package/build/routes/edge/http/screenshot.post.response.json +5 -0
  68. package/build/routes/edge/tests/content.spec.d.ts +1 -0
  69. package/build/routes/edge/tests/content.spec.js +312 -0
  70. package/build/routes/edge/tests/download.spec.d.ts +1 -0
  71. package/build/routes/edge/tests/download.spec.js +67 -0
  72. package/build/routes/edge/tests/function.spec.d.ts +1 -0
  73. package/build/routes/edge/tests/function.spec.js +277 -0
  74. package/build/routes/edge/tests/json-version.spec.d.ts +1 -0
  75. package/build/routes/edge/tests/json-version.spec.js +37 -0
  76. package/build/routes/edge/tests/kill-sessions.spec.d.ts +1 -0
  77. package/build/routes/edge/tests/kill-sessions.spec.js +80 -0
  78. package/build/routes/edge/tests/page-websocket.spec.d.ts +1 -0
  79. package/build/routes/edge/tests/page-websocket.spec.js +97 -0
  80. package/build/routes/edge/tests/pdf.spec.d.ts +1 -0
  81. package/build/routes/edge/tests/pdf.spec.js +345 -0
  82. package/build/routes/edge/tests/performance.spec.d.ts +1 -0
  83. package/build/routes/edge/tests/performance.spec.js +124 -0
  84. package/build/routes/edge/tests/scrape.spec.d.ts +1 -0
  85. package/build/routes/edge/tests/scrape.spec.js +354 -0
  86. package/build/routes/edge/tests/screenshot.spec.d.ts +1 -0
  87. package/build/routes/edge/tests/screenshot.spec.js +339 -0
  88. package/build/routes/edge/tests/websocket.spec.d.ts +1 -0
  89. package/build/routes/edge/tests/websocket.spec.js +384 -0
  90. package/build/routes/edge/ws/browser.d.ts +7 -0
  91. package/build/routes/edge/ws/browser.js +6 -0
  92. package/build/routes/edge/ws/browser.query.json +120 -0
  93. package/build/routes/edge/ws/cdp.d.ts +8 -0
  94. package/build/routes/edge/ws/cdp.js +7 -0
  95. package/build/routes/edge/ws/cdp.query.json +120 -0
  96. package/build/routes/edge/ws/page.d.ts +8 -0
  97. package/build/routes/edge/ws/page.js +7 -0
  98. package/build/routes/edge/ws/page.query.json +120 -0
  99. package/build/routes/edge/ws/playwright.d.ts +8 -0
  100. package/build/routes/edge/ws/playwright.js +7 -0
  101. package/build/routes/edge/ws/playwright.query.json +100 -0
  102. package/build/routes/firefox/tests/kill-sessions.spec.js +1 -1
  103. package/build/routes/management/http/meta.get.js +3 -1
  104. package/build/routes/webkit/tests/kill-sessions.spec.js +1 -1
  105. package/build/sdk-utils.js +1 -1
  106. package/build/shared/scrape.http.js +2 -2
  107. package/build/types.d.ts +32 -0
  108. package/build/types.js +18 -0
  109. package/build/utils.d.ts +1 -0
  110. package/build/utils.js +16 -2
  111. package/docker/chrome/Dockerfile +14 -14
  112. package/docker/chromium/Dockerfile +14 -14
  113. package/docker/edge/.dockerignore +16 -0
  114. package/docker/edge/Dockerfile +43 -0
  115. package/docker/firefox/Dockerfile +14 -14
  116. package/docker/multi/Dockerfile +18 -18
  117. package/docker/sdk/Dockerfile +10 -0
  118. package/extensions/ublock/_locales/ar/messages.json +3 -3
  119. package/extensions/ublock/_locales/bg/messages.json +1 -1
  120. package/extensions/ublock/_locales/br_FR/messages.json +2 -2
  121. package/extensions/ublock/_locales/cy/messages.json +11 -11
  122. package/extensions/ublock/_locales/el/messages.json +2 -2
  123. package/extensions/ublock/_locales/hu/messages.json +1 -1
  124. package/extensions/ublock/_locales/id/messages.json +1 -1
  125. package/extensions/ublock/_locales/lv/messages.json +4 -4
  126. package/extensions/ublock/_locales/mk/messages.json +130 -130
  127. package/extensions/ublock/_locales/oc/messages.json +1 -1
  128. package/extensions/ublock/_locales/pt_BR/messages.json +1 -1
  129. package/extensions/ublock/_locales/pt_PT/messages.json +2 -2
  130. package/extensions/ublock/_locales/si/messages.json +100 -100
  131. package/extensions/ublock/_locales/sr/messages.json +4 -4
  132. package/extensions/ublock/_locales/vi/messages.json +19 -19
  133. package/extensions/ublock/_locales/zh_TW/messages.json +28 -28
  134. package/extensions/ublock/assets/assets.json +33 -29
  135. package/extensions/ublock/assets/thirdparties/easylist/easylist.txt +2984 -3287
  136. package/extensions/ublock/assets/thirdparties/easylist/easyprivacy.txt +150 -171
  137. package/extensions/ublock/assets/thirdparties/pgl.yoyo.org/as/serverlist +37 -27
  138. package/extensions/ublock/assets/thirdparties/publicsuffix.org/list/effective_tld_names.dat +802 -888
  139. package/extensions/ublock/assets/thirdparties/urlhaus-filter/urlhaus-filter-online.txt +2355 -2071
  140. package/extensions/ublock/assets/ublock/badlists.txt +9 -1
  141. package/extensions/ublock/assets/ublock/badware.min.txt +354 -243
  142. package/extensions/ublock/assets/ublock/filters.min.txt +5837 -5737
  143. package/extensions/ublock/assets/ublock/privacy.min.txt +151 -38
  144. package/extensions/ublock/assets/ublock/quick-fixes.min.txt +83 -127
  145. package/extensions/ublock/assets/ublock/unbreak.min.txt +66 -50
  146. package/extensions/ublock/css/codemirror.css +4 -0
  147. package/extensions/ublock/document-blocked.html +3 -1
  148. package/extensions/ublock/js/arglist-parser.js +116 -0
  149. package/extensions/ublock/js/background.js +1 -1
  150. package/extensions/ublock/js/logger-ui.js +1 -1
  151. package/extensions/ublock/js/messaging.js +9 -2
  152. package/extensions/ublock/js/pagestore.js +3 -1
  153. package/extensions/ublock/js/redirect-engine.js +3 -1
  154. package/extensions/ublock/{assets/resources/set-attr.js → js/resources/attribute.js} +115 -11
  155. package/extensions/ublock/js/resources/base.js +38 -0
  156. package/extensions/ublock/js/resources/cookie.js +419 -0
  157. package/extensions/ublock/js/resources/href-sanitizer.js +188 -0
  158. package/extensions/ublock/js/resources/localstorage.js +235 -0
  159. package/extensions/ublock/js/resources/parse-replace.js +54 -0
  160. package/extensions/ublock/js/resources/prevent-settimeout.js +236 -0
  161. package/extensions/ublock/js/resources/proxy-apply.js +109 -0
  162. package/extensions/ublock/js/resources/replace-argument.js +120 -0
  163. package/extensions/ublock/{assets → js}/resources/run-at.js +20 -4
  164. package/extensions/ublock/{assets → js}/resources/safe-self.js +5 -4
  165. package/extensions/ublock/{assets → js}/resources/scriptlets.js +90 -1589
  166. package/extensions/ublock/js/resources/set-constant.js +287 -0
  167. package/extensions/ublock/js/resources/shared.js +44 -0
  168. package/extensions/ublock/js/resources/spoof-css.js +163 -0
  169. package/extensions/ublock/js/s14e-serializer.js +2 -1
  170. package/extensions/ublock/js/scriptlet-filtering-core.js +1 -1
  171. package/extensions/ublock/js/scriptlet-filtering.js +1 -31
  172. package/extensions/ublock/js/static-dnr-filtering.js +143 -129
  173. package/extensions/ublock/js/static-filtering-parser.js +27 -117
  174. package/extensions/ublock/js/static-net-filtering.js +53 -141
  175. package/extensions/ublock/js/traffic.js +1 -1
  176. package/extensions/ublock/js/urlskip.js +166 -0
  177. package/extensions/ublock/js/vapi-background-ext.js +38 -14
  178. package/extensions/ublock/manifest.json +1 -1
  179. package/package.json +22 -22
  180. package/src/browserless.ts +4 -0
  181. package/src/browsers/browsers.cdp.ts +5 -0
  182. package/src/browsers/browsers.playwright.ts +14 -1
  183. package/src/browsers/index.ts +5 -2
  184. package/src/http.ts +9 -0
  185. package/src/routes/chrome/tests/kill-sessions.spec.ts +1 -1
  186. package/src/routes/chromium/tests/kill-sessions.spec.ts +1 -1
  187. package/src/routes/edge/http/content.post.ts +20 -0
  188. package/src/routes/edge/http/download.post.ts +20 -0
  189. package/src/routes/edge/http/function.post.ts +20 -0
  190. package/src/routes/edge/http/json-list.get.ts +7 -0
  191. package/src/routes/edge/http/json-new.put.ts +7 -0
  192. package/src/routes/edge/http/json-protocol.get.ts +7 -0
  193. package/src/routes/edge/http/json-version.get.ts +7 -0
  194. package/src/routes/edge/http/pdf.post.ts +20 -0
  195. package/src/routes/edge/http/performance.post.ts +20 -0
  196. package/src/routes/edge/http/scrape.post.ts +20 -0
  197. package/src/routes/edge/http/screenshot.post.ts +20 -0
  198. package/src/routes/edge/tests/content.spec.ts +376 -0
  199. package/src/routes/edge/tests/download.spec.ts +77 -0
  200. package/src/routes/edge/tests/function.spec.ts +317 -0
  201. package/src/routes/edge/tests/json-version.spec.ts +52 -0
  202. package/src/routes/edge/tests/kill-sessions.spec.ts +99 -0
  203. package/src/routes/edge/tests/page-websocket.spec.ts +129 -0
  204. package/src/routes/edge/tests/pdf.spec.ts +389 -0
  205. package/src/routes/edge/tests/performance.spec.ts +155 -0
  206. package/src/routes/edge/tests/scrape.spec.ts +417 -0
  207. package/src/routes/edge/tests/screenshot.spec.ts +387 -0
  208. package/src/routes/edge/tests/websocket.spec.ts +510 -0
  209. package/src/routes/edge/ws/browser.ts +10 -0
  210. package/src/routes/edge/ws/cdp.ts +17 -0
  211. package/src/routes/edge/ws/page.ts +10 -0
  212. package/src/routes/edge/ws/playwright.ts +17 -0
  213. package/src/routes/firefox/tests/kill-sessions.spec.ts +1 -1
  214. package/src/routes/management/http/meta.get.ts +6 -1
  215. package/src/routes/management/http/static.get.ts +1 -1
  216. package/src/routes/webkit/tests/kill-sessions.spec.ts +1 -1
  217. package/src/sdk-utils.ts +1 -1
  218. package/src/shared/scrape.http.ts +2 -2
  219. package/src/types.ts +19 -0
  220. package/src/utils.ts +38 -16
  221. package/static/docs/swagger.json +2097 -10
  222. package/static/docs/swagger.min.json +2096 -9
  223. package/static/function/client.js +141 -253
  224. package/static/function/index.html +141 -253
@@ -0,0 +1,389 @@
1
+ import { Browserless, Config, Metrics } from '@browserless.io/browserless';
2
+ import { expect } from 'chai';
3
+
4
+ describe('/edge/pdf API', function () {
5
+ let browserless: Browserless;
6
+
7
+ const start = ({
8
+ config = new Config(),
9
+ metrics = new Metrics(),
10
+ }: { config?: Config; metrics?: Metrics } = {}) => {
11
+ browserless = new Browserless({ config, metrics });
12
+ return browserless.start();
13
+ };
14
+
15
+ afterEach(async () => {
16
+ await browserless.stop();
17
+ });
18
+
19
+ it('allows requests', async () => {
20
+ const config = new Config();
21
+ config.setToken('browserless');
22
+ const metrics = new Metrics();
23
+ await start({ config, metrics });
24
+ const body = {
25
+ url: 'https://example.com',
26
+ };
27
+
28
+ await fetch('http://localhost:3000/edge/pdf?token=browserless', {
29
+ body: JSON.stringify(body),
30
+ headers: {
31
+ 'content-type': 'application/json',
32
+ },
33
+ method: 'POST',
34
+ }).then(async (res) => {
35
+ await res.body?.pipeTo(new WritableStream({}));
36
+ expect(res.headers.get('x-response-code')).to.not.be.undefined;
37
+ expect(res.headers.get('x-response-url')).to.not.be.undefined;
38
+ expect(res.headers.get('x-response-ip')).to.not.be.undefined;
39
+ expect(res.headers.get('x-response-por')).to.not.be.undefined;
40
+ expect(res.headers.get('content-type')).to.equal('application/pdf');
41
+ expect(res.status).to.equal(200);
42
+ });
43
+ });
44
+
45
+ it('404s GET requests', async () => {
46
+ const config = new Config();
47
+ config.setToken('browserless');
48
+ const metrics = new Metrics();
49
+ await start({ config, metrics });
50
+
51
+ await fetch('http://localhost:3000/edge/pdf?token=browserless').then(
52
+ (res) => {
53
+ expect(res.headers.get('content-type')).to.equal(
54
+ 'text/plain; charset=UTF-8',
55
+ );
56
+ expect(res.status).not.to.equal(200);
57
+ },
58
+ );
59
+ });
60
+
61
+ it('handles `waitForFunction` properties', async () => {
62
+ const config = new Config();
63
+ config.setToken('browserless');
64
+ const metrics = new Metrics();
65
+ await start({ config, metrics });
66
+ const body = {
67
+ url: 'https://example.com',
68
+ waitForFunction: {
69
+ fn: '() => 5 + 5',
70
+ },
71
+ };
72
+
73
+ await fetch('http://localhost:3000/edge/pdf?token=browserless', {
74
+ body: JSON.stringify(body),
75
+ headers: {
76
+ 'content-type': 'application/json',
77
+ },
78
+ method: 'POST',
79
+ }).then(async (res) => {
80
+ await res.body?.pipeTo(new WritableStream({}));
81
+ expect(res.headers.get('content-type')).to.equal('application/pdf');
82
+ expect(res.status).to.equal(200);
83
+ });
84
+ });
85
+
86
+ it('handles async `waitForFunction` properties', async () => {
87
+ const config = new Config();
88
+ config.setToken('browserless');
89
+ const metrics = new Metrics();
90
+ await start({ config, metrics });
91
+ const body = {
92
+ url: 'https://example.com',
93
+ waitForFunction: {
94
+ fn: 'async () => new Promise(resolve => resolve(5))',
95
+ },
96
+ };
97
+
98
+ await fetch('http://localhost:3000/edge/pdf?token=browserless', {
99
+ body: JSON.stringify(body),
100
+ headers: {
101
+ 'content-type': 'application/json',
102
+ },
103
+ method: 'POST',
104
+ }).then(async (res) => {
105
+ await res.body?.pipeTo(new WritableStream({}));
106
+ expect(res.headers.get('content-type')).to.equal('application/pdf');
107
+ expect(res.status).to.equal(200);
108
+ });
109
+ });
110
+
111
+ it('handles `waitForSelector` properties', async () => {
112
+ const config = new Config();
113
+ config.setToken('browserless');
114
+ const metrics = new Metrics();
115
+ await start({ config, metrics });
116
+ const body = {
117
+ url: 'https://example.com',
118
+ waitForSelector: {
119
+ selector: 'h1',
120
+ },
121
+ };
122
+
123
+ await fetch('http://localhost:3000/edge/pdf?token=browserless', {
124
+ body: JSON.stringify(body),
125
+ headers: {
126
+ 'content-type': 'application/json',
127
+ },
128
+ method: 'POST',
129
+ }).then(async (res) => {
130
+ await res.body?.pipeTo(new WritableStream({}));
131
+ expect(res.headers.get('content-type')).to.equal('application/pdf');
132
+ expect(res.status).to.equal(200);
133
+ });
134
+ });
135
+
136
+ it('handles `waitForEvent` properties', async () => {
137
+ const config = new Config();
138
+ config.setToken('browserless');
139
+ const metrics = new Metrics();
140
+ await start({ config, metrics });
141
+ const body = {
142
+ html: `<script type="text/javascript">
143
+ const event = new Event("customEvent");
144
+ setTimeout(() => document.dispatchEvent(event), 1500);
145
+ </script>`,
146
+ waitForEvent: {
147
+ event: 'customEvent',
148
+ },
149
+ };
150
+
151
+ await fetch('http://localhost:3000/edge/pdf?token=browserless', {
152
+ body: JSON.stringify(body),
153
+ headers: {
154
+ 'content-type': 'application/json',
155
+ },
156
+ method: 'POST',
157
+ }).then(async (res) => {
158
+ await res.body?.pipeTo(new WritableStream({}));
159
+ expect(res.headers.get('content-type')).to.equal('application/pdf');
160
+ expect(res.status).to.equal(200);
161
+ });
162
+ });
163
+
164
+ it('allows cookies', async () => {
165
+ const config = new Config();
166
+ config.setToken('browserless');
167
+ const metrics = new Metrics();
168
+ await start({ config, metrics });
169
+ const body = {
170
+ cookies: [{ domain: 'example.com', name: 'foo', value: 'bar' }],
171
+ url: 'https://example.com',
172
+ };
173
+
174
+ await fetch('http://localhost:3000/edge/pdf?token=browserless', {
175
+ body: JSON.stringify(body),
176
+ headers: {
177
+ 'content-type': 'application/json',
178
+ },
179
+ method: 'POST',
180
+ }).then(async (res) => {
181
+ await res.body?.pipeTo(new WritableStream({}));
182
+ expect(res.headers.get('content-type')).to.equal('application/pdf');
183
+ expect(res.status).to.equal(200);
184
+ });
185
+ });
186
+
187
+ it('times out requests', async () => {
188
+ const config = new Config();
189
+ config.setToken('browserless');
190
+ const metrics = new Metrics();
191
+ await start({ config, metrics });
192
+
193
+ const body = {
194
+ url: 'https://example.com',
195
+ };
196
+
197
+ await fetch('http://localhost:3000/edge/pdf?token=browserless&timeout=10', {
198
+ body: JSON.stringify(body),
199
+ headers: {
200
+ 'content-type': 'application/json',
201
+ },
202
+ method: 'POST',
203
+ }).then((res) => {
204
+ expect(res.status).to.equal(408);
205
+ });
206
+ });
207
+
208
+ it('rejects requests', async () => {
209
+ const config = new Config();
210
+ const metrics = new Metrics();
211
+ config.setConcurrent(0);
212
+ config.setQueued(0);
213
+ config.setToken('browserless');
214
+
215
+ await start({ config, metrics });
216
+
217
+ const body = {
218
+ url: 'https://example.com',
219
+ };
220
+
221
+ await fetch('http://localhost:3000/edge/pdf?token=browserless', {
222
+ body: JSON.stringify(body),
223
+ headers: {
224
+ 'content-type': 'application/json',
225
+ },
226
+ method: 'POST',
227
+ }).then((res) => {
228
+ expect(res.status).to.equal(429);
229
+ });
230
+ });
231
+
232
+ it('allows for providing http response payloads', async () => {
233
+ const config = new Config();
234
+ const metrics = new Metrics();
235
+ config.setConcurrent(10);
236
+ config.setQueued(10);
237
+ config.setTimeout(30000);
238
+ config.setToken('browserless');
239
+
240
+ await start({ config, metrics });
241
+ const body = {
242
+ requestInterceptors: [
243
+ {
244
+ pattern: '.*data.json',
245
+ response: {
246
+ body: '{"data": 123}',
247
+ contentType: 'application/json',
248
+ status: 200,
249
+ },
250
+ },
251
+ ],
252
+ url: 'https://example.com',
253
+ };
254
+
255
+ await fetch('http://localhost:3000/edge/pdf?token=browserless', {
256
+ body: JSON.stringify(body),
257
+ headers: {
258
+ 'content-type': 'application/json',
259
+ },
260
+ method: 'POST',
261
+ }).then(async (res) => {
262
+ await res.body?.pipeTo(new WritableStream({}));
263
+ expect(res.headers.get('content-type')).to.equal('application/pdf');
264
+ expect(res.status).to.equal(200);
265
+ });
266
+ });
267
+
268
+ it('allows setting goto options', async () => {
269
+ const config = new Config();
270
+ config.setToken('browserless');
271
+ const metrics = new Metrics();
272
+ await start({ config, metrics });
273
+ const body = {
274
+ gotoOptions: {
275
+ waitUntil: `networkidle2`,
276
+ },
277
+ url: 'https://example.com',
278
+ };
279
+
280
+ await fetch('http://localhost:3000/edge/pdf?token=browserless', {
281
+ body: JSON.stringify(body),
282
+ headers: {
283
+ 'content-type': 'application/json',
284
+ },
285
+ method: 'POST',
286
+ }).then(async (res) => {
287
+ await res.body?.pipeTo(new WritableStream({}));
288
+ expect(res.headers.get('content-type')).to.equal('application/pdf');
289
+ expect(res.status).to.equal(200);
290
+ });
291
+ });
292
+
293
+ it('allows setting HTML body', async () => {
294
+ const config = new Config();
295
+ config.setToken('browserless');
296
+ const metrics = new Metrics();
297
+ await start({ config, metrics });
298
+ const body = {
299
+ html: '<h1>Hello!</h1>',
300
+ };
301
+
302
+ await fetch('http://localhost:3000/edge/pdf?token=browserless', {
303
+ body: JSON.stringify(body),
304
+ headers: {
305
+ 'content-type': 'application/json',
306
+ },
307
+ method: 'POST',
308
+ }).then(async (res) => {
309
+ await res.body?.pipeTo(new WritableStream({}));
310
+ expect(res.headers.get('content-type')).to.equal('application/pdf');
311
+ expect(res.status).to.equal(200);
312
+ });
313
+ });
314
+
315
+ it('allows setting PDF options', async () => {
316
+ const config = new Config();
317
+ config.setToken('browserless');
318
+ const metrics = new Metrics();
319
+ await start({ config, metrics });
320
+ const body = {
321
+ options: {
322
+ landscape: true,
323
+ },
324
+ url: 'https://example.com',
325
+ };
326
+
327
+ await fetch('http://localhost:3000/edge/pdf?token=browserless', {
328
+ body: JSON.stringify(body),
329
+ headers: {
330
+ 'content-type': 'application/json',
331
+ },
332
+ method: 'POST',
333
+ }).then(async (res) => {
334
+ await res.body?.pipeTo(new WritableStream({}));
335
+ expect(res.headers.get('content-type')).to.equal('application/pdf');
336
+ expect(res.status).to.equal(200);
337
+ });
338
+ });
339
+
340
+ it('allows custom viewports', async () => {
341
+ const config = new Config();
342
+ config.setToken('browserless');
343
+ const metrics = new Metrics();
344
+ await start({ config, metrics });
345
+ const body = {
346
+ url: 'https://example.com',
347
+ viewport: {
348
+ deviceScaleFactor: 3,
349
+ height: 100,
350
+ width: 100,
351
+ },
352
+ };
353
+
354
+ await fetch('http://localhost:3000/edge/pdf?token=browserless', {
355
+ body: JSON.stringify(body),
356
+ headers: {
357
+ 'content-type': 'application/json',
358
+ },
359
+ method: 'POST',
360
+ }).then(async (res) => {
361
+ await res.body?.pipeTo(new WritableStream({}));
362
+ expect(res.headers.get('content-type')).to.equal('application/pdf');
363
+ expect(res.status).to.equal(200);
364
+ });
365
+ });
366
+
367
+ it('allows requests without token when auth token is not set', async () => {
368
+ await start();
369
+ const body = {
370
+ url: 'https://example.com',
371
+ };
372
+
373
+ await fetch('http://localhost:3000/edge/pdf', {
374
+ body: JSON.stringify(body),
375
+ headers: {
376
+ 'content-type': 'application/json',
377
+ },
378
+ method: 'POST',
379
+ }).then(async (res) => {
380
+ await res.body?.pipeTo(new WritableStream({}));
381
+ expect(res.headers.get('x-response-code')).to.not.be.undefined;
382
+ expect(res.headers.get('x-response-url')).to.not.be.undefined;
383
+ expect(res.headers.get('x-response-ip')).to.not.be.undefined;
384
+ expect(res.headers.get('x-response-por')).to.not.be.undefined;
385
+ expect(res.headers.get('content-type')).to.equal('application/pdf');
386
+ expect(res.status).to.equal(200);
387
+ });
388
+ });
389
+ });
@@ -0,0 +1,155 @@
1
+ import { Browserless, Config, Metrics } from '@browserless.io/browserless';
2
+ import { expect } from 'chai';
3
+
4
+ describe('/edge/performance API', function () {
5
+ let browserless: Browserless;
6
+
7
+ const start = ({
8
+ config = new Config(),
9
+ metrics = new Metrics(),
10
+ }: { config?: Config; metrics?: Metrics } = {}) => {
11
+ browserless = new Browserless({ config, metrics });
12
+ return browserless.start();
13
+ };
14
+
15
+ afterEach(async () => {
16
+ await browserless.stop();
17
+ });
18
+
19
+ it('allows requests', async () => {
20
+ const config = new Config();
21
+ config.setToken('browserless');
22
+ const metrics = new Metrics();
23
+ await start({ config, metrics });
24
+ const body = {
25
+ url: 'https://example.com',
26
+ };
27
+
28
+ await fetch('http://localhost:3000/edge/performance?token=browserless', {
29
+ body: JSON.stringify(body),
30
+ headers: {
31
+ 'content-type': 'application/json',
32
+ },
33
+ method: 'POST',
34
+ }).then((res) => {
35
+ expect(res.headers.get('content-type')).to.equal(
36
+ 'application/json; charset=UTF-8',
37
+ );
38
+ expect(res.status).to.equal(200);
39
+ });
40
+ });
41
+
42
+ it('404s GET requests', async () => {
43
+ const config = new Config();
44
+ config.setToken('browserless');
45
+ const metrics = new Metrics();
46
+ await start({ config, metrics });
47
+
48
+ await fetch(
49
+ 'http://localhost:3000/edge/performance?token=browserless',
50
+ ).then((res) => {
51
+ expect(res.headers.get('content-type')).to.equal(
52
+ 'text/plain; charset=UTF-8',
53
+ );
54
+ expect(res.status).not.to.equal(200);
55
+ });
56
+ });
57
+
58
+ it('allows setting config', async () => {
59
+ const config = new Config();
60
+ config.setToken('browserless');
61
+ const metrics = new Metrics();
62
+ await start({ config, metrics });
63
+ const body = {
64
+ config: {
65
+ extends: 'lighthouse:default',
66
+ settings: {
67
+ onlyAudits: ['unminified-css'],
68
+ },
69
+ },
70
+ url: 'https://browserless.io',
71
+ };
72
+
73
+ await fetch('http://localhost:3000/edge/performance?token=browserless', {
74
+ body: JSON.stringify(body),
75
+ headers: {
76
+ 'content-type': 'application/json',
77
+ },
78
+ method: 'POST',
79
+ }).then(async (res) => {
80
+ expect(res.headers.get('content-type')).to.equal(
81
+ 'application/json; charset=UTF-8',
82
+ );
83
+ expect(res.status).to.equal(200);
84
+
85
+ const json = await res.json();
86
+ expect(json).to.have.property('data');
87
+ expect(json.data.audits).to.have.all.keys('unminified-css');
88
+ });
89
+ });
90
+
91
+ it('times out request', async () => {
92
+ const config = new Config();
93
+ config.setTimeout(10);
94
+ config.setToken('browserless');
95
+ const metrics = new Metrics();
96
+ await start({ config, metrics });
97
+
98
+ const body = {
99
+ url: 'https://example.com',
100
+ };
101
+
102
+ await fetch('http://localhost:3000/edge/performance?token=browserless', {
103
+ body: JSON.stringify(body),
104
+ headers: {
105
+ 'content-type': 'application/json',
106
+ },
107
+ method: 'POST',
108
+ }).then((res) => {
109
+ expect(res.status).to.equal(408);
110
+ });
111
+ });
112
+
113
+ it('rejects requests', async () => {
114
+ const config = new Config();
115
+ const metrics = new Metrics();
116
+ config.setConcurrent(0);
117
+ config.setQueued(0);
118
+ config.setToken('browserless');
119
+ await start({ config, metrics });
120
+
121
+ const body = {
122
+ url: 'https://example.com',
123
+ };
124
+
125
+ await fetch('http://localhost:3000/edge/performance?token=browserless', {
126
+ body: JSON.stringify(body),
127
+ headers: {
128
+ 'content-type': 'application/json',
129
+ },
130
+ method: 'POST',
131
+ }).then((res) => {
132
+ expect(res.status).to.equal(429);
133
+ });
134
+ });
135
+
136
+ it('allows requests without token when auth token is not set', async () => {
137
+ await start();
138
+ const body = {
139
+ url: 'https://example.com',
140
+ };
141
+
142
+ await fetch('http://localhost:3000/edge/performance', {
143
+ body: JSON.stringify(body),
144
+ headers: {
145
+ 'content-type': 'application/json',
146
+ },
147
+ method: 'POST',
148
+ }).then((res) => {
149
+ expect(res.headers.get('content-type')).to.equal(
150
+ 'application/json; charset=UTF-8',
151
+ );
152
+ expect(res.status).to.equal(200);
153
+ });
154
+ });
155
+ });