-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jack Steam
committed
Mar 10, 2020
1 parent
c5af655
commit 1273b6e
Showing
8 changed files
with
276 additions
and
389 deletions.
There are no files selected for viewing
File renamed without changes.
Large diffs are not rendered by default.
Oops, something went wrong.
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,68 @@ | ||
import jestChromeSchema from '../jest-chrome-schema.json'; | ||
import { addEvent, addFunction, addProperty } from './add-elements'; | ||
import { JestChrome } from '../jest-chrome'; | ||
|
||
export function createHandler(schema = jestChromeSchema as any): ProxyHandler<JestChrome> { | ||
return { | ||
ownKeys() { | ||
return Reflect.ownKeys(schema); | ||
}, | ||
getOwnPropertyDescriptor(target, prop) { | ||
if (prop in schema) { | ||
return { | ||
enumerable: true, | ||
configurable: true, | ||
}; | ||
} | ||
else { | ||
return { | ||
enumerable: false, | ||
configurable: false, | ||
}; | ||
} | ||
}, | ||
set(target, key, value) { | ||
if (key in schema && | ||
key === 'lastError' && | ||
!(typeof value === 'object' && | ||
typeof value?.message === 'string')) { | ||
throw new TypeError('chrome.runtime.lastError should be type { message: string }'); | ||
} | ||
return Reflect.set(target, key, value); | ||
}, | ||
deleteProperty(target, key) { | ||
// Mark property as deleted, so it won't be retrieved from the schema | ||
return Reflect.set(target, key, null); | ||
}, | ||
has(target, key) { | ||
const inTarget = key in target && Reflect.get(target, key); | ||
return inTarget !== null && !!inTarget; | ||
}, | ||
get(target, key) { | ||
if (key in target) { | ||
const value = Reflect.get(target, key); | ||
// Check that the value wasn't deleted | ||
return value !== null ? value : undefined; | ||
} | ||
else if (key in schema) { | ||
switch (schema[key].type) { | ||
case 'event': | ||
return addEvent(schema[key], target); | ||
case 'function': | ||
return addFunction(schema[key], target); | ||
case 'property': | ||
return addProperty(schema[key], target); | ||
// default is namespace | ||
default: { | ||
const proxy = new Proxy<Record<string, any>>({}, createHandler(schema[key])); | ||
Object.assign(target, { [key]: proxy }); | ||
return proxy; | ||
} | ||
} | ||
} | ||
else { | ||
return undefined; | ||
} | ||
}, | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,92 +1,7 @@ | ||
import { JestChrome as JestChromeNamespace } from './jest-chrome' | ||
import jestChromeSchema from './jest-chrome-schema.json' | ||
import { | ||
addEvent, | ||
addFunction, | ||
addProperty, | ||
} from './add-elements' | ||
|
||
export type JestChrome = typeof JestChromeNamespace | ||
import { JestChrome } from '../jest-chrome' | ||
import { createHandler } from './createHandler' | ||
|
||
export const chrome = new Proxy<JestChrome>( | ||
{} as JestChrome, | ||
createHandler(), | ||
) | ||
|
||
export function createHandler( | ||
schema = jestChromeSchema as any, | ||
): ProxyHandler<JestChrome> { | ||
return { | ||
ownKeys() { | ||
return Reflect.ownKeys(schema) | ||
}, | ||
getOwnPropertyDescriptor(target, prop) { | ||
if (prop in schema) { | ||
return { | ||
enumerable: true, | ||
configurable: true, | ||
} | ||
} else { | ||
return { | ||
enumerable: false, | ||
configurable: false, | ||
} | ||
} | ||
}, | ||
set(target, key, value) { | ||
if ( | ||
key in schema && | ||
key === 'lastError' && | ||
!( | ||
typeof value === 'object' && | ||
typeof value?.message === 'string' | ||
) | ||
) { | ||
throw new TypeError( | ||
'chrome.runtime.lastError should be type { message: string }', | ||
) | ||
} | ||
|
||
return Reflect.set(target, key, value) | ||
}, | ||
deleteProperty(target, key) { | ||
// Mark property as deleted, so it won't be retrieved from the schema | ||
return Reflect.set(target, key, null) | ||
}, | ||
has(target, key) { | ||
const inTarget = key in target && Reflect.get(target, key) | ||
|
||
return inTarget !== null && !!inTarget | ||
}, | ||
get(target, key) { | ||
if (key in target) { | ||
const value = Reflect.get(target, key) | ||
// Check that the value wasn't deleted | ||
return value !== null ? value : undefined | ||
} else if (key in schema) { | ||
switch (schema[key].type) { | ||
case 'event': | ||
return addEvent(schema[key], target) | ||
case 'function': | ||
return addFunction(schema[key], target) | ||
case 'property': | ||
return addProperty(schema[key], target) | ||
|
||
// default is namespace | ||
default: { | ||
const proxy = new Proxy<Record<string, any>>( | ||
{}, | ||
createHandler(schema[key]), | ||
) | ||
|
||
Object.assign(target, { [key]: proxy }) | ||
|
||
return proxy | ||
} | ||
} | ||
} else { | ||
return undefined | ||
} | ||
}, | ||
} | ||
} |