Skip to content

Commit

Permalink
fix(core): handle format's responseType in UrlSource
Browse files Browse the repository at this point in the history
Needed by Arrow, which is binary data.
  • Loading branch information
tuner committed Apr 30, 2024
1 parent aceb97c commit 14d6609
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
9 changes: 9 additions & 0 deletions packages/core/src/data/sources/dataUtils.js
@@ -1,3 +1,4 @@
import { formats } from "vega-loader";
import { withoutExprRef } from "../../view/paramMediator.js";
import { isInlineData } from "./inlineSource.js";

Expand Down Expand Up @@ -29,6 +30,14 @@ export function getFormat(params) {
return format;
}

/**
* @param {string} type
* @returns {string}
*/
export function responseType(type) {
return formats(type)?.responseType ?? "text";
}

/**
* @param {string | string[]} url
*/
Expand Down
19 changes: 12 additions & 7 deletions packages/core/src/data/sources/urlSource.js
@@ -1,5 +1,5 @@
import { read } from "vega-loader";
import { getFormat } from "./dataUtils.js";
import { getFormat, responseType } from "./dataUtils.js";
import DataSource from "./dataSource.js";
import {
activateExprRefProps,
Expand Down Expand Up @@ -40,6 +40,9 @@ export default class UrlSource extends DataSource {
/** @type {string[]} */
const urls = Array.isArray(url) ? url : [url];

const format = getFormat(this.params);
const type = responseType(format.type);

if (urls.length === 0 || !urls[0]) {
this.reset();
this.complete();
Expand All @@ -54,8 +57,11 @@ export default class UrlSource extends DataSource {
if (!result.ok) {
throw new Error(`${result.status} ${result.statusText}`);
}
// TODO: what about binary data (for arrow), etc?
return result.text();
// @ts-ignore
return typeof result[type] == "function"
? // @ts-ignore
result[type]()
: result.text();
} catch (e) {
throw new Error(
`Could not load data: ${url}. Reason: ${e.message}`
Expand All @@ -64,13 +70,13 @@ export default class UrlSource extends DataSource {
};

/**
* @param {string} text
* @param {any} content
* @param {string} [url]
*/
const readAndParse = (text, url) => {
const readAndParse = (content, url) => {
try {
/** @type {any[]} */
const data = read(text, getFormat(this.params));
const data = read(content, format);
this.beginBatch({ type: "file", url: url });
for (const d of data) {
this._propagate(d);
Expand All @@ -89,7 +95,6 @@ export default class UrlSource extends DataSource {
} catch (e) {
this.setLoadingStatus("error", e.message);
}

this.complete();
}
}

0 comments on commit 14d6609

Please sign in to comment.