{"id":16823285,"url":"https://github.com/digitalbrainjs/safe-jsonp","last_synced_at":"2025-07-30T18:09:38.231Z","repository":{"id":40792540,"uuid":"159389400","full_name":"DigitalBrainJS/safe-jsonp","owner":"DigitalBrainJS","description":"sandboxed jsonp client with Promise support","archived":false,"fork":false,"pushed_at":"2023-01-04T08:57:39.000Z","size":1296,"stargazers_count":3,"open_issues_count":13,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-19T04:35:35.560Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/safe-jsonp","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DigitalBrainJS.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-11-27T19:39:43.000Z","updated_at":"2024-09-02T04:19:07.000Z","dependencies_parsed_at":"2023-02-02T06:15:58.978Z","dependency_job_id":null,"html_url":"https://github.com/DigitalBrainJS/safe-jsonp","commit_stats":null,"previous_names":[],"tags_count":25,"template":false,"template_full_name":null,"purl":"pkg:github/DigitalBrainJS/safe-jsonp","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DigitalBrainJS%2Fsafe-jsonp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DigitalBrainJS%2Fsafe-jsonp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DigitalBrainJS%2Fsafe-jsonp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DigitalBrainJS%2Fsafe-jsonp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DigitalBrainJS","download_url":"https://codeload.github.com/DigitalBrainJS/safe-jsonp/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DigitalBrainJS%2Fsafe-jsonp/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267914675,"owners_count":24164768,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-30T02:00:09.044Z","response_time":70,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-13T11:07:30.766Z","updated_at":"2025-07-30T18:09:38.199Z","avatar_url":"https://github.com/DigitalBrainJS.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# safe-jsonp \n[![Build Status](https://travis-ci.org/DigitalBrainJS/safe-jsonp.svg?branch=master)](https://travis-ci.org/DigitalBrainJS/safe-jsonp)\n[![](https://badgen.net/bundlephobia/min/safe-jsonp)](https://unpkg.com/safe-jsonp/dist/safe-jsonp.umd.js)\n[![](https://badgen.net/bundlephobia/minzip/safe-jsonp)](https://unpkg.com/safe-jsonp/dist/safe-jsonp.umd.js)\n[![](https://badgen.net/npm/license/safe-jsonp)](https://unpkg.com/safe-jsonp/dist/safe-jsonp.umd.js)\n\n\n:star: A safer JSONP implementation for browsers with extra features :star:\n\n# Features\n- :zap: **safer requests to untrusted origins using optional sandbox mechanism\\* (iframes \u0026 postMessage used inside)**\n- :zap: **can abort the related http requests\\** (other libs just ignore the response, but related http query keeps in pending state for some time**\n- :heavy_check_mark: no dependencies\n- :heavy_check_mark: CDN friendly\n- supports Promise and callback styles\n- supports custom Promise class\n- supports query params in url string and/or options.params property\n- automatically encoding params, converting objects and arrays params to JSON strings  \n- anti-caching `_rnd` query param\n\n\\* this feature works only in modern browsers (except all IE).\n\n\\** not available in IE, Edge has partial support. \n## Try It!\n[JSFiddle.net demo](https://jsfiddle.net/DigitalBrain/ugz5qn0r/)\n\n## Installation\n\nInstall for node.js or browserify using npm or yarn:\n\n``` bash\n$ npm install safe-jsonp --save\n```\n\n``` bash\n$ yarn add safe-jsonp\n```\n\n## Basic usage example\nPromise style:\n```javascript\nimport JSONP from \"safe-jsonp\";\n\nJSONP('http://api.github.com/users/DigitalBrainJS')\n    .then( data =\u003e console.log('JSONP data object:', data))\n    .catch( err =\u003e console.warn('Oops...we got an error', err.message))\n\n//or inside an async function \nconst response= await JSONP('http://api.github.com/users/DigitalBrainJS');\nconsole.log(response);\n```\n\n\nCallback style:\n```javascript\nimport JSONP from \"safe-jsonp\";\n\nJSONP('http://api.github.com/users/DigitalBrainJS', (err, data) =\u003e {\n        if(err){\n            console.warn('Oops...we got an error', err.message)\n        }else{\n            console.log('JSON data:', data)\n        }    \n    })\n```\n\n## CDN\nUse unpkg.com cdn to get the link to the script/module from the package:\n- minified (production) UMD ES5 version (~7kB)\n```html\n\u003cscript src=\"https://unpkg.com/safe-jsonp\"\u003e\u003c/script\u003e\n```\n- ESM ES2015 module(~14kB)\n```javascript\nimport JSONP from \"https://unpkg.com/safe-jsonp/dist/safe-jsonp.esm.js\"\n//or minified version\nimport JSONP from \"https://unpkg.com/safe-jsonp/dist/safe-jsonp.esm.min.js\"\n```\n\n## More examples\n##### additional options:\n```javascript\nconst Promise = require(\"bluebird\");\n\n//...async function\n\nconst data= await JSONP('http://api.github.com/users/DigitalBrainJS?name=bla\u0026age=23', {\n    params: {\n        foo: 1,\n        bar: [1,2,3] // We can pass objects and arrays as a param value\n    },\n    \n    timeout: 60000, //60 seconds\n    preventCache: true,//add _rnd query param with random value\n    cbParam: 'callback',\n    Promise //custom Promise class\n})\n\n//will make a request like https://api.github.com/users/DigitalBrainJS?name=bla\u0026age=23\u0026foo=1\u0026bar=%5B1%2C2%2C3%5D\u0026callback=_jsonpvqz.cb0\n//callback param is randomly generated to avoid collisions\n```   \n\n\n##### Force sandbox mode:\n```javascript\nJSONP('http://api.github.com/users/DigitalBrainJS', {sandbox: true})\n    .then(data=\u003econsole.log(data), err=\u003econsole.warn(err))\n    //will fail if the current browser doesn't support sandbox mode or data/blob uri for iframe\n```\n\n##### Aborting the request:\n```javascript\nconst jsonp= JSONP('http://api.github.com/users/DigitalBrainJS', (err, data) =\u003e {\n        console.log(err) //Error: aborted  \n    });\n    \njsonp.abort();\n```\nOr when using Promise:\n```javascript\nconst sharedOptions= {abortable: true};\n//new method \"abort\" will be attached to this object\n\nJSONP('http://api.github.com/users/DigitalBrainJS', sharedOptions)\n    .then(data=\u003econsole.log(data), err=\u003econsole.warn(err));\n    \nsharedOptions.abort();\n```\n\n## API\n\n### JSONP(url: String, [options: Object]): \\\u003cPromise\u003e\n### JSONP(url: String, [options: Object], cb: Function): \\\u003cJSONP\u003e\n\n  - `url: String` url to fetch\n  - `[options: Object]`\n      - `sandbox: Boolean|Undefined= undefined` sets sandbox mode for query handling to untrusted origins. \n      Default `undefined` value means prefer sandboxed mode, but allow non-sandboxed query if the environment doesn't \n      support it. In sandboxed mode all requests will be done in invisible iframe proxy, created temporally for each \n      origin \n      - `idleTimeout: Number= 60000` idle timeout for each sandbox in ms\n      - `params: Object` Object with query params to combine with a URL string\n      - `timeout: Number= 15000` max query pending time in ms. Default: `15000` (15 seconds)\n      - `preventCache: Boolean= true` force disable cache by adding timestamp to a query param `_rnd`\n      - `cbParam: String= 'callback'` name of the query param used by backend to get the name of the JSONP callback\n      - `Promise: Function` Promise class that be used instead of native (if environment supports it)  \n      - `abortable: Boolean` enables ability to abort for Promise mode. If this option is set to true, \n      an additional property called abort will be created in options object. \n      This allows to get the abort function via shared options object. \n      Additionally if sandbox mode is set (and it supported by a browser) this mode allows to abort the related http request of internal script element. \n- `[cb: Function(err: ?Error, [data: Object])]` callback function, called when jsonp request completes\n(with success or error). \nIf this argument is omitted, the function returns a Promise, otherwise, a JSONP instance will be returned.\n\nReturns a promise or JSON instance depending on the presence of a callback argument\n\n### JSONP class instance\n*instance methods:*\n  - `abort()` aborts the jsonp query with `Error: aborted`, handled by a callback or Promise chain.\n\n*static methods:*\n  - `parseURL(url: String): URL|Object` parse URL into components\n  - `parseParams(paramsStr: String): Object` parse URL params string eg. `a=1\u0026b=2` to params object `{a:1, b:2}`\n  - `encodeParams(params: Object): String` encode params object to string\n## Functional diagram\nSandbox mode: \n\n![Sandbox functional diagram](https://github.com/DigitalBrainJS/safe-jsonp/raw/master/public/safe-jsonp.png)  \n## Contribution\n Feel free to fork, open issues, enhance or create pull requests. \n## License\n\nThe MIT License (MIT)\n\nCopyright (c) 2018 Dmitriy Mozgovoy \u003crobotshara@gmail.com\u003e\n\nPermission 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:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE 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.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdigitalbrainjs%2Fsafe-jsonp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdigitalbrainjs%2Fsafe-jsonp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdigitalbrainjs%2Fsafe-jsonp/lists"}