diff --git a/src/workerd/api/global-scope.c++ b/src/workerd/api/global-scope.c++ index 913476d5eb4..d5ff0cf09cc 100644 --- a/src/workerd/api/global-scope.c++ +++ b/src/workerd/api/global-scope.c++ @@ -880,16 +880,19 @@ double Performance::now() { } #ifdef WORKERD_EXPERIMENTAL_ENABLE_WEBGPU -jsg::Ref Navigator::getGPU(CompatibilityFlags::Reader flags) { +jsg::Optional> Navigator::getGPU(CompatibilityFlags::Reader flags) { // is this a durable object? KJ_IF_SOME(actor, IoContext::current().getActor()) { - JSG_REQUIRE(actor.getPersistent() != kj::none, TypeError, - "webgpu api is only available in Durable Objects (no storage)"); + if (actor.getPersistent() == kj::none) { + return kj::none; + } } else { - JSG_FAIL_REQUIRE(TypeError, "webgpu api is only available in Durable Objects"); + return kj::none; }; - JSG_REQUIRE(flags.getWebgpu(), TypeError, "webgpu needs the webgpu compatibility flag set"); + if (!flags.getWebgpu()) { + return kj::none; + } return jsg::alloc(); } diff --git a/src/workerd/api/global-scope.h b/src/workerd/api/global-scope.h index 8a09ffb705c..919fc72d3ac 100644 --- a/src/workerd/api/global-scope.h +++ b/src/workerd/api/global-scope.h @@ -74,7 +74,7 @@ class Navigator: public jsg::Object { return "Cloudflare-Workers"_kj; } #ifdef WORKERD_EXPERIMENTAL_ENABLE_WEBGPU - jsg::Ref getGPU(CompatibilityFlags::Reader flags); + jsg::Optional> getGPU(CompatibilityFlags::Reader flags); #endif bool sendBeacon(jsg::Lock& js, kj::String url, jsg::Optional body); diff --git a/src/workerd/api/tests/global-scope-test.js b/src/workerd/api/tests/global-scope-test.js index 3fa2884b7ec..37858e920ef 100644 --- a/src/workerd/api/tests/global-scope-test.js +++ b/src/workerd/api/tests/global-scope-test.js @@ -2,11 +2,13 @@ import { deepStrictEqual, strictEqual, throws, + doesNotThrow, notStrictEqual, ok, } from 'node:assert'; import { AsyncLocalStorage } from 'node:async_hooks'; +import util from 'node:util'; export const navigatorUserAgent = { async test() { @@ -744,3 +746,8 @@ export const toStringTag = { strictEqual(new Headers()[internalFlag], internalFlag); }, }; +export const validateGlobalThis = { + test() { + util.inspect(globalThis); + }, +}; diff --git a/types/generated-snapshot/2022-03-21/index.d.ts b/types/generated-snapshot/2022-03-21/index.d.ts index a04ad690e2d..ea05f03a1c1 100755 --- a/types/generated-snapshot/2022-03-21/index.d.ts +++ b/types/generated-snapshot/2022-03-21/index.d.ts @@ -469,7 +469,7 @@ declare abstract class Navigator { | URLSearchParams, ): boolean; readonly userAgent: string; - readonly gpu: GPU; + readonly gpu?: GPU; } /** * The Workers runtime supports a subset of the Performance API, used to measure timing and performance, diff --git a/types/generated-snapshot/2022-03-21/index.ts b/types/generated-snapshot/2022-03-21/index.ts index 0899302931d..34fa63b4b4e 100755 --- a/types/generated-snapshot/2022-03-21/index.ts +++ b/types/generated-snapshot/2022-03-21/index.ts @@ -474,7 +474,7 @@ export declare abstract class Navigator { | URLSearchParams, ): boolean; readonly userAgent: string; - readonly gpu: GPU; + readonly gpu?: GPU; } /** * The Workers runtime supports a subset of the Performance API, used to measure timing and performance, diff --git a/types/generated-snapshot/2022-08-04/index.d.ts b/types/generated-snapshot/2022-08-04/index.d.ts index 3a75e303bfe..280f001a1dc 100755 --- a/types/generated-snapshot/2022-08-04/index.d.ts +++ b/types/generated-snapshot/2022-08-04/index.d.ts @@ -469,7 +469,7 @@ declare abstract class Navigator { | URLSearchParams, ): boolean; readonly userAgent: string; - readonly gpu: GPU; + readonly gpu?: GPU; } /** * The Workers runtime supports a subset of the Performance API, used to measure timing and performance, diff --git a/types/generated-snapshot/2022-08-04/index.ts b/types/generated-snapshot/2022-08-04/index.ts index e2afb123ccf..d53a0abbea9 100755 --- a/types/generated-snapshot/2022-08-04/index.ts +++ b/types/generated-snapshot/2022-08-04/index.ts @@ -474,7 +474,7 @@ export declare abstract class Navigator { | URLSearchParams, ): boolean; readonly userAgent: string; - readonly gpu: GPU; + readonly gpu?: GPU; } /** * The Workers runtime supports a subset of the Performance API, used to measure timing and performance, diff --git a/types/generated-snapshot/2022-10-31/index.d.ts b/types/generated-snapshot/2022-10-31/index.d.ts index 3af1c244e26..49b1e9bff6e 100755 --- a/types/generated-snapshot/2022-10-31/index.d.ts +++ b/types/generated-snapshot/2022-10-31/index.d.ts @@ -469,7 +469,7 @@ declare abstract class Navigator { | URLSearchParams, ): boolean; readonly userAgent: string; - readonly gpu: GPU; + readonly gpu?: GPU; } /** * The Workers runtime supports a subset of the Performance API, used to measure timing and performance, diff --git a/types/generated-snapshot/2022-10-31/index.ts b/types/generated-snapshot/2022-10-31/index.ts index 51793a72543..b0e46cd052c 100755 --- a/types/generated-snapshot/2022-10-31/index.ts +++ b/types/generated-snapshot/2022-10-31/index.ts @@ -474,7 +474,7 @@ export declare abstract class Navigator { | URLSearchParams, ): boolean; readonly userAgent: string; - readonly gpu: GPU; + readonly gpu?: GPU; } /** * The Workers runtime supports a subset of the Performance API, used to measure timing and performance, diff --git a/types/generated-snapshot/2022-11-30/index.d.ts b/types/generated-snapshot/2022-11-30/index.d.ts index 657ed4ae4a4..e5c2d19eabd 100755 --- a/types/generated-snapshot/2022-11-30/index.d.ts +++ b/types/generated-snapshot/2022-11-30/index.d.ts @@ -474,7 +474,7 @@ declare abstract class Navigator { | URLSearchParams, ): boolean; readonly userAgent: string; - readonly gpu: GPU; + readonly gpu?: GPU; } /** * The Workers runtime supports a subset of the Performance API, used to measure timing and performance, diff --git a/types/generated-snapshot/2022-11-30/index.ts b/types/generated-snapshot/2022-11-30/index.ts index 5cdf9b28aa4..25bf4076d44 100755 --- a/types/generated-snapshot/2022-11-30/index.ts +++ b/types/generated-snapshot/2022-11-30/index.ts @@ -479,7 +479,7 @@ export declare abstract class Navigator { | URLSearchParams, ): boolean; readonly userAgent: string; - readonly gpu: GPU; + readonly gpu?: GPU; } /** * The Workers runtime supports a subset of the Performance API, used to measure timing and performance, diff --git a/types/generated-snapshot/2023-03-01/index.d.ts b/types/generated-snapshot/2023-03-01/index.d.ts index 2a604f01b7a..3b6f50ac59e 100755 --- a/types/generated-snapshot/2023-03-01/index.d.ts +++ b/types/generated-snapshot/2023-03-01/index.d.ts @@ -474,7 +474,7 @@ declare abstract class Navigator { | URLSearchParams, ): boolean; readonly userAgent: string; - readonly gpu: GPU; + readonly gpu?: GPU; } /** * The Workers runtime supports a subset of the Performance API, used to measure timing and performance, diff --git a/types/generated-snapshot/2023-03-01/index.ts b/types/generated-snapshot/2023-03-01/index.ts index 251228bd91b..e07db1d98ec 100755 --- a/types/generated-snapshot/2023-03-01/index.ts +++ b/types/generated-snapshot/2023-03-01/index.ts @@ -479,7 +479,7 @@ export declare abstract class Navigator { | URLSearchParams, ): boolean; readonly userAgent: string; - readonly gpu: GPU; + readonly gpu?: GPU; } /** * The Workers runtime supports a subset of the Performance API, used to measure timing and performance, diff --git a/types/generated-snapshot/2023-07-01/index.d.ts b/types/generated-snapshot/2023-07-01/index.d.ts index fd499c12ada..c5dd48489ef 100755 --- a/types/generated-snapshot/2023-07-01/index.d.ts +++ b/types/generated-snapshot/2023-07-01/index.d.ts @@ -474,7 +474,7 @@ declare abstract class Navigator { | URLSearchParams, ): boolean; readonly userAgent: string; - readonly gpu: GPU; + readonly gpu?: GPU; } /** * The Workers runtime supports a subset of the Performance API, used to measure timing and performance, diff --git a/types/generated-snapshot/2023-07-01/index.ts b/types/generated-snapshot/2023-07-01/index.ts index a41034b6dd9..b8c8d018cd1 100755 --- a/types/generated-snapshot/2023-07-01/index.ts +++ b/types/generated-snapshot/2023-07-01/index.ts @@ -479,7 +479,7 @@ export declare abstract class Navigator { | URLSearchParams, ): boolean; readonly userAgent: string; - readonly gpu: GPU; + readonly gpu?: GPU; } /** * The Workers runtime supports a subset of the Performance API, used to measure timing and performance, diff --git a/types/generated-snapshot/experimental/index.d.ts b/types/generated-snapshot/experimental/index.d.ts index f6a5e67fdd2..68956abed15 100755 --- a/types/generated-snapshot/experimental/index.d.ts +++ b/types/generated-snapshot/experimental/index.d.ts @@ -476,7 +476,7 @@ declare abstract class Navigator { | URLSearchParams, ): boolean; readonly userAgent: string; - readonly gpu: GPU; + readonly gpu?: GPU; } /** * The Workers runtime supports a subset of the Performance API, used to measure timing and performance, diff --git a/types/generated-snapshot/experimental/index.ts b/types/generated-snapshot/experimental/index.ts index b76063b5042..850a66bdd6e 100755 --- a/types/generated-snapshot/experimental/index.ts +++ b/types/generated-snapshot/experimental/index.ts @@ -481,7 +481,7 @@ export declare abstract class Navigator { | URLSearchParams, ): boolean; readonly userAgent: string; - readonly gpu: GPU; + readonly gpu?: GPU; } /** * The Workers runtime supports a subset of the Performance API, used to measure timing and performance,