Skip to content

Commit de421e1

Browse files
authored
Merge pull request #1776 from polywrap/nerfzael-add-redirect-deployment-and-headers
HTTP deployment of redirect and added headers to http deployer
2 parents cd36ead + aedd152 commit de421e1

3 files changed

Lines changed: 58 additions & 22 deletions

File tree

packages/cli/src/lib/defaults/deploy-modules/http/index.ts

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import FormData from "form-data";
55
import axios from "axios";
66
import { readdirSync, readFileSync } from "fs-extra";
77

8-
const isValidUri = (uri: Uri) => uri.authority === "fs";
9-
108
const dirToFormData = (baseDirPath: string) => {
119
const formData = new FormData();
1210

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

3634
class HTTPDeployer implements DeployModule {
37-
async execute(uri: Uri, config?: { postUrl: string }): Promise<Uri> {
38-
if (!isValidUri(uri)) {
39-
throw new Error(
40-
`HTTP Deployer error: Invalid URI: ${uri.toString()}. Supplied URI needs to be a Filesystem URI, example: fs/./build`
41-
);
42-
}
43-
35+
async execute(
36+
uri: Uri,
37+
config?: { postUrl: string; headers: { name: string; value: string }[] }
38+
): Promise<Uri> {
4439
if (!config?.postUrl) {
4540
throw new Error(
4641
`HTTP Deployer error: No postUrl provided. Please provide a postUrl in the deploy manifest's config object`
4742
);
4843
}
4944

50-
const formData = dirToFormData(uri.path);
45+
let response;
46+
if (uri.authority === "fs" || uri.authority === "file") {
47+
// URI is a FileSystem URI, so we read the directory and publish it
48+
const formData = dirToFormData(uri.path);
5149

52-
const response = await axios.post<{ uri: string; error: string }>(
53-
config.postUrl,
54-
formData,
55-
{
56-
headers: formData.getHeaders(),
57-
}
58-
);
50+
response = await axios.post<{ uri: string; error: string }>(
51+
config.postUrl,
52+
formData,
53+
{
54+
headers: {
55+
...formData.getHeaders(),
56+
...parseConfigHeaders(config.headers),
57+
},
58+
}
59+
);
60+
} else {
61+
// URI is of another kind, so we just publish it as a redirect
62+
response = await axios.post<{ uri: string; error: string }>(
63+
config.postUrl,
64+
{
65+
uri: uri.toString(),
66+
},
67+
{
68+
headers: parseConfigHeaders(config.headers),
69+
}
70+
);
71+
}
5972

6073
if (response.data.error) {
6174
throw new Error(response.data.error);
@@ -65,4 +78,14 @@ class HTTPDeployer implements DeployModule {
6578
}
6679
}
6780

81+
function parseConfigHeaders(configHeaders: { name: string; value: string }[]) {
82+
const headers: { [key: string]: string } = {};
83+
84+
configHeaders.forEach((header) => {
85+
headers[header.name] = header.value;
86+
});
87+
88+
return headers;
89+
}
90+
6891
export default new HTTPDeployer();
Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
{
22
"id": "DeployManifest_Deployer_WasmAsExt",
33
"type": "object",
4-
"required": [
5-
"postUrl"
6-
],
4+
"required": ["postUrl"],
75
"properties": {
86
"postUrl": {
97
"type": "string"
8+
},
9+
"headers": {
10+
"type": "array",
11+
"items": {
12+
"type": "object",
13+
"properties": {
14+
"name": {
15+
"type": "string"
16+
},
17+
"value": {
18+
"type": "string"
19+
}
20+
}
21+
}
1022
}
1123
}
12-
}
24+
}

packages/cli/src/lib/defaults/deploy-modules/ipfs/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import { Uri } from "@polywrap/core-js";
66
const IPFSClient = require("ipfs-http-client");
77
const { globSource } = IPFSClient;
88

9-
const isValidUri = (uri: Uri) => uri.authority === "fs";
9+
const isValidUri = (uri: Uri) =>
10+
uri.authority === "fs" || uri.authority === "file";
1011

1112
class IPFSDeployer implements DeployModule {
1213
async execute(uri: Uri, config?: { gatewayUri: string }): Promise<Uri> {

0 commit comments

Comments
 (0)