pax_global_header00006660000000000000000000000064131526241410014511gustar00rootroot0000000000000052 comment=38567dcc5dc7050a6294f8e1f807e9eb1e790cbe d3-request-1.0.6/000077500000000000000000000000001315262414100135115ustar00rootroot00000000000000d3-request-1.0.6/.eslintrc000066400000000000000000000001771315262414100153420ustar00rootroot00000000000000parserOptions: sourceType: module env: browser: true extends: "eslint:recommended" rules: no-cond-assign: 0 d3-request-1.0.6/.gitignore000066400000000000000000000001001315262414100154700ustar00rootroot00000000000000*.sublime-workspace .DS_Store build/ node_modules npm-debug.log d3-request-1.0.6/.npmignore000066400000000000000000000000361315262414100155070ustar00rootroot00000000000000*.sublime-* build/*.zip test/ d3-request-1.0.6/LICENSE000066400000000000000000000027031315262414100145200ustar00rootroot00000000000000Copyright 2010-2016 Mike Bostock All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the author nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. d3-request-1.0.6/README.md000066400000000000000000000410011315262414100147640ustar00rootroot00000000000000# d3-request This module provides a convenient alternative to XMLHttpRequest. For example, to load a text file: ```js d3.text("/path/to/file.txt", function(error, text) { if (error) throw error; console.log(text); // Hello, world! }); ``` To load and parse a CSV file: ```js d3.csv("/path/to/file.csv", function(error, data) { if (error) throw error; console.log(data); // [{"Hello": "world"}, …] }); ``` To post some query parameters: ```js d3.request("/path/to/resource") .header("X-Requested-With", "XMLHttpRequest") .header("Content-Type", "application/x-www-form-urlencoded") .post("a=2&b=3", callback); ``` This module has built-in support for parsing [JSON](#json), [CSV](#csv) and [TSV](#tsv); in browsers, but not in Node, [HTML](#html) and [XML](#xml) are also supported. You can parse additional formats by using [request](#request) or [text](#text) directly. ## Installing If you use NPM, `npm install d3-request`. Otherwise, download the [latest release](https://github.com/d3/d3-request/releases/latest). You can also load directly from [d3js.org](https://d3js.org), either as a [standalone library](https://d3js.org/d3-request.v1.min.js) or as part of [D3 4.0](https://github.com/d3/d3). AMD, CommonJS, and vanilla environments are supported. In vanilla, a `d3` global is exported: ```html ``` [Try d3-request in your browser.](https://tonicdev.com/npm/d3-request) ## API Reference # d3.request(url[, callback]) [<>](https://github.com/d3/d3-request/blob/master/src/request.js#L4 "Source") Returns a new *request* for specified *url*. If no *callback* is specified, the returned *request* is not yet [sent](#request_send) and can be further configured. If a *callback* is specified, it is equivalent to calling [*request*.get](#request_get) immediately after construction: ```js d3.request(url) .get(callback); ``` If you wish to specify a request header or a mime type, you must *not* specify a callback to the constructor. Use [*request*.header](#request_header) or [*request*.mimeType](#request_mimeType) followed by [*request*.get](#request_get) instead. See [d3.json](#json), [d3.csv](#csv), [d3.tsv](#tsv), [d3.html](#html) and [d3.xml](#xml) for content-specific convenience constructors. # request.header(name[, value]) [<>](https://github.com/d3/d3-request/blob/master/src/request.js#L51 "Source") If *value* is specified, sets the request header with the specified *name* to the specified value and returns this request instance. If *value* is null, removes the request header with the specified *name* instead. If *value* is not specified, returns the current value of the request header with the specified *name*. Header names are case-insensitive. Request headers can only be modified before the request is [sent](#request_send). Therefore, you cannot pass a callback to the [request constructor](#request) if you wish to specify a header; use [*request*.get](#request_get) or similar instead. For example: ```js d3.request(url) .header("Accept-Language", "en-US") .header("X-Requested-With", "XMLHttpRequest") .get(callback); ``` Note: this library does not set the X-Requested-With header to `XMLHttpRequest` by default. Some servers require this header to mitigate unwanted requests, but the presence of the header triggers CORS preflight checks; if necessary, set this header before sending the request. # request.mimeType([type]) [<>](https://github.com/d3/d3-request/blob/master/src/request.js#L60 "Source") If *type* is specified, sets the request mime type to the specified value and returns this request instance. If *type* is null, clears the current mime type (if any) instead. If *type* is not specified, returns the current mime type, which defaults to null. The mime type is used to both set the ["Accept" request header](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html) and for [overrideMimeType](http://www.w3.org/TR/XMLHttpRequest/#the-overridemimetype%28%29-method), where supported. The request mime type can only be modified before the request is [sent](#request_send). Therefore, you cannot pass a callback to the [request constructor](#request) if you wish to override the mime type; use [*request*.get](#request_get) or similar instead. For example: ```js d3.request(url) .mimeType("text/csv") .get(callback); ``` # request.user([value]) [<>](https://github.com/d3/d3-request/blob/master/src/request.js#L80 "Source") If *value* is specified, sets the user name for authentication to the specified string and returns this request instance. If *value* is not specified, returns the current user name, which defaults to null. # request.password([value]) [<>](https://github.com/d3/d3-request/blob/master/src/request.js#L84 "Source") If *value* is specified, sets the password for authentication to the specified string and returns this request instance. If *value* is not specified, returns the current password, which defaults to null. # request.timeout([timeout]) [<>](https://github.com/d3/d3-request/blob/master/src/request.js#L74 "Source") If *timeout* is specified, sets the [timeout](http://www.w3.org/TR/XMLHttpRequest/#the-timeout-attribute) attribute of the request to the specified number of milliseconds and returns this request instance. If *timeout* is not specified, returns the current response timeout, which defaults to 0. # request.responseType([type]) [<>](https://github.com/d3/d3-request/blob/master/src/request.js#L68 "Source") If *type* is specified, sets the [response type](http://www.w3.org/TR/XMLHttpRequest/#the-responsetype-attribute) attribute of the request and returns this request instance. Typical values are: `​` (the empty string), `arraybuffer`, `blob`, `document`, and `text`. If *type* is not specified, returns the current response type, which defaults to `​`. # request.response(value) [<>](https://github.com/d3/d3-request/blob/master/src/request.js#L90 "Source") Sets the response value function to the specified function and returns this request instance. The response value function is used to map the response XMLHttpRequest object to a useful data value. See the convenience methods [json](#json) and [text](#text) for examples. # request.get([data][, callback]) [<>](https://github.com/d3/d3-request/blob/master/src/request.js#L96 "Source") Equivalent to [*request*.send](#request_send) with the GET method: ```js request.send("GET", data, callback); ``` # request.post([data][, callback]) [<>](https://github.com/d3/d3-request/blob/master/src/request.js#L101 "Source") Equivalent to [*request*.send](#request_send) with the POST method: ```js request.send("POST", data, callback); ``` # request.send(method[, data][, callback]) [<>](https://github.com/d3/d3-request/blob/master/src/request.js#L106 "Source") Issues this request using the specified *method* (such as `GET` or `POST`), optionally posting the specified *data* in the request body, and returns this request instance. If a *callback* is specified, the callback will be invoked asynchronously when the request succeeds or fails. The callback is invoked with two arguments: the error, if any, and the [response value](#request_response). The response value is undefined if an error occurs. This is equivalent to: ```js request .on("error", function(error) { callback(error); }) .on("load", function(xhr) { callback(null, xhr); }) .send(method, data); ``` If no *callback* is specified, then "load" and "error" listeners should be registered via [*request*.on](#request_on). # request.abort() [<>](https://github.com/d3/d3-request/blob/master/src/request.js#L121 "Source") Aborts this request, if it is currently in-flight, and returns this request instance. See [XMLHttpRequest’s abort](http://www.w3.org/TR/XMLHttpRequest/#the-abort%28%29-method). # request.on(type[, listener]) [<>](https://github.com/d3/d3-request/blob/master/src/request.js#L126 "Source") If *listener* is specified, sets the event *listener* for the specified *type* and returns this request instance. If an event listener was already registered for the same type, the existing listener is removed before the new listener is added. If *listener* is null, removes the current event *listener* for the specified *type* (if any) instead. If *listener* is not specified, returns the currently-assigned listener for the specified type, if any. The type must be one of the following: * `beforesend` - to allow custom headers and the like to be set before the request is [sent](#request_send). * `progress` - to monitor the [progress of the request](http://www.w3.org/TR/progress-events/). * `load` - when the request completes successfully. * `error` - when the request completes unsuccessfully; this includes 4xx and 5xx response codes. To register multiple listeners for the same *type*, the type may be followed by an optional name, such as `load.foo` and `load.bar`. See [d3-dispatch](https://github.com/d3/d3-dispatch) for details. # d3.csv(url[[, row], callback]) [<>](https://github.com/d3/d3-request/blob/master/src/csv.js "Source") Returns a new [*request*](#request) for the [CSV](https://github.com/d3/d3-dsv#csvParse) file at the specified *url* with the default mime type `text/csv`. If no *callback* is specified, this is equivalent to: ```js d3.request(url) .mimeType("text/csv") .response(function(xhr) { return d3.csvParse(xhr.responseText, row); }); ``` If a *callback* is specified, a [GET](#request_get) request is sent, making it equivalent to: ```js d3.request(url) .mimeType("text/csv") .response(function(xhr) { return d3.csvParse(xhr.responseText, row); }) .get(callback); ``` An optional *row* conversion function may be specified to map and filter row objects to a more-specific representation; see [*dsv*.parse](https://github.com/d3/d3-dsv#dsv_parse) for details. For example: ```js function row(d) { return { year: new Date(+d.Year, 0, 1), // convert "Year" column to Date make: d.Make, model: d.Model, length: +d.Length // convert "Length" column to number }; } ``` The returned *request* exposes an additional *request*.row method as an alternative to passing the *row* conversion function to d3.csv, allowing you to configure the request before sending it. For example, this: ```js d3.csv(url, row, callback); ``` Is equivalent to this: ```js d3.csv(url) .row(row) .get(callback); ``` # d3.html(url[, callback]) [<>](https://github.com/d3/d3-request/blob/master/src/html.js "Source") Returns a new [*request*](#request) for the HTML file at the specified *url* with the default mime type `text/html`. The HTML file is returned as a [document fragment](https://developer.mozilla.org/en-US/docs/DOM/range.createContextualFragment). If no *callback* is specified, this is equivalent to: ```js d3.request(url) .mimeType("text/html") .response(function(xhr) { return document.createRange().createContextualFragment(xhr.responseText); }); ``` If a *callback* is specified, a [GET](#request_get) request is sent, making it equivalent to: ```js d3.request(url) .mimeType("text/html") .response(function(xhr) { return document.createRange().createContextualFragment(xhr.responseText); }) .get(callback); ``` HTML parsing requires a global document and relies on [DOM Ranges](https://dom.spec.whatwg.org/#ranges), which are [not supported by JSDOM](https://github.com/tmpvar/jsdom/issues/317) as of version 8.3; thus, this method is supported in browsers but not in Node. # d3.json(url[, callback]) [<>](https://github.com/d3/d3-request/blob/master/src/json.js "Source") Returns a new [*request*](#request) to [get](#request_get) the [JSON](http://json.org) file at the specified *url* with the default mime type `application/json`. If no *callback* is specified, this is equivalent to: ```js d3.request(url) .mimeType("application/json") .response(function(xhr) { return JSON.parse(xhr.responseText); }); ``` If a *callback* is specified, a [GET](#request_get) request is sent, making it equivalent to: ```js d3.request(url) .mimeType("application/json") .response(function(xhr) { return JSON.parse(xhr.responseText); }) .get(callback); ``` # d3.text(url[, callback]) [<>](https://github.com/d3/d3-request/blob/master/src/text.js "Source") Returns a new [*request*](#request) to [get](#request_get) the text file at the specified *url* with the default mime type `text/plain`. If no *callback* is specified, this is equivalent to: ```js d3.request(url) .mimeType("text/plain") .response(function(xhr) { return xhr.responseText; }); ``` If a *callback* is specified, a [GET](#request_get) request is sent, making it equivalent to: ```js d3.request(url) .mimeType("text/plain") .response(function(xhr) { return xhr.responseText; }) .get(callback); ``` # d3.tsv(url[[, row], callback]) [<>](https://github.com/d3/d3-request/blob/master/src/tsv.js "Source") Returns a new [*request*](#request) for a [TSV](https://github.com/d3/d3-dsv#tsvParse) file at the specified *url* with the default mime type `text/tab-separated-values`. If no *callback* is specified, this is equivalent to: ```js d3.request(url) .mimeType("text/tab-separated-values") .response(function(xhr) { return d3.tsvParse(xhr.responseText, row); }); ``` If a *callback* is specified, a [GET](#request_get) request is sent, making it equivalent to: ```js d3.request(url) .mimeType("text/tab-separated-values") .response(function(xhr) { return d3.tsvParse(xhr.responseText, row); }) .get(callback); ``` An optional *row* conversion function may be specified to map and filter row objects to a more-specific representation; see [*dsv*.parse](https://github.com/d3/d3-dsv#dsv_parse) for details. For example: ```js function row(d) { return { year: new Date(+d.Year, 0, 1), // convert "Year" column to Date make: d.Make, model: d.Model, length: +d.Length // convert "Length" column to number }; } ``` The returned *request* exposes an additional *request*.row method as an alternative to passing the *row* conversion function to d3.tsv, allowing you to configure the request before sending it. For example, this: ```js d3.tsv(url, row, callback); ``` Is equivalent to this: ```js d3.tsv(url) .row(row) .get(callback); ``` # d3.xml(url[, callback]) [<>](https://github.com/d3/d3-request/blob/master/src/xml.js "Source") Returns a new [*request*](#request) to [get](#request_get) the XML file at the specified *url* with the default mime type `application/xml`. If no *callback* is specified, this is equivalent to: ```js d3.request(url) .mimeType("application/xml") .response(function(xhr) { return xhr.responseXML; }); ``` If a *callback* is specified, a [GET](#request_get) request is sent, making it equivalent to: ```js d3.request(url) .mimeType("application/xml") .response(function(xhr) { return xhr.responseXML; }) .get(callback); ``` XML parsing relies on [*xhr*.responseXML](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseXML) which is not supported by [node-XMLHttpRequest](https://github.com/driverdan/node-XMLHttpRequest/issues/8) as of version 1.8; thus, this method is supported in browsers but not in Node. d3-request-1.0.6/bin/000077500000000000000000000000001315262414100142615ustar00rootroot00000000000000d3-request-1.0.6/bin/rollup-node000077500000000000000000000013461315262414100164530ustar00rootroot00000000000000#!/usr/bin/env node var fs = require("fs"), rollup = require("rollup"), dependencies = require("../package.json").dependencies; rollup.rollup({ input: "index.js", external: Object.keys(dependencies) }).then(function(bundle) { return bundle.generate({ format: "cjs" }); }).then(function(bundle) { var code = bundle.code.replace( /^'use strict';$/m, "'use strict';\n\nvar XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;" ); return new Promise(function(resolve, reject) { fs.writeFile("build/d3-request.node.js", code, "utf8", function(error) { if (error) return reject(error); else resolve(); }); }); }).catch(abort); function abort(error) { console.error(error.stack); } d3-request-1.0.6/d3-request.sublime-project000066400000000000000000000002711315262414100205330ustar00rootroot00000000000000{ "folders": [ { "path": ".", "file_exclude_patterns": [ "*.sublime-workspace" ], "folder_exclude_patterns": [ "build" ] } ] } d3-request-1.0.6/index.js000066400000000000000000000004641315262414100151620ustar00rootroot00000000000000export {default as request} from "./src/request"; export {default as html} from "./src/html"; export {default as json} from "./src/json"; export {default as text} from "./src/text"; export {default as xml} from "./src/xml"; export {default as csv} from "./src/csv"; export {default as tsv} from "./src/tsv"; d3-request-1.0.6/package.json000066400000000000000000000031471315262414100160040ustar00rootroot00000000000000{ "name": "d3-request", "version": "1.0.6", "description": "A convenient alternative to XMLHttpRequest.", "keywords": [ "d3", "d3-module", "request", "ajax", "XMLHttpRequest" ], "homepage": "https://d3js.org/d3-request/", "license": "BSD-3-Clause", "author": { "name": "Mike Bostock", "url": "http://bost.ocks.org/mike" }, "main": "build/d3-request.node.js", "unpkg": "build/d3-request.js", "jsdelivr": "build/d3-request.js", "module": "index", "jsnext:main": "index.js", "repository": { "type": "git", "url": "https://github.com/d3/d3-request.git" }, "scripts": { "pretest": "rm -rf build && mkdir build && bin/rollup-node && rollup -c --banner \"$(preamble)\"", "test": "tape 'test/**/*-test.js' && eslint index.js src", "prepublishOnly": "npm run test && uglifyjs -b beautify=false,preamble=\"'$(preamble)'\" build/d3-request.js -c -m -o build/d3-request.min.js", "postpublish": "git push && git push --tags && cd ../d3.github.com && git pull && cp ../d3-request/build/d3-request.js d3-request.v1.js && cp ../d3-request/build/d3-request.min.js d3-request.v1.min.js && git add d3-request.v1.js d3-request.v1.min.js && git commit -m \"d3-request ${npm_package_version}\" && git push && cd - && zip -j build/d3-request.zip -- LICENSE README.md build/d3-request.js build/d3-request.min.js" }, "dependencies": { "d3-collection": "1", "d3-dispatch": "1", "d3-dsv": "1", "xmlhttprequest": "1" }, "devDependencies": { "eslint": "4", "package-preamble": "0.1", "rollup": "0.49", "tape": "4", "uglify-js": "3" } } d3-request-1.0.6/rollup.config.js000066400000000000000000000004661315262414100166360ustar00rootroot00000000000000export default { input: "index", external: [ "d3-collection", "d3-dispatch", "d3-dsv" ], output: { extend: true, file: "build/d3-request.js", format: "umd", globals: { "d3-collection": "d3", "d3-dispatch": "d3", "d3-dsv": "d3" }, name: "d3" } }; d3-request-1.0.6/src/000077500000000000000000000000001315262414100143005ustar00rootroot00000000000000d3-request-1.0.6/src/csv.js000066400000000000000000000001451315262414100154310ustar00rootroot00000000000000import {csvParse} from "d3-dsv"; import dsv from "./dsv"; export default dsv("text/csv", csvParse); d3-request-1.0.6/src/dsv.js000066400000000000000000000010051315262414100154260ustar00rootroot00000000000000import request from "./request"; export default function(defaultMimeType, parse) { return function(url, row, callback) { if (arguments.length < 3) callback = row, row = null; var r = request(url).mimeType(defaultMimeType); r.row = function(_) { return arguments.length ? r.response(responseOf(parse, row = _)) : row; }; r.row(row); return callback ? r.get(callback) : r; }; } function responseOf(parse, row) { return function(request) { return parse(request.responseText, row); }; } d3-request-1.0.6/src/html.js000066400000000000000000000002351315262414100156020ustar00rootroot00000000000000import type from "./type"; export default type("text/html", function(xhr) { return document.createRange().createContextualFragment(xhr.responseText); }); d3-request-1.0.6/src/json.js000066400000000000000000000001771315262414100156140ustar00rootroot00000000000000import type from "./type"; export default type("application/json", function(xhr) { return JSON.parse(xhr.responseText); }); d3-request-1.0.6/src/request.js000066400000000000000000000107611315262414100163330ustar00rootroot00000000000000import {map} from "d3-collection"; import {dispatch} from "d3-dispatch"; export default function(url, callback) { var request, event = dispatch("beforesend", "progress", "load", "error"), mimeType, headers = map(), xhr = new XMLHttpRequest, user = null, password = null, response, responseType, timeout = 0; // If IE does not support CORS, use XDomainRequest. if (typeof XDomainRequest !== "undefined" && !("withCredentials" in xhr) && /^(http(s)?:)?\/\//.test(url)) xhr = new XDomainRequest; "onload" in xhr ? xhr.onload = xhr.onerror = xhr.ontimeout = respond : xhr.onreadystatechange = function(o) { xhr.readyState > 3 && respond(o); }; function respond(o) { var status = xhr.status, result; if (!status && hasResponse(xhr) || status >= 200 && status < 300 || status === 304) { if (response) { try { result = response.call(request, xhr); } catch (e) { event.call("error", request, e); return; } } else { result = xhr; } event.call("load", request, result); } else { event.call("error", request, o); } } xhr.onprogress = function(e) { event.call("progress", request, e); }; request = { header: function(name, value) { name = (name + "").toLowerCase(); if (arguments.length < 2) return headers.get(name); if (value == null) headers.remove(name); else headers.set(name, value + ""); return request; }, // If mimeType is non-null and no Accept header is set, a default is used. mimeType: function(value) { if (!arguments.length) return mimeType; mimeType = value == null ? null : value + ""; return request; }, // Specifies what type the response value should take; // for instance, arraybuffer, blob, document, or text. responseType: function(value) { if (!arguments.length) return responseType; responseType = value; return request; }, timeout: function(value) { if (!arguments.length) return timeout; timeout = +value; return request; }, user: function(value) { return arguments.length < 1 ? user : (user = value == null ? null : value + "", request); }, password: function(value) { return arguments.length < 1 ? password : (password = value == null ? null : value + "", request); }, // Specify how to convert the response content to a specific type; // changes the callback value on "load" events. response: function(value) { response = value; return request; }, // Alias for send("GET", …). get: function(data, callback) { return request.send("GET", data, callback); }, // Alias for send("POST", …). post: function(data, callback) { return request.send("POST", data, callback); }, // If callback is non-null, it will be used for error and load events. send: function(method, data, callback) { xhr.open(method, url, true, user, password); if (mimeType != null && !headers.has("accept")) headers.set("accept", mimeType + ",*/*"); if (xhr.setRequestHeader) headers.each(function(value, name) { xhr.setRequestHeader(name, value); }); if (mimeType != null && xhr.overrideMimeType) xhr.overrideMimeType(mimeType); if (responseType != null) xhr.responseType = responseType; if (timeout > 0) xhr.timeout = timeout; if (callback == null && typeof data === "function") callback = data, data = null; if (callback != null && callback.length === 1) callback = fixCallback(callback); if (callback != null) request.on("error", callback).on("load", function(xhr) { callback(null, xhr); }); event.call("beforesend", request, xhr); xhr.send(data == null ? null : data); return request; }, abort: function() { xhr.abort(); return request; }, on: function() { var value = event.on.apply(event, arguments); return value === event ? request : value; } }; if (callback != null) { if (typeof callback !== "function") throw new Error("invalid callback: " + callback); return request.get(callback); } return request; } function fixCallback(callback) { return function(error, xhr) { callback(error == null ? xhr : null); }; } function hasResponse(xhr) { var type = xhr.responseType; return type && type !== "text" ? xhr.response // null on error : xhr.responseText; // "" on error } d3-request-1.0.6/src/text.js000066400000000000000000000001551315262414100156230ustar00rootroot00000000000000import type from "./type"; export default type("text/plain", function(xhr) { return xhr.responseText; }); d3-request-1.0.6/src/tsv.js000066400000000000000000000001661315262414100154550ustar00rootroot00000000000000import {tsvParse} from "d3-dsv"; import dsv from "./dsv"; export default dsv("text/tab-separated-values", tsvParse); d3-request-1.0.6/src/type.js000066400000000000000000000005621315262414100156220ustar00rootroot00000000000000import request from "./request"; export default function(defaultMimeType, response) { return function(url, callback) { var r = request(url).mimeType(defaultMimeType).response(response); if (callback != null) { if (typeof callback !== "function") throw new Error("invalid callback: " + callback); return r.get(callback); } return r; }; } d3-request-1.0.6/src/xml.js000066400000000000000000000002561315262414100154410ustar00rootroot00000000000000import type from "./type"; export default type("application/xml", function(xhr) { var xml = xhr.responseXML; if (!xml) throw new Error("parse error"); return xml; }); d3-request-1.0.6/test/000077500000000000000000000000001315262414100144705ustar00rootroot00000000000000d3-request-1.0.6/test/XMLHttpRequest.js000066400000000000000000000032361315262414100177030ustar00rootroot00000000000000var fs = require("fs"); global.XMLHttpRequestProgressEvent = function XMLHttpRequestProgressEvent(target) { this.target = target; }; XMLHttpRequestProgressEvent.prototype = Object.create(Error); global.XMLHttpRequest = function XMLHttpRequest() { var that = this, info = that._info = {}, headers = {}, url; // TODO handle file system errors? that.readyState = 0; that.open = function(m, u, a) { info.method = m; info.url = u; info.async = a; that.readyState = 1; that.send = a ? read : readSync; }; that.setRequestHeader = function(n, v) { if (/^Accept$/i.test(n)) info.mimeType = v.split(/,/g)[0]; }; function read() { that.readyState = 2; fs.readFile(info.url, "binary", function(e, d) { if (e) { that.status = 404; // assumed } else { that.status = 200; that.responseText = d; that.responseXML = {_xml: d}; headers["Content-Length"] = d.length; } that.readyState = 4; XMLHttpRequest._last = that; if (that.onreadystatechange) that.onreadystatechange(new XMLHttpRequestProgressEvent(that)); }); } function readSync() { that.readyState = 2; try { var d = fs.readFileSync(info.url, "binary"); that.status = 200; that.responseText = d; that.responseXML = {_xml: d}; headers["Content-Length"] = d.length; } catch (e) { that.status = 404; // assumed } that.readyState = 4; XMLHttpRequest._last = that; if (that.onreadystatechange) that.onreadystatechange(new XMLHttpRequestProgressEvent(that)); } that.getResponseHeader = function(n) { return headers[n]; }; }; d3-request-1.0.6/test/csv-test.js000066400000000000000000000044271315262414100166050ustar00rootroot00000000000000require("./XMLHttpRequest"); var tape = require("tape"), request = require("../build/d3-request"), table = require("./table"); tape("csv(url, callback) makes an asynchronous GET request for a CSV file", function(test) { request.csv("test/data/sample.csv", function(error, data) { if (error) throw error; test.equal(XMLHttpRequest._last._info.url, "test/data/sample.csv"); test.equal(XMLHttpRequest._last._info.method, "GET"); test.equal(XMLHttpRequest._last._info.async, true); test.equal(XMLHttpRequest._last._info.mimeType, "text/csv"); test.deepEqual(data, table([{Hello: "42", World: "\"fish\""}], ["Hello", "World"])); test.end(); }); }); tape("csv(url, callback) is an alias csv(url).get(callback)", function(test) { request.csv("test/data/sample.csv").get(function(error, data) { if (error) throw error; test.equal(XMLHttpRequest._last._info.url, "test/data/sample.csv"); test.equal(XMLHttpRequest._last._info.method, "GET"); test.equal(XMLHttpRequest._last._info.async, true); test.equal(XMLHttpRequest._last._info.mimeType, "text/csv"); test.deepEqual(data, table([{Hello: "42", World: "\"fish\""}], ["Hello", "World"])); test.end(); }); }); tape("csv(url, row, callback) observes the specified row conversion function", function(test) { request.csv("test/data/sample.csv", function(d) { d.Hello = -d.Hello; return d; }, function(error, data) { if (error) throw error; test.deepEqual(data, table([{Hello: -42, World: "\"fish\""}], ["Hello", "World"])); test.end(); }); }); tape("csv(url, row, callback) is an alias for csv(url).row(row).get(callback)", function(test) { request.csv("test/data/sample.csv").row(function(d) { d.Hello = -d.Hello; return d; }).get(function(error, data) { if (error) throw error; test.deepEqual(data, table([{Hello: -42, World: "\"fish\""}], ["Hello", "World"])); test.end(); }); }); tape("csv(url).mimeType(type).get(callback) observes the specified mime type", function(test) { request.csv("test/data/sample.csv").mimeType("text/plain").get(function(error, data) { if (error) throw error; test.deepEqual(data, table([{Hello: "42", World: "\"fish\""}], ["Hello", "World"])); test.equal(XMLHttpRequest._last._info.mimeType, "text/plain"); test.end(); }); }); d3-request-1.0.6/test/data/000077500000000000000000000000001315262414100154015ustar00rootroot00000000000000d3-request-1.0.6/test/data/sample.csv000066400000000000000000000000321315262414100173720ustar00rootroot00000000000000Hello,World 42,"""fish""" d3-request-1.0.6/test/data/sample.json000066400000000000000000000000411315262414100175500ustar00rootroot00000000000000{ "message": "Hello, world!" } d3-request-1.0.6/test/data/sample.tsv000066400000000000000000000000321315262414100174130ustar00rootroot00000000000000Hello World 42 """fish""" d3-request-1.0.6/test/data/sample.txt000066400000000000000000000000161315262414100174200ustar00rootroot00000000000000Hello, world! d3-request-1.0.6/test/json-test.js000066400000000000000000000036511315262414100167610ustar00rootroot00000000000000require("./XMLHttpRequest"); var tape = require("tape"), request = require("../build/d3-request"); tape("json(url, callback) makes an asynchronous GET request for a JSON file", function(test) { request.json("test/data/sample.json", function(error, json) { if (error) throw error; test.equal(XMLHttpRequest._last._info.url, "test/data/sample.json"); test.equal(XMLHttpRequest._last._info.method, "GET"); test.equal(XMLHttpRequest._last._info.async, true); test.equal(XMLHttpRequest._last._info.mimeType, "application/json"); test.equal(XMLHttpRequest._last.readyState, 4); test.equal(XMLHttpRequest._last.status, 200); test.deepEqual(json, {message: "Hello, world!"}); test.end(); }); }); tape("json(url, callback) returns an error when given invalid JSON", function(test) { request.json("test/data/sample.tsv", function(error, json) { test.ok(error instanceof SyntaxError); test.end(); }); }); tape("json(url, callback) is an alias for json(url).get(callback)", function(test) { request.json("test/data/sample.json").get(function(error, json) { if (error) throw error; test.equal(XMLHttpRequest._last._info.url, "test/data/sample.json"); test.equal(XMLHttpRequest._last._info.method, "GET"); test.equal(XMLHttpRequest._last._info.async, true); test.equal(XMLHttpRequest._last._info.mimeType, "application/json"); test.equal(XMLHttpRequest._last.readyState, 4); test.equal(XMLHttpRequest._last.status, 200); test.deepEqual(json, {message: "Hello, world!"}); test.end(); }); }); tape("json(url).mimeType(type).get(callback) observes the specified mime type", function(test) { request.json("test/data/sample.json").mimeType("applicatin/json+test").get(function(error, json) { if (error) throw error; test.equal(XMLHttpRequest._last._info.mimeType, "applicatin/json+test"); test.deepEqual(json, {message: "Hello, world!"}); test.end(); }); }); d3-request-1.0.6/test/table.js000066400000000000000000000001271315262414100161150ustar00rootroot00000000000000module.exports = function(rows, columns) { rows.columns = columns; return rows; }; d3-request-1.0.6/test/text-test.js000066400000000000000000000032301315262414100167650ustar00rootroot00000000000000require("./XMLHttpRequest"); var tape = require("tape"), request = require("../build/d3-request"); tape("text(url, callback) makes an asynchronous GET request for a plain text file", function(test) { request.text("test/data/sample.txt", function(error, text) { if (error) throw error; test.equal(XMLHttpRequest._last._info.url, "test/data/sample.txt"); test.equal(XMLHttpRequest._last._info.method, "GET"); test.equal(XMLHttpRequest._last._info.async, true); test.equal(XMLHttpRequest._last._info.mimeType, "text/plain"); test.equal(XMLHttpRequest._last.readyState, 4); test.equal(XMLHttpRequest._last.status, 200); test.equal(text, "Hello, world!\n"); test.end(); }); }); tape("text(url, callback) is an alias for text(url).get(callback)", function(test) { request.text("test/data/sample.txt").get(function(error, text) { if (error) throw error; test.equal(XMLHttpRequest._last._info.url, "test/data/sample.txt"); test.equal(XMLHttpRequest._last._info.method, "GET"); test.equal(XMLHttpRequest._last._info.async, true); test.equal(XMLHttpRequest._last._info.mimeType, "text/plain"); test.equal(XMLHttpRequest._last.readyState, 4); test.equal(XMLHttpRequest._last.status, 200); test.equal(text, "Hello, world!\n"); test.end(); }); }); tape("text(url).mimeType(type).get(callback) observes the specified mime type", function(test) { request.text("test/data/sample.txt").mimeType("text/plain+special").get(function(error, text) { if (error) throw error; test.equal(XMLHttpRequest._last._info.mimeType, "text/plain+special"); test.equal(text, "Hello, world!\n"); test.end(); }); }); d3-request-1.0.6/test/tsv-test.js000066400000000000000000000044711315262414100166250ustar00rootroot00000000000000require("./XMLHttpRequest"); var tape = require("tape"), request = require("../build/d3-request"), table = require("./table"); tape("tsv(url, callback) makes an asynchronous GET request for a TSV file", function(test) { request.tsv("test/data/sample.tsv", function(error, data) { if (error) throw error; test.equal(XMLHttpRequest._last._info.url, "test/data/sample.tsv"); test.equal(XMLHttpRequest._last._info.method, "GET"); test.equal(XMLHttpRequest._last._info.async, true); test.equal(XMLHttpRequest._last._info.mimeType, "text/tab-separated-values"); test.deepEqual(data, table([{Hello: "42", World: "\"fish\""}], ["Hello", "World"])); test.end(); }); }); tape("tsv(url, callback) is an alias tsv(url).get(callback)", function(test) { request.tsv("test/data/sample.tsv").get(function(error, data) { if (error) throw error; test.equal(XMLHttpRequest._last._info.url, "test/data/sample.tsv"); test.equal(XMLHttpRequest._last._info.method, "GET"); test.equal(XMLHttpRequest._last._info.async, true); test.equal(XMLHttpRequest._last._info.mimeType, "text/tab-separated-values"); test.deepEqual(data, table([{Hello: "42", World: "\"fish\""}], ["Hello", "World"])); test.end(); }); }); tape("tsv(url, row, callback) observes the specified row conversion function", function(test) { request.tsv("test/data/sample.tsv", function(d) { d.Hello = -d.Hello; return d; }, function(error, data) { if (error) throw error; test.deepEqual(data, table([{Hello: -42, World: "\"fish\""}], ["Hello", "World"])); test.end(); }); }); tape("tsv(url, row, callback) is an alias for tsv(url).row(row).get(callback)", function(test) { request.tsv("test/data/sample.tsv").row(function(d) { d.Hello = -d.Hello; return d; }).get(function(error, data) { if (error) throw error; test.deepEqual(data, table([{Hello: -42, World: "\"fish\""}], ["Hello", "World"])); test.end(); }); }); tape("tsv(url).mimeType(type).get(callback) observes the specified mime type", function(test) { request.tsv("test/data/sample.tsv").mimeType("text/plain").get(function(error, data) { if (error) throw error; test.deepEqual(data, table([{Hello: "42", World: "\"fish\""}], ["Hello", "World"])); test.equal(XMLHttpRequest._last._info.mimeType, "text/plain"); test.end(); }); }); d3-request-1.0.6/test/xhr-test.js000066400000000000000000000060031315262414100166030ustar00rootroot00000000000000require("./XMLHttpRequest"); var tape = require("tape"), request = require("../build/d3-request"); tape("request(url, callback) makes an asynchronous GET request with the default mime type", function(test) { request.request("test/data/sample.txt", function(error, request) { if (error) throw error; test.equal(request._info.url, "test/data/sample.txt"); test.equal(request._info.method, "GET"); test.equal(request._info.async, true); test.equal(request._info.mimeType, undefined); test.equal(request.responseText, "Hello, world!\n"); test.equal(request.readyState, 4); test.equal(request.status, 200); test.end(); }); }); tape("request(url, callback) invokes the callback with an error if the request fails", function(test) { request.request("//does/not/exist", function(error, request) { test.ok(error instanceof XMLHttpRequestProgressEvent); test.ok(error.target instanceof XMLHttpRequest); test.equal(request, undefined); test.end(); }); }); tape("request(url, callback) is an alias for request(url).get(callback)", function(test) { request.request("test/data/sample.txt").get(function(error, request) { if (error) throw error; test.equal(request._info.url, "test/data/sample.txt"); test.equal(request._info.method, "GET"); test.equal(request._info.async, true); test.equal(request.responseText, "Hello, world!\n"); test.equal(request._info.mimeType, undefined); test.equal(request.readyState, 4); test.equal(request.status, 200); test.end(); }); }); tape("request(url, nonFunction) throws an error", function(test) { test.throws(function() { request.request("test/data/sample.txt", "fail"); }, /invalid callback/); test.throws(function() { request.request("test/data/sample.txt", false); }, /invalid callback/); test.throws(function() { request.request("test/data/sample.txt", 0); }, /invalid callback/); test.end(); }); tape("request(url).mimeType(type).get(callback) observes the specified mime type", function(test) { request.request("test/data/sample.txt").mimeType("text/plain").get(function(error, request) { if (error) throw error; test.equal(request._info.mimeType, "text/plain"); test.equal(request.responseText, "Hello, world!\n"); test.end(); }); }); tape("request(url).get(null, nonFunction) throws an error", function(test) { test.throws(function() { request.request("test/data/sample.txt").get(null, "fail"); }, /invalid callback/); test.throws(function() { request.request("test/data/sample.txt").get(null, false); }, /invalid callback/); test.throws(function() { request.request("test/data/sample.txt").get(null, 0); }, /invalid callback/); test.end(); }); tape("request(url).on(\"beforesend\", listener).get() invokes the listener before sending", function(test) { var r = request.request("test/data/sample.txt"); r.on("beforesend", function(request) { test.equal(this, r); test.ok(request instanceof XMLHttpRequest); test.equal(request.readyState, 1); test.end(); }); r.get(); });