@browserless.io/browserless 2.0.0-beta-8 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/CHANGELOG.md +19 -2
  2. package/README.md +10 -6
  3. package/bin/browserless.js +49 -13
  4. package/bin/scaffold/README.md +7 -7
  5. package/bin/scaffold/tsconfig.json +1 -1
  6. package/build/browserless.js +9 -2
  7. package/build/browsers/cdp-chromium.d.ts +1 -1
  8. package/build/browsers/cdp-chromium.js +4 -5
  9. package/build/browsers/index.d.ts +10 -0
  10. package/build/browsers/index.js +44 -0
  11. package/build/browsers/playwright-chromium.d.ts +1 -1
  12. package/build/browsers/playwright-chromium.js +1 -2
  13. package/build/browsers/playwright-firefox.d.ts +1 -1
  14. package/build/browsers/playwright-firefox.js +1 -2
  15. package/build/browsers/playwright-webkit.d.ts +1 -1
  16. package/build/browsers/playwright-webkit.js +1 -2
  17. package/build/config.d.ts +9 -0
  18. package/build/config.js +15 -0
  19. package/build/constants.d.ts +1 -0
  20. package/build/constants.js +1 -0
  21. package/build/data/classes.json +1 -1
  22. package/build/data/selectors.json +1 -1
  23. package/build/file-system.spec.js +1 -1
  24. package/build/http.d.ts +4 -0
  25. package/build/http.js +4 -0
  26. package/build/routes/chromium/http/content-post.body.json +8 -8
  27. package/build/routes/chromium/http/json-list.d.ts +15 -0
  28. package/build/routes/chromium/http/json-list.js +23 -0
  29. package/build/routes/chromium/http/json-list.response.json +52 -0
  30. package/build/routes/chromium/http/json-new.d.ts +15 -0
  31. package/build/routes/chromium/http/json-new.js +23 -0
  32. package/build/routes/chromium/http/json-new.response.json +44 -0
  33. package/build/routes/chromium/http/json-protocol-get.d.ts +15 -0
  34. package/build/routes/chromium/http/json-protocol-get.js +20 -0
  35. package/build/routes/chromium/http/json-protocol-get.response.json +6 -0
  36. package/build/routes/chromium/http/json-version-get.d.ts +15 -0
  37. package/build/routes/chromium/http/json-version-get.js +30 -0
  38. package/build/routes/chromium/http/json-version-get.response.json +37 -0
  39. package/build/routes/chromium/http/pdf-post.body.json +12 -8
  40. package/build/routes/chromium/http/scrape-post.body.json +8 -8
  41. package/build/routes/chromium/http/screenshot-post.body.json +8 -8
  42. package/build/routes/chromium/tests/json-version.spec.d.ts +1 -0
  43. package/build/routes/chromium/tests/json-version.spec.js +37 -0
  44. package/build/routes/chromium/utils/cdp.d.ts +2 -0
  45. package/build/routes/chromium/utils/cdp.js +14 -0
  46. package/build/types.d.ts +31 -0
  47. package/build/utils.d.ts +9 -0
  48. package/build/utils.js +17 -2
  49. package/package.json +15 -18
  50. package/src/browserless.ts +12 -1
  51. package/src/browsers/cdp-chromium.ts +7 -10
  52. package/src/browsers/index.ts +65 -2
  53. package/src/browsers/playwright-chromium.ts +3 -4
  54. package/src/browsers/playwright-firefox.ts +3 -4
  55. package/src/browsers/playwright-webkit.ts +3 -4
  56. package/src/config.ts +17 -0
  57. package/src/constants.ts +1 -0
  58. package/src/file-system.spec.ts +1 -1
  59. package/src/http.ts +4 -0
  60. package/src/routes/chromium/http/json-list.ts +50 -0
  61. package/src/routes/chromium/http/json-new.ts +50 -0
  62. package/src/routes/chromium/http/json-protocol-get.ts +38 -0
  63. package/src/routes/chromium/http/json-version-get.ts +55 -0
  64. package/src/routes/chromium/tests/json-version.spec.ts +52 -0
  65. package/src/routes/chromium/utils/cdp.ts +19 -0
  66. package/src/types.ts +38 -0
  67. package/src/utils.ts +26 -4
  68. package/static/docs/swagger.json +404 -10
  69. package/static/function/client.js +2328 -1975
  70. package/browser.json +0 -7
  71. package/scripts/install-cdp-json.js +0 -37
@@ -0,0 +1,52 @@
1
+ import { Browserless, Config, Metrics } from '@browserless.io/browserless';
2
+ import { expect } from 'chai';
3
+
4
+ describe('/json/version 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 to /json/version', async () => {
20
+ const config = new Config();
21
+ config.setToken('browserless');
22
+ const metrics = new Metrics();
23
+ await start({ config, metrics });
24
+
25
+ const res = await fetch(
26
+ 'http://localhost:3000/json/version?token=browserless',
27
+ );
28
+ const resJSON = await res.json();
29
+
30
+ [
31
+ 'Browser',
32
+ 'Protocol-Version',
33
+ 'User-Agent',
34
+ 'V8-Version',
35
+ 'WebKit-Version',
36
+ 'webSocketDebuggerUrl',
37
+ 'Debugger-Version',
38
+ ].forEach((k) => expect(resJSON).to.haveOwnProperty(k));
39
+ });
40
+
41
+ it('rejects unauthorized requests to /json/version', async () => {
42
+ const config = new Config();
43
+ config.setToken('browserless');
44
+ const metrics = new Metrics();
45
+ await start({ config, metrics });
46
+
47
+ const res = await fetch(
48
+ 'http://localhost:3000/json/version?token=imabadboi',
49
+ );
50
+ expect(res.status).to.equal(401);
51
+ });
52
+ });
@@ -0,0 +1,19 @@
1
+ import { CDPJSONPayload, pageID } from '@browserless.io/browserless';
2
+
3
+ export const getCDPJSONPayload = (externalAddress: string): CDPJSONPayload => {
4
+ const id = pageID();
5
+ const { protocol, host, pathname, href } = new URL(
6
+ `/devtools/page/${id}`,
7
+ externalAddress,
8
+ );
9
+
10
+ return {
11
+ description: '',
12
+ devtoolsFrontendUrl: `/devtools/inspector.html?${protocol.replace(':', '')}=${host}${pathname}`,
13
+ id,
14
+ title: 'New Tab',
15
+ type: 'page',
16
+ url: 'about:blank',
17
+ webSocketDebuggerUrl: href,
18
+ };
19
+ };
package/src/types.ts CHANGED
@@ -556,3 +556,41 @@ export interface IBrowserlessStats {
556
556
  unhealthy: number;
557
557
  units: number;
558
558
  }
559
+
560
+ export interface CDPJSONPayload {
561
+ /**
562
+ * The description of the target. Generally the page's title.
563
+ */
564
+ description: string;
565
+
566
+ /**
567
+ * The fully-qualified URL of the Devtools inspector app.
568
+ */
569
+ devtoolsFrontendUrl: string;
570
+
571
+ /**
572
+ * A Unique Id for the underlying target.
573
+ */
574
+ id: string;
575
+
576
+ /**
577
+ * The title of the target. For pages this is the page's title.
578
+ */
579
+ title: string;
580
+
581
+ /**
582
+ * The type of target, generally "page" or "background_page".
583
+ */
584
+ type: string;
585
+
586
+ /**
587
+ * The current URL the target is consuming or visiting.
588
+ */
589
+ url: string;
590
+
591
+ /**
592
+ * The target or page's WebSocket Debugger URL. Primarily used for legacy
593
+ * libraries to connect and inspect or remote automate this target.
594
+ */
595
+ webSocketDebuggerUrl: string;
596
+ }
package/src/utils.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import * as fs from 'fs/promises';
2
2
  import {
3
+ BLESS_PAGE_IDENTIFIER,
3
4
  CDPChromium,
4
5
  Config,
5
6
  PlaywrightChromium,
@@ -52,6 +53,23 @@ export const jsExtension = '.js';
52
53
 
53
54
  export const id = (): string => crypto.randomUUID();
54
55
 
56
+ /**
57
+ * Generates a random, Chrome-compliant page ID with "BLESS"
58
+ * prepended. This prepended text signals to other parts of the
59
+ * system that this is a Browserless-created ID so it can be appropriately
60
+ * handled.
61
+ *
62
+ * @returns {string} A random Page ID
63
+ */
64
+ export const pageID = (): string => {
65
+ const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
66
+ const id = Array.from({ length: 32 - BLESS_PAGE_IDENTIFIER.length })
67
+ .map(() => chars[Math.floor(Math.random() * chars.length)])
68
+ .join('');
69
+
70
+ return `${BLESS_PAGE_IDENTIFIER}${id}`;
71
+ };
72
+
55
73
  export const createLogger = (domain: string): debug.Debugger => {
56
74
  return debug(`browserless.io:${domain}`);
57
75
  };
@@ -224,10 +242,14 @@ export const removeNullStringify = (
224
242
  json: unknown,
225
243
  allowNull = true,
226
244
  ): string => {
227
- return JSON.stringify(json, (_key, value) => {
228
- if (allowNull) return value;
229
- if (value !== null) return value;
230
- });
245
+ return JSON.stringify(
246
+ json,
247
+ (_key, value) => {
248
+ if (allowNull) return value;
249
+ if (value !== null) return value;
250
+ },
251
+ ' ',
252
+ );
231
253
  };
232
254
 
233
255
  export const jsonOrString = (maybeJson: string): unknown | string =>