Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test")
load("@emsdk//emscripten_toolchain:wasm_rules.bzl", "wasm_cc_binary")

package(default_visibility = ["//visibility:public"])

Expand Down Expand Up @@ -60,3 +61,21 @@ cc_binary(
":robots",
],
)

cc_binary(
name = "robots_js",
srcs = ["robots_wasm.cc"],
deps = [
":robots",
],
linkopts = [
"-l", "embind",
"-s", "EXPORTED_FUNCTIONS=_IsAllowed",
"-s", "EXPORTED_RUNTIME_METHODS=ccall,cwrap"
],
)

wasm_cc_binary(
name = "robots_wasm",
cc_target = ":robots_js",
)
5 changes: 5 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ bazel_dep(
name = "rules_cc",
version = "0.2.17",
)

bazel_dep(
name = "emsdk",
version = "5.0.4"
)
19 changes: 19 additions & 0 deletions robots_wasm.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <emscripten/bind.h>
#include "robots.h"

// Returns true if the given user agent is allowed to crawl the URL given the robots content
// Otherwise, returns false.
// We also need to make sure our function gets exported as IsAllowed
bool IsAllowed(std::string user_agent, std::string url, std::string robots_content) asm("IsAllowed");
bool IsAllowed(std::string user_agent, std::string url, std::string robots_content) {
googlebot::RobotsMatcher matcher;
std::vector<std::string> user_agents(1, user_agent);
return matcher.AllowedByRobots(robots_content, &user_agents, url);
}

// Create a binding so we can call the function from JavaScript
// Example: Module.IsAllowed('googlebot', 'https://example.com/test', 'user-agent: *\ndisallow: /') // will return false
EMSCRIPTEN_BINDINGS(my_module) {
emscripten::function("IsAllowed", &IsAllowed);
}