From 8a9aef57776966b2e39b176684f57c09daea7019 Mon Sep 17 00:00:00 2001 From: Irakli Gozalishvili Date: Mon, 1 Apr 2024 15:22:22 -0700 Subject: [PATCH] chore: expose simple route to debug (#2546) I was thinking to try something along these lines --- packages/api/src/index.js | 5 +++++ packages/api/src/repl.js | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 packages/api/src/repl.js diff --git a/packages/api/src/index.js b/packages/api/src/index.js index 1ef0931dfa..57c81f54a4 100644 --- a/packages/api/src/index.js +++ b/packages/api/src/index.js @@ -35,6 +35,7 @@ import { } from './middleware/maintenance.js' import { getContext } from './utils/context.js' import { withAuth } from './middleware/auth.js' +import { repl } from './repl.js' const r = new Router(getContext, { onError(req, err, ctx) { @@ -47,6 +48,10 @@ const checkHasDeleteRestriction = true const checkHasPsaAccess = true const checkUcan = true +// Debugging + +r.add('get', '/repl', repl) + // Monitoring r.add('get', '/metrics', withMode(metrics, RO)) r.add('get', '/stats', withMode(getStats, RO), [postCors]) diff --git a/packages/api/src/repl.js b/packages/api/src/repl.js new file mode 100644 index 0000000000..f0ecf84886 --- /dev/null +++ b/packages/api/src/repl.js @@ -0,0 +1,27 @@ +import { JSONResponse } from './utils/json-response.js' +import { HTTPError } from './errors.js' + +/** @type {import('./bindings').Handler} */ +export const repl = async (event, ctx) => { + const { w3up } = ctx + + if (w3up) { + const blob = await event.request.blob() + if (blob.size === 0) { + throw new HTTPError('empty payload', 400) + } + + const result = await w3up.uploadCAR(blob, { + onShardStored: ({ cid }) => { + console.log(`SHARD ${cid}`) + }, + // @ts-expect-error TODO adjust upstream type + pieceHasher: null, + }) + console.log('UPLOADED CAR') + + return new JSONResponse({ ok: true, value: result }) + } else { + throw new HTTPError(`No w3up client`, 500) + } +}