@rdfx/serializer 0.0.51
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/README.md +5 -0
- package/dist/N3Serializer.d.ts +15 -0
- package/dist/N3Serializer.js +24 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/parseSync.d.ts +10 -0
- package/dist/parseSync.js +9 -0
- package/dist/serialize.d.ts +25 -0
- package/dist/serialize.js +35 -0
- package/dist/serializeSync.d.ts +7 -0
- package/dist/serializeSync.js +13 -0
- package/dist/serializers.d.ts +16 -0
- package/dist/serializers.js +42 -0
- package/package.json +50 -0
package/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type PrefixMap from "@rdfjs/prefix-map/PrefixMap.js";
|
|
2
|
+
import type { Sink, Stream } from "@rdfjs/types";
|
|
3
|
+
import { type MimeFormat } from "n3";
|
|
4
|
+
export type SerializerOptions = {
|
|
5
|
+
prefixes?: PrefixMap;
|
|
6
|
+
end?: boolean;
|
|
7
|
+
};
|
|
8
|
+
export default class N3Serializer implements Sink<Stream, Stream> {
|
|
9
|
+
private readonly options;
|
|
10
|
+
constructor(options: SerializerOptions & {
|
|
11
|
+
format: MimeFormat;
|
|
12
|
+
});
|
|
13
|
+
import(input: Stream, options?: SerializerOptions): Stream;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=N3Serializer.d.ts.map
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { StreamWriter } from "n3";
|
|
2
|
+
export default class N3Serializer {
|
|
3
|
+
options;
|
|
4
|
+
constructor(options) {
|
|
5
|
+
this.options = options;
|
|
6
|
+
}
|
|
7
|
+
import(input, options) {
|
|
8
|
+
options = { ...this.options, ...options };
|
|
9
|
+
const { prefixes: prefixMap, ...otherOptions } = options;
|
|
10
|
+
let prefixes;
|
|
11
|
+
if (prefixMap) {
|
|
12
|
+
// N3 expects a Record<string, NamedNode> and PrefixMap is a Map<string, NamedNode>
|
|
13
|
+
prefixes = {};
|
|
14
|
+
for (const entry of prefixMap.entries()) {
|
|
15
|
+
prefixes[entry[0]] = entry[1];
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
const writer = new StreamWriter({ ...otherOptions, prefixes });
|
|
19
|
+
input.on("error", (err) => writer.emit("error", err));
|
|
20
|
+
input.pipe(writer);
|
|
21
|
+
return writer;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=N3Serializer.js.map
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { DataFactory, Quad } from "@rdfjs/types";
|
|
2
|
+
import type { UncompressedRdfFormat } from "@rdfx/format";
|
|
3
|
+
import { type ParserOptions as N3ParserOptions, type PrefixCallback } from "n3";
|
|
4
|
+
import { Either } from "purify-ts";
|
|
5
|
+
export declare function parseSync(input: string, { dataFactory, prefixCallback, ...otherOptions }: {
|
|
6
|
+
dataFactory: DataFactory;
|
|
7
|
+
format?: Exclude<UncompressedRdfFormat["mimeType"], "application/ld+json">;
|
|
8
|
+
prefixCallback?: PrefixCallback;
|
|
9
|
+
} & Omit<N3ParserOptions, "factory" | "format">): Either<Error, readonly Quad[]>;
|
|
10
|
+
//# sourceMappingURL=parseSync.d.ts.map
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Parser as N3Parser, } from "n3";
|
|
2
|
+
import { Either } from "purify-ts";
|
|
3
|
+
export function parseSync(input, { dataFactory, prefixCallback, ...otherOptions }) {
|
|
4
|
+
return Either.encase(() => {
|
|
5
|
+
const n3Parser = new N3Parser({ ...otherOptions, factory: dataFactory });
|
|
6
|
+
return n3Parser.parse(input, undefined, prefixCallback);
|
|
7
|
+
});
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=parseSync.js.map
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { SerializerOptions as JsonLdSerializerOptions } from "@rdfjs/serializer-jsonld-ext";
|
|
2
|
+
import type { SerializerOptions as RdfjsSerializerOptions } from "@rdfjs/serializer-rdfjs";
|
|
3
|
+
import type { SerializerOptions as TurtleSerializerOptions } from "@rdfjs/serializer-turtle";
|
|
4
|
+
import type { Quad } from "@rdfjs/types";
|
|
5
|
+
import type { SerializerOptions as N3SerializerOptions } from "./N3Serializer.js";
|
|
6
|
+
type SerializeOptions = ({
|
|
7
|
+
format: "application/ld+json";
|
|
8
|
+
} & JsonLdSerializerOptions) | {
|
|
9
|
+
format: "";
|
|
10
|
+
} | {
|
|
11
|
+
format: "application/n-quads";
|
|
12
|
+
} | {
|
|
13
|
+
format: "application/n-triples";
|
|
14
|
+
} | ({
|
|
15
|
+
format: "application/trig";
|
|
16
|
+
} & N3SerializerOptions) | ({
|
|
17
|
+
format: "text/javascript";
|
|
18
|
+
} & RdfjsSerializerOptions) | ({
|
|
19
|
+
format: "text/n3";
|
|
20
|
+
} & N3SerializerOptions) | ({
|
|
21
|
+
format: "text/turtle";
|
|
22
|
+
} & TurtleSerializerOptions);
|
|
23
|
+
export declare function serialize(input: Iterable<Quad>, options: SerializeOptions): Promise<import("purify-ts").Either<unknown, string>>;
|
|
24
|
+
export {};
|
|
25
|
+
//# sourceMappingURL=serialize.d.ts.map
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { iterableToStream, streamToString } from "@rdfx/stream";
|
|
2
|
+
import { EitherAsync } from "purify-ts";
|
|
3
|
+
import { serializers as serializersFactory } from "./serializers.js";
|
|
4
|
+
export async function serialize(input, options) {
|
|
5
|
+
return EitherAsync(async ({ liftEither }) => {
|
|
6
|
+
const serializersOptions = {};
|
|
7
|
+
switch (options.format) {
|
|
8
|
+
case "application/ld+json":
|
|
9
|
+
serializersOptions.jsonLd = options;
|
|
10
|
+
break;
|
|
11
|
+
case "application/n-quads":
|
|
12
|
+
case "application/n-triples":
|
|
13
|
+
break;
|
|
14
|
+
case "application/trig":
|
|
15
|
+
serializersOptions.trig = options;
|
|
16
|
+
break;
|
|
17
|
+
case "text/javascript":
|
|
18
|
+
serializersOptions.rdfjs = options;
|
|
19
|
+
break;
|
|
20
|
+
case "text/n3":
|
|
21
|
+
serializersOptions.n3 = options;
|
|
22
|
+
break;
|
|
23
|
+
case "text/turtle":
|
|
24
|
+
serializersOptions.turtle = options;
|
|
25
|
+
break;
|
|
26
|
+
}
|
|
27
|
+
const serializers = serializersFactory(serializersOptions);
|
|
28
|
+
const stream = serializers.import(options.format, iterableToStream(input));
|
|
29
|
+
if (stream === null) {
|
|
30
|
+
throw new RangeError(`unsupported format: ${options.format}`);
|
|
31
|
+
}
|
|
32
|
+
return await liftEither(await streamToString(stream));
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=serialize.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Quad } from "@rdfjs/types";
|
|
2
|
+
import type { UncompressedRdfFormat } from "@rdfx/format";
|
|
3
|
+
import { Either } from "purify-ts";
|
|
4
|
+
export declare function serializeSync(input: Iterable<Quad>, options: {
|
|
5
|
+
format: Extract<UncompressedRdfFormat["mimeType"], "application/n-quads" | "application/n-triples">;
|
|
6
|
+
}): Either<Error, string>;
|
|
7
|
+
//# sourceMappingURL=serializeSync.d.ts.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Writer as N3Writer } from "n3";
|
|
2
|
+
import { Either } from "purify-ts";
|
|
3
|
+
export function serializeSync(input, options) {
|
|
4
|
+
return Either.encase(() => {
|
|
5
|
+
const ntriples = [];
|
|
6
|
+
const n3Writer = new N3Writer(options);
|
|
7
|
+
for (const quad of input) {
|
|
8
|
+
ntriples.push(n3Writer.quadToString(quad.subject, quad.predicate, quad.object, quad.graph));
|
|
9
|
+
}
|
|
10
|
+
return ntriples.join("\n");
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=serializeSync.js.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type PrefixMap from "@rdfjs/prefix-map/PrefixMap.js";
|
|
2
|
+
import { type SerializerOptions as JsonLdSerializerOptions } from "@rdfjs/serializer-jsonld-ext";
|
|
3
|
+
import { type SerializerOptions as RdfjsSerializerOptions } from "@rdfjs/serializer-rdfjs";
|
|
4
|
+
import { type SerializerOptions as TurtleSerializerOptions } from "@rdfjs/serializer-turtle";
|
|
5
|
+
import SinkMap from "@rdfjs/sink-map";
|
|
6
|
+
import { type SerializerOptions as N3SerializerOptions } from "./N3Serializer.js";
|
|
7
|
+
export declare function serializers(options?: {
|
|
8
|
+
baseIRI?: string;
|
|
9
|
+
prefixes?: PrefixMap;
|
|
10
|
+
jsonLd?: JsonLdSerializerOptions;
|
|
11
|
+
n3?: N3SerializerOptions;
|
|
12
|
+
rdfjs?: RdfjsSerializerOptions;
|
|
13
|
+
trig?: N3SerializerOptions;
|
|
14
|
+
turtle?: TurtleSerializerOptions;
|
|
15
|
+
}): SinkMap<import("@rdfjs/types").Stream<import("@rdfjs/types").Quad>, import("events")<[never]>>;
|
|
16
|
+
//# sourceMappingURL=serializers.d.ts.map
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import JsonLdSerializer from "@rdfjs/serializer-jsonld-ext";
|
|
2
|
+
import NTriplesSerializer from "@rdfjs/serializer-ntriples";
|
|
3
|
+
import RdfjsSerializer from "@rdfjs/serializer-rdfjs";
|
|
4
|
+
import TurtleSerializer from "@rdfjs/serializer-turtle";
|
|
5
|
+
import SinkMap from "@rdfjs/sink-map";
|
|
6
|
+
import N3Serializer from "./N3Serializer.js";
|
|
7
|
+
export function serializers(options) {
|
|
8
|
+
const commonSerializerOptions = {
|
|
9
|
+
baseIRI: options?.baseIRI,
|
|
10
|
+
prefixes: options?.prefixes,
|
|
11
|
+
};
|
|
12
|
+
return new SinkMap([
|
|
13
|
+
[
|
|
14
|
+
"application/ld+json",
|
|
15
|
+
new JsonLdSerializer({ ...commonSerializerOptions, ...options?.jsonLd }),
|
|
16
|
+
],
|
|
17
|
+
["application/n-quads", new NTriplesSerializer()],
|
|
18
|
+
["application/n-triples", new NTriplesSerializer()],
|
|
19
|
+
[
|
|
20
|
+
"application/trig",
|
|
21
|
+
new N3Serializer({
|
|
22
|
+
...commonSerializerOptions,
|
|
23
|
+
...options?.trig,
|
|
24
|
+
format: "application/trig",
|
|
25
|
+
}),
|
|
26
|
+
],
|
|
27
|
+
["text/javascript", new RdfjsSerializer(options?.rdfjs)],
|
|
28
|
+
[
|
|
29
|
+
"text/n3",
|
|
30
|
+
new N3Serializer({
|
|
31
|
+
...commonSerializerOptions,
|
|
32
|
+
...options?.n3,
|
|
33
|
+
format: "text/n3",
|
|
34
|
+
}),
|
|
35
|
+
],
|
|
36
|
+
[
|
|
37
|
+
"text/turtle",
|
|
38
|
+
new TurtleSerializer({ ...commonSerializerOptions, ...options?.turtle }),
|
|
39
|
+
],
|
|
40
|
+
]);
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=serializers.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"dependencies": {
|
|
3
|
+
"@rdfx/format": "0.0.51",
|
|
4
|
+
"@rdfx/stream": "0.0.51",
|
|
5
|
+
"@rdfjs/prefix-map": "~0.1.2",
|
|
6
|
+
"@rdfjs/serializer-jsonld-ext": "~4.0.2",
|
|
7
|
+
"@rdfjs/serializer-ntriples": "~2.0.1",
|
|
8
|
+
"@rdfjs/serializer-rdfjs": "0.1.3",
|
|
9
|
+
"@rdfjs/serializer-turtle": "~1.1.5",
|
|
10
|
+
"@rdfjs/sink-map": "~2.0.1",
|
|
11
|
+
"@rdfjs/types": "~2.0.1",
|
|
12
|
+
"@types/n3": "~1.26.0",
|
|
13
|
+
"@types/rdfjs__prefix-map": "~0.1.5",
|
|
14
|
+
"@types/rdfjs__serializer-jsonld-ext": "~4.0.1",
|
|
15
|
+
"@types/rdfjs__serializer-ntriples": "~2.0.6",
|
|
16
|
+
"@types/rdfjs__serializer-rdfjs": "0.1.6",
|
|
17
|
+
"@types/rdfjs__serializer-turtle": "~1.1.0",
|
|
18
|
+
"@types/rdfjs__sink-map": "~2.0.5",
|
|
19
|
+
"@types/readable-stream": "~4.0.23",
|
|
20
|
+
"n3": "~1.26.0",
|
|
21
|
+
"purify-ts": "~2.1.4",
|
|
22
|
+
"readable-stream": "^4.7.0"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {},
|
|
25
|
+
"files": [
|
|
26
|
+
"LICENSE",
|
|
27
|
+
"README.md",
|
|
28
|
+
"dist/*.d.ts",
|
|
29
|
+
"dist/*.js"
|
|
30
|
+
],
|
|
31
|
+
"license": "Apache-2.0",
|
|
32
|
+
"main": "./dist/index.js",
|
|
33
|
+
"name": "@rdfx/serializer",
|
|
34
|
+
"packageManager": "npm@11.11.0",
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "git+https://github.com/minorg/rdfx.git"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "tsc -b",
|
|
41
|
+
"clean": "rimraf dist",
|
|
42
|
+
"depcheck": "depcheck --ignores=\"@tsconfig/*,vitest,vitest-fetch-mock\" .",
|
|
43
|
+
"dev": "tsc -w --preserveWatchOutput",
|
|
44
|
+
"dev:tests": "tsc -p __tests__ -w --preserveWatchOutput",
|
|
45
|
+
"test": "cd ../.. && vitest run packages/serializer/__tests__"
|
|
46
|
+
},
|
|
47
|
+
"type": "module",
|
|
48
|
+
"types": "./dist/index.d.ts",
|
|
49
|
+
"version": "0.0.51"
|
|
50
|
+
}
|