flagpole
Version:
Simple and fast DOM integration, headless or headful browser, and REST API testing framework.
48 lines • 1.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchImageWithNeedle = void 0;
const httpresponse_1 = require("../httpresponse");
const needle = require("needle");
const needle_1 = require("./needle");
const image_probe_1 = require("@zerodeps/image-probe");
const fetchImageWithNeedle = (request, opts) => {
return new Promise((resolve, reject) => {
const stream = needle.request("get", request.uri || "/", null, needle_1.getNeedleOptions(request));
if (opts === null || opts === void 0 ? void 0 : opts.redirect) {
stream.on("redirect", opts.redirect);
}
const response = {
statusCode: 0,
length: 0,
url: request.uri || "",
headers: {},
imageData: {
width: 0,
height: 0,
type: "",
mimeType: "",
},
};
stream
.on("header", (statusCode, headers) => {
response.statusCode = statusCode;
response.headers = headers;
response.length = Number(headers["content-length"]);
})
.on("readable", () => {
const chunk = stream.read(512);
if (chunk) {
response.imageData =
image_probe_1.ImageProbe.fromBuffer(chunk) || response.imageData;
}
stream.pause();
try {
stream.destroy();
}
catch (_a) { }
resolve(httpresponse_1.HttpResponse.fromProbeImage(response));
});
});
};
exports.fetchImageWithNeedle = fetchImageWithNeedle;
//# sourceMappingURL=image.js.map