Skip to content

Commit

Permalink
Add support for navigator.userAgent, closes #209
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbbot committed Jun 10, 2022
1 parent 75abe07 commit 0fd23dc
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/core/src/plugins/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
DOMException,
FetchEvent,
FixedLengthStream,
Navigator,
Request,
Response,
ScheduledEvent,
Expand Down Expand Up @@ -365,6 +366,12 @@ export class CorePlugin extends Plugin<CoreOptions> implements CoreOptions {
CompatResponse = proxyStringFormDataFiles(CompatResponse);
}

// Only include `navigator` if `global_navigator` compatibility flag is set
const compatGlobals: Context = {};
if (ctx.compat.isEnabled("global_navigator")) {
compatGlobals.navigator = new Navigator();
}

// Try to parse upstream URL if set
try {
this.upstreamURL =
Expand Down Expand Up @@ -449,6 +456,8 @@ export class CorePlugin extends Plugin<CoreOptions> implements CoreOptions {
// Approximate with serialize/deserialize if not there.
structuredClone: globalThis.structuredClone ?? structuredCloneBuffer,

...compatGlobals,

// The types below would be included automatically, but it's not possible
// to create instances of them without using their constructors and they
// may be returned from Miniflare's realm (e.g. ArrayBuffer responses,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/standards/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ export type {
} from "./http";
export { FixedLengthStream } from "./streams";
export type { ArrayBufferViewConstructor } from "./streams";
export * from "./navigator";
export * from "./timers";
3 changes: 3 additions & 0 deletions packages/core/src/standards/navigator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class Navigator {
readonly userAgent = "Cloudflare-Workers";
}
11 changes: 11 additions & 0 deletions packages/core/test/plugins/core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,17 @@ test("CorePlugin: setup: Response parses files in FormData as File objects only
resFormData = await res.formData();
t.true(resFormData.get("file") instanceof File);
});
test("CorePlugin: setup: includes navigator only if compatibility flag enabled", async (t) => {
let plugin = new CorePlugin(ctx);
let globals = (await plugin.setup()).globals;
t.is(globals?.navigator, undefined);

const compat = new Compatibility(undefined, ["global_navigator"]);
plugin = new CorePlugin({ log, compat, rootPath });
globals = (await plugin.setup()).globals;
t.is(globals?.navigator.userAgent, "Cloudflare-Workers");
});

test("CorePlugin: setup: structuredClone: creates deep-copy of value", async (t) => {
const plugin = new CorePlugin(ctx);
const { globals } = await plugin.setup();
Expand Down
7 changes: 7 additions & 0 deletions packages/core/test/standards/navigator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Navigator } from "@miniflare/core";
import test from "ava";

test("Navigator: userAgent is Cloudflare-Workers", (t) => {
const navigator = new Navigator();
t.is(navigator.userAgent, "Cloudflare-Workers");
});
7 changes: 7 additions & 0 deletions packages/shared/src/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ export interface CompatibilityFeature {
// will get a type error if they try to use an unsupported flag via the API,
// and they won't be logged in the "Enabled Compatibility Flags" section.
export type CompatibilityEnableFlag =
| "global_navigator"
| "durable_object_fetch_requires_full_url"
| "fetch_refuses_unknown_protocols"
| "formdata_parser_supports_files"
| "html_rewriter_treats_esi_include_as_void_tag";
export type CompatibilityDisableFlag =
| "no_global_navigator"
| "durable_object_fetch_allows_relative_url"
| "fetch_treats_unknown_protocols_as_http"
| "formdata_parser_converts_files_to_strings";
Expand All @@ -24,6 +26,11 @@ export type CompatibilityFlag =
| CompatibilityDisableFlag;

const FEATURES: CompatibilityFeature[] = [
{
defaultAsOf: "2022-03-21",
enableFlag: "global_navigator",
disableFlag: "no_global_navigator",
},
{
defaultAsOf: "2021-11-10",
enableFlag: "durable_object_fetch_requires_full_url",
Expand Down

0 comments on commit 0fd23dc

Please sign in to comment.