Skip to content

Commit

Permalink
Fix caching failing with binary files
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Sep 27, 2019
1 parent 3a2b470 commit 1dfee43
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/Util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {createReadStream, existsSync, readFileSync, ReadStream, writeFileSync} from "fs";
import {createReadStream, createWriteStream, existsSync, readFileSync, ReadStream, writeFileSync} from "fs";
import {JsonLdParser} from "jsonld-streaming-parser";
import * as RDF from "rdf-js";
import {RdfXmlParser} from "rdfxml-streaming-parser";
import {PassThrough} from "stream";
import {DocumentLoaderCached} from "./DocumentLoaderCached";
import {GeneralizedN3StreamParser} from "./GeneralizedN3StreamParser";
// tslint:disable:no-var-requires
Expand Down Expand Up @@ -135,19 +136,29 @@ export class Util {
if (!response.ok) {
throw new Error(`Could not find ${url}`);
}
const bodyString = await response.text();
const body1 = (<any> response.body).pipe(new PassThrough());
const body2 = (<any> response.body).pipe(new PassThrough());

// Remove unneeded headers
response.headers.delete('content-length');
response.headers.delete('content-encoding');

if (cachePathLocal) {
// Save in cache
writeFileSync(cachePathLocal, bodyString);
const writeStream = createWriteStream(cachePathLocal);
body1.pipe(writeStream);
await new Promise((resolve, reject) => {
writeStream.on('close', resolve);
writeStream.on('error', reject);
});
writeFileSync(cachePathLocal + '.url', response.url);
const headersRaw: any = {};
response.headers.forEach((value: string, key: string) => headersRaw[key] = value);
writeFileSync(cachePathLocal + '.headers', JSON.stringify(headersRaw));
}

return {
body: streamifyString(bodyString),
body: body2,
headers: response.headers,
url: response.url,
};
Expand Down

0 comments on commit 1dfee43

Please sign in to comment.