diff --git a/packages/edge/bundle.ts b/packages/edge/bundle.ts index b3eb76b..d728bed 100644 --- a/packages/edge/bundle.ts +++ b/packages/edge/bundle.ts @@ -13,7 +13,6 @@ import { setFile as setGist } from "./gist.ts"; import { build, setFile, useFileSystem, createConfig, compress, resolveVersion } from "@bundlejs/core/src/index.ts"; import { createNotice } from "@bundlejs/core/src/utils/create-notice.ts"; -import { extname } from "../core/src/deno/path/posix.ts"; const FileSystem = useFileSystem(); export const timeFormatter = new Intl.RelativeTimeFormat("en", { @@ -42,7 +41,7 @@ export const inputModelResetValue = [ 'export * from "spring-easing";' ].join("\n"); -export async function bundle(url: URL, initialValue: string, configObj: Config) { +export async function bundle(url: URL, initialValue: string, configObj: Config, versions: string[], query: string) { const fs = await FileSystem; const start = performance.now(); @@ -65,32 +64,13 @@ export async function bundle(url: URL, initialValue: string, configObj: Config) const { init: _init, ...printableConfig } = createConfig("build", configObj); const duration = (end - start); - - const { fileId, fileUrl, fileHTMLUrl } = await setGist(url.href, result.outputs) ?? {}; - const allPkgs = ( - ( - url.searchParams.get("q") || - url.searchParams.get("query") - ) ?? "spring-easing" - ).split(","); - const versionsList = await Promise.allSettled( - allPkgs - .filter(x => !/^https?\:\/\//.exec(x)) - .map(x => resolveVersion(x)) - ); - const versions = []; - for (const version of versionsList) { - if (version.status === "fulfilled" && version.value) { - versions.push(version.value); - } - } - - const searchQueries = url.search ?? `?q=${allPkgs.join(",")}`; + const { fileId, fileUrl, fileHTMLUrl } = await setGist(url.href, result.outputs) ?? {}; + const searchQueries = url.search || `?q=${query}`; const finalResult: BundleResult = { query: decodeURIComponent(searchQueries), rawQuery: encodeURIComponent(searchQueries), - ...(versions.length === 1 ? { version: versions[0] } : versions), + ...(versions.length === 1 ? { version: versions[0] } : { versions }), config: printableConfig, input: initialValue, size, diff --git a/packages/edge/gist.ts b/packages/edge/gist.ts index a98b584..01a3baa 100644 --- a/packages/edge/gist.ts +++ b/packages/edge/gist.ts @@ -1,4 +1,4 @@ -import { Octokit } from "octokit"; +import { Octokit } from "npm:octokit"; import { path, dispatchEvent, LOGGER_ERROR, LOGGER_WARN } from "@bundlejs/core/src/index.ts"; import { Velo } from "https://deno.land/x/velo/mod.ts"; import { ESBUILD } from "@bundlejs/core/src/types.ts"; @@ -39,9 +39,6 @@ export async function setFile(url: string, files: ESBUILD.OutputFile[]) { 'public': true, files: { ...filesObj, - // [BUNDLE_FILE_PATH]: { - // content: text - // }, 'README.md': { content: `Hey 👋, this is a gist which stores the final bundle results of the bundlejs api, learn more on the website https://bundlejs.com. This is the result of ${newUrl.href}.` } @@ -67,6 +64,7 @@ export async function setFile(url: string, files: ESBUILD.OutputFile[]) { } export async function listFiles(page = 0) { + // octokit.paginate return ( await octokit.request('GET /gists', { page, diff --git a/packages/edge/mod.ts b/packages/edge/mod.ts index a5f49d5..0258a00 100644 --- a/packages/edge/mod.ts +++ b/packages/edge/mod.ts @@ -1,4 +1,3 @@ -#!/usr/bin/env -S deno run --unstable -A --config deno.jsonc import type { BuildConfig, CompressConfig } from "@bundlejs/core"; import type { BundleResult } from "./bundle.ts"; @@ -12,7 +11,7 @@ globalThis.Worker = worker ?? class { constructor() {} }; -import { deepAssign, createConfig, lzstring, dispatchEvent, LOGGER_INFO, BUILD_CONFIG } from "@bundlejs/core/src/index.ts"; +import { deepAssign, createConfig, resolveVersion, lzstring, dispatchEvent, LOGGER_INFO, BUILD_CONFIG } from "@bundlejs/core/src/index.ts"; import ESBUILD_WASM from "@bundlejs/core/src/wasm.ts"; import { parseShareURLQuery, parseConfig } from "./parse-query.ts"; @@ -95,10 +94,32 @@ serve(async (req: Request) => { ); console.log(configObj) + const query = ( + ( + url.searchParams.get("q") || + url.searchParams.get("query") + ) ?? "spring-easing" + ); + const versionsList = await Promise.allSettled( + query + .split(",") + .filter(x => !/^https?\:\/\//.exec(x)) + .map(async x => [x, await resolveVersion(x)]) + ); + + const versions: string[] = []; + for (const version of versionsList) { + if (version.status === "fulfilled" && version.value) { + const [name, ver] = version.value; + versions.push(`${name}@${ver}`); + } + } + const jsonKey = `json-${ compressToBase64( JSON.stringify({ ...configObj, + versions, initialValue: initialValue.trim(), }).trim() ) // .slice(0, 512 - 1) @@ -155,7 +176,7 @@ serve(async (req: Request) => { const start = Date.now(); if (!WASM_MODULE) WASM_MODULE = await ESBUILD_WASM(); if (!wasmModule) wasmModule = new WebAssembly.Module(WASM_MODULE); - const response = await bundle(url, initialValue, configObj); + const response = await bundle(url, initialValue, configObj, versions, query); if (!response.ok) { const headers = response.headers;