Skip to content

Instantly share code, notes, and snippets.

@abalmos
Created January 5, 2020 19:52
Show Gist options
  • Select an option

  • Save abalmos/3afb2422bc2f8038c338f84fc01f2709 to your computer and use it in GitHub Desktop.

Select an option

Save abalmos/3afb2422bc2f8038c338f84fc01f2709 to your computer and use it in GitHub Desktop.
binary via cacahe ?
import { pipeline } from 'stream';
import { promisify } from 'util';
const pipelineAsync = promisify(pipeline);
import * as express from 'express';
import * as ash from 'express-async-handler';
import * as uuid from 'uuid/v4';
import * as cacache from 'cacache';
const CACHE_PATH = "/tmp/oada-cache"; // This should come from the config
const app = express();
app.put('/*', ash(async (req, res) => {
// application/json or application/vnd.blah+json
if (req.header('content-type')?.match(/[\/|+]json$/)) {
// Normal JSON path
} else {
// Assume "binary"
// stream the file from the req source to the `cacache` sink. Pipeline handles errors correctly.
await pipelineAsync(
req,
cacache.put.stream(CACHE_PATH, req.oadaGraph.resource_id)
);
putBodies.savePutBody({}) ... // What ever we need to get an empty "resource" into arango. We don't care about
// the body because on the GET we will fetch the data from cacache, but everything
// about OADA needs that vertex in arango to exist
}
}));
app.get('/*', ash(async (req, res) => {
if (req.header('content-type')?.match(/[\/|+]json$/)) {
// Normal JSON path
} else {
// Assume "binary"
if (req.oadaGraph["path_leftover"]) {
throw new OADAError(...) // Fail the request, binary can't have path_leftover
}
// Stream the file from `cacache` source to the response sink
await pipelineAsync(
cacache.get.stream(CACHE_PATH, req.oadaGraph["resource_id"]),
res
);
res.end(); // NOTE: I'm not sure if this is the right place for this. The current
// http-handler is full of (appears to be as-harvest bookmark specific?) extra code and
// I'm not completely clear on the order of things.
}
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment