-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a script provider to only load scripts once
- Loading branch information
Showing
13 changed files
with
139 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`configuration throws an exception when the log configuration is invalid 1`] = `"Invalid configuration."`; | ||
|
||
exports[`configuration throws an exception when the queue configuration is invalid 1`] = `"Invalid configuration."`; | ||
|
||
exports[`configuration throws an exception when the worker configuration is invalid 1`] = `"Invalid configuration."`; |
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,5 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`worker :: scriptProvider throws an exception when requesting a non supported script 1`] = `"Non supported script key. \\"nonSupportedScript\\" provided but expected one of postRender."`; | ||
|
||
exports[`worker :: scriptProvider throws an exception when the script content is invalid 1`] = `"The script provided doesn't set the script variable as a valid function."`; |
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,62 @@ | ||
import { __, includes, pipe } from 'ramda' | ||
import fs from 'fs' | ||
import path from 'path' | ||
import vm from 'vm' | ||
|
||
/** | ||
* @type ScriptProvider = { | ||
* get :: String -> Function, | ||
* } | ||
*/ | ||
|
||
export const POST_RENDER_SCRIPT_KEY = 'postRender' | ||
const VALID_SCRIPT_KEYS = [POST_RENDER_SCRIPT_KEY] | ||
|
||
// isScriptKeyValid :: String -> Boolean | ||
const isScriptKeyValid = includes(__, VALID_SCRIPT_KEYS) | ||
|
||
// resolveScriptPath :: String -> String | ||
const resolveScriptPath = key => path.join(process.cwd(), `scripts/${key}.js`) | ||
|
||
// resolveScriptContent :: String -> String | ||
const resolveScriptContent = pipe( | ||
resolveScriptPath, | ||
scriptPath => fs.readFileSync(scriptPath, { encoding: 'utf8', flag: 'r' }), | ||
) | ||
|
||
// resolveScript :: String -> Function | ||
const resolveScript = pipe( | ||
resolveScriptContent, | ||
scriptContent => { | ||
const script = new vm.Script(scriptContent) | ||
|
||
const context = { script: null } | ||
vm.createContext(context) | ||
|
||
script.runInContext(context) | ||
|
||
if (typeof context.script !== 'function') { | ||
throw new Error(`The script provided doesn't set the script variable as a valid function.`) | ||
} | ||
|
||
return context.script | ||
}, | ||
) | ||
|
||
// createScriptProvider :: () -> ScriptProvider | ||
export default () => ({ | ||
_scripts: {}, | ||
|
||
get: function (key) { | ||
if (!isScriptKeyValid(key)) { | ||
throw new Error(`Non supported script key. "${key}" provided but expected one of ${VALID_SCRIPT_KEYS.join(', ')}.`) | ||
} | ||
|
||
// eslint-disable-next-line no-prototype-builtins | ||
if (!this._scripts.hasOwnProperty(key)) { | ||
this._scripts[key] = resolveScript(key) | ||
} | ||
|
||
return this._scripts[key] | ||
}, | ||
}) |
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,32 @@ | ||
import createScriptProvider from './scriptProvider' | ||
import fs from 'fs' | ||
|
||
jest.mock('fs') | ||
|
||
beforeEach(() => { | ||
fs.readFileSync.mockClear() | ||
}) | ||
|
||
describe('worker :: scriptProvider', () => { | ||
it(`throws an exception when requesting a non supported script`, () => { | ||
const scriptProvider = createScriptProvider() | ||
|
||
expect(() => scriptProvider.get('nonSupportedScript')).toThrowErrorMatchingSnapshot() | ||
}) | ||
|
||
it(`throws an exception when the script content is invalid`, () => { | ||
const scriptProvider = createScriptProvider() | ||
|
||
fs.readFileSync.mockReturnValueOnce('notTheRightVariable = () => {};') | ||
|
||
expect(() => scriptProvider.get('postRender')).toThrowErrorMatchingSnapshot() | ||
}) | ||
|
||
it(`returns the postRender script`, () => { | ||
const scriptProvider = createScriptProvider() | ||
|
||
fs.readFileSync.mockReturnValueOnce('script = () => {};') | ||
|
||
expect(scriptProvider.get('postRender')).toEqual(expect.any(Function)) | ||
}) | ||
}) |