Skip to content

Commit

Permalink
Remove eval usage
Browse files Browse the repository at this point in the history
  • Loading branch information
EtienneLem committed Sep 4, 2020
1 parent c148025 commit 79fd9e5
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 139 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,18 +204,20 @@ With Ndex, you don’t have handle the database version. It will always increase

```js
migrations = {
'201412041358_CreateUsersObjectStore': function() {
this.createObjectStore('users', { keyPath: 'id', autoIncrement: true })
'201412041358_CreateUsersObjectStore': {
type: 'createObjectStore',
args: ['users', { keyPath: 'id', autoIncrement: true }],
},

'201412041527_CreateOrganizationsObjectStore': function() {
this.createObjectStore('organizations')
this.createIndex('organizations', 'est', 'est')
},
'201412041527_CreateOrganizationsObjectStore': [
{ type: 'createObjectStore', args: ['organizations'] },
{ type: 'createIndex', args: ['organizations', 'est', 'est'] },
],

'201412041527_AddJobIndexToUsers': function() {
this.createIndex('users', 'job', 'job')
}
'201412041527_AddJobIndexToUsers': {
type: 'createIndex',
args: ['users', 'job', 'job'],
},
}
```
![](http://f.cl.ly/items/3D1k1g1J0Z29381w2f3s/Screen%20Shot%202014-12-08%20at%2010.18.22%20PM.png)
Expand Down
2 changes: 1 addition & 1 deletion dist/ndex.min.js

Large diffs are not rendered by default.

9 changes: 0 additions & 9 deletions lib/ndex/adapters/worker_adapter.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Adapter = require('../adapter')
Helpers = require('../helpers')
ConnectionRaw = require('raw!../connection')
WorkerRaw = require('raw!../workers/connection_worker')

Expand All @@ -15,10 +14,6 @@ class WorkerAdapter extends Adapter
spawnWorker: ->
{ name, migrations } = @connection

# Cannot transfer functions to a worker
# Will be eval’d in the worker thread
migrations = Helpers.stringifyFunctions(migrations)

blob = new Blob([ConnectionRaw, WorkerRaw])
blobURL = window.URL.createObjectURL(blob)

Expand All @@ -45,10 +40,6 @@ class WorkerAdapter extends Adapter
id = @id++
promise = this.createPromiseForId(id)

# Cannot transfer functions to a worker
# Will be eval’d in the worker thread for supported functions (i.e. :limit predicate)
args = Helpers.stringifyFunctions(args)

# Schedule only 1 postMessage per event loop
this.schedulePostMessage() unless @messages.length
@messages.push { id: id, method: method, args: args }
Expand Down
25 changes: 8 additions & 17 deletions lib/ndex/connection.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ factory = ->

parseMigrations: (migrations) ->
keys = Object.keys(migrations).sort()
keys.map (k) =>
version = parseInt(k)
titleMatches = k.match(/_(.+)/)
keys.map (key) =>
version = parseInt(key)
titleMatches = key.match(/_(.+)/)
title = if titleMatches then titleMatches[1].replace(/(\w)([A-Z])/g, ($1, $2, $3) -> "#{$2} #{$3.toLowerCase()}") else ''
actions = migrations[key] || []
actions = [actions] unless Array.isArray(actions)

version: version
title: title
migration: migrations[k]
key: k
{ version, title, actions, key }

deleteDatabase: ->
new Promise (resolve) =>
Expand Down Expand Up @@ -60,12 +59,8 @@ factory = ->
migrationTransaction.createObjectStore('migrations', keyPath: 'version')

for migration in migrations
# Migrations functions where transfered as string to worker
if typeof migration.migration is 'string'
migration.migration = eval("__#{migration.key} = #{migration.migration}")

(migration.migration.up || migration.migration).bind(migrationTransaction).call()
delete migration.migration
for action in migration.actions
migrationTransaction[action.type]?(action.args...)

transaction.objectStore('migrations').put(migration)

Expand Down Expand Up @@ -425,10 +420,6 @@ factory = ->

# Limit
if limit
# Limit functions where transfered as string to worker
if typeof limit is 'string'
limit = eval("__limit = #{limit}")

if typeof limit is 'function' && limit(result)
return resolve(result)
else if limit is result.length
Expand Down
21 changes: 0 additions & 21 deletions lib/ndex/helpers.coffee

This file was deleted.

27 changes: 21 additions & 6 deletions spec/fixtures/migrations.coffee
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
migrations =
'201412041358_CreateUsersObjectStore': ->
this.createObjectStore('users', { keyPath: 'id', autoIncrement: true })
# '201412041358_CreateUsersObjectStore': ->
# this.createObjectStore('users', { keyPath: 'id', autoIncrement: true })
'201412041358_CreateUsersObjectStore': [
{
type: 'createObjectStore'
args: ['users', { keyPath: 'id', autoIncrement: true }]
}
]

'201412041527_CreateOrganizationsObjectStore': ->
this.createObjectStore('organizations')
# '201412041527_CreateOrganizationsObjectStore': ->
# this.createObjectStore('organizations')
'201412041527_CreateOrganizationsObjectStore': {
type: 'createObjectStore'
args: ['organizations']
}

'201412041527_AddJobIndexToUsers': ->
this.createIndex('users', 'job', 'job')
# '201412041527_AddJobIndexToUsers': ->
# this.createIndex('users', 'job', 'job')
'201412041527_AddJobIndexToUsers': [
{
type: 'createIndex'
args: ['users', 'job', 'job']
}]

module.exports = migrations
1 change: 0 additions & 1 deletion spec/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require('./ndex/helpers_spec.coffee')
require('./ndex_spec.coffee')

describe('Adapters', function() {
Expand Down
5 changes: 0 additions & 5 deletions spec/ndex/adapters/worker_adapter_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ describe 'WorkerAdapter < Adapter', ->
expect(args).to.have.deep.property('args.name', 'foo')
expect(args).to.have.deep.property('args.migrations')

it 'transfers migrations function as string', ->
args = @adapter.worker.postMessage.firstCall.args[0]
expect(args.args.migrations).to.deep.equal
foo_migration: 'function() { return this.doSomething(); }'

describe '#handleMethod', ->
beforeEach (done) -> mockWorker(done)

Expand Down
12 changes: 1 addition & 11 deletions spec/ndex/connection_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe 'Connection', ->
expect(migrations.map (m) -> m.key).to.deep.equal ['12345', '54321']

it 'parses migrations', ->
migrations = @connection.parseMigrations({ '54321_CreateFooBar': (->), '12345_CreateBarFoo': (->) })
migrations = @connection.parseMigrations({ '54321_CreateFooBar': {}, '12345_CreateBarFoo': {} })

expected = [
{ version: 12345, title: "Create bar foo", key: "12345_CreateBarFoo" }
Expand Down Expand Up @@ -468,16 +468,6 @@ describe 'Connection', ->
{ name: 'r', job: 'developer', id: 2, interests: ['a', 'b'] }
]

it 'limits to a truthy function', ->
limit = (data) -> data.length is 3

promise = @connection.users.where(limit: limit)
expect(promise).to.eventually.deep.equal [
{ name: 'e', job: 'developer', id: 1, interests: ['a'] }
{ name: 'r', job: 'developer', id: 2, interests: ['a', 'b'] }
{ name: 'p', job: 'developer', id: 3, interests: ['b'] }
]

describe ':offset', ->
it 'offsets the items', ->
promise = @connection.users.where(offset: 2)
Expand Down
59 changes: 0 additions & 59 deletions spec/ndex/helpers_spec.coffee

This file was deleted.

0 comments on commit 79fd9e5

Please sign in to comment.