-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cba2d4d
commit cf38c40
Showing
15 changed files
with
1,044 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import fetch from "node-fetch"; | ||
import AdmZip from "adm-zip"; | ||
import fs from "fs/promises"; | ||
import fse from "fs-extra"; | ||
import path from "path"; | ||
import { getAosProcessPath } from "@/utils/getAosProcessPath.js"; | ||
|
||
const zipUrl = "https://github.com/permaweb/aos/archive/refs/heads/main.zip"; | ||
const folderToCopy = "aos-main/process"; | ||
const maxRetries = 3; | ||
const retryDelay = 2000; // 2 seconds | ||
|
||
async function fetchWithRetry(url: string, retries: number, delay: number) { | ||
for (let attempt = 1; attempt <= retries; attempt++) { | ||
try { | ||
const response = await fetch(url); | ||
if (!response.ok) { | ||
throw new Error(`Failed to download zip file: ${response.statusText}`); | ||
} | ||
return response; | ||
} catch (error: any) { | ||
if (attempt < retries) { | ||
console.warn(`Attempt ${attempt} failed. Retrying in ${delay}ms...`); | ||
await new Promise((resolve) => setTimeout(resolve, delay)); | ||
} else { | ||
throw new Error(`Failed to download zip file after ${retries} attempts: ${error.message}`); | ||
} | ||
} | ||
} | ||
return; | ||
} | ||
|
||
async function folderExists(folderPath: string) { | ||
try { | ||
await fs.access(folderPath); | ||
return true; | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
|
||
export async function downloadAosProcess(projectDir: string) { | ||
try { | ||
const destinationDir = getAosProcessPath(); | ||
|
||
const isAosDownloaded = await folderExists(destinationDir); | ||
if (isAosDownloaded) return true; | ||
|
||
// Ensure the destination directory exists | ||
await fs.mkdir(destinationDir, { recursive: true }); | ||
|
||
// Download the zip file with retries | ||
const response = await fetchWithRetry(zipUrl, maxRetries, retryDelay); | ||
if (!response) return false; | ||
|
||
const arrayBuffer = await response.arrayBuffer(); | ||
const zip = new AdmZip(Buffer.from(arrayBuffer)); | ||
const zipEntries = zip.getEntries(); | ||
|
||
// Copy the specific folder to the destination directory | ||
for (const entry of zipEntries) { | ||
if (entry.entryName.startsWith(folderToCopy)) { | ||
const relativePath = entry.entryName.replace(folderToCopy, ""); | ||
const filePath = path.join(destinationDir, relativePath); | ||
|
||
if (entry.isDirectory) { | ||
await fs.mkdir(filePath, { recursive: true }); | ||
} else { | ||
await fs.writeFile(filePath, entry.getData()); | ||
} | ||
} | ||
} | ||
|
||
// Copy json.lua | ||
const jsonFileDst = path.join(destinationDir, "json.lua"); | ||
const jsonFileSrc = path.join(projectDir, "src", "libs", "json", "json.lua"); | ||
await fse.copy(jsonFileSrc, jsonFileDst); | ||
|
||
// Copy everything inside testing Directory | ||
const testingDir = path.join(projectDir, "src", "libs", "testing"); | ||
await fse.copy(testingDir, destinationDir); | ||
await fse.remove(testingDir); | ||
return true; | ||
} catch (error: any) { | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import path from "path"; | ||
import envPaths from "env-paths"; | ||
|
||
export function getAosProcessPath() { | ||
const paths = envPaths("create-ao-contract"); | ||
const aosProcessPath = path.join(paths.data, "aos-process"); | ||
return aosProcessPath; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
local ao = require(".ao") | ||
require(".process") | ||
|
||
---Generate a valid Arweave address | ||
---@return string | ||
local function generateAddress() | ||
local id = "" | ||
|
||
-- possible characters in a valid arweave address | ||
local chars = | ||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_-" | ||
|
||
while string.len(id) < 43 do | ||
-- get random char | ||
local char = math.random(1, string.len(chars)) | ||
|
||
-- select and apply char | ||
id = id .. string.sub(chars, char, char) | ||
end | ||
|
||
return id | ||
end | ||
|
||
local env = { | ||
Module = { | ||
Tags = { | ||
{ | ||
name = "Memory-Limit", | ||
value = "1-gb" | ||
}, | ||
{ | ||
name = "Compute-Limit", | ||
value = "9000000000000" | ||
}, | ||
{ | ||
name = "Module-Format", | ||
value = "wasm64-unknown-emscripten-draft_2024_02_15" | ||
}, | ||
{ | ||
name = "Data-Protocol", | ||
value = "ao" | ||
}, | ||
{ | ||
name = "Type", | ||
value = "Module" | ||
}, | ||
{ | ||
name = "Input-Encoding", | ||
value = "JSON-1" | ||
}, | ||
{ | ||
name = "Output-Encoding", | ||
value = "JSON-1" | ||
}, | ||
{ | ||
name = "Variant", | ||
value = "ao.TN.1" | ||
}, | ||
{ | ||
name = "Content-Type", | ||
value = "application/wasm" | ||
} | ||
}, | ||
Owner = "vh-NTHVvlKZqRxc8LyyTNok65yQ55a_PJ1zWLb9G2JI", | ||
Id = "Pq2Zftrqut0hdisH_MC2pDOT6S4eQFoxGsFUzR6r350" | ||
}, | ||
Process = { | ||
Tags = { | ||
["App-Name"] = "aos", | ||
["aos-Version"] = "1.11.3", | ||
[" Data-Protocol"] = "ao", | ||
Scheduler = "_GQ33BkPtZrqxA84vM8Zk-N2aO0toNNu_C-l-rawrBA", | ||
Variant = "ao.TN.1", | ||
Name = "aos-process", | ||
Type = "Process", | ||
SDK = "aoconnect", | ||
Module = "Pq2Zftrqut0hdisH_MC2pDOT6S4eQFoxGsFUzR6r350", | ||
Authority = "fcoN_xJeisVsPXA-trzVAuIiqO3ydLQxM-L4XbrQKzY" | ||
}, | ||
TagArray = { | ||
{ | ||
name = "App-Name", | ||
value = "aos" | ||
}, | ||
{ | ||
name = "Name", | ||
value = "aos-process" | ||
}, | ||
{ | ||
name = "Authority", | ||
value = "fcoN_xJeisVsPXA-trzVAuIiqO3ydLQxM-L4XbrQKzY" | ||
}, | ||
{ | ||
name = "aos-Version", | ||
value = "1.11.3" | ||
}, | ||
{ | ||
name = "Data-Protocol", | ||
value = "ao" | ||
}, | ||
{ | ||
name = "Variant", | ||
value = "ao.TN.1" | ||
}, | ||
{ | ||
name = "Type", | ||
value = "Process" | ||
}, | ||
{ | ||
name = "Module", | ||
value = "Pq2Zftrqut0hdisH_MC2pDOT6S4eQFoxGsFUzR6r350" | ||
}, | ||
{ | ||
name = "Scheduler", | ||
value = "_GQ33BkPtZrqxA84vM8Zk-N2aO0toNNu_C-l-rawrBA" | ||
}, | ||
{ | ||
name = "SDK", | ||
value = "aoconnect" | ||
} | ||
}, | ||
Owner = generateAddress(), | ||
Id = generateAddress() | ||
} | ||
} | ||
|
||
ao.init(env) |
Oops, something went wrong.