Skip to content

Commit

Permalink
fix(op_crates/web): Use WorkerLocation for location in workers
Browse files Browse the repository at this point in the history
  • Loading branch information
nayeemrmn committed Jan 16, 2021
1 parent 69fb2b3 commit c109930
Show file tree
Hide file tree
Showing 12 changed files with 277 additions and 100 deletions.
72 changes: 72 additions & 0 deletions cli/dts/lib.deno.window.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,75 @@ declare function confirm(message?: string): boolean;
* @param defaultValue
*/
declare function prompt(message?: string, defaultValue?: string): string | null;

// TODO(nayeemrmn): Move this to `op_crates/web` where its implementation is.
// The types there must first be split into window, worker and global types.
/** The location (URL) of the object it is linked to. Changes done on it are
* reflected on the object it relates to. Accessible via
* `globalThis.location`. */
declare class Location {
constructor();
/** Returns a DOMStringList object listing the origins of the ancestor
* browsing contexts, from the parent browsing context to the top-level
* browsing context.
*
* Always empty in Deno. */
readonly ancestorOrigins: DOMStringList;
/** Returns the Location object's URL's fragment (includes leading "#" if
* non-empty).
*
* Cannot be set in Deno. */
hash: string;
/** Returns the Location object's URL's host and port (if different from the
* default port for the scheme).
*
* Cannot be set in Deno. */
host: string;
/** Returns the Location object's URL's host.
*
* Cannot be set in Deno. */
hostname: string;
/** Returns the Location object's URL.
*
* Cannot be set in Deno. */
href: string;
toString(): string;
/** Returns the Location object's URL's origin. */
readonly origin: string;
/** Returns the Location object's URL's path.
*
* Cannot be set in Deno. */
pathname: string;
/** Returns the Location object's URL's port.
*
* Cannot be set in Deno. */
port: string;
/** Returns the Location object's URL's scheme.
*
* Cannot be set in Deno. */
protocol: string;
/** Returns the Location object's URL's query (includes leading "?" if
* non-empty).
*
* Cannot be set in Deno. */
search: string;
/** Navigates to the given URL.
*
* Cannot be set in Deno. */
assign(url: string): void;
/** Reloads the current page.
*
* Disabled in Deno. */
reload(): void;
/** @deprecated */
reload(forcedReload: boolean): void;
/** Removes the current page from the session history and navigates to the
* given URL.
*
* Disabled in Deno. */
replace(url: string): void;
}

// TODO(nayeemrmn): Move this to `op_crates/web` where its implementation is.
// The types there must first be split into window, worker and global types.
declare var location: Location;
23 changes: 23 additions & 0 deletions cli/dts/lib.deno.worker.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,26 @@ declare var onerror:
declare var close: () => void;
declare var name: string;
declare var postMessage: (message: any) => void;

// TODO(nayeemrmn): Move this to `op_crates/web` where its implementation is.
// The types there must first be split into window, worker and global types.
/** The absolute location of the script executed by the Worker. Such an object
* is initialized for each worker and is available via the
* WorkerGlobalScope.location property obtained by calling self.location. */
declare class WorkerLocation {
constructor();
readonly hash: string;
readonly host: string;
readonly hostname: string;
readonly href: string;
toString(): string;
readonly origin: string;
readonly pathname: string;
readonly port: string;
readonly protocol: string;
readonly search: string;
}

// TODO(nayeemrmn): Move this to `op_crates/web` where its implementation is.
// The types there must first be split into window, worker and global types.
declare var location: WorkerLocation;
5 changes: 4 additions & 1 deletion cli/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1437,7 +1437,10 @@ fn location_arg<'a, 'b>() -> Arg<'a, 'b> {
if url.is_err() {
return Err("Failed to parse URL".to_string());
}
if !["http", "https"].contains(&url.unwrap().scheme()) {
let mut url = url.unwrap();
url.set_username("").unwrap();
url.set_password(None).unwrap();
if !["http", "https"].contains(&url.scheme()) {
return Err("Expected protocol \"http\" or \"https\"".to_string());
}
Ok(())
Expand Down
2 changes: 0 additions & 2 deletions cli/tests/070_location.ts.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ Location {
hostname: [Getter/Setter],
href: [Getter/Setter],
origin: [Getter],
password: [Getter/Setter],
pathname: [Getter/Setter],
port: [Getter/Setter],
protocol: [Getter/Setter],
search: [Getter/Setter],
username: [Getter/Setter],
ancestorOrigins: [Getter],
assign: [Function: assign],
reload: [Function: reload],
Expand Down
1 change: 1 addition & 0 deletions cli/tests/079_location_authentication.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(location.href);
3 changes: 3 additions & 0 deletions cli/tests/079_location_authentication.ts.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[WILDCARD]
https://baz/qux
[WILDCARD]
5 changes: 5 additions & 0 deletions cli/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2651,6 +2651,11 @@ itest!(_078_unload_on_exit {
output: "078_unload_on_exit.ts.out",
});

itest!(_079_location_authentication {
args: "run --location https://foo:bar@baz/qux 079_location_authentication.ts",
output: "079_location_authentication.ts.out",
});

itest!(js_import_detect {
args: "run --quiet --reload js_import_detect.ts",
output: "js_import_detect.ts.out",
Expand Down
4 changes: 3 additions & 1 deletion cli/tests/subdir/worker_location.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
onmessage = function (): void {
postMessage(self.location.href);
postMessage(
`${location.href}, ${location instanceof WorkerLocation}`,
);
close();
};
2 changes: 1 addition & 1 deletion cli/tests/workers_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ Deno.test({
new URL("subdir/worker_location.ts", import.meta.url).href;
const w = new Worker(workerModuleHref, { type: "module" });
w.onmessage = (e): void => {
assertEquals(e.data, workerModuleHref);
assertEquals(e.data, `${workerModuleHref}, true`);
promise.resolve();
};
w.postMessage("Hello, world!");
Expand Down
Loading

0 comments on commit c109930

Please sign in to comment.