Skip to content

Commit

Permalink
chore: reorganise and add JS bridge code
Browse files Browse the repository at this point in the history
  • Loading branch information
juice49 committed Sep 15, 2023
1 parent 89a04eb commit d5cf8de
Show file tree
Hide file tree
Showing 17 changed files with 792 additions and 680 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
dist
node_modules

# Created by https://www.toptal.com/developers/gitignore/api/macos,go
# Edit at https://www.toptal.com/developers/gitignore?templates=macos,go
Expand Down
4 changes: 0 additions & 4 deletions Makefile

This file was deleted.

6 changes: 3 additions & 3 deletions README.md
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`.
Binary file added bun.lockb
Binary file not shown.
15 changes: 0 additions & 15 deletions example.ts

This file was deleted.

32 changes: 32 additions & 0 deletions package.json
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"
}
}
2 changes: 2 additions & 0 deletions src/bridge/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from '../groqfmt/types'
export * from './lib/format'
47 changes: 47 additions & 0 deletions src/bridge/lib/format.ts
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,
}
}
12 changes: 12 additions & 0 deletions src/bridge/lib/get-query-params.ts
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('"', ''),
}
}, {})
}
4 changes: 4 additions & 0 deletions src/groqfmt/Makefile
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.
12 changes: 8 additions & 4 deletions groqfmt.d.ts → src/groqfmt/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
interface GroqfmtError {
/** @public */
export interface GroqfmtError {
begin: number
end?: number
message: string
}

type GroqfmtResult =
/** @public */
export type GroqfmtResult =
| {
error: GroqfmtError
result: undefined
Expand All @@ -14,9 +16,11 @@ type GroqfmtResult =
result: string
}

declare const groqfmt: (query: string) => GroqfmtResult
/** @public */
export type Groqfmt = (query: string) => GroqfmtResult

declare const Go: {
/**@public */
export interface Go {
new (): {
run: (instance: WebAssembly.Instance) => Promise<void>
importObject: WebAssembly.Imports
Expand Down
Loading

0 comments on commit d5cf8de

Please sign in to comment.