Browse Source

refactor: bring js-sandbox project to the monorepo

Andrew Bastin 3 years ago
parent
commit
d2865c637c

+ 29 - 0
packages/hoppscotch-js-sandbox/.eslintrc.js

@@ -0,0 +1,29 @@
+module.exports = {
+  root: true,
+  env: {
+    node: true,
+    jest: true,
+    browser: true,
+  },
+  parser: "@typescript-eslint/parser",
+  parserOptions: {
+    sourceType: "module",
+    requireConfigFile: false,
+    ecmaVersion: 2021,
+  },
+  plugins: ["prettier"],
+  extends: [
+    "prettier/prettier",
+    "eslint:recommended",
+    "plugin:prettier/recommended",
+    "plugin:@typescript-eslint/eslint-recommended",
+    "plugin:@typescript-eslint/recommended",
+  ],
+  rules: {
+    semi: [2, "never"],
+    "prettier/prettier": ["warn", { semi: false }],
+    "import/no-named-as-default": "off",
+    "no-undef": "off",
+    "@typescript-eslint/no-explicit-any": "off",
+  },
+}

+ 5 - 0
packages/hoppscotch-js-sandbox/.gitignore

@@ -0,0 +1,5 @@
+node_modules
+coverage
+.husky/
+dist
+lib

+ 8 - 0
packages/hoppscotch-js-sandbox/.prettierignore

@@ -0,0 +1,8 @@
+.dependabot
+.github
+.hoppscotch
+.vscode
+package-lock.json
+node_modules
+dist
+lib

+ 3 - 0
packages/hoppscotch-js-sandbox/.prettierrc.js

@@ -0,0 +1,3 @@
+module.exports = {
+  semi: false,
+}

+ 21 - 0
packages/hoppscotch-js-sandbox/LICENSE

@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2020
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.

+ 56 - 0
packages/hoppscotch-js-sandbox/README.md

@@ -0,0 +1,56 @@
+<div align="center">
+  <a href="https://hoppscotch.io">
+    <img height=64 src="https://raw.githubusercontent.com/hoppscotch/hoppscotch/main/static/logo.png" alt="The Hoppscotch UFO logo" />
+  </a>
+</div>
+<div align="center">
+
+# Hoppscotch JavaScript Sandbox <font size=2><sup>ALPHA</sup></font>
+
+</div>
+
+This package deals with providing a JavaScript sandbox for executing various security sensitive external scripts.
+
+## Usage
+- Install the [npm package](https://www.npmjs.com/package/@hoppscotch/js-sandbox)
+```
+npm install --save @hoppscotch/js-sandbox
+```
+
+## How does this work ?
+This package makes use of [quickjs-empscripten](https://www.npmjs.com/package/quickjs-emscripten) for building sandboxes for running external code on Hoppscotch.
+
+Currently implemented sandboxes:
+- Hoppscotch Test Scripts
+
+## Development
+- Clone the repository
+```
+git clone https://github.com/hoppscotch/hopp-js-sandbox
+```
+
+- Install the package deps
+```
+npm install
+```
+
+- Try out the demo [`src/demo.ts`](https://github.com/hoppscotch/hopp-js-sandbox/blob/main/src/demo.ts) using
+```
+npm run demo
+```
+
+## Versioning
+This project follows [Semantic Versioning](https://semver.org/) but as the project is still pre-1.0. The code and the public exposed API should not be considered to be fixed and stable. Things can change at any time!
+
+## License
+This project is licensed under the [MIT License](https://opensource.org/licenses/MIT) - see [`LICENSE`](https://github.com/hoppscotch/hopp-js-sandbox/blob/main/LICENSE) for more details.
+
+<div align="center">
+  <br />
+  <br />
+  <br />
+
+  
+  ###### built with ❤︎ by the [Hoppscotch Team](https://github.com/hoppscotch) and [contributors](https://github.com/AndrewBastin/hopp-js-sandbox/graphs/contributors).
+  
+</div>

+ 6 - 0
packages/hoppscotch-js-sandbox/jest.config.js

@@ -0,0 +1,6 @@
+export default {
+  preset: "ts-jest",
+  testEnvironment: "node",
+  collectCoverage: true,
+  setupFilesAfterEnv: ['./jest.setup.ts'],
+}

+ 1 - 0
packages/hoppscotch-js-sandbox/jest.setup.ts

@@ -0,0 +1 @@
+require('@relmify/jest-fp-ts');

+ 59 - 0
packages/hoppscotch-js-sandbox/package.json

@@ -0,0 +1,59 @@
+{
+  "name": "@hoppscotch/js-sandbox",
+  "version": "0.3.1",
+  "description": "JavaScript sandboxes for running external scripts used by Hoppscotch clients",
+  "main": "./lib/index.js",
+  "types": "./lib/",
+  "type": "module",
+  "engines": {
+    "node": ">=14",
+    "pnpm": ">=3"
+  },
+  "scripts": {
+    "demo": "esrun src/demo.ts",
+    "lint": "eslint --ext .ts,.js --ignore-path .gitignore",
+    "test": "npx jest",
+    "build": "npx tsc",
+    "clean": "npx tsc --build --clean",
+    "prepublish": "npm run build",
+    "do-lintfix": "pnpm run lint",
+    "do-test": "pnpm run test"
+  },
+  "keywords": [
+    "hoppscotch",
+    "sandbox",
+    "js-sandbox",
+    "apis",
+    "test-runner"
+  ],
+  "author": "The Hoppscotch Team <support@hoppscotch.io> (https://hoppscotch.com/)",
+  "license": "MIT",
+  "dependencies": {
+    "fp-ts": "^2.11.3",
+    "lodash": "^4.17.21",
+    "quickjs-emscripten": "^0.13.0"
+  },
+  "devDependencies": {
+    "@digitak/esrun": "^1.2.4",
+    "@relmify/jest-fp-ts": "^1.1.1",
+    "@types/jest": "^26.0.23",
+    "@types/lodash": "^4.14.173",
+    "@types/node": "^15.12.5",
+    "@typescript-eslint/eslint-plugin": "^4.28.1",
+    "@typescript-eslint/parser": "^4.28.1",
+    "eslint": "^7.29.0",
+    "eslint-config-prettier": "^8.3.0",
+    "eslint-plugin-prettier": "^3.4.0",
+    "io-ts": "^2.2.16",
+    "jest": "^27.0.6",
+    "prettier": "^2.3.2",
+    "pretty-quick": "^3.1.1",
+    "ts-jest": "^27.0.3",
+    "typescript": "^4.3.5"
+  },
+  "jest": {
+    "setupFilesAfterEnv": [
+      "@relmify/jest-fp-ts"
+    ]
+  }
+}

+ 64 - 0
packages/hoppscotch-js-sandbox/src/__tests__/preRequest.spec.ts

@@ -0,0 +1,64 @@
+import { execPreRequestScript } from "../preRequest"
+import "@relmify/jest-fp-ts"
+
+describe("execPreRequestScript", () => {
+  test("returns the updated envirionment properly", () => {
+    return expect(
+      execPreRequestScript(
+        `
+          pw.env.set("bob", "newbob")
+        `,
+        [{ key: "bob", value: "oldbob" }, { key: "foo", value: "bar" }]
+      )()
+    ).resolves.toEqualRight([
+      { key: "bob", value: "newbob" },
+      { key: "foo", value: "bar" }
+    ])
+  })
+
+  test("fails if the key is not a string", () => {
+    return expect(
+      execPreRequestScript(
+        `
+          pw.env.set(10, "newbob")
+        `,
+        [{ key: "bob", value: "oldbob" }, { key: "foo", value: "bar" }]
+      )()
+    ).resolves.toBeLeft()
+  })
+
+  test("fails if the value is not a string", () => {
+    return expect(
+      execPreRequestScript(
+        `
+          pw.env.set("bob", 10)
+        `,
+        [{ key: "bob", value: "oldbob" }, { key: "foo", value: "bar" }]
+      )()
+    ).resolves.toBeLeft()
+  })
+
+  test("fails for invalid syntax", () => {
+    return expect(
+      execPreRequestScript(
+        `
+          pw.env.set("bob", 
+        `,
+        [{ key: "bob", value: "oldbob" }, { key: "foo", value: "bar" }]
+      )()
+    ).resolves.toBeLeft()
+  })
+
+  test("creates new env variable if doesn't exist", () => {
+    return expect(
+      execPreRequestScript(
+        `
+          pw.env.set("foo", "bar")
+        `,
+        []
+      )()
+    ).resolves.toEqualRight(
+      [{ key: "foo", value: "bar" }]
+    )
+  })
+})

Some files were not shown because too many files changed in this diff