UNPKG

@zazuko/rdf-utils-fs

Version:
23 lines (22 loc) 993 B
import { createWriteStream } from 'fs'; import { extname } from 'path'; import { promisify } from 'util'; import url from 'url'; import { finished } from 'readable-stream'; import config from './defaults.js'; export default function toFile(env, stream, pathOrUrl, { extensions = config.extensions, ...options } = {}) { const filename = typeof pathOrUrl === 'string' ? pathOrUrl : url.fileURLToPath(pathOrUrl); const extension = extname(filename).split('.').pop(); const mediaType = extensions[extension]; if (!mediaType) { throw new Error(`Unknown file extension: ${extension}`); } // eslint-disable-next-line @typescript-eslint/no-explicit-any const serializer = env.formats.serializers.get(mediaType); if (!serializer) { throw new Error(`No serializer available for media type: ${mediaType}`); } const output = createWriteStream(filename); serializer.import(stream, options).pipe(output); return promisify(finished)(output); }