diff --git a/packages/cli/src/lib/defaults/deploy-modules/http/index.ts b/packages/cli/src/lib/defaults/deploy-modules/http/index.ts index 80b1c9094c..c0ec89af70 100644 --- a/packages/cli/src/lib/defaults/deploy-modules/http/index.ts +++ b/packages/cli/src/lib/defaults/deploy-modules/http/index.ts @@ -5,8 +5,6 @@ import FormData from "form-data"; import axios from "axios"; import { readdirSync, readFileSync } from "fs-extra"; -const isValidUri = (uri: Uri) => uri.authority === "fs"; - const dirToFormData = (baseDirPath: string) => { const formData = new FormData(); @@ -34,28 +32,43 @@ const dirToFormData = (baseDirPath: string) => { }; class HTTPDeployer implements DeployModule { - async execute(uri: Uri, config?: { postUrl: string }): Promise { - if (!isValidUri(uri)) { - throw new Error( - `HTTP Deployer error: Invalid URI: ${uri.toString()}. Supplied URI needs to be a Filesystem URI, example: fs/./build` - ); - } - + async execute( + uri: Uri, + config?: { postUrl: string; headers: { name: string; value: string }[] } + ): Promise { if (!config?.postUrl) { throw new Error( `HTTP Deployer error: No postUrl provided. Please provide a postUrl in the deploy manifest's config object` ); } - const formData = dirToFormData(uri.path); + let response; + if (uri.authority === "fs" || uri.authority === "file") { + // URI is a FileSystem URI, so we read the directory and publish it + const formData = dirToFormData(uri.path); - const response = await axios.post<{ uri: string; error: string }>( - config.postUrl, - formData, - { - headers: formData.getHeaders(), - } - ); + response = await axios.post<{ uri: string; error: string }>( + config.postUrl, + formData, + { + headers: { + ...formData.getHeaders(), + ...parseConfigHeaders(config.headers), + }, + } + ); + } else { + // URI is of another kind, so we just publish it as a redirect + response = await axios.post<{ uri: string; error: string }>( + config.postUrl, + { + uri: uri.toString(), + }, + { + headers: parseConfigHeaders(config.headers), + } + ); + } if (response.data.error) { throw new Error(response.data.error); @@ -65,4 +78,14 @@ class HTTPDeployer implements DeployModule { } } +function parseConfigHeaders(configHeaders: { name: string; value: string }[]) { + const headers: { [key: string]: string } = {}; + + configHeaders.forEach((header) => { + headers[header.name] = header.value; + }); + + return headers; +} + export default new HTTPDeployer(); diff --git a/packages/cli/src/lib/defaults/deploy-modules/http/polywrap.deploy.ext.json b/packages/cli/src/lib/defaults/deploy-modules/http/polywrap.deploy.ext.json index 0ee538d0df..f05e7fbf56 100644 --- a/packages/cli/src/lib/defaults/deploy-modules/http/polywrap.deploy.ext.json +++ b/packages/cli/src/lib/defaults/deploy-modules/http/polywrap.deploy.ext.json @@ -1,12 +1,24 @@ { "id": "DeployManifest_Deployer_WasmAsExt", "type": "object", - "required": [ - "postUrl" - ], + "required": ["postUrl"], "properties": { "postUrl": { "type": "string" + }, + "headers": { + "type": "array", + "items": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "value": { + "type": "string" + } + } + } } } -} \ No newline at end of file +} diff --git a/packages/cli/src/lib/defaults/deploy-modules/ipfs/index.ts b/packages/cli/src/lib/defaults/deploy-modules/ipfs/index.ts index 446d033840..9a9de9c00f 100644 --- a/packages/cli/src/lib/defaults/deploy-modules/ipfs/index.ts +++ b/packages/cli/src/lib/defaults/deploy-modules/ipfs/index.ts @@ -6,7 +6,8 @@ import { Uri } from "@polywrap/core-js"; const IPFSClient = require("ipfs-http-client"); const { globSource } = IPFSClient; -const isValidUri = (uri: Uri) => uri.authority === "fs"; +const isValidUri = (uri: Uri) => + uri.authority === "fs" || uri.authority === "file"; class IPFSDeployer implements DeployModule { async execute(uri: Uri, config?: { gatewayUri: string }): Promise {