forked from ampproject/worker-dom
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce "nodom" binary (ampproject#882)
* new binary: lite version * Create 'lite' version for protocol adapters use case * fix tests * make more lazy, fix more tests * Introduce the concept of DocumentLite. Sometimes developers may want to run arbitrary JS on a Worker safely/without access to a DOM. In those situations, the whole vdom implementation is overkill. This provides a DocumentLite with none of the implementation. * example to use lite version * reduce filesize * remove main-thread lite binary changes, also remove long-task from worker-thread * example no longer lite main-thread. * self nits * duped var * remove duplication * lite --> nodom, pull out to shared file * organize imports * missed a few spots * ...compilePlugin * Revert "...compilePlugin" This reverts commit f5679a1. * choumx updates * fix local prettier settings
- Loading branch information
Showing
11 changed files
with
300 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/** | ||
* Copyright 2020 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
const ALLOWLISTED_GLOBALS: { [key: string]: boolean } = { | ||
Array: true, | ||
ArrayBuffer: true, | ||
BigInt: true, | ||
BigInt64Array: true, | ||
BigUint64Array: true, | ||
Boolean: true, | ||
Cache: true, | ||
CustomEvent: true, | ||
DataView: true, | ||
Date: true, | ||
Error: true, | ||
EvalError: true, | ||
Event: true, | ||
EventTarget: true, | ||
Float32Array: true, | ||
Float64Array: true, | ||
Function: true, | ||
Infinity: true, | ||
Int16Array: true, | ||
Int32Array: true, | ||
Int8Array: true, | ||
Intl: true, | ||
JSON: true, | ||
Map: true, | ||
Math: true, | ||
NaN: true, | ||
Number: true, | ||
Object: true, | ||
Promise: true, | ||
Proxy: true, | ||
RangeError: true, | ||
ReferenceError: true, | ||
Reflect: true, | ||
RegExp: true, | ||
Set: true, | ||
String: true, | ||
Symbol: true, | ||
SyntaxError: true, | ||
TextDecoder: true, | ||
TextEncoder: true, | ||
TypeError: true, | ||
URIError: true, | ||
URL: true, | ||
Uint16Array: true, | ||
Uint32Array: true, | ||
Uint8Array: true, | ||
Uint8ClampedArray: true, | ||
WeakMap: true, | ||
WeakSet: true, | ||
WebAssembly: true, | ||
WebSocket: true, | ||
XMLHttpRequest: true, | ||
atob: true, | ||
addEventListener: true, | ||
removeEventListener: true, | ||
btoa: true, | ||
caches: true, | ||
clearInterval: true, | ||
clearTimeout: true, | ||
console: true, | ||
decodeURI: true, | ||
decodeURIComponent: true, | ||
document: true, | ||
encodeURI: true, | ||
encodeURIComponent: true, | ||
escape: true, | ||
fetch: true, | ||
indexedDB: true, | ||
isFinite: true, | ||
isNaN: true, | ||
location: true, | ||
navigator: true, | ||
onerror: true, | ||
onrejectionhandled: true, | ||
onunhandledrejection: true, | ||
parseFloat: true, | ||
parseInt: true, | ||
performance: true, | ||
requestAnimationFrame: true, | ||
cancelAnimationFrame: true, | ||
self: true, | ||
setTimeout: true, | ||
setInterval: true, | ||
unescape: true, | ||
}; | ||
|
||
// Modify global scope by removing disallowed properties. | ||
export function deleteGlobals(global: WorkerGlobalScope) { | ||
/** | ||
* @param object | ||
* @param property | ||
* @return True if property was deleted from object. Otherwise, false. | ||
*/ | ||
const deleteUnsafe = (object: any, property: string): boolean => { | ||
if (!ALLOWLISTED_GLOBALS.hasOwnProperty(property)) { | ||
try { | ||
delete object[property]; | ||
return true; | ||
} catch (e) {} | ||
} | ||
return false; | ||
}; | ||
|
||
// Walk up global's prototype chain and dereference non-allowlisted properties | ||
// until EventTarget is reached. | ||
let current = global; | ||
while (current && current.constructor !== EventTarget) { | ||
const deleted: string[] = []; | ||
const failedToDelete: string[] = []; | ||
Object.getOwnPropertyNames(current).forEach((prop) => { | ||
if (deleteUnsafe(current, prop)) { | ||
deleted.push(prop); | ||
} else { | ||
failedToDelete.push(prop); | ||
} | ||
}); | ||
console.info(`Removed ${deleted.length} references from`, current, ':', deleted); | ||
if (failedToDelete.length) { | ||
console.info(`Failed to remove ${failedToDelete.length} references from`, current, ':', failedToDelete); | ||
} | ||
current = Object.getPrototypeOf(current); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* Copyright 2020 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { PostMessage } from '../worker-thread'; | ||
import { Phase } from '../../transfer/Phase'; | ||
import { TransferrableKeys } from '../../transfer/TransferrableKeys'; | ||
import { set as setPhase } from '../phase'; | ||
import { WorkerNoDOMGlobalScope } from '../WorkerDOMGlobalScope'; | ||
|
||
/** | ||
* A lightweight Document stub for the no-dom amp binary. | ||
*/ | ||
export class DocumentStub { | ||
// Internal variables. | ||
public defaultView: WorkerNoDOMGlobalScope; | ||
public postMessage: PostMessage; | ||
public addGlobalEventListener: Function; | ||
public removeGlobalEventListener: Function; | ||
public [TransferrableKeys.allowTransfer]: boolean = true; | ||
public [TransferrableKeys.index]: number = -1; | ||
|
||
constructor() { | ||
this.defaultView = { document: this }; | ||
} | ||
|
||
public [TransferrableKeys.observe](): void { | ||
setPhase(Phase.Mutating); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.