@valbuild/shared 0.72.4 → 0.73.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.
@@ -3,6 +3,7 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var core = require('@valbuild/core');
6
+ var buffer = require('buffer');
6
7
  var zod = require('zod');
7
8
  var zodValidationError = require('zod-validation-error');
8
9
 
@@ -116,7 +117,7 @@ function _createForOfIteratorHelper(r, e) {
116
117
  };
117
118
  }
118
119
 
119
- function remirrorToRichTextSource(node, configDirectory) {
120
+ function remirrorToRichTextSource(node, configDirectory, remoteOptions) {
120
121
  var files = {};
121
122
  var blocks = [];
122
123
  var i = 0;
@@ -125,7 +126,7 @@ function remirrorToRichTextSource(node, configDirectory) {
125
126
  try {
126
127
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
127
128
  var child = _step.value;
128
- var block = convertBlock([(i++).toString()], child, files, configDirectory);
129
+ var block = convertBlock([(i++).toString()], child, files, configDirectory, remoteOptions);
129
130
  blocks.push(block);
130
131
  }
131
132
  } catch (err) {
@@ -138,7 +139,7 @@ function remirrorToRichTextSource(node, configDirectory) {
138
139
  files: files
139
140
  };
140
141
  }
141
- function convertBlock(path, node, files, configDirectory) {
142
+ function convertBlock(path, node, files, configDirectory, remoteOptions) {
142
143
  if (node.type === "heading") {
143
144
  var _node$attrs, _node$content;
144
145
  var depth = ((_node$attrs = node.attrs) === null || _node$attrs === void 0 ? void 0 : _node$attrs.level) || 1;
@@ -148,21 +149,21 @@ function convertBlock(path, node, files, configDirectory) {
148
149
  return {
149
150
  tag: "h".concat(depth),
150
151
  children: ((_node$content = node.content) === null || _node$content === void 0 ? void 0 : _node$content.map(function (child, i) {
151
- return convertHeadingChild(path.concat("children", i.toString()), child, files, configDirectory);
152
+ return convertHeadingChild(path.concat("children", i.toString()), child, files, configDirectory, remoteOptions);
152
153
  })) || []
153
154
  };
154
155
  } else if (node.type === "paragraph") {
155
- return convertParagraph(path, node, files, configDirectory);
156
+ return convertParagraph(path, node, files, configDirectory, remoteOptions);
156
157
  } else if (node.type === "bulletList") {
157
- return convertBulletList(path, node, files, configDirectory);
158
+ return convertBulletList(path, node, files, configDirectory, remoteOptions);
158
159
  } else if (node.type === "orderedList") {
159
- return convertOrderedList(path, node, files, configDirectory);
160
+ return convertOrderedList(path, node, files, configDirectory, remoteOptions);
160
161
  } else {
161
162
  var exhaustiveCheck = node;
162
163
  throw new Error("Unhandled node type: ".concat("type" in exhaustiveCheck ? "exhaustiveCheck.type" : "unknown"));
163
164
  }
164
165
  }
165
- function convertHeadingChild(path, node, files, configDirectory) {
166
+ function convertHeadingChild(path, node, files, configDirectory, remoteOptions) {
166
167
  if (node.type === "text") {
167
168
  return convertTextNode(node);
168
169
  } else if (node.type === "hardBreak") {
@@ -170,13 +171,13 @@ function convertHeadingChild(path, node, files, configDirectory) {
170
171
  tag: "br"
171
172
  };
172
173
  } else if (node.type === "image") {
173
- return convertImageNode(path, node, files, configDirectory);
174
+ return convertImageNode(path, node, files, configDirectory, remoteOptions);
174
175
  } else {
175
176
  var exhaustiveCheck = node;
176
177
  throw new Error("Unexpected heading child type: ".concat(JSON.stringify(exhaustiveCheck, null, 2)));
177
178
  }
178
179
  }
179
- function convertParagraph(path, child, files, configDirectory) {
180
+ function convertParagraph(path, child, files, configDirectory, remoteOptions) {
180
181
  var _child$content;
181
182
  return {
182
183
  tag: "p",
@@ -188,7 +189,7 @@ function convertParagraph(path, child, files, configDirectory) {
188
189
  tag: "br"
189
190
  };
190
191
  } else if (child.type === "image") {
191
- return convertImageNode(path.concat("children", i.toString()), child, files, configDirectory);
192
+ return convertImageNode(path.concat("children", i.toString()), child, files, configDirectory, remoteOptions);
192
193
  }
193
194
  var exhaustiveCheck = child;
194
195
  throw new Error("Unexpected paragraph child type: ".concat(JSON.stringify(exhaustiveCheck, null, 2)));
@@ -247,17 +248,17 @@ function convertTextNode(node) {
247
248
  throw new Error("Unexpected node type: ".concat(exhaustiveCheck));
248
249
  }
249
250
  }
250
- function convertListItem(path, child, files, configDirectory) {
251
+ function convertListItem(path, child, files, configDirectory, remoteOptions) {
251
252
  var _child$content2;
252
253
  return {
253
254
  tag: "li",
254
255
  children: ((_child$content2 = child.content) === null || _child$content2 === void 0 ? void 0 : _child$content2.map(function (child, i) {
255
256
  if (child.type === "paragraph") {
256
- return convertParagraph(path.concat("children", i.toString()), child, files, configDirectory);
257
+ return convertParagraph(path.concat("children", i.toString()), child, files, configDirectory, remoteOptions);
257
258
  } else if (child.type === "bulletList") {
258
- return convertBulletList(path.concat("children", i.toString()), child, files, configDirectory);
259
+ return convertBulletList(path.concat("children", i.toString()), child, files, configDirectory, remoteOptions);
259
260
  } else if (child.type === "orderedList") {
260
- return convertOrderedList(path.concat("children", i.toString()), child, files, configDirectory);
261
+ return convertOrderedList(path.concat("children", i.toString()), child, files, configDirectory, remoteOptions);
261
262
  } else {
262
263
  var exhaustiveCheck = child;
263
264
  throw new Error("Unexpected list child type: ".concat(exhaustiveCheck));
@@ -265,13 +266,13 @@ function convertListItem(path, child, files, configDirectory) {
265
266
  })) || []
266
267
  };
267
268
  }
268
- function convertBulletList(path, node, files, configDirectory) {
269
+ function convertBulletList(path, node, files, configDirectory, remoteOptions) {
269
270
  var _node$content2;
270
271
  return {
271
272
  tag: "ul",
272
273
  children: ((_node$content2 = node.content) === null || _node$content2 === void 0 ? void 0 : _node$content2.map(function (child, i) {
273
274
  if (child.type === "listItem") {
274
- return convertListItem(path.concat("children", i.toString()), child, files, configDirectory);
275
+ return convertListItem(path.concat("children", i.toString()), child, files, configDirectory, remoteOptions);
275
276
  } else {
276
277
  var exhaustiveCheck = child.type;
277
278
  throw new Error("Unexpected bullet list child type: ".concat(exhaustiveCheck));
@@ -279,13 +280,13 @@ function convertBulletList(path, node, files, configDirectory) {
279
280
  })) || []
280
281
  };
281
282
  }
282
- function convertOrderedList(path, node, files, configDirectory) {
283
+ function convertOrderedList(path, node, files, configDirectory, remoteOptions) {
283
284
  var _node$content3;
284
285
  return {
285
286
  tag: "ol",
286
287
  children: ((_node$content3 = node.content) === null || _node$content3 === void 0 ? void 0 : _node$content3.map(function (child, i) {
287
288
  if (child.type === "listItem") {
288
- return convertListItem(path.concat("children", i.toString()), child, files, configDirectory);
289
+ return convertListItem(path.concat("children", i.toString()), child, files, configDirectory, remoteOptions);
289
290
  } else {
290
291
  var exhaustiveCheck = child.type;
291
292
  throw new Error("Unexpected ordered list child type: ".concat(exhaustiveCheck));
@@ -294,9 +295,10 @@ function convertOrderedList(path, node, files, configDirectory) {
294
295
  };
295
296
  }
296
297
  var textEncoder = new TextEncoder();
297
- function convertImageNode(path, node, files, configDirectory) {
298
+ function convertImageNode(path, node, files, configDirectory, remoteOptions) {
298
299
  if (node.attrs && node.attrs.src.startsWith("data:")) {
299
- var sha256 = core.Internal.getSHA256Hash(textEncoder.encode(node.attrs.src));
300
+ var binaryData = buffer.Buffer.from(node.attrs.src.split(",")[1], "base64");
301
+ var fullFileHash = core.Internal.getSHA256Hash(new Uint8Array(binaryData));
300
302
  var mimeType = core.Internal.getMimeType(node.attrs.src);
301
303
  if (mimeType === undefined) {
302
304
  throw new Error("Could not detect Mime Type for image: ".concat(node.attrs.src));
@@ -305,7 +307,7 @@ function convertImageNode(path, node, files, configDirectory) {
305
307
  width: typeof node.attrs.width === "number" ? node.attrs.width : 0,
306
308
  height: typeof node.attrs.height === "number" ? node.attrs.height : 0,
307
309
  mimeType: mimeType
308
- }, sha256);
310
+ }, fullFileHash);
309
311
  var dir = configDirectory !== null && configDirectory !== void 0 && configDirectory.endsWith("/") ? configDirectory : "".concat(configDirectory, "/");
310
312
  var filePath = "".concat(dir).concat(fileName);
311
313
  var existingFilesEntry = files[filePath];
@@ -320,26 +322,41 @@ function convertImageNode(path, node, files, configDirectory) {
320
322
  patchPaths: [thisPath]
321
323
  };
322
324
  }
325
+ var remoteFileHash = core.Internal.remote.hashToRemoteFileHash(fullFileHash);
326
+ var ref = remoteOptions ? core.Internal.remote.createRemoteRef(remoteOptions.remoteHost, _objectSpread2(_objectSpread2({}, remoteOptions), {}, {
327
+ fileHash: remoteFileHash,
328
+ validationHash: core.Internal.remote.getValidationHash(remoteOptions.coreVersion, remoteOptions.schema, mimeType, {
329
+ width: typeof node.attrs.width === "number" ? node.attrs.width : 0,
330
+ height: typeof node.attrs.height === "number" ? node.attrs.height : 0
331
+ }, remoteFileHash, textEncoder),
332
+ filePath: filePath.slice(1)
333
+ })) : filePath;
323
334
  return {
324
335
  tag: "img",
325
- src: _defineProperty(_defineProperty(_defineProperty(_defineProperty({}, core.VAL_EXTENSION, "file"), core.FILE_REF_PROP, filePath), core.FILE_REF_SUBTYPE_TAG, "image"), "metadata", {
326
- width: typeof node.attrs.width === "number" ? node.attrs.width : 0,
327
- height: typeof node.attrs.height === "number" ? node.attrs.height : 0,
328
- mimeType: mimeType
336
+ src: _objectSpread2(_objectSpread2(_defineProperty({}, core.FILE_REF_PROP, ref), remoteOptions ? _defineProperty({}, core.VAL_EXTENSION, "remote") : _defineProperty(_defineProperty({}, core.VAL_EXTENSION, "file"), core.FILE_REF_SUBTYPE_TAG, "image")), {}, {
337
+ metadata: {
338
+ width: typeof node.attrs.width === "number" ? node.attrs.width : 0,
339
+ height: typeof node.attrs.height === "number" ? node.attrs.height : 0,
340
+ mimeType: mimeType
341
+ }
329
342
  })
330
343
  };
331
344
  } else if (node.attrs) {
332
345
  var url = node.attrs.src;
333
346
  var patchId = getParam("patch_id", url);
334
347
  var noParamsUrl = url.split("?")[0];
348
+ var remote = false;
335
349
  if (patchId) {
350
+ remote = getParam("remote", url) === "true";
336
351
  if (noParamsUrl.startsWith("/api/val/files/public")) {
337
352
  noParamsUrl = noParamsUrl.slice("/api/val/files".length);
338
353
  } else {
339
354
  console.error("Patched image URL does not start with /api/val/files: " + url);
340
355
  }
341
356
  } else {
342
- if (!noParamsUrl.startsWith("/public")) {
357
+ if (remoteOptions && noParamsUrl.startsWith(remoteOptions.remoteHost)) {
358
+ remote = true;
359
+ } else if (!noParamsUrl.startsWith("/public")) {
343
360
  noParamsUrl = "/public".concat(noParamsUrl);
344
361
  } else {
345
362
  console.error("Unpatched image URL starts with /public: " + url);
@@ -347,11 +364,13 @@ function convertImageNode(path, node, files, configDirectory) {
347
364
  }
348
365
  var tag = {
349
366
  tag: "img",
350
- src: _objectSpread2(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, core.VAL_EXTENSION, "file"), core.FILE_REF_PROP, noParamsUrl), core.FILE_REF_SUBTYPE_TAG, "image"), "metadata", {
351
- width: typeof node.attrs.width === "number" ? node.attrs.width : 0,
352
- height: typeof node.attrs.height === "number" ? node.attrs.height : 0,
353
- mimeType: noParamsUrl && core.Internal.filenameToMimeType(noParamsUrl) || ""
354
- }), patchId ? {
367
+ src: _objectSpread2(_objectSpread2({}, remote ? _defineProperty(_defineProperty({}, core.VAL_EXTENSION, "remote"), core.FILE_REF_PROP, noParamsUrl) : _defineProperty(_defineProperty(_defineProperty({}, core.VAL_EXTENSION, "file"), core.FILE_REF_SUBTYPE_TAG, "image"), core.FILE_REF_PROP, noParamsUrl)), {}, {
368
+ metadata: {
369
+ width: typeof node.attrs.width === "number" ? node.attrs.width : 0,
370
+ height: typeof node.attrs.height === "number" ? node.attrs.height : 0,
371
+ mimeType: noParamsUrl && core.Internal.filenameToMimeType(noParamsUrl) || ""
372
+ }
373
+ }, patchId ? {
355
374
  patch_id: patchId
356
375
  } : {})
357
376
  };
@@ -625,10 +644,15 @@ function convertLinkNodeToRemirror(linkNode) {
625
644
  function convertImageNodeToRemirror(imageNode) {
626
645
  var _fileSource$metadata, _fileSource$metadata2;
627
646
  var fileSource = imageNode.src;
628
- if (!(core.VAL_EXTENSION in fileSource) || fileSource[core.VAL_EXTENSION] !== "file") {
647
+ if (!(core.VAL_EXTENSION in fileSource) || fileSource[core.VAL_EXTENSION] !== "file" && fileSource[core.VAL_EXTENSION] !== "remote") {
629
648
  throw Error("Expected file source in image node");
630
649
  }
631
- var fileVal = core.Internal.convertFileSource(fileSource);
650
+ var fileVal;
651
+ if (fileSource[core.VAL_EXTENSION] === "remote") {
652
+ fileVal = core.Internal.convertRemoteSource(fileSource);
653
+ } else {
654
+ fileVal = core.Internal.convertFileSource(fileSource);
655
+ }
632
656
  return {
633
657
  type: "image",
634
658
  attrs: {
@@ -1088,7 +1112,8 @@ var OperationJSONT = zod.z.discriminatedUnion("op", [zod.z.object({
1088
1112
  op: zod.z.literal("file"),
1089
1113
  path: zod.z.string(),
1090
1114
  filePath: zod.z.string(),
1091
- value: zod.z.string()
1115
+ value: zod.z.string(),
1116
+ remote: zod.z["boolean"]()
1092
1117
  }).strict()]);
1093
1118
  var PatchJSON = zod.z.array(OperationJSONT);
1094
1119
  /**
@@ -1122,7 +1147,8 @@ var OperationT = zod.z.discriminatedUnion("op", [zod.z.object({
1122
1147
  path: zod.z.array(zod.z.string()),
1123
1148
  filePath: zod.z.string(),
1124
1149
  nestedFilePath: zod.z.array(zod.z.string()).optional(),
1125
- value: zod.z.string()
1150
+ value: zod.z.string(),
1151
+ remote: zod.z["boolean"]()
1126
1152
  }).strict()]);
1127
1153
  var Patch = zod.z.array(OperationT);
1128
1154
  var PatchId = zod.z.string().refine(function (_id) {
@@ -1202,6 +1228,17 @@ var SerializedUnionSchema = zod.z.lazy(function () {
1202
1228
  opt: zod.z["boolean"]()
1203
1229
  })]);
1204
1230
  });
1231
+ var ImageOptions = zod.z.object({
1232
+ ext: zod.z.union([zod.z.tuple([zod.z.literal("jpg")]), zod.z.tuple([zod.z.literal("webp")])]).optional(),
1233
+ directory: zod.z.string().optional(),
1234
+ prefix: zod.z.string().optional(),
1235
+ accept: zod.z.string().optional()
1236
+ });
1237
+ var SerializedImageSchema = zod.z.object({
1238
+ type: zod.z.literal("image"),
1239
+ options: ImageOptions.optional(),
1240
+ opt: zod.z["boolean"]()
1241
+ });
1205
1242
  var RichTextOptions = zod.z.object({
1206
1243
  style: zod.z.object({
1207
1244
  bold: zod.z["boolean"]().optional(),
@@ -1220,7 +1257,7 @@ var RichTextOptions = zod.z.object({
1220
1257
  }).optional(),
1221
1258
  inline: zod.z.object({
1222
1259
  a: zod.z["boolean"]().optional(),
1223
- img: zod.z["boolean"]().optional()
1260
+ img: zod.z.union([zod.z["boolean"](), SerializedImageSchema]).optional()
1224
1261
  }).optional()
1225
1262
  });
1226
1263
  var SerializedRichTextSchema = zod.z.object({
@@ -1256,17 +1293,6 @@ var SerializedDateSchema = zod.z.object({
1256
1293
  type: zod.z.literal("date"),
1257
1294
  opt: zod.z["boolean"]()
1258
1295
  });
1259
- var ImageOptions = zod.z.object({
1260
- ext: zod.z.union([zod.z.tuple([zod.z.literal("jpg")]), zod.z.tuple([zod.z.literal("webp")])]).optional(),
1261
- directory: zod.z.string().optional(),
1262
- prefix: zod.z.string().optional(),
1263
- accept: zod.z.string().optional()
1264
- });
1265
- var SerializedImageSchema = zod.z.object({
1266
- type: zod.z.literal("image"),
1267
- options: ImageOptions.optional(),
1268
- opt: zod.z["boolean"]()
1269
- });
1270
1296
  var SerializedSchema = zod.z.union([SerializedStringSchema, SerializedLiteralSchema, SerializedBooleanSchema, SerializedNumberSchema, SerializedObjectSchema, SerializedArraySchema, SerializedUnionSchema, SerializedRichTextSchema, SerializedRecordSchema, SerializedKeyOfSchema, SerializedFileSchema, SerializedDateSchema, SerializedImageSchema]);
1271
1297
 
1272
1298
  var ModuleFilePath = zod.z.string().refine(function (_path) {
@@ -1289,7 +1315,7 @@ var ValConfig = zod.z.object({
1289
1315
  gitCommit: zod.z.string().optional(),
1290
1316
  gitBranch: zod.z.string().optional()
1291
1317
  });
1292
- var ValidationFixZ = zod.z.union([zod.z.literal("image:change-extension"), zod.z.literal("image:add-metadata"), zod.z.literal("image:check-metadata"), zod.z.literal("file:change-extension"), zod.z.literal("file:add-metadata"), zod.z.literal("file:check-metadata"), zod.z.literal("keyof:check-keys")]);
1318
+ var ValidationFixZ = zod.z.union([zod.z.literal("image:change-extension"), zod.z.literal("image:add-metadata"), zod.z.literal("image:check-metadata"), zod.z.literal("image:check-remote"), zod.z.literal("image:upload-remote"), zod.z.literal("image:download-remote"), zod.z.literal("file:change-extension"), zod.z.literal("file:add-metadata"), zod.z.literal("file:check-metadata"), zod.z.literal("file:check-remote"), zod.z.literal("file:upload-remote"), zod.z.literal("file:download-remote"), zod.z.literal("keyof:check-keys")]);
1293
1319
  var ValidationError = zod.z.object({
1294
1320
  message: zod.z.string(),
1295
1321
  value: zod.z.unknown().optional(),
@@ -1559,6 +1585,42 @@ var Api = {
1559
1585
  })
1560
1586
  }
1561
1587
  },
1588
+ "/remote/settings": {
1589
+ GET: {
1590
+ req: {
1591
+ cookies: {
1592
+ val_session: zod.z.string().optional()
1593
+ }
1594
+ },
1595
+ res: zod.z.union([zod.z.object({
1596
+ status: zod.z.literal(200),
1597
+ json: zod.z.object({
1598
+ publicProjectId: zod.z.string(),
1599
+ coreVersion: zod.z.string(),
1600
+ remoteFileBuckets: zod.z.array(zod.z.object({
1601
+ bucket: zod.z.string()
1602
+ }))
1603
+ })
1604
+ }), zod.z.object({
1605
+ status: zod.z.literal(400),
1606
+ json: zod.z.object({
1607
+ errorCode: zod.z.union([zod.z.literal("project-not-configured"), zod.z.literal("error-could-not-get-settings"), zod.z.literal("project-not-configured"), zod.z.literal("pat-error"), zod.z.literal("api-key-missing")]),
1608
+ message: zod.z.string()
1609
+ })
1610
+ }), zod.z.object({
1611
+ status: zod.z.literal(401),
1612
+ json: zod.z.object({
1613
+ errorCode: zod.z.literal("unauthorized").optional(),
1614
+ message: zod.z.string()
1615
+ })
1616
+ }), zod.z.object({
1617
+ status: zod.z.literal(500),
1618
+ json: zod.z.object({
1619
+ message: zod.z.string()
1620
+ })
1621
+ })])
1622
+ }
1623
+ },
1562
1624
  "/stat": {
1563
1625
  POST: {
1564
1626
  req: {
@@ -1850,13 +1912,16 @@ var Api = {
1850
1912
  })
1851
1913
  }), zod.z.object({
1852
1914
  status: zod.z.literal(400),
1853
- json: zod.z.object({
1915
+ json: zod.z.union([zod.z.object({
1854
1916
  message: zod.z.string(),
1855
1917
  details: zod.z.union([zod.z.object({
1856
1918
  sourceFilePatchErrors: zod.z.record(ModuleFilePath, zod.z.array(GenericError)),
1857
1919
  binaryFilePatchErrors: zod.z.record(GenericError)
1858
1920
  }), zod.z.array(GenericError)])
1859
- })
1921
+ }), zod.z.object({
1922
+ message: zod.z.string(),
1923
+ errorCode: zod.z.union([zod.z.literal("project-not-configured"), zod.z.literal("pat-error")])
1924
+ })])
1860
1925
  })])
1861
1926
  }
1862
1927
  },
@@ -1865,7 +1930,8 @@ var Api = {
1865
1930
  req: {
1866
1931
  path: zod.z.string(),
1867
1932
  query: {
1868
- patch_id: PatchId.optional()
1933
+ patch_id: PatchId.optional(),
1934
+ remote: zod.z.string().optional()
1869
1935
  }
1870
1936
  },
1871
1937
  res: zod.z.union([unauthorizedResponse, notFoundResponse, zod.z.object({
@@ -2035,10 +2101,10 @@ var createValClient = function createValClient(host) {
2035
2101
  console.log("Server responded with an error", json);
2036
2102
  return _context.abrupt("return", {
2037
2103
  status: 500,
2038
- json: {
2039
- message: json.message,
2040
- type: "unknown"
2041
- }
2104
+ json: _objectSpread2({
2105
+ type: "unknown",
2106
+ message: json.message
2107
+ }, json)
2042
2108
  });
2043
2109
  case 10:
2044
2110
  responseResult = (_apiEndpoint$res = apiEndpoint.res) === null || _apiEndpoint$res === void 0 ? void 0 : _apiEndpoint$res.safeParse(valClientResult);