Skip to content

Commit 0f7aa70

Browse files
fixed sandbox resource injection; fixed minified version; changed sandbox idleTimeout default value;
1 parent 6a4a599 commit 0f7aa70

12 files changed

+91
-50
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/dist/
22
/node_modules/
33
/lib/
4+
/trash/
45
/.idea/

.npmignore

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/node_modules/
22
/.idea/
3-
.babelrc
3+
/.babelrc
44
/public/
55
/test/
66
/src/
7-
gulpfile.js
8-
.travis.yml
7+
/trash/
8+
/gulpfile.js
9+
/.travis.yml

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ sharedOptions.abort();
139139
Default `undefined` value means prefer sandboxed mode, but allow non-sandboxed query if the environment doesn't
140140
support it. In sandboxed mode all requests will be done in invisible iframe proxy, created temporally for each
141141
origin
142-
- `idleTimeout: Number= 15000` idle timeout for each sandbox in ms
142+
- `idleTimeout: Number= 60000` idle timeout for each sandbox in ms
143143
- `params: Object` Object with query params to combine with a URL string
144144
- `timeout: Number= 15000` max query pending time in ms. Default: `15000` (15 seconds)
145145
- `preventCache: Boolean= true` force disable cache by adding timestamp to a query param `_rnd`

gulpfile.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,12 @@ function createBuildTask(entryFile, buildOptions) {
3535
minify
3636
} = buildOptions || {};
3737

38-
3938
const taskName = `build:${taskTargetName}`;
4039

4140
const rollupPlugins = [
4241
resolve({jsnext: true}),
4342
commonjs({
44-
include: include === false ? undefined : include,
43+
include: include !== false ? include : undefined,
4544
exclude
4645
})
4746
];
@@ -95,6 +94,7 @@ const clientBuildTaskES = createBuildTask(clientEntryFile, {format: "esm"});
9594
const clientBuildTests = createBuildTask("test/safe-jsonp.spec.js", {
9695
taskTargetName: "test",
9796
format: "cjs",
97+
include: ["node_modules/**", "dist/**"],
9898
toES5: true,
9999
destPath: "./test/"
100100
});

package-lock.json

+49
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<script>
1111
var origin = location.origin || (location.protocol + "//" + location.host);
12-
var src = origin.indexOf('localhost') ? "/dist/safe-jsonp.umd.js" : "https://unpkg.com/safe-jsonp/dist/safe-jsonp.umd.js";
12+
var src = origin.indexOf('localhost') ? "/dist/safe-jsonp.umd.min.js" : "https://unpkg.com/safe-jsonp/dist/safe-jsonp.umd.min.js";
1313

1414
document.write("<script src='" + src + "'></" + "script>")
1515
</script>

src/lib/proxy.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import fetch from "./fetch";
2+
13
export default function proxy() {
24
const queries = {},
35
response = (data) => {

src/lib/sandbox.js

+21-23
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,30 @@ import {parseURL, whenDOMReady, addEvent} from "./browser";
33
import fetch from "./fetch";
44
import proxy from "./proxy";
55

6-
const injectResource = (imports) => Object.keys(imports).map((name) => {
7-
const resource = imports[name];
8-
9-
switch (getType(resource)) {
10-
case "string":
6+
const injectResources = (imports) => imports.map((resource) => {
7+
if (getType(resource) === "function") {
8+
const {name} = resource;
9+
if (!name) {
10+
throw Error(`Cannot resolve fn resource name`);
11+
}
12+
return `var ${name}=function ` + resource.toString().replace(/^function\s?/, "");
13+
} else {
1114
return resource;
12-
case "function":
13-
return `var ${name}= ${resource.toString()}`;
14-
default:
15-
return resource.toString();
15+
}
1616
}
17-
18-
}).join(";\n");
17+
).join(";\n");
1918

2019

2120
const sandboxes = {};
2221

2322
export default function Sandbox(options) {
24-
const sandbox = this,
25-
imports = {
26-
fetch,
27-
proxy,
28-
mixin,
29-
encodeParams,
30-
randomStr,
31-
generateUniquePropName
32-
};
23+
const sandbox = this;
3324

3425
let iframe = document.createElement("iframe"),
3526
queries = {},
3627
{
3728
origin,
38-
idleTimeout = 15000,
29+
idleTimeout = 60000,
3930
mode = "data"
4031
} = options || {},
4132
isReady = false,
@@ -51,8 +42,15 @@ export default function Sandbox(options) {
5142
const content = [
5243
"<!DOCTYPE html><html><body>iframe:sandbox<",
5344
"script>",
54-
injectResource(imports),
55-
";proxy();</",
45+
injectResources([
46+
fetch,
47+
proxy,
48+
mixin,
49+
encodeParams,
50+
randomStr,
51+
generateUniquePropName
52+
]),
53+
`;${proxy.name}();</`,
5654
"script></body></html>"
5755
].join("");
5856

src/lib/utils.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function randomStr(length = 6) {
1313
return str;
1414
}
1515

16-
export const generateUniquePropName= (obj, generator= (i)=>`${i.toString(36)}`, maxIterations= 1000)=>{
16+
export function generateUniquePropName(obj, generator = (i) => `${i.toString(36)}`, maxIterations = 1000) {
1717
let key, i= 0;
1818
while((key= generator.call(obj, i)) && key in obj){
1919
if(i++>maxIterations) return null;
@@ -85,7 +85,9 @@ export function mixin(obj) {
8585
return obj;
8686
}
8787

88-
export const encodeParams = (params) => Object.keys(params).map(param => {
89-
let rawValue = params[param];
90-
return `${param}=${encodeURIComponent(rawValue && typeof rawValue == "object" ? JSON.stringify(rawValue) : ("" + rawValue))}`;
91-
}).join("&");
88+
export function encodeParams(params) {
89+
return Object.keys(params).map(param => {
90+
let rawValue = params[param];
91+
return `${param}=${encodeURIComponent(rawValue && typeof rawValue == "object" ? JSON.stringify(rawValue) : ("" + rawValue))}`;
92+
}).join("&")
93+
};

src/safe-jsonp.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export default function JSONP(url, options, callback) {
4141

4242
let {
4343
sandbox,
44-
idleTimeout = 5000,
44+
idleTimeout = 60000,
4545
timeout,
4646
preventCache,
4747
cbParam,

test/safe-jsonp.spec.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
//const chai = require("chai");
2-
1+
import JSONP from "../dist/safe-jsonp.umd.min";
2+
import chai from "chai";
33

44
const {expect}= chai;
5-
/*const JSONP = require("../dist/safe-jsonp.umd");*/
65

76
const JSONP_URL_VALID = "http://www.mocky.io/v2/5c069fbf3300006c00ef2b3e";
87
const JSONP_URL_VALID2 = "http://www.mocky.io/v2/5c06a4bb3300006300ef2b61";
@@ -15,11 +14,6 @@ const JSONP_URL_CB_TOO_MANY_ARGS = "http://www.mocky.io/v2/5c06a20e3300006c00ef2
1514

1615
describe("JSONP: remote endpoint test", function () {
1716

18-
const params = {
19-
y: 2
20-
},
21-
expected = {x: 1};
22-
2317
describe("constructor", function () {
2418

2519
it("should throw if the first argument is not a string", function () {
@@ -130,6 +124,4 @@ describe("JSONP: remote endpoint test", function () {
130124

131125
makeTests({sandbox: false});
132126
makeTests({sandbox: true});
133-
134-
135127
});

test/test.html

-4
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,11 @@
1010
<div id="mocha"></div>
1111
<script src="http://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/7.0.0/polyfill.min.js"></script>
1212
<script src="../node_modules/mocha/mocha.js"></script>
13-
<script src="../node_modules/chai/chai.js"></script>
14-
1513

1614
<script>
1715
window.initMochaPhantomJS && window.initMochaPhantomJS();
1816
mocha.setup('bdd');
1917
</script>
20-
<script src="../dist/safe-jsonp.umd.js"></script>
21-
<!--<script src="safe-jsonp.spec.js"></script>-->
2218
<script src="../test/safe-jsonp.spec.cjs.js"></script>
2319
<script>
2420
mocha.run();

0 commit comments

Comments
 (0)