Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

fix(plugins): vue package in production #1536

Merged
merged 5 commits into from
Nov 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions __tests__/unit/services/plugin-manager/plugin-sandbox.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import path from 'path'
import { createLocalVue } from '@vue/test-utils'
import { PluginSandbox } from '@/services/plugin-manager/plugin-sandbox'
import { Plugin } from '@/services/plugin-manager/plugin'

const rootPath = path.resolve(__dirname, '../../../')
const app = createLocalVue()
const plugin = new Plugin({
rootPath,
fullPath: path.resolve(__dirname),
config: {
permissions: []
}
})

beforeEach(() => {
plugin.config.permissions = []
})

describe('Plugin Sandbox', () => {
it('should parse file with api', async () => {
const sandbox = new PluginSandbox({
app,
plugin
})

await sandbox.install()

const pluginVM = sandbox.getComponentVM()
expect(pluginVM.run(`
module.exports = walletApi
`)).toBeDefined()
})

it('should parse file without api', async () => {
const sandbox = new PluginSandbox({
app,
plugin
})

await sandbox.install()

const pluginVM = sandbox.getPluginVM()
expect(() => pluginVM.run(`
module.exports = walletApi
`)).toThrowError('walletApi is not defined')
})

it('should read permissions', async () => {
plugin.config.permissions = ['ALERTS']
const sandbox = new PluginSandbox({
app,
plugin
})

await sandbox.install()

const componentVM = sandbox.getComponentVM()
expect(() => componentVM.run(`
module.exports = walletApi.alert
`)).toBeDefined()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ const pluginObject = {
}

const sandbox = {
getVM: jest.fn(() => ({
getComponentVM: jest.fn(() => ({
run: jest.fn()
})),
getPluginVM: jest.fn(() => ({
run: jest.fn()
})),
app: {}
Expand Down
33 changes: 19 additions & 14 deletions src/renderer/services/plugin-manager/plugin-sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export class PluginSandbox {

this.sandbox = {}
this.walletApi = {}

this.sandboxes = this.__mapPermissionsToSandbox()
}

Expand All @@ -57,32 +56,38 @@ export class PluginSandbox {
}
}

getVM ({ loadApi }) {
const fullPath = this.plugin.fullPath

getComponentVM () {
return new NodeVM({
sandbox: this.getSandbox(loadApi),
sandbox: this.getSandbox(true),
require: {
builtin: [],
context: 'sandbox',
resolve: function (source) {
return path.resolve(fullPath, source)
},
external: {
modules: [
path.resolve(fullPath),
'vue/dist/vue.common.js'
],
modules: [],
transitive: true
},
root: [
this.plugin.rootPath,
path.resolve(fullPath)
path.resolve(this.plugin.fullPath)
]
}
})
}

getPluginVM () {
const { rootPath } = this.plugin

return new NodeVM({
sandbox: this.getSandbox(false),
require: {
context: 'sandbox',
resolve: function (source) {
return path.resolve(rootPath, 'node_modules', source)
},
external: ['vue']
}
})
}

async install () {
await this.__run(this.sandboxes[PUBLIC.name])

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/services/plugin-manager/plugin-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class PluginSetup {
localVue.options._base = localVue
this.vue = localVue

this.pluginObject = this.sandbox.getVM(false).run(
this.pluginObject = this.sandbox.getPluginVM().run(
fs.readFileSync(path.join(plugin.fullPath, 'src/index.js')),
path.join(plugin.fullPath, 'src/index.js')
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export function createComponentsSetup (plugin, pluginObject, sandbox, vue) {
vue,
fullPath,
name: componentName,
pluginVM: sandbox.getVM({ loadApi: false }),
componentVM: sandbox.getVM({ loadApi: true }),
pluginVM: sandbox.getPluginVM(),
componentVM: sandbox.getComponentVM(),
logger: sandbox.app.$logger
})

Expand Down