-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: reorganise and add JS bridge code
- Loading branch information
Showing
17 changed files
with
792 additions
and
680 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# groqfmt-wasm | ||
# @groqfmt/wasm | ||
|
||
groqfmt-wasm is a formatter for [the GROQ query language](https://github.com/sanity-io/GROQ), designed to be compiled to WebAssembly. This tool is largely based on the existing [groqfmt](https://github.com/sanity-io/groqfmt) tool, and built on top of other tools from the GROQ ecosystem. | ||
@groqfmt/wasm is a formatter for [the GROQ query language](https://github.com/sanity-io/GROQ), designed to be compiled to WebAssembly. This tool is largely based on the existing [groqfmt](https://github.com/sanity-io/groqfmt) tool, and built on top of other tools from the GROQ ecosystem. | ||
|
||
Currently the formatter is exposed to JS as the global function `groqfmt`, because I can't get TinyGo's exports working. I'd like to fix that. | ||
The formatter is exposed to JS as the global function `groqfmt`. |
This file was deleted.
Oops, something went wrong.
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,32 @@ | ||
{ | ||
"name": "@groqfmt/wasm", | ||
"author": "Ash", | ||
"version": "1.0.0", | ||
"license": "MIT", | ||
"repository": "github:juice49/groqfmt-wasm", | ||
"keywords": [ | ||
"groq", | ||
"sanity", | ||
"format", | ||
"wasm", | ||
"webassembly" | ||
], | ||
"files": [ | ||
"dist" | ||
], | ||
"types": "./dist/types.d.ts", | ||
"main": "./dist/index.js", | ||
"module": "./dist/index.mjs", | ||
"source": "./src/bridge/index.ts", | ||
"scripts": { | ||
"groqfmt:build": "cd ./src/groqfmt && make", | ||
"bridge:build": "pkg build --strict && pkg check --strict && cp ./src/groqfmt/wasm-exec.js ./dist", | ||
"build": "bun run groqfmt:build && bun run bridge:build" | ||
}, | ||
"devDependencies": { | ||
"@sanity/pkg-utils": "^2.4.8", | ||
"bun-types": "latest", | ||
"prettier": "^3.0.3", | ||
"typescript": "^5.0.0" | ||
} | ||
} |
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,2 @@ | ||
export * from '../groqfmt/types' | ||
export * from './lib/format' |
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,47 @@ | ||
import { type Groqfmt, type GroqfmtResult } from '../../groqfmt/types' | ||
import getQueryParams from './get-query-params' | ||
|
||
/** @public */ | ||
export type GroqfmtResultEnhanced = GroqfmtResult & { | ||
params?: Record<string, string> | ||
} | ||
|
||
/** @public */ | ||
export function format({ | ||
input, | ||
groqfmt, | ||
}: { | ||
input: string | ||
groqfmt: Groqfmt | ||
}): GroqfmtResultEnhanced { | ||
const { result, error } = groqfmt(input) | ||
|
||
if (error) { | ||
try { | ||
const url = new URL(input) | ||
const query = url.searchParams.get('query') | ||
|
||
if (query) { | ||
return { | ||
...groqfmt(query), | ||
params: getQueryParams(url), | ||
} | ||
} | ||
|
||
return { | ||
result, | ||
error, | ||
} | ||
} catch {} | ||
|
||
return { | ||
result, | ||
error, | ||
} | ||
} | ||
|
||
return { | ||
result, | ||
error, | ||
} | ||
} |
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,12 @@ | ||
export default function getQueryParams(url: URL): Record<string, string> { | ||
return [...url.searchParams.entries()].reduce((params, [key, value]) => { | ||
if (!key.startsWith('$')) { | ||
return params | ||
} | ||
|
||
return { | ||
...params, | ||
[key.slice(1)]: value.replaceAll('"', ''), | ||
} | ||
}, {}) | ||
} |
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,4 @@ | ||
all: build | ||
|
||
build: | ||
GOOS=js GOARCH=wasm go build -o ../../dist/groqfmt.wasm |
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
Oops, something went wrong.