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
75 changes: 75 additions & 0 deletions .github/workflows/build-wasm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
name: Build WebAssembly

on:
push:
branches: [main, master]
paths:
- 'code/**'
- 'wasm/**'
pull_request:
paths:
- 'code/**'
- 'wasm/**'

permissions:
contents: read

jobs:
build:
name: Build Wasm
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Emscripten SDK
uses: mymindstorm/setup-emsdk@v14

- name: Configure (emcmake cmake)
run: emcmake cmake -S wasm -B build-wasm

- name: Build
run: cmake --build build-wasm -j$(nproc)

- name: Prepare distribution files
run: |
mkdir -p dist
cp wasm/index.html dist/
cp build-wasm/downgrader.js dist/
cp build-wasm/downgrader.wasm dist/

- name: Upload build artifact
uses: actions/upload-artifact@v4
with:
name: wasm-dist
path: dist/
if-no-files-found: error

deploy:
name: Deploy to GitHub Pages
needs: build
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master')
runs-on: ubuntu-latest
permissions:
pages: write
id-token: write
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}

steps:
- name: Download build artifact
uses: actions/download-artifact@v4
with:
name: wasm-dist
path: dist/

- name: Upload Pages artifact
uses: actions/upload-pages-artifact@v3
with:
path: dist/

- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ Loosely based on [api-spec-converter](https://github.com/LucyBot-Inc/api-spec-co

Uses [yaml-cpp](https://github.com/jbeder/yaml-cpp).
Built using Visual Studio 2022.

### Web Version

A browser-based version is available that runs the conversion entirely client-side via WebAssembly (no server required). It can be hosted on GitHub Pages or any static file host.

See [`wasm/README.md`](wasm/README.md) for build instructions.
17 changes: 13 additions & 4 deletions code/Converter.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "converter.h"
#include "Converter.h"
#include "Util.h"
#include <iostream>
#include <regex>
Expand Down Expand Up @@ -530,8 +530,7 @@ void Converter::ConvertSecurityDefinitions() {
input["components"].remove("securitySchemes");
}

std::string Converter::Convert(const std::string& source) {
input = YAML::LoadFile(source);
std::string Converter::RunConversion() {
if (input["components"]) {
components = YAML::Clone(input["components"]);
}
Expand Down Expand Up @@ -567,4 +566,14 @@ std::string Converter::Convert(const std::string& source) {
std::string str = "swagger: \"2.0\"\n";
str += out.c_str();
return str;
}
}

std::string Converter::Convert(const std::string& source) {
input = YAML::LoadFile(source);
return RunConversion();
}

std::string Converter::ConvertString(const std::string& yamlContent) {
input = YAML::Load(yamlContent);
return RunConversion();
}
2 changes: 2 additions & 0 deletions code/Converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
class Converter {
public:
std::string Convert(const std::string& source);
std::string ConvertString(const std::string& yamlContent);
private:
std::string RunConversion();
YAML::Node input;
YAML::Node components;
void ConvertInfos();
Expand Down
38 changes: 38 additions & 0 deletions wasm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
cmake_minimum_required(VERSION 3.15)
project(openapi-downgrader-wasm)

include(FetchContent)

FetchContent_Declare(
yaml-cpp
GIT_REPOSITORY https://github.com/jbeder/yaml-cpp.git
GIT_TAG 0.8.0
)
set(YAML_CPP_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(YAML_CPP_BUILD_TOOLS OFF CACHE BOOL "" FORCE)
set(YAML_CPP_BUILD_CONTRIB OFF CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(yaml-cpp)

add_executable(downgrader
../code/Converter.cpp
../code/Util.cpp
wasm_entry.cpp
)

target_include_directories(downgrader PRIVATE ../include)
target_link_libraries(downgrader PRIVATE yaml-cpp)

set_target_properties(downgrader PROPERTIES
OUTPUT_NAME "downgrader"
SUFFIX ".js"
)

target_link_options(downgrader PRIVATE
--bind
-O2
-sWASM=1
-sALLOW_MEMORY_GROWTH=1
-sMODULARIZE=1
-sEXPORT_NAME=createModule
-sENVIRONMENT=web
)
63 changes: 63 additions & 0 deletions wasm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# openapi-downgrader — WebAssembly / Browser Build

This directory contains everything needed to build and run the OpenAPI 3.0 → Swagger 2.0 converter **entirely in the browser** using WebAssembly (Emscripten).

## Prerequisites

- [Emscripten SDK](https://emscripten.org/docs/getting_started/downloads.html) (`emcc`/`em++` on your PATH)
- CMake >= 3.15
- Git (used by CMake `FetchContent` to download yaml-cpp)
- Python 3 (for the local dev server)

### Install Emscripten SDK

```bash
git clone https://github.com/emscripten-core/emsdk.git
cd emsdk
./emsdk install latest
./emsdk activate latest
source ./emsdk_env.sh # add to your shell profile for persistence
```

## Build

Run the following commands from the **repository root**:

```bash
mkdir build-wasm
cd build-wasm
emcmake cmake ../wasm
cmake --build . -- -j$(nproc)
```

This will produce `downgrader.js` and `downgrader.wasm` in the `build-wasm/` directory.

After a successful build, copy (or symlink) those two files next to `wasm/index.html`:

```bash
cp build-wasm/downgrader.js wasm/
cp build-wasm/downgrader.wasm wasm/
```

## Serve locally

Browsers require a proper HTTP server to load `.wasm` files (the `file://` protocol does not work):

```bash
cd wasm
python3 -m http.server 8080
```

Then open <http://localhost:8080> in your browser.

## GitHub Pages hosting

1. After building, commit `wasm/index.html`, `wasm/downgrader.js`, and `wasm/downgrader.wasm` to your repository.
2. Enable **GitHub Pages** in the repository settings, pointing at the branch/folder that contains the `wasm/` directory (or move the files to `docs/`).
3. GitHub Pages serves static files with the correct MIME types for `.wasm`, so no extra configuration is needed.

## Notes

- yaml-cpp is fetched automatically by CMake `FetchContent` during configure — no manual download required.
- The prebuilt `.lib` files in `lib/` are Windows-only and are **not** used by the Wasm build.
- The conversion runs 100 % client-side; no data is sent to any server.
Loading
Loading