Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 40 additions & 17 deletions packages/cli/src/lib/defaults/deploy-modules/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -34,28 +32,43 @@ const dirToFormData = (baseDirPath: string) => {
};

class HTTPDeployer implements DeployModule {
async execute(uri: Uri, config?: { postUrl: string }): Promise<Uri> {
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<Uri> {
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);
Expand All @@ -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();
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uri> {
Expand Down