@valbuild/shared 0.72.3 → 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.
@@ -1,4 +1,5 @@
1
- import { Internal, VAL_EXTENSION, FILE_REF_PROP, FILE_REF_SUBTYPE_TAG } from '@valbuild/core';
1
+ import { Internal, FILE_REF_PROP, VAL_EXTENSION, FILE_REF_SUBTYPE_TAG } from '@valbuild/core';
2
+ import { Buffer } from 'buffer';
2
3
  import { z } from 'zod';
3
4
  import { fromZodError } from 'zod-validation-error';
4
5
 
@@ -112,7 +113,7 @@ function _createForOfIteratorHelper(r, e) {
112
113
  };
113
114
  }
114
115
 
115
- function remirrorToRichTextSource(node, configDirectory) {
116
+ function remirrorToRichTextSource(node, configDirectory, remoteOptions) {
116
117
  var files = {};
117
118
  var blocks = [];
118
119
  var i = 0;
@@ -121,7 +122,7 @@ function remirrorToRichTextSource(node, configDirectory) {
121
122
  try {
122
123
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
123
124
  var child = _step.value;
124
- var block = convertBlock([(i++).toString()], child, files, configDirectory);
125
+ var block = convertBlock([(i++).toString()], child, files, configDirectory, remoteOptions);
125
126
  blocks.push(block);
126
127
  }
127
128
  } catch (err) {
@@ -134,7 +135,7 @@ function remirrorToRichTextSource(node, configDirectory) {
134
135
  files: files
135
136
  };
136
137
  }
137
- function convertBlock(path, node, files, configDirectory) {
138
+ function convertBlock(path, node, files, configDirectory, remoteOptions) {
138
139
  if (node.type === "heading") {
139
140
  var _node$attrs, _node$content;
140
141
  var depth = ((_node$attrs = node.attrs) === null || _node$attrs === void 0 ? void 0 : _node$attrs.level) || 1;
@@ -144,21 +145,21 @@ function convertBlock(path, node, files, configDirectory) {
144
145
  return {
145
146
  tag: "h".concat(depth),
146
147
  children: ((_node$content = node.content) === null || _node$content === void 0 ? void 0 : _node$content.map(function (child, i) {
147
- return convertHeadingChild(path.concat("children", i.toString()), child, files, configDirectory);
148
+ return convertHeadingChild(path.concat("children", i.toString()), child, files, configDirectory, remoteOptions);
148
149
  })) || []
149
150
  };
150
151
  } else if (node.type === "paragraph") {
151
- return convertParagraph(path, node, files, configDirectory);
152
+ return convertParagraph(path, node, files, configDirectory, remoteOptions);
152
153
  } else if (node.type === "bulletList") {
153
- return convertBulletList(path, node, files, configDirectory);
154
+ return convertBulletList(path, node, files, configDirectory, remoteOptions);
154
155
  } else if (node.type === "orderedList") {
155
- return convertOrderedList(path, node, files, configDirectory);
156
+ return convertOrderedList(path, node, files, configDirectory, remoteOptions);
156
157
  } else {
157
158
  var exhaustiveCheck = node;
158
159
  throw new Error("Unhandled node type: ".concat("type" in exhaustiveCheck ? "exhaustiveCheck.type" : "unknown"));
159
160
  }
160
161
  }
161
- function convertHeadingChild(path, node, files, configDirectory) {
162
+ function convertHeadingChild(path, node, files, configDirectory, remoteOptions) {
162
163
  if (node.type === "text") {
163
164
  return convertTextNode(node);
164
165
  } else if (node.type === "hardBreak") {
@@ -166,13 +167,13 @@ function convertHeadingChild(path, node, files, configDirectory) {
166
167
  tag: "br"
167
168
  };
168
169
  } else if (node.type === "image") {
169
- return convertImageNode(path, node, files, configDirectory);
170
+ return convertImageNode(path, node, files, configDirectory, remoteOptions);
170
171
  } else {
171
172
  var exhaustiveCheck = node;
172
173
  throw new Error("Unexpected heading child type: ".concat(JSON.stringify(exhaustiveCheck, null, 2)));
173
174
  }
174
175
  }
175
- function convertParagraph(path, child, files, configDirectory) {
176
+ function convertParagraph(path, child, files, configDirectory, remoteOptions) {
176
177
  var _child$content;
177
178
  return {
178
179
  tag: "p",
@@ -184,7 +185,7 @@ function convertParagraph(path, child, files, configDirectory) {
184
185
  tag: "br"
185
186
  };
186
187
  } else if (child.type === "image") {
187
- return convertImageNode(path.concat("children", i.toString()), child, files, configDirectory);
188
+ return convertImageNode(path.concat("children", i.toString()), child, files, configDirectory, remoteOptions);
188
189
  }
189
190
  var exhaustiveCheck = child;
190
191
  throw new Error("Unexpected paragraph child type: ".concat(JSON.stringify(exhaustiveCheck, null, 2)));
@@ -243,17 +244,17 @@ function convertTextNode(node) {
243
244
  throw new Error("Unexpected node type: ".concat(exhaustiveCheck));
244
245
  }
245
246
  }
246
- function convertListItem(path, child, files, configDirectory) {
247
+ function convertListItem(path, child, files, configDirectory, remoteOptions) {
247
248
  var _child$content2;
248
249
  return {
249
250
  tag: "li",
250
251
  children: ((_child$content2 = child.content) === null || _child$content2 === void 0 ? void 0 : _child$content2.map(function (child, i) {
251
252
  if (child.type === "paragraph") {
252
- return convertParagraph(path.concat("children", i.toString()), child, files, configDirectory);
253
+ return convertParagraph(path.concat("children", i.toString()), child, files, configDirectory, remoteOptions);
253
254
  } else if (child.type === "bulletList") {
254
- return convertBulletList(path.concat("children", i.toString()), child, files, configDirectory);
255
+ return convertBulletList(path.concat("children", i.toString()), child, files, configDirectory, remoteOptions);
255
256
  } else if (child.type === "orderedList") {
256
- return convertOrderedList(path.concat("children", i.toString()), child, files, configDirectory);
257
+ return convertOrderedList(path.concat("children", i.toString()), child, files, configDirectory, remoteOptions);
257
258
  } else {
258
259
  var exhaustiveCheck = child;
259
260
  throw new Error("Unexpected list child type: ".concat(exhaustiveCheck));
@@ -261,13 +262,13 @@ function convertListItem(path, child, files, configDirectory) {
261
262
  })) || []
262
263
  };
263
264
  }
264
- function convertBulletList(path, node, files, configDirectory) {
265
+ function convertBulletList(path, node, files, configDirectory, remoteOptions) {
265
266
  var _node$content2;
266
267
  return {
267
268
  tag: "ul",
268
269
  children: ((_node$content2 = node.content) === null || _node$content2 === void 0 ? void 0 : _node$content2.map(function (child, i) {
269
270
  if (child.type === "listItem") {
270
- return convertListItem(path.concat("children", i.toString()), child, files, configDirectory);
271
+ return convertListItem(path.concat("children", i.toString()), child, files, configDirectory, remoteOptions);
271
272
  } else {
272
273
  var exhaustiveCheck = child.type;
273
274
  throw new Error("Unexpected bullet list child type: ".concat(exhaustiveCheck));
@@ -275,13 +276,13 @@ function convertBulletList(path, node, files, configDirectory) {
275
276
  })) || []
276
277
  };
277
278
  }
278
- function convertOrderedList(path, node, files, configDirectory) {
279
+ function convertOrderedList(path, node, files, configDirectory, remoteOptions) {
279
280
  var _node$content3;
280
281
  return {
281
282
  tag: "ol",
282
283
  children: ((_node$content3 = node.content) === null || _node$content3 === void 0 ? void 0 : _node$content3.map(function (child, i) {
283
284
  if (child.type === "listItem") {
284
- return convertListItem(path.concat("children", i.toString()), child, files, configDirectory);
285
+ return convertListItem(path.concat("children", i.toString()), child, files, configDirectory, remoteOptions);
285
286
  } else {
286
287
  var exhaustiveCheck = child.type;
287
288
  throw new Error("Unexpected ordered list child type: ".concat(exhaustiveCheck));
@@ -290,9 +291,10 @@ function convertOrderedList(path, node, files, configDirectory) {
290
291
  };
291
292
  }
292
293
  var textEncoder = new TextEncoder();
293
- function convertImageNode(path, node, files, configDirectory) {
294
+ function convertImageNode(path, node, files, configDirectory, remoteOptions) {
294
295
  if (node.attrs && node.attrs.src.startsWith("data:")) {
295
- var sha256 = Internal.getSHA256Hash(textEncoder.encode(node.attrs.src));
296
+ var binaryData = Buffer.from(node.attrs.src.split(",")[1], "base64");
297
+ var fullFileHash = Internal.getSHA256Hash(new Uint8Array(binaryData));
296
298
  var mimeType = Internal.getMimeType(node.attrs.src);
297
299
  if (mimeType === undefined) {
298
300
  throw new Error("Could not detect Mime Type for image: ".concat(node.attrs.src));
@@ -301,7 +303,7 @@ function convertImageNode(path, node, files, configDirectory) {
301
303
  width: typeof node.attrs.width === "number" ? node.attrs.width : 0,
302
304
  height: typeof node.attrs.height === "number" ? node.attrs.height : 0,
303
305
  mimeType: mimeType
304
- }, sha256);
306
+ }, fullFileHash);
305
307
  var dir = configDirectory !== null && configDirectory !== void 0 && configDirectory.endsWith("/") ? configDirectory : "".concat(configDirectory, "/");
306
308
  var filePath = "".concat(dir).concat(fileName);
307
309
  var existingFilesEntry = files[filePath];
@@ -316,26 +318,41 @@ function convertImageNode(path, node, files, configDirectory) {
316
318
  patchPaths: [thisPath]
317
319
  };
318
320
  }
321
+ var remoteFileHash = Internal.remote.hashToRemoteFileHash(fullFileHash);
322
+ var ref = remoteOptions ? Internal.remote.createRemoteRef(remoteOptions.remoteHost, _objectSpread2(_objectSpread2({}, remoteOptions), {}, {
323
+ fileHash: remoteFileHash,
324
+ validationHash: Internal.remote.getValidationHash(remoteOptions.coreVersion, remoteOptions.schema, mimeType, {
325
+ width: typeof node.attrs.width === "number" ? node.attrs.width : 0,
326
+ height: typeof node.attrs.height === "number" ? node.attrs.height : 0
327
+ }, remoteFileHash, textEncoder),
328
+ filePath: filePath.slice(1)
329
+ })) : filePath;
319
330
  return {
320
331
  tag: "img",
321
- src: _defineProperty(_defineProperty(_defineProperty(_defineProperty({}, VAL_EXTENSION, "file"), FILE_REF_PROP, filePath), FILE_REF_SUBTYPE_TAG, "image"), "metadata", {
322
- width: typeof node.attrs.width === "number" ? node.attrs.width : 0,
323
- height: typeof node.attrs.height === "number" ? node.attrs.height : 0,
324
- mimeType: mimeType
332
+ src: _objectSpread2(_objectSpread2(_defineProperty({}, FILE_REF_PROP, ref), remoteOptions ? _defineProperty({}, VAL_EXTENSION, "remote") : _defineProperty(_defineProperty({}, VAL_EXTENSION, "file"), FILE_REF_SUBTYPE_TAG, "image")), {}, {
333
+ metadata: {
334
+ width: typeof node.attrs.width === "number" ? node.attrs.width : 0,
335
+ height: typeof node.attrs.height === "number" ? node.attrs.height : 0,
336
+ mimeType: mimeType
337
+ }
325
338
  })
326
339
  };
327
340
  } else if (node.attrs) {
328
341
  var url = node.attrs.src;
329
342
  var patchId = getParam("patch_id", url);
330
343
  var noParamsUrl = url.split("?")[0];
344
+ var remote = false;
331
345
  if (patchId) {
346
+ remote = getParam("remote", url) === "true";
332
347
  if (noParamsUrl.startsWith("/api/val/files/public")) {
333
348
  noParamsUrl = noParamsUrl.slice("/api/val/files".length);
334
349
  } else {
335
350
  console.error("Patched image URL does not start with /api/val/files: " + url);
336
351
  }
337
352
  } else {
338
- if (!noParamsUrl.startsWith("/public")) {
353
+ if (remoteOptions && noParamsUrl.startsWith(remoteOptions.remoteHost)) {
354
+ remote = true;
355
+ } else if (!noParamsUrl.startsWith("/public")) {
339
356
  noParamsUrl = "/public".concat(noParamsUrl);
340
357
  } else {
341
358
  console.error("Unpatched image URL starts with /public: " + url);
@@ -343,11 +360,13 @@ function convertImageNode(path, node, files, configDirectory) {
343
360
  }
344
361
  var tag = {
345
362
  tag: "img",
346
- src: _objectSpread2(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, VAL_EXTENSION, "file"), FILE_REF_PROP, noParamsUrl), FILE_REF_SUBTYPE_TAG, "image"), "metadata", {
347
- width: typeof node.attrs.width === "number" ? node.attrs.width : 0,
348
- height: typeof node.attrs.height === "number" ? node.attrs.height : 0,
349
- mimeType: noParamsUrl && Internal.filenameToMimeType(noParamsUrl) || ""
350
- }), patchId ? {
363
+ src: _objectSpread2(_objectSpread2({}, remote ? _defineProperty(_defineProperty({}, VAL_EXTENSION, "remote"), FILE_REF_PROP, noParamsUrl) : _defineProperty(_defineProperty(_defineProperty({}, VAL_EXTENSION, "file"), FILE_REF_SUBTYPE_TAG, "image"), FILE_REF_PROP, noParamsUrl)), {}, {
364
+ metadata: {
365
+ width: typeof node.attrs.width === "number" ? node.attrs.width : 0,
366
+ height: typeof node.attrs.height === "number" ? node.attrs.height : 0,
367
+ mimeType: noParamsUrl && Internal.filenameToMimeType(noParamsUrl) || ""
368
+ }
369
+ }, patchId ? {
351
370
  patch_id: patchId
352
371
  } : {})
353
372
  };
@@ -621,10 +640,15 @@ function convertLinkNodeToRemirror(linkNode) {
621
640
  function convertImageNodeToRemirror(imageNode) {
622
641
  var _fileSource$metadata, _fileSource$metadata2;
623
642
  var fileSource = imageNode.src;
624
- if (!(VAL_EXTENSION in fileSource) || fileSource[VAL_EXTENSION] !== "file") {
643
+ if (!(VAL_EXTENSION in fileSource) || fileSource[VAL_EXTENSION] !== "file" && fileSource[VAL_EXTENSION] !== "remote") {
625
644
  throw Error("Expected file source in image node");
626
645
  }
627
- var fileVal = Internal.convertFileSource(fileSource);
646
+ var fileVal;
647
+ if (fileSource[VAL_EXTENSION] === "remote") {
648
+ fileVal = Internal.convertRemoteSource(fileSource);
649
+ } else {
650
+ fileVal = Internal.convertFileSource(fileSource);
651
+ }
628
652
  return {
629
653
  type: "image",
630
654
  attrs: {
@@ -1084,7 +1108,8 @@ var OperationJSONT = z.discriminatedUnion("op", [z.object({
1084
1108
  op: z.literal("file"),
1085
1109
  path: z.string(),
1086
1110
  filePath: z.string(),
1087
- value: z.string()
1111
+ value: z.string(),
1112
+ remote: z["boolean"]()
1088
1113
  }).strict()]);
1089
1114
  var PatchJSON = z.array(OperationJSONT);
1090
1115
  /**
@@ -1118,7 +1143,8 @@ var OperationT = z.discriminatedUnion("op", [z.object({
1118
1143
  path: z.array(z.string()),
1119
1144
  filePath: z.string(),
1120
1145
  nestedFilePath: z.array(z.string()).optional(),
1121
- value: z.string()
1146
+ value: z.string(),
1147
+ remote: z["boolean"]()
1122
1148
  }).strict()]);
1123
1149
  var Patch = z.array(OperationT);
1124
1150
  var PatchId = z.string().refine(function (_id) {
@@ -1198,6 +1224,17 @@ var SerializedUnionSchema = z.lazy(function () {
1198
1224
  opt: z["boolean"]()
1199
1225
  })]);
1200
1226
  });
1227
+ var ImageOptions = z.object({
1228
+ ext: z.union([z.tuple([z.literal("jpg")]), z.tuple([z.literal("webp")])]).optional(),
1229
+ directory: z.string().optional(),
1230
+ prefix: z.string().optional(),
1231
+ accept: z.string().optional()
1232
+ });
1233
+ var SerializedImageSchema = z.object({
1234
+ type: z.literal("image"),
1235
+ options: ImageOptions.optional(),
1236
+ opt: z["boolean"]()
1237
+ });
1201
1238
  var RichTextOptions = z.object({
1202
1239
  style: z.object({
1203
1240
  bold: z["boolean"]().optional(),
@@ -1216,7 +1253,7 @@ var RichTextOptions = z.object({
1216
1253
  }).optional(),
1217
1254
  inline: z.object({
1218
1255
  a: z["boolean"]().optional(),
1219
- img: z["boolean"]().optional()
1256
+ img: z.union([z["boolean"](), SerializedImageSchema]).optional()
1220
1257
  }).optional()
1221
1258
  });
1222
1259
  var SerializedRichTextSchema = z.object({
@@ -1252,17 +1289,6 @@ var SerializedDateSchema = z.object({
1252
1289
  type: z.literal("date"),
1253
1290
  opt: z["boolean"]()
1254
1291
  });
1255
- var ImageOptions = z.object({
1256
- ext: z.union([z.tuple([z.literal("jpg")]), z.tuple([z.literal("webp")])]).optional(),
1257
- directory: z.string().optional(),
1258
- prefix: z.string().optional(),
1259
- accept: z.string().optional()
1260
- });
1261
- var SerializedImageSchema = z.object({
1262
- type: z.literal("image"),
1263
- options: ImageOptions.optional(),
1264
- opt: z["boolean"]()
1265
- });
1266
1292
  var SerializedSchema = z.union([SerializedStringSchema, SerializedLiteralSchema, SerializedBooleanSchema, SerializedNumberSchema, SerializedObjectSchema, SerializedArraySchema, SerializedUnionSchema, SerializedRichTextSchema, SerializedRecordSchema, SerializedKeyOfSchema, SerializedFileSchema, SerializedDateSchema, SerializedImageSchema]);
1267
1293
 
1268
1294
  var ModuleFilePath = z.string().refine(function (_path) {
@@ -1285,7 +1311,7 @@ var ValConfig = z.object({
1285
1311
  gitCommit: z.string().optional(),
1286
1312
  gitBranch: z.string().optional()
1287
1313
  });
1288
- var ValidationFixZ = z.union([z.literal("image:change-extension"), z.literal("image:add-metadata"), z.literal("image:check-metadata"), z.literal("file:change-extension"), z.literal("file:add-metadata"), z.literal("file:check-metadata"), z.literal("keyof:check-keys")]);
1314
+ var ValidationFixZ = z.union([z.literal("image:change-extension"), z.literal("image:add-metadata"), z.literal("image:check-metadata"), z.literal("image:check-remote"), z.literal("image:upload-remote"), z.literal("image:download-remote"), z.literal("file:change-extension"), z.literal("file:add-metadata"), z.literal("file:check-metadata"), z.literal("file:check-remote"), z.literal("file:upload-remote"), z.literal("file:download-remote"), z.literal("keyof:check-keys")]);
1289
1315
  var ValidationError = z.object({
1290
1316
  message: z.string(),
1291
1317
  value: z.unknown().optional(),
@@ -1555,6 +1581,42 @@ var Api = {
1555
1581
  })
1556
1582
  }
1557
1583
  },
1584
+ "/remote/settings": {
1585
+ GET: {
1586
+ req: {
1587
+ cookies: {
1588
+ val_session: z.string().optional()
1589
+ }
1590
+ },
1591
+ res: z.union([z.object({
1592
+ status: z.literal(200),
1593
+ json: z.object({
1594
+ publicProjectId: z.string(),
1595
+ coreVersion: z.string(),
1596
+ remoteFileBuckets: z.array(z.object({
1597
+ bucket: z.string()
1598
+ }))
1599
+ })
1600
+ }), z.object({
1601
+ status: z.literal(400),
1602
+ json: z.object({
1603
+ errorCode: z.union([z.literal("project-not-configured"), z.literal("error-could-not-get-settings"), z.literal("project-not-configured"), z.literal("pat-error"), z.literal("api-key-missing")]),
1604
+ message: z.string()
1605
+ })
1606
+ }), z.object({
1607
+ status: z.literal(401),
1608
+ json: z.object({
1609
+ errorCode: z.literal("unauthorized").optional(),
1610
+ message: z.string()
1611
+ })
1612
+ }), z.object({
1613
+ status: z.literal(500),
1614
+ json: z.object({
1615
+ message: z.string()
1616
+ })
1617
+ })])
1618
+ }
1619
+ },
1558
1620
  "/stat": {
1559
1621
  POST: {
1560
1622
  req: {
@@ -1846,13 +1908,16 @@ var Api = {
1846
1908
  })
1847
1909
  }), z.object({
1848
1910
  status: z.literal(400),
1849
- json: z.object({
1911
+ json: z.union([z.object({
1850
1912
  message: z.string(),
1851
1913
  details: z.union([z.object({
1852
1914
  sourceFilePatchErrors: z.record(ModuleFilePath, z.array(GenericError)),
1853
1915
  binaryFilePatchErrors: z.record(GenericError)
1854
1916
  }), z.array(GenericError)])
1855
- })
1917
+ }), z.object({
1918
+ message: z.string(),
1919
+ errorCode: z.union([z.literal("project-not-configured"), z.literal("pat-error")])
1920
+ })])
1856
1921
  })])
1857
1922
  }
1858
1923
  },
@@ -1861,7 +1926,8 @@ var Api = {
1861
1926
  req: {
1862
1927
  path: z.string(),
1863
1928
  query: {
1864
- patch_id: PatchId.optional()
1929
+ patch_id: PatchId.optional(),
1930
+ remote: z.string().optional()
1865
1931
  }
1866
1932
  },
1867
1933
  res: z.union([unauthorizedResponse, notFoundResponse, z.object({
@@ -2031,10 +2097,10 @@ var createValClient = function createValClient(host) {
2031
2097
  console.log("Server responded with an error", json);
2032
2098
  return _context.abrupt("return", {
2033
2099
  status: 500,
2034
- json: {
2035
- message: json.message,
2036
- type: "unknown"
2037
- }
2100
+ json: _objectSpread2({
2101
+ type: "unknown",
2102
+ message: json.message
2103
+ }, json)
2038
2104
  });
2039
2105
  case 10:
2040
2106
  responseResult = (_apiEndpoint$res = apiEndpoint.res) === null || _apiEndpoint$res === void 0 ? void 0 : _apiEndpoint$res.safeParse(valClientResult);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@valbuild/shared",
3
- "version": "0.72.3",
3
+ "version": "0.73.0",
4
4
  "private": false,
5
5
  "description": "Val shared types and utilities",
6
6
  "scripts": {
@@ -29,7 +29,7 @@
29
29
  "exports": true
30
30
  },
31
31
  "dependencies": {
32
- "@valbuild/core": "~0.72.3",
32
+ "@valbuild/core": "~0.73.0",
33
33
  "zod": "^3.22.4",
34
34
  "zod-validation-error": "^3.3.0"
35
35
  },