@vitessce/export-utils 2.0.3-beta.0 → 3.0.0

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.
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Encode a configuration as url params with a version and an lz-compressed conf.
3
+ * @param {Object} params
4
+ * @param {Object} params.conf Previous scope names.
5
+ * @param {function} params.onOverMaximumUrlLength Callback for when new url
6
+ * is over max length for your browser - takes two arguments: { message, willWorkOn }
7
+ * for the error message and the browsers for which the url will work (optional).
8
+ * @returns {string} The new params like
9
+ * vitessce_conf_length=10&vitessce_conf_version=0.0.1&vitessce_conf=fksdfasdfjkl
10
+ */
11
+ export function encodeConfInUrl({ conf, onOverMaximumUrlLength, }: {
12
+ conf: Object;
13
+ onOverMaximumUrlLength: Function;
14
+ }): string;
15
+ /**
16
+ * Decode URL params to a Vitessce configuration.
17
+ * The URL params must have version and vitessce_conf params,
18
+ * like vitessce_conf_length=10&vitessce_conf_version=0.0.1&vitessce_conf=fksdfasdfjkl.
19
+ * @param {Object} queryString The URL params,
20
+ * like vitessce_conf_length=10&vitessce_conf_version=0.0.1&vitessce_conf=fksdfasdfjkl.
21
+ * @returns {string} A vitessce configuration.
22
+ */
23
+ export function decodeURLParamsToConf(queryString: Object): string;
24
+ export default class CompressedConfLengthError {
25
+ constructor(message: any);
26
+ message: any;
27
+ }
28
+ //# sourceMappingURL=export-utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"export-utils.d.ts","sourceRoot":"","sources":["../src/export-utils.js"],"names":[],"mappings":"AA4BA;;;;;;;;;GASG;AACH;IAP0B,IAAI,EAAnB,MAAM;IACW,sBAAsB;IAGrC,MAAM,CAoBlB;AAED;;;;;;;GAOG;AACH,mDAJW,MAAM,GAEJ,MAAM,CAelB;AAxDD;IACE,0BAEC;IADC,aAAsB;CAEzB"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=export-utils.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"export-utils.test.d.ts","sourceRoot":"","sources":["../src/export-utils.test.js"],"names":[],"mappings":""}
@@ -1,4 +1,4 @@
1
- import { encodeConfInUrl, decodeURLParamsToConf, } from './export-utils';
1
+ import { encodeConfInUrl, decodeURLParamsToConf, } from './export-utils.js';
2
2
  const fakeConfig = {
3
3
  version: '0.1.0',
4
4
  public: false,
@@ -0,0 +1,2 @@
1
+ export { encodeConfInUrl, decodeURLParamsToConf } from "./export-utils.js";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":""}
@@ -0,0 +1 @@
1
+ export { encodeConfInUrl, decodeURLParamsToConf, } from './export-utils.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitessce/export-utils",
3
- "version": "2.0.3-beta.0",
3
+ "version": "3.0.0",
4
4
  "author": "Gehlenborg Lab",
5
5
  "homepage": "http://vitessce.io",
6
6
  "repository": {
@@ -8,17 +8,26 @@
8
8
  "url": "git+https://github.com/vitessce/vitessce.git"
9
9
  },
10
10
  "license": "MIT",
11
+ "type": "module",
11
12
  "main": "dist/index.js",
12
13
  "files": [
13
- "dist"
14
+ "src",
15
+ "dist",
16
+ "dist-tsc"
14
17
  ],
15
18
  "dependencies": {
16
19
  "bowser": "^2.11.0",
17
20
  "lz-string": "^1.4.4"
18
21
  },
19
22
  "scripts": {
20
- "start": "tsc --watch",
21
- "build": "tsc",
23
+ "bundle": "pnpm exec vite build -c ../../../scripts/vite.config.js",
22
24
  "test": "pnpm exec vitest --run -r ../../../ --dir ."
25
+ },
26
+ "module": "dist/index.js",
27
+ "exports": {
28
+ ".": {
29
+ "types": "./dist-tsc/index.d.ts",
30
+ "import": "./dist/index.js"
31
+ }
23
32
  }
24
33
  }
@@ -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.js';
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
@@ -0,0 +1,4 @@
1
+ export {
2
+ encodeConfInUrl,
3
+ decodeURLParamsToConf,
4
+ } from './export-utils.js';
File without changes