@contentstech/stackflow-plugin-history-sync
Advanced tools
Comparing version 0.2.0 to 0.3.0
import type { StackflowPlugin } from "@stackflow/core"; | ||
import type { History } from "history"; | ||
import type { ActivityDefinition, Config } from "@stackflow/config"; | ||
import type { ActivityRoute } from "./ActivityRoute"; | ||
@@ -7,6 +8,14 @@ import type { RouteLike } from "./RouteLike"; | ||
import type { UrlPatternOptions } from "./makeTemplate"; | ||
type HistorySyncPluginOptions<T, K extends Extract<keyof T, string>> = { | ||
declare module "@stackflow/config" { | ||
interface ActivityDefinition<ActivityName extends string> { | ||
path: string; | ||
} | ||
} | ||
type HistorySyncPluginOptions<T, K extends Extract<keyof T, string>> = ({ | ||
routes: { | ||
[key in keyof T]: RouteLike<T[key]>; | ||
}; | ||
} | { | ||
config: Config<ActivityDefinition<string>>; | ||
}) & { | ||
fallbackActivity: (args: { | ||
@@ -13,0 +22,0 @@ initialContext: any; |
@@ -0,3 +1,3 @@ | ||
import type { ActivityComponentType as SolidActivityComponentType } from "@contentstech/stackflow-solid"; | ||
import type { ActivityComponentType as ReactActivityComponentType } from "@stackflow/react"; | ||
import type { ActivityComponentType as SolidActivityComponentType } from "@contentstech/stackflow-solid"; | ||
export type Route<K> = { | ||
@@ -4,0 +4,0 @@ path: string; |
import type { StackflowReactPlugin } from "@stackflow/react"; | ||
import type { History } from "history"; | ||
import type { ActivityDefinition, Config } from "@stackflow/config"; | ||
import type { RouteLike } from "./RouteLike"; | ||
import type { UrlPatternOptions } from "./makeTemplate"; | ||
type HistorySyncPluginOptions<T, K extends Extract<keyof T, string>> = { | ||
declare module "@stackflow/config" { | ||
interface ActivityDefinition<ActivityName extends string> { | ||
path: string; | ||
} | ||
} | ||
type HistorySyncPluginOptions<T, K extends Extract<keyof T, string>> = ({ | ||
routes: { | ||
[key in keyof T]: RouteLike<T[key]>; | ||
}; | ||
} | { | ||
config: Config<ActivityDefinition<string>>; | ||
}) & { | ||
fallbackActivity: (args: { | ||
@@ -10,0 +19,0 @@ initialContext: any; |
@@ -12,3 +12,3 @@ "use strict"; | ||
var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __defNormalProp = (obj, key, value2) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value: value2 }) : obj[key] = value2; | ||
var __spreadValues = (a, b) => { | ||
@@ -26,5 +26,8 @@ for (var prop in b || (b = {})) | ||
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); | ||
var __commonJS = (cb, mod) => function __require() { | ||
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; | ||
}; | ||
var __export = (target, all) => { | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
for (var name2 in all) | ||
__defProp(target, name2, { get: all[name2], enumerable: true }); | ||
}; | ||
@@ -49,2 +52,97 @@ var __copyProps = (to, from, except, desc) => { | ||
// ../../.yarn/cache/json-cycle-npm-1.5.0-4c80b4d041-4ce7594eb8.zip/node_modules/json-cycle/cycle.js | ||
var require_cycle = __commonJS({ | ||
"../../.yarn/cache/json-cycle-npm-1.5.0-4c80b4d041-4ce7594eb8.zip/node_modules/json-cycle/cycle.js"(exports, module) { | ||
"use strict"; | ||
(function(global, factory) { | ||
typeof define === "function" && define.amd ? define(factory) : typeof module === "object" && module.exports ? module.exports = factory : global.json = factory; | ||
})(exports, /* @__PURE__ */ function() { | ||
function decycle(object) { | ||
var objects = [], paths = []; | ||
return function derez(value2, path2) { | ||
var i2, name2, nu; | ||
var _value = value2; | ||
try { | ||
_value = value2.toJSON(); | ||
} catch (error) { | ||
} | ||
if (typeof _value === "object" && _value) { | ||
for (i2 = 0; i2 < objects.length; i2 += 1) { | ||
if (objects[i2] === _value) { | ||
return { $ref: paths[i2] }; | ||
} | ||
} | ||
objects.push(_value); | ||
paths.push(path2); | ||
if (Object.prototype.toString.apply(_value) === "[object Array]") { | ||
nu = []; | ||
for (i2 = 0; i2 < _value.length; i2 += 1) { | ||
nu[i2] = derez(_value[i2], path2 + "[" + i2 + "]"); | ||
} | ||
} else { | ||
nu = {}; | ||
for (name2 in _value) { | ||
if (Object.prototype.hasOwnProperty.call(_value, name2)) { | ||
nu[name2] = derez( | ||
_value[name2], | ||
path2 + "[" + JSON.stringify(name2) + "]" | ||
); | ||
} | ||
} | ||
} | ||
return nu; | ||
} | ||
return _value; | ||
}(object, "$"); | ||
} | ||
function retrocycle($) { | ||
var px = /^\$(?:\[(?:\d+|\"(?:[^\\\"\u0000-\u001f]|\\([\\\"\/bfnrt]|u[0-9a-zA-Z]{4}))*\")\])*$/; | ||
(function rez(value) { | ||
var i, item, name, path; | ||
if (value && typeof value === "object") { | ||
if (Object.prototype.toString.apply(value) === "[object Array]") { | ||
for (i = 0; i < value.length; i += 1) { | ||
item = value[i]; | ||
if (item && typeof item === "object") { | ||
path = item.$ref; | ||
if (typeof path === "string" && px.test(path)) { | ||
value[i] = eval(path); | ||
} else { | ||
rez(item); | ||
} | ||
} | ||
} | ||
} else { | ||
for (name in value) { | ||
if (typeof value[name] === "object") { | ||
item = value[name]; | ||
if (item) { | ||
path = item.$ref; | ||
if (typeof path === "string" && px.test(path)) { | ||
value[name] = eval(path); | ||
} else { | ||
rez(item); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
})($); | ||
return $; | ||
} | ||
return { | ||
decycle, | ||
retrocycle, | ||
stringify: function(object, replacer, space) { | ||
return JSON.stringify(decycle(object), replacer, space); | ||
}, | ||
parse: function($2, reviver) { | ||
return retrocycle(JSON.parse($2, reviver)); | ||
} | ||
}; | ||
}()); | ||
} | ||
}); | ||
// src/index.ts | ||
@@ -96,18 +194,10 @@ var src_exports = {}; | ||
// src/historyState.ts | ||
var import_json_cycle = __toESM(require_cycle()); | ||
var STATE_TAG = "@stackflow/plugin-history-sync"; | ||
function clone(input) { | ||
return JSON.parse(JSON.stringify(input)); | ||
} | ||
function serializeStep(step) { | ||
return clone(step); | ||
} | ||
function serializeActivity(activity) { | ||
return clone(activity); | ||
} | ||
function serializeState(state) { | ||
return { | ||
return (0, import_json_cycle.decycle)({ | ||
_TAG: STATE_TAG, | ||
activity: serializeActivity(state.activity), | ||
step: state.step ? serializeStep(state.step) : void 0 | ||
}; | ||
activity: state.activity, | ||
step: state.step | ||
}); | ||
} | ||
@@ -117,3 +207,3 @@ function safeParseState(state) { | ||
if (typeof _state === "object" && _state !== null && "_TAG" in _state && typeof _state._TAG === "string" && _state._TAG === STATE_TAG) { | ||
return state; | ||
return (0, import_json_cycle.retrocycle)(state); | ||
} | ||
@@ -171,9 +261,9 @@ return null; | ||
var import_url_pattern = __toESM(require("url-pattern")); | ||
function pathToUrl(path) { | ||
return new URL(path, "file://"); | ||
function pathToUrl(path2) { | ||
return new URL(path2, "file://"); | ||
} | ||
function urlSearchParamsToMap(urlSearchParams) { | ||
const map = {}; | ||
urlSearchParams.forEach((value, key) => { | ||
map[key] = value; | ||
urlSearchParams.forEach((value2, key) => { | ||
map[key] = value2; | ||
}); | ||
@@ -195,5 +285,5 @@ return map; | ||
} | ||
function makeTemplate({ path, decode }, urlPatternOptions) { | ||
const pattern = new import_url_pattern.default(`${path}(/)`, urlPatternOptions); | ||
const onlyAsterisk = path === "*" || path === "/*"; | ||
function makeTemplate({ path: path2, decode }, urlPatternOptions) { | ||
const pattern = new import_url_pattern.default(`${path2}(/)`, urlPatternOptions); | ||
const onlyAsterisk = path2 === "*" || path2 === "/*"; | ||
const variableCount = onlyAsterisk ? Number.POSITIVE_INFINITY : pattern.names.length; | ||
@@ -210,4 +300,4 @@ return { | ||
Object.entries(searchParamsMap).reduce( | ||
(acc, [key, value]) => __spreadValues(__spreadValues({}, acc), value ? { | ||
[key]: value | ||
(acc, [key, value2]) => __spreadValues(__spreadValues({}, acc), value2 ? { | ||
[key]: value2 | ||
} : null), | ||
@@ -219,4 +309,4 @@ {} | ||
}, | ||
parse(path2) { | ||
const url = pathToUrl(path2); | ||
parse(path3) { | ||
const url = pathToUrl(path3); | ||
const pathParams = pattern.match(url.pathname); | ||
@@ -235,4 +325,4 @@ const searchParams = urlSearchParamsToMap(url.searchParams); | ||
// src/normalizeRouteInput.ts | ||
function makeRoute(path) { | ||
return typeof path === "string" ? { path } : path; | ||
function makeRoute(path2) { | ||
return typeof path2 === "string" ? { path: path2 } : path2; | ||
} | ||
@@ -258,5 +348,22 @@ function normalizeRouteInput(route) { | ||
// src/sortActivityRoutes.ts | ||
var paramRe = /^:[\w-]+$/; | ||
var dynamicSegmentValue = 3; | ||
var emptySegmentValue = 1; | ||
var staticSegmentValue = 10; | ||
var splatPenalty = -2; | ||
var isSplat = (s) => s === "*"; | ||
function computeScore(path2) { | ||
const segments = path2.split("/"); | ||
let initialScore = segments.length; | ||
if (segments.some(isSplat)) { | ||
initialScore += splatPenalty; | ||
} | ||
return segments.filter((s) => !isSplat(s)).reduce( | ||
(score, segment) => score + (paramRe.test(segment) ? dynamicSegmentValue : segment === "" ? emptySegmentValue : staticSegmentValue), | ||
initialScore | ||
); | ||
} | ||
function sortActivityRoutes(routes) { | ||
return [...routes].sort( | ||
(a, b) => makeTemplate(a).variableCount - makeTemplate(b).variableCount | ||
(a, b) => computeScore(b.path) - computeScore(a.path) | ||
); | ||
@@ -275,5 +382,9 @@ } | ||
const { location } = history; | ||
const activityRoutes = sortActivityRoutes( | ||
normalizeActivityRouteMap(options.routes) | ||
const routes = "routes" in options ? options.routes : options.config.activities.reduce( | ||
(acc, a) => __spreadProps(__spreadValues({}, acc), { | ||
[a.name]: a.path | ||
}), | ||
{} | ||
); | ||
const activityRoutes = sortActivityRoutes(normalizeActivityRouteMap(routes)); | ||
return () => { | ||
@@ -563,6 +674,6 @@ let pushFlag = 0; | ||
const template = makeTemplate(match, options.urlPatternOptions); | ||
const path = template.fill(actionParams.activityParams); | ||
const path2 = template.fill(actionParams.activityParams); | ||
overrideActionParams(__spreadProps(__spreadValues({}, actionParams), { | ||
activityContext: __spreadProps(__spreadValues({}, actionParams.activityContext), { | ||
path | ||
path: path2 | ||
}) | ||
@@ -579,6 +690,6 @@ })); | ||
const template = makeTemplate(match, options.urlPatternOptions); | ||
const path = template.fill(actionParams.activityParams); | ||
const path2 = template.fill(actionParams.activityParams); | ||
overrideActionParams(__spreadProps(__spreadValues({}, actionParams), { | ||
activityContext: __spreadProps(__spreadValues({}, actionParams.activityContext), { | ||
path | ||
path: path2 | ||
}) | ||
@@ -592,3 +703,3 @@ })); | ||
if (previousActivity) { | ||
for (let i = 0; i < previousActivity.steps.length - 1; i += 1) { | ||
for (let i2 = 0; i2 < previousActivity.steps.length - 1; i2 += 1) { | ||
requestHistoryTick((resolve) => { | ||
@@ -630,3 +741,3 @@ if (!safeParseState(getCurrentState({ history }))) { | ||
const popCount = isRoot ? 0 : steps.length; | ||
for (let i = 0; i < popCount; i += 1) { | ||
for (let i2 = 0; i2 < popCount; i2 += 1) { | ||
requestHistoryTick((resolve) => { | ||
@@ -633,0 +744,0 @@ if (!safeParseState(getCurrentState({ history }))) { |
import type { StackflowReactPlugin } from "@stackflow/react"; | ||
export declare const historySyncPlugin: <T extends { | ||
[activityName: string]: unknown; | ||
}, K extends Extract<keyof T, string>>(options: { | ||
}, K extends Extract<keyof T, string>>(options: ({ | ||
config: import("@stackflow/config").Config<import("@stackflow/config").ActivityDefinition<string>>; | ||
} | { | ||
routes: { [key in keyof T]: import("../common/RouteLike").RouteLike<T[key]>; }; | ||
}) & { | ||
fallbackActivity: (args: { | ||
@@ -7,0 +10,0 @@ initialContext: any; |
@@ -12,3 +12,3 @@ "use strict"; | ||
var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __defNormalProp = (obj, key, value2) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value: value2 }) : obj[key] = value2; | ||
var __spreadValues = (a, b) => { | ||
@@ -26,5 +26,8 @@ for (var prop in b || (b = {})) | ||
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); | ||
var __commonJS = (cb, mod) => function __require() { | ||
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; | ||
}; | ||
var __export = (target, all) => { | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
for (var name2 in all) | ||
__defProp(target, name2, { get: all[name2], enumerable: true }); | ||
}; | ||
@@ -49,2 +52,97 @@ var __copyProps = (to, from, except, desc) => { | ||
// ../../.yarn/cache/json-cycle-npm-1.5.0-4c80b4d041-4ce7594eb8.zip/node_modules/json-cycle/cycle.js | ||
var require_cycle = __commonJS({ | ||
"../../.yarn/cache/json-cycle-npm-1.5.0-4c80b4d041-4ce7594eb8.zip/node_modules/json-cycle/cycle.js"(exports, module) { | ||
"use strict"; | ||
(function(global, factory) { | ||
typeof define === "function" && define.amd ? define(factory) : typeof module === "object" && module.exports ? module.exports = factory : global.json = factory; | ||
})(exports, /* @__PURE__ */ function() { | ||
function decycle(object) { | ||
var objects = [], paths = []; | ||
return function derez(value2, path2) { | ||
var i2, name2, nu; | ||
var _value = value2; | ||
try { | ||
_value = value2.toJSON(); | ||
} catch (error) { | ||
} | ||
if (typeof _value === "object" && _value) { | ||
for (i2 = 0; i2 < objects.length; i2 += 1) { | ||
if (objects[i2] === _value) { | ||
return { $ref: paths[i2] }; | ||
} | ||
} | ||
objects.push(_value); | ||
paths.push(path2); | ||
if (Object.prototype.toString.apply(_value) === "[object Array]") { | ||
nu = []; | ||
for (i2 = 0; i2 < _value.length; i2 += 1) { | ||
nu[i2] = derez(_value[i2], path2 + "[" + i2 + "]"); | ||
} | ||
} else { | ||
nu = {}; | ||
for (name2 in _value) { | ||
if (Object.prototype.hasOwnProperty.call(_value, name2)) { | ||
nu[name2] = derez( | ||
_value[name2], | ||
path2 + "[" + JSON.stringify(name2) + "]" | ||
); | ||
} | ||
} | ||
} | ||
return nu; | ||
} | ||
return _value; | ||
}(object, "$"); | ||
} | ||
function retrocycle($) { | ||
var px = /^\$(?:\[(?:\d+|\"(?:[^\\\"\u0000-\u001f]|\\([\\\"\/bfnrt]|u[0-9a-zA-Z]{4}))*\")\])*$/; | ||
(function rez(value) { | ||
var i, item, name, path; | ||
if (value && typeof value === "object") { | ||
if (Object.prototype.toString.apply(value) === "[object Array]") { | ||
for (i = 0; i < value.length; i += 1) { | ||
item = value[i]; | ||
if (item && typeof item === "object") { | ||
path = item.$ref; | ||
if (typeof path === "string" && px.test(path)) { | ||
value[i] = eval(path); | ||
} else { | ||
rez(item); | ||
} | ||
} | ||
} | ||
} else { | ||
for (name in value) { | ||
if (typeof value[name] === "object") { | ||
item = value[name]; | ||
if (item) { | ||
path = item.$ref; | ||
if (typeof path === "string" && px.test(path)) { | ||
value[name] = eval(path); | ||
} else { | ||
rez(item); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
})($); | ||
return $; | ||
} | ||
return { | ||
decycle, | ||
retrocycle, | ||
stringify: function(object, replacer, space) { | ||
return JSON.stringify(decycle(object), replacer, space); | ||
}, | ||
parse: function($2, reviver) { | ||
return retrocycle(JSON.parse($2, reviver)); | ||
} | ||
}; | ||
}()); | ||
} | ||
}); | ||
// src/react/index.ts | ||
@@ -62,9 +160,9 @@ var react_exports = {}; | ||
var import_url_pattern = __toESM(require("url-pattern")); | ||
function pathToUrl(path) { | ||
return new URL(path, "file://"); | ||
function pathToUrl(path2) { | ||
return new URL(path2, "file://"); | ||
} | ||
function urlSearchParamsToMap(urlSearchParams) { | ||
const map = {}; | ||
urlSearchParams.forEach((value, key) => { | ||
map[key] = value; | ||
urlSearchParams.forEach((value2, key) => { | ||
map[key] = value2; | ||
}); | ||
@@ -86,5 +184,5 @@ return map; | ||
} | ||
function makeTemplate({ path, decode }, urlPatternOptions) { | ||
const pattern = new import_url_pattern.default(`${path}(/)`, urlPatternOptions); | ||
const onlyAsterisk = path === "*" || path === "/*"; | ||
function makeTemplate({ path: path2, decode }, urlPatternOptions) { | ||
const pattern = new import_url_pattern.default(`${path2}(/)`, urlPatternOptions); | ||
const onlyAsterisk = path2 === "*" || path2 === "/*"; | ||
const variableCount = onlyAsterisk ? Number.POSITIVE_INFINITY : pattern.names.length; | ||
@@ -101,4 +199,4 @@ return { | ||
Object.entries(searchParamsMap).reduce( | ||
(acc, [key, value]) => __spreadValues(__spreadValues({}, acc), value ? { | ||
[key]: value | ||
(acc, [key, value2]) => __spreadValues(__spreadValues({}, acc), value2 ? { | ||
[key]: value2 | ||
} : null), | ||
@@ -110,4 +208,4 @@ {} | ||
}, | ||
parse(path2) { | ||
const url = pathToUrl(path2); | ||
parse(path3) { | ||
const url = pathToUrl(path3); | ||
const pathParams = pattern.match(url.pathname); | ||
@@ -151,18 +249,10 @@ const searchParams = urlSearchParamsToMap(url.searchParams); | ||
// src/common/historyState.ts | ||
var import_json_cycle = __toESM(require_cycle()); | ||
var STATE_TAG = "@contentstech/stackflow-plugin-history-sync"; | ||
function clone(input) { | ||
return JSON.parse(JSON.stringify(input)); | ||
} | ||
function serializeStep(step) { | ||
return clone(step); | ||
} | ||
function serializeActivity(activity) { | ||
return clone(activity); | ||
} | ||
function serializeState(state) { | ||
return { | ||
return (0, import_json_cycle.decycle)({ | ||
_TAG: STATE_TAG, | ||
activity: serializeActivity(state.activity), | ||
step: state.step ? serializeStep(state.step) : void 0 | ||
}; | ||
activity: state.activity, | ||
step: state.step | ||
}); | ||
} | ||
@@ -172,3 +262,3 @@ function safeParseState(state) { | ||
if (typeof _state === "object" && _state !== null && "_TAG" in _state && typeof _state._TAG === "string" && _state._TAG === STATE_TAG) { | ||
return state; | ||
return (0, import_json_cycle.retrocycle)(state); | ||
} | ||
@@ -225,4 +315,4 @@ return null; | ||
// src/common/normalizeRouteInput.ts | ||
function makeRoute(path) { | ||
return typeof path === "string" ? { path } : path; | ||
function makeRoute(path2) { | ||
return typeof path2 === "string" ? { path: path2 } : path2; | ||
} | ||
@@ -254,4 +344,4 @@ function normalizeRouteInput(route) { | ||
var isSplat = (s) => s === "*"; | ||
function computeScore(path) { | ||
const segments = path.split("/"); | ||
function computeScore(path2) { | ||
const segments = path2.split("/"); | ||
let initialScore = segments.length; | ||
@@ -281,4 +371,10 @@ if (segments.some(isSplat)) { | ||
const { location } = history; | ||
const routes = "routes" in options ? options.routes : options.config.activities.reduce( | ||
(acc, a) => __spreadProps(__spreadValues({}, acc), { | ||
[a.name]: a.path | ||
}), | ||
{} | ||
); | ||
const activityRoutes = sortActivityRoutes( | ||
normalizeActivityRouteMap(options.routes) | ||
normalizeActivityRouteMap(routes) | ||
); | ||
@@ -566,6 +662,6 @@ const plugin = () => { | ||
const template = makeTemplate(match, options.urlPatternOptions); | ||
const path = template.fill(actionParams.activityParams); | ||
const path2 = template.fill(actionParams.activityParams); | ||
overrideActionParams(__spreadProps(__spreadValues({}, actionParams), { | ||
activityContext: __spreadProps(__spreadValues({}, actionParams.activityContext), { | ||
path | ||
path: path2 | ||
}) | ||
@@ -582,6 +678,6 @@ })); | ||
const template = makeTemplate(match, options.urlPatternOptions); | ||
const path = template.fill(actionParams.activityParams); | ||
const path2 = template.fill(actionParams.activityParams); | ||
overrideActionParams(__spreadProps(__spreadValues({}, actionParams), { | ||
activityContext: __spreadProps(__spreadValues({}, actionParams.activityContext), { | ||
path | ||
path: path2 | ||
}) | ||
@@ -595,3 +691,3 @@ })); | ||
if (previousActivity) { | ||
for (let i = 0; i < previousActivity.steps.length - 1; i += 1) { | ||
for (let i2 = 0; i2 < previousActivity.steps.length - 1; i2 += 1) { | ||
requestHistoryTick((resolve) => { | ||
@@ -633,3 +729,3 @@ if (!safeParseState(getCurrentState({ history }))) { | ||
const popCount = isRoot ? 0 : steps.length; | ||
for (let i = 0; i < popCount; i += 1) { | ||
for (let i2 = 0; i2 < popCount; i2 += 1) { | ||
requestHistoryTick((resolve) => { | ||
@@ -636,0 +732,0 @@ if (!safeParseState(getCurrentState({ history }))) { |
import type { StackflowSolidPlugin } from "@contentstech/stackflow-solid"; | ||
export declare const historySyncPlugin: <T extends { | ||
[activityName: string]: unknown; | ||
}, K extends Extract<keyof T, string>>(options: { | ||
}, K extends Extract<keyof T, string>>(options: ({ | ||
config: import("@stackflow/config").Config<import("@stackflow/config").ActivityDefinition<string>>; | ||
} | { | ||
routes: { [key in keyof T]: import("../common/RouteLike").RouteLike<T[key]>; }; | ||
}) & { | ||
fallbackActivity: (args: { | ||
@@ -7,0 +10,0 @@ initialContext: any; |
@@ -12,3 +12,3 @@ "use strict"; | ||
var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __defNormalProp = (obj, key, value2) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value: value2 }) : obj[key] = value2; | ||
var __spreadValues = (a, b) => { | ||
@@ -26,5 +26,8 @@ for (var prop in b || (b = {})) | ||
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); | ||
var __commonJS = (cb, mod) => function __require() { | ||
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; | ||
}; | ||
var __export = (target, all) => { | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
for (var name2 in all) | ||
__defProp(target, name2, { get: all[name2], enumerable: true }); | ||
}; | ||
@@ -49,2 +52,97 @@ var __copyProps = (to, from, except, desc) => { | ||
// ../../.yarn/cache/json-cycle-npm-1.5.0-4c80b4d041-4ce7594eb8.zip/node_modules/json-cycle/cycle.js | ||
var require_cycle = __commonJS({ | ||
"../../.yarn/cache/json-cycle-npm-1.5.0-4c80b4d041-4ce7594eb8.zip/node_modules/json-cycle/cycle.js"(exports, module) { | ||
"use strict"; | ||
(function(global, factory) { | ||
typeof define === "function" && define.amd ? define(factory) : typeof module === "object" && module.exports ? module.exports = factory : global.json = factory; | ||
})(exports, /* @__PURE__ */ function() { | ||
function decycle(object) { | ||
var objects = [], paths = []; | ||
return function derez(value2, path2) { | ||
var i2, name2, nu; | ||
var _value = value2; | ||
try { | ||
_value = value2.toJSON(); | ||
} catch (error) { | ||
} | ||
if (typeof _value === "object" && _value) { | ||
for (i2 = 0; i2 < objects.length; i2 += 1) { | ||
if (objects[i2] === _value) { | ||
return { $ref: paths[i2] }; | ||
} | ||
} | ||
objects.push(_value); | ||
paths.push(path2); | ||
if (Object.prototype.toString.apply(_value) === "[object Array]") { | ||
nu = []; | ||
for (i2 = 0; i2 < _value.length; i2 += 1) { | ||
nu[i2] = derez(_value[i2], path2 + "[" + i2 + "]"); | ||
} | ||
} else { | ||
nu = {}; | ||
for (name2 in _value) { | ||
if (Object.prototype.hasOwnProperty.call(_value, name2)) { | ||
nu[name2] = derez( | ||
_value[name2], | ||
path2 + "[" + JSON.stringify(name2) + "]" | ||
); | ||
} | ||
} | ||
} | ||
return nu; | ||
} | ||
return _value; | ||
}(object, "$"); | ||
} | ||
function retrocycle($) { | ||
var px = /^\$(?:\[(?:\d+|\"(?:[^\\\"\u0000-\u001f]|\\([\\\"\/bfnrt]|u[0-9a-zA-Z]{4}))*\")\])*$/; | ||
(function rez(value) { | ||
var i, item, name, path; | ||
if (value && typeof value === "object") { | ||
if (Object.prototype.toString.apply(value) === "[object Array]") { | ||
for (i = 0; i < value.length; i += 1) { | ||
item = value[i]; | ||
if (item && typeof item === "object") { | ||
path = item.$ref; | ||
if (typeof path === "string" && px.test(path)) { | ||
value[i] = eval(path); | ||
} else { | ||
rez(item); | ||
} | ||
} | ||
} | ||
} else { | ||
for (name in value) { | ||
if (typeof value[name] === "object") { | ||
item = value[name]; | ||
if (item) { | ||
path = item.$ref; | ||
if (typeof path === "string" && px.test(path)) { | ||
value[name] = eval(path); | ||
} else { | ||
rez(item); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
})($); | ||
return $; | ||
} | ||
return { | ||
decycle, | ||
retrocycle, | ||
stringify: function(object, replacer, space) { | ||
return JSON.stringify(decycle(object), replacer, space); | ||
}, | ||
parse: function($2, reviver) { | ||
return retrocycle(JSON.parse($2, reviver)); | ||
} | ||
}; | ||
}()); | ||
} | ||
}); | ||
// src/solid/index.ts | ||
@@ -62,9 +160,9 @@ var solid_exports = {}; | ||
var import_url_pattern = __toESM(require("url-pattern")); | ||
function pathToUrl(path) { | ||
return new URL(path, "file://"); | ||
function pathToUrl(path2) { | ||
return new URL(path2, "file://"); | ||
} | ||
function urlSearchParamsToMap(urlSearchParams) { | ||
const map = {}; | ||
urlSearchParams.forEach((value, key) => { | ||
map[key] = value; | ||
urlSearchParams.forEach((value2, key) => { | ||
map[key] = value2; | ||
}); | ||
@@ -86,5 +184,5 @@ return map; | ||
} | ||
function makeTemplate({ path, decode }, urlPatternOptions) { | ||
const pattern = new import_url_pattern.default(`${path}(/)`, urlPatternOptions); | ||
const onlyAsterisk = path === "*" || path === "/*"; | ||
function makeTemplate({ path: path2, decode }, urlPatternOptions) { | ||
const pattern = new import_url_pattern.default(`${path2}(/)`, urlPatternOptions); | ||
const onlyAsterisk = path2 === "*" || path2 === "/*"; | ||
const variableCount = onlyAsterisk ? Number.POSITIVE_INFINITY : pattern.names.length; | ||
@@ -101,4 +199,4 @@ return { | ||
Object.entries(searchParamsMap).reduce( | ||
(acc, [key, value]) => __spreadValues(__spreadValues({}, acc), value ? { | ||
[key]: value | ||
(acc, [key, value2]) => __spreadValues(__spreadValues({}, acc), value2 ? { | ||
[key]: value2 | ||
} : null), | ||
@@ -110,4 +208,4 @@ {} | ||
}, | ||
parse(path2) { | ||
const url = pathToUrl(path2); | ||
parse(path3) { | ||
const url = pathToUrl(path3); | ||
const pathParams = pattern.match(url.pathname); | ||
@@ -151,18 +249,10 @@ const searchParams = urlSearchParamsToMap(url.searchParams); | ||
// src/common/historyState.ts | ||
var import_json_cycle = __toESM(require_cycle()); | ||
var STATE_TAG = "@contentstech/stackflow-plugin-history-sync"; | ||
function clone(input) { | ||
return JSON.parse(JSON.stringify(input)); | ||
} | ||
function serializeStep(step) { | ||
return clone(step); | ||
} | ||
function serializeActivity(activity) { | ||
return clone(activity); | ||
} | ||
function serializeState(state) { | ||
return { | ||
return (0, import_json_cycle.decycle)({ | ||
_TAG: STATE_TAG, | ||
activity: serializeActivity(state.activity), | ||
step: state.step ? serializeStep(state.step) : void 0 | ||
}; | ||
activity: state.activity, | ||
step: state.step | ||
}); | ||
} | ||
@@ -172,3 +262,3 @@ function safeParseState(state) { | ||
if (typeof _state === "object" && _state !== null && "_TAG" in _state && typeof _state._TAG === "string" && _state._TAG === STATE_TAG) { | ||
return state; | ||
return (0, import_json_cycle.retrocycle)(state); | ||
} | ||
@@ -225,4 +315,4 @@ return null; | ||
// src/common/normalizeRouteInput.ts | ||
function makeRoute(path) { | ||
return typeof path === "string" ? { path } : path; | ||
function makeRoute(path2) { | ||
return typeof path2 === "string" ? { path: path2 } : path2; | ||
} | ||
@@ -254,4 +344,4 @@ function normalizeRouteInput(route) { | ||
var isSplat = (s) => s === "*"; | ||
function computeScore(path) { | ||
const segments = path.split("/"); | ||
function computeScore(path2) { | ||
const segments = path2.split("/"); | ||
let initialScore = segments.length; | ||
@@ -281,4 +371,10 @@ if (segments.some(isSplat)) { | ||
const { location } = history; | ||
const routes = "routes" in options ? options.routes : options.config.activities.reduce( | ||
(acc, a) => __spreadProps(__spreadValues({}, acc), { | ||
[a.name]: a.path | ||
}), | ||
{} | ||
); | ||
const activityRoutes = sortActivityRoutes( | ||
normalizeActivityRouteMap(options.routes) | ||
normalizeActivityRouteMap(routes) | ||
); | ||
@@ -566,6 +662,6 @@ const plugin = () => { | ||
const template = makeTemplate(match, options.urlPatternOptions); | ||
const path = template.fill(actionParams.activityParams); | ||
const path2 = template.fill(actionParams.activityParams); | ||
overrideActionParams(__spreadProps(__spreadValues({}, actionParams), { | ||
activityContext: __spreadProps(__spreadValues({}, actionParams.activityContext), { | ||
path | ||
path: path2 | ||
}) | ||
@@ -582,6 +678,6 @@ })); | ||
const template = makeTemplate(match, options.urlPatternOptions); | ||
const path = template.fill(actionParams.activityParams); | ||
const path2 = template.fill(actionParams.activityParams); | ||
overrideActionParams(__spreadProps(__spreadValues({}, actionParams), { | ||
activityContext: __spreadProps(__spreadValues({}, actionParams.activityContext), { | ||
path | ||
path: path2 | ||
}) | ||
@@ -595,3 +691,3 @@ })); | ||
if (previousActivity) { | ||
for (let i = 0; i < previousActivity.steps.length - 1; i += 1) { | ||
for (let i2 = 0; i2 < previousActivity.steps.length - 1; i2 += 1) { | ||
requestHistoryTick((resolve) => { | ||
@@ -633,3 +729,3 @@ if (!safeParseState(getCurrentState({ history }))) { | ||
const popCount = isRoot ? 0 : steps.length; | ||
for (let i = 0; i < popCount; i += 1) { | ||
for (let i2 = 0; i2 < popCount; i2 += 1) { | ||
requestHistoryTick((resolve) => { | ||
@@ -636,0 +732,0 @@ if (!safeParseState(getCurrentState({ history }))) { |
@@ -12,3 +12,3 @@ "use strict"; | ||
var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __defNormalProp = (obj, key, value2) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value: value2 }) : obj[key] = value2; | ||
var __spreadValues = (a, b) => { | ||
@@ -26,5 +26,8 @@ for (var prop in b || (b = {})) | ||
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); | ||
var __commonJS = (cb, mod) => function __require() { | ||
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; | ||
}; | ||
var __export = (target, all) => { | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
for (var name2 in all) | ||
__defProp(target, name2, { get: all[name2], enumerable: true }); | ||
}; | ||
@@ -49,2 +52,97 @@ var __copyProps = (to, from, except, desc) => { | ||
// ../../.yarn/cache/json-cycle-npm-1.5.0-4c80b4d041-4ce7594eb8.zip/node_modules/json-cycle/cycle.js | ||
var require_cycle = __commonJS({ | ||
"../../.yarn/cache/json-cycle-npm-1.5.0-4c80b4d041-4ce7594eb8.zip/node_modules/json-cycle/cycle.js"(exports, module) { | ||
"use strict"; | ||
(function(global, factory) { | ||
typeof define === "function" && define.amd ? define(factory) : typeof module === "object" && module.exports ? module.exports = factory : global.json = factory; | ||
})(exports, /* @__PURE__ */ function() { | ||
function decycle(object) { | ||
var objects = [], paths = []; | ||
return function derez(value2, path2) { | ||
var i2, name2, nu; | ||
var _value = value2; | ||
try { | ||
_value = value2.toJSON(); | ||
} catch (error) { | ||
} | ||
if (typeof _value === "object" && _value) { | ||
for (i2 = 0; i2 < objects.length; i2 += 1) { | ||
if (objects[i2] === _value) { | ||
return { $ref: paths[i2] }; | ||
} | ||
} | ||
objects.push(_value); | ||
paths.push(path2); | ||
if (Object.prototype.toString.apply(_value) === "[object Array]") { | ||
nu = []; | ||
for (i2 = 0; i2 < _value.length; i2 += 1) { | ||
nu[i2] = derez(_value[i2], path2 + "[" + i2 + "]"); | ||
} | ||
} else { | ||
nu = {}; | ||
for (name2 in _value) { | ||
if (Object.prototype.hasOwnProperty.call(_value, name2)) { | ||
nu[name2] = derez( | ||
_value[name2], | ||
path2 + "[" + JSON.stringify(name2) + "]" | ||
); | ||
} | ||
} | ||
} | ||
return nu; | ||
} | ||
return _value; | ||
}(object, "$"); | ||
} | ||
function retrocycle($) { | ||
var px = /^\$(?:\[(?:\d+|\"(?:[^\\\"\u0000-\u001f]|\\([\\\"\/bfnrt]|u[0-9a-zA-Z]{4}))*\")\])*$/; | ||
(function rez(value) { | ||
var i, item, name, path; | ||
if (value && typeof value === "object") { | ||
if (Object.prototype.toString.apply(value) === "[object Array]") { | ||
for (i = 0; i < value.length; i += 1) { | ||
item = value[i]; | ||
if (item && typeof item === "object") { | ||
path = item.$ref; | ||
if (typeof path === "string" && px.test(path)) { | ||
value[i] = eval(path); | ||
} else { | ||
rez(item); | ||
} | ||
} | ||
} | ||
} else { | ||
for (name in value) { | ||
if (typeof value[name] === "object") { | ||
item = value[name]; | ||
if (item) { | ||
path = item.$ref; | ||
if (typeof path === "string" && px.test(path)) { | ||
value[name] = eval(path); | ||
} else { | ||
rez(item); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
})($); | ||
return $; | ||
} | ||
return { | ||
decycle, | ||
retrocycle, | ||
stringify: function(object, replacer, space) { | ||
return JSON.stringify(decycle(object), replacer, space); | ||
}, | ||
parse: function($2, reviver) { | ||
return retrocycle(JSON.parse($2, reviver)); | ||
} | ||
}; | ||
}()); | ||
} | ||
}); | ||
// src/solid/index.ts | ||
@@ -62,9 +160,9 @@ var solid_exports = {}; | ||
var import_url_pattern = __toESM(require("url-pattern")); | ||
function pathToUrl(path) { | ||
return new URL(path, "file://"); | ||
function pathToUrl(path2) { | ||
return new URL(path2, "file://"); | ||
} | ||
function urlSearchParamsToMap(urlSearchParams) { | ||
const map = {}; | ||
urlSearchParams.forEach((value, key) => { | ||
map[key] = value; | ||
urlSearchParams.forEach((value2, key) => { | ||
map[key] = value2; | ||
}); | ||
@@ -86,5 +184,5 @@ return map; | ||
} | ||
function makeTemplate({ path, decode }, urlPatternOptions) { | ||
const pattern = new import_url_pattern.default(`${path}(/)`, urlPatternOptions); | ||
const onlyAsterisk = path === "*" || path === "/*"; | ||
function makeTemplate({ path: path2, decode }, urlPatternOptions) { | ||
const pattern = new import_url_pattern.default(`${path2}(/)`, urlPatternOptions); | ||
const onlyAsterisk = path2 === "*" || path2 === "/*"; | ||
const variableCount = onlyAsterisk ? Number.POSITIVE_INFINITY : pattern.names.length; | ||
@@ -101,4 +199,4 @@ return { | ||
Object.entries(searchParamsMap).reduce( | ||
(acc, [key, value]) => __spreadValues(__spreadValues({}, acc), value ? { | ||
[key]: value | ||
(acc, [key, value2]) => __spreadValues(__spreadValues({}, acc), value2 ? { | ||
[key]: value2 | ||
} : null), | ||
@@ -110,4 +208,4 @@ {} | ||
}, | ||
parse(path2) { | ||
const url = pathToUrl(path2); | ||
parse(path3) { | ||
const url = pathToUrl(path3); | ||
const pathParams = pattern.match(url.pathname); | ||
@@ -151,18 +249,10 @@ const searchParams = urlSearchParamsToMap(url.searchParams); | ||
// src/common/historyState.ts | ||
var import_json_cycle = __toESM(require_cycle()); | ||
var STATE_TAG = "@contentstech/stackflow-plugin-history-sync"; | ||
function clone(input) { | ||
return JSON.parse(JSON.stringify(input)); | ||
} | ||
function serializeStep(step) { | ||
return clone(step); | ||
} | ||
function serializeActivity(activity) { | ||
return clone(activity); | ||
} | ||
function serializeState(state) { | ||
return { | ||
return (0, import_json_cycle.decycle)({ | ||
_TAG: STATE_TAG, | ||
activity: serializeActivity(state.activity), | ||
step: state.step ? serializeStep(state.step) : void 0 | ||
}; | ||
activity: state.activity, | ||
step: state.step | ||
}); | ||
} | ||
@@ -172,3 +262,3 @@ function safeParseState(state) { | ||
if (typeof _state === "object" && _state !== null && "_TAG" in _state && typeof _state._TAG === "string" && _state._TAG === STATE_TAG) { | ||
return state; | ||
return (0, import_json_cycle.retrocycle)(state); | ||
} | ||
@@ -225,4 +315,4 @@ return null; | ||
// src/common/normalizeRouteInput.ts | ||
function makeRoute(path) { | ||
return typeof path === "string" ? { path } : path; | ||
function makeRoute(path2) { | ||
return typeof path2 === "string" ? { path: path2 } : path2; | ||
} | ||
@@ -254,4 +344,4 @@ function normalizeRouteInput(route) { | ||
var isSplat = (s) => s === "*"; | ||
function computeScore(path) { | ||
const segments = path.split("/"); | ||
function computeScore(path2) { | ||
const segments = path2.split("/"); | ||
let initialScore = segments.length; | ||
@@ -281,4 +371,10 @@ if (segments.some(isSplat)) { | ||
const { location } = history; | ||
const routes = "routes" in options ? options.routes : options.config.activities.reduce( | ||
(acc, a) => __spreadProps(__spreadValues({}, acc), { | ||
[a.name]: a.path | ||
}), | ||
{} | ||
); | ||
const activityRoutes = sortActivityRoutes( | ||
normalizeActivityRouteMap(options.routes) | ||
normalizeActivityRouteMap(routes) | ||
); | ||
@@ -566,6 +662,6 @@ const plugin = () => { | ||
const template = makeTemplate(match, options.urlPatternOptions); | ||
const path = template.fill(actionParams.activityParams); | ||
const path2 = template.fill(actionParams.activityParams); | ||
overrideActionParams(__spreadProps(__spreadValues({}, actionParams), { | ||
activityContext: __spreadProps(__spreadValues({}, actionParams.activityContext), { | ||
path | ||
path: path2 | ||
}) | ||
@@ -582,6 +678,6 @@ })); | ||
const template = makeTemplate(match, options.urlPatternOptions); | ||
const path = template.fill(actionParams.activityParams); | ||
const path2 = template.fill(actionParams.activityParams); | ||
overrideActionParams(__spreadProps(__spreadValues({}, actionParams), { | ||
activityContext: __spreadProps(__spreadValues({}, actionParams.activityContext), { | ||
path | ||
path: path2 | ||
}) | ||
@@ -595,3 +691,3 @@ })); | ||
if (previousActivity) { | ||
for (let i = 0; i < previousActivity.steps.length - 1; i += 1) { | ||
for (let i2 = 0; i2 < previousActivity.steps.length - 1; i2 += 1) { | ||
requestHistoryTick((resolve) => { | ||
@@ -633,3 +729,3 @@ if (!safeParseState(getCurrentState({ history }))) { | ||
const popCount = isRoot ? 0 : steps.length; | ||
for (let i = 0; i < popCount; i += 1) { | ||
for (let i2 = 0; i2 < popCount; i2 += 1) { | ||
requestHistoryTick((resolve) => { | ||
@@ -636,0 +732,0 @@ if (!safeParseState(getCurrentState({ history }))) { |
@@ -12,3 +12,3 @@ "use strict"; | ||
var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __defNormalProp = (obj, key, value2) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value: value2 }) : obj[key] = value2; | ||
var __spreadValues = (a, b) => { | ||
@@ -26,5 +26,8 @@ for (var prop in b || (b = {})) | ||
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); | ||
var __commonJS = (cb, mod) => function __require() { | ||
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; | ||
}; | ||
var __export = (target, all) => { | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
for (var name2 in all) | ||
__defProp(target, name2, { get: all[name2], enumerable: true }); | ||
}; | ||
@@ -49,2 +52,97 @@ var __copyProps = (to, from, except, desc) => { | ||
// ../../.yarn/cache/json-cycle-npm-1.5.0-4c80b4d041-4ce7594eb8.zip/node_modules/json-cycle/cycle.js | ||
var require_cycle = __commonJS({ | ||
"../../.yarn/cache/json-cycle-npm-1.5.0-4c80b4d041-4ce7594eb8.zip/node_modules/json-cycle/cycle.js"(exports, module) { | ||
"use strict"; | ||
(function(global, factory) { | ||
typeof define === "function" && define.amd ? define(factory) : typeof module === "object" && module.exports ? module.exports = factory : global.json = factory; | ||
})(exports, /* @__PURE__ */ function() { | ||
function decycle(object) { | ||
var objects = [], paths = []; | ||
return function derez(value2, path2) { | ||
var i2, name2, nu; | ||
var _value = value2; | ||
try { | ||
_value = value2.toJSON(); | ||
} catch (error) { | ||
} | ||
if (typeof _value === "object" && _value) { | ||
for (i2 = 0; i2 < objects.length; i2 += 1) { | ||
if (objects[i2] === _value) { | ||
return { $ref: paths[i2] }; | ||
} | ||
} | ||
objects.push(_value); | ||
paths.push(path2); | ||
if (Object.prototype.toString.apply(_value) === "[object Array]") { | ||
nu = []; | ||
for (i2 = 0; i2 < _value.length; i2 += 1) { | ||
nu[i2] = derez(_value[i2], path2 + "[" + i2 + "]"); | ||
} | ||
} else { | ||
nu = {}; | ||
for (name2 in _value) { | ||
if (Object.prototype.hasOwnProperty.call(_value, name2)) { | ||
nu[name2] = derez( | ||
_value[name2], | ||
path2 + "[" + JSON.stringify(name2) + "]" | ||
); | ||
} | ||
} | ||
} | ||
return nu; | ||
} | ||
return _value; | ||
}(object, "$"); | ||
} | ||
function retrocycle($) { | ||
var px = /^\$(?:\[(?:\d+|\"(?:[^\\\"\u0000-\u001f]|\\([\\\"\/bfnrt]|u[0-9a-zA-Z]{4}))*\")\])*$/; | ||
(function rez(value) { | ||
var i, item, name, path; | ||
if (value && typeof value === "object") { | ||
if (Object.prototype.toString.apply(value) === "[object Array]") { | ||
for (i = 0; i < value.length; i += 1) { | ||
item = value[i]; | ||
if (item && typeof item === "object") { | ||
path = item.$ref; | ||
if (typeof path === "string" && px.test(path)) { | ||
value[i] = eval(path); | ||
} else { | ||
rez(item); | ||
} | ||
} | ||
} | ||
} else { | ||
for (name in value) { | ||
if (typeof value[name] === "object") { | ||
item = value[name]; | ||
if (item) { | ||
path = item.$ref; | ||
if (typeof path === "string" && px.test(path)) { | ||
value[name] = eval(path); | ||
} else { | ||
rez(item); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
})($); | ||
return $; | ||
} | ||
return { | ||
decycle, | ||
retrocycle, | ||
stringify: function(object, replacer, space) { | ||
return JSON.stringify(decycle(object), replacer, space); | ||
}, | ||
parse: function($2, reviver) { | ||
return retrocycle(JSON.parse($2, reviver)); | ||
} | ||
}; | ||
}()); | ||
} | ||
}); | ||
// src/solid/index.ts | ||
@@ -62,9 +160,9 @@ var solid_exports = {}; | ||
var import_url_pattern = __toESM(require("url-pattern")); | ||
function pathToUrl(path) { | ||
return new URL(path, "file://"); | ||
function pathToUrl(path2) { | ||
return new URL(path2, "file://"); | ||
} | ||
function urlSearchParamsToMap(urlSearchParams) { | ||
const map = {}; | ||
urlSearchParams.forEach((value, key) => { | ||
map[key] = value; | ||
urlSearchParams.forEach((value2, key) => { | ||
map[key] = value2; | ||
}); | ||
@@ -86,5 +184,5 @@ return map; | ||
} | ||
function makeTemplate({ path, decode }, urlPatternOptions) { | ||
const pattern = new import_url_pattern.default(`${path}(/)`, urlPatternOptions); | ||
const onlyAsterisk = path === "*" || path === "/*"; | ||
function makeTemplate({ path: path2, decode }, urlPatternOptions) { | ||
const pattern = new import_url_pattern.default(`${path2}(/)`, urlPatternOptions); | ||
const onlyAsterisk = path2 === "*" || path2 === "/*"; | ||
const variableCount = onlyAsterisk ? Number.POSITIVE_INFINITY : pattern.names.length; | ||
@@ -101,4 +199,4 @@ return { | ||
Object.entries(searchParamsMap).reduce( | ||
(acc, [key, value]) => __spreadValues(__spreadValues({}, acc), value ? { | ||
[key]: value | ||
(acc, [key, value2]) => __spreadValues(__spreadValues({}, acc), value2 ? { | ||
[key]: value2 | ||
} : null), | ||
@@ -110,4 +208,4 @@ {} | ||
}, | ||
parse(path2) { | ||
const url = pathToUrl(path2); | ||
parse(path3) { | ||
const url = pathToUrl(path3); | ||
const pathParams = pattern.match(url.pathname); | ||
@@ -144,18 +242,10 @@ const searchParams = urlSearchParamsToMap(url.searchParams); | ||
// src/common/historyState.ts | ||
var import_json_cycle = __toESM(require_cycle()); | ||
var STATE_TAG = "@contentstech/stackflow-plugin-history-sync"; | ||
function clone(input) { | ||
return JSON.parse(JSON.stringify(input)); | ||
} | ||
function serializeStep(step) { | ||
return clone(step); | ||
} | ||
function serializeActivity(activity) { | ||
return clone(activity); | ||
} | ||
function serializeState(state) { | ||
return { | ||
return (0, import_json_cycle.decycle)({ | ||
_TAG: STATE_TAG, | ||
activity: serializeActivity(state.activity), | ||
step: state.step ? serializeStep(state.step) : void 0 | ||
}; | ||
activity: state.activity, | ||
step: state.step | ||
}); | ||
} | ||
@@ -165,3 +255,3 @@ function safeParseState(state) { | ||
if (typeof _state === "object" && _state !== null && "_TAG" in _state && typeof _state._TAG === "string" && _state._TAG === STATE_TAG) { | ||
return state; | ||
return (0, import_json_cycle.retrocycle)(state); | ||
} | ||
@@ -218,4 +308,4 @@ return null; | ||
// src/common/normalizeRouteInput.ts | ||
function makeRoute(path) { | ||
return typeof path === "string" ? { path } : path; | ||
function makeRoute(path2) { | ||
return typeof path2 === "string" ? { path: path2 } : path2; | ||
} | ||
@@ -247,4 +337,4 @@ function normalizeRouteInput(route) { | ||
var isSplat = (s) => s === "*"; | ||
function computeScore(path) { | ||
const segments = path.split("/"); | ||
function computeScore(path2) { | ||
const segments = path2.split("/"); | ||
let initialScore = segments.length; | ||
@@ -274,4 +364,10 @@ if (segments.some(isSplat)) { | ||
const { location } = history; | ||
const routes = "routes" in options ? options.routes : options.config.activities.reduce( | ||
(acc, a) => __spreadProps(__spreadValues({}, acc), { | ||
[a.name]: a.path | ||
}), | ||
{} | ||
); | ||
const activityRoutes = sortActivityRoutes( | ||
normalizeActivityRouteMap(options.routes) | ||
normalizeActivityRouteMap(routes) | ||
); | ||
@@ -559,6 +655,6 @@ const plugin = () => { | ||
const template = makeTemplate(match, options.urlPatternOptions); | ||
const path = template.fill(actionParams.activityParams); | ||
const path2 = template.fill(actionParams.activityParams); | ||
overrideActionParams(__spreadProps(__spreadValues({}, actionParams), { | ||
activityContext: __spreadProps(__spreadValues({}, actionParams.activityContext), { | ||
path | ||
path: path2 | ||
}) | ||
@@ -575,6 +671,6 @@ })); | ||
const template = makeTemplate(match, options.urlPatternOptions); | ||
const path = template.fill(actionParams.activityParams); | ||
const path2 = template.fill(actionParams.activityParams); | ||
overrideActionParams(__spreadProps(__spreadValues({}, actionParams), { | ||
activityContext: __spreadProps(__spreadValues({}, actionParams.activityContext), { | ||
path | ||
path: path2 | ||
}) | ||
@@ -588,3 +684,3 @@ })); | ||
if (previousActivity) { | ||
for (let i = 0; i < previousActivity.steps.length - 1; i += 1) { | ||
for (let i2 = 0; i2 < previousActivity.steps.length - 1; i2 += 1) { | ||
requestHistoryTick((resolve) => { | ||
@@ -626,3 +722,3 @@ if (!safeParseState(getCurrentState({ history }))) { | ||
const popCount = isRoot ? 0 : steps.length; | ||
for (let i = 0; i < popCount; i += 1) { | ||
for (let i2 = 0; i2 < popCount; i2 += 1) { | ||
requestHistoryTick((resolve) => { | ||
@@ -629,0 +725,0 @@ if (!safeParseState(getCurrentState({ history }))) { |
279
package.json
{ | ||
"name": "@contentstech/stackflow-plugin-history-sync", | ||
"version": "0.2.0", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/contentstech-com/stackflow.git", | ||
"directory": "extensions/plugin-history-sync" | ||
}, | ||
"license": "MIT", | ||
"exports": { | ||
".": { | ||
"types": "./dist/react/index.d.ts", | ||
"require": "./dist/react/index.js", | ||
"import": "./dist/react/index.mjs" | ||
}, | ||
"./react": { | ||
"types": "./dist/react/index.d.ts", | ||
"require": "./dist/react/index.js", | ||
"import": "./dist/react/index.mjs" | ||
}, | ||
"./solid": { | ||
"types": "./dist/solid/index.d.ts", | ||
"solid": { | ||
"require": "./dist/solid/index.jsx", | ||
"import": "./dist/solid/index.mjsx" | ||
}, | ||
"browser": { | ||
"require": { | ||
"solid": "./dist/solid/index.browser.jsx", | ||
"default": "./dist/solid/index.browser.js" | ||
}, | ||
"import": { | ||
"solid": "./dist/solid/index.browser.mjsx", | ||
"default": "./dist/solid/index.browser.mjs" | ||
} | ||
}, | ||
"default": { | ||
"require": { | ||
"default": "./dist/solid/index.js" | ||
}, | ||
"import": { | ||
"solid": "./dist/solid/index.mjsx", | ||
"default": "./dist/solid/index.mjs" | ||
} | ||
} | ||
} | ||
}, | ||
"main": "./dist/react/index.js", | ||
"module": "./dist/react/index.mjs", | ||
"types": "./dist/react/index.d.ts", | ||
"typesVersions": { | ||
"*": { | ||
"*": [ | ||
"dist/react/index.d.ts" | ||
], | ||
"solid": [ | ||
"dist/solid/index.d.ts" | ||
] | ||
} | ||
}, | ||
"files": [ | ||
"dist", | ||
"src", | ||
"README.md" | ||
], | ||
"scripts": { | ||
"build": "yarn build:js && yarn build:dts", | ||
"build:dts": "tsc --emitDeclarationOnly", | ||
"build:js": "node ./esbuild.config.js", | ||
"clean": "rimraf dist", | ||
"dev": "yarn build:js --watch && yarn build:dts --watch && yarn test --watch", | ||
"test": "yarn jest", | ||
"typecheck": "tsc --noEmit" | ||
}, | ||
"jest": { | ||
"coveragePathIgnorePatterns": [ | ||
"index.ts", | ||
"index.tsx" | ||
], | ||
"transform": { | ||
"^.+\\.(t|j)sx?$": "@swc/jest" | ||
} | ||
}, | ||
"dependencies": { | ||
"history": "^5.3.0", | ||
"url-pattern": "^1.0.3" | ||
}, | ||
"devDependencies": { | ||
"@contentstech/stackflow-solid": "^0.1.0", | ||
"@stackflow/core": "^1.0.13", | ||
"@stackflow/esbuild-config": "^1.0.3", | ||
"@stackflow/react": "^1.1.11", | ||
"@swc/core": "^1.6.6", | ||
"@swc/jest": "^0.2.36", | ||
"@types/jest": "^29.5.12", | ||
"@types/node": "^20.14.9", | ||
"@types/react": "^18.3.3", | ||
"esbuild": "^0.23.0", | ||
"esbuild-plugin-solid": "^0.6.0", | ||
"jest": "^29.7.0", | ||
"react": "^18.3.1", | ||
"rimraf": "^3.0.2", | ||
"solid-js": "^1.8.18", | ||
"typescript": "^5.5.3" | ||
}, | ||
"peerDependencies": { | ||
"@contentstech/stackflow-solid": "^0.1", | ||
"@stackflow/core": "^1", | ||
"@stackflow/react": "^1", | ||
"@types/react": ">=16.8.0", | ||
"react": ">=16.8.0", | ||
"solid-js": ">=1.4.0" | ||
}, | ||
"peerDependenciesMeta": { | ||
"@contentstech/stackflow-solid": { | ||
"optional": true | ||
}, | ||
"@stackflow/react": { | ||
"optional": true | ||
}, | ||
"@types/react": { | ||
"optional": true | ||
}, | ||
"react": { | ||
"optional": true | ||
}, | ||
"solid-js": { | ||
"optional": true | ||
} | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"ultra": { | ||
"concurrent": [ | ||
"dev", | ||
"build" | ||
] | ||
} | ||
"name": "@contentstech/stackflow-plugin-history-sync", | ||
"version": "0.3.0", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/contentstech-com/stackflow.git", | ||
"directory": "extensions/plugin-history-sync" | ||
}, | ||
"license": "MIT", | ||
"exports": { | ||
".": { | ||
"types": "./dist/react/index.d.ts", | ||
"require": "./dist/react/index.js", | ||
"import": "./dist/react/index.mjs" | ||
}, | ||
"./react": { | ||
"types": "./dist/react/index.d.ts", | ||
"require": "./dist/react/index.js", | ||
"import": "./dist/react/index.mjs" | ||
}, | ||
"./solid": { | ||
"types": "./dist/solid/index.d.ts", | ||
"solid": { | ||
"require": "./dist/solid/index.jsx", | ||
"import": "./dist/solid/index.mjsx" | ||
}, | ||
"browser": { | ||
"require": { | ||
"solid": "./dist/solid/index.browser.jsx", | ||
"default": "./dist/solid/index.browser.js" | ||
}, | ||
"import": { | ||
"solid": "./dist/solid/index.browser.mjsx", | ||
"default": "./dist/solid/index.browser.mjs" | ||
} | ||
}, | ||
"default": { | ||
"require": { | ||
"default": "./dist/solid/index.js" | ||
}, | ||
"import": { | ||
"solid": "./dist/solid/index.mjsx", | ||
"default": "./dist/solid/index.mjs" | ||
} | ||
} | ||
} | ||
}, | ||
"main": "./dist/react/index.js", | ||
"module": "./dist/react/index.mjs", | ||
"types": "./dist/react/index.d.ts", | ||
"typesVersions": { | ||
"*": { | ||
"*": [ | ||
"dist/react/index.d.ts" | ||
], | ||
"solid": [ | ||
"dist/solid/index.d.ts" | ||
] | ||
} | ||
}, | ||
"files": [ | ||
"dist", | ||
"src", | ||
"README.md" | ||
], | ||
"scripts": { | ||
"build": "yarn build:js && yarn build:dts", | ||
"build:dts": "tsc --emitDeclarationOnly", | ||
"build:js": "node ./esbuild.config.js", | ||
"clean": "rimraf dist", | ||
"dev": "yarn build:js --watch && yarn build:dts --watch && yarn test --watch", | ||
"test": "yarn jest", | ||
"typecheck": "tsc --noEmit" | ||
}, | ||
"jest": { | ||
"coveragePathIgnorePatterns": [ | ||
"index.ts", | ||
"index.tsx" | ||
], | ||
"transform": { | ||
"^.+\\.(t|j)sx?$": "@swc/jest" | ||
} | ||
}, | ||
"dependencies": { | ||
"history": "^5.3.0", | ||
"url-pattern": "^1.0.3" | ||
}, | ||
"devDependencies": { | ||
"@contentstech/stackflow-solid": "^0.2.0", | ||
"@stackflow/config": "^1.1.0", | ||
"@stackflow/core": "^1.1.0", | ||
"@stackflow/esbuild-config": "^1.0.3", | ||
"@stackflow/react": "^1.3.0", | ||
"@swc/core": "^1.6.6", | ||
"@swc/jest": "^0.2.36", | ||
"@types/jest": "^29.5.12", | ||
"@types/node": "^20.14.9", | ||
"@types/react": "^18.3.3", | ||
"esbuild": "^0.23.0", | ||
"esbuild-plugin-solid": "^0.6.0", | ||
"jest": "^29.7.0", | ||
"json-cycle": "^1.5.0", | ||
"react": "^18.3.1", | ||
"rimraf": "^3.0.2", | ||
"solid-js": "^1.8.18", | ||
"typescript": "^5.5.3" | ||
}, | ||
"peerDependencies": { | ||
"@contentstech/stackflow-solid": "^0.2.0", | ||
"@stackflow/config": "^1.0.1-canary.0", | ||
"@stackflow/core": "^1.1.0-canary.0", | ||
"@stackflow/react": "^1.3.0-canary.0", | ||
"@types/react": ">=16.8.0", | ||
"react": ">=16.8.0", | ||
"solid-js": ">=1.4.0" | ||
}, | ||
"peerDependenciesMeta": { | ||
"@contentstech/stackflow-solid": { | ||
"optional": true | ||
}, | ||
"@stackflow/react": { | ||
"optional": true | ||
}, | ||
"@types/react": { | ||
"optional": true | ||
}, | ||
"react": { | ||
"optional": true | ||
}, | ||
"solid-js": { | ||
"optional": true | ||
} | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"ultra": { | ||
"concurrent": [ | ||
"dev", | ||
"build" | ||
] | ||
} | ||
} |
import type { Activity, ActivityStep } from "@stackflow/core"; | ||
import type { History } from "history"; | ||
import { decycle, retrocycle } from "json-cycle"; | ||
@@ -15,20 +16,8 @@ const STATE_TAG = "@contentstech/stackflow-plugin-history-sync"; | ||
function clone<T>(input: T): T { | ||
return JSON.parse(JSON.stringify(input)); | ||
} | ||
function serializeStep(step: ActivityStep): ActivityStep { | ||
return clone(step); | ||
} | ||
function serializeActivity(activity: Activity): Activity { | ||
return clone(activity); | ||
} | ||
function serializeState(state: State): SerializedState { | ||
return { | ||
return decycle({ | ||
_TAG: STATE_TAG, | ||
activity: serializeActivity(state.activity), | ||
step: state.step ? serializeStep(state.step) : undefined, | ||
}; | ||
activity: state.activity, | ||
step: state.step, | ||
}); | ||
} | ||
@@ -46,3 +35,3 @@ | ||
) { | ||
return state as State; | ||
return retrocycle<State>(state); | ||
} | ||
@@ -49,0 +38,0 @@ |
@@ -1329,2 +1329,47 @@ import type { | ||
}); | ||
test("historySyncPlugin - activity.context에 cyclic dependency가 있어도 정상적으로 로드됩니다", async () => { | ||
history = createMemoryHistory({ | ||
initialEntries: ["/home"], | ||
}); | ||
const coreStore = stackflow({ | ||
activityNames: ["Home", "Article"], | ||
plugins: [ | ||
historySyncPlugin({ | ||
history, | ||
routes: { | ||
Home: "/home", | ||
Article: "/articles/:articleId", | ||
}, | ||
fallbackActivity: () => "Home", | ||
}), | ||
], | ||
}); | ||
actions = makeActionsProxy({ | ||
actions: coreStore.actions, | ||
}); | ||
const cyclic: any = {}; | ||
cyclic.self = cyclic; | ||
await actions.push({ | ||
activityId: "a1", | ||
activityName: "Article", | ||
activityParams: { | ||
articleId: "1", | ||
}, | ||
activityContext: { | ||
cyclic, | ||
}, | ||
}); | ||
const stack = await actions.getStack(); | ||
expect( | ||
(stack.activities[1].context as any).cyclic === | ||
(stack.activities[1].context as any).cyclic.self, | ||
).toEqual(true); | ||
}); | ||
}); |
@@ -0,3 +1,3 @@ | ||
import type { ActivityComponentType as SolidActivityComponentType } from "@contentstech/stackflow-solid"; | ||
import type { ActivityComponentType as ReactActivityComponentType } from "@stackflow/react"; | ||
import type { ActivityComponentType as SolidActivityComponentType } from "@contentstech/stackflow-solid"; | ||
@@ -4,0 +4,0 @@ export type Route<K> = { |
@@ -28,4 +28,4 @@ import type { ActivityRoute } from "./ActivityRoute"; | ||
: segment === "" | ||
? emptySegmentValue | ||
: staticSegmentValue), | ||
? emptySegmentValue | ||
: staticSegmentValue), | ||
initialScore, | ||
@@ -32,0 +32,0 @@ ); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
826205
138
9223
9
18
16