@rdfx/fs 0.0.22 → 0.0.24

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/RdfFile.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type PrefixMap from "@rdfjs/prefix-map/PrefixMap.js";
2
2
  import type { DatasetCore, Quad, Stream } from "@rdfjs/types";
3
+ import serializers from "@rdfx/serializers";
3
4
  import { Either, type Maybe } from "purify-ts";
4
5
  import type { Logger } from "ts-log";
5
6
  import { AbstractRdfFileSystemEntry } from "./AbstractRdfFileSystemEntry.js";
@@ -16,6 +17,7 @@ export declare class RdfFile extends AbstractRdfFileSystemEntry {
16
17
  parseInto(dataset: DatasetCore, options?: {
17
18
  prefixMap?: PrefixMap;
18
19
  }): Promise<Either<Error, DatasetCore>>;
20
+ serialize(quads: Iterable<Quad>, options?: Parameters<typeof serializers>[0]): Promise<Either<Error, void>>;
19
21
  }
20
22
  export declare namespace RdfFile {
21
23
  const COMPRESSION_METHODS: readonly ["application/gzip", "application/x-brotli", "application/x-bzip2"];
package/dist/RdfFile.js CHANGED
@@ -1,12 +1,15 @@
1
1
  import fs from "node:fs";
2
2
  import * as path from "node:path";
3
+ import { Readable } from "node:stream";
4
+ import { pipeline } from "node:stream/promises";
3
5
  import zlib from "node:zlib";
4
6
  import dataFactory from "@rdfx/data-factory";
5
7
  import parsersFactory from "@rdfx/parsers";
8
+ import serializers from "@rdfx/serializers";
6
9
  import { Mime } from "mime";
7
10
  import otherMimeTypes from "mime/types/other.js";
8
11
  import standardMimeTypes from "mime/types/standard.js";
9
- import { Either, Just, Left, Nothing, Right } from "purify-ts";
12
+ import { Either, EitherAsync, Just, Left, Nothing, Right, } from "purify-ts";
10
13
  import bz2 from "unbzip2-stream";
11
14
  import { AbstractRdfFileSystemEntry } from "./AbstractRdfFileSystemEntry.js";
12
15
  import { RDF_FORMATS } from "./RdfFormat.js";
@@ -59,21 +62,21 @@ export class RdfFile extends AbstractRdfFileSystemEntry {
59
62
  return Left(new Error(`${filePath} has a non-RDF MIME type: ${mimeType}`));
60
63
  }
61
64
  parse() {
62
- let rdfFileStream = fs.createReadStream(this.path);
65
+ let fileStream = fs.createReadStream(this.path);
63
66
  if (this.format.compressionMethod.isJust()) {
64
67
  switch (this.format.compressionMethod.unsafeCoerce()) {
65
68
  case "application/gzip":
66
- rdfFileStream = rdfFileStream.pipe(zlib.createGunzip());
69
+ fileStream = fileStream.pipe(zlib.createGunzip());
67
70
  break;
68
71
  case "application/x-brotli":
69
- rdfFileStream = rdfFileStream.pipe(zlib.createBrotliDecompress());
72
+ fileStream = fileStream.pipe(zlib.createBrotliDecompress());
70
73
  break;
71
74
  case "application/x-bzip2":
72
- rdfFileStream = rdfFileStream.pipe(bz2());
75
+ fileStream = fileStream.pipe(bz2());
73
76
  break;
74
77
  }
75
78
  }
76
- return parsers.import(this.format.rdfFormat, rdfFileStream);
79
+ return parsers.import(this.format.rdfFormat, fileStream);
77
80
  }
78
81
  parseInto(dataset, options) {
79
82
  return new Promise((resolve) => {
@@ -101,6 +104,32 @@ export class RdfFile extends AbstractRdfFileSystemEntry {
101
104
  stream.on("error", (error) => resolve(Left(error)));
102
105
  });
103
106
  }
107
+ async serialize(quads, options) {
108
+ return EitherAsync(async () => {
109
+ const rdfStream = serializers(options).import(this.format.rdfFormat, Readable.from(quads));
110
+ if (rdfStream === null) {
111
+ throw new RangeError(`unsupported RDF serialization format: ${this.format.rdfFormat}`);
112
+ }
113
+ const fileStream = fs.createWriteStream(this.path);
114
+ if (this.format.compressionMethod.isJust()) {
115
+ let compressor;
116
+ switch (this.format.compressionMethod.extract()) {
117
+ case "application/gzip":
118
+ compressor = zlib.createGzip();
119
+ break;
120
+ case "application/x-brotli":
121
+ compressor = zlib.createBrotliCompress();
122
+ break;
123
+ case "application/x-bzip2":
124
+ throw new RangeError("bzip2 compression unsupported");
125
+ }
126
+ await pipeline(rdfStream, compressor, fileStream);
127
+ }
128
+ else {
129
+ await pipeline(rdfStream, fileStream);
130
+ }
131
+ });
132
+ }
104
133
  }
105
134
  (function (RdfFile) {
106
135
  RdfFile.COMPRESSION_METHODS = [
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "dependencies": {
3
- "@rdfx/data-factory": "0.0.22",
4
- "@rdfx/parsers": "0.0.22",
3
+ "@rdfx/data-factory": "0.0.24",
4
+ "@rdfx/parsers": "0.0.24",
5
+ "@rdfx/serializers": "0.0.24",
5
6
  "@rdfjs/types": "~2.0.1",
6
7
  "@types/unbzip2-stream": "~1.4.3",
7
8
  "mime": "~4.1.0",
@@ -36,5 +37,5 @@
36
37
  },
37
38
  "type": "module",
38
39
  "types": "./dist/index.d.ts",
39
- "version": "0.0.22"
40
+ "version": "0.0.24"
40
41
  }