@powerlines/core 0.48.57 → 0.48.58

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/dist/lib/vfs.cjs CHANGED
@@ -26,6 +26,7 @@ let _stryke_fs_resolve = require("@stryke/fs/resolve");
26
26
  let _stryke_string_format_pretty_bytes = require("@stryke/string-format/pretty-bytes");
27
27
  let _stryke_capnp = require("@stryke/capnp");
28
28
  _stryke_capnp = require_runtime.__toESM(_stryke_capnp, 1);
29
+ let _stryke_convert = require("@stryke/convert");
29
30
  let _stryke_fs_buffer = require("@stryke/fs/buffer");
30
31
  let _stryke_path_correct_path = require("@stryke/path/correct-path");
31
32
  let _stryke_path_glob_to_regex = require("@stryke/path/glob-to-regex");
@@ -39,6 +40,9 @@ let node_url = require("node:url");
39
40
  function toFilePath(path) {
40
41
  return (0, _stryke_path_correct_path.correctPath)((0, _stryke_path_slash.slash)(path?.toString() || ".").replace(/^file:\/\//, ""));
41
42
  }
43
+ function toUint8Array(view) {
44
+ return new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
45
+ }
42
46
  /**
43
47
  * Checks if a given file id is valid based on the specified prefix.
44
48
  *
@@ -843,7 +847,9 @@ var VirtualFileSystem = class VirtualFileSystem {
843
847
  if (!filePath || !this.existsSync(filePath)) return;
844
848
  const { adapter } = this.#getStorage(filePath);
845
849
  this.#logger.trace(`Reading ${adapter.name} file: ${filePath}`);
846
- return await adapter.get(filePath) ?? void 0;
850
+ const result = await adapter.get(filePath);
851
+ if (!result) return;
852
+ return (0, _stryke_type_checks_is_string.isString)(result) ? result : (0, _stryke_convert.uint8ArrayToString)(toUint8Array(result));
847
853
  }
848
854
  /**
849
855
  * Synchronously reads a file from the virtual file system (VFS).
@@ -856,7 +862,39 @@ var VirtualFileSystem = class VirtualFileSystem {
856
862
  if (!filePath || !this.existsSync(filePath)) return;
857
863
  const { adapter } = this.#getStorage(filePath);
858
864
  this.#logger.trace(`Reading ${adapter.name} file: ${filePath}`);
859
- return adapter.getSync(filePath) ?? void 0;
865
+ const result = adapter.getSync(filePath);
866
+ if (!result) return;
867
+ return (0, _stryke_type_checks_is_string.isString)(result) ? result : (0, _stryke_convert.uint8ArrayToString)(toUint8Array(result));
868
+ }
869
+ /**
870
+ * Asynchronously reads a file from the virtual file system (VFS) as a buffer.
871
+ *
872
+ * @param path - The path or ID of the file to read.
873
+ * @returns A promise that resolves to the file contents as a buffer, or `undefined` if the file does not exist.
874
+ */
875
+ async readBuffer(path) {
876
+ const filePath = await this.resolve(path, void 0, { isFile: true });
877
+ if (!filePath || !this.existsSync(filePath)) return;
878
+ const { adapter } = this.#getStorage(filePath);
879
+ this.#logger.trace(`Reading ${adapter.name} file buffer: ${filePath}`);
880
+ const result = await adapter.get(filePath);
881
+ if (!result) return;
882
+ return (0, _stryke_type_checks_is_string.isString)(result) ? (0, _stryke_convert.stringToUint8Array)(result) : result;
883
+ }
884
+ /**
885
+ * Synchronously reads a file from the virtual file system (VFS) as a buffer.
886
+ *
887
+ * @param path - The path or ID of the file to read.
888
+ * @returns The file contents as a buffer, or `undefined` if the file does not exist.
889
+ */
890
+ readBufferSync(path) {
891
+ const filePath = this.resolveSync(path, void 0, { isFile: true });
892
+ if (!filePath || !this.existsSync(filePath)) return;
893
+ const { adapter } = this.#getStorage(filePath);
894
+ this.#logger.trace(`Reading ${adapter.name} file buffer: ${filePath}`);
895
+ const result = adapter.getSync(filePath);
896
+ if (!result) return;
897
+ return (0, _stryke_type_checks_is_string.isString)(result) ? (0, _stryke_convert.stringToUint8Array)(result) : result;
860
898
  }
861
899
  /**
862
900
  * Writes a file to the virtual file system (VFS).
@@ -870,15 +908,14 @@ var VirtualFileSystem = class VirtualFileSystem {
870
908
  const meta = options.meta ?? {};
871
909
  const resolvedPath = await this.resolve(this.#normalizePath(path)) || path;
872
910
  const { relativeKey, adapter } = this.#getStorage(resolvedPath, options.storage);
873
- this.#logger.trace(`Writing ${resolvedPath} to ${adapter.name === "virtual" ? "the virtual file system" : adapter.name === "file-system" ? "the local file system" : adapter.name} (size: ${(0, _stryke_string_format_pretty_bytes.prettyBytes)(new node_buffer.Blob((0, _stryke_convert_to_array.toArray)(data)).size)})`);
874
911
  let code = data;
875
- try {
912
+ if ((0, _stryke_type_checks_is_string.isString)(data)) try {
876
913
  if (!options.skipFormat) code = await require_lib_utilities_format.format(this.#context, resolvedPath, data);
877
914
  } catch (err) {
878
915
  if (require_constants_extensions.DEFAULT_EXTENSIONS.includes((0, _stryke_path_file_path_fns.findFileExtensionSafe)(resolvedPath, { fullExtension: false }))) this.#logger.warn(`Failed to format file ${resolvedPath} before writing: ${err.message}`);
879
916
  code = data;
880
917
  }
881
- this.#logger.trace(`Writing ${resolvedPath} to ${adapter.name === "virtual" ? "the virtual file system" : adapter.name === "file-system" ? "the local file system" : adapter.name} (size: ${(0, _stryke_string_format_pretty_bytes.prettyBytes)(new node_buffer.Blob((0, _stryke_convert_to_array.toArray)(code)).size)})`);
918
+ this.#logger.trace(`Writing ${resolvedPath} to ${adapter.name === "virtual" ? "the virtual file system" : adapter.name === "file-system" ? "the local file system" : adapter.name} (size: ${(0, _stryke_string_format_pretty_bytes.prettyBytes)((0, _stryke_type_checks_is_string.isString)(code) ? new node_buffer.Blob((0, _stryke_convert_to_array.toArray)(code)).size : code.byteLength)})`);
882
919
  const id = this.#normalizeId(meta.id || resolvedPath);
883
920
  this.metadata[id] = {
884
921
  type: "normal",
@@ -888,7 +925,7 @@ var VirtualFileSystem = class VirtualFileSystem {
888
925
  };
889
926
  this.paths[id] = resolvedPath;
890
927
  this.ids[resolvedPath] = id;
891
- return adapter.set(relativeKey, code);
928
+ return adapter.set(relativeKey, (0, _stryke_type_checks_is_string.isString)(code) ? code : (0, _stryke_convert.uint8ArrayToString)(toUint8Array(code)));
892
929
  }
893
930
  /**
894
931
  * Synchronously writes a file to the virtual file system (VFS).
@@ -901,7 +938,7 @@ var VirtualFileSystem = class VirtualFileSystem {
901
938
  const meta = options.meta ?? {};
902
939
  const resolvedPath = this.resolveSync(this.#normalizePath(path)) || path;
903
940
  const { relativeKey, adapter } = this.#getStorage(resolvedPath, options.storage);
904
- this.#logger.trace(`Writing ${resolvedPath} file to ${adapter.name === "virtual" ? "the virtual file system" : adapter.name === "file-system" ? "the local file system" : adapter.name} (size: ${(0, _stryke_string_format_pretty_bytes.prettyBytes)(new node_buffer.Blob((0, _stryke_convert_to_array.toArray)(data)).size)})`);
941
+ this.#logger.trace(`Writing ${resolvedPath} file to ${adapter.name === "virtual" ? "the virtual file system" : adapter.name === "file-system" ? "the local file system" : adapter.name} (size: ${(0, _stryke_string_format_pretty_bytes.prettyBytes)((0, _stryke_type_checks_is_string.isString)(data) ? new node_buffer.Blob((0, _stryke_convert_to_array.toArray)(data)).size : data.byteLength)})`);
905
942
  const id = this.#normalizeId(meta.id || resolvedPath);
906
943
  this.metadata[id] = {
907
944
  type: "normal",
@@ -911,7 +948,7 @@ var VirtualFileSystem = class VirtualFileSystem {
911
948
  };
912
949
  this.paths[id] = resolvedPath;
913
950
  this.ids[resolvedPath] = id;
914
- return adapter.setSync(relativeKey, data);
951
+ return adapter.setSync(relativeKey, (0, _stryke_type_checks_is_string.isString)(data) ? data : (0, _stryke_convert.uint8ArrayToString)(toUint8Array(data)));
915
952
  }
916
953
  /**
917
954
  * Synchronously creates a directory at the specified path.
@@ -211,6 +211,20 @@ declare class VirtualFileSystem implements VirtualFileSystemInterface {
211
211
  * @returns The contents of the file as a string, or undefined if the file does not exist.
212
212
  */
213
213
  readSync(path: string): string | undefined;
214
+ /**
215
+ * Asynchronously reads a file from the virtual file system (VFS) as a buffer.
216
+ *
217
+ * @param path - The path or ID of the file to read.
218
+ * @returns A promise that resolves to the file contents as a buffer, or `undefined` if the file does not exist.
219
+ */
220
+ readBuffer(path: string): Promise<NodeJS.ArrayBufferView | undefined>;
221
+ /**
222
+ * Synchronously reads a file from the virtual file system (VFS) as a buffer.
223
+ *
224
+ * @param path - The path or ID of the file to read.
225
+ * @returns The file contents as a buffer, or `undefined` if the file does not exist.
226
+ */
227
+ readBufferSync(path: string): NodeJS.ArrayBufferView | undefined;
214
228
  /**
215
229
  * Writes a file to the virtual file system (VFS).
216
230
  *
@@ -219,7 +233,7 @@ declare class VirtualFileSystem implements VirtualFileSystemInterface {
219
233
  * @param options - Optional parameters for writing the file.
220
234
  * @returns A promise that resolves when the file is written.
221
235
  */
222
- write(path: string, data?: string, options?: WriteOptions): Promise<void>;
236
+ write(path: string, data?: string | NodeJS.ArrayBufferView, options?: WriteOptions): Promise<void>;
223
237
  /**
224
238
  * Synchronously writes a file to the virtual file system (VFS).
225
239
  *
@@ -227,7 +241,7 @@ declare class VirtualFileSystem implements VirtualFileSystemInterface {
227
241
  * @param data - The contents of the file.
228
242
  * @param options - Optional parameters for writing the file.
229
243
  */
230
- writeSync(path: string, data?: string, options?: WriteOptions): void;
244
+ writeSync(path: string, data?: string | NodeJS.ArrayBufferView, options?: WriteOptions): void;
231
245
  /**
232
246
  * Synchronously creates a directory at the specified path.
233
247
  *
@@ -1 +1 @@
1
- {"version":3,"file":"vfs.d.cts","names":[],"sources":["../../src/lib/vfs.ts"],"mappings":";;;;;;;;;AA4LA;;;;cAAa,iBAAA,YAA6B,0BAAA;EAAA;EAmcM;;;;;EAAA,OA7bvC,gBAAA,EAAkB,UAAA;EA4oBL;;;;;;EAAA,OA/MA,MAAA,CAAO,OAAA,EAAS,OAAA,GAAU,OAAA,CAAQ,iBAAA;EAihB1C;;;;;;EAAA,OAlcE,UAAA,CAAW,OAAA,EAAS,OAAA,GAAU,iBAAA;EAioBtC;;;EAAA,IAvjBK,QAAA,IAAY,MAAA,SAAe,mBAAA;EAymB3B;;;EAAA,IA9kBA,GAAA,IAAO,MAAA;EA8nBE;;;EAAA,IAnmBT,KAAA,IAAS,MAAA;EAomBI;;;EAAA,cAvkBV,aAAA,IAAiB,SAAA;EA4nBV;;;;;;EAAA,QA7jBd,WAAA;EAu0B+B;;;;;;EAptBzB,MAAA,CAAO,IAAA,WA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gB,OAAA;EAsItC;;;;;;EAlHN,QAAA,CAAS,OAAA,UAAiB,QAAA;EAsH9B;;;;;;EApGU,IAAA,CAAK,IAAA,WAAe,OAAA;EAyK/B;;;;;;EAlJK,QAAA,CAAS,IAAA;EA8MG;;;;;;EAvLN,UAAA,CACX,IAAA,WACC,OAAA,CAAQ,MAAA,CAAO,eAAA;EAkTI;;;;;;EA3Rf,cAAA,CAAe,IAAA,WAAe,MAAA,CAAO,eAAA;EAsWrC;;;;;;;;EA7UM,KAAA,CACX,IAAA,UACA,IAAA,YAAe,MAAA,CAAO,eAAA,EACtB,OAAA,GAAS,YAAA,GACR,OAAA;EA2b+B;;AAAA;;;;;EAzX3B,SAAA,CACL,IAAA,UACA,IAAA,YAAe,MAAA,CAAO,eAAA,EACtB,OAAA,GAAS,YAAA;;;;;;EAyCJ,SAAA,CAAU,OAAA;;;;;;EASJ,KAAA,CAAM,IAAA,WAAe,OAAA;;;;;;;EAU3B,WAAA,CAAY,QAAA,WAAmB,mBAAA;;;;;;;;;;EAkB/B,YAAA,CAAa,EAAA;;;;;;;;;;EAsEb,OAAA,CAAQ,EAAA;;;;;;;;;;EAqCR,cAAA,CAAe,EAAA;;;;;;;;;;;;;;;;;EAwBT,OAAA,CACX,EAAA,UACA,QAAA,WACA,OAAA,GAAS,cAAA,GACR,OAAA;;;;;;;;;;;;;;;;;EA+CI,WAAA,CACL,EAAA,UACA,QAAA,WACA,OAAA,GAAS,cAAA;;;;EAmCE,OAAA,IAAO,OAAA;;;;;;;GA4EN,MAAA,CAAO,YAAA,KAAa,OAAA;AAAA"}
@@ -211,6 +211,20 @@ declare class VirtualFileSystem implements VirtualFileSystemInterface {
211
211
  * @returns The contents of the file as a string, or undefined if the file does not exist.
212
212
  */
213
213
  readSync(path: string): string | undefined;
214
+ /**
215
+ * Asynchronously reads a file from the virtual file system (VFS) as a buffer.
216
+ *
217
+ * @param path - The path or ID of the file to read.
218
+ * @returns A promise that resolves to the file contents as a buffer, or `undefined` if the file does not exist.
219
+ */
220
+ readBuffer(path: string): Promise<NodeJS.ArrayBufferView | undefined>;
221
+ /**
222
+ * Synchronously reads a file from the virtual file system (VFS) as a buffer.
223
+ *
224
+ * @param path - The path or ID of the file to read.
225
+ * @returns The file contents as a buffer, or `undefined` if the file does not exist.
226
+ */
227
+ readBufferSync(path: string): NodeJS.ArrayBufferView | undefined;
214
228
  /**
215
229
  * Writes a file to the virtual file system (VFS).
216
230
  *
@@ -219,7 +233,7 @@ declare class VirtualFileSystem implements VirtualFileSystemInterface {
219
233
  * @param options - Optional parameters for writing the file.
220
234
  * @returns A promise that resolves when the file is written.
221
235
  */
222
- write(path: string, data?: string, options?: WriteOptions): Promise<void>;
236
+ write(path: string, data?: string | NodeJS.ArrayBufferView, options?: WriteOptions): Promise<void>;
223
237
  /**
224
238
  * Synchronously writes a file to the virtual file system (VFS).
225
239
  *
@@ -227,7 +241,7 @@ declare class VirtualFileSystem implements VirtualFileSystemInterface {
227
241
  * @param data - The contents of the file.
228
242
  * @param options - Optional parameters for writing the file.
229
243
  */
230
- writeSync(path: string, data?: string, options?: WriteOptions): void;
244
+ writeSync(path: string, data?: string | NodeJS.ArrayBufferView, options?: WriteOptions): void;
231
245
  /**
232
246
  * Synchronously creates a directory at the specified path.
233
247
  *
@@ -1 +1 @@
1
- {"version":3,"file":"vfs.d.mts","names":[],"sources":["../../src/lib/vfs.ts"],"mappings":";;;;;;;;;AA4LA;;;;cAAa,iBAAA,YAA6B,0BAAA;EAAA;EAmcM;;;;;EAAA,OA7bvC,gBAAA,EAAkB,UAAA;EA4oBL;;;;;;EAAA,OA/MA,MAAA,CAAO,OAAA,EAAS,OAAA,GAAU,OAAA,CAAQ,iBAAA;EAihB1C;;;;;;EAAA,OAlcE,UAAA,CAAW,OAAA,EAAS,OAAA,GAAU,iBAAA;EAioBtC;;;EAAA,IAvjBK,QAAA,IAAY,MAAA,SAAe,mBAAA;EAymB3B;;;EAAA,IA9kBA,GAAA,IAAO,MAAA;EA8nBE;;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resolveSync } from "@stryke/fs/resolve";
24
24
  import { prettyBytes } from "@stryke/string-format/pretty-bytes";
25
25
  import * as capnp from "@stryke/capnp";
26
+ import { stringToUint8Array, uint8ArrayToString } from "@stryke/convert";
26
27
  import { readFileBuffer, readFileBufferSync, writeFileBuffer } from "@stryke/fs/buffer";
27
28
  import { correctPath, stripStars } from "@stryke/path/correct-path";
28
29
  import { globToRegex } from "@stryke/path/glob-to-regex";
@@ -36,6 +37,9 @@ import { fileURLToPath } from "node:url";
36
37
  function toFilePath(path) {
37
38
  return correctPath(slash(path?.toString() || ".").replace(/^file:\/\//, ""));
38
39
  }
40
+ function toUint8Array(view) {
41
+ return new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
42
+ }
39
43
  /**
40
44
  * Checks if a given file id is valid based on the specified prefix.
41
45
  *
@@ -840,7 +844,9 @@ var VirtualFileSystem = class VirtualFileSystem {
840
844
  if (!filePath || !this.existsSync(filePath)) return;
841
845
  const { adapter } = this.#getStorage(filePath);
842
846
  this.#logger.trace(`Reading ${adapter.name} file: ${filePath}`);
843
- return await adapter.get(filePath) ?? void 0;
847
+ const result = await adapter.get(filePath);
848
+ if (!result) return;
849
+ return isString(result) ? result : uint8ArrayToString(toUint8Array(result));
844
850
  }
845
851
  /**
846
852
  * Synchronously reads a file from the virtual file system (VFS).
@@ -853,7 +859,39 @@ var VirtualFileSystem = class VirtualFileSystem {
853
859
  if (!filePath || !this.existsSync(filePath)) return;
854
860
  const { adapter } = this.#getStorage(filePath);
855
861
  this.#logger.trace(`Reading ${adapter.name} file: ${filePath}`);
856
- return adapter.getSync(filePath) ?? void 0;
862
+ const result = adapter.getSync(filePath);
863
+ if (!result) return;
864
+ return isString(result) ? result : uint8ArrayToString(toUint8Array(result));
865
+ }
866
+ /**
867
+ * Asynchronously reads a file from the virtual file system (VFS) as a buffer.
868
+ *
869
+ * @param path - The path or ID of the file to read.
870
+ * @returns A promise that resolves to the file contents as a buffer, or `undefined` if the file does not exist.
871
+ */
872
+ async readBuffer(path) {
873
+ const filePath = await this.resolve(path, void 0, { isFile: true });
874
+ if (!filePath || !this.existsSync(filePath)) return;
875
+ const { adapter } = this.#getStorage(filePath);
876
+ this.#logger.trace(`Reading ${adapter.name} file buffer: ${filePath}`);
877
+ const result = await adapter.get(filePath);
878
+ if (!result) return;
879
+ return isString(result) ? stringToUint8Array(result) : result;
880
+ }
881
+ /**
882
+ * Synchronously reads a file from the virtual file system (VFS) as a buffer.
883
+ *
884
+ * @param path - The path or ID of the file to read.
885
+ * @returns The file contents as a buffer, or `undefined` if the file does not exist.
886
+ */
887
+ readBufferSync(path) {
888
+ const filePath = this.resolveSync(path, void 0, { isFile: true });
889
+ if (!filePath || !this.existsSync(filePath)) return;
890
+ const { adapter } = this.#getStorage(filePath);
891
+ this.#logger.trace(`Reading ${adapter.name} file buffer: ${filePath}`);
892
+ const result = adapter.getSync(filePath);
893
+ if (!result) return;
894
+ return isString(result) ? stringToUint8Array(result) : result;
857
895
  }
858
896
  /**
859
897
  * Writes a file to the virtual file system (VFS).
@@ -867,15 +905,14 @@ var VirtualFileSystem = class VirtualFileSystem {
867
905
  const meta = options.meta ?? {};
868
906
  const resolvedPath = await this.resolve(this.#normalizePath(path)) || path;
869
907
  const { relativeKey, adapter } = this.#getStorage(resolvedPath, options.storage);
870
- this.#logger.trace(`Writing ${resolvedPath} to ${adapter.name === "virtual" ? "the virtual file system" : adapter.name === "file-system" ? "the local file system" : adapter.name} (size: ${prettyBytes(new Blob(toArray(data)).size)})`);
871
908
  let code = data;
872
- try {
909
+ if (isString(data)) try {
873
910
  if (!options.skipFormat) code = await format(this.#context, resolvedPath, data);
874
911
  } catch (err) {
875
912
  if (DEFAULT_EXTENSIONS.includes(findFileExtensionSafe(resolvedPath, { fullExtension: false }))) this.#logger.warn(`Failed to format file ${resolvedPath} before writing: ${err.message}`);
876
913
  code = data;
877
914
  }
878
- this.#logger.trace(`Writing ${resolvedPath} to ${adapter.name === "virtual" ? "the virtual file system" : adapter.name === "file-system" ? "the local file system" : adapter.name} (size: ${prettyBytes(new Blob(toArray(code)).size)})`);
915
+ this.#logger.trace(`Writing ${resolvedPath} to ${adapter.name === "virtual" ? "the virtual file system" : adapter.name === "file-system" ? "the local file system" : adapter.name} (size: ${prettyBytes(isString(code) ? new Blob(toArray(code)).size : code.byteLength)})`);
879
916
  const id = this.#normalizeId(meta.id || resolvedPath);
880
917
  this.metadata[id] = {
881
918
  type: "normal",
@@ -885,7 +922,7 @@ var VirtualFileSystem = class VirtualFileSystem {
885
922
  };
886
923
  this.paths[id] = resolvedPath;
887
924
  this.ids[resolvedPath] = id;
888
- return adapter.set(relativeKey, code);
925
+ return adapter.set(relativeKey, isString(code) ? code : uint8ArrayToString(toUint8Array(code)));
889
926
  }
890
927
  /**
891
928
  * Synchronously writes a file to the virtual file system (VFS).
@@ -898,7 +935,7 @@ var VirtualFileSystem = class VirtualFileSystem {
898
935
  const meta = options.meta ?? {};
899
936
  const resolvedPath = this.resolveSync(this.#normalizePath(path)) || path;
900
937
  const { relativeKey, adapter } = this.#getStorage(resolvedPath, options.storage);
901
- this.#logger.trace(`Writing ${resolvedPath} file to ${adapter.name === "virtual" ? "the virtual file system" : adapter.name === "file-system" ? "the local file system" : adapter.name} (size: ${prettyBytes(new Blob(toArray(data)).size)})`);
938
+ this.#logger.trace(`Writing ${resolvedPath} file to ${adapter.name === "virtual" ? "the virtual file system" : adapter.name === "file-system" ? "the local file system" : adapter.name} (size: ${prettyBytes(isString(data) ? new Blob(toArray(data)).size : data.byteLength)})`);
902
939
  const id = this.#normalizeId(meta.id || resolvedPath);
903
940
  this.metadata[id] = {
904
941
  type: "normal",
@@ -908,7 +945,7 @@ var VirtualFileSystem = class VirtualFileSystem {
908
945
  };
909
946
  this.paths[id] = resolvedPath;
910
947
  this.ids[resolvedPath] = id;
911
- return adapter.setSync(relativeKey, data);
948
+ return adapter.setSync(relativeKey, isString(data) ? data : uint8ArrayToString(toUint8Array(data)));
912
949
  }
913
950
  /**
914
951
  * Synchronously creates a directory at the specified path.