protocol-parser.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. "use strict";
  2. /**
  3. * Copyright 2022 Google LLC.
  4. * Copyright (c) Microsoft Corporation.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  19. if (k2 === undefined) k2 = k;
  20. var desc = Object.getOwnPropertyDescriptor(m, k);
  21. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  22. desc = { enumerable: true, get: function() { return m[k]; } };
  23. }
  24. Object.defineProperty(o, k2, desc);
  25. }) : (function(o, m, k, k2) {
  26. if (k2 === undefined) k2 = k;
  27. o[k2] = m[k];
  28. }));
  29. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  30. Object.defineProperty(o, "default", { enumerable: true, value: v });
  31. }) : function(o, v) {
  32. o["default"] = v;
  33. });
  34. var __importStar = (this && this.__importStar) || function (mod) {
  35. if (mod && mod.__esModule) return mod;
  36. var result = {};
  37. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  38. __setModuleDefault(result, mod);
  39. return result;
  40. };
  41. Object.defineProperty(exports, "__esModule", { value: true });
  42. exports.Permissions = exports.Cdp = exports.Storage = exports.Input = exports.Session = exports.BrowsingContext = exports.Script = exports.Network = exports.Browser = exports.parseObject = void 0;
  43. /**
  44. * @fileoverview Provides parsing and validator for WebDriver BiDi protocol.
  45. * Parser types should match the `../protocol` types.
  46. */
  47. const zod_1 = require("zod");
  48. const protocol_js_1 = require("../protocol/protocol.js");
  49. const WebDriverBidiPermissions = __importStar(require("./generated/webdriver-bidi-permissions.js"));
  50. const WebDriverBidi = __importStar(require("./generated/webdriver-bidi.js"));
  51. function parseObject(obj, schema) {
  52. const parseResult = schema.safeParse(obj);
  53. if (parseResult.success) {
  54. return parseResult.data;
  55. }
  56. const errorMessage = parseResult.error.errors
  57. .map((e) => `${e.message} in ` +
  58. `${e.path.map((p) => JSON.stringify(p)).join('/')}.`)
  59. .join(' ');
  60. throw new protocol_js_1.InvalidArgumentException(errorMessage);
  61. }
  62. exports.parseObject = parseObject;
  63. /** @see https://w3c.github.io/webdriver-bidi/#module-browser */
  64. var Browser;
  65. (function (Browser) {
  66. function parseRemoveUserContextParams(params) {
  67. return parseObject(params, WebDriverBidi.Browser.RemoveUserContextParametersSchema);
  68. }
  69. Browser.parseRemoveUserContextParams = parseRemoveUserContextParams;
  70. })(Browser || (exports.Browser = Browser = {}));
  71. /** @see https://w3c.github.io/webdriver-bidi/#module-network */
  72. var Network;
  73. (function (Network) {
  74. function parseAddInterceptParameters(params) {
  75. // Work around of `cddlconv` https://github.com/google/cddlconv/issues/19.
  76. return parseObject(params, WebDriverBidi.Network.AddInterceptParametersSchema);
  77. }
  78. Network.parseAddInterceptParameters = parseAddInterceptParameters;
  79. function parseContinueRequestParameters(params) {
  80. return parseObject(params, WebDriverBidi.Network.ContinueRequestParametersSchema);
  81. }
  82. Network.parseContinueRequestParameters = parseContinueRequestParameters;
  83. function parseContinueResponseParameters(params) {
  84. // TODO: remove cast after https://github.com/google/cddlconv/issues/19 is fixed.
  85. return parseObject(params, WebDriverBidi.Network.ContinueResponseParametersSchema);
  86. }
  87. Network.parseContinueResponseParameters = parseContinueResponseParameters;
  88. function parseContinueWithAuthParameters(params) {
  89. return parseObject(params, WebDriverBidi.Network.ContinueWithAuthParametersSchema);
  90. }
  91. Network.parseContinueWithAuthParameters = parseContinueWithAuthParameters;
  92. function parseFailRequestParameters(params) {
  93. return parseObject(params, WebDriverBidi.Network.FailRequestParametersSchema);
  94. }
  95. Network.parseFailRequestParameters = parseFailRequestParameters;
  96. function parseProvideResponseParameters(params) {
  97. // TODO: remove cast after https://github.com/google/cddlconv/issues/19 is fixed.
  98. return parseObject(params, WebDriverBidi.Network.ProvideResponseParametersSchema);
  99. }
  100. Network.parseProvideResponseParameters = parseProvideResponseParameters;
  101. function parseRemoveInterceptParameters(params) {
  102. return parseObject(params, WebDriverBidi.Network.RemoveInterceptParametersSchema);
  103. }
  104. Network.parseRemoveInterceptParameters = parseRemoveInterceptParameters;
  105. })(Network || (exports.Network = Network = {}));
  106. /** @see https://w3c.github.io/webdriver-bidi/#module-script */
  107. var Script;
  108. (function (Script) {
  109. function parseGetRealmsParams(params) {
  110. return parseObject(params, WebDriverBidi.Script.GetRealmsParametersSchema);
  111. }
  112. Script.parseGetRealmsParams = parseGetRealmsParams;
  113. function parseEvaluateParams(params) {
  114. return parseObject(params, WebDriverBidi.Script.EvaluateParametersSchema);
  115. }
  116. Script.parseEvaluateParams = parseEvaluateParams;
  117. function parseDisownParams(params) {
  118. return parseObject(params, WebDriverBidi.Script.DisownParametersSchema);
  119. }
  120. Script.parseDisownParams = parseDisownParams;
  121. function parseAddPreloadScriptParams(params) {
  122. return parseObject(params, WebDriverBidi.Script.AddPreloadScriptParametersSchema);
  123. }
  124. Script.parseAddPreloadScriptParams = parseAddPreloadScriptParams;
  125. function parseRemovePreloadScriptParams(params) {
  126. return parseObject(params, WebDriverBidi.Script.RemovePreloadScriptParametersSchema);
  127. }
  128. Script.parseRemovePreloadScriptParams = parseRemovePreloadScriptParams;
  129. function parseCallFunctionParams(params) {
  130. return parseObject(params, WebDriverBidi.Script.CallFunctionParametersSchema);
  131. }
  132. Script.parseCallFunctionParams = parseCallFunctionParams;
  133. })(Script || (exports.Script = Script = {}));
  134. /** @see https://w3c.github.io/webdriver-bidi/#module-browsingContext */
  135. var BrowsingContext;
  136. (function (BrowsingContext) {
  137. function parseActivateParams(params) {
  138. return parseObject(params, WebDriverBidi.BrowsingContext.ActivateParametersSchema);
  139. }
  140. BrowsingContext.parseActivateParams = parseActivateParams;
  141. function parseGetTreeParams(params) {
  142. return parseObject(params, WebDriverBidi.BrowsingContext.GetTreeParametersSchema);
  143. }
  144. BrowsingContext.parseGetTreeParams = parseGetTreeParams;
  145. function parseNavigateParams(params) {
  146. return parseObject(params, WebDriverBidi.BrowsingContext.NavigateParametersSchema);
  147. }
  148. BrowsingContext.parseNavigateParams = parseNavigateParams;
  149. function parseReloadParams(params) {
  150. return parseObject(params, WebDriverBidi.BrowsingContext.ReloadParametersSchema);
  151. }
  152. BrowsingContext.parseReloadParams = parseReloadParams;
  153. function parseCreateParams(params) {
  154. return parseObject(params, WebDriverBidi.BrowsingContext.CreateParametersSchema);
  155. }
  156. BrowsingContext.parseCreateParams = parseCreateParams;
  157. function parseCloseParams(params) {
  158. return parseObject(params, WebDriverBidi.BrowsingContext.CloseParametersSchema);
  159. }
  160. BrowsingContext.parseCloseParams = parseCloseParams;
  161. function parseCaptureScreenshotParams(params) {
  162. return parseObject(params, WebDriverBidi.BrowsingContext.CaptureScreenshotParametersSchema);
  163. }
  164. BrowsingContext.parseCaptureScreenshotParams = parseCaptureScreenshotParams;
  165. function parsePrintParams(params) {
  166. return parseObject(params, WebDriverBidi.BrowsingContext.PrintParametersSchema);
  167. }
  168. BrowsingContext.parsePrintParams = parsePrintParams;
  169. function parseSetViewportParams(params) {
  170. return parseObject(params, WebDriverBidi.BrowsingContext.SetViewportParametersSchema);
  171. }
  172. BrowsingContext.parseSetViewportParams = parseSetViewportParams;
  173. function parseTraverseHistoryParams(params) {
  174. return parseObject(params, WebDriverBidi.BrowsingContext.TraverseHistoryParametersSchema);
  175. }
  176. BrowsingContext.parseTraverseHistoryParams = parseTraverseHistoryParams;
  177. function parseHandleUserPromptParameters(params) {
  178. return parseObject(params, WebDriverBidi.BrowsingContext.HandleUserPromptParametersSchema);
  179. }
  180. BrowsingContext.parseHandleUserPromptParameters = parseHandleUserPromptParameters;
  181. function parseLocateNodesParams(params) {
  182. // TODO: remove cast after https://github.com/google/cddlconv/issues/19 is fixed.
  183. return parseObject(params, WebDriverBidi.BrowsingContext.LocateNodesParametersSchema);
  184. }
  185. BrowsingContext.parseLocateNodesParams = parseLocateNodesParams;
  186. })(BrowsingContext || (exports.BrowsingContext = BrowsingContext = {}));
  187. /** @see https://w3c.github.io/webdriver-bidi/#module-session */
  188. var Session;
  189. (function (Session) {
  190. function parseSubscribeParams(params) {
  191. return parseObject(params, WebDriverBidi.Session.SubscriptionRequestSchema);
  192. }
  193. Session.parseSubscribeParams = parseSubscribeParams;
  194. })(Session || (exports.Session = Session = {}));
  195. var Input;
  196. (function (Input) {
  197. function parsePerformActionsParams(params) {
  198. return parseObject(params, WebDriverBidi.Input.PerformActionsParametersSchema);
  199. }
  200. Input.parsePerformActionsParams = parsePerformActionsParams;
  201. function parseReleaseActionsParams(params) {
  202. return parseObject(params, WebDriverBidi.Input.ReleaseActionsParametersSchema);
  203. }
  204. Input.parseReleaseActionsParams = parseReleaseActionsParams;
  205. function parseSetFilesParams(params) {
  206. return parseObject(params, WebDriverBidi.Input.SetFilesParametersSchema);
  207. }
  208. Input.parseSetFilesParams = parseSetFilesParams;
  209. })(Input || (exports.Input = Input = {}));
  210. var Storage;
  211. (function (Storage) {
  212. function parseGetCookiesParams(params) {
  213. // Work around of `cddlconv` https://github.com/google/cddlconv/issues/19.
  214. // The generated schema `SameSiteSchema` in `src/protocol-parser/webdriver-bidi.ts` is
  215. // of type `"none" | "strict" | "lax"` which is not assignable to generated enum
  216. // `SameSite` in `src/protocol/webdriver-bidi.ts`.
  217. // TODO: remove cast after https://github.com/google/cddlconv/issues/19 is fixed.
  218. return parseObject(params, WebDriverBidi.Storage.GetCookiesParametersSchema);
  219. }
  220. Storage.parseGetCookiesParams = parseGetCookiesParams;
  221. function parseSetCookieParams(params) {
  222. // Work around of `cddlconv` https://github.com/google/cddlconv/issues/19.
  223. // The generated schema `SameSiteSchema` in `src/protocol-parser/webdriver-bidi.ts` is
  224. // of type `"none" | "strict" | "lax"` which is not assignable to generated enum
  225. // `SameSite` in `src/protocol/webdriver-bidi.ts`.
  226. // TODO: remove cast after https://github.com/google/cddlconv/issues/19 is fixed.
  227. return parseObject(params, WebDriverBidi.Storage.SetCookieParametersSchema);
  228. }
  229. Storage.parseSetCookieParams = parseSetCookieParams;
  230. function parseDeleteCookiesParams(params) {
  231. // Work around of `cddlconv` https://github.com/google/cddlconv/issues/19.
  232. // The generated schema `SameSiteSchema` in `src/protocol-parser/webdriver-bidi.ts` is
  233. // of type `"none" | "strict" | "lax"` which is not assignable to generated enum
  234. // `SameSite` in `src/protocol/webdriver-bidi.ts`.
  235. // TODO: remove cast after https://github.com/google/cddlconv/issues/19 is fixed.
  236. return parseObject(params, WebDriverBidi.Storage.DeleteCookiesParametersSchema);
  237. }
  238. Storage.parseDeleteCookiesParams = parseDeleteCookiesParams;
  239. })(Storage || (exports.Storage = Storage = {}));
  240. var Cdp;
  241. (function (Cdp) {
  242. const SendCommandRequestSchema = zod_1.z.object({
  243. // Allowing any cdpMethod, and casting to proper type later on.
  244. method: zod_1.z.string(),
  245. // `passthrough` allows object to have any fields.
  246. // https://github.com/colinhacks/zod#passthrough
  247. params: zod_1.z.object({}).passthrough().optional(),
  248. session: zod_1.z.string().optional(),
  249. });
  250. const GetSessionRequestSchema = zod_1.z.object({
  251. context: WebDriverBidi.BrowsingContext.BrowsingContextSchema,
  252. });
  253. const ResolveRealmRequestSchema = zod_1.z.object({
  254. realm: WebDriverBidi.Script.RealmSchema,
  255. });
  256. function parseSendCommandRequest(params) {
  257. return parseObject(params, SendCommandRequestSchema);
  258. }
  259. Cdp.parseSendCommandRequest = parseSendCommandRequest;
  260. function parseGetSessionRequest(params) {
  261. return parseObject(params, GetSessionRequestSchema);
  262. }
  263. Cdp.parseGetSessionRequest = parseGetSessionRequest;
  264. function parseResolveRealmRequest(params) {
  265. return parseObject(params, ResolveRealmRequestSchema);
  266. }
  267. Cdp.parseResolveRealmRequest = parseResolveRealmRequest;
  268. })(Cdp || (exports.Cdp = Cdp = {}));
  269. var Permissions;
  270. (function (Permissions) {
  271. function parseSetPermissionsParams(params) {
  272. return {
  273. // TODO: remove once "goog:" attributes are not needed.
  274. ...params,
  275. ...parseObject(params, WebDriverBidiPermissions.Permissions.SetPermissionParametersSchema),
  276. };
  277. }
  278. Permissions.parseSetPermissionsParams = parseSetPermissionsParams;
  279. })(Permissions || (exports.Permissions = Permissions = {}));
  280. //# sourceMappingURL=protocol-parser.js.map