@vitessce/export-utils 2.0.0-beta.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,70 @@
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
+ }
@@ -0,0 +1,63 @@
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
+ });
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export { encodeConfInUrl, decodeURLParamsToConf, } from './export-utils';
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@vitessce/export-utils",
3
+ "version": "2.0.0-beta.1",
4
+ "author": "Gehlenborg Lab",
5
+ "homepage": "http://vitessce.io",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/vitessce/vitessce.git"
9
+ },
10
+ "license": "MIT",
11
+ "main": "dist/index.js",
12
+ "files": [
13
+ "dist"
14
+ ],
15
+ "dependencies": {
16
+ "bowser": "^2.11.0",
17
+ "lz-string": "^1.4.4"
18
+ },
19
+ "scripts": {
20
+ "start": "tsc --watch",
21
+ "build": "tsc",
22
+ "test": "pnpm exec vitest --run -r ../../ --dir ."
23
+ }
24
+ }