Skip to content

Commit

Permalink
feat(middleware): PoC of ANS-101 ar-io#27
Browse files Browse the repository at this point in the history
  • Loading branch information
TillaTheHun0 committed Aug 21, 2023
1 parent b242736 commit fc7b01a
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/middleware/ANS-101.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* AR.IO Gateway
* Copyright (C) 2022-2023 Permanent Data Solutions, Inc. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import type {
ArweaveG8wayMiddleware,
ArweaveG8wayMiddlewareContext,
} from './types.js';

const capabilities: { name: string }[] = [];

/**
* Can be instantiated and then injected to add capabilities
*/
export const addCapability: ArweaveG8wayMiddlewareContext['addCapability'] =
async (capability) => {
const found = capabilities.find((c) => c.name === capability.name);

if (found)
throw new Error(
`Multiple middleware implementing the ${capability.name} capability`,
);

capabilities.push(capability);
};

/**
* An Arweave G8way Middleware that exposes an implementation of ANS-101
*
* See https://specs.g8way.io/?tx=hLSKTSwd5_3xB71zciyK_WFEpK9wVX2IeGzxk9Yl2xY
*/
export const createCapabilitiesMiddleware: () => ArweaveG8wayMiddleware =
() =>
async ({ addCapability }) => {
await addCapability({ name: 'reflexive', version: '1.0.0' });

return async (app) =>
app.get('/info/capabilities', (_req, res) => res.json({ capabilities }));
};
39 changes: 39 additions & 0 deletions src/middleware/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* AR.IO Gateway
* Copyright (C) 2022-2023 Permanent Data Solutions, Inc. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import type { Express } from 'express';

type JSONValue =
| string
| number
| boolean
| { [x: string]: JSONValue }
| Array<JSONValue>;

export type ArweaveG8wayCapability<
C extends Record<string, JSONValue> = Record<string, JSONValue>,
> = C & { name: string; version: string };

export type ArweaveG8wayMiddlewareContext = {
addCapability: <C extends ArweaveG8wayCapability>(
capability: C,
) => Promise<void>;
};

export type ArweaveG8wayMiddleware = (
context: ArweaveG8wayMiddlewareContext,
) => Promise<(app: Express) => Express | Promise<Express>>;

0 comments on commit fc7b01a

Please sign in to comment.