summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKayla Washburn <mckayla@hey.com>2022-08-10 12:57:30 -0600
committerGitHub <noreply@github.com>2022-08-10 12:57:30 -0600
commit08061b60d9be2b6990d1134aa5b94ec36f9266aa (patch)
tree88f4621b251d74cdb179afd1af07cc446951ca08
parentf16fe443038454ef49641b097c0a0161a97bd1c3 (diff)
fix: allow setting `globalThis.location` when no `--location` is provided (#15448)
-rw-r--r--cli/tests/testdata/070_location.ts8
-rw-r--r--cli/tests/testdata/070_location.ts.out1
-rw-r--r--cli/tests/testdata/071_location_unset.ts13
-rw-r--r--cli/tests/testdata/071_location_unset.ts.out1
-rw-r--r--runtime/js/99_main.js38
5 files changed, 47 insertions, 14 deletions
diff --git a/cli/tests/testdata/070_location.ts b/cli/tests/testdata/070_location.ts
index 149de5423..05e5abdf1 100644
--- a/cli/tests/testdata/070_location.ts
+++ b/cli/tests/testdata/070_location.ts
@@ -1,7 +1,15 @@
+// deno-lint-ignore-file no-global-assign
console.log(Location);
console.log(Location.prototype);
console.log(location);
try {
+ location = {};
+} catch (error) {
+ if (error instanceof Error) {
+ console.log(error.toString());
+ }
+}
+try {
location.hostname = "bar";
} catch (error) {
if (error instanceof Error) {
diff --git a/cli/tests/testdata/070_location.ts.out b/cli/tests/testdata/070_location.ts.out
index e05561e58..692d7c976 100644
--- a/cli/tests/testdata/070_location.ts.out
+++ b/cli/tests/testdata/070_location.ts.out
@@ -11,5 +11,6 @@ Location {
protocol: "https:",
search: "?baz"
}
+NotSupportedError: Cannot set "location".
NotSupportedError: Cannot set "location.hostname".
[WILDCARD]
diff --git a/cli/tests/testdata/071_location_unset.ts b/cli/tests/testdata/071_location_unset.ts
index bb60df8c4..f560d2716 100644
--- a/cli/tests/testdata/071_location_unset.ts
+++ b/cli/tests/testdata/071_location_unset.ts
@@ -1,3 +1,16 @@
console.log(Location);
console.log(Location.prototype);
console.log(location);
+
+globalThis.location = {
+ hash: "#bat",
+ host: "foo",
+ hostname: "foo",
+ href: "https://foo/bar?baz#bat",
+ origin: "https://foo",
+ pathname: "/bar",
+ port: "",
+ protocol: "https:",
+ search: "?baz",
+};
+console.log(location.pathname);
diff --git a/cli/tests/testdata/071_location_unset.ts.out b/cli/tests/testdata/071_location_unset.ts.out
index f786d7a64..99f87a7fc 100644
--- a/cli/tests/testdata/071_location_unset.ts.out
+++ b/cli/tests/testdata/071_location_unset.ts.out
@@ -1,4 +1,5 @@
[WILDCARD][Function: Location]
Location {}
undefined
+/bar
[WILDCARD]
diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js
index 167862a9e..b397fcb47 100644
--- a/runtime/js/99_main.js
+++ b/runtime/js/99_main.js
@@ -639,6 +639,18 @@ delete Intl.v8BreakIterator;
throw new Error("Worker runtime already bootstrapped");
}
+ const {
+ args,
+ location: locationHref,
+ noColor,
+ isTty,
+ pid,
+ ppid,
+ unstableFlag,
+ cpuCount,
+ userAgent: userAgentInfo,
+ } = runtimeOptions;
+
performance.setTimeOrigin(DateNow());
const consoleFromV8 = window.console;
const wrapConsole = window.__bootstrap.console.wrapConsole;
@@ -648,6 +660,18 @@ delete Intl.v8BreakIterator;
delete globalThis.bootstrap;
util.log("bootstrapMainRuntime");
hasBootstrapped = true;
+
+ // If the `--location` flag isn't set, make `globalThis.location` `undefined` and
+ // writable, so that they can mock it themselves if they like. If the flag was
+ // set, define `globalThis.location`, using the provided value.
+ if (locationHref == null) {
+ mainRuntimeGlobalProperties.location = {
+ writable: true,
+ };
+ } else {
+ location.setLocationHref(locationHref);
+ }
+
ObjectDefineProperties(globalThis, windowOrWorkerGlobalScope);
if (runtimeOptions.unstableFlag) {
ObjectDefineProperties(globalThis, unstableWindowOrWorkerGlobalScope);
@@ -678,22 +702,8 @@ delete Intl.v8BreakIterator;
});
runtimeStart(runtimeOptions);
- const {
- args,
- location: locationHref,
- noColor,
- isTty,
- pid,
- ppid,
- unstableFlag,
- cpuCount,
- userAgent: userAgentInfo,
- } = runtimeOptions;
colors.setNoColor(noColor || !isTty);
- if (locationHref != null) {
- location.setLocationHref(locationHref);
- }
numCpus = cpuCount;
userAgent = userAgentInfo;
registerErrors();