protocol-parser.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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. Object.defineProperty(exports, "__esModule", { value: true });
  19. exports.Input = exports.Session = exports.Cdp = exports.BrowsingContext = exports.Script = exports.CommonDataTypes = exports.parseObject = void 0;
  20. /**
  21. * @fileoverview Provides parsing and validator for WebDriver BiDi protocol.
  22. * Parser types should match the `../protocol` types.
  23. */
  24. const zod_1 = require("zod");
  25. const protocol_js_1 = require("../protocol/protocol.js");
  26. const MAX_INT = 9007199254740991;
  27. function parseObject(obj, schema) {
  28. const parseResult = schema.safeParse(obj);
  29. if (parseResult.success) {
  30. return parseResult.data;
  31. }
  32. const errorMessage = parseResult.error.errors
  33. .map((e) => `${e.message} in ` +
  34. `${e.path.map((p) => JSON.stringify(p)).join('/')}.`)
  35. .join(' ');
  36. throw new protocol_js_1.Message.InvalidArgumentException(errorMessage);
  37. }
  38. exports.parseObject = parseObject;
  39. const UnicodeCharacterSchema = zod_1.z.string().refine((value) => {
  40. // The spread is a little hack so JS gives us an array of unicode characters
  41. // to measure.
  42. return [...value].length === 1;
  43. });
  44. var CommonDataTypes;
  45. (function (CommonDataTypes) {
  46. CommonDataTypes.SharedReferenceSchema = zod_1.z.object({
  47. sharedId: zod_1.z.string().min(1),
  48. handle: zod_1.z.string().optional(),
  49. });
  50. CommonDataTypes.RemoteReferenceSchema = zod_1.z.object({
  51. handle: zod_1.z.string().min(1),
  52. });
  53. // UndefinedValue = {
  54. // type: "undefined",
  55. // }
  56. const UndefinedValueSchema = zod_1.z.object({ type: zod_1.z.literal('undefined') });
  57. // NullValue = {
  58. // type: "null",
  59. // }
  60. const NullValueSchema = zod_1.z.object({ type: zod_1.z.literal('null') });
  61. // StringValue = {
  62. // type: "string",
  63. // value: text,
  64. // }
  65. const StringValueSchema = zod_1.z.object({
  66. type: zod_1.z.literal('string'),
  67. value: zod_1.z.string(),
  68. });
  69. // SpecialNumber = "NaN" / "-0" / "Infinity" / "-Infinity";
  70. const SpecialNumberSchema = zod_1.z.enum(['NaN', '-0', 'Infinity', '-Infinity']);
  71. // NumberValue = {
  72. // type: "number",
  73. // value: number / SpecialNumber,
  74. // }
  75. const NumberValueSchema = zod_1.z.object({
  76. type: zod_1.z.literal('number'),
  77. value: zod_1.z.union([SpecialNumberSchema, zod_1.z.number()]),
  78. });
  79. // BooleanValue = {
  80. // type: "boolean",
  81. // value: bool,
  82. // }
  83. const BooleanValueSchema = zod_1.z.object({
  84. type: zod_1.z.literal('boolean'),
  85. value: zod_1.z.boolean(),
  86. });
  87. // BigIntValue = {
  88. // type: "bigint",
  89. // value: text,
  90. // }
  91. const BigIntValueSchema = zod_1.z.object({
  92. type: zod_1.z.literal('bigint'),
  93. value: zod_1.z.string(),
  94. });
  95. const PrimitiveProtocolValueSchema = zod_1.z.union([
  96. UndefinedValueSchema,
  97. NullValueSchema,
  98. StringValueSchema,
  99. NumberValueSchema,
  100. BooleanValueSchema,
  101. BigIntValueSchema,
  102. ]);
  103. CommonDataTypes.LocalValueSchema = zod_1.z.lazy(() => zod_1.z.union([
  104. PrimitiveProtocolValueSchema,
  105. ArrayLocalValueSchema,
  106. DateLocalValueSchema,
  107. MapLocalValueSchema,
  108. ObjectLocalValueSchema,
  109. RegExpLocalValueSchema,
  110. SetLocalValueSchema,
  111. ]));
  112. // Order is important, as `parse` is processed in the same order.
  113. // `SharedReferenceSchema`->`RemoteReferenceSchema`->`LocalValueSchema`.
  114. const LocalOrRemoteValueSchema = zod_1.z.union([
  115. CommonDataTypes.SharedReferenceSchema,
  116. CommonDataTypes.RemoteReferenceSchema,
  117. CommonDataTypes.LocalValueSchema,
  118. ]);
  119. // ListLocalValue = [*LocalValue];
  120. const ListLocalValueSchema = zod_1.z.array(LocalOrRemoteValueSchema);
  121. // ArrayLocalValue = {
  122. // type: "array",
  123. // value: ListLocalValue,
  124. // }
  125. const ArrayLocalValueSchema = zod_1.z.object({
  126. type: zod_1.z.literal('array'),
  127. value: ListLocalValueSchema,
  128. });
  129. // DateLocalValue = {
  130. // type: "date",
  131. // value: text
  132. // }
  133. const DateLocalValueSchema = zod_1.z.object({
  134. type: zod_1.z.literal('date'),
  135. value: zod_1.z.string().min(1),
  136. });
  137. // MappingLocalValue = [*[(LocalValue / text), LocalValue]];
  138. const MappingLocalValueSchema = zod_1.z.tuple([
  139. zod_1.z.union([zod_1.z.string(), LocalOrRemoteValueSchema]),
  140. LocalOrRemoteValueSchema,
  141. ]);
  142. // MapLocalValue = {
  143. // type: "map",
  144. // value: MappingLocalValue,
  145. // }
  146. const MapLocalValueSchema = zod_1.z.object({
  147. type: zod_1.z.literal('map'),
  148. value: zod_1.z.array(MappingLocalValueSchema),
  149. });
  150. // ObjectLocalValue = {
  151. // type: "object",
  152. // value: MappingLocalValue,
  153. // }
  154. const ObjectLocalValueSchema = zod_1.z.object({
  155. type: zod_1.z.literal('object'),
  156. value: zod_1.z.array(MappingLocalValueSchema),
  157. });
  158. // RegExpLocalValue = {
  159. // type: "regexp",
  160. // value: RegExpValue,
  161. // }
  162. const RegExpLocalValueSchema = zod_1.z.object({
  163. type: zod_1.z.literal('regexp'),
  164. value: zod_1.z.object({
  165. pattern: zod_1.z.string(),
  166. flags: zod_1.z.string().optional(),
  167. }),
  168. });
  169. // SetLocalValue = {
  170. // type: "set",
  171. // value: ListLocalValue,
  172. // }
  173. const SetLocalValueSchema = zod_1.z.lazy(() => zod_1.z.object({
  174. type: zod_1.z.literal('set'),
  175. value: ListLocalValueSchema,
  176. }));
  177. // BrowsingContext = text;
  178. CommonDataTypes.BrowsingContextSchema = zod_1.z.string();
  179. CommonDataTypes.MaxDepthSchema = zod_1.z.number().int().nonnegative().max(MAX_INT);
  180. })(CommonDataTypes = exports.CommonDataTypes || (exports.CommonDataTypes = {}));
  181. /** @see https://w3c.github.io/webdriver-bidi/#module-script */
  182. var Script;
  183. (function (Script) {
  184. const RealmTypeSchema = zod_1.z.enum([
  185. 'window',
  186. 'dedicated-worker',
  187. 'shared-worker',
  188. 'service-worker',
  189. 'worker',
  190. 'paint-worklet',
  191. 'audio-worklet',
  192. 'worklet',
  193. ]);
  194. Script.GetRealmsParametersSchema = zod_1.z.object({
  195. context: CommonDataTypes.BrowsingContextSchema.optional(),
  196. type: RealmTypeSchema.optional(),
  197. });
  198. function parseGetRealmsParams(params) {
  199. return parseObject(params, Script.GetRealmsParametersSchema);
  200. }
  201. Script.parseGetRealmsParams = parseGetRealmsParams;
  202. // ContextTarget = {
  203. // context: BrowsingContext,
  204. // ?sandbox: text
  205. // }
  206. const ContextTargetSchema = zod_1.z.object({
  207. context: CommonDataTypes.BrowsingContextSchema,
  208. sandbox: zod_1.z.string().optional(),
  209. });
  210. // RealmTarget = {realm: Realm};
  211. const RealmTargetSchema = zod_1.z.object({
  212. realm: zod_1.z.string().min(1),
  213. });
  214. // Target = (
  215. // RealmTarget //
  216. // ContextTarget
  217. // );
  218. // Order is important, as `parse` is processed in the same order.
  219. // `RealmTargetSchema` has higher priority.
  220. const TargetSchema = zod_1.z.union([RealmTargetSchema, ContextTargetSchema]);
  221. // ResultOwnership = "root" / "none"
  222. const ResultOwnershipSchema = zod_1.z.enum(['root', 'none']);
  223. // SerializationOptions = {
  224. // ?maxDomDepth: (js-uint / null) .default 0,
  225. // ?maxObjectDepth: (js-uint / null) .default null,
  226. // ?includeShadowTree: ("none" / "open" / "all") .default "none",
  227. // }
  228. const SerializationOptionsSchema = zod_1.z.object({
  229. maxDomDepth: zod_1.z
  230. .union([zod_1.z.null(), zod_1.z.number().int().nonnegative()])
  231. .optional(),
  232. maxObjectDepth: zod_1.z
  233. .union([zod_1.z.null(), zod_1.z.number().int().nonnegative().max(MAX_INT)])
  234. .optional(),
  235. includeShadowTree: zod_1.z.enum(['none', 'open', 'all']).optional(),
  236. });
  237. // script.EvaluateParameters = {
  238. // expression: text,
  239. // target: script.Target,
  240. // awaitPromise: bool,
  241. // ?resultOwnership: script.ResultOwnership,
  242. // ?serializationOptions: script.SerializationOptions,
  243. // }
  244. const EvaluateParametersSchema = zod_1.z.object({
  245. expression: zod_1.z.string(),
  246. awaitPromise: zod_1.z.boolean(),
  247. target: TargetSchema,
  248. resultOwnership: ResultOwnershipSchema.optional(),
  249. serializationOptions: SerializationOptionsSchema.optional(),
  250. });
  251. function parseEvaluateParams(params) {
  252. return parseObject(params, EvaluateParametersSchema);
  253. }
  254. Script.parseEvaluateParams = parseEvaluateParams;
  255. // DisownParameters = {
  256. // handles: [Handle]
  257. // target: script.Target;
  258. // }
  259. const DisownParametersSchema = zod_1.z.object({
  260. target: TargetSchema,
  261. handles: zod_1.z.array(zod_1.z.string()),
  262. });
  263. function parseDisownParams(params) {
  264. return parseObject(params, DisownParametersSchema);
  265. }
  266. Script.parseDisownParams = parseDisownParams;
  267. const ChannelSchema = zod_1.z.string();
  268. const ChannelPropertiesSchema = zod_1.z.object({
  269. channel: ChannelSchema,
  270. serializationOptions: SerializationOptionsSchema.optional(),
  271. ownership: ResultOwnershipSchema.optional(),
  272. });
  273. Script.ChannelValueSchema = zod_1.z.object({
  274. type: zod_1.z.literal('channel'),
  275. value: ChannelPropertiesSchema,
  276. });
  277. Script.PreloadScriptSchema = zod_1.z.string();
  278. Script.AddPreloadScriptParametersSchema = zod_1.z.object({
  279. functionDeclaration: zod_1.z.string(),
  280. arguments: zod_1.z.array(Script.ChannelValueSchema).optional(),
  281. sandbox: zod_1.z.string().optional(),
  282. context: CommonDataTypes.BrowsingContextSchema.optional(),
  283. });
  284. function parseAddPreloadScriptParams(params) {
  285. return parseObject(params, Script.AddPreloadScriptParametersSchema);
  286. }
  287. Script.parseAddPreloadScriptParams = parseAddPreloadScriptParams;
  288. Script.RemovePreloadScriptParametersSchema = zod_1.z.object({
  289. script: Script.PreloadScriptSchema,
  290. });
  291. function parseRemovePreloadScriptParams(params) {
  292. return parseObject(params, Script.RemovePreloadScriptParametersSchema);
  293. }
  294. Script.parseRemovePreloadScriptParams = parseRemovePreloadScriptParams;
  295. // ArgumentValue = (
  296. // RemoteReference //
  297. // LocalValue //
  298. // script.Channel
  299. // );
  300. const ArgumentValueSchema = zod_1.z.union([
  301. CommonDataTypes.RemoteReferenceSchema,
  302. CommonDataTypes.SharedReferenceSchema,
  303. CommonDataTypes.LocalValueSchema,
  304. Script.ChannelValueSchema,
  305. ]);
  306. // CallFunctionParameters = {
  307. // functionDeclaration: text,
  308. // awaitPromise: bool,
  309. // target: script.Target,
  310. // ?arguments: [*script.ArgumentValue],
  311. // ?resultOwnership: script.ResultOwnership,
  312. // ?serializationOptions: script.SerializationOptions,
  313. // ?this: script.ArgumentValue,
  314. // }
  315. const CallFunctionParametersSchema = zod_1.z.object({
  316. functionDeclaration: zod_1.z.string(),
  317. awaitPromise: zod_1.z.boolean(),
  318. target: TargetSchema,
  319. arguments: zod_1.z.array(ArgumentValueSchema).optional(),
  320. resultOwnership: ResultOwnershipSchema.optional(),
  321. serializationOptions: SerializationOptionsSchema.optional(),
  322. this: ArgumentValueSchema.optional(),
  323. });
  324. function parseCallFunctionParams(params) {
  325. return parseObject(params, CallFunctionParametersSchema);
  326. }
  327. Script.parseCallFunctionParams = parseCallFunctionParams;
  328. })(Script = exports.Script || (exports.Script = {}));
  329. /** @see https://w3c.github.io/webdriver-bidi/#module-browsingContext */
  330. var BrowsingContext;
  331. (function (BrowsingContext) {
  332. // GetTreeParameters = {
  333. // ?maxDepth: js-uint,
  334. // ?root: browsingContext.BrowsingContext,
  335. // }
  336. const GetTreeParametersSchema = zod_1.z.object({
  337. maxDepth: CommonDataTypes.MaxDepthSchema.optional(),
  338. root: CommonDataTypes.BrowsingContextSchema.optional(),
  339. });
  340. function parseGetTreeParams(params) {
  341. return parseObject(params, GetTreeParametersSchema);
  342. }
  343. BrowsingContext.parseGetTreeParams = parseGetTreeParams;
  344. // ReadinessState = "none" / "interactive" / "complete"
  345. const ReadinessStateSchema = zod_1.z.enum(['none', 'interactive', 'complete']);
  346. // BrowsingContextNavigateParameters = {
  347. // context: BrowsingContext,
  348. // url: text,
  349. // ?wait: ReadinessState,
  350. // }
  351. // ReadinessState = "none" / "interactive" / "complete"
  352. const NavigateParametersSchema = zod_1.z.object({
  353. context: CommonDataTypes.BrowsingContextSchema,
  354. url: zod_1.z.string().url(),
  355. wait: ReadinessStateSchema.optional(),
  356. });
  357. function parseNavigateParams(params) {
  358. return parseObject(params, NavigateParametersSchema);
  359. }
  360. BrowsingContext.parseNavigateParams = parseNavigateParams;
  361. const ReloadParametersSchema = zod_1.z.object({
  362. context: CommonDataTypes.BrowsingContextSchema,
  363. ignoreCache: zod_1.z.boolean().optional(),
  364. wait: ReadinessStateSchema.optional(),
  365. });
  366. function parseReloadParams(params) {
  367. return parseObject(params, ReloadParametersSchema);
  368. }
  369. BrowsingContext.parseReloadParams = parseReloadParams;
  370. // BrowsingContextCreateType = "tab" / "window"
  371. // BrowsingContextCreateParameters = {
  372. // type: BrowsingContextCreateType
  373. // }
  374. const CreateParametersSchema = zod_1.z.object({
  375. type: zod_1.z.enum(['tab', 'window']),
  376. referenceContext: CommonDataTypes.BrowsingContextSchema.optional(),
  377. });
  378. function parseCreateParams(params) {
  379. return parseObject(params, CreateParametersSchema);
  380. }
  381. BrowsingContext.parseCreateParams = parseCreateParams;
  382. // BrowsingContextCloseParameters = {
  383. // context: BrowsingContext
  384. // }
  385. const CloseParametersSchema = zod_1.z.object({
  386. context: CommonDataTypes.BrowsingContextSchema,
  387. });
  388. function parseCloseParams(params) {
  389. return parseObject(params, CloseParametersSchema);
  390. }
  391. BrowsingContext.parseCloseParams = parseCloseParams;
  392. // browsingContext.CaptureScreenshotParameters = {
  393. // context: browsingContext.BrowsingContext
  394. // }
  395. const CaptureScreenshotParametersSchema = zod_1.z.object({
  396. context: CommonDataTypes.BrowsingContextSchema,
  397. });
  398. function parseCaptureScreenshotParams(params) {
  399. return parseObject(params, CaptureScreenshotParametersSchema);
  400. }
  401. BrowsingContext.parseCaptureScreenshotParams = parseCaptureScreenshotParams;
  402. // All units are in cm.
  403. // PrintPageParameters = {
  404. // ?height: (float .ge 0.0) .default 27.94,
  405. // ?width: (float .ge 0.0) .default 21.59,
  406. // }
  407. const PrintPageParametersSchema = zod_1.z.object({
  408. height: zod_1.z.number().nonnegative().optional(),
  409. width: zod_1.z.number().nonnegative().optional(),
  410. });
  411. // All units are in cm.
  412. // PrintMarginParameters = {
  413. // ?bottom: (float .ge 0.0) .default 1.0,
  414. // ?left: (float .ge 0.0) .default 1.0,
  415. // ?right: (float .ge 0.0) .default 1.0,
  416. // ?top: (float .ge 0.0) .default 1.0,
  417. // }
  418. const PrintMarginParametersSchema = zod_1.z.object({
  419. bottom: zod_1.z.number().nonnegative().optional(),
  420. left: zod_1.z.number().nonnegative().optional(),
  421. right: zod_1.z.number().nonnegative().optional(),
  422. top: zod_1.z.number().nonnegative().optional(),
  423. });
  424. /** @see https://w3c.github.io/webdriver/#dfn-parse-a-page-range */
  425. const PrintPageRangesSchema = zod_1.z
  426. .array(zod_1.z.union([zod_1.z.string().min(1), zod_1.z.number().int().nonnegative()]))
  427. .refine((pageRanges) => {
  428. return pageRanges.every((pageRange) => {
  429. const match = String(pageRange).match(
  430. // matches: '2' | '2-' | '-2' | '2-4'
  431. /^(?:(?:\d+)|(?:\d+[-])|(?:[-]\d+)|(?:(?<start>\d+)[-](?<end>\d+)))$/);
  432. // If a page range is specified, validate start <= end.
  433. const { start, end } = match?.groups ?? {};
  434. if (start && end && Number(start) > Number(end)) {
  435. return false;
  436. }
  437. return match;
  438. });
  439. });
  440. // PrintParameters = {
  441. // context: browsingContext.BrowsingContext,
  442. // ?background: bool .default false,
  443. // ?margin: browsingContext.PrintMarginParameters,
  444. // ?orientation: ("portrait" / "landscape") .default "portrait",
  445. // ?page: browsingContext.PrintPageParameters,
  446. // ?pageRanges: [*(js-uint / text)],
  447. // ?scale: 0.1..2.0 .default 1.0,
  448. // ?shrinkToFit: bool .default true,
  449. // }
  450. const PrintParametersSchema = zod_1.z.object({
  451. context: CommonDataTypes.BrowsingContextSchema,
  452. background: zod_1.z.boolean().optional(),
  453. margin: PrintMarginParametersSchema.optional(),
  454. orientation: zod_1.z.enum(['portrait', 'landscape']).optional(),
  455. page: PrintPageParametersSchema.optional(),
  456. pageRanges: PrintPageRangesSchema.optional(),
  457. scale: zod_1.z.number().min(0.1).max(2.0).optional(),
  458. shrinkToFit: zod_1.z.boolean().optional(),
  459. });
  460. function parsePrintParams(params) {
  461. return parseObject(params, PrintParametersSchema);
  462. }
  463. BrowsingContext.parsePrintParams = parsePrintParams;
  464. // browsingContext.Viewport = {
  465. // width: js-uint,
  466. // height: js-uint,
  467. // }
  468. const ViewportSchema = zod_1.z.object({
  469. width: zod_1.z.number().int().nonnegative(),
  470. height: zod_1.z.number().int().nonnegative(),
  471. });
  472. // browsingContext.SetViewportParameters = {
  473. // context: browsingContext.BrowsingContext,
  474. // viewport: emulation.Viewport / null
  475. // }
  476. const SetViewportActionSchema = zod_1.z.object({
  477. context: CommonDataTypes.BrowsingContextSchema,
  478. viewport: zod_1.z.union([zod_1.z.null(), ViewportSchema]),
  479. });
  480. function parseSetViewportParams(params) {
  481. return parseObject(params, SetViewportActionSchema);
  482. }
  483. BrowsingContext.parseSetViewportParams = parseSetViewportParams;
  484. })(BrowsingContext = exports.BrowsingContext || (exports.BrowsingContext = {}));
  485. var Cdp;
  486. (function (Cdp) {
  487. const SendCommandParamsSchema = zod_1.z.object({
  488. // Allowing any cdpMethod, and casting to proper type later on.
  489. method: zod_1.z.string(),
  490. // `passthrough` allows object to have any fields.
  491. // https://github.com/colinhacks/zod#passthrough
  492. params: zod_1.z.object({}).passthrough().optional(),
  493. session: zod_1.z.string().optional(),
  494. });
  495. function parseSendCommandParams(params) {
  496. return parseObject(params, SendCommandParamsSchema);
  497. }
  498. Cdp.parseSendCommandParams = parseSendCommandParams;
  499. const GetSessionParamsSchema = zod_1.z.object({
  500. context: CommonDataTypes.BrowsingContextSchema,
  501. });
  502. function parseGetSessionParams(params) {
  503. return parseObject(params, GetSessionParamsSchema);
  504. }
  505. Cdp.parseGetSessionParams = parseGetSessionParams;
  506. })(Cdp = exports.Cdp || (exports.Cdp = {}));
  507. /** @see https://w3c.github.io/webdriver-bidi/#module-session */
  508. var Session;
  509. (function (Session) {
  510. const BiDiSubscriptionRequestParametersEventsSchema = zod_1.z.enum([
  511. protocol_js_1.BrowsingContext.AllEvents,
  512. ...Object.values(protocol_js_1.BrowsingContext.EventNames),
  513. protocol_js_1.Log.AllEvents,
  514. ...Object.values(protocol_js_1.Log.EventNames),
  515. protocol_js_1.Network.AllEvents,
  516. ...Object.values(protocol_js_1.Network.EventNames),
  517. protocol_js_1.Script.AllEvents,
  518. ...Object.values(protocol_js_1.Script.EventNames),
  519. ]);
  520. // BiDi+ events
  521. const CdpSubscriptionRequestParametersEventsSchema = zod_1.z.custom((value) => {
  522. return typeof value === 'string' && value.startsWith('cdp.');
  523. }, 'Not a CDP event');
  524. const SubscriptionRequestParametersEventsSchema = zod_1.z.union([
  525. BiDiSubscriptionRequestParametersEventsSchema,
  526. CdpSubscriptionRequestParametersEventsSchema,
  527. ]);
  528. // SessionSubscriptionRequest = {
  529. // events: [*text],
  530. // ?contexts: [*BrowsingContext],
  531. // }
  532. const SubscriptionRequestParametersSchema = zod_1.z.object({
  533. events: zod_1.z.array(SubscriptionRequestParametersEventsSchema),
  534. contexts: zod_1.z.array(CommonDataTypes.BrowsingContextSchema).optional(),
  535. });
  536. function parseSubscribeParams(params) {
  537. return parseObject(params, SubscriptionRequestParametersSchema);
  538. }
  539. Session.parseSubscribeParams = parseSubscribeParams;
  540. })(Session = exports.Session || (exports.Session = {}));
  541. /** @see https://w3c.github.io/webdriver-bidi/#module-input */
  542. var Input;
  543. (function (Input) {
  544. // input.ElementOrigin = {
  545. // type: "element",
  546. // element: script.SharedReference
  547. // }
  548. const ElementOriginSchema = zod_1.z.object({
  549. type: zod_1.z.literal('element'),
  550. element: CommonDataTypes.SharedReferenceSchema,
  551. });
  552. // input.Origin = "viewport" / "pointer" / input.ElementOrigin
  553. const OriginSchema = zod_1.z.union([
  554. zod_1.z.literal('viewport'),
  555. zod_1.z.literal('pointer'),
  556. ElementOriginSchema,
  557. ]);
  558. // input.PauseAction = {
  559. // type: "pause",
  560. // ? duration: js-uint
  561. // }
  562. const PauseActionSchema = zod_1.z.object({
  563. type: zod_1.z.literal(protocol_js_1.Input.ActionType.Pause),
  564. duration: zod_1.z.number().nonnegative().int().optional(),
  565. });
  566. // input.KeyDownAction = {
  567. // type: "keyDown",
  568. // value: text
  569. // }
  570. const KeyDownActionSchema = zod_1.z.object({
  571. type: zod_1.z.literal(protocol_js_1.Input.ActionType.KeyDown),
  572. value: UnicodeCharacterSchema,
  573. });
  574. // input.KeyUpAction = {
  575. // type: "keyUp",
  576. // value: text
  577. // }
  578. const KeyUpActionSchema = zod_1.z.object({
  579. type: zod_1.z.literal(protocol_js_1.Input.ActionType.KeyUp),
  580. value: UnicodeCharacterSchema,
  581. });
  582. // input.TiltProperties = (
  583. // ? tiltX: -90..90 .default 0,
  584. // ? tiltY: -90..90 .default 0,
  585. // )
  586. const TiltPropertiesSchema = zod_1.z.object({
  587. tiltX: zod_1.z.number().min(-90).max(90).int().default(0).optional(),
  588. tiltY: zod_1.z.number().min(-90).max(90).int().default(0).optional(),
  589. });
  590. // input.AngleProperties = (
  591. // ? altitudeAngle: float .default 0.0,
  592. // ? azimuthAngle: float .default 0.0,
  593. // )
  594. const AnglePropertiesSchema = zod_1.z.object({
  595. altitudeAngle: zod_1.z
  596. .number()
  597. .nonnegative()
  598. .max(Math.PI / 2)
  599. .default(0.0)
  600. .optional(),
  601. azimuthAngle: zod_1.z
  602. .number()
  603. .nonnegative()
  604. .max(2 * Math.PI)
  605. .default(0.0)
  606. .optional(),
  607. });
  608. // input.PointerCommonProperties = (
  609. // ? width: js-uint .default 1,
  610. // ? height: js-uint .default 1,
  611. // ? pressure: float .default 0.0,
  612. // ? tangentialPressure: float .default 0.0,
  613. // ? twist: 0..359 .default 0,
  614. // (input.TiltProperties // input.AngleProperties)
  615. // )
  616. const PointerCommonPropertiesSchema = zod_1.z
  617. .object({
  618. width: zod_1.z.number().nonnegative().int().default(1),
  619. height: zod_1.z.number().nonnegative().int().default(1),
  620. pressure: zod_1.z.number().min(0.0).max(1.0).default(0.0),
  621. tangentialPressure: zod_1.z.number().min(-1.0).max(1.0).default(0.0),
  622. twist: zod_1.z.number().nonnegative().max(359).int().default(0),
  623. })
  624. .and(zod_1.z.union([TiltPropertiesSchema, AnglePropertiesSchema]));
  625. // input.PointerUpAction = {
  626. // type: "pointerUp",
  627. // button: js-uint,
  628. // input.PointerCommonProperties
  629. // }
  630. const PointerUpActionSchema = zod_1.z
  631. .object({
  632. type: zod_1.z.literal(protocol_js_1.Input.ActionType.PointerUp),
  633. button: zod_1.z.number().nonnegative().int(),
  634. })
  635. .and(PointerCommonPropertiesSchema);
  636. // input.PointerDownAction = {
  637. // type: "pointerDown",
  638. // button: js-uint,
  639. // input.PointerCommonProperties
  640. // }
  641. const PointerDownActionSchema = zod_1.z
  642. .object({
  643. type: zod_1.z.literal(protocol_js_1.Input.ActionType.PointerDown),
  644. button: zod_1.z.number().nonnegative().int(),
  645. })
  646. .and(PointerCommonPropertiesSchema);
  647. // input.PointerMoveAction = {
  648. // type: "pointerMove",
  649. // x: js-int,
  650. // y: js-int,
  651. // ? duration: js-uint,
  652. // ? origin: input.Origin,
  653. // input.PointerCommonProperties
  654. // }
  655. const PointerMoveActionSchema = zod_1.z
  656. .object({
  657. type: zod_1.z.literal(protocol_js_1.Input.ActionType.PointerMove),
  658. x: zod_1.z.number().int(),
  659. y: zod_1.z.number().int(),
  660. duration: zod_1.z.number().nonnegative().int().optional(),
  661. origin: OriginSchema.optional().default('viewport'),
  662. })
  663. .and(PointerCommonPropertiesSchema);
  664. // input.WheelScrollAction = {
  665. // type: "scroll",
  666. // x: js-int,
  667. // y: js-int,
  668. // deltaX: js-int,
  669. // deltaY: js-int,
  670. // ? duration: js-uint,
  671. // ? origin: input.Origin .default "viewport",
  672. // }
  673. const WheelScrollActionSchema = zod_1.z.object({
  674. type: zod_1.z.literal(protocol_js_1.Input.ActionType.Scroll),
  675. x: zod_1.z.number().int(),
  676. y: zod_1.z.number().int(),
  677. deltaX: zod_1.z.number().int(),
  678. deltaY: zod_1.z.number().int(),
  679. duration: zod_1.z.number().nonnegative().int().optional(),
  680. origin: OriginSchema.optional().default('viewport'),
  681. });
  682. // input.WheelSourceAction = (
  683. // input.PauseAction //
  684. // input.WheelScrollAction
  685. // )
  686. const WheelSourceActionSchema = zod_1.z.discriminatedUnion('type', [
  687. PauseActionSchema,
  688. WheelScrollActionSchema,
  689. ]);
  690. // input.WheelSourceActions = {
  691. // type: "wheel",
  692. // id: text,
  693. // actions: [*input.WheelSourceAction]
  694. // }
  695. const WheelSourceActionsSchema = zod_1.z.object({
  696. type: zod_1.z.literal(protocol_js_1.Input.SourceActionsType.Wheel),
  697. id: zod_1.z.string(),
  698. actions: zod_1.z.array(WheelSourceActionSchema),
  699. });
  700. // input.PointerSourceAction = (
  701. // input.PauseAction //
  702. // input.PointerDownAction //
  703. // input.PointerUpAction //
  704. // input.PointerMoveAction
  705. // )
  706. const PointerSourceActionSchema = zod_1.z.union([
  707. PauseActionSchema,
  708. PointerDownActionSchema,
  709. PointerUpActionSchema,
  710. PointerMoveActionSchema,
  711. ]);
  712. // input.PointerType = "mouse" / "pen" / "touch"
  713. const PointerTypeSchema = zod_1.z.nativeEnum(protocol_js_1.Input.PointerType);
  714. // input.PointerParameters = {
  715. // ? pointerType: input.PointerType .default "mouse"
  716. // }
  717. const PointerParametersSchema = zod_1.z.object({
  718. pointerType: PointerTypeSchema.optional().default(protocol_js_1.Input.PointerType.Mouse),
  719. });
  720. // input.PointerSourceActions = {
  721. // type: "pointer",
  722. // id: text,
  723. // ? parameters: input.PointerParameters,
  724. // actions: [*input.PointerSourceAction]
  725. // }
  726. const PointerSourceActionsSchema = zod_1.z.object({
  727. type: zod_1.z.literal(protocol_js_1.Input.SourceActionsType.Pointer),
  728. id: zod_1.z.string(),
  729. parameters: PointerParametersSchema.optional(),
  730. actions: zod_1.z.array(PointerSourceActionSchema),
  731. });
  732. // input.KeySourceAction = (
  733. // input.PauseAction //
  734. // input.KeyDownAction //
  735. // input.KeyUpAction
  736. // )
  737. const KeySourceActionSchema = zod_1.z.discriminatedUnion('type', [
  738. PauseActionSchema,
  739. KeyDownActionSchema,
  740. KeyUpActionSchema,
  741. ]);
  742. // input.KeySourceActions = {
  743. // type: "key",
  744. // id: text,
  745. // actions: [*input.KeySourceAction]
  746. // }
  747. const KeySourceActionsSchema = zod_1.z.object({
  748. type: zod_1.z.literal(protocol_js_1.Input.SourceActionsType.Key),
  749. id: zod_1.z.string(),
  750. actions: zod_1.z.array(KeySourceActionSchema),
  751. });
  752. // input.NoneSourceAction = input.PauseAction
  753. const NoneSourceActionSchema = PauseActionSchema;
  754. // input.NoneSourceActions = {
  755. // type: "none",
  756. // id: text,
  757. // actions: [*input.NoneSourceAction]
  758. // }
  759. const NoneSourceActionsSchema = zod_1.z.object({
  760. type: zod_1.z.literal(protocol_js_1.Input.SourceActionsType.None),
  761. id: zod_1.z.string(),
  762. actions: zod_1.z.array(NoneSourceActionSchema),
  763. });
  764. // input.SourceActions = (
  765. // input.NoneSourceActions //
  766. // input.KeySourceActions //
  767. // input.PointerSourceActions //
  768. // input.WheelSourceActions
  769. // )
  770. const SourceActionsSchema = zod_1.z.discriminatedUnion('type', [
  771. NoneSourceActionsSchema,
  772. KeySourceActionsSchema,
  773. PointerSourceActionsSchema,
  774. WheelSourceActionsSchema,
  775. ]);
  776. // input.PerformActionsParameters = {
  777. // context: browsingContext.BrowsingContext,
  778. // actions: [*input.SourceActions]
  779. // }
  780. const PerformActionsParametersSchema = zod_1.z.object({
  781. context: CommonDataTypes.BrowsingContextSchema,
  782. actions: zod_1.z.array(SourceActionsSchema),
  783. });
  784. function parsePerformActionsParams(params) {
  785. return parseObject(params, PerformActionsParametersSchema);
  786. }
  787. Input.parsePerformActionsParams = parsePerformActionsParams;
  788. // input.ReleaseActionsParameters = {
  789. // context: browsingContext.BrowsingContext,
  790. // }
  791. const ReleaseActionsParametersSchema = zod_1.z.object({
  792. context: CommonDataTypes.BrowsingContextSchema,
  793. });
  794. function parseReleaseActionsParams(params) {
  795. return parseObject(params, ReleaseActionsParametersSchema);
  796. }
  797. Input.parseReleaseActionsParams = parseReleaseActionsParams;
  798. })(Input = exports.Input || (exports.Input = {}));
  799. //# sourceMappingURL=protocol-parser.js.map