From ab1db54fb24ce7388fce0bfaf1cd67cd365abed9 Mon Sep 17 00:00:00 2001 From: Norbert de Langen Date: Fri, 19 Oct 2018 15:35:50 +0200 Subject: [PATCH] ADD support for cyclic objects in channel && CLEANUP --- lib/channel-postmessage/package.json | 5 +- lib/channel-postmessage/src/index.js | 58 +++++++++++++++---- lib/ui/package.json | 4 +- scripts/verdaccio.yaml | 12 ---- yarn.lock | 83 ++++++---------------------- 5 files changed, 71 insertions(+), 91 deletions(-) diff --git a/lib/channel-postmessage/package.json b/lib/channel-postmessage/package.json index 54dc486bb17..fe6e96cfdf8 100644 --- a/lib/channel-postmessage/package.json +++ b/lib/channel-postmessage/package.json @@ -24,7 +24,10 @@ }, "dependencies": { "@storybook/channels": "4.0.0-alpha.25", + "flatted": "^1.0.3", "global": "^4.3.2", - "json-fn": "^1.1.1" + "is-function": "^1.0.1", + "is-regex": "^1.0.4", + "safe-eval": "^0.4.1" } } diff --git a/lib/channel-postmessage/src/index.js b/lib/channel-postmessage/src/index.js index 9a47fa19480..a47d5aa1604 100644 --- a/lib/channel-postmessage/src/index.js +++ b/lib/channel-postmessage/src/index.js @@ -1,13 +1,56 @@ /* eslint-disable no-underscore-dangle */ -import JSON from 'json-fn'; import { window, document } from 'global'; import Channel from '@storybook/channels'; +import isRegExp from 'is-regex'; +import isFunction from 'is-function'; + +import safeEval from 'safe-eval'; +import { parse, stringify } from 'flatted/esm'; + export const KEY = 'storybook-channel'; +const dateFormat = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{3})?Z$/; + +const replacer = (k, value) => { + if (isRegExp(value)) { + return `_regexp_${value.flags}|${value.source}`; + } + + if (isFunction(value)) { + const stringified = value.toString(); + if (!stringified.includes('[native code]')) { + return `_function_${stringified}`; + } + } + + if (typeof value === 'string' && dateFormat.test(value)) { + return `_date_${value}`; + } + + return value; +}; + +const reviver = (k, value) => { + if (value && value.startsWith && value.startsWith('_function_')) { + return safeEval(`(${value.replace('_function_', '')})`); + } + + if (value && value.startsWith && value.startsWith('_regexp_')) { + const [flags, source] = value.replace('_regexp_', '').split('|', 2); + return new RegExp(source, flags); + } + + if (value && value.startsWith && value.startsWith('_date_')) { + return new Date(value.replace('_date_', '')); + } + + return value; +}; + // TODO: we should export a method for opening child windows here and keep track of em. -// that way we can send postmessage to child windows as well, not just iframe +// that way we can send postMessage to child windows as well, not just iframe // https://stackoverflow.com/questions/6340160/how-to-get-the-references-of-all-already-opened-child-windows // eslint-disable-next-line no-useless-escape @@ -40,7 +83,7 @@ export class PostmsgTransport { }); } - const data = JSON.stringify({ key: KEY, event }); + const data = stringify({ key: KEY, event }, replacer); // TODO: investigate http://blog.teamtreehouse.com/cross-domain-messaging-with-postmessage // might replace '*' with document.location ? @@ -74,14 +117,9 @@ export class PostmsgTransport { _handleEvent(rawEvent) { try { const { data } = rawEvent; - const { key, event } = typeof data === 'string' && isJSON(data) ? JSON.parse(data) : data; + const { key, event } = typeof data === 'string' && isJSON(data) ? parse(data, reviver) : data; if (key === KEY) { - console.debug( - `message from ${this._config.page}`, - event.type, - event.args[0], - event.args[1] - ); + console.debug(`message from ${this._config.page}`, event.type, ...event.args); this._handler(event); } } catch (error) { diff --git a/lib/ui/package.json b/lib/ui/package.json index eb9afddfc34..2dd36cbc09a 100644 --- a/lib/ui/package.json +++ b/lib/ui/package.json @@ -30,11 +30,9 @@ "@emotion/styled": "^0.10.6", "@reach/router": "^1.1.1", "@storybook/addons": "4.0.0-alpha.25", + "@storybook/client-logger": "4.0.0-alpha.25", "@storybook/components": "4.0.0-alpha.25", "@storybook/core-events": "4.0.0-alpha.25", - "@storybook/mantra-core": "^1.7.2", - "@storybook/podda": "^1.2.3", - "@storybook/react-komposer": "^2.0.4", "emotion": "^9.2.8", "events": "^3.0.0", "fast-deep-equal": "^2.0.1", diff --git a/scripts/verdaccio.yaml b/scripts/verdaccio.yaml index 430c12216cd..3c099eb6e81 100644 --- a/scripts/verdaccio.yaml +++ b/scripts/verdaccio.yaml @@ -10,10 +10,6 @@ uplinks: packages: # storybook's 'legacy' packages - '@storybook/mantra-core': - access: $all - publish: $all - proxy: npmjs '@storybook/react-simple-di': access: $all publish: $all @@ -22,14 +18,6 @@ packages: access: $all publish: $all proxy: npmjs - '@storybook/podda': - access: $all - publish: $all - proxy: npmjs - '@storybook/react-komposer': - access: $all - publish: $all - proxy: npmjs # storybook packages are NOT proxied to global registry # allowing us to republish any version during tests diff --git a/yarn.lock b/yarn.lock index 7dabdff3973..9f8fc527f13 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1887,51 +1887,6 @@ semver "^5.3.0" semver-intersect "^1.1.2" -"@storybook/mantra-core@^1.7.2": - version "1.7.2" - resolved "https://registry.yarnpkg.com/@storybook/mantra-core/-/mantra-core-1.7.2.tgz#e10c7faca29769e97131e0e0308ef7cfb655b70c" - integrity sha512-GD4OYJ8GsayVhIg306sfgcKDk9j8YfuSKIAWvdB/g7IDlw0pDgueONALVEEE2XWJtCwcsUyDtCYzXFgCBWLEjA== - dependencies: - "@storybook/react-komposer" "^2.0.1" - "@storybook/react-simple-di" "^1.2.1" - babel-runtime "6.x.x" - -"@storybook/podda@^1.2.3": - version "1.2.3" - resolved "https://registry.yarnpkg.com/@storybook/podda/-/podda-1.2.3.tgz#53c4a1a3f8c7bbd5755dff5c34576fd1af9d38ba" - integrity sha512-g7dsdsn50AhlGZ8iIDKdF8bi7Am++iFOq+QN+hNKz3FvgLuf8Dz+mpC/BFl90eE9bEYxXqXKeMf87399Ec5Qhw== - dependencies: - babel-runtime "^6.11.6" - immutable "^3.8.1" - -"@storybook/react-komposer@^2.0.1", "@storybook/react-komposer@^2.0.4": - version "2.0.4" - resolved "https://registry.yarnpkg.com/@storybook/react-komposer/-/react-komposer-2.0.4.tgz#c2c0d4a75d9b4a9c0c6b46f14ab050f458ad4bb0" - integrity sha1-wsDUp12bSpwMa0bxSrBQ9FitS7A= - dependencies: - "@storybook/react-stubber" "^1.0.0" - babel-runtime "^6.11.6" - hoist-non-react-statics "^1.2.0" - lodash.pick "^4.4.0" - shallowequal "^0.2.2" - -"@storybook/react-simple-di@^1.2.1": - version "1.3.0" - resolved "https://registry.yarnpkg.com/@storybook/react-simple-di/-/react-simple-di-1.3.0.tgz#13116d89a2f42898716a7f8c4095b47415526371" - integrity sha512-RH6gPQaYMs/VzQX2dgbZU8DQMKFXVOv1ruohHjjNPys4q+YdqMFMDe5jOP1AUE3j9g01x0eW7bVjRawSpl++Ew== - dependencies: - babel-runtime "6.x.x" - create-react-class "^15.6.2" - hoist-non-react-statics "1.x.x" - prop-types "^15.6.0" - -"@storybook/react-stubber@^1.0.0": - version "1.0.1" - resolved "https://registry.yarnpkg.com/@storybook/react-stubber/-/react-stubber-1.0.1.tgz#8c312c2658b9eeafce470e1c39e4193f0b5bf9b1" - integrity sha512-k+CHH+vA8bQfCmzBTtJsPkITFgD+C/w19KuByZ9WeEvNUFtnDaCqfP+Vp3/OR+3IAfAXYYOWolqPLxNPcEqEjw== - dependencies: - babel-runtime "^6.5.0" - "@svgr/core@^2.4.1": version "2.4.1" resolved "https://registry.yarnpkg.com/@svgr/core/-/core-2.4.1.tgz#03a407c28c4a1d84305ae95021e8eabfda8fa731" @@ -4500,7 +4455,7 @@ babel-register@^6.24.1, babel-register@^6.26.0, babel-register@^6.9.0: mkdirp "^0.5.1" source-map-support "^0.4.15" -babel-runtime@6.x.x, babel-runtime@^6.0.0, babel-runtime@^6.11.6, babel-runtime@^6.18.0, babel-runtime@^6.22.0, babel-runtime@^6.23.0, babel-runtime@^6.26.0, babel-runtime@^6.5.0, babel-runtime@^6.9.2: +babel-runtime@6.x.x, babel-runtime@^6.0.0, babel-runtime@^6.18.0, babel-runtime@^6.22.0, babel-runtime@^6.23.0, babel-runtime@^6.26.0, babel-runtime@^6.9.2: version "6.26.0" resolved "https://registry.yarnpkg.com/babel-runtime/-/babel-runtime-6.26.0.tgz#965c7058668e82b55d7bfe04ff2337bc8b5647fe" integrity sha1-llxwWGaOgrVde/4E/yM3vItWR/4= @@ -7015,7 +6970,7 @@ create-hmac@^1.1.0, create-hmac@^1.1.2, create-hmac@^1.1.4: safe-buffer "^5.0.1" sha.js "^2.4.8" -create-react-class@^15.5.2, create-react-class@^15.6.2: +create-react-class@^15.5.2: version "15.6.3" resolved "https://registry.yarnpkg.com/create-react-class/-/create-react-class-15.6.3.tgz#2d73237fb3f970ae6ebe011a9e66f46dbca80036" integrity sha512-M+/3Q6E6DLO6Yx3OwrWjwHBnvfXXYA7W+dFjt/ZDBemHO1DDZhsalX/NUtnTYclN6GfnBDRh4qRHjcDHmlJBJg== @@ -9929,6 +9884,11 @@ flat-cache@^1.2.1: graceful-fs "^4.1.2" write "^0.2.1" +flatted@^1.0.3: + version "1.0.3" + resolved "https://registry.yarnpkg.com/flatted/-/flatted-1.0.3.tgz#18dc8704f89e757d88d7048e9d77b316f91705c2" + integrity sha512-3JPwKDX4pKsnnuPtApEoD4HEy61R1gO6CN8XH5/wdA5i7TrhLKIB2lgHEb9ETGkigo9snd1iCSvvcjhFanyxvg== + flatten@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/flatten/-/flatten-1.0.2.tgz#dae46a9d78fbe25292258cc1e780a41d95c03782" @@ -11077,11 +11037,6 @@ hoek@4.x.x: resolved "https://registry.yarnpkg.com/hoek/-/hoek-4.2.1.tgz#9634502aa12c445dd5a7c5734b572bb8738aacbb" integrity sha512-QLg82fGkfnJ/4iy1xZ81/9SIJiq1NGFUMGs6ParyjBZr6jW2Ufj/snDqTHixNlHdPNwN2RLVD0Pi3igeK9+JfA== -hoist-non-react-statics@1.x.x, hoist-non-react-statics@^1.2.0: - version "1.2.0" - resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-1.2.0.tgz#aa448cf0986d55cc40773b17174b7dd066cb7cfb" - integrity sha1-qkSM8JhtVcxAdzsXF0t90GbLfPs= - hoist-non-react-statics@^2.5.0: version "2.5.5" resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-2.5.5.tgz#c5903cf409c0dfd908f388e619d86b9c1174cb47" @@ -11529,11 +11484,6 @@ immer@^1.5.0: resolved "https://registry.yarnpkg.com/immer/-/immer-1.5.0.tgz#1ed9b1b653643fe9276bbc36377613523ea32a39" integrity sha1-HtmxtlNkP+kna7w2N3YTUj6jKjk= -immutable@^3.8.1: - version "3.8.2" - resolved "https://registry.yarnpkg.com/immutable/-/immutable-3.8.2.tgz#c2439951455bb39913daf281376f1530e104adf3" - integrity sha1-wkOZUUVbs5kT2vKBN28VMOEErfM= - import-cwd@^2.0.0: version "2.1.0" resolved "https://registry.yarnpkg.com/import-cwd/-/import-cwd-2.1.0.tgz#aa6cf36e722761285cb371ec6519f53e2435b0a9" @@ -12028,6 +11978,11 @@ is-fullwidth-code-point@^2.0.0: resolved "https://registry.yarnpkg.com/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz#a3b30a5c4f199183167aaab93beefae3ddfb654f" integrity sha1-o7MKXE8ZkYMWeqq5O+764937ZU8= +is-function@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/is-function/-/is-function-1.0.1.tgz#12cfb98b65b57dd3d193a3121f5f6e2f437602b5" + integrity sha1-Es+5i2W1fdPRk6MSH19uL0N2ArU= + is-generator-fn@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/is-generator-fn/-/is-generator-fn-1.0.0.tgz#969d49e1bb3329f6bb7f09089be26578b2ddd46a" @@ -14361,7 +14316,7 @@ lodash.istypedarray@^3.0.0: resolved "https://registry.yarnpkg.com/lodash.istypedarray/-/lodash.istypedarray-3.0.6.tgz#c9a477498607501d8e8494d283b87c39281cef62" integrity sha1-yaR3SYYHUB2OhJTSg7h8OSgc72I= -lodash.keys@^3.0.0, lodash.keys@^3.1.2: +lodash.keys@^3.0.0: version "3.1.2" resolved "https://registry.yarnpkg.com/lodash.keys/-/lodash.keys-3.1.2.tgz#4dbc0472b156be50a0b286855d1bd0b0c656098a" integrity sha1-TbwEcrFWvlCgsoaFXRvQsMZWCYo= @@ -20272,6 +20227,11 @@ safe-buffer@~5.0.1: resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.0.1.tgz#d263ca54696cd8a306b5ca6551e92de57918fbe7" integrity sha1-0mPKVGls2KMGtcplUekt5XkY++c= +safe-eval@^0.4.1: + version "0.4.1" + resolved "https://registry.yarnpkg.com/safe-eval/-/safe-eval-0.4.1.tgz#e54ba5a1bbdec795d488f6c8765e0c2a78b4cdc0" + integrity sha512-wmiu4RSYVZ690RP1+cv/LxfPK1dIlEN35aW7iv4SMYdqDrHbkll4+NJcHmKm7PbCuI1df1otOcPwgcc2iFR85g== + safe-json-parse@~1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/safe-json-parse/-/safe-json-parse-1.0.1.tgz#3e76723e38dfdda13c9b1d29a1e07ffee4b30b57" @@ -20697,13 +20657,6 @@ shallow-clone@^1.0.0: kind-of "^5.0.0" mixin-object "^2.0.1" -shallowequal@^0.2.2: - version "0.2.2" - resolved "https://registry.yarnpkg.com/shallowequal/-/shallowequal-0.2.2.tgz#1e32fd5bcab6ad688a4812cb0cc04efc75c7014e" - integrity sha1-HjL9W8q2rWiKSBLLDMBO/HXHAU4= - dependencies: - lodash.keys "^3.1.2" - shallowequal@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/shallowequal/-/shallowequal-1.1.0.tgz#188d521de95b9087404fd4dcb68b13df0ae4e7f8"