Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

stencila-js

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

stencila-js - npm Package Compare versions

Comparing version 0.2.1 to 0.3.0

src/need.js

8

package.json
{
"name": "stencila-js",
"version": "0.2.1",
"version": "0.3.0",
"description": "Stencila components for Javascript",

@@ -9,2 +9,4 @@ "main": "index.js",

"test": "tape tests | tap-spec",
"test-bundle": "browserify tests/*.test.js > tests/bundle.temp.js",
"test-browser": "browserify tests/*.test.js | testling",
"cover": "istanbul cover tests",

@@ -34,2 +36,3 @@ "docs": "documentation build --config docs/docs.yml --output docs --format html",

"dependencies": {
"buble": "^0.15.2",
"d3": "^4.4.0"

@@ -46,4 +49,5 @@ },

"tap-spec": "^4.1.1",
"tape": "^4.6.3"
"tape": "^4.6.3",
"testling": "^1.7.1"
}
}

@@ -11,3 +11,3 @@ ## `stencila/js` : Stencila for Javascript

- a `JsSession` class for executing code in Javascript
- a `JsSession` class for executing chunks of Javascript code
- data `pack` and `unpack` functions for transferring data over the wire and between languages

@@ -17,2 +17,10 @@

The `JsSession.execute()` method is really just a fancy `eval` with some extra functionality including:
- transpiles Javascript to [ES2015(ES6)](https://en.wikipedia.org/wiki/ECMAScript#6th_Edition_-_ECMAScript_2015)
- provides a `global` scope for persistence of session variables across calls
- unpacks input arguments into a local scope for each call
- returns errors by line number
- provides a `require` function for requiring NPM modules when in the browser
### Install

@@ -27,4 +35,6 @@

```js
const stencilaJs = require('stencila-js')
// Create a session
let session = new JsSession()
let session = new stencilaJs.JsSession()

@@ -69,2 +79,3 @@ // Evaluate an expression...

Run tests | `npm test` | `make test`
Run tests in the browser | `npm run test-browser`| `make test-browser`
Run tests with coverage | `npm run cover` | `make cover`

@@ -75,2 +86,2 @@ Build documentation | `npm run docs` | `make docs`

Tests live in the `tests` folder and are written using the [`tape`](https://github.com/substack/tape) test harness. And, in another breathtaking display of naming logic, documentation lives in the `docs` folder. Docs are published using Github Pages, so to update them after making changes run `make docs`, commit the updated docs and do a `git push`.
Tests live in the `tests` folder and are written using the [`tape`](https://github.com/substack/tape) test harness. And, in further breathtaking displays of naming logic, documentation lives in the `docs` folder and uses [documentation.js](http://documentation.js.org). Docs are published using Github Pages, so to update them after making changes run `make docs`, commit the updated docs and do a `git push`.

@@ -0,2 +1,5 @@

const buble = require('buble')
const {pack, unpack} = require('./packing')
const require_ = typeof window !== 'undefined' ? require('./need') : require

@@ -14,3 +17,10 @@ /**

constructor () {
constructor (options) {
this.options = options || {}
if (typeof this.options.transform === 'undefined') {
// By default transform code chunks whenin the browser
this.options.transform = typeof window !== 'undefined'
}
this.globals = {}

@@ -50,2 +60,4 @@ }

let error = null
// Add inputs to `locals` i.e. the execution's local scope

@@ -64,2 +76,12 @@ let locals = {}

// Transform the code
if (this.options.transform) {
try {
code = buble.transform(code).code
} catch (e) {
// Catch a syntax error
error = e
}
}
// Generate a function body

@@ -76,7 +98,6 @@ let body = 'with(globals){ with(locals){\n'

let func = null
let error = null
try {
func = Function('locals', 'globals', body) // eslint-disable-line no-new-func
func = Function('require', 'locals', 'globals', body) // eslint-disable-line no-new-func
} catch (e) {
// Catch a syntax error
// Catch a syntax error (not caught above if no transformation)
error = e

@@ -89,3 +110,3 @@ }

try {
output = func(locals, this.globals)
output = func(require_, locals, this.globals)
} catch (e) {

@@ -92,0 +113,0 @@ // Catch any errors

@@ -6,2 +6,14 @@ const {pack} = require('../src/packing')

test('JsSession can be constructed with options', t => {
let s1 = new JsSession()
let s2 = new JsSession({
transform: true
})
t.equal(s1.options.transform, typeof window !== 'undefined', 'transform defaults to true in browser, false otherwise')
t.equal(s2.options.transform, true)
t.end()
})
test('JsSession.execute with no inputs, no errors and no output', function (t) {

@@ -57,1 +69,21 @@ let s = new JsSession()

})
test('JsSession will transform code to ES2015(ES6)', function (t) {
let s = new JsSession({
transform: true
})
t.deepEqual(s.execute('Math.max(...[1,3,2])'), {errors: {}, output: pack(3)})
t.end()
})
if (typeof window !== 'undefined') {
test('JsSession can dynamically require NPM modules', t => {
let s = new JsSession()
t.deepEqual(s.execute('let isNumber = require("is-number")\nisNumber(1)'), {errors: {}, output: pack(true)})
t.end()
})
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc