Skip to content

Commit 6cb1e03

Browse files
authored
feat(rpc): disallow deps into rpc client from outside (#3199)
For this, common converters are extracted from rpc serializers.
1 parent 3e023f6 commit 6cb1e03

20 files changed

+148
-124
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"tsc-installer": "tsc -p ./src/install/tsconfig.json",
1919
"doc": "node utils/doclint/cli.js",
2020
"test-infra": "node utils/doclint/check_public_api/test/test.js && node utils/doclint/preprocessor/test.js && node utils/testrunner/test/test.js",
21-
"lint": "npm run eslint && npm run tsc && npm run doc && npm run test-types && npm run test-infra",
21+
"lint": "npm run eslint && npm run tsc && npm run doc && npm run check-deps && npm run test-types && npm run test-infra",
2222
"debug-test": "node --inspect-brk test/test.js",
2323
"clean": "rimraf lib && rimraf types",
2424
"prepare": "node install-from-github.js",

src/converters.ts

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/**
2+
* Copyright (c) Microsoft Corporation.
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+
*/
16+
17+
import * as fs from 'fs';
18+
import * as mime from 'mime';
19+
import * as path from 'path';
20+
import * as util from 'util';
21+
import * as types from './types';
22+
import { helper, assert } from './helper';
23+
24+
export async function normalizeFilePayloads(files: string | types.FilePayload | string[] | types.FilePayload[]): Promise<types.FilePayload[]> {
25+
let ff: string[] | types.FilePayload[];
26+
if (!Array.isArray(files))
27+
ff = [ files ] as string[] | types.FilePayload[];
28+
else
29+
ff = files;
30+
const filePayloads: types.FilePayload[] = [];
31+
for (const item of ff) {
32+
if (typeof item === 'string') {
33+
const file: types.FilePayload = {
34+
name: path.basename(item),
35+
mimeType: mime.getType(item) || 'application/octet-stream',
36+
buffer: await util.promisify(fs.readFile)(item)
37+
};
38+
filePayloads.push(file);
39+
} else {
40+
filePayloads.push(item);
41+
}
42+
}
43+
return filePayloads;
44+
}
45+
46+
export async function normalizeFulfillParameters(params: types.FulfillResponse & { path?: string }): Promise<types.NormalizedFulfillResponse> {
47+
let body = '';
48+
let isBase64 = false;
49+
let length = 0;
50+
if (params.path) {
51+
const buffer = await util.promisify(fs.readFile)(params.path);
52+
body = buffer.toString('base64');
53+
isBase64 = true;
54+
length = buffer.length;
55+
} else if (helper.isString(params.body)) {
56+
body = params.body;
57+
isBase64 = false;
58+
length = Buffer.byteLength(body);
59+
} else if (params.body) {
60+
body = params.body.toString('base64');
61+
isBase64 = true;
62+
length = params.body.length;
63+
}
64+
const headers: types.Headers = {};
65+
for (const header of Object.keys(params.headers || {}))
66+
headers[header.toLowerCase()] = String(params.headers![header]);
67+
if (params.contentType)
68+
headers['content-type'] = String(params.contentType);
69+
else if (params.path)
70+
headers['content-type'] = mime.getType(params.path) || 'application/octet-stream';
71+
if (length && !('content-length' in headers))
72+
headers['content-length'] = String(length);
73+
74+
return {
75+
status: params.status || 200,
76+
headers: headersObjectToArray(headers),
77+
body,
78+
isBase64
79+
};
80+
}
81+
82+
export function normalizeContinueOverrides(overrides: types.ContinueOverrides): types.NormalizedContinueOverrides {
83+
return {
84+
method: overrides.method,
85+
headers: overrides.headers ? headersObjectToArray(overrides.headers) : undefined,
86+
postData: helper.isString(overrides.postData) ? Buffer.from(overrides.postData, 'utf8') : overrides.postData,
87+
};
88+
}
89+
90+
export function headersObjectToArray(headers: types.Headers): types.HeadersArray {
91+
const result: types.HeadersArray = [];
92+
for (const name in headers) {
93+
if (!Object.is(headers[name], undefined)) {
94+
const value = headers[name];
95+
assert(helper.isString(value), `Expected value of header "${name}" to be String, but "${typeof value}" is found.`);
96+
result.push({ name, value });
97+
}
98+
}
99+
return result;
100+
}
101+
102+
export function headersArrayToObject(headers: types.HeadersArray): types.Headers {
103+
const result: types.Headers = {};
104+
for (const { name, value } of headers)
105+
result[name] = value;
106+
return result;
107+
}
108+
109+
export function envObjectToArray(env: types.Env): types.EnvArray {
110+
const result: types.EnvArray = [];
111+
for (const name in env) {
112+
if (!Object.is(env[name], undefined))
113+
result.push({ name, value: String(env[name]) });
114+
}
115+
return result;
116+
}
117+
118+
export function envArrayToObject(env: types.EnvArray): types.Env {
119+
const result: types.Env = {};
120+
for (const { name, value } of env)
121+
result[name] = value;
122+
return result;
123+
}

src/dom.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import * as types from './types';
2626
import { Progress } from './progress';
2727
import DebugScript from './debug/injected/debugScript';
2828
import { FatalDOMError, RetargetableDOMError } from './common/domErrors';
29-
import { normalizeFilePayloads } from './rpc/serializers';
29+
import { normalizeFilePayloads } from './converters';
3030

3131
export class FrameExecutionContext extends js.ExecutionContext {
3232
readonly frame: frames.Frame;

src/network.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import * as frames from './frames';
1818
import * as types from './types';
1919
import { assert, helper } from './helper';
2020
import { URLSearchParams } from 'url';
21-
import { normalizeFulfillParameters, normalizeContinueOverrides } from './rpc/serializers';
21+
import { normalizeFulfillParameters, normalizeContinueOverrides } from './converters';
2222

2323
export function filterCookies(cookies: types.NetworkCookie[], urls: string[]): types.NetworkCookie[] {
2424
const parsedURLs = urls.map(s => new URL(s));

src/rpc/client/browser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { ChannelOwner } from './channelOwner';
2222
import { Events } from '../../events';
2323
import { LoggerSink } from '../../loggerSink';
2424
import { BrowserType } from './browserType';
25-
import { headersObjectToArray } from '../serializers';
25+
import { headersObjectToArray } from '../../converters';
2626

2727
export class Browser extends ChannelOwner<BrowserChannel, BrowserInitializer> {
2828
readonly _contexts = new Set<BrowserContext>();

src/rpc/client/browserContext.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { Browser } from './browser';
2626
import { Events } from '../../events';
2727
import { TimeoutSettings } from '../../timeoutSettings';
2828
import { Waiter } from './waiter';
29-
import { headersObjectToArray } from '../serializers';
29+
import { headersObjectToArray } from '../../converters';
3030

3131
export class BrowserContext extends ChannelOwner<BrowserContextChannel, BrowserContextInitializer> {
3232
_pages = new Set<Page>();

src/rpc/client/browserType.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { BrowserContext } from './browserContext';
2121
import { ChannelOwner } from './channelOwner';
2222
import { BrowserServer } from './browserServer';
2323
import { LoggerSink } from '../../loggerSink';
24-
import { headersObjectToArray, envObjectToArray } from '../serializers';
24+
import { headersObjectToArray, envObjectToArray } from '../../converters';
2525
import { serializeArgument } from './jsHandle';
2626
import { assert } from '../../helper';
2727

src/rpc/client/electron.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { TimeoutSettings } from '../../timeoutSettings';
2525
import { Waiter } from './waiter';
2626
import { Events } from '../../events';
2727
import { LoggerSink } from '../../loggerSink';
28-
import { envObjectToArray } from '../serializers';
28+
import { envObjectToArray } from '../../converters';
2929

3030
export class Electron extends ChannelOwner<ElectronChannel, ElectronInitializer> {
3131
static from(electron: ElectronChannel): Electron {

src/rpc/client/elementHandle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { Frame } from './frame';
2020
import { FuncOn, JSHandle, serializeArgument, parseResult } from './jsHandle';
2121
import { ChannelOwner } from './channelOwner';
2222
import { helper, assert } from '../../helper';
23-
import { normalizeFilePayloads } from '../serializers';
23+
import { normalizeFilePayloads } from '../../converters';
2424

2525
export class ElementHandle<T extends Node = Node> extends JSHandle<T> {
2626
readonly _elementChannel: ElementHandleChannel;

src/rpc/client/network.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import * as types from '../../types';
1919
import { RequestChannel, ResponseChannel, RouteChannel, RequestInitializer, ResponseInitializer, RouteInitializer } from '../channels';
2020
import { ChannelOwner } from './channelOwner';
2121
import { Frame } from './frame';
22-
import { normalizeFulfillParameters, headersArrayToObject, normalizeContinueOverrides } from '../serializers';
22+
import { normalizeFulfillParameters, headersArrayToObject, normalizeContinueOverrides } from '../../converters';
2323

2424
export type NetworkCookie = {
2525
name: string,

0 commit comments

Comments
 (0)