stitchkit 0.54.0 → 0.56.0
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 +4 -1
- package/dist/browser/socket-io.d.ts +25 -7
- package/dist/browser/socket-io.d.ts.map +1 -1
- package/dist/cli.js +14 -9
- package/dist/contract/errors.d.ts +7 -0
- package/dist/contract/errors.d.ts.map +1 -1
- package/dist/contract/file-ref.d.ts +13 -0
- package/dist/contract/file-ref.d.ts.map +1 -0
- package/dist/contract/index.d.ts +1 -0
- package/dist/contract/index.d.ts.map +1 -1
- package/dist/contract/index.js +21 -17
- package/dist/files/boundary.d.ts +55 -0
- package/dist/files/boundary.d.ts.map +1 -0
- package/dist/files.d.ts +3 -0
- package/dist/files.d.ts.map +1 -0
- package/dist/files.js +16 -0
- package/dist/index-1bx83sw4.js +4 -0
- package/dist/{index-pfqjb5xy.js → index-28cqssm3.js} +30 -5
- package/dist/index-557by2db.js +34 -0
- package/dist/index-5r13htq1.js +283 -0
- package/dist/index-6k1937bx.js +20 -0
- package/dist/index-6y759j86.js +1031 -0
- package/dist/{index-bmnhqtya.js → index-7eq9va6e.js} +1 -4
- package/dist/index-bfcpjw20.js +363 -0
- package/dist/{index-yxpe3phd.js → index-cby4ar3v.js} +1 -18
- package/dist/{index-7vb44hy8.js → index-escqg10p.js} +193 -751
- package/dist/{index-173tvqcp.js → index-j3dem06f.js} +9 -114
- package/dist/{index-gvha32jh.js → index-jcc611vh.js} +2 -2
- package/dist/{index-z7761jk2.js → index-n4nfa7gh.js} +16 -22
- package/dist/{index-e1es808r.js → index-qh4xevf5.js} +1 -1
- package/dist/index-sbdmyz75.js +18 -0
- package/dist/{index-b15rtha1.js → index-sm2tjx06.js} +98 -3
- package/dist/{index-yez419px.js → index-xy8fmh6w.js} +99 -7
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +188 -40
- package/dist/node.js +31 -27
- package/dist/observability/index.js +28 -25
- package/dist/react.js +2 -2
- package/dist/realtime/contract.d.ts +9 -0
- package/dist/realtime/contract.d.ts.map +1 -1
- package/dist/realtime/index.d.ts +2 -1
- package/dist/realtime/index.d.ts.map +1 -1
- package/dist/realtime/request.d.ts +21 -0
- package/dist/realtime/request.d.ts.map +1 -0
- package/dist/realtime/socket.d.ts +3 -0
- package/dist/realtime/socket.d.ts.map +1 -1
- package/dist/remote.d.ts +9 -0
- package/dist/remote.d.ts.map +1 -0
- package/dist/remote.js +79 -0
- package/dist/server/context-contribution.d.ts +18 -0
- package/dist/server/context-contribution.d.ts.map +1 -0
- package/dist/server/context.d.ts.map +1 -1
- package/dist/server/index.d.ts +2 -1
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +136 -88
- package/dist/server/lifecycle.d.ts +4 -0
- package/dist/server/lifecycle.d.ts.map +1 -0
- package/dist/server/middleware/auth.d.ts +64 -9
- package/dist/server/middleware/auth.d.ts.map +1 -1
- package/dist/server/types.d.ts +1 -1
- package/dist/server/types.d.ts.map +1 -1
- package/dist/testing/surface-conformance.d.ts +180 -0
- package/dist/testing/surface-conformance.d.ts.map +1 -0
- package/dist/testing/surface-manifest.d.ts +255 -0
- package/dist/testing/surface-manifest.d.ts.map +1 -0
- package/dist/testing.d.ts +2 -0
- package/dist/testing.d.ts.map +1 -1
- package/dist/testing.js +714 -3
- package/dist/tools/agent.d.ts.map +1 -1
- package/dist/tools/async-operation.d.ts +360 -0
- package/dist/tools/async-operation.d.ts.map +1 -0
- package/dist/tools/cli-args.d.ts +1 -1
- package/dist/tools/cli-args.d.ts.map +1 -1
- package/dist/tools/cli-format.d.ts +2 -2
- package/dist/tools/cli-format.d.ts.map +1 -1
- package/dist/tools/cli-wait.d.ts +11 -2
- package/dist/tools/cli-wait.d.ts.map +1 -1
- package/dist/tools/cli.d.ts +2 -0
- package/dist/tools/cli.d.ts.map +1 -1
- package/dist/tools/define-download-tool.d.ts +6 -5
- package/dist/tools/define-download-tool.d.ts.map +1 -1
- package/dist/tools/define-upload-tool.d.ts +3 -1
- package/dist/tools/define-upload-tool.d.ts.map +1 -1
- package/dist/tools/define-wait-tool.d.ts +1 -1
- package/dist/tools/define-wait-tool.d.ts.map +1 -1
- package/dist/tools/download-core.d.ts +5 -10
- package/dist/tools/download-core.d.ts.map +1 -1
- package/dist/tools/internal/surface-projector.d.ts +101 -0
- package/dist/tools/internal/surface-projector.d.ts.map +1 -0
- package/dist/tools/lifecycle.d.ts +4 -0
- package/dist/tools/lifecycle.d.ts.map +1 -0
- package/dist/tools/managed-file-error.d.ts +6 -0
- package/dist/tools/managed-file-error.d.ts.map +1 -0
- package/dist/tools/mcp-prepare.d.ts +3 -21
- package/dist/tools/mcp-prepare.d.ts.map +1 -1
- package/dist/tools/mcp-round-policy.d.ts +9 -0
- package/dist/tools/mcp-round-policy.d.ts.map +1 -0
- package/dist/tools/mcp-round.d.ts +0 -4
- package/dist/tools/mcp-round.d.ts.map +1 -1
- package/dist/tools/mount-download.d.ts +5 -4
- package/dist/tools/mount-download.d.ts.map +1 -1
- package/dist/tools/mount-upload.d.ts +4 -2
- package/dist/tools/mount-upload.d.ts.map +1 -1
- package/dist/tools/mount.d.ts +5 -9
- package/dist/tools/mount.d.ts.map +1 -1
- package/dist/tools/runtime-tool.d.ts.map +1 -1
- package/dist/tools/surface.d.ts +7 -0
- package/dist/tools/surface.d.ts.map +1 -1
- package/dist/tools/upload-core.d.ts +3 -2
- package/dist/tools/upload-core.d.ts.map +1 -1
- package/dist/tools/view-file.d.ts +4 -3
- package/dist/tools/view-file.d.ts.map +1 -1
- package/dist/tools/wait-core.d.ts +6 -1
- package/dist/tools/wait-core.d.ts.map +1 -1
- package/dist/tools.d.ts +3 -2
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +705 -599
- package/llms-full.txt +727 -34
- package/package.json +10 -2
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import {
|
|
2
|
+
parseTrailingWildcard
|
|
3
|
+
} from "./index-7eq9va6e.js";
|
|
4
|
+
|
|
5
|
+
// src/contract/errors.ts
|
|
6
|
+
var APP_ERROR_BRAND = Symbol.for("stitchkit.AppError");
|
|
7
|
+
|
|
8
|
+
class AppError extends Error {
|
|
9
|
+
code;
|
|
10
|
+
status;
|
|
11
|
+
details;
|
|
12
|
+
hint;
|
|
13
|
+
traceId;
|
|
14
|
+
constructor(code, message, status = 500, details, hint, traceId) {
|
|
15
|
+
super(message ?? code);
|
|
16
|
+
this.code = code;
|
|
17
|
+
this.status = status;
|
|
18
|
+
this.details = details;
|
|
19
|
+
this.hint = hint;
|
|
20
|
+
this.traceId = traceId;
|
|
21
|
+
this.name = "AppError";
|
|
22
|
+
Object.defineProperty(this, APP_ERROR_BRAND, { value: true });
|
|
23
|
+
}
|
|
24
|
+
static is(err) {
|
|
25
|
+
return typeof err === "object" && err !== null && APP_ERROR_BRAND in err;
|
|
26
|
+
}
|
|
27
|
+
toJSON() {
|
|
28
|
+
return {
|
|
29
|
+
error: {
|
|
30
|
+
code: this.code,
|
|
31
|
+
message: this.message,
|
|
32
|
+
...this.details && { details: this.details },
|
|
33
|
+
...this.hint && { hint: this.hint }
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function notFound(message = "Not found") {
|
|
39
|
+
throw new AppError("NOT_FOUND", message, 404);
|
|
40
|
+
}
|
|
41
|
+
function badRequest(message, details) {
|
|
42
|
+
throw new AppError("BAD_REQUEST", message, 400, details);
|
|
43
|
+
}
|
|
44
|
+
function unauthorized(message = "Unauthorized") {
|
|
45
|
+
throw new AppError("UNAUTHORIZED", message, 401);
|
|
46
|
+
}
|
|
47
|
+
function forbidden(message = "Forbidden") {
|
|
48
|
+
throw new AppError("FORBIDDEN", message, 403);
|
|
49
|
+
}
|
|
50
|
+
function conflict(message = "Conflict", details) {
|
|
51
|
+
throw new AppError("CONFLICT", message, 409, details);
|
|
52
|
+
}
|
|
53
|
+
function rateLimited(message = "Too many requests") {
|
|
54
|
+
throw new AppError("RATE_LIMITED", message, 429);
|
|
55
|
+
}
|
|
56
|
+
var STITCH_ERROR_STATUS = {
|
|
57
|
+
BAD_REQUEST: 400,
|
|
58
|
+
UNAUTHORIZED: 401,
|
|
59
|
+
FORBIDDEN: 403,
|
|
60
|
+
NOT_FOUND: 404,
|
|
61
|
+
METHOD_NOT_ALLOWED: 405,
|
|
62
|
+
CONFLICT: 409,
|
|
63
|
+
RATE_LIMITED: 429,
|
|
64
|
+
VALIDATION_ERROR: 400,
|
|
65
|
+
FILE_INVALID_PATH: 400,
|
|
66
|
+
FILE_OUTSIDE_ROOT: 400,
|
|
67
|
+
FILE_NOT_FOUND: 404,
|
|
68
|
+
FILE_NOT_REGULAR: 422,
|
|
69
|
+
FILE_INSPECTION_REJECTED: 422,
|
|
70
|
+
FILE_TOO_LARGE: 413,
|
|
71
|
+
FILE_EXISTS: 409,
|
|
72
|
+
REALTIME_CONTRACT_VIOLATION: 500,
|
|
73
|
+
INTERNAL_SERVER_ERROR: 500
|
|
74
|
+
};
|
|
75
|
+
function isStitchErrorCode(code) {
|
|
76
|
+
return Object.hasOwn(STITCH_ERROR_STATUS, code);
|
|
77
|
+
}
|
|
78
|
+
function appError(code, message, details) {
|
|
79
|
+
throw new AppError(code, message, isStitchErrorCode(code) ? STITCH_ERROR_STATUS[code] : 500, details);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// src/contract/define.ts
|
|
83
|
+
import { z } from "zod";
|
|
84
|
+
|
|
85
|
+
// src/internal/safe-json.ts
|
|
86
|
+
function isUnsafeKey(key) {
|
|
87
|
+
return key === "__proto__";
|
|
88
|
+
}
|
|
89
|
+
function safeJsonParse(text) {
|
|
90
|
+
return JSON.parse(text, (key, value) => isUnsafeKey(key) ? undefined : value);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// src/contract/define.ts
|
|
94
|
+
function defineContract(meta, endpoints) {
|
|
95
|
+
const toolTransports = new Map;
|
|
96
|
+
for (const [key, ep] of Object.entries(endpoints)) {
|
|
97
|
+
const wildcard = parseTrailingWildcard(ep.path);
|
|
98
|
+
if (wildcard) {
|
|
99
|
+
if (!ep.params) {
|
|
100
|
+
throw new Error(`Contract "${meta.prefix}": endpoint "${key}" wildcard "${wildcard.name}" requires a params schema field`);
|
|
101
|
+
}
|
|
102
|
+
const paramsJson = z.toJSONSchema(ep.params, { io: "input" });
|
|
103
|
+
if (!paramsJson.properties || !(wildcard.name in paramsJson.properties)) {
|
|
104
|
+
throw new Error(`Contract "${meta.prefix}": endpoint "${key}" params schema is missing wildcard field "${wildcard.name}"`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (ep.desc.trim() === "") {
|
|
108
|
+
throw new Error(`Contract "${meta.prefix}": endpoint "${key}" has an empty desc`);
|
|
109
|
+
}
|
|
110
|
+
if (ep.maxJsonBodyBytes !== undefined && (!Number.isSafeInteger(ep.maxJsonBodyBytes) || ep.maxJsonBodyBytes <= 0)) {
|
|
111
|
+
throw new Error(`Contract "${meta.prefix}": endpoint "${key}" maxJsonBodyBytes must be a positive safe integer, received ${ep.maxJsonBodyBytes}`);
|
|
112
|
+
}
|
|
113
|
+
if (ep.multipart)
|
|
114
|
+
assertMultipartEndpoint(meta.prefix, key, ep);
|
|
115
|
+
if (ep.rawResponse)
|
|
116
|
+
assertRawEndpoint(meta.prefix, key, ep);
|
|
117
|
+
if (ep.method === "HEAD")
|
|
118
|
+
assertHeadEndpoint(meta.prefix, key, ep);
|
|
119
|
+
if (ep.rawBody)
|
|
120
|
+
assertRawBodyEndpoint(meta.prefix, key, ep);
|
|
121
|
+
if ("responseMeta" in ep)
|
|
122
|
+
assertResponseMetaEndpoint(meta.prefix, key, ep);
|
|
123
|
+
if (!("toolName" in ep) || !ep.toolName)
|
|
124
|
+
continue;
|
|
125
|
+
const transports = new Set(ep.expose ? ep.expose.filter((t) => t !== "HTTP") : ["MCP", "AGENT"]);
|
|
126
|
+
if (transports.size === 0) {
|
|
127
|
+
throw new Error(`Contract "${meta.prefix}": endpoint "${key}" sets toolName "${ep.toolName}" but is not exposed on any tool transport (MCP / AGENT)`);
|
|
128
|
+
}
|
|
129
|
+
const existing = toolTransports.get(ep.toolName);
|
|
130
|
+
if (existing) {
|
|
131
|
+
for (const t of transports) {
|
|
132
|
+
if (existing.transports.has(t)) {
|
|
133
|
+
throw new Error(`Contract "${meta.prefix}": duplicate toolName "${ep.toolName}" on transport "${t}" (endpoints: "${existing.key}" and "${key}")`);
|
|
134
|
+
}
|
|
135
|
+
existing.transports.add(t);
|
|
136
|
+
}
|
|
137
|
+
} else {
|
|
138
|
+
toolTransports.set(ep.toolName, { key, transports });
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return { meta, endpoints };
|
|
142
|
+
}
|
|
143
|
+
function assertPositiveLimit(where, name, value) {
|
|
144
|
+
if (value !== undefined && (!Number.isSafeInteger(value) || value <= 0)) {
|
|
145
|
+
throw new Error(`${where} ${name} must be a positive safe integer, received ${value}`);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
function assertMultipartEndpoint(prefix, key, ep) {
|
|
149
|
+
const where = `Contract "${prefix}": multipart endpoint "${key}"`;
|
|
150
|
+
if (ep.method !== "POST" && ep.method !== "PUT" && ep.method !== "PATCH") {
|
|
151
|
+
throw new Error(`${where} must use POST, PUT or PATCH`);
|
|
152
|
+
}
|
|
153
|
+
const multipart = ep.multipart;
|
|
154
|
+
if (!multipart || typeof multipart !== "object") {
|
|
155
|
+
throw new Error(`${where} must declare a multipart descriptor`);
|
|
156
|
+
}
|
|
157
|
+
assertPositiveLimit(where, "maxRequestBytes", multipart.maxRequestBytes);
|
|
158
|
+
assertPositiveLimit(where, "maxFieldBytes", multipart.maxFieldBytes);
|
|
159
|
+
const entries = Object.entries(multipart.files);
|
|
160
|
+
if (entries.length === 0)
|
|
161
|
+
throw new Error(`${where} must declare at least one file field`);
|
|
162
|
+
for (const [field, policy] of entries) {
|
|
163
|
+
if (!field || isUnsafeKey(field))
|
|
164
|
+
throw new Error(`${where} has an invalid file field name`);
|
|
165
|
+
assertPositiveLimit(`${where} field "${field}"`, "maxBytes", policy.maxBytes);
|
|
166
|
+
assertPositiveLimit(`${where} field "${field}"`, "maxFiles", policy.maxFiles);
|
|
167
|
+
if (policy.multiple !== true && policy.maxFiles !== undefined) {
|
|
168
|
+
throw new Error(`${where} field "${field}" may set maxFiles only with multiple: true`);
|
|
169
|
+
}
|
|
170
|
+
if (policy.contentTypes) {
|
|
171
|
+
if (policy.contentTypes.length === 0) {
|
|
172
|
+
throw new Error(`${where} field "${field}" contentTypes cannot be empty`);
|
|
173
|
+
}
|
|
174
|
+
for (const contentType of policy.contentTypes) {
|
|
175
|
+
if (!/^[a-z0-9!#$&^_.+-]+\/(?:[a-z0-9!#$&^_.+-]+|\*)$/i.test(contentType)) {
|
|
176
|
+
throw new Error(`${where} field "${field}" has invalid content type policy "${contentType}"`);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
function assertRawEndpoint(prefix, key, ep) {
|
|
183
|
+
const where = `Contract "${prefix}": raw endpoint "${key}"`;
|
|
184
|
+
if (ep.output)
|
|
185
|
+
throw new Error(`${where} cannot declare an output schema`);
|
|
186
|
+
if ("toolName" in ep && ep.toolName)
|
|
187
|
+
throw new Error(`${where} cannot set a toolName`);
|
|
188
|
+
if ("ui" in ep && ep.ui)
|
|
189
|
+
throw new Error(`${where} cannot set MCP ui metadata`);
|
|
190
|
+
if ("annotations" in ep && ep.annotations) {
|
|
191
|
+
throw new Error(`${where} cannot set MCP annotations`);
|
|
192
|
+
}
|
|
193
|
+
const nonHttp = (ep.expose ?? []).filter((t) => t !== "HTTP");
|
|
194
|
+
if (nonHttp.length > 0) {
|
|
195
|
+
throw new Error(`${where} is HTTP-only — remove ${nonHttp.join(", ")} from expose`);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
function assertHeadEndpoint(prefix, key, ep) {
|
|
199
|
+
const where = `Contract "${prefix}": HEAD endpoint "${key}"`;
|
|
200
|
+
if (!ep.rawResponse)
|
|
201
|
+
throw new Error(`${where} must declare rawResponse: true`);
|
|
202
|
+
if (ep.input)
|
|
203
|
+
throw new Error(`${where} cannot declare an input schema`);
|
|
204
|
+
if (ep.multipart)
|
|
205
|
+
throw new Error(`${where} cannot be multipart`);
|
|
206
|
+
if (ep.rawBody)
|
|
207
|
+
throw new Error(`${where} cannot retain a raw body`);
|
|
208
|
+
}
|
|
209
|
+
function assertRawBodyEndpoint(prefix, key, ep) {
|
|
210
|
+
const where = `Contract "${prefix}": rawBody endpoint "${key}"`;
|
|
211
|
+
if (!ep.input)
|
|
212
|
+
throw new Error(`${where} must declare an input schema`);
|
|
213
|
+
if (ep.multipart)
|
|
214
|
+
throw new Error(`${where} cannot be multipart`);
|
|
215
|
+
if (ep.method !== "POST" && ep.method !== "PUT" && ep.method !== "PATCH") {
|
|
216
|
+
throw new Error(`${where} must use POST, PUT or PATCH`);
|
|
217
|
+
}
|
|
218
|
+
if ("toolName" in ep && ep.toolName)
|
|
219
|
+
throw new Error(`${where} cannot set a toolName`);
|
|
220
|
+
if ("ui" in ep && ep.ui)
|
|
221
|
+
throw new Error(`${where} cannot set MCP ui metadata`);
|
|
222
|
+
if ("annotations" in ep && ep.annotations) {
|
|
223
|
+
throw new Error(`${where} cannot set MCP annotations`);
|
|
224
|
+
}
|
|
225
|
+
const nonHttp = (ep.expose ?? []).filter((transport) => transport !== "HTTP");
|
|
226
|
+
if (nonHttp.length > 0) {
|
|
227
|
+
throw new Error(`${where} is HTTP-only — remove ${nonHttp.join(", ")} from expose`);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
function assertResponseMetaEndpoint(prefix, key, ep) {
|
|
231
|
+
const where = `Contract "${prefix}": responseMeta endpoint "${key}"`;
|
|
232
|
+
if (!ep.responseMeta || typeof ep.responseMeta !== "object") {
|
|
233
|
+
throw new Error(`${where} must declare responseMeta as an object`);
|
|
234
|
+
}
|
|
235
|
+
const status = ep.responseMeta.status;
|
|
236
|
+
if (status !== undefined && (!Number.isSafeInteger(status) || status < 200 || status > 299)) {
|
|
237
|
+
throw new Error(`${where} status must be a successful 2xx integer, received ${status}`);
|
|
238
|
+
}
|
|
239
|
+
if (ep.output && (status === 204 || status === 205)) {
|
|
240
|
+
throw new Error(`${where} cannot combine output with bodyless status ${status}`);
|
|
241
|
+
}
|
|
242
|
+
if (ep.rawResponse)
|
|
243
|
+
throw new Error(`${where} cannot also be a rawResponse endpoint`);
|
|
244
|
+
if ("toolName" in ep && ep.toolName)
|
|
245
|
+
throw new Error(`${where} cannot set a toolName`);
|
|
246
|
+
if ("ui" in ep && ep.ui)
|
|
247
|
+
throw new Error(`${where} cannot set MCP ui metadata`);
|
|
248
|
+
if ("annotations" in ep && ep.annotations) {
|
|
249
|
+
throw new Error(`${where} cannot set MCP annotations`);
|
|
250
|
+
}
|
|
251
|
+
const nonHttp = (ep.expose ?? []).filter((transport) => transport !== "HTTP");
|
|
252
|
+
if (nonHttp.length > 0) {
|
|
253
|
+
throw new Error(`${where} is HTTP-only — remove ${nonHttp.join(", ")} from expose`);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
function mergeMeta(contractMeta, endpointMeta) {
|
|
257
|
+
if (!contractMeta)
|
|
258
|
+
return endpointMeta;
|
|
259
|
+
if (!endpointMeta)
|
|
260
|
+
return { ...contractMeta };
|
|
261
|
+
return { ...contractMeta, ...endpointMeta };
|
|
262
|
+
}
|
|
263
|
+
// src/contract/errors-factory.ts
|
|
264
|
+
import { z as z2 } from "zod";
|
|
265
|
+
// src/contract/pagination.ts
|
|
266
|
+
import { z as z3 } from "zod";
|
|
267
|
+
|
|
268
|
+
// src/internal/base64url.ts
|
|
269
|
+
function bytesToBase64Url(bytes) {
|
|
270
|
+
let binary = "";
|
|
271
|
+
for (const byte of bytes)
|
|
272
|
+
binary += String.fromCharCode(byte);
|
|
273
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
274
|
+
}
|
|
275
|
+
function base64UrlToBytes(segment) {
|
|
276
|
+
if (!/^[A-Za-z0-9_-]*$/.test(segment) || segment.length % 4 === 1) {
|
|
277
|
+
throw new Error("invalid base64url segment");
|
|
278
|
+
}
|
|
279
|
+
const b64 = segment.replace(/-/g, "+").replace(/_/g, "/");
|
|
280
|
+
const padded = b64.padEnd(Math.ceil(b64.length / 4) * 4, "=");
|
|
281
|
+
return Uint8Array.from(atob(padded), (c) => c.charCodeAt(0));
|
|
282
|
+
}
|
|
283
|
+
export { isUnsafeKey, safeJsonParse, defineContract, mergeMeta, AppError, notFound, badRequest, unauthorized, forbidden, conflict, rateLimited, STITCH_ERROR_STATUS, isStitchErrorCode, appError, bytesToBase64Url, base64UrlToBytes };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// src/contract/file-ref.ts
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
function isManagedFilePath(value) {
|
|
4
|
+
if (value.length === 0 || value.includes("\x00") || value.includes("\\"))
|
|
5
|
+
return false;
|
|
6
|
+
if (value.startsWith("/") || value.startsWith("//") || /^[A-Za-z]:/.test(value)) {
|
|
7
|
+
return false;
|
|
8
|
+
}
|
|
9
|
+
const parts = value.split("/");
|
|
10
|
+
return parts.every((part) => part.length > 0 && part !== "." && part !== "..");
|
|
11
|
+
}
|
|
12
|
+
var ManagedFilePathSchema = z.string().refine(isManagedFilePath, "expected a canonical relative managed-file path");
|
|
13
|
+
var ManagedFileRefSchema = z.object({
|
|
14
|
+
path: ManagedFilePathSchema,
|
|
15
|
+
size: z.number().int().nonnegative(),
|
|
16
|
+
mediaType: z.string().min(1).optional(),
|
|
17
|
+
name: z.string().min(1).optional()
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
export { ManagedFilePathSchema, ManagedFileRefSchema };
|