@capacms/sdk 1.0.0-next.11 → 1.0.0-next.12
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/CHANGELOG.md +133 -0
- package/README.md +246 -5
- package/bin/capa-check-bundle.js +80 -0
- package/bin/capa.js +3 -3
- package/bin/graphql-project.js +1 -1
- package/dist/esm/overlay/index.d.ts +4 -1
- package/dist/esm/overlay/index.js +382 -30
- package/dist/esm/overlay/live-fields.d.ts +133 -0
- package/dist/esm/overlay/live-fields.js +478 -0
- package/dist/esm/overlay/markdown.d.ts +38 -0
- package/dist/esm/overlay/markdown.js +536 -0
- package/dist/esm/overlay/protocol.d.ts +51 -5
- package/dist/esm/overlay/protocol.js +79 -9
- package/dist/next/attrs.d.ts +42 -1
- package/dist/next/attrs.js +42 -3
- package/dist/next/client.d.ts +48 -1
- package/dist/next/client.js +33 -2
- package/dist/next/graphql/request.d.ts +4 -0
- package/dist/next/graphql/request.js +4 -0
- package/dist/next/index.d.ts +3 -3
- package/dist/next/index.js +3 -1
- package/dist/nextjs/index.d.ts +91 -7
- package/dist/nextjs/index.js +230 -17
- package/dist/nextjs/write-key-guard.d.ts +117 -0
- package/dist/nextjs/write-key-guard.js +285 -0
- package/dist/overlay/index.d.ts +4 -1
- package/dist/overlay/index.js +399 -29
- package/dist/overlay/live-fields.d.ts +133 -0
- package/dist/overlay/live-fields.js +493 -0
- package/dist/overlay/markdown.d.ts +38 -0
- package/dist/overlay/markdown.js +539 -0
- package/dist/overlay/protocol.d.ts +51 -5
- package/dist/overlay/protocol.js +82 -9
- package/dist/server/browser.d.ts +1 -0
- package/dist/server/browser.js +20 -0
- package/dist/server/client.d.ts +268 -0
- package/dist/server/client.js +335 -0
- package/dist/server/errors.d.ts +60 -0
- package/dist/server/errors.js +120 -0
- package/dist/server/index.d.ts +14 -0
- package/dist/server/index.js +32 -0
- package/dist/server/key-format.d.ts +41 -0
- package/dist/server/key-format.js +91 -0
- package/dist/server/transport.d.ts +47 -0
- package/dist/server/transport.js +114 -0
- package/package.json +18 -4
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* key-format.ts — the shape and checksum of a `capw_` server write key.
|
|
4
|
+
*
|
|
5
|
+
* capw_<43 base62 secret><6 base62 checksum>
|
|
6
|
+
*
|
|
7
|
+
* The checksum is CRC32 (IEEE) of the string `capw_<secret>`, written in
|
|
8
|
+
* base62 with the digits 0-9, A-Z, a-z in that order and left-padded with `0`
|
|
9
|
+
* to 6 characters. It lets a client (and a secret scanner) reject a mistyped
|
|
10
|
+
* or truncated key without asking the write host. It is not a secret and
|
|
11
|
+
* proves nothing about the key being real: only the write host can say that.
|
|
12
|
+
*
|
|
13
|
+
* The API holds the same rule (`apps/api/src/write-api/auth/key-format.ts`).
|
|
14
|
+
* Both are tested against vectors computed independently with `zlib.crc32`.
|
|
15
|
+
*/
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.WRITE_KEY_LENGTH = exports.WRITE_KEY_PREFIX = exports.WRITE_KEY_IN_TEXT = exports.WRITE_KEY_SHAPE = void 0;
|
|
18
|
+
exports.crc32 = crc32;
|
|
19
|
+
exports.base62 = base62;
|
|
20
|
+
exports.writeKeyChecksum = writeKeyChecksum;
|
|
21
|
+
exports.isWriteKeyShaped = isWriteKeyShaped;
|
|
22
|
+
exports.writeKeyChecksumOk = writeKeyChecksumOk;
|
|
23
|
+
exports.writeKeyLabel = writeKeyLabel;
|
|
24
|
+
const BASE62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
25
|
+
/** `capw_` plus 49 base62 characters, the whole value. */
|
|
26
|
+
exports.WRITE_KEY_SHAPE = /^capw_[0-9A-Za-z]{49}$/;
|
|
27
|
+
/**
|
|
28
|
+
* The same shape inside other text, the regex a bundle scan uses (write API
|
|
29
|
+
* spec 6.2). No word boundaries, unlike GitHub's pattern: a key in a bundle
|
|
30
|
+
* can sit right after an escape (`"\ncapw_…"`), an underscore or other
|
|
31
|
+
* letters, and `\b` would miss it there. A run longer than 49 is matched on
|
|
32
|
+
* its first 49, whose checksum then decides whether it is a key.
|
|
33
|
+
*/
|
|
34
|
+
exports.WRITE_KEY_IN_TEXT = /capw_[0-9A-Za-z]{49}/g;
|
|
35
|
+
exports.WRITE_KEY_PREFIX = "capw_";
|
|
36
|
+
exports.WRITE_KEY_LENGTH = 54;
|
|
37
|
+
const SECRET_LENGTH = 43;
|
|
38
|
+
const CHECKSUM_LENGTH = 6;
|
|
39
|
+
let table = null;
|
|
40
|
+
function crcTable() {
|
|
41
|
+
if (table)
|
|
42
|
+
return table;
|
|
43
|
+
table = new Uint32Array(256);
|
|
44
|
+
for (let n = 0; n < 256; n++) {
|
|
45
|
+
let c = n;
|
|
46
|
+
for (let k = 0; k < 8; k++)
|
|
47
|
+
c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
48
|
+
table[n] = c >>> 0;
|
|
49
|
+
}
|
|
50
|
+
return table;
|
|
51
|
+
}
|
|
52
|
+
/** CRC32 (IEEE 802.3, the zlib one) of an ASCII string. A key holds ASCII only. */
|
|
53
|
+
function crc32(text) {
|
|
54
|
+
const t = crcTable();
|
|
55
|
+
let crc = 0xffffffff;
|
|
56
|
+
for (let i = 0; i < text.length; i++)
|
|
57
|
+
crc = t[(crc ^ text.charCodeAt(i)) & 0xff] ^ (crc >>> 8);
|
|
58
|
+
return (crc ^ 0xffffffff) >>> 0;
|
|
59
|
+
}
|
|
60
|
+
/** A non-negative integer in base62, left-padded with `0` to `width`. */
|
|
61
|
+
function base62(value, width) {
|
|
62
|
+
let n = Math.floor(value);
|
|
63
|
+
let out = "";
|
|
64
|
+
while (n > 0) {
|
|
65
|
+
out = BASE62[n % 62] + out;
|
|
66
|
+
n = Math.floor(n / 62);
|
|
67
|
+
}
|
|
68
|
+
return out.padStart(width, "0");
|
|
69
|
+
}
|
|
70
|
+
/** The 6-character checksum of a 43-character secret. */
|
|
71
|
+
function writeKeyChecksum(secret) {
|
|
72
|
+
return base62(crc32(`${exports.WRITE_KEY_PREFIX}${secret}`), CHECKSUM_LENGTH);
|
|
73
|
+
}
|
|
74
|
+
/** Whether `value` is shaped like a write key: `capw_` and 49 base62 characters. */
|
|
75
|
+
function isWriteKeyShaped(value) {
|
|
76
|
+
return typeof value === "string" && exports.WRITE_KEY_SHAPE.test(value);
|
|
77
|
+
}
|
|
78
|
+
/** Whether `value` is shaped like a write key and its last 6 characters are the checksum of the rest. */
|
|
79
|
+
function writeKeyChecksumOk(value) {
|
|
80
|
+
if (!isWriteKeyShaped(value))
|
|
81
|
+
return false;
|
|
82
|
+
const secret = value.slice(exports.WRITE_KEY_PREFIX.length, exports.WRITE_KEY_PREFIX.length + SECRET_LENGTH);
|
|
83
|
+
return value.slice(exports.WRITE_KEY_PREFIX.length + SECRET_LENGTH) === writeKeyChecksum(secret);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* How a message names a key without printing it: the first 12 characters,
|
|
87
|
+
* which is the `keyPrefix` the admin lists. Never the secret.
|
|
88
|
+
*/
|
|
89
|
+
function writeKeyLabel(value) {
|
|
90
|
+
return `${value.slice(0, 12)}…`;
|
|
91
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* transport.ts — how `@capacms/sdk/server` sends a request.
|
|
3
|
+
*
|
|
4
|
+
* NOT `fetch` BY DEFAULT ON NODE. On Node the client sends through
|
|
5
|
+
* `node:https` (or `node:http` for a local `http://` host). The reason is
|
|
6
|
+
* Next.js: it replaces the global `fetch` in server code with one that
|
|
7
|
+
* caches and dedupes, and a cached `GET` of an entry would hand back an old
|
|
8
|
+
* version number that the next `If-Match` then fails on. A write client wants
|
|
9
|
+
* every request to reach the host. `node:https` adds no header of its own
|
|
10
|
+
* beyond `Host`, `Connection` and `Content-Length`; the client adds its
|
|
11
|
+
* `User-Agent` and enforces its own timeout (client.ts).
|
|
12
|
+
*
|
|
13
|
+
* Node's fetch would also be accepted by the write host. It adds
|
|
14
|
+
* `Sec-Fetch-Mode: cors` to every request, and the host does not count that
|
|
15
|
+
* header as a browser (write API spec 5): only `Origin`, `Sec-Fetch-Site`,
|
|
16
|
+
* `Sec-Fetch-Dest` and `Cookie` are refused.
|
|
17
|
+
*
|
|
18
|
+
* `node:https` is reached through `process.getBuiltinModule` (Node 20.16 and
|
|
19
|
+
* 22.3 onward) or, on an older Node, this module's own `module.require`, never
|
|
20
|
+
* a static import, so a bundler building this file for an edge runtime sees no
|
|
21
|
+
* Node module. Where neither exists (an edge runtime, Deno, a worker, or a
|
|
22
|
+
* bundle on a Node older than 20.16) the global fetch is used. A `fetch`
|
|
23
|
+
* passed in config is always used as given.
|
|
24
|
+
*/
|
|
25
|
+
/** What the client reads of a response. A `fetch` Response is one. */
|
|
26
|
+
export interface WireResponse {
|
|
27
|
+
status: number;
|
|
28
|
+
ok: boolean;
|
|
29
|
+
headers: {
|
|
30
|
+
get(name: string): string | null;
|
|
31
|
+
};
|
|
32
|
+
text(): Promise<string>;
|
|
33
|
+
}
|
|
34
|
+
export interface WireRequest {
|
|
35
|
+
method: string;
|
|
36
|
+
headers: Record<string, string>;
|
|
37
|
+
body?: string;
|
|
38
|
+
signal?: AbortSignal;
|
|
39
|
+
}
|
|
40
|
+
export type Transport = (url: string, init: WireRequest) => Promise<WireResponse>;
|
|
41
|
+
type BuiltinLoader = (id: string) => unknown;
|
|
42
|
+
export declare function builtinLoader(): BuiltinLoader | null;
|
|
43
|
+
/** A transport over `node:http` / `node:https`. Exported for tests. */
|
|
44
|
+
export declare function nodeTransport(load: BuiltinLoader): Transport;
|
|
45
|
+
/** The transport for a client: the given fetch, else Node's http modules, else the global fetch. */
|
|
46
|
+
export declare function defaultTransport(fetchImpl: typeof fetch | undefined): Transport | null;
|
|
47
|
+
export {};
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* transport.ts — how `@capacms/sdk/server` sends a request.
|
|
4
|
+
*
|
|
5
|
+
* NOT `fetch` BY DEFAULT ON NODE. On Node the client sends through
|
|
6
|
+
* `node:https` (or `node:http` for a local `http://` host). The reason is
|
|
7
|
+
* Next.js: it replaces the global `fetch` in server code with one that
|
|
8
|
+
* caches and dedupes, and a cached `GET` of an entry would hand back an old
|
|
9
|
+
* version number that the next `If-Match` then fails on. A write client wants
|
|
10
|
+
* every request to reach the host. `node:https` adds no header of its own
|
|
11
|
+
* beyond `Host`, `Connection` and `Content-Length`; the client adds its
|
|
12
|
+
* `User-Agent` and enforces its own timeout (client.ts).
|
|
13
|
+
*
|
|
14
|
+
* Node's fetch would also be accepted by the write host. It adds
|
|
15
|
+
* `Sec-Fetch-Mode: cors` to every request, and the host does not count that
|
|
16
|
+
* header as a browser (write API spec 5): only `Origin`, `Sec-Fetch-Site`,
|
|
17
|
+
* `Sec-Fetch-Dest` and `Cookie` are refused.
|
|
18
|
+
*
|
|
19
|
+
* `node:https` is reached through `process.getBuiltinModule` (Node 20.16 and
|
|
20
|
+
* 22.3 onward) or, on an older Node, this module's own `module.require`, never
|
|
21
|
+
* a static import, so a bundler building this file for an edge runtime sees no
|
|
22
|
+
* Node module. Where neither exists (an edge runtime, Deno, a worker, or a
|
|
23
|
+
* bundle on a Node older than 20.16) the global fetch is used. A `fetch`
|
|
24
|
+
* passed in config is always used as given.
|
|
25
|
+
*/
|
|
26
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
27
|
+
exports.builtinLoader = builtinLoader;
|
|
28
|
+
exports.nodeTransport = nodeTransport;
|
|
29
|
+
exports.defaultTransport = defaultTransport;
|
|
30
|
+
function builtinLoader() {
|
|
31
|
+
const proc = globalThis.process;
|
|
32
|
+
if (typeof proc?.getBuiltinModule === "function")
|
|
33
|
+
return proc.getBuiltinModule.bind(proc);
|
|
34
|
+
// An older Node running this file unbundled. Not a `require("node:https")` a bundler would resolve.
|
|
35
|
+
if (proc?.versions?.node && typeof module !== "undefined" && typeof module?.require === "function")
|
|
36
|
+
return module.require.bind(module);
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
function decodeUtf8(chunks) {
|
|
40
|
+
let length = 0;
|
|
41
|
+
for (const chunk of chunks)
|
|
42
|
+
length += chunk.length;
|
|
43
|
+
const all = new Uint8Array(length);
|
|
44
|
+
let at = 0;
|
|
45
|
+
for (const chunk of chunks) {
|
|
46
|
+
all.set(chunk, at);
|
|
47
|
+
at += chunk.length;
|
|
48
|
+
}
|
|
49
|
+
return new TextDecoder().decode(all);
|
|
50
|
+
}
|
|
51
|
+
/** A transport over `node:http` / `node:https`. Exported for tests. */
|
|
52
|
+
function nodeTransport(load) {
|
|
53
|
+
return (url, init) => new Promise((resolve, reject) => {
|
|
54
|
+
const http = load(url.startsWith("http://") ? "node:http" : "node:https");
|
|
55
|
+
const headers = { ...init.headers };
|
|
56
|
+
if (init.body !== undefined)
|
|
57
|
+
headers["Content-Length"] = String(new TextEncoder().encode(init.body).length);
|
|
58
|
+
const request = http.request(url, { method: init.method, headers, signal: init.signal }, (response) => {
|
|
59
|
+
const chunks = [];
|
|
60
|
+
response.on("data", (chunk) => chunks.push(chunk));
|
|
61
|
+
response.on("error", reject);
|
|
62
|
+
response.on("end", () => {
|
|
63
|
+
const text = decodeUtf8(chunks);
|
|
64
|
+
const status = response.statusCode ?? 0;
|
|
65
|
+
resolve({
|
|
66
|
+
status,
|
|
67
|
+
ok: status >= 200 && status < 300,
|
|
68
|
+
headers: {
|
|
69
|
+
get(name) {
|
|
70
|
+
const value = response.headers[name.toLowerCase()];
|
|
71
|
+
if (value === undefined)
|
|
72
|
+
return null;
|
|
73
|
+
return Array.isArray(value) ? value.join(", ") : String(value);
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
text: async () => text,
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
request.on("error", reject);
|
|
81
|
+
if (init.body !== undefined)
|
|
82
|
+
request.write(init.body);
|
|
83
|
+
request.end();
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
function fetchTransport(fetchImpl) {
|
|
87
|
+
return async (url, init) => {
|
|
88
|
+
const response = await fetchImpl(url, {
|
|
89
|
+
method: init.method,
|
|
90
|
+
headers: init.headers,
|
|
91
|
+
body: init.body,
|
|
92
|
+
signal: init.signal,
|
|
93
|
+
// Never follow a redirect, so the key is never sent to another address:
|
|
94
|
+
// the 3xx comes back and the client refuses it (client.ts). "manual",
|
|
95
|
+
// not "error": Cloudflare's workerd implements only "follow" and
|
|
96
|
+
// "manual" and throws on "error". No `credentials` either, which
|
|
97
|
+
// workerd has also refused; a server fetch has no cookie jar to send.
|
|
98
|
+
redirect: "manual",
|
|
99
|
+
});
|
|
100
|
+
return response;
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/** The transport for a client: the given fetch, else Node's http modules, else the global fetch. */
|
|
104
|
+
function defaultTransport(fetchImpl) {
|
|
105
|
+
if (fetchImpl)
|
|
106
|
+
return fetchTransport(fetchImpl);
|
|
107
|
+
const load = builtinLoader();
|
|
108
|
+
if (load)
|
|
109
|
+
return nodeTransport(load);
|
|
110
|
+
if (typeof globalThis.fetch === "function") {
|
|
111
|
+
return fetchTransport(((input, init) => globalThis.fetch(input, init)));
|
|
112
|
+
}
|
|
113
|
+
return null;
|
|
114
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capacms/sdk",
|
|
3
|
-
"version": "1.0.0-next.
|
|
3
|
+
"version": "1.0.0-next.12",
|
|
4
4
|
"description": "The TypeScript SDK for Capa's content API: REST and GraphQL reads typed from your models, Next.js caching and live preview, and codegen.",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"homepage": "https://docs.capacms.com/api",
|
|
@@ -33,6 +33,16 @@
|
|
|
33
33
|
"types": "./dist/nextjs/index.d.ts",
|
|
34
34
|
"default": "./dist/nextjs/index.js"
|
|
35
35
|
},
|
|
36
|
+
"./server": {
|
|
37
|
+
"types": "./dist/server/index.d.ts",
|
|
38
|
+
"edge-light": "./dist/server/index.js",
|
|
39
|
+
"workerd": "./dist/server/index.js",
|
|
40
|
+
"worker": "./dist/server/index.js",
|
|
41
|
+
"react-server": "./dist/server/index.js",
|
|
42
|
+
"node": "./dist/server/index.js",
|
|
43
|
+
"browser": "./dist/server/browser.js",
|
|
44
|
+
"default": "./dist/server/index.js"
|
|
45
|
+
},
|
|
36
46
|
"./image": {
|
|
37
47
|
"import": {
|
|
38
48
|
"types": "./dist/esm/image/index.d.ts",
|
|
@@ -75,6 +85,9 @@
|
|
|
75
85
|
"nextjs": [
|
|
76
86
|
"dist/nextjs/index.d.ts"
|
|
77
87
|
],
|
|
88
|
+
"server": [
|
|
89
|
+
"dist/server/index.d.ts"
|
|
90
|
+
],
|
|
78
91
|
"overlay": [
|
|
79
92
|
"dist/overlay/index.d.ts"
|
|
80
93
|
],
|
|
@@ -94,7 +107,8 @@
|
|
|
94
107
|
},
|
|
95
108
|
"bin": {
|
|
96
109
|
"capa": "bin/capa.js",
|
|
97
|
-
"capa-codegen": "bin/capa-codegen.js"
|
|
110
|
+
"capa-codegen": "bin/capa-codegen.js",
|
|
111
|
+
"capa-check-bundle": "bin/capa-check-bundle.js"
|
|
98
112
|
},
|
|
99
113
|
"peerDependencies": {
|
|
100
114
|
"graphql": "^16.9.0",
|
|
@@ -112,13 +126,13 @@
|
|
|
112
126
|
"graphql": "^16.9.0",
|
|
113
127
|
"typescript": "^5.5.0",
|
|
114
128
|
"@types/react": "19.0.1",
|
|
115
|
-
"next": "15.5.
|
|
129
|
+
"next": "15.5.28",
|
|
116
130
|
"react": "19.2.8",
|
|
117
131
|
"@capa/shared": "0.1.0"
|
|
118
132
|
},
|
|
119
133
|
"scripts": {
|
|
120
134
|
"build": "node scripts/shared-image.js && tsc -p tsconfig.json && tsc -p tsconfig.esm.json && node scripts/esm-scope.js",
|
|
121
135
|
"typecheck": "node scripts/shared-image.js && tsc -p tsconfig.json --noEmit && tsc -p tsconfig.esm.json --noEmit",
|
|
122
|
-
"test": "pnpm run build && node --test test/comments.test.js test/codegen.test.js test/client.test.js test/next-client.test.js test/nextjs.test.js test/webhooks.test.js test/relations.test.js test/attrs.test.js test/overlay.test.js test/overlay-dom.test.js test/nextjs-overlay-refresh.test.js test/nextjs-overlay-bundle.test.js test/inflate.test.js test/field-names.test.js test/next-graphql.test.js test/graphql-codegen.test.js test/graphql-contract.test.js test/readme-snippets.test.js test/nextjs-app.test.js test/nextjs-preview-app.test.js test/package-json.test.js test/changelog.test.js test/published-docs.test.js test/builder-messages.test.js test/image.test.js test/nextjs-image-loader.test.js test/capa-cli-handoff.test.js && tsc -p test/types/tsconfig.consumer.json --noEmit && tsc -p test/types/tsconfig.nonstrict.json --noEmit && tsc -p test/types/tsconfig.next.json --noEmit"
|
|
136
|
+
"test": "pnpm run build && node --test test/comments.test.js test/codegen.test.js test/client.test.js test/next-client.test.js test/nextjs.test.js test/webhooks.test.js test/relations.test.js test/attrs.test.js test/overlay.test.js test/overlay-dom.test.js test/overlay-items.test.js test/live-fields.test.js test/markdown.test.js test/overlay-fields.test.js test/nextjs-overlay-refresh.test.js test/nextjs-overlay-bundle.test.js test/inflate.test.js test/field-names.test.js test/next-graphql.test.js test/graphql-codegen.test.js test/graphql-contract.test.js test/readme-snippets.test.js test/nextjs-app.test.js test/nextjs-preview-app.test.js test/package-json.test.js test/changelog.test.js test/published-docs.test.js test/builder-messages.test.js test/image.test.js test/nextjs-image-loader.test.js test/capa-cli-handoff.test.js test/server.test.js test/nextjs-write-key-guard.test.js && tsc -p test/types/tsconfig.consumer.json --noEmit && tsc -p test/types/tsconfig.nonstrict.json --noEmit && tsc -p test/types/tsconfig.next.json --noEmit"
|
|
123
137
|
}
|
|
124
138
|
}
|