Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BrowserFetcher bindings #42

Merged
merged 12 commits into from
Mar 26, 2018
30 changes: 30 additions & 0 deletions __tests__/puppeteer_test.re
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,36 @@ describe("Puppeteer", () => {
);
});

describe("BrowserFetcher", () => {
let browserFetcher = ref(BrowserFetcher.empty());
beforeAll(() =>
browserFetcher :=
Puppeteer.createBrowserFetcher(
~options=Puppeteer.makeBrowserFetcherOption(),
(),
)
);
test("createBrowserFetcher", () =>
browserFetcher |> expect |> ExpectJs.toBeTruthy
);
testPromise("canDownload", () =>
Js.Promise.(
browserFetcher^
|> BrowserFetcher.canDownload(_, "533271")
|> then_(boolean =>
boolean |> Js.to_bool |> expect |> toBe(true) |> resolve
)
)
);
testPromise("download", () =>
Js.Promise.(
browserFetcher^
|> BrowserFetcher.download(~revision="533271")
|> then_(info => info##revision |> expect |> toBe("533271") |> resolve)
)
);
});

describe("Browser", () => {
let browser = ref(Browser.empty());
beforeAllPromise(() =>
Expand Down
24 changes: 24 additions & 0 deletions lib/js/__tests__/puppeteer_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var Js_primitive = require("bs-platform/lib/js/js_primitive.js");
var Page$BsPuppeteer = require("../src/Page.js");
var Unit$BsPuppeteer = require("../src/Unit.js");
var Puppeteer$BsPuppeteer = require("../src/Puppeteer.js");
var BrowserFetcher$BsPuppeteer = require("../src/BrowserFetcher.js");

var getElementValueJs = ( function (element) { return element.value; } );

Expand Down Expand Up @@ -46,6 +47,29 @@ describe("Puppeteer", (function () {
}));
}));

describe("BrowserFetcher", (function () {
var browserFetcher = [/* () */0];
beforeAll((function () {
browserFetcher[0] = Puppeteer.createBrowserFetcher(Puppeteer$BsPuppeteer.makeBrowserFetcherOption(/* None */0, /* None */0, /* None */0, /* () */0));
return /* () */0;
}));
Jest.test("createBrowserFetcher", (function () {
return Jest.ExpectJs[/* toBeTruthy */28](Jest.Expect[/* expect */0](browserFetcher));
}));
Jest.testPromise(/* None */0, "canDownload", (function () {
return browserFetcher[0].canDownload("533271").then((function ($$boolean) {
return Promise.resolve(Jest.Expect[/* toBe */2](/* true */1, Jest.Expect[/* expect */0](+$$boolean)));
}));
}));
return Jest.testPromise(/* None */0, "download", (function () {
return (function (eta) {
return BrowserFetcher$BsPuppeteer.download("533271", /* None */0, eta);
})(browserFetcher[0]).then((function (info) {
return Promise.resolve(Jest.Expect[/* toBe */2]("533271", Jest.Expect[/* expect */0](info.revision)));
}));
}));
}));

describe("Browser", (function () {
var browser = [/* () */0];
Jest.beforeAllPromise(/* None */0, (function () {
Expand Down
40 changes: 40 additions & 0 deletions lib/js/src/BrowserFetcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

var Js_mapperRt = require("bs-platform/lib/js/js_mapperRt.js");
var Js_undefined = require("bs-platform/lib/js/js_undefined.js");

function download(revision, progressCallback, t) {
return t.download(revision, Js_undefined.fromOption(progressCallback));
}

var jsMapperConstantArray = /* array */[
/* tuple */[
-577272933,
"win32"
],
/* tuple */[
-577272262,
"Win64"
],
/* tuple */[
3850863,
"mac"
],
/* tuple */[
137321428,
"linux"
]
];

function platformToJs(param) {
return Js_mapperRt.binarySearch(4, param, jsMapperConstantArray);
}

function platformFromJs(param) {
return Js_mapperRt.revSearch(4, jsMapperConstantArray, param);
}

exports.download = download;
exports.platformToJs = platformToJs;
exports.platformFromJs = platformFromJs;
/* No side effect */
18 changes: 18 additions & 0 deletions lib/js/src/Puppeteer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

var Js_option = require("bs-platform/lib/js/js_option.js");
var Util$BsPuppeteer = require("./Util.js");
var BrowserFetcher$BsPuppeteer = require("./BrowserFetcher.js");

function makeConnectOptions(browserWSEndpoint, ignoreHTTPSErrors, _) {
var tmp = { };
Expand Down Expand Up @@ -69,6 +71,22 @@ function makeLaunchOptions(ignoreHTTPSErrors, headless, executablePath, slowMo,
return tmp;
}

function makeBrowserFetcherOption(host, path, platform, _) {
var tmp = { };
if (host) {
tmp.host = host[0];
}
if (path) {
tmp.path = path[0];
}
var tmp$1 = Js_option.map(BrowserFetcher$BsPuppeteer.platformToJs, platform);
if (tmp$1) {
tmp.platform = tmp$1[0];
}
return tmp;
}

exports.makeConnectOptions = makeConnectOptions;
exports.makeLaunchOptions = makeLaunchOptions;
exports.makeBrowserFetcherOption = makeBrowserFetcherOption;
/* No side effect */
44 changes: 44 additions & 0 deletions src/BrowserFetcher.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
type t;

type revisionInfo = {
.
"revision": string,
"folderPath": string,
"executablePath": string,
"url": string,
"local": Js.boolean,
};

external empty : unit => t = "%identity";

[@bs.send]
external canDownload : (t, string) => Js.Promise.t(Js.boolean) = "";

[@bs.send]
external download :
(t, string, Js.Undefined.t((float, float) => unit)) =>
Js.Promise.t(revisionInfo) =
"";

let download =
(~revision, ~progressCallback=?, t)
: Js.Promise.t(revisionInfo) =>
download(t, revision, progressCallback |> Js.Undefined.fromOption);

[@bs.send] external localRevisions : t => Js.Promise.t(array(string)) = "";

[@bs.deriving jsConverter]
type platform = [
| [@bs.as "mac"] `Mac
| [@bs.as "linux"] `Linux
| [@bs.as "win32"] `Win32
| [@bs.as "Win64"] `Win64
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

win64 should be all lower case. If you just make the polymorphic variants all lower case and the same as the string doesn't it convert them to the right string? No need to use bs.as.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought variant constructor need to be capitalized. tried lower case but without success.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Polymorphic variants don't need to be capitalized. For example: https://github.com/bs-puppeteer/bs-puppeteer/blob/master/src/ConsoleMessage.re

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! That works, Thanks!

];

/* Returns one of mac, linux, win32 or win64. */
[@bs.send] external platform : t => platform = "";

[@bs.send] external remove : (t, string) => Js.Promise.t(unit) = "";

[@bs.send]
external revisionInfo : (t, string) => Js.Promise.t(revisionInfo) = "";
29 changes: 29 additions & 0 deletions src/Puppeteer.re
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,32 @@ external launch : (~options: launchOptions=?, unit) => Js.Promise.t(Browser.t) =
[@bs.val]
[@bs.module "puppeteer"]
external defaultArgs : unit => array(string) = "";

type browserFetcherOptions = {
.
"host": Js.undefined(string),
"path": Js.undefined(string),
"platform": Js.undefined(string),
};

[@bs.obj]
external makeBrowserFetcherOption :
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be consistent this should be makeBrowserFetcherOptions, plural.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit 784ba5c

(~host: string=?, ~path: string=?, ~platform: string=?, unit) =>
browserFetcherOptions =
"";

let makeBrowserFetcherOption =
(~host=?, ~path=?, ~platform=?, ())
: browserFetcherOptions =>
makeBrowserFetcherOption(
~host?,
~path?,
~platform=?
platform |> Js.Option.map((. v) => v |> BrowserFetcher.platformToJs),
(),
);

[@bs.val] [@bs.module "puppeteer"]
external createBrowserFetcher :
(~options: browserFetcherOptions=?, unit) => BrowserFetcher.t =
"";