@vitessce/export-utils 2.0.3-beta.0 → 2.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.mjs +2113 -0
- package/{dist → dist-tsc}/index.js +0 -0
- package/package.json +5 -5
- package/src/export-utils.js +79 -0
- package/src/export-utils.test.js +73 -0
- package/src/index.js +4 -0
- package/dist/export-utils.js +0 -70
- package/dist/export-utils.test.js +0 -63
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vitessce/export-utils",
|
|
3
|
-
"version": "2.0.3
|
|
3
|
+
"version": "2.0.3",
|
|
4
4
|
"author": "Gehlenborg Lab",
|
|
5
5
|
"homepage": "http://vitessce.io",
|
|
6
6
|
"repository": {
|
|
@@ -8,17 +8,17 @@
|
|
|
8
8
|
"url": "git+https://github.com/vitessce/vitessce.git"
|
|
9
9
|
},
|
|
10
10
|
"license": "MIT",
|
|
11
|
-
"main": "dist/index.
|
|
11
|
+
"main": "dist/index.mjs",
|
|
12
12
|
"files": [
|
|
13
|
-
"dist"
|
|
13
|
+
"dist",
|
|
14
|
+
"src"
|
|
14
15
|
],
|
|
15
16
|
"dependencies": {
|
|
16
17
|
"bowser": "^2.11.0",
|
|
17
18
|
"lz-string": "^1.4.4"
|
|
18
19
|
},
|
|
19
20
|
"scripts": {
|
|
20
|
-
"
|
|
21
|
-
"build": "tsc",
|
|
21
|
+
"bundle": "pnpm exec vite build -c ../../../scripts/vite.config.js",
|
|
22
22
|
"test": "pnpm exec vitest --run -r ../../../ --dir ."
|
|
23
23
|
}
|
|
24
24
|
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import Bowser from 'bowser';
|
|
2
|
+
import { compressToEncodedURIComponent, decompressFromEncodedURIComponent } from 'lz-string';
|
|
3
|
+
|
|
4
|
+
const CURRENT_VERSION = '0.0.1';
|
|
5
|
+
const VITESSCE_CONF_QUERY_STRING = 'vitessce_conf';
|
|
6
|
+
const VERSION_QUERY_STRING = 'vitessce_conf_version';
|
|
7
|
+
const LENGTH_QUERY_STRING = 'vitessce_conf_length';
|
|
8
|
+
|
|
9
|
+
function sniffBrowser() {
|
|
10
|
+
const { browser } = Bowser.parse(window.navigator.userAgent);
|
|
11
|
+
return browser.name;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// https://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers
|
|
15
|
+
const MAX_BROWSER_URL_LENGTHS = {
|
|
16
|
+
Chrome: 32779,
|
|
17
|
+
'Internet Explorer': 2047,
|
|
18
|
+
Edge: 2047,
|
|
19
|
+
Safari: 65000,
|
|
20
|
+
Firefox: 65000,
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export default class CompressedConfLengthError {
|
|
24
|
+
constructor(message) {
|
|
25
|
+
this.message = message;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Encode a configuration as url params with a version and an lz-compressed conf.
|
|
31
|
+
* @param {Object} params
|
|
32
|
+
* @param {Object} params.conf Previous scope names.
|
|
33
|
+
* @param {function} params.onOverMaximumUrlLength Callback for when new url
|
|
34
|
+
* is over max length for your browser - takes two arguments: { message, willWorkOn }
|
|
35
|
+
* for the error message and the browsers for which the url will work (optional).
|
|
36
|
+
* @returns {string} The new params like
|
|
37
|
+
* vitessce_conf_length=10&vitessce_conf_version=0.0.1&vitessce_conf=fksdfasdfjkl
|
|
38
|
+
*/
|
|
39
|
+
export function encodeConfInUrl({
|
|
40
|
+
conf,
|
|
41
|
+
onOverMaximumUrlLength = () => { },
|
|
42
|
+
}) {
|
|
43
|
+
const compressedConf = compressToEncodedURIComponent(JSON.stringify(conf));
|
|
44
|
+
const newParams = `${LENGTH_QUERY_STRING}=${compressedConf.length}&${VERSION_QUERY_STRING}=${CURRENT_VERSION}&${VITESSCE_CONF_QUERY_STRING}=${compressedConf}`;
|
|
45
|
+
const browser = sniffBrowser();
|
|
46
|
+
const maxLength = MAX_BROWSER_URL_LENGTHS[browser];
|
|
47
|
+
if (newParams.length > maxLength) {
|
|
48
|
+
const willWorkOn = Object.entries(MAX_BROWSER_URL_LENGTHS)
|
|
49
|
+
.filter(entry => entry[1] > newParams.length)
|
|
50
|
+
.map(entry => entry[0]);
|
|
51
|
+
const message = `Configuration is ${compressedConf.length} characters; max URL for ${browser} is ${maxLength}: it will work on ${willWorkOn.join(', ') || 'no browser'}.`;
|
|
52
|
+
console.error(message);
|
|
53
|
+
onOverMaximumUrlLength({ message, willWorkOn });
|
|
54
|
+
}
|
|
55
|
+
return newParams;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Decode URL params to a Vitessce configuration.
|
|
60
|
+
* The URL params must have version and vitessce_conf params,
|
|
61
|
+
* like vitessce_conf_length=10&vitessce_conf_version=0.0.1&vitessce_conf=fksdfasdfjkl.
|
|
62
|
+
* @param {Object} queryString The URL params,
|
|
63
|
+
* like vitessce_conf_length=10&vitessce_conf_version=0.0.1&vitessce_conf=fksdfasdfjkl.
|
|
64
|
+
* @returns {string} A vitessce configuration.
|
|
65
|
+
*/
|
|
66
|
+
export function decodeURLParamsToConf(queryString) {
|
|
67
|
+
const params = new URLSearchParams(queryString.replace('#', '&'));
|
|
68
|
+
const compressedConfString = params.get(VITESSCE_CONF_QUERY_STRING);
|
|
69
|
+
const expectedConfLength = Number(params.get(LENGTH_QUERY_STRING));
|
|
70
|
+
if (expectedConfLength !== compressedConfString.length) {
|
|
71
|
+
throw new CompressedConfLengthError(`Compressed conf length (${compressedConfString.length}) != expected (${expectedConfLength}). URL truncated?`);
|
|
72
|
+
}
|
|
73
|
+
const version = params.get(VERSION_QUERY_STRING);
|
|
74
|
+
if (version === CURRENT_VERSION) {
|
|
75
|
+
const conf = JSON.parse(decompressFromEncodedURIComponent(compressedConfString));
|
|
76
|
+
return conf;
|
|
77
|
+
}
|
|
78
|
+
throw new Error('Unrecognized URL Param Version');
|
|
79
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import {
|
|
2
|
+
encodeConfInUrl,
|
|
3
|
+
decodeURLParamsToConf,
|
|
4
|
+
} from './export-utils';
|
|
5
|
+
|
|
6
|
+
const fakeConfig = {
|
|
7
|
+
version: '0.1.0',
|
|
8
|
+
public: false,
|
|
9
|
+
layers: [
|
|
10
|
+
{
|
|
11
|
+
name: 'cells',
|
|
12
|
+
type: 'CELLS',
|
|
13
|
+
fileType: 'cells.json',
|
|
14
|
+
url: 'http://example.com/cells.json',
|
|
15
|
+
},
|
|
16
|
+
],
|
|
17
|
+
name: 'Just scatterplot',
|
|
18
|
+
staticLayout: [
|
|
19
|
+
{
|
|
20
|
+
component: 'scatterplot',
|
|
21
|
+
props: {
|
|
22
|
+
mapping: 't-SNE',
|
|
23
|
+
view: {
|
|
24
|
+
zoom: 0.75,
|
|
25
|
+
target: [0, 0, 0],
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
x: 0,
|
|
29
|
+
y: 0,
|
|
30
|
+
w: 12,
|
|
31
|
+
h: 2,
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
function makeLongString(length) {
|
|
37
|
+
let result = '';
|
|
38
|
+
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
39
|
+
const charactersLength = characters.length;
|
|
40
|
+
// eslint-disable-next-line no-plusplus
|
|
41
|
+
for (let i = 0; i < length; i++) {
|
|
42
|
+
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
|
43
|
+
}
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
describe('src/app/export-utils.js', () => {
|
|
48
|
+
describe('encodeConfInUrl', () => {
|
|
49
|
+
it('encodes a view config for a URL', () => {
|
|
50
|
+
const urlParams1 = encodeConfInUrl({ conf: {} });
|
|
51
|
+
expect(urlParams1).toEqual('vitessce_conf_length=5&vitessce_conf_version=0.0.1&vitessce_conf=N4XyA');
|
|
52
|
+
|
|
53
|
+
const urlParams2 = encodeConfInUrl({ conf: fakeConfig });
|
|
54
|
+
expect(urlParams2).toEqual('vitessce_conf_length=327&vitessce_conf_version=0.0.1&vitessce_conf=N4IgbgpgTgzglgewHYgFwgAwDoCMWMgA0IADgK4BGANnAMZoBmAhlTBMVUwJ7QxoDaoJEwC2ENCFoQqrIiAAuXEuPQBhAKIAZTQGU5DOFQgAVJSsnTWWAFYxkcslCoSAFvPklUAei8QAHqIkRli0CCJeUjIwNnYoAL4AusTCYhIAUmQw8gAEMLRM7tBBCPJyWQV0mtwIZKWogpJhJMgQSHUgeQXyRVQlciRQCCR8qKAiTCQkcEgA5hLyALQ6AHLqcmBwEADuaKAAXghhaNgA7ACsxPJMUDMQdfwYhI8YCXFxxH7HxFxfIDuoOAATMQXGhAYk4kA');
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('Calls onOverMaximumUrlLength for conf too long', () => {
|
|
58
|
+
let browsers;
|
|
59
|
+
// eslint-disable-next-line no-return-assign
|
|
60
|
+
const onOverMaximumUrlLength = ({ willWorkOn }) => browsers = willWorkOn;
|
|
61
|
+
encodeConfInUrl({ conf: { foo: makeLongString(69000) }, onOverMaximumUrlLength });
|
|
62
|
+
expect(browsers).toEqual([]);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('decodes a view config for a URL', () => {
|
|
66
|
+
const viewConfig1 = decodeURLParamsToConf('vitessce_conf_length=5&vitessce_conf_version=0.0.1&vitessce_conf=N4XyA');
|
|
67
|
+
expect(viewConfig1).toEqual({});
|
|
68
|
+
|
|
69
|
+
const viewConfig2 = decodeURLParamsToConf('vitessce_conf_length=327&vitessce_conf_version=0.0.1&vitessce_conf=N4IgbgpgTgzglgewHYgFwgAwDoCMWMgA0IADgK4BGANnAMZoBmAhlTBMVUwJ7QxoDaoJEwC2ENCFoQqrIiAAuXEuPQBhAKIAZTQGU5DOFQgAVJSsnTWWAFYxkcslCoSAFvPklUAei8QAHqIkRli0CCJeUjIwNnYoAL4AusTCYhIAUmQw8gAEMLRM7tBBCPJyWQV0mtwIZKWogpJhJMgQSHUgeQXyRVQlciRQCCR8qKAiTCQkcEgA5hLyALQ6AHLqcmBwEADuaKAAXghhaNgA7ACsxPJMUDMQdfwYhI8YCXFxxH7HxFxfIDuoOAATMQXGhAYk4kA');
|
|
70
|
+
expect(viewConfig2).toEqual(fakeConfig);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
});
|
package/src/index.js
ADDED
package/dist/export-utils.js
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import Bowser from 'bowser';
|
|
2
|
-
import { compressToEncodedURIComponent, decompressFromEncodedURIComponent } from 'lz-string';
|
|
3
|
-
const CURRENT_VERSION = '0.0.1';
|
|
4
|
-
const VITESSCE_CONF_QUERY_STRING = 'vitessce_conf';
|
|
5
|
-
const VERSION_QUERY_STRING = 'vitessce_conf_version';
|
|
6
|
-
const LENGTH_QUERY_STRING = 'vitessce_conf_length';
|
|
7
|
-
function sniffBrowser() {
|
|
8
|
-
const { browser } = Bowser.parse(window.navigator.userAgent);
|
|
9
|
-
return browser.name;
|
|
10
|
-
}
|
|
11
|
-
// https://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers
|
|
12
|
-
const MAX_BROWSER_URL_LENGTHS = {
|
|
13
|
-
Chrome: 32779,
|
|
14
|
-
'Internet Explorer': 2047,
|
|
15
|
-
Edge: 2047,
|
|
16
|
-
Safari: 65000,
|
|
17
|
-
Firefox: 65000,
|
|
18
|
-
};
|
|
19
|
-
export default class CompressedConfLengthError {
|
|
20
|
-
constructor(message) {
|
|
21
|
-
this.message = message;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Encode a configuration as url params with a version and an lz-compressed conf.
|
|
26
|
-
* @param {Object} params
|
|
27
|
-
* @param {Object} params.conf Previous scope names.
|
|
28
|
-
* @param {function} params.onOverMaximumUrlLength Callback for when new url
|
|
29
|
-
* is over max length for your browser - takes two arguments: { message, willWorkOn }
|
|
30
|
-
* for the error message and the browsers for which the url will work (optional).
|
|
31
|
-
* @returns {string} The new params like
|
|
32
|
-
* vitessce_conf_length=10&vitessce_conf_version=0.0.1&vitessce_conf=fksdfasdfjkl
|
|
33
|
-
*/
|
|
34
|
-
export function encodeConfInUrl({ conf, onOverMaximumUrlLength = () => { }, }) {
|
|
35
|
-
const compressedConf = compressToEncodedURIComponent(JSON.stringify(conf));
|
|
36
|
-
const newParams = `${LENGTH_QUERY_STRING}=${compressedConf.length}&${VERSION_QUERY_STRING}=${CURRENT_VERSION}&${VITESSCE_CONF_QUERY_STRING}=${compressedConf}`;
|
|
37
|
-
const browser = sniffBrowser();
|
|
38
|
-
const maxLength = MAX_BROWSER_URL_LENGTHS[browser];
|
|
39
|
-
if (newParams.length > maxLength) {
|
|
40
|
-
const willWorkOn = Object.entries(MAX_BROWSER_URL_LENGTHS)
|
|
41
|
-
.filter(entry => entry[1] > newParams.length)
|
|
42
|
-
.map(entry => entry[0]);
|
|
43
|
-
const message = `Configuration is ${compressedConf.length} characters; max URL for ${browser} is ${maxLength}: it will work on ${willWorkOn.join(', ') || 'no browser'}.`;
|
|
44
|
-
console.error(message);
|
|
45
|
-
onOverMaximumUrlLength({ message, willWorkOn });
|
|
46
|
-
}
|
|
47
|
-
return newParams;
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Decode URL params to a Vitessce configuration.
|
|
51
|
-
* The URL params must have version and vitessce_conf params,
|
|
52
|
-
* like vitessce_conf_length=10&vitessce_conf_version=0.0.1&vitessce_conf=fksdfasdfjkl.
|
|
53
|
-
* @param {Object} queryString The URL params,
|
|
54
|
-
* like vitessce_conf_length=10&vitessce_conf_version=0.0.1&vitessce_conf=fksdfasdfjkl.
|
|
55
|
-
* @returns {string} A vitessce configuration.
|
|
56
|
-
*/
|
|
57
|
-
export function decodeURLParamsToConf(queryString) {
|
|
58
|
-
const params = new URLSearchParams(queryString.replace('#', '&'));
|
|
59
|
-
const compressedConfString = params.get(VITESSCE_CONF_QUERY_STRING);
|
|
60
|
-
const expectedConfLength = Number(params.get(LENGTH_QUERY_STRING));
|
|
61
|
-
if (expectedConfLength !== compressedConfString.length) {
|
|
62
|
-
throw new CompressedConfLengthError(`Compressed conf length (${compressedConfString.length}) != expected (${expectedConfLength}). URL truncated?`);
|
|
63
|
-
}
|
|
64
|
-
const version = params.get(VERSION_QUERY_STRING);
|
|
65
|
-
if (version === CURRENT_VERSION) {
|
|
66
|
-
const conf = JSON.parse(decompressFromEncodedURIComponent(compressedConfString));
|
|
67
|
-
return conf;
|
|
68
|
-
}
|
|
69
|
-
throw new Error('Unrecognized URL Param Version');
|
|
70
|
-
}
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { encodeConfInUrl, decodeURLParamsToConf, } from './export-utils';
|
|
2
|
-
const fakeConfig = {
|
|
3
|
-
version: '0.1.0',
|
|
4
|
-
public: false,
|
|
5
|
-
layers: [
|
|
6
|
-
{
|
|
7
|
-
name: 'cells',
|
|
8
|
-
type: 'CELLS',
|
|
9
|
-
fileType: 'cells.json',
|
|
10
|
-
url: 'http://example.com/cells.json',
|
|
11
|
-
},
|
|
12
|
-
],
|
|
13
|
-
name: 'Just scatterplot',
|
|
14
|
-
staticLayout: [
|
|
15
|
-
{
|
|
16
|
-
component: 'scatterplot',
|
|
17
|
-
props: {
|
|
18
|
-
mapping: 't-SNE',
|
|
19
|
-
view: {
|
|
20
|
-
zoom: 0.75,
|
|
21
|
-
target: [0, 0, 0],
|
|
22
|
-
},
|
|
23
|
-
},
|
|
24
|
-
x: 0,
|
|
25
|
-
y: 0,
|
|
26
|
-
w: 12,
|
|
27
|
-
h: 2,
|
|
28
|
-
},
|
|
29
|
-
],
|
|
30
|
-
};
|
|
31
|
-
function makeLongString(length) {
|
|
32
|
-
let result = '';
|
|
33
|
-
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
34
|
-
const charactersLength = characters.length;
|
|
35
|
-
// eslint-disable-next-line no-plusplus
|
|
36
|
-
for (let i = 0; i < length; i++) {
|
|
37
|
-
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
|
38
|
-
}
|
|
39
|
-
return result;
|
|
40
|
-
}
|
|
41
|
-
describe('src/app/export-utils.js', () => {
|
|
42
|
-
describe('encodeConfInUrl', () => {
|
|
43
|
-
it('encodes a view config for a URL', () => {
|
|
44
|
-
const urlParams1 = encodeConfInUrl({ conf: {} });
|
|
45
|
-
expect(urlParams1).toEqual('vitessce_conf_length=5&vitessce_conf_version=0.0.1&vitessce_conf=N4XyA');
|
|
46
|
-
const urlParams2 = encodeConfInUrl({ conf: fakeConfig });
|
|
47
|
-
expect(urlParams2).toEqual('vitessce_conf_length=327&vitessce_conf_version=0.0.1&vitessce_conf=N4IgbgpgTgzglgewHYgFwgAwDoCMWMgA0IADgK4BGANnAMZoBmAhlTBMVUwJ7QxoDaoJEwC2ENCFoQqrIiAAuXEuPQBhAKIAZTQGU5DOFQgAVJSsnTWWAFYxkcslCoSAFvPklUAei8QAHqIkRli0CCJeUjIwNnYoAL4AusTCYhIAUmQw8gAEMLRM7tBBCPJyWQV0mtwIZKWogpJhJMgQSHUgeQXyRVQlciRQCCR8qKAiTCQkcEgA5hLyALQ6AHLqcmBwEADuaKAAXghhaNgA7ACsxPJMUDMQdfwYhI8YCXFxxH7HxFxfIDuoOAATMQXGhAYk4kA');
|
|
48
|
-
});
|
|
49
|
-
it('Calls onOverMaximumUrlLength for conf too long', () => {
|
|
50
|
-
let browsers;
|
|
51
|
-
// eslint-disable-next-line no-return-assign
|
|
52
|
-
const onOverMaximumUrlLength = ({ willWorkOn }) => browsers = willWorkOn;
|
|
53
|
-
encodeConfInUrl({ conf: { foo: makeLongString(69000) }, onOverMaximumUrlLength });
|
|
54
|
-
expect(browsers).toEqual([]);
|
|
55
|
-
});
|
|
56
|
-
it('decodes a view config for a URL', () => {
|
|
57
|
-
const viewConfig1 = decodeURLParamsToConf('vitessce_conf_length=5&vitessce_conf_version=0.0.1&vitessce_conf=N4XyA');
|
|
58
|
-
expect(viewConfig1).toEqual({});
|
|
59
|
-
const viewConfig2 = decodeURLParamsToConf('vitessce_conf_length=327&vitessce_conf_version=0.0.1&vitessce_conf=N4IgbgpgTgzglgewHYgFwgAwDoCMWMgA0IADgK4BGANnAMZoBmAhlTBMVUwJ7QxoDaoJEwC2ENCFoQqrIiAAuXEuPQBhAKIAZTQGU5DOFQgAVJSsnTWWAFYxkcslCoSAFvPklUAei8QAHqIkRli0CCJeUjIwNnYoAL4AusTCYhIAUmQw8gAEMLRM7tBBCPJyWQV0mtwIZKWogpJhJMgQSHUgeQXyRVQlciRQCCR8qKAiTCQkcEgA5hLyALQ6AHLqcmBwEADuaKAAXghhaNgA7ACsxPJMUDMQdfwYhI8YCXFxxH7HxFxfIDuoOAATMQXGhAYk4kA');
|
|
60
|
-
expect(viewConfig2).toEqual(fakeConfig);
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
});
|