@rdfx/fs 0.0.34 → 0.0.36

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.
Files changed (43) hide show
  1. package/dist/CompressedRdfFormat.d.ts +7 -0
  2. package/dist/CompressedRdfFormat.js +2 -0
  3. package/dist/CompressedRdfStream.d.ts +15 -0
  4. package/dist/CompressedRdfStream.js +68 -0
  5. package/dist/CompressionMethod.d.ts +3 -0
  6. package/dist/CompressionMethod.js +2 -0
  7. package/dist/Dirent.d.ts +8 -0
  8. package/dist/Dirent.js +2 -0
  9. package/dist/ErrnoException.d.ts +8 -0
  10. package/dist/ErrnoException.js +4 -0
  11. package/dist/FileSystem.d.ts +93 -0
  12. package/dist/FileSystem.js +2 -0
  13. package/dist/NodeFileSystem.d.ts +30 -0
  14. package/dist/NodeFileSystem.js +42 -0
  15. package/dist/RdfDirectory.d.ts +12 -4
  16. package/dist/RdfDirectory.js +37 -50
  17. package/dist/RdfDirectoryGraphStore.d.ts +7 -3
  18. package/dist/RdfDirectoryGraphStore.js +58 -70
  19. package/dist/RdfFile.d.ts +19 -20
  20. package/dist/RdfFile.js +29 -104
  21. package/dist/RdfFileGraphStore.d.ts +9 -5
  22. package/dist/RdfFileGraphStore.js +35 -39
  23. package/dist/RdfFileSystemEntry.d.ts +3 -1
  24. package/dist/RdfFileSystemEntry.js +3 -2
  25. package/dist/RdfFormat.d.ts +8 -2
  26. package/dist/RdfFormat.js +47 -9
  27. package/dist/Stat.d.ts +9 -0
  28. package/dist/Stat.js +2 -0
  29. package/dist/UncompressedRdfFormat.d.ts +7 -0
  30. package/dist/UncompressedRdfFormat.js +2 -0
  31. package/dist/compressionMethods.d.ts +2 -0
  32. package/dist/compressionMethods.js +6 -0
  33. package/dist/index.d.ts +12 -1
  34. package/dist/index.js +12 -1
  35. package/dist/uncompressedRdfFormats.d.ts +3 -0
  36. package/dist/uncompressedRdfFormats.js +33 -0
  37. package/dist/uncompressedRdfFormatsByMimeType.d.ts +3 -0
  38. package/dist/uncompressedRdfFormatsByMimeType.js +6 -0
  39. package/package.json +7 -6
  40. package/dist/AbstractRdfFileSystemEntry.d.ts +0 -19
  41. package/dist/AbstractRdfFileSystemEntry.js +0 -11
  42. package/dist/stat.d.ts +0 -4
  43. package/dist/stat.js +0 -11
@@ -1,5 +1,4 @@
1
1
  import { Buffer } from "node:buffer";
2
- import fs from "node:fs";
3
2
  import path from "node:path";
4
3
  import datasetFactory from "@rdfjs/dataset";
5
4
  import dataFactory from "@rdfx/data-factory";
@@ -8,37 +7,45 @@ import parsers from "@rdfx/parsers";
8
7
  import { NTriplesTerm } from "@rdfx/string";
9
8
  import { Either, EitherAsync, Left, Maybe } from "purify-ts";
10
9
  import { dummyLogger } from "ts-log";
10
+ import { NodeFileSystem } from "./NodeFileSystem.js";
11
11
  import { RdfDirectory } from "./RdfDirectory.js";
12
- import { stat } from "./stat.js";
12
+ import { RdfFormat } from "./RdfFormat.js";
13
+ import { uncompressedRdfFormatsByMimeType } from "./uncompressedRdfFormatsByMimeType.js";
13
14
  /**
14
15
  * A GraphStore implementation backed by files in a directory.
15
16
  */
16
17
  export class RdfDirectoryGraphStore {
17
- directoryPath;
18
+ path;
19
+ fileSystem;
18
20
  logger;
19
- constructor(directoryPath, options) {
20
- this.directoryPath = directoryPath;
21
+ static fileFormat = uncompressedRdfFormatsByMimeType["application/n-triples"];
22
+ constructor(path, options) {
23
+ this.path = path;
24
+ this.fileSystem = options?.fileSystem ?? NodeFileSystem.instance;
21
25
  this.logger = options?.logger ?? dummyLogger;
22
26
  }
23
27
  async clear() {
24
- return EitherAsync(async () => {
25
- await fs.promises.rm(this.directoryPath, {
26
- force: true,
27
- recursive: true,
28
- });
28
+ return EitherAsync(async ({ liftEither }) => {
29
+ for (const dirent of await liftEither(await this.fileSystem.readDirectory(this.path, { recursive: true }))) {
30
+ if (!dirent.isFile()) {
31
+ continue;
32
+ }
33
+ const direntPath = path.join(dirent.parentPath, dirent.name);
34
+ const formatEither = RdfFormat.fromPath(direntPath);
35
+ if (formatEither.isLeft()) {
36
+ this.logger.debug("%s is not an RDF file, ignoring", direntPath);
37
+ continue;
38
+ }
39
+ await liftEither(await this.fileSystem.deleteFile(direntPath));
40
+ }
29
41
  return {};
30
42
  });
31
43
  }
32
44
  async delete(identifier) {
33
- return EitherAsync(async () => {
34
- try {
35
- await fs.promises.rm(this.graphFilePath(identifier));
36
- }
37
- catch (error) {
38
- if (errorCode(error) !== "ENOENT") {
39
- throw error;
40
- }
41
- }
45
+ return EitherAsync(async ({ liftEither }) => {
46
+ await liftEither(await this.fileSystem.deleteFile(this.graphFilePath(identifier), {
47
+ force: true,
48
+ }));
42
49
  return {};
43
50
  });
44
51
  }
@@ -47,17 +54,28 @@ export class RdfDirectoryGraphStore {
47
54
  if (!(await liftEither(await this.head(identifier)))) {
48
55
  return Maybe.empty();
49
56
  }
50
- let stream = parser.import(fs.createReadStream(this.graphFilePath(identifier)));
51
- if (identifier.termType !== "DefaultGraph") {
52
- stream = stream.map((quad) => dataFactory.quad(quad.subject, quad.predicate, quad.object, identifier));
53
- }
54
- return Maybe.of(stream);
57
+ const stream = parser.import(this.fileSystem.createReadStream(this.graphFilePath(identifier)));
58
+ return Maybe.of(identifier.termType === "DefaultGraph"
59
+ ? stream
60
+ : stream.map((quad) => dataFactory.quad(quad.subject, quad.predicate, quad.object, identifier)));
55
61
  });
56
62
  }
63
+ graphFilePath(identifier) {
64
+ const identifierString = typeof identifier === "object"
65
+ ? GraphIdentifier.stringify(identifier)
66
+ : identifier;
67
+ return path.join(this.path, identifierString.length === 0
68
+ ? "default.nq"
69
+ : `${Buffer.from(identifierString).toString("base64url")}.nq`);
70
+ }
57
71
  async head(identifier) {
58
- return (await EitherAsync(async ({ liftEither }) => (await liftEither(await stat(this.graphFilePath(identifier)))).isFile())).chainLeft((error) => errorCode(error) === "ENOENT"
59
- ? Either.of(false)
60
- : Left(error));
72
+ return EitherAsync(async ({ liftEither }) => {
73
+ const statEither = await this.fileSystem.stat(this.graphFilePath(identifier));
74
+ if (statEither.isLeft() && statEither.extract().code === "ENOENT") {
75
+ return false;
76
+ }
77
+ return (await liftEither(statEither)).isFile();
78
+ });
61
79
  }
62
80
  async identifiers() {
63
81
  return EitherAsync(async () => {
@@ -70,18 +88,10 @@ export class RdfDirectoryGraphStore {
70
88
  }
71
89
  async isEmpty() {
72
90
  return EitherAsync(async () => {
73
- try {
74
- for await (const _ of this.files()) {
75
- return false;
76
- }
77
- return true;
78
- }
79
- catch (error) {
80
- if (errorCode(error) === "ENOENT") {
81
- return true;
82
- }
83
- throw error;
91
+ for await (const _ of this.files()) {
92
+ return false;
84
93
  }
94
+ return true;
85
95
  });
86
96
  }
87
97
  async post(quads) {
@@ -92,7 +102,7 @@ export class RdfDirectoryGraphStore {
92
102
  }
93
103
  async unionDataset() {
94
104
  return EitherAsync(async ({ liftEither }) => {
95
- this.logger.debug("parsing dataset from %s", this.directoryPath);
105
+ this.logger.debug("parsing dataset from %s", this.path);
96
106
  const unionDataset = datasetFactory.dataset();
97
107
  for await (const [file, graphIdentifier] of this.files()) {
98
108
  const fileDataset = datasetFactory.dataset();
@@ -105,12 +115,12 @@ export class RdfDirectoryGraphStore {
105
115
  unionDataset.add(dataFactory.quad(quad.subject, quad.predicate, quad.object, graphIdentifier));
106
116
  }
107
117
  }
108
- this.logger.debug("parsed %d quads from %s", unionDataset.size, this.directoryPath);
118
+ this.logger.debug("parsed %d quads from %s", unionDataset.size, this.path);
109
119
  return unionDataset;
110
120
  });
111
121
  }
112
122
  async *files() {
113
- for await (const file of new RdfDirectory(this.directoryPath, {
123
+ for await (const file of new RdfDirectory(this.path, {
114
124
  logger: this.logger,
115
125
  }).files()) {
116
126
  const fileStem = path.basename(file.path, path.extname(file.path));
@@ -125,14 +135,6 @@ export class RdfDirectoryGraphStore {
125
135
  yield [file, graphIdentifier];
126
136
  }
127
137
  }
128
- graphFilePath(identifier) {
129
- const identifierString = typeof identifier === "object"
130
- ? GraphIdentifier.stringify(identifier)
131
- : identifier;
132
- return path.join(this.directoryPath, identifierString.length === 0
133
- ? "default.nq"
134
- : `${Buffer.from(identifierString).toString("base64url")}.nq`);
135
- }
136
138
  async postOrPut(method, quads) {
137
139
  return EitherAsync(async ({ liftEither }) => {
138
140
  const ntriplesByGraphIdentifier = new Map();
@@ -150,34 +152,20 @@ export class RdfDirectoryGraphStore {
150
152
  quads.on("error", (error) => resolve(Left(error)));
151
153
  }));
152
154
  for (let [graphIdentifierString, ntriples,] of ntriplesByGraphIdentifier.entries()) {
153
- await fs.promises.mkdir(this.directoryPath, {
154
- recursive: true,
155
- });
156
155
  const graphFilePath = this.graphFilePath(graphIdentifierString);
157
156
  if (method === "post") {
158
- try {
159
- ntriples = (await fs.promises.readFile(graphFilePath))
160
- .toString("utf-8")
161
- .split("\n")
162
- .concat(ntriples);
163
- }
164
- catch (error) {
165
- if (errorCode(error) !== "ENOENT") {
166
- throw error;
167
- }
168
- }
157
+ ntriples = (await liftEither((await this.fileSystem.readFile(graphFilePath))
158
+ .map((buffer) => new TextDecoder("utf-8").decode(buffer).split("\n"))
159
+ .chainLeft((error) => error.code === "ENOENT"
160
+ ? Either.of([])
161
+ : Left(error)))).concat(ntriples);
169
162
  }
170
163
  ntriples.sort();
171
- await fs.promises.writeFile(graphFilePath, ntriples.join("\n"), "utf-8");
164
+ await liftEither(await this.fileSystem.writeFile(graphFilePath, new TextEncoder().encode(ntriples.join("\n"))));
172
165
  }
173
166
  return {};
174
167
  });
175
168
  }
176
169
  }
177
- function errorCode(error) {
178
- return error instanceof Error && "code" in error
179
- ? error.code
180
- : undefined;
181
- }
182
- const parser = parsers({ dataFactory }).get("application/n-quads");
170
+ const parser = parsers({ dataFactory }).get(RdfDirectoryGraphStore.fileFormat.mimeType);
183
171
  //# sourceMappingURL=RdfDirectoryGraphStore.js.map
package/dist/RdfFile.d.ts CHANGED
@@ -1,30 +1,29 @@
1
1
  import type PrefixMap from "@rdfjs/prefix-map/PrefixMap.js";
2
- import type { DatasetCore, Quad, Stream } from "@rdfjs/types";
3
- import serializers from "@rdfx/serializers";
4
- import { Either, type Maybe } from "purify-ts";
5
- import type { Logger } from "ts-log";
6
- import { AbstractRdfFileSystemEntry } from "./AbstractRdfFileSystemEntry.js";
7
- import { type RdfFormat } from "./RdfFormat.js";
8
- export declare class RdfFile extends AbstractRdfFileSystemEntry {
9
- readonly format: RdfFile.Format;
10
- constructor(path: string, { format, ...superParameters }: {
11
- format: RdfFile.Format;
12
- } & ConstructorParameters<typeof AbstractRdfFileSystemEntry>[1]);
2
+ import type { DatasetCore, Stream } from "@rdfjs/types";
3
+ import type serializers from "@rdfx/serializers";
4
+ import { Either } from "purify-ts";
5
+ import { type Logger } from "ts-log";
6
+ import type { FileSystem } from "./FileSystem.js";
7
+ import { RdfFormat } from "./RdfFormat.js";
8
+ export declare class RdfFile {
9
+ readonly path: string;
10
+ readonly format: RdfFormat;
11
+ private readonly fileSystem;
12
+ private readonly logger;
13
+ constructor(path: string, options?: {
14
+ fileSystem?: FileSystem;
15
+ format?: RdfFormat;
16
+ logger?: Logger;
17
+ });
13
18
  static fromPath(filePath: string, options?: {
19
+ fileSystem?: FileSystem;
14
20
  logger?: Logger;
15
21
  }): Either<Error, RdfFile>;
16
- parse(): Stream<Quad>;
22
+ parse(): Stream;
17
23
  parseInto(dataset: DatasetCore, options?: {
18
24
  prefixMap?: PrefixMap;
19
25
  }): Promise<Either<Error, DatasetCore>>;
20
26
  serialize(quads: Stream, options?: Parameters<typeof serializers>[0]): Promise<Either<Error, void>>;
21
27
  }
22
- export declare namespace RdfFile {
23
- const COMPRESSION_METHODS: readonly ["application/gzip", "application/x-brotli", "application/x-bzip2"];
24
- type CompressionMethod = (typeof COMPRESSION_METHODS)[number];
25
- interface Format {
26
- compressionMethod: Maybe<CompressionMethod>;
27
- rdfFormat: RdfFormat;
28
- }
29
- }
28
+ export declare namespace RdfFile { }
30
29
  //# sourceMappingURL=RdfFile.d.ts.map
package/dist/RdfFile.js CHANGED
@@ -1,81 +1,31 @@
1
- import fs from "node:fs";
2
- import * as path from "node:path";
3
- import { pipeline } from "node:stream/promises";
4
- import zlib from "node:zlib";
5
- import dataFactory from "@rdfx/data-factory";
6
- import parsersFactory from "@rdfx/parsers";
7
- import serializers from "@rdfx/serializers";
8
- import { Mime } from "mime";
9
- import otherMimeTypes from "mime/types/other.js";
10
- import standardMimeTypes from "mime/types/standard.js";
11
- import { Either, EitherAsync, Just, Left, Nothing, Right, } from "purify-ts";
12
- import bz2 from "unbzip2-stream";
13
- import { AbstractRdfFileSystemEntry } from "./AbstractRdfFileSystemEntry.js";
14
- import { RDF_FORMATS } from "./RdfFormat.js";
15
- const parsers = parsersFactory({ dataFactory });
16
- const mime = new Mime(standardMimeTypes, otherMimeTypes, {
17
- "application/x-brotli": ["br"],
18
- });
19
- export class RdfFile extends AbstractRdfFileSystemEntry {
1
+ import { Either, EitherAsync, Left } from "purify-ts";
2
+ import { dummyLogger } from "ts-log";
3
+ import { CompressedRdfStream } from "./CompressedRdfStream.js";
4
+ import { NodeFileSystem } from "./NodeFileSystem.js";
5
+ import { RdfFormat } from "./RdfFormat.js";
6
+ import { uncompressedRdfFormatsByMimeType } from "./uncompressedRdfFormatsByMimeType.js";
7
+ export class RdfFile {
8
+ path;
20
9
  format;
21
- constructor(path, { format, ...superParameters }) {
22
- super(path, superParameters);
23
- this.format = format;
10
+ fileSystem;
11
+ logger;
12
+ constructor(path, options) {
13
+ this.path = path;
14
+ this.fileSystem = options?.fileSystem ?? NodeFileSystem.instance;
15
+ this.format =
16
+ options?.format ??
17
+ uncompressedRdfFormatsByMimeType["application/n-quads"];
18
+ this.logger = options?.logger ?? dummyLogger;
24
19
  }
25
20
  static fromPath(filePath, options) {
26
- const mimeType = mime.getType(filePath);
27
- if (mimeType === null) {
28
- return Left(new Error(`unable to infer MIME type of ${filePath}`));
29
- }
30
- for (const compressionMethod of RdfFile.COMPRESSION_METHODS) {
31
- if (compressionMethod === mimeType) {
32
- const uncompressedFileName = path.basename(path.basename(filePath), path.extname(filePath));
33
- const uncompressedMimeType = mime.getType(uncompressedFileName);
34
- if (uncompressedMimeType === null) {
35
- return Left(new Error(`unable to infer MIME type of ${uncompressedFileName}`));
36
- }
37
- for (const rdfFormat of RDF_FORMATS) {
38
- if (uncompressedMimeType === rdfFormat) {
39
- return Right(new RdfFile(filePath, {
40
- format: {
41
- compressionMethod: Just(compressionMethod),
42
- rdfFormat,
43
- },
44
- logger: options?.logger,
45
- }));
46
- }
47
- }
48
- }
49
- }
50
- for (const rdfFormat of RDF_FORMATS) {
51
- if (mimeType === rdfFormat) {
52
- return Right(new RdfFile(filePath, {
53
- format: {
54
- compressionMethod: Nothing,
55
- rdfFormat,
56
- },
57
- logger: options?.logger,
58
- }));
59
- }
60
- }
61
- return Left(new Error(`${filePath} has a non-RDF MIME type: ${mimeType}`));
21
+ return RdfFormat.fromPath(filePath).map((format) => new RdfFile(filePath, {
22
+ fileSystem: options?.fileSystem,
23
+ format,
24
+ logger: options?.logger,
25
+ }));
62
26
  }
63
27
  parse() {
64
- let fileStream = fs.createReadStream(this.path);
65
- if (this.format.compressionMethod.isJust()) {
66
- switch (this.format.compressionMethod.unsafeCoerce()) {
67
- case "application/gzip":
68
- fileStream = fileStream.pipe(zlib.createGunzip());
69
- break;
70
- case "application/x-brotli":
71
- fileStream = fileStream.pipe(zlib.createBrotliDecompress());
72
- break;
73
- case "application/x-bzip2":
74
- fileStream = fileStream.pipe(bz2());
75
- break;
76
- }
77
- }
78
- return parsers.import(this.format.rdfFormat, fileStream);
28
+ return CompressedRdfStream.parse(this.format, this.fileSystem.createReadStream(this.path));
79
29
  }
80
30
  parseInto(dataset, options) {
81
31
  return new Promise((resolve) => {
@@ -104,37 +54,12 @@ export class RdfFile extends AbstractRdfFileSystemEntry {
104
54
  });
105
55
  }
106
56
  async serialize(quads, options) {
107
- return EitherAsync(async () => {
108
- const rdfStream = serializers(options).import(this.format.rdfFormat, quads);
109
- if (rdfStream === null) {
110
- throw new RangeError(`unsupported RDF serialization format: ${this.format.rdfFormat}`);
111
- }
112
- const fileStream = fs.createWriteStream(this.path);
113
- if (this.format.compressionMethod.isJust()) {
114
- let compressor;
115
- switch (this.format.compressionMethod.extract()) {
116
- case "application/gzip":
117
- compressor = zlib.createGzip();
118
- break;
119
- case "application/x-brotli":
120
- compressor = zlib.createBrotliCompress();
121
- break;
122
- case "application/x-bzip2":
123
- throw new RangeError("bzip2 compression unsupported");
124
- }
125
- await pipeline(rdfStream, compressor, fileStream);
126
- }
127
- else {
128
- await pipeline(rdfStream, fileStream);
129
- }
130
- });
57
+ return EitherAsync(async ({ liftEither }) => await liftEither(await this.fileSystem.writeFileStream(this.path, (fileStream) => CompressedRdfStream.serialize({
58
+ format: this.format,
59
+ source: quads,
60
+ destination: fileStream,
61
+ serializerOptions: options,
62
+ }))));
131
63
  }
132
64
  }
133
- (function (RdfFile) {
134
- RdfFile.COMPRESSION_METHODS = [
135
- "application/gzip",
136
- "application/x-brotli",
137
- "application/x-bzip2",
138
- ];
139
- })(RdfFile || (RdfFile = {}));
140
65
  //# sourceMappingURL=RdfFile.js.map
@@ -3,20 +3,24 @@ import type { DatasetCore, Stream } from "@rdfjs/types";
3
3
  import { type GraphIdentifier, type GraphStore } from "@rdfx/graph-store";
4
4
  import { Either, type Maybe } from "purify-ts";
5
5
  import { type Logger } from "ts-log";
6
- import { RdfFile } from "./RdfFile.js";
6
+ import type { FileSystem } from "./FileSystem.js";
7
+ import { RdfFormat } from "./RdfFormat.js";
7
8
  /**
8
9
  * A GraphStore implementation backed by a single RdfFile.
9
10
  */
10
11
  export declare class RdfFileGraphStore implements GraphStore {
11
- readonly filePath: string;
12
- private readonly file;
12
+ readonly path: string;
13
+ private readonly fileSystem;
13
14
  private readonly logger;
14
15
  private readonly prefixMap?;
15
- constructor(filePath: string, options?: {
16
- format?: RdfFile.Format;
16
+ readonly format: RdfFormat;
17
+ constructor(path: string, options?: {
18
+ fileSystem?: FileSystem;
19
+ format?: RdfFormat;
17
20
  logger?: Logger;
18
21
  prefixMap?: PrefixMap;
19
22
  });
23
+ private get rdfFile();
20
24
  clear(): Promise<Either<Error, object>>;
21
25
  delete(identifier: GraphIdentifier): Promise<Either<Error, object>>;
22
26
  get(identifier: GraphIdentifier): Promise<Either<Error, Maybe<Stream>>>;
@@ -1,39 +1,45 @@
1
- import fs from "node:fs/promises";
2
- import path from "node:path";
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
3
7
  import datasetFactory from "@rdfjs/dataset";
4
8
  import { RdfjsDatasetGraphStore, } from "@rdfx/graph-store";
5
9
  import intoStream from "into-stream";
6
10
  import { Either, EitherAsync, Left } from "purify-ts";
7
11
  import { dummyLogger } from "ts-log";
12
+ import { Memoize } from "typescript-memoize";
13
+ import { isErrnoException } from "./ErrnoException.js";
14
+ import { NodeFileSystem } from "./NodeFileSystem.js";
8
15
  import { RdfFile } from "./RdfFile.js";
16
+ import { RdfFormat } from "./RdfFormat.js";
9
17
  /**
10
18
  * A GraphStore implementation backed by a single RdfFile.
11
19
  */
12
20
  export class RdfFileGraphStore {
13
- filePath;
14
- file;
21
+ path;
22
+ fileSystem;
15
23
  logger;
16
24
  prefixMap;
17
- constructor(filePath, options) {
18
- this.filePath = filePath;
25
+ format;
26
+ constructor(path, options) {
27
+ this.path = path;
28
+ this.fileSystem = options?.fileSystem ?? NodeFileSystem.instance;
29
+ this.format = options?.format ?? RdfFormat.fromPath(path).unsafeCoerce();
19
30
  this.logger = options?.logger ?? dummyLogger;
20
- this.file = options?.format
21
- ? new RdfFile(this.filePath, { format: options.format })
22
- : RdfFile.fromPath(this.filePath, {
23
- logger: this.logger,
24
- }).unsafeCoerce();
25
31
  this.prefixMap = options?.prefixMap;
26
32
  }
33
+ get rdfFile() {
34
+ return new RdfFile(this.path, {
35
+ fileSystem: this.fileSystem,
36
+ format: this.format,
37
+ logger: this.logger,
38
+ });
39
+ }
27
40
  async clear() {
28
- return EitherAsync(async () => {
29
- try {
30
- await fs.unlink(this.filePath);
31
- }
32
- catch (error) {
33
- if (errorCode(error) !== "ENOENT") {
34
- throw error;
35
- }
36
- }
41
+ return EitherAsync(async ({ liftEither }) => {
42
+ await liftEither(await this.fileSystem.deleteFile(this.path, { force: true }));
37
43
  return {};
38
44
  });
39
45
  }
@@ -60,12 +66,12 @@ export class RdfFileGraphStore {
60
66
  }
61
67
  async unionDataset() {
62
68
  return (await EitherAsync(async ({ liftEither }) => {
63
- this.logger.debug("parsing dataset from %s", this.filePath);
64
- const dataset = await liftEither(await this.file.parseInto(datasetFactory.dataset()));
65
- this.logger.debug("parsed %d quads from %d", dataset.size, this.filePath);
69
+ this.logger.debug("parsing dataset from %s", this.path);
70
+ const dataset = await liftEither(await this.rdfFile.parseInto(datasetFactory.dataset()));
71
+ this.logger.debug("parsed %d quads from %d", dataset.size, this.path);
66
72
  return dataset;
67
73
  })).chainLeft((error) => {
68
- if (errorCode(error) === "ENOENT") {
74
+ if (isErrnoException(error) && error.code === "ENOENT") {
69
75
  return Either.of(datasetFactory.dataset());
70
76
  }
71
77
  else {
@@ -78,18 +84,10 @@ export class RdfFileGraphStore {
78
84
  const unionDataset = await liftEither(await this.unionDataset());
79
85
  const return_ = await liftEither(await mutator(new RdfjsDatasetGraphStore(unionDataset)));
80
86
  if (unionDataset.size === 0) {
81
- try {
82
- await fs.unlink(this.filePath);
83
- }
84
- catch (error) {
85
- if (errorCode(error) !== "ENOENT") {
86
- throw error;
87
- }
88
- }
87
+ await liftEither(await this.fileSystem.deleteFile(this.path, { force: true }));
89
88
  return return_;
90
89
  }
91
- await fs.mkdir(path.dirname(this.filePath), { recursive: true });
92
- await liftEither(await this.file.serialize(intoStream.object(unionDataset), {
90
+ await liftEither(await this.rdfFile.serialize(intoStream.object(unionDataset), {
93
91
  prefixes: this.prefixMap,
94
92
  }));
95
93
  return return_;
@@ -99,9 +97,7 @@ export class RdfFileGraphStore {
99
97
  return (await this.unionDataset()).map((dataset) => new RdfjsDatasetGraphStore(dataset));
100
98
  }
101
99
  }
102
- function errorCode(error) {
103
- return error instanceof Error && "code" in error
104
- ? error.code
105
- : undefined;
106
- }
100
+ __decorate([
101
+ Memoize()
102
+ ], RdfFileGraphStore.prototype, "rdfFile", null);
107
103
  //# sourceMappingURL=RdfFileGraphStore.js.map
@@ -1,11 +1,13 @@
1
1
  import { type Either } from "purify-ts";
2
2
  import type { Logger } from "ts-log";
3
+ import type { FileSystem } from "./FileSystem.js";
3
4
  import { RdfDirectory } from "./RdfDirectory.js";
4
5
  import { RdfFile } from "./RdfFile.js";
5
6
  export type RdfFileSystemEntry = RdfDirectory | RdfFile;
6
7
  export declare namespace RdfFileSystemEntry {
7
8
  function fromPath(path: string, options?: {
8
- logger: Logger;
9
+ fileSystem?: FileSystem;
10
+ logger?: Logger;
9
11
  }): Promise<Either<Error, RdfFileSystemEntry>>;
10
12
  }
11
13
  //# sourceMappingURL=RdfFileSystemEntry.d.ts.map
@@ -1,12 +1,13 @@
1
1
  import { EitherAsync } from "purify-ts";
2
+ import { NodeFileSystem } from "./NodeFileSystem.js";
2
3
  import { RdfDirectory } from "./RdfDirectory.js";
3
4
  import { RdfFile } from "./RdfFile.js";
4
- import { stat } from "./stat.js";
5
5
  export var RdfFileSystemEntry;
6
6
  (function (RdfFileSystemEntry) {
7
7
  async function fromPath(path, options) {
8
+ const fileSystem = options?.fileSystem ?? NodeFileSystem.instance;
8
9
  return EitherAsync(async ({ liftEither }) => {
9
- const stat_ = await liftEither(await stat(path));
10
+ const stat_ = await liftEither(await fileSystem.stat(path));
10
11
  if (stat_.isDirectory())
11
12
  return new RdfDirectory(path, { logger: options?.logger });
12
13
  if (stat_.isFile()) {
@@ -1,3 +1,9 @@
1
- export declare const RDF_FORMATS: readonly ["application/ld+json", "application/n-quads", "application/n-triples", "application/rdf+xml", "application/trig", "text/n3", "text/turtle"];
2
- export type RdfFormat = (typeof RDF_FORMATS)[number];
1
+ import { type Either } from "purify-ts";
2
+ import type { CompressedRdfFormat } from "./CompressedRdfFormat.js";
3
+ import type { UncompressedRdfFormat } from "./UncompressedRdfFormat.js";
4
+ export type RdfFormat = CompressedRdfFormat | UncompressedRdfFormat;
5
+ export declare namespace RdfFormat {
6
+ function fromPath(filePath: string): Either<Error, RdfFormat>;
7
+ function isCompressed(rdfFormat: RdfFormat): rdfFormat is CompressedRdfFormat;
8
+ }
3
9
  //# sourceMappingURL=RdfFormat.d.ts.map
package/dist/RdfFormat.js CHANGED
@@ -1,10 +1,48 @@
1
- export const RDF_FORMATS = [
2
- "application/ld+json",
3
- "application/n-quads",
4
- "application/n-triples",
5
- "application/rdf+xml",
6
- "application/trig",
7
- "text/n3",
8
- "text/turtle",
9
- ];
1
+ import path from "node:path";
2
+ import { Mime } from "mime";
3
+ import otherMimeTypes from "mime/types/other.js";
4
+ import standardMimeTypes from "mime/types/standard.js";
5
+ import { Left, Right } from "purify-ts";
6
+ import { compressionMethods } from "./compressionMethods.js";
7
+ import { uncompressedRdfFormatsByMimeType } from "./uncompressedRdfFormatsByMimeType.js";
8
+ const compressionMethodsSet = new Set(compressionMethods);
9
+ const mime = new Mime(standardMimeTypes, otherMimeTypes, {
10
+ "application/x-brotli": ["br"],
11
+ });
12
+ export var RdfFormat;
13
+ (function (RdfFormat) {
14
+ function fromPath(filePath) {
15
+ const mimeType = mime.getType(filePath);
16
+ if (mimeType === null) {
17
+ return Left(new Error(`unable to infer MIME type of ${filePath}`));
18
+ }
19
+ if (compressionMethodsSet.has(mimeType)) {
20
+ const uncompressedFileName = path.basename(path.basename(filePath), path.extname(filePath));
21
+ const uncompressedMimeType = mime.getType(uncompressedFileName);
22
+ if (uncompressedMimeType === null) {
23
+ return Left(new Error(`unable to infer MIME type of ${uncompressedFileName}`));
24
+ }
25
+ const uncompressedRdfFormat = uncompressedRdfFormatsByMimeType[uncompressedMimeType];
26
+ if (uncompressedRdfFormat === undefined) {
27
+ return Left(new Error(`${filePath} has a non-RDF MIME type: ${uncompressedMimeType}`));
28
+ }
29
+ return Right({
30
+ lineOriented: uncompressedRdfFormat.lineOriented,
31
+ mimeType: mimeType,
32
+ supportsQuads: uncompressedRdfFormat.supportsQuads,
33
+ uncompressedMimeType: uncompressedRdfFormat.mimeType,
34
+ });
35
+ }
36
+ const uncompressedRdfFormat = uncompressedRdfFormatsByMimeType[mimeType];
37
+ if (uncompressedRdfFormat === undefined) {
38
+ return Left(new Error(`${filePath} has a non-RDF MIME type: ${mimeType}`));
39
+ }
40
+ return Right(uncompressedRdfFormat);
41
+ }
42
+ RdfFormat.fromPath = fromPath;
43
+ function isCompressed(rdfFormat) {
44
+ return compressionMethods.some((compressionMethod) => compressionMethod === rdfFormat.mimeType);
45
+ }
46
+ RdfFormat.isCompressed = isCompressed;
47
+ })(RdfFormat || (RdfFormat = {}));
10
48
  //# sourceMappingURL=RdfFormat.js.map