pax_global_header 0000666 0000000 0000000 00000000064 13152624141 0014511 g ustar 00root root 0000000 0000000 52 comment=38567dcc5dc7050a6294f8e1f807e9eb1e790cbe
d3-request-1.0.6/ 0000775 0000000 0000000 00000000000 13152624141 0013511 5 ustar 00root root 0000000 0000000 d3-request-1.0.6/.eslintrc 0000664 0000000 0000000 00000000177 13152624141 0015342 0 ustar 00root root 0000000 0000000 parserOptions:
sourceType: module
env:
browser: true
extends:
"eslint:recommended"
rules:
no-cond-assign: 0
d3-request-1.0.6/.gitignore 0000664 0000000 0000000 00000000100 13152624141 0015470 0 ustar 00root root 0000000 0000000 *.sublime-workspace
.DS_Store
build/
node_modules
npm-debug.log
d3-request-1.0.6/.npmignore 0000664 0000000 0000000 00000000036 13152624141 0015507 0 ustar 00root root 0000000 0000000 *.sublime-*
build/*.zip
test/
d3-request-1.0.6/LICENSE 0000664 0000000 0000000 00000002703 13152624141 0014520 0 ustar 00root root 0000000 0000000 Copyright 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.md 0000664 0000000 0000000 00000041001 13152624141 0014764 0 ustar 00root root 0000000 0000000 # 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/ 0000775 0000000 0000000 00000000000 13152624141 0014261 5 ustar 00root root 0000000 0000000 d3-request-1.0.6/bin/rollup-node 0000775 0000000 0000000 00000001346 13152624141 0016453 0 ustar 00root root 0000000 0000000 #!/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-project 0000664 0000000 0000000 00000000271 13152624141 0020533 0 ustar 00root root 0000000 0000000 {
"folders": [
{
"path": ".",
"file_exclude_patterns": [
"*.sublime-workspace"
],
"folder_exclude_patterns": [
"build"
]
}
]
}
d3-request-1.0.6/index.js 0000664 0000000 0000000 00000000464 13152624141 0015162 0 ustar 00root root 0000000 0000000 export {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.json 0000664 0000000 0000000 00000003147 13152624141 0016004 0 ustar 00root root 0000000 0000000 {
"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.js 0000664 0000000 0000000 00000000466 13152624141 0016636 0 ustar 00root root 0000000 0000000 export 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/ 0000775 0000000 0000000 00000000000 13152624141 0014300 5 ustar 00root root 0000000 0000000 d3-request-1.0.6/src/csv.js 0000664 0000000 0000000 00000000145 13152624141 0015431 0 ustar 00root root 0000000 0000000 import {csvParse} from "d3-dsv";
import dsv from "./dsv";
export default dsv("text/csv", csvParse);
d3-request-1.0.6/src/dsv.js 0000664 0000000 0000000 00000001005 13152624141 0015426 0 ustar 00root root 0000000 0000000 import 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.js 0000664 0000000 0000000 00000000235 13152624141 0015602 0 ustar 00root root 0000000 0000000 import type from "./type";
export default type("text/html", function(xhr) {
return document.createRange().createContextualFragment(xhr.responseText);
});
d3-request-1.0.6/src/json.js 0000664 0000000 0000000 00000000177 13152624141 0015614 0 ustar 00root root 0000000 0000000 import type from "./type";
export default type("application/json", function(xhr) {
return JSON.parse(xhr.responseText);
});
d3-request-1.0.6/src/request.js 0000664 0000000 0000000 00000010761 13152624141 0016333 0 ustar 00root root 0000000 0000000 import {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.js 0000664 0000000 0000000 00000000155 13152624141 0015623 0 ustar 00root root 0000000 0000000 import type from "./type";
export default type("text/plain", function(xhr) {
return xhr.responseText;
});
d3-request-1.0.6/src/tsv.js 0000664 0000000 0000000 00000000166 13152624141 0015455 0 ustar 00root root 0000000 0000000 import {tsvParse} from "d3-dsv";
import dsv from "./dsv";
export default dsv("text/tab-separated-values", tsvParse);
d3-request-1.0.6/src/type.js 0000664 0000000 0000000 00000000562 13152624141 0015622 0 ustar 00root root 0000000 0000000 import 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.js 0000664 0000000 0000000 00000000256 13152624141 0015441 0 ustar 00root root 0000000 0000000 import 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/ 0000775 0000000 0000000 00000000000 13152624141 0014470 5 ustar 00root root 0000000 0000000 d3-request-1.0.6/test/XMLHttpRequest.js 0000664 0000000 0000000 00000003236 13152624141 0017703 0 ustar 00root root 0000000 0000000 var 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.js 0000664 0000000 0000000 00000004427 13152624141 0016605 0 ustar 00root root 0000000 0000000 require("./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/ 0000775 0000000 0000000 00000000000 13152624141 0015401 5 ustar 00root root 0000000 0000000 d3-request-1.0.6/test/data/sample.csv 0000664 0000000 0000000 00000000032 13152624141 0017372 0 ustar 00root root 0000000 0000000 Hello,World
42,"""fish"""
d3-request-1.0.6/test/data/sample.json 0000664 0000000 0000000 00000000041 13152624141 0017550 0 ustar 00root root 0000000 0000000 {
"message": "Hello, world!"
}
d3-request-1.0.6/test/data/sample.tsv 0000664 0000000 0000000 00000000032 13152624141 0017413 0 ustar 00root root 0000000 0000000 Hello World
42 """fish"""
d3-request-1.0.6/test/data/sample.txt 0000664 0000000 0000000 00000000016 13152624141 0017420 0 ustar 00root root 0000000 0000000 Hello, world!
d3-request-1.0.6/test/json-test.js 0000664 0000000 0000000 00000003651 13152624141 0016761 0 ustar 00root root 0000000 0000000 require("./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.js 0000664 0000000 0000000 00000000127 13152624141 0016115 0 ustar 00root root 0000000 0000000 module.exports = function(rows, columns) {
rows.columns = columns;
return rows;
};
d3-request-1.0.6/test/text-test.js 0000664 0000000 0000000 00000003230 13152624141 0016765 0 ustar 00root root 0000000 0000000 require("./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.js 0000664 0000000 0000000 00000004471 13152624141 0016625 0 ustar 00root root 0000000 0000000 require("./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.js 0000664 0000000 0000000 00000006003 13152624141 0016603 0 ustar 00root root 0000000 0000000 require("./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();
});