Skip to content

Commit

Permalink
fix(:mag:): allow node.js clients to access streams
Browse files Browse the repository at this point in the history
AFFECTS PACKAGES:
@esri/arcgis-rest-request

ISSUES CLOSED: #373
  • Loading branch information
jgravois committed Oct 29, 2018
1 parent afb8274 commit 95a35ce
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ packages/arcgis-rest-portal/

#
test.html
test.js

#
demos/test/*
25 changes: 21 additions & 4 deletions packages/arcgis-rest-request/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,16 +271,16 @@ export function request(
return response.text();
case "text":
return response.text();
/* istanbul ignore next blob responses are difficult to make cross platform we will just have to trust the isomorphic fetch will do its job */
/* istanbul ignore next blob responses are difficult to make cross platform we will just have to trust that isomorphic fetch will do its job */
case "image":
return response.blob();
return polymorphicBlob(response);
/* istanbul ignore next */
case "zip":
return response.blob();
return polymorphicBlob(response);
/* istanbul ignore next */
default:
// hopefully we never need to handle JSON payloads when no f= parameter is set
return response.blob();
return polymorphicBlob(response);
}
})
.then(data => {
Expand All @@ -291,3 +291,20 @@ export function request(
}
});
}

function polymorphicBlob(response: any) {
/* istanbul ignore else
fetch in the browser includes support for response.blob()
*/
if (typeof window !== "undefined") {
return response.blob();
} else {
/*
node-fetch recommends streaming response.body instead
https://github.com/bitinn/node-fetch/issues/78
karma coverage only measures browser tests
*/
return response;
}
}
Binary file added packages/arcgis-rest-request/test/mocks/foo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions packages/arcgis-rest-request/test/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ import { ArcGISOnlineError } from "./mocks/errors";
import { WebMapAsText, WebMapAsJSON } from "./mocks/webmap";
import { GeoJSONFeatureCollection } from "./mocks/geojson-feature-collection";

function imageResponse() {
if (typeof File !== "undefined" && File) {
return new File(["foo"], "foo.png", { type: "image/png" });
} else {
const fs = require("fs");
return fs.createReadStream(
"./packages/arcgis-rest-request/test/mocks/foo.png"
);
}
}

describe("request()", () => {
afterEach(fetchMock.restore);

Expand Down Expand Up @@ -91,6 +102,36 @@ describe("request()", () => {
fail(e);
});
});
it("should make a basic POST request for binary data", done => {
fetchMock.once("*", imageResponse());

request(
"https://server/arcgis/rest/services/Custom/ImageServer/exportImage",
{
params: {
bbox: "-8529110,4764085,-8528610,4764085",
bboxSR: "102100",
size: "400,400",
imageSR: "102100",
format: "png8",
f: "image"
}
}
)
.then(response => {
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://server/arcgis/rest/services/Custom/ImageServer/exportImage"
);
expect(options.method).toBe("POST");
expect(options.body).toContain("f=image");
// to do: introspect blob response?
done();
})
.catch(e => {
fail(e);
});
});

it("should make a basic GET request for geojson", done => {
fetchMock.once("*", GeoJSONFeatureCollection);
Expand Down

0 comments on commit 95a35ce

Please sign in to comment.