UNPKG

5.24 kBJavaScriptView Raw
1/**
2 * Copyright 2018 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16const EventEmitter = require('events');
17const {helper, debugError} = require('./helper');
18const {ExecutionContext, JSHandle} = require('./ExecutionContext');
19
20class Worker extends EventEmitter {
21 /**
22 * @param {Puppeteer.CDPSession} client
23 * @param {string} url
24 * @param {function(!string, !Array<!JSHandle>)} consoleAPICalled
25 * @param {function(!Protocol.Runtime.ExceptionDetails)} exceptionThrown
26 */
27 constructor(client, url, consoleAPICalled, exceptionThrown) {
28 super();
29 this._client = client;
30 this._url = url;
31 this._executionContextPromise = new Promise(x => this._executionContextCallback = x);
32 /** @type {function(!Protocol.Runtime.RemoteObject):!JSHandle} */
33 let jsHandleFactory;
34 this._client.once('Runtime.executionContextCreated', /* async */ event => {return (fn => {
35 const gen = fn.call(this);
36 return new Promise((resolve, reject) => {
37 function step(key, arg) {
38 let info, value;
39 try {
40 info = gen[key](arg);
41 value = info.value;
42 } catch (error) {
43 reject(error);
44 return;
45 }
46 if (info.done) {
47 resolve(value);
48 } else {
49 return Promise.resolve(value).then(
50 value => {
51 step('next', value);
52 },
53 err => {
54 step('throw', err);
55 });
56 }
57 }
58 return step('next');
59 });
60})(function*(){
61 jsHandleFactory = remoteObject => new JSHandle(executionContext, client, remoteObject);
62 const executionContext = new ExecutionContext(client, event.context, jsHandleFactory, null);
63 this._executionContextCallback(executionContext);
64 });});
65 // This might fail if the target is closed before we recieve all execution contexts.
66 this._client.send('Runtime.enable', {}).catch(debugError);
67
68 this._client.on('Runtime.consoleAPICalled', event => consoleAPICalled(event.type, event.args.map(jsHandleFactory)));
69 this._client.on('Runtime.exceptionThrown', exception => exceptionThrown(exception.exceptionDetails));
70 }
71
72 /**
73 * @return {string}
74 */
75 url() {
76 return this._url;
77 }
78
79 /**
80 * @return {!Promise<ExecutionContext>}
81 */
82 /* async */ executionContext() {return (fn => {
83 const gen = fn.call(this);
84 return new Promise((resolve, reject) => {
85 function step(key, arg) {
86 let info, value;
87 try {
88 info = gen[key](arg);
89 value = info.value;
90 } catch (error) {
91 reject(error);
92 return;
93 }
94 if (info.done) {
95 resolve(value);
96 } else {
97 return Promise.resolve(value).then(
98 value => {
99 step('next', value);
100 },
101 err => {
102 step('throw', err);
103 });
104 }
105 }
106 return step('next');
107 });
108})(function*(){
109 return this._executionContextPromise;
110 });}
111
112 /**
113 * @param {function()|string} pageFunction
114 * @param {!Array<*>} args
115 * @return {!Promise<*>}
116 */
117 /* async */ evaluate(pageFunction, ...args) {return (fn => {
118 const gen = fn.call(this);
119 return new Promise((resolve, reject) => {
120 function step(key, arg) {
121 let info, value;
122 try {
123 info = gen[key](arg);
124 value = info.value;
125 } catch (error) {
126 reject(error);
127 return;
128 }
129 if (info.done) {
130 resolve(value);
131 } else {
132 return Promise.resolve(value).then(
133 value => {
134 step('next', value);
135 },
136 err => {
137 step('throw', err);
138 });
139 }
140 }
141 return step('next');
142 });
143})(function*(){
144 return ((yield this._executionContextPromise)).evaluate(pageFunction, ...args);
145 });}
146
147 /**
148 * @param {function()|string} pageFunction
149 * @param {!Array<*>} args
150 * @return {!Promise<!JSHandle>}
151 */
152 /* async */ evaluateHandle(pageFunction, ...args) {return (fn => {
153 const gen = fn.call(this);
154 return new Promise((resolve, reject) => {
155 function step(key, arg) {
156 let info, value;
157 try {
158 info = gen[key](arg);
159 value = info.value;
160 } catch (error) {
161 reject(error);
162 return;
163 }
164 if (info.done) {
165 resolve(value);
166 } else {
167 return Promise.resolve(value).then(
168 value => {
169 step('next', value);
170 },
171 err => {
172 step('throw', err);
173 });
174 }
175 }
176 return step('next');
177 });
178})(function*(){
179 return ((yield this._executionContextPromise)).evaluateHandle(pageFunction, ...args);
180 });}
181}
182
183module.exports = {Worker};
184helper.tracePublicAPI(Worker);