fix: expose puppeteer.Permission type (#6856)

* fix: expose puppeteer.Permission type

A regression in our own types when compared to @types/puppeteer - we
needed to expose the permissons.
This commit is contained in:
Jack Franklin 2021-02-11 15:44:56 +00:00 committed by GitHub
parent ad5935738d
commit a5e174f696
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 26 deletions

View File

@ -26,6 +26,52 @@ import { Viewport } from './PuppeteerViewport.js';
type BrowserCloseCallback = () => Promise<void> | void;
const WEB_PERMISSION_TO_PROTOCOL_PERMISSION = new Map<
Permission,
Protocol.Browser.PermissionType
>([
['geolocation', 'geolocation'],
['midi', 'midi'],
['notifications', 'notifications'],
// TODO: push isn't a valid type?
// ['push', 'push'],
['camera', 'videoCapture'],
['microphone', 'audioCapture'],
['background-sync', 'backgroundSync'],
['ambient-light-sensor', 'sensors'],
['accelerometer', 'sensors'],
['gyroscope', 'sensors'],
['magnetometer', 'sensors'],
['accessibility-events', 'accessibilityEvents'],
['clipboard-read', 'clipboardReadWrite'],
['clipboard-write', 'clipboardReadWrite'],
['payment-handler', 'paymentHandler'],
['idle-detection', 'idleDetection'],
// chrome-specific permissions we have.
['midi-sysex', 'midiSysex'],
]);
/**
* @public
*/
export type Permission =
| 'geolocation'
| 'midi'
| 'notifications'
| 'camera'
| 'microphone'
| 'background-sync'
| 'ambient-light-sensor'
| 'accelerometer'
| 'gyroscope'
| 'magnetometer'
| 'accessibility-events'
| 'clipboard-read'
| 'clipboard-write'
| 'payment-handler'
| 'idle-detection'
| 'midi-sysex';
/**
* @public
*/
@ -650,34 +696,12 @@ export class BrowserContext extends EventEmitter {
*/
async overridePermissions(
origin: string,
permissions: string[]
permissions: Permission[]
): Promise<void> {
const webPermissionToProtocol = new Map<
string,
Protocol.Browser.PermissionType
>([
['geolocation', 'geolocation'],
['midi', 'midi'],
['notifications', 'notifications'],
// TODO: push isn't a valid type?
// ['push', 'push'],
['camera', 'videoCapture'],
['microphone', 'audioCapture'],
['background-sync', 'backgroundSync'],
['ambient-light-sensor', 'sensors'],
['accelerometer', 'sensors'],
['gyroscope', 'sensors'],
['magnetometer', 'sensors'],
['accessibility-events', 'accessibilityEvents'],
['clipboard-read', 'clipboardReadWrite'],
['clipboard-write', 'clipboardReadWrite'],
['payment-handler', 'paymentHandler'],
['idle-detection', 'idleDetection'],
// chrome-specific permissions we have.
['midi-sysex', 'midiSysex'],
]);
const protocolPermissions = permissions.map((permission) => {
const protocolPermission = webPermissionToProtocol.get(permission);
const protocolPermission = WEB_PERMISSION_TO_PROTOCOL_PERMISSION.get(
permission
);
if (!protocolPermission)
throw new Error('Unknown permission: ' + permission);
return protocolPermission;

View File

@ -260,6 +260,7 @@ describe('Page', function () {
await page.goto(server.EMPTY_PAGE);
let error = null;
await context
// @ts-expect-error purposeful bad input for test
.overridePermissions(server.EMPTY_PAGE, ['foo'])
.catch((error_) => (error = error_));
expect(error.message).toBe('Unknown permission: foo');

View File

@ -857,6 +857,13 @@ function compareDocumentations(actual, expected) {
'"none"|"achromatopsia"|"blurredVision"|"deuteranopia"|"protanopia"|"tritanopia"',
},
],
[
'Method BrowserContext.overridePermissions() permissions',
{
actualName: 'Array<string>',
expectedName: 'Array<Permission>',
},
],
]);
const expectedForSource = expectedNamingMismatches.get(source);