goatly 0.1.0 → 0.1.2
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/README.md +874 -1
- package/bin/goatly.js +99 -0
- package/dist/cli/index.js +234 -0
- package/dist/compat/gatsby-node-entry.js +16 -0
- package/dist/compat/gatsby-package.json +6 -0
- package/dist/compat/md5-file.js +47 -0
- package/dist/compat-apis.json +72 -0
- package/dist/compat-apis.json.js +62 -0
- package/dist/graphql.js +3 -0
- package/dist/node/actions/index.js +619 -0
- package/dist/node/actions/job-store.js +180 -0
- package/dist/node/actions/jobs.js +174 -0
- package/dist/node/api-runner.js +258 -0
- package/dist/node/bootstrap.js +569 -0
- package/dist/node/build/assets.js +110 -0
- package/dist/node/build/incremental.js +208 -0
- package/dist/node/build/index.js +763 -0
- package/dist/node/build/memory-governor.js +450 -0
- package/dist/node/build/output-dirs.js +70 -0
- package/dist/node/build/page-data.js +40 -0
- package/dist/node/build/page-files.js +186 -0
- package/dist/node/build/partitioned-state.js +258 -0
- package/dist/node/build/query-worker.js +626 -0
- package/dist/node/build/query-workers.js +863 -0
- package/dist/node/build/render-thread.js +103 -0
- package/dist/node/build/render-worker.js +162 -0
- package/dist/node/build/state.js +24 -0
- package/dist/node/build/timing-breakdown.js +216 -0
- package/dist/node/build/work-clock.js +75 -0
- package/dist/node/build/worker-pool.js +242 -0
- package/dist/node/cache-layout.js +99 -0
- package/dist/node/cache.js +1430 -0
- package/dist/node/compat/chokidar-shim.js +148 -0
- package/dist/node/compat/core-utils-lmdb.js +145 -0
- package/dist/node/compat/create-node-id-shim.js +1 -0
- package/dist/node/compat/module-hooks.js +245 -0
- package/dist/node/compat/redux-shim.js +1 -0
- package/dist/node/config/index.js +162 -0
- package/dist/node/config/not-found-pages.js +95 -0
- package/dist/node/datastore/codec.js +144 -0
- package/dist/node/datastore/index.js +127 -0
- package/dist/node/datastore/logfile/cache.js +89 -0
- package/dist/node/datastore/logfile/index.js +1329 -0
- package/dist/node/datastore/logfile/record.js +71 -0
- package/dist/node/datastore/types.js +1 -0
- package/dist/node/develop/express-compat.js +207 -0
- package/dist/node/develop/graphiql.js +22 -0
- package/dist/node/develop/hydration-report.js +41 -0
- package/dist/node/develop/index.js +618 -0
- package/dist/node/develop/local-url.js +10 -0
- package/dist/node/develop/ssr-runner.js +66 -0
- package/dist/node/gql.js +5 -0
- package/dist/node/index.js +15 -0
- package/dist/node/info.js +270 -0
- package/dist/node/internal-plugins/data-bridge.js +196 -0
- package/dist/node/internal-plugins/page-creator.js +318 -0
- package/dist/node/loader/import-module.js +160 -0
- package/dist/node/node-helpers.js +66 -0
- package/dist/node/not-found-pages.js +220 -0
- package/dist/node/plugins/api-list.js +12 -0
- package/dist/node/plugins/load-plugins.js +427 -0
- package/dist/node/plugins/resolve-plugin.js +79 -0
- package/dist/node/plugins/static-exports.js +64 -0
- package/dist/node/query/compile.js +326 -0
- package/dist/node/query/dependencies.js +45 -0
- package/dist/node/query/execute.js +417 -0
- package/dist/node/query/extract.js +372 -0
- package/dist/node/query/gql-tada-introspection.js +124 -0
- package/dist/node/query/graphql-env.js +231 -0
- package/dist/node/query/graphql-runner.js +79 -0
- package/dist/node/redux/actions.js +1 -0
- package/dist/node/redux/index.js +133 -0
- package/dist/node/redux/node-changes.js +128 -0
- package/dist/node/redux/reducers.js +193 -0
- package/dist/node/redux/state.js +71 -0
- package/dist/node/renderer.js +124 -0
- package/dist/node/reporter-transports.js +259 -0
- package/dist/node/reporter.js +497 -0
- package/dist/node/schema/date.js +539 -0
- package/dist/node/schema/extensions.js +315 -0
- package/dist/node/schema/index.js +542 -0
- package/dist/node/schema/infer.js +388 -0
- package/dist/node/schema/inline-objects.js +128 -0
- package/dist/node/schema/naming.js +20 -0
- package/dist/node/schema/node-model.js +207 -0
- package/dist/node/schema/query-engine.js +1808 -0
- package/dist/node/schema/resolver-kinds.js +7 -0
- package/dist/node/schema/resolvers.js +273 -0
- package/dist/node/schema/type-builders.js +45 -0
- package/dist/node/schema/types.js +241 -0
- package/dist/node/serve.js +90 -0
- package/dist/node/site-page-nodes.js +277 -0
- package/dist/node/types.js +1 -0
- package/dist/node/utils/content-digest.js +107 -0
- package/dist/node/utils/cpus.js +104 -0
- package/dist/node/utils/create-node-id.js +51 -0
- package/dist/node/utils/env.js +25 -0
- package/dist/node/utils/fs-retry.js +74 -0
- package/dist/node/utils/memory-signals.js +441 -0
- package/dist/node/utils/memory.js +657 -0
- package/dist/node/utils/murmurhash.js +32 -0
- package/dist/node/utils/parser.js +33 -0
- package/dist/node/utils/partition-hash.js +19 -0
- package/dist/node/utils/paths.js +122 -0
- package/dist/node/utils/resolver.js +48 -0
- package/dist/node/utils/runtime.js +5 -0
- package/dist/node/utils/serializable.js +41 -0
- package/dist/node/utils/strings.js +41 -0
- package/dist/node/vite/config.js +289 -0
- package/dist/node/vite/paths.js +12 -0
- package/dist/node/vite/plugin-deps.js +232 -0
- package/dist/node/vite/plugin.js +283 -0
- package/dist/node/vite/react-compiler.js +362 -0
- package/dist/node/vite/source-files.js +83 -0
- package/dist/node/vite/static-image.js +136 -0
- package/dist/node/vite/styled-components.js +73 -0
- package/dist/node/vite/transforms.js +330 -0
- package/dist/node/wasm-data/host.js +406 -0
- package/dist/node/wasm-data/lower.js +406 -0
- package/dist/node/wasm-data/runner.js +79 -0
- package/dist/node/wasm-data/worker.js +95 -0
- package/dist/reporter.js +5 -0
- package/dist/runtime/api-runner.js +81 -0
- package/dist/runtime/app.js +256 -0
- package/dist/runtime/client-entry.js +4 -0
- package/dist/runtime/client.js +383 -0
- package/dist/runtime/default-html.js +31 -0
- package/dist/runtime/head-markup.js +100 -0
- package/dist/runtime/head-sync.js +101 -0
- package/dist/runtime/head-tags.js +45 -0
- package/dist/runtime/hydration-report.js +111 -0
- package/dist/runtime/index.js +12 -0
- package/dist/runtime/link.js +293 -0
- package/dist/runtime/loader.js +449 -0
- package/dist/runtime/not-found.js +61 -0
- package/dist/runtime/page-renderer.js +35 -0
- package/dist/runtime/preamble.js +47 -0
- package/dist/runtime/react-compat.js +14 -0
- package/dist/runtime/renderer-shared.js +10 -0
- package/dist/runtime/renderers/react-client.js +14 -0
- package/dist/runtime/renderers/react-server.js +99 -0
- package/dist/runtime/renderers/react.js +17 -0
- package/dist/runtime/renderers/types.js +1 -0
- package/dist/runtime/root-error-handlers.js +29 -0
- package/dist/runtime/router/history.js +132 -0
- package/dist/runtime/router/index.js +240 -0
- package/dist/runtime/router/utils.js +118 -0
- package/dist/runtime/script.js +110 -0
- package/dist/runtime/scroll.js +40 -0
- package/dist/runtime/slice.js +11 -0
- package/dist/runtime/ssr.js +240 -0
- package/dist/runtime/static-query.js +47 -0
- package/dist/utils.js +4 -0
- package/package.json +105 -3
- package/types/graphql-tada.d.ts +223 -0
- package/types/graphql.d.ts +2 -0
- package/types/index.d.ts +2287 -0
- package/types/node.d.ts +389 -0
- package/types/reporter.d.ts +187 -0
- package/types/router.d.ts +102 -0
- package/types/vendor/gql-tada.d.ts +2529 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { escapeRawText } from "../head-markup.js";
|
|
2
|
+
import { splitPreamble } from "../preamble.js";
|
|
3
|
+
import { LEGACY_REACT } from "./react.js";
|
|
4
|
+
import { Writable } from "node:stream";
|
|
5
|
+
import { renderToPipeableStream, renderToStaticMarkup } from "react-dom/server";
|
|
6
|
+
// src/runtime/renderers/react-server.ts
|
|
7
|
+
const REACT18_VIEW_SIZE = 2048;
|
|
8
|
+
/**
|
|
9
|
+
* Renders the whole tree, waiting for every Suspense boundary (React.lazy, suspending data), with
|
|
10
|
+
* `renderToPipeableStream` – the API Gatsby 5 uses, available in react-dom 18 and 19 (on 19 the
|
|
11
|
+
* output is byte-identical to `prerenderToNodeStream` from react-dom/static, which 18 lacks).
|
|
12
|
+
* Piping only starts in `onAllReady`, so the output has no streaming instructions.
|
|
13
|
+
*/
|
|
14
|
+
const renderBody = (element, { ignoreError }) => new Promise((resolve, reject) => {
|
|
15
|
+
const chunks = [];
|
|
16
|
+
let bytes = false;
|
|
17
|
+
let settled = false;
|
|
18
|
+
const fail = (error) => {
|
|
19
|
+
if (settled) return;
|
|
20
|
+
settled = true;
|
|
21
|
+
reject(error);
|
|
22
|
+
};
|
|
23
|
+
const sink = new Writable({
|
|
24
|
+
decodeStrings: false,
|
|
25
|
+
write(chunk, _encoding, callback) {
|
|
26
|
+
if (typeof chunk === "string") {
|
|
27
|
+
chunks.push(chunk);
|
|
28
|
+
return callback();
|
|
29
|
+
}
|
|
30
|
+
bytes = true;
|
|
31
|
+
if (LEGACY_REACT && chunk.length === REACT18_VIEW_SIZE) {
|
|
32
|
+
let end = chunk.length;
|
|
33
|
+
while (end > chunk.length - 3 && chunk[end - 1] === 0) end--;
|
|
34
|
+
if (end !== chunk.length) chunk = chunk.subarray(0, end);
|
|
35
|
+
}
|
|
36
|
+
chunks.push(chunk);
|
|
37
|
+
callback();
|
|
38
|
+
},
|
|
39
|
+
final(callback) {
|
|
40
|
+
if (!settled) {
|
|
41
|
+
settled = true;
|
|
42
|
+
resolve(bytes ? Buffer.concat(chunks.map((c) => typeof c === "string" ? Buffer.from(c) : c)).toString("utf8") : chunks.join(""));
|
|
43
|
+
}
|
|
44
|
+
callback();
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
sink.on("error", fail);
|
|
48
|
+
let boundaryError;
|
|
49
|
+
const { pipe } = renderToPipeableStream(element, {
|
|
50
|
+
onAllReady: () => {
|
|
51
|
+
if (settled) return;
|
|
52
|
+
if (boundaryError !== void 0) fail(boundaryError);
|
|
53
|
+
else pipe(sink);
|
|
54
|
+
},
|
|
55
|
+
onShellError: fail,
|
|
56
|
+
onError: (error) => {
|
|
57
|
+
if (!ignoreError(error) && boundaryError === void 0) boundaryError = error;
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
}).then(splitBody);
|
|
61
|
+
/** React 19 emits the hoisted Head elements at the start of the body markup; React 18 does not hoist */
|
|
62
|
+
const splitBody = (html) => {
|
|
63
|
+
if (LEGACY_REACT) return {
|
|
64
|
+
html,
|
|
65
|
+
head: ""
|
|
66
|
+
};
|
|
67
|
+
const { preamble, body } = splitPreamble(html);
|
|
68
|
+
return {
|
|
69
|
+
html: body,
|
|
70
|
+
head: preamble
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
const renderHead = (element) => {
|
|
74
|
+
const html = renderToStaticMarkup(element);
|
|
75
|
+
return {
|
|
76
|
+
html: LEGACY_REACT ? unescapeLegacyRawText(html) : html,
|
|
77
|
+
head: ""
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
const ENTITIES = {
|
|
81
|
+
"&": "&",
|
|
82
|
+
"<": "<",
|
|
83
|
+
">": ">",
|
|
84
|
+
""": "\"",
|
|
85
|
+
"'": "'"
|
|
86
|
+
};
|
|
87
|
+
const decode = (text) => text.replace(/&(?:amp|lt|gt|quot|#x27);/g, (e) => ENTITIES[e]);
|
|
88
|
+
/**
|
|
89
|
+
* react-dom 18 HTML-escapes text children of `<script>`/`<style>` (`{"@type"…}` would be
|
|
90
|
+
* invalid JSON-LD), but writes `dangerouslySetInnerHTML` as it is; escaped text never contains a
|
|
91
|
+
* raw `<`, `>`, `"` or `'`. Gives each script and style its source text in `escapeRawText`'s form
|
|
92
|
+
* (renderHead's contract). The text is decoded only after its end was found: a decoded
|
|
93
|
+
* `</script>` would close the element early.
|
|
94
|
+
*/
|
|
95
|
+
function unescapeLegacyRawText(html) {
|
|
96
|
+
return html.replace(/(<(script|style)(?:\s[^>]*)?>)([\s\S]*?)(<\/\2>)/g, (_, open, tag, inner, close) => open + escapeRawText(/[<>"']/.test(inner) ? inner : decode(inner), tag) + close);
|
|
97
|
+
}
|
|
98
|
+
const renderDocument = (element) => renderToStaticMarkup(element);
|
|
99
|
+
export { renderBody, renderDocument, renderHead, splitBody, unescapeLegacyRawText };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as ReactDOM from "react-dom";
|
|
2
|
+
// src/runtime/renderers/react.ts
|
|
3
|
+
/**
|
|
4
|
+
* `true` with react-dom 18, `false` with react-dom 19 (and 19 canary/experimental builds).
|
|
5
|
+
*
|
|
6
|
+
* react-dom 19 hoists `<title>`/`<meta>`/`<link>` rendered anywhere in the tree into `<head>`
|
|
7
|
+
* (server and client). That shipped together with the resource APIs (`preinit`, `preload`), so
|
|
8
|
+
* their presence identifies it. react-dom 18 renders head elements where they are, and its Node
|
|
9
|
+
* stream writer pads chunks with NUL bytes (see `renderBody` in react-server.ts).
|
|
10
|
+
*
|
|
11
|
+
* Server and browser evaluate this against the same react-dom (one copy: Vite `resolve.dedupe`
|
|
12
|
+
* in the browser and dev SSR, an external resolved from the site in the SSR bundle), so the
|
|
13
|
+
* tree rendered on the server and the tree that hydrates always take the same branch.
|
|
14
|
+
*/
|
|
15
|
+
const LEGACY_REACT = typeof ReactDOM.preinit !== "function";
|
|
16
|
+
const headMode = LEGACY_REACT ? "portal" : "hoist";
|
|
17
|
+
export { LEGACY_REACT, headMode };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// src/runtime/root-error-handlers.ts
|
|
2
|
+
/**
|
|
3
|
+
* The root options behind Gatsby 5.16's `onCaughtError` and `onUncaughtError` browser APIs (React
|
|
4
|
+
* 19's root options of the same name; React 18 has none). Each is set only when a plugin or the
|
|
5
|
+
* site implements its API, and then behaves like Gatsby's: an uncaught error is logged with
|
|
6
|
+
* `console.error` before the API runs, a caught one is only passed to the API.
|
|
7
|
+
*
|
|
8
|
+
* Gatsby always sets both, and a root option replaces React's own reporting: on every Gatsby site
|
|
9
|
+
* with React 19, errors caught by a boundary are not logged and uncaught ones do not reach
|
|
10
|
+
* `reportError` (`window.onerror`). Here sites that do not use the APIs keep React's defaults.
|
|
11
|
+
*/
|
|
12
|
+
function rootErrorHandlers(runner) {
|
|
13
|
+
return {
|
|
14
|
+
onCaughtError: runner.hasApi("onCaughtError") ? (error, errorInfo) => {
|
|
15
|
+
runner.apiRunner("onCaughtError", {
|
|
16
|
+
error,
|
|
17
|
+
errorInfo
|
|
18
|
+
});
|
|
19
|
+
} : void 0,
|
|
20
|
+
onUncaughtError: runner.hasApi("onUncaughtError") ? (error, errorInfo) => {
|
|
21
|
+
console.error("Uncaught error:", error, errorInfo);
|
|
22
|
+
runner.apiRunner("onUncaughtError", {
|
|
23
|
+
error,
|
|
24
|
+
errorInfo
|
|
25
|
+
});
|
|
26
|
+
} : void 0
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export { rootErrorHandlers };
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// src/runtime/router/history.ts
|
|
2
|
+
function getLocation(source) {
|
|
3
|
+
const { search, hash, href = "", origin = "", protocol = "", host = "", hostname = "", port = "" } = source.location;
|
|
4
|
+
let { pathname } = source.location;
|
|
5
|
+
if (!pathname && href && canUseDOM) pathname = new URL(href).pathname;
|
|
6
|
+
return {
|
|
7
|
+
pathname: encodeURI(decodeURI(pathname)),
|
|
8
|
+
search,
|
|
9
|
+
hash,
|
|
10
|
+
href,
|
|
11
|
+
origin,
|
|
12
|
+
protocol,
|
|
13
|
+
host,
|
|
14
|
+
hostname,
|
|
15
|
+
port,
|
|
16
|
+
state: source.history.state,
|
|
17
|
+
key: source.history.state && source.history.state.key || "initial"
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
const canUseDOM = typeof window !== "undefined" && !!window.document && !!window.document.createElement;
|
|
21
|
+
function createHistory(source, _options) {
|
|
22
|
+
let listeners = [];
|
|
23
|
+
let location = getLocation(source);
|
|
24
|
+
let transitioning = false;
|
|
25
|
+
let resolveTransition = () => {};
|
|
26
|
+
return {
|
|
27
|
+
get location() {
|
|
28
|
+
return location;
|
|
29
|
+
},
|
|
30
|
+
get transitioning() {
|
|
31
|
+
return transitioning;
|
|
32
|
+
},
|
|
33
|
+
_onTransitionComplete() {
|
|
34
|
+
transitioning = false;
|
|
35
|
+
resolveTransition();
|
|
36
|
+
},
|
|
37
|
+
listen(listener) {
|
|
38
|
+
listeners.push(listener);
|
|
39
|
+
const popstateListener = () => {
|
|
40
|
+
location = getLocation(source);
|
|
41
|
+
listener({
|
|
42
|
+
location,
|
|
43
|
+
action: "POP"
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
source.addEventListener("popstate", popstateListener);
|
|
47
|
+
return () => {
|
|
48
|
+
source.removeEventListener("popstate", popstateListener);
|
|
49
|
+
listeners = listeners.filter((fn) => fn !== listener);
|
|
50
|
+
};
|
|
51
|
+
},
|
|
52
|
+
navigate(to, { state, replace = false } = {}) {
|
|
53
|
+
if (typeof to === "number") {
|
|
54
|
+
source.history.go(to);
|
|
55
|
+
return Promise.resolve();
|
|
56
|
+
}
|
|
57
|
+
const nextState = {
|
|
58
|
+
...state,
|
|
59
|
+
key: `${Date.now()}`
|
|
60
|
+
};
|
|
61
|
+
try {
|
|
62
|
+
if (transitioning || replace) source.history.replaceState(nextState, null, to);
|
|
63
|
+
else source.history.pushState(nextState, null, to);
|
|
64
|
+
} catch {
|
|
65
|
+
source.location[replace ? "replace" : "assign"](to);
|
|
66
|
+
}
|
|
67
|
+
location = getLocation(source);
|
|
68
|
+
transitioning = true;
|
|
69
|
+
const transition = new Promise((res) => resolveTransition = res);
|
|
70
|
+
for (const listener of listeners) listener({
|
|
71
|
+
location,
|
|
72
|
+
action: "PUSH"
|
|
73
|
+
});
|
|
74
|
+
return transition;
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
function createMemorySource(initialPath = "/") {
|
|
79
|
+
const searchIndex = initialPath.indexOf("?");
|
|
80
|
+
const initialLocation = {
|
|
81
|
+
pathname: searchIndex > -1 ? initialPath.slice(0, searchIndex) : initialPath,
|
|
82
|
+
search: searchIndex > -1 ? initialPath.slice(searchIndex) : "",
|
|
83
|
+
hash: ""
|
|
84
|
+
};
|
|
85
|
+
let index = 0;
|
|
86
|
+
const stack = [initialLocation];
|
|
87
|
+
const states = [null];
|
|
88
|
+
return {
|
|
89
|
+
get location() {
|
|
90
|
+
return stack[index];
|
|
91
|
+
},
|
|
92
|
+
addEventListener() {},
|
|
93
|
+
removeEventListener() {},
|
|
94
|
+
history: {
|
|
95
|
+
get entries() {
|
|
96
|
+
return stack;
|
|
97
|
+
},
|
|
98
|
+
get index() {
|
|
99
|
+
return index;
|
|
100
|
+
},
|
|
101
|
+
get state() {
|
|
102
|
+
return states[index];
|
|
103
|
+
},
|
|
104
|
+
pushState(state, _, uri) {
|
|
105
|
+
const [pathname, search = ""] = uri.split("?");
|
|
106
|
+
index++;
|
|
107
|
+
stack.push({
|
|
108
|
+
pathname,
|
|
109
|
+
search: search.length ? `?${search}` : search,
|
|
110
|
+
hash: ""
|
|
111
|
+
});
|
|
112
|
+
states.push(state);
|
|
113
|
+
},
|
|
114
|
+
replaceState(state, _, uri) {
|
|
115
|
+
const [pathname, search = ""] = uri.split("?");
|
|
116
|
+
stack[index] = {
|
|
117
|
+
pathname,
|
|
118
|
+
search,
|
|
119
|
+
hash: ""
|
|
120
|
+
};
|
|
121
|
+
states[index] = state;
|
|
122
|
+
},
|
|
123
|
+
go(to) {
|
|
124
|
+
const newIndex = index + to;
|
|
125
|
+
if (newIndex < 0 || newIndex > states.length - 1) return;
|
|
126
|
+
index = newIndex;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
const globalHistory = createHistory(canUseDOM ? window : createMemorySource());
|
|
132
|
+
export { canUseDOM, createHistory, createMemorySource, globalHistory };
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { createHistory, createMemorySource, globalHistory } from "./history.js";
|
|
2
|
+
import { insertParams, match, pick, resolve } from "./utils.js";
|
|
3
|
+
import { Children, Fragment, cloneElement, createContext, createElement, forwardRef, isValidElement, useContext, useEffect, useState } from "react";
|
|
4
|
+
import { Fragment as Fragment$1, jsx } from "react/jsx-runtime";
|
|
5
|
+
// src/runtime/router/index.tsx
|
|
6
|
+
/**
|
|
7
|
+
* @reach/router compatible API (Gatsby re-exports/aliases `@reach/router` and
|
|
8
|
+
* `@gatsbyjs/reach-router`). Implemented on top of goatly's history so the
|
|
9
|
+
* page router, `Link` and user-land `<Router>`s share the same location.
|
|
10
|
+
*/
|
|
11
|
+
let navigateImpl = (to, options) => globalHistory.navigate(to, options);
|
|
12
|
+
/** Lets the Gatsby runtime route navigations through its loader (`window.___navigate`). */
|
|
13
|
+
function setNavigateImplementation(fn) {
|
|
14
|
+
navigateImpl = fn;
|
|
15
|
+
}
|
|
16
|
+
const navigate = (to, options) => navigateImpl(to, options);
|
|
17
|
+
const LocationContext = createContext({
|
|
18
|
+
location: globalHistory.location,
|
|
19
|
+
navigate
|
|
20
|
+
});
|
|
21
|
+
LocationContext.displayName = "Location";
|
|
22
|
+
const BaseContext = createContext({
|
|
23
|
+
baseuri: "/",
|
|
24
|
+
basepath: "/"
|
|
25
|
+
});
|
|
26
|
+
BaseContext.displayName = "Base";
|
|
27
|
+
function LocationProvider({ history = globalHistory, children }) {
|
|
28
|
+
const [location, setLocation] = useState(history.location);
|
|
29
|
+
useEffect(() => history.listen(() => setLocation(history.location)), [history]);
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
history._onTransitionComplete();
|
|
32
|
+
});
|
|
33
|
+
const value = {
|
|
34
|
+
location,
|
|
35
|
+
navigate: history === globalHistory ? navigate : (to, opts) => history.navigate(to, opts)
|
|
36
|
+
};
|
|
37
|
+
return /* @__PURE__ */ jsx(LocationContext.Provider, {
|
|
38
|
+
value,
|
|
39
|
+
children: typeof children === "function" ? children(value) : children
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
function Location({ children }) {
|
|
43
|
+
const ctx = useContext(LocationContext);
|
|
44
|
+
return /* @__PURE__ */ jsx(Fragment$1, { children: children(ctx) });
|
|
45
|
+
}
|
|
46
|
+
function ServerLocation({ url, children }) {
|
|
47
|
+
const searchIndex = url.indexOf("?");
|
|
48
|
+
const searchExists = searchIndex > -1;
|
|
49
|
+
const location = {
|
|
50
|
+
pathname: searchExists ? url.substring(0, searchIndex) : url,
|
|
51
|
+
search: searchExists ? url.substring(searchIndex) : "",
|
|
52
|
+
hash: "",
|
|
53
|
+
href: url,
|
|
54
|
+
origin: "",
|
|
55
|
+
protocol: "",
|
|
56
|
+
host: "",
|
|
57
|
+
hostname: "",
|
|
58
|
+
port: "",
|
|
59
|
+
state: null,
|
|
60
|
+
key: "initial"
|
|
61
|
+
};
|
|
62
|
+
return /* @__PURE__ */ jsx(LocationContext.Provider, {
|
|
63
|
+
value: {
|
|
64
|
+
location,
|
|
65
|
+
navigate: () => {
|
|
66
|
+
throw new Error("You can't call navigate on the server.");
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
children
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
const stripSlashes = (str) => str.replace(/(^\/+|\/+$)/g, "");
|
|
73
|
+
function createRoute(basepath) {
|
|
74
|
+
return (element) => {
|
|
75
|
+
if (!element) return null;
|
|
76
|
+
const props = element.props ?? {};
|
|
77
|
+
if (element.type === Redirect || element.type?.__isRedirect) {
|
|
78
|
+
const from = props.from;
|
|
79
|
+
return {
|
|
80
|
+
value: element,
|
|
81
|
+
default: false,
|
|
82
|
+
path: from === "/" ? basepath : `${stripSlashes(basepath)}/${stripSlashes(from ?? "")}`.replace(/^\/?/, "/")
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
if (props.default) return {
|
|
86
|
+
value: element,
|
|
87
|
+
default: true,
|
|
88
|
+
path: basepath
|
|
89
|
+
};
|
|
90
|
+
const elementPath = props.path;
|
|
91
|
+
if (elementPath === void 0) return null;
|
|
92
|
+
const path = elementPath === "/" ? basepath : `${stripSlashes(basepath)}/${stripSlashes(elementPath)}`.replace(/\/$/, "");
|
|
93
|
+
return {
|
|
94
|
+
value: element,
|
|
95
|
+
default: false,
|
|
96
|
+
path: path.startsWith("/") ? path : `/${path}`
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
function Router(props) {
|
|
101
|
+
const base = useContext(BaseContext);
|
|
102
|
+
const locationCtx = useContext(LocationContext);
|
|
103
|
+
const { children, basepath = base.basepath, location = locationCtx.location, component = "div", primary: _primary, ...domProps } = props;
|
|
104
|
+
const routes = Children.toArray(children).filter(isValidElement).flatMap((el) => el.type === Fragment ? Children.toArray(el.props.children).filter(isValidElement) : [el]).map(createRoute(basepath)).filter(Boolean);
|
|
105
|
+
const matched = pick(routes, location.pathname);
|
|
106
|
+
if (!matched) return null;
|
|
107
|
+
const { route, params, uri } = matched;
|
|
108
|
+
const element = route.value;
|
|
109
|
+
if (element.type === Redirect || element.type?.__isRedirect) {
|
|
110
|
+
const to = insertParams(element.props.to, params);
|
|
111
|
+
return /* @__PURE__ */ jsx(Redirect, {
|
|
112
|
+
to,
|
|
113
|
+
noThrow: element.props.noThrow
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
const nested = element.props.children;
|
|
117
|
+
const routeBase = route.default ? basepath : uri;
|
|
118
|
+
const clone = cloneElement(element, {
|
|
119
|
+
...params,
|
|
120
|
+
uri,
|
|
121
|
+
location,
|
|
122
|
+
navigate: (to, options) => locationCtx.navigate(typeof to === "string" ? resolve(to, uri) : to, options)
|
|
123
|
+
}, nested ? /* @__PURE__ */ jsx(Router, {
|
|
124
|
+
location,
|
|
125
|
+
basepath: routeBase,
|
|
126
|
+
children: nested
|
|
127
|
+
}) : void 0);
|
|
128
|
+
const inner = /* @__PURE__ */ jsx(BaseContext.Provider, {
|
|
129
|
+
value: {
|
|
130
|
+
baseuri: uri,
|
|
131
|
+
basepath: routeBase
|
|
132
|
+
},
|
|
133
|
+
children: clone
|
|
134
|
+
});
|
|
135
|
+
if (component === Fragment || component === null) return inner;
|
|
136
|
+
return createElement(component, domProps, inner);
|
|
137
|
+
}
|
|
138
|
+
function useLocation() {
|
|
139
|
+
return useContext(LocationContext).location;
|
|
140
|
+
}
|
|
141
|
+
function useNavigate() {
|
|
142
|
+
return useContext(LocationContext).navigate;
|
|
143
|
+
}
|
|
144
|
+
function useParams() {
|
|
145
|
+
const { basepath } = useContext(BaseContext);
|
|
146
|
+
const location = useLocation();
|
|
147
|
+
const result = match(basepath, location.pathname);
|
|
148
|
+
return result ? result.params : null;
|
|
149
|
+
}
|
|
150
|
+
function useMatch(path) {
|
|
151
|
+
const { baseuri } = useContext(BaseContext);
|
|
152
|
+
const location = useLocation();
|
|
153
|
+
const resolvedPath = resolve(path, baseuri);
|
|
154
|
+
const result = match(resolvedPath, location.pathname);
|
|
155
|
+
return result ? {
|
|
156
|
+
...result.params,
|
|
157
|
+
uri: result.uri,
|
|
158
|
+
path
|
|
159
|
+
} : null;
|
|
160
|
+
}
|
|
161
|
+
function Match({ path, children }) {
|
|
162
|
+
const { baseuri } = useContext(BaseContext);
|
|
163
|
+
const { location, navigate: nav } = useContext(LocationContext);
|
|
164
|
+
const resolvedPath = resolve(path, baseuri);
|
|
165
|
+
const result = match(resolvedPath, location.pathname);
|
|
166
|
+
return /* @__PURE__ */ jsx(Fragment$1, { children: children({
|
|
167
|
+
navigate: nav,
|
|
168
|
+
location,
|
|
169
|
+
match: result ? {
|
|
170
|
+
...result.params,
|
|
171
|
+
uri: result.uri,
|
|
172
|
+
path
|
|
173
|
+
} : null
|
|
174
|
+
}) });
|
|
175
|
+
}
|
|
176
|
+
var RedirectRequest = class {
|
|
177
|
+
uri;
|
|
178
|
+
constructor(uri) {
|
|
179
|
+
this.uri = uri;
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
function isRedirect(o) {
|
|
183
|
+
return o instanceof RedirectRequest;
|
|
184
|
+
}
|
|
185
|
+
function redirectTo(to) {
|
|
186
|
+
throw new RedirectRequest(to);
|
|
187
|
+
}
|
|
188
|
+
function Redirect({ to, noThrow, state, replace = true }) {
|
|
189
|
+
const { baseuri } = useContext(BaseContext);
|
|
190
|
+
const { navigate: nav } = useContext(LocationContext);
|
|
191
|
+
useEffect(() => {
|
|
192
|
+
Promise.resolve().then(() => nav(resolve(to, baseuri), {
|
|
193
|
+
replace,
|
|
194
|
+
state
|
|
195
|
+
}));
|
|
196
|
+
}, []);
|
|
197
|
+
if (typeof window === "undefined" && !noThrow) redirectTo(resolve(to, baseuri));
|
|
198
|
+
return null;
|
|
199
|
+
}
|
|
200
|
+
Redirect.__isRedirect = true;
|
|
201
|
+
function shouldNavigate(event) {
|
|
202
|
+
return !event.defaultPrevented && event.button === 0 && !(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);
|
|
203
|
+
}
|
|
204
|
+
const Link = forwardRef(function Link(props, ref) {
|
|
205
|
+
const { innerRef, to, state, replace, getProps = () => void 0, ...anchorProps } = props;
|
|
206
|
+
const { baseuri } = useContext(BaseContext);
|
|
207
|
+
const { location, navigate: nav } = useContext(LocationContext);
|
|
208
|
+
const href = resolve(to, baseuri);
|
|
209
|
+
const encodedHref = encodeURI(href);
|
|
210
|
+
const isCurrent = location.pathname === encodedHref;
|
|
211
|
+
const isPartiallyCurrent = location.pathname.startsWith(encodedHref);
|
|
212
|
+
return /* @__PURE__ */ jsx("a", {
|
|
213
|
+
ref: ref || innerRef,
|
|
214
|
+
"aria-current": isCurrent ? "page" : void 0,
|
|
215
|
+
...anchorProps,
|
|
216
|
+
...getProps({
|
|
217
|
+
isCurrent,
|
|
218
|
+
isPartiallyCurrent,
|
|
219
|
+
href,
|
|
220
|
+
location
|
|
221
|
+
}),
|
|
222
|
+
href,
|
|
223
|
+
onClick: (event) => {
|
|
224
|
+
anchorProps.onClick?.(event);
|
|
225
|
+
if (shouldNavigate(event)) {
|
|
226
|
+
event.preventDefault();
|
|
227
|
+
let shouldReplace = replace;
|
|
228
|
+
if (typeof replace !== "boolean" && isCurrent) {
|
|
229
|
+
const { key: _k, ...restState } = { ...location.state };
|
|
230
|
+
shouldReplace = JSON.stringify(restState) === JSON.stringify(state ?? {});
|
|
231
|
+
}
|
|
232
|
+
nav(href, {
|
|
233
|
+
state,
|
|
234
|
+
replace: shouldReplace
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
});
|
|
240
|
+
export { BaseContext, Link, Location, LocationContext, LocationProvider, Match, Redirect, Router, ServerLocation, createHistory, createMemorySource, globalHistory, insertParams, isRedirect, match, navigate, pick, redirectTo, resolve, setNavigateImplementation, useLocation, useMatch, useNavigate, useParams };
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
// src/runtime/router/utils.ts
|
|
2
|
+
const paramRe = /^:(.+)/;
|
|
3
|
+
const SEGMENT_POINTS = 4;
|
|
4
|
+
const STATIC_POINTS = 3;
|
|
5
|
+
const DYNAMIC_POINTS = 2;
|
|
6
|
+
const ROOT_POINTS = 1;
|
|
7
|
+
function segmentize(uri) {
|
|
8
|
+
return uri.replace(/(^\/+|\/+$)/g, "").split("/");
|
|
9
|
+
}
|
|
10
|
+
const isRootSegment = (s) => s === "";
|
|
11
|
+
const isDynamic = (s) => paramRe.test(s);
|
|
12
|
+
const isSplat = (s) => !!s && s[0] === "*";
|
|
13
|
+
function rankRoute(path, isDefault = false) {
|
|
14
|
+
if (isDefault) return 0;
|
|
15
|
+
return segmentize(path).reduce((score, segment) => {
|
|
16
|
+
score += SEGMENT_POINTS;
|
|
17
|
+
if (isRootSegment(segment)) score += ROOT_POINTS;
|
|
18
|
+
else if (isDynamic(segment)) score += DYNAMIC_POINTS;
|
|
19
|
+
else if (isSplat(segment)) score -= 5;
|
|
20
|
+
else score += STATIC_POINTS;
|
|
21
|
+
return score;
|
|
22
|
+
}, 0);
|
|
23
|
+
}
|
|
24
|
+
function rankRoutes(routes) {
|
|
25
|
+
return routes.map((route, index) => ({
|
|
26
|
+
route,
|
|
27
|
+
index,
|
|
28
|
+
score: rankRoute(route.path, route.default)
|
|
29
|
+
})).sort((a, b) => a.score < b.score ? 1 : a.score > b.score ? -1 : a.index - b.index).map((r) => r.route);
|
|
30
|
+
}
|
|
31
|
+
/** Matches `uri` against a route path like `/users/:id` or `/app/*` (named splat `*rest` supported). */
|
|
32
|
+
function match(path, uri) {
|
|
33
|
+
return pick([{ path }], uri);
|
|
34
|
+
}
|
|
35
|
+
function pick(routes, uri) {
|
|
36
|
+
let matched = null;
|
|
37
|
+
let defaultRoute;
|
|
38
|
+
const [uriPathname] = uri.split("?");
|
|
39
|
+
const uriSegments = segmentize(uriPathname);
|
|
40
|
+
const isRootUri = uriSegments[0] === "";
|
|
41
|
+
for (const route of rankRoutes(routes)) {
|
|
42
|
+
if (route.default) {
|
|
43
|
+
defaultRoute = route;
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
const routeSegments = segmentize(route.path);
|
|
47
|
+
const params = {};
|
|
48
|
+
const max = Math.max(uriSegments.length, routeSegments.length);
|
|
49
|
+
let index = 0;
|
|
50
|
+
let missed = false;
|
|
51
|
+
for (; index < max; index++) {
|
|
52
|
+
const routeSegment = routeSegments[index];
|
|
53
|
+
const uriSegment = uriSegments[index];
|
|
54
|
+
if (routeSegment !== void 0 && isSplat(routeSegment)) {
|
|
55
|
+
const param = routeSegment.slice(1) || "*";
|
|
56
|
+
const value = uriSegments.slice(index).map(decodeURIComponent).join("/");
|
|
57
|
+
params[param] = value;
|
|
58
|
+
if (param !== "*") params["*"] = value;
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
if (uriSegment === void 0) {
|
|
62
|
+
missed = true;
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
const dynamicMatch = routeSegment !== void 0 ? paramRe.exec(routeSegment) : null;
|
|
66
|
+
if (dynamicMatch && !isRootUri) params[dynamicMatch[1]] = decodeURIComponent(uriSegment);
|
|
67
|
+
else if (routeSegment !== uriSegment) {
|
|
68
|
+
missed = true;
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (!missed) {
|
|
73
|
+
matched = {
|
|
74
|
+
route,
|
|
75
|
+
params,
|
|
76
|
+
uri: `/${uriSegments.slice(0, index).join("/")}`
|
|
77
|
+
};
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (!matched && defaultRoute) return {
|
|
82
|
+
route: defaultRoute,
|
|
83
|
+
params: {},
|
|
84
|
+
uri
|
|
85
|
+
};
|
|
86
|
+
return matched;
|
|
87
|
+
}
|
|
88
|
+
function resolve(to, base) {
|
|
89
|
+
if (to.startsWith("/")) return to;
|
|
90
|
+
const [toPathname, toQuery] = to.split("?");
|
|
91
|
+
const [basePathname] = base.split("?");
|
|
92
|
+
const toSegments = segmentize(toPathname);
|
|
93
|
+
const baseSegments = segmentize(basePathname);
|
|
94
|
+
if (toSegments[0] === "") return addQuery(basePathname, toQuery);
|
|
95
|
+
if (!toSegments[0].startsWith(".")) {
|
|
96
|
+
const pathname = baseSegments.concat(toSegments).join("/");
|
|
97
|
+
return addQuery((basePathname === "/" ? "" : "/") + pathname, toQuery);
|
|
98
|
+
}
|
|
99
|
+
const allSegments = baseSegments.concat(toSegments);
|
|
100
|
+
const segments = [];
|
|
101
|
+
for (const segment of allSegments) if (segment === "..") segments.pop();
|
|
102
|
+
else if (segment !== ".") segments.push(segment);
|
|
103
|
+
return addQuery(`/${segments.join("/")}`, toQuery);
|
|
104
|
+
}
|
|
105
|
+
function addQuery(pathname, query) {
|
|
106
|
+
return pathname + (query ? `?${query}` : "");
|
|
107
|
+
}
|
|
108
|
+
function insertParams(path, params) {
|
|
109
|
+
const [pathBase, query = ""] = path.split("?");
|
|
110
|
+
const constructed = `/${segmentize(pathBase).map((segment) => {
|
|
111
|
+
const m = paramRe.exec(segment);
|
|
112
|
+
return m ? params[m[1]] : segment;
|
|
113
|
+
}).join("/")}`;
|
|
114
|
+
const { location: { search = "" } = {} } = params;
|
|
115
|
+
const searchSplit = search.split("?")[1] || "";
|
|
116
|
+
return addQuery(constructed, query || searchSplit);
|
|
117
|
+
}
|
|
118
|
+
export { insertParams, match, pick, rankRoute, rankRoutes, resolve, segmentize };
|