Skip to content

Commit d5cf8de

Browse files
committed
chore: reorganise and add JS bridge code
1 parent 89a04eb commit d5cf8de

File tree

17 files changed

+792
-680
lines changed

17 files changed

+792
-680
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
dist
2+
node_modules
23

34
# Created by https://www.toptal.com/developers/gitignore/api/macos,go
45
# Edit at https://www.toptal.com/developers/gitignore?templates=macos,go

Makefile

Lines changed: 0 additions & 4 deletions
This file was deleted.

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# groqfmt-wasm
1+
# @groqfmt/wasm
22

3-
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.
3+
@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.
44

5-
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.
5+
The formatter is exposed to JS as the global function `groqfmt`.

bun.lockb

172 KB
Binary file not shown.

example.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

package.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "@groqfmt/wasm",
3+
"author": "Ash",
4+
"version": "1.0.0",
5+
"license": "MIT",
6+
"repository": "github:juice49/groqfmt-wasm",
7+
"keywords": [
8+
"groq",
9+
"sanity",
10+
"format",
11+
"wasm",
12+
"webassembly"
13+
],
14+
"files": [
15+
"dist"
16+
],
17+
"types": "./dist/types.d.ts",
18+
"main": "./dist/index.js",
19+
"module": "./dist/index.mjs",
20+
"source": "./src/bridge/index.ts",
21+
"scripts": {
22+
"groqfmt:build": "cd ./src/groqfmt && make",
23+
"bridge:build": "pkg build --strict && pkg check --strict && cp ./src/groqfmt/wasm-exec.js ./dist",
24+
"build": "bun run groqfmt:build && bun run bridge:build"
25+
},
26+
"devDependencies": {
27+
"@sanity/pkg-utils": "^2.4.8",
28+
"bun-types": "latest",
29+
"prettier": "^3.0.3",
30+
"typescript": "^5.0.0"
31+
}
32+
}

src/bridge/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from '../groqfmt/types'
2+
export * from './lib/format'

src/bridge/lib/format.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { type Groqfmt, type GroqfmtResult } from '../../groqfmt/types'
2+
import getQueryParams from './get-query-params'
3+
4+
/** @public */
5+
export type GroqfmtResultEnhanced = GroqfmtResult & {
6+
params?: Record<string, string>
7+
}
8+
9+
/** @public */
10+
export function format({
11+
input,
12+
groqfmt,
13+
}: {
14+
input: string
15+
groqfmt: Groqfmt
16+
}): GroqfmtResultEnhanced {
17+
const { result, error } = groqfmt(input)
18+
19+
if (error) {
20+
try {
21+
const url = new URL(input)
22+
const query = url.searchParams.get('query')
23+
24+
if (query) {
25+
return {
26+
...groqfmt(query),
27+
params: getQueryParams(url),
28+
}
29+
}
30+
31+
return {
32+
result,
33+
error,
34+
}
35+
} catch {}
36+
37+
return {
38+
result,
39+
error,
40+
}
41+
}
42+
43+
return {
44+
result,
45+
error,
46+
}
47+
}

src/bridge/lib/get-query-params.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default function getQueryParams(url: URL): Record<string, string> {
2+
return [...url.searchParams.entries()].reduce((params, [key, value]) => {
3+
if (!key.startsWith('$')) {
4+
return params
5+
}
6+
7+
return {
8+
...params,
9+
[key.slice(1)]: value.replaceAll('"', ''),
10+
}
11+
}, {})
12+
}

src/groqfmt/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
all: build
2+
3+
build:
4+
GOOS=js GOARCH=wasm go build -o ../../dist/groqfmt.wasm
File renamed without changes.
File renamed without changes.
File renamed without changes.

groqfmt.d.ts renamed to src/groqfmt/types.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
interface GroqfmtError {
1+
/** @public */
2+
export interface GroqfmtError {
23
begin: number
34
end?: number
45
message: string
56
}
67

7-
type GroqfmtResult =
8+
/** @public */
9+
export type GroqfmtResult =
810
| {
911
error: GroqfmtError
1012
result: undefined
@@ -14,9 +16,11 @@ type GroqfmtResult =
1416
result: string
1517
}
1618

17-
declare const groqfmt: (query: string) => GroqfmtResult
19+
/** @public */
20+
export type Groqfmt = (query: string) => GroqfmtResult
1821

19-
declare const Go: {
22+
/**@public */
23+
export interface Go {
2024
new (): {
2125
run: (instance: WebAssembly.Instance) => Promise<void>
2226
importObject: WebAssembly.Imports

0 commit comments

Comments
 (0)