@cmsstatic/next 5.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Thinkmill Labs Pty Ltd
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,2 @@
1
+ export * from "./declarations/src/api.js";
2
+ //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY21zc3RhdGljLW5leHQtYXBpLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuL2RlY2xhcmF0aW9ucy9zcmMvYXBpLmQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEifQ==
@@ -0,0 +1,156 @@
1
+ import { makeGenericAPIRouteHandler } from '@cmsstatic/core/api/generic';
2
+ import { g as getResolvedDirectories, a as getReaderKey } from './utils-72ac1816.js';
3
+ import '@cmsstatic/core/api/utils';
4
+ import 'path';
5
+ import 'fs/promises';
6
+ import 'crypto';
7
+
8
+ function createPromiseWithResolver() {
9
+ let resolve;
10
+ let promise = new Promise(r => {
11
+ resolve = r;
12
+ });
13
+ return {
14
+ promise,
15
+ resolve: resolve
16
+ };
17
+ }
18
+ function createWatcher(watcher) {
19
+ let state = 'init';
20
+ let eventQueue = [];
21
+ let promiseWithResolver = createPromiseWithResolver();
22
+ let lastError = null;
23
+ function emitEvent(event) {
24
+ eventQueue.push(event);
25
+ if (eventQueue.length === 1) {
26
+ promiseWithResolver.resolve();
27
+ }
28
+ }
29
+ return async () => {
30
+ if (lastError) {
31
+ let err = lastError;
32
+ lastError = null;
33
+ throw err;
34
+ }
35
+ if (state === 'init') {
36
+ state = 'started';
37
+ watcher.on('ready', () => {
38
+ state = 'ready';
39
+ emitEvent({
40
+ type: 'ready'
41
+ });
42
+ });
43
+ for (const eventName of ['add', 'change', 'unlink']) {
44
+ watcher.on(eventName, path => {
45
+ if (state !== 'ready') return;
46
+ emitEvent({
47
+ type: eventName,
48
+ path
49
+ });
50
+ });
51
+ }
52
+ watcher.on('error', err => {
53
+ lastError = err;
54
+ });
55
+ }
56
+ if (eventQueue.length === 0) {
57
+ await promiseWithResolver.promise;
58
+ }
59
+ const currentEventQueue = eventQueue;
60
+ eventQueue = [];
61
+ promiseWithResolver = createPromiseWithResolver();
62
+ return currentEventQueue;
63
+ };
64
+ }
65
+
66
+ function makeAPIRouteHandler(_config) {
67
+ const handler = makeGenericAPIRouteHandler(_config, {
68
+ slugEnvName: 'NEXT_PUBLIC_KEYSTATIC_GITHUB_APP_SLUG'
69
+ });
70
+ return async function keystaticAPIRoute(req, res) {
71
+ const host = req.headers['x-forwarded-host'] || req.headers['host'];
72
+ const proto = req.headers['x-forwarded-proto'] || (req.socket.encrypted ? 'https' : 'http');
73
+ const parsedUrl = new URL(`${proto}://${host}${req.url}`);
74
+ if (parsedUrl.pathname.startsWith('/api/keystatic/reader-refresh/') && process.env.NODE_ENV === 'development') {
75
+ const key = parsedUrl.pathname.slice('/api/keystatic/reader-refresh/'.length);
76
+ if (process.env.NODE_ENV !== 'development') {
77
+ return new Response(null, {
78
+ status: 404
79
+ });
80
+ }
81
+ const directories = getResolvedDirectories(_config.config, _config.localBaseDirectory || '');
82
+ const readerKey = await getReaderKey(directories);
83
+ if (key !== readerKey) {
84
+ res.status(200).send(readerKey);
85
+ return;
86
+ }
87
+ const a = require;
88
+ const b = 'chokidar';
89
+ const {
90
+ watch
91
+ } = a(b);
92
+ const watcher = watch(directories, {
93
+ ignored: [/node_modules/]
94
+ });
95
+ const waitForNextEvent = createWatcher(watcher);
96
+ try {
97
+ while (true) {
98
+ await waitForNextEvent();
99
+ const readerKey = await getReaderKey(directories);
100
+ if (key !== readerKey) {
101
+ res.status(200).send(readerKey);
102
+ return;
103
+ }
104
+ }
105
+ } catch {
106
+ res.status(500).send(null);
107
+ return;
108
+ } finally {
109
+ watcher.close();
110
+ }
111
+ }
112
+ const {
113
+ body,
114
+ headers,
115
+ status
116
+ } = await handler({
117
+ headers: {
118
+ get(name) {
119
+ const val = req.headers[name];
120
+ if (Array.isArray(val)) {
121
+ return val[0];
122
+ }
123
+ return val !== null && val !== void 0 ? val : null;
124
+ }
125
+ },
126
+ json: async () => req.body,
127
+ method: req.method,
128
+ url: parsedUrl.toString()
129
+ });
130
+ if (headers) {
131
+ if (Array.isArray(headers)) {
132
+ const headersInADifferentStructure = new Map();
133
+ for (const [key, value] of headers) {
134
+ if (!headersInADifferentStructure.has(key)) {
135
+ headersInADifferentStructure.set(key, []);
136
+ }
137
+ headersInADifferentStructure.get(key).push(value);
138
+ }
139
+ for (const [key, value] of headersInADifferentStructure) {
140
+ res.setHeader(key, value);
141
+ }
142
+ } else if (typeof headers.entries === 'function') {
143
+ for (const [key, value] of headers.entries()) {
144
+ res.setHeader(key, value);
145
+ }
146
+ } else {
147
+ for (const [key, value] of Object.entries(headers)) {
148
+ res.setHeader(key, value);
149
+ }
150
+ }
151
+ }
152
+ res.status(status !== null && status !== void 0 ? status : 200).send(body);
153
+ };
154
+ }
155
+
156
+ export { makeAPIRouteHandler };
@@ -0,0 +1,2 @@
1
+ export * from "./declarations/src/reader-refresh.js";
2
+ //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY21zc3RhdGljLW5leHQtcmVhZGVyLXJlZnJlc2guZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4vZGVjbGFyYXRpb25zL3NyYy9yZWFkZXItcmVmcmVzaC5kLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBIn0=
@@ -0,0 +1,30 @@
1
+ import 'server-only';
2
+ import { Suspense } from 'react';
3
+ import { ReaderRefreshClient } from './reader-refresh-client-cb4c43c9.js';
4
+ import { a as getReaderKey, g as getResolvedDirectories } from './utils-72ac1816.js';
5
+ import { jsx } from 'react/jsx-runtime';
6
+ import '@cmsstatic/core/api/utils';
7
+ import 'path';
8
+ import 'fs/promises';
9
+ import 'crypto';
10
+
11
+ async function ReaderRefreshInner(props) {
12
+ return /*#__PURE__*/jsx(ReaderRefreshClient, {
13
+ currentKey: await getReaderKey(getResolvedDirectories(props.reader.config, props.reader.repoPath))
14
+ });
15
+ }
16
+ function ReaderRefresh(props) {
17
+ if (process.env.NODE_ENV !== 'development') {
18
+ return null;
19
+ }
20
+ return /*#__PURE__*/jsx(Suspense, {
21
+ fallback: null,
22
+ children: /*#__PURE__*/jsx(ReaderRefreshInner
23
+ //
24
+ , {
25
+ reader: props.reader
26
+ })
27
+ });
28
+ }
29
+
30
+ export { ReaderRefresh };
@@ -0,0 +1,2 @@
1
+ export * from "./declarations/src/route-handler.js";
2
+ //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY21zc3RhdGljLW5leHQtcm91dGUtaGFuZGxlci5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi9kZWNsYXJhdGlvbnMvc3JjL3JvdXRlLWhhbmRsZXIuZC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9
@@ -0,0 +1,30 @@
1
+ import { makeGenericAPIRouteHandler } from '@cmsstatic/core/api/generic';
2
+
3
+ function makeRouteHandler(_config) {
4
+ const handler = makeGenericAPIRouteHandler(_config, {
5
+ slugEnvName: 'NEXT_PUBLIC_KEYSTATIC_GITHUB_APP_SLUG'
6
+ });
7
+ async function wrappedHandler(request) {
8
+ const url = new URL(request.url);
9
+ // next replaces 127.0.0.1 with localhost in the url or something like that
10
+ if (url.hostname === 'localhost') {
11
+ url.hostname = '127.0.0.1';
12
+ request = new Request(url.toString(), request);
13
+ }
14
+ const {
15
+ body,
16
+ headers,
17
+ status
18
+ } = await handler(request);
19
+ return new Response(body, {
20
+ status,
21
+ headers
22
+ });
23
+ }
24
+ return {
25
+ GET: wrappedHandler,
26
+ POST: wrappedHandler
27
+ };
28
+ }
29
+
30
+ export { makeRouteHandler };
@@ -0,0 +1,2 @@
1
+ export * from "./declarations/src/ui/app.js";
2
+ //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY21zc3RhdGljLW5leHQtdWktYXBwLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuL2RlY2xhcmF0aW9ucy9zcmMvdWkvYXBwLmQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEifQ==
@@ -0,0 +1,17 @@
1
+ import { Keystatic } from '@cmsstatic/core/ui';
2
+ import { jsx } from 'react/jsx-runtime';
3
+
4
+ function makePage(config) {
5
+ return function Page() {
6
+ return /*#__PURE__*/jsx(Keystatic, {
7
+ config: config,
8
+ appSlug: appSlug
9
+ });
10
+ };
11
+ }
12
+ const appSlug = {
13
+ envName: 'NEXT_PUBLIC_KEYSTATIC_GITHUB_APP_SLUG',
14
+ value: process.env.NEXT_PUBLIC_KEYSTATIC_GITHUB_APP_SLUG
15
+ };
16
+
17
+ export { makePage };
@@ -0,0 +1,2 @@
1
+ export * from "./declarations/src/ui/pages.js";
2
+ //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY21zc3RhdGljLW5leHQtdWktcGFnZXMuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4vZGVjbGFyYXRpb25zL3NyYy91aS9wYWdlcy5kLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBIn0=
@@ -0,0 +1,17 @@
1
+ import { Keystatic } from '@cmsstatic/core/ui';
2
+ import { jsx } from 'react/jsx-runtime';
3
+
4
+ function makePage(config) {
5
+ return function Page() {
6
+ return /*#__PURE__*/jsx(Keystatic, {
7
+ config: config,
8
+ appSlug: appSlug
9
+ });
10
+ };
11
+ }
12
+ const appSlug = {
13
+ envName: 'NEXT_PUBLIC_KEYSTATIC_GITHUB_APP_SLUG',
14
+ value: process.env.NEXT_PUBLIC_KEYSTATIC_GITHUB_APP_SLUG
15
+ };
16
+
17
+ export { makePage };
@@ -0,0 +1,3 @@
1
+ import type { NextApiRequest, NextApiResponse } from 'next';
2
+ import { APIRouteConfig } from '@cmsstatic/core/api/generic';
3
+ export declare function makeAPIRouteHandler(_config: APIRouteConfig): (req: NextApiRequest, res: NextApiResponse) => Promise<Response | undefined>;
@@ -0,0 +1,5 @@
1
+ import 'server-only';
2
+ import { Reader } from '@cmsstatic/core/reader';
3
+ export declare function ReaderRefresh(props: {
4
+ reader: Reader<any, any>;
5
+ }): import("react").JSX.Element | null;
@@ -0,0 +1,5 @@
1
+ import { APIRouteConfig } from '@cmsstatic/core/api/generic';
2
+ export declare function makeRouteHandler(_config: APIRouteConfig): {
3
+ GET: (request: Request) => Promise<Response>;
4
+ POST: (request: Request) => Promise<Response>;
5
+ };
@@ -0,0 +1,2 @@
1
+ import { Config } from '@cmsstatic/core';
2
+ export declare function makePage(config: Config<any, any>): () => import("react").JSX.Element;
@@ -0,0 +1,2 @@
1
+ import { Config } from '@cmsstatic/core';
2
+ export declare function makePage(config: Config<any, any>): () => import("react").JSX.Element;
@@ -0,0 +1,53 @@
1
+ 'use client';
2
+ import { useRouter } from 'next/navigation';
3
+ import { useEffect, useState } from 'react';
4
+
5
+ function useIsVisible() {
6
+ const [isVisible, setIsVisible] = useState(false);
7
+ useEffect(() => {
8
+ const handler = () => {
9
+ setIsVisible(document.visibilityState === 'visible');
10
+ };
11
+ handler();
12
+ document.addEventListener('visibilitychange', handler);
13
+ return () => {
14
+ document.removeEventListener('visibilitychange', handler);
15
+ };
16
+ }, []);
17
+ return isVisible;
18
+ }
19
+ function ReaderRefreshClient(props) {
20
+ const isVisible = useIsVisible();
21
+ const router = useRouter();
22
+ useEffect(() => {
23
+ if (isVisible) {
24
+ const abortController = new AbortController();
25
+ const fetchThing = async () => {
26
+ while (!abortController.signal.aborted) {
27
+ try {
28
+ const res = await fetch(`/api/keystatic/reader-refresh/${props.currentKey}`, {
29
+ signal: abortController.signal
30
+ });
31
+ if (res.status === 404) {
32
+ return;
33
+ }
34
+ if (res.ok) {
35
+ const key = await res.text();
36
+ if (key !== props.currentKey) {
37
+ router.refresh();
38
+ return;
39
+ }
40
+ }
41
+ } catch {}
42
+ }
43
+ };
44
+ fetchThing();
45
+ return () => {
46
+ abortController.abort();
47
+ };
48
+ }
49
+ }, [props.currentKey, router, isVisible]);
50
+ return null;
51
+ }
52
+
53
+ export { ReaderRefreshClient };
@@ -0,0 +1,33 @@
1
+ import { getAllowedDirectories } from '@cmsstatic/core/api/utils';
2
+ import path from 'path';
3
+ import fs from 'fs/promises';
4
+ import { createHash } from 'crypto';
5
+
6
+ async function getDirKeyComponents(dirpath) {
7
+ return Promise.all((await fs.readdir(dirpath, {
8
+ withFileTypes: true
9
+ })).map(async entry => {
10
+ const joined = path.join(dirpath, entry.name);
11
+ if (entry.isFile()) {
12
+ const stat = await fs.stat(joined);
13
+ return [entry.name, stat.mtimeMs];
14
+ }
15
+ if (entry.isDirectory() && entry.name !== 'node_modules') {
16
+ return [entry.name, await getDirKeyComponents(joined)];
17
+ }
18
+ return null;
19
+ }));
20
+ }
21
+ function getResolvedDirectories(config, repoPath) {
22
+ const directories = getAllowedDirectories(config);
23
+ const resolvedRepoPath = path.resolve(repoPath);
24
+ return directories.map(dir => path.join(resolvedRepoPath, dir));
25
+ }
26
+ async function getReaderKey(directories) {
27
+ const data = JSON.stringify(await Promise.all(directories.map(async dir => {
28
+ return [dir, await getDirKeyComponents(dir)];
29
+ })));
30
+ return createHash('sha1').update(data).digest('hex');
31
+ }
32
+
33
+ export { getReaderKey as a, getResolvedDirectories as g };
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@cmsstatic/next",
3
+ "version": "5.0.4",
4
+ "license": "MIT",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/Thinkmill/keystatic/",
8
+ "directory": "packages/next"
9
+ },
10
+ "type": "module",
11
+ "exports": {
12
+ "./api": "./dist/cmsstatic-next-api.js",
13
+ "./ui/app": "./dist/cmsstatic-next-ui-app.js",
14
+ "./ui/pages": "./dist/cmsstatic-next-ui-pages.js",
15
+ "./route-handler": "./dist/cmsstatic-next-route-handler.js",
16
+ "./reader-refresh": "./dist/cmsstatic-next-reader-refresh.js",
17
+ "./package.json": "./package.json"
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "dependencies": {
23
+ "@babel/runtime": "^7.18.3",
24
+ "@types/react": "^19.0.8",
25
+ "chokidar": "^3.5.3",
26
+ "server-only": "^0.0.1"
27
+ },
28
+ "devDependencies": {
29
+ "next": "~15.1.10",
30
+ "react": "^19.0.0",
31
+ "react-dom": "^19.0.0",
32
+ "@cmsstatic/core": "^0.5.51"
33
+ },
34
+ "peerDependencies": {
35
+ "@cmsstatic/core": "*",
36
+ "next": ">=14",
37
+ "react": "^18.2.0 || ^19.0.0",
38
+ "react-dom": "^18.2.0 || ^19.0.0"
39
+ },
40
+ "preconstruct": {
41
+ "entrypoints": [
42
+ "ui/pages.tsx",
43
+ "ui/app.tsx",
44
+ "api.tsx",
45
+ "route-handler.tsx",
46
+ "reader-refresh.tsx"
47
+ ]
48
+ }
49
+ }