From 1d5c6f92776f8bfca2ad9cb3f49421fd9329a2d8 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Wed, 29 Nov 2023 16:51:28 +0100 Subject: [PATCH] Replace YAML parser --- common/deps.ts | 2 +- common/syscalls/yaml.ts | 41 ++++++++++++++++++++++++++++++++---- common/util.ts | 2 +- plug-api/app_event.ts | 2 +- plug-api/lib/attribute.ts | 2 +- plug-api/lib/syscall_mock.ts | 2 +- plugs/index/lint.ts | 2 +- plugs/share/publish.ts | 5 ++++- server/http_server.ts | 9 +++++++- 9 files changed, 55 insertions(+), 12 deletions(-) diff --git a/common/deps.ts b/common/deps.ts index c542f3de..2905568a 100644 --- a/common/deps.ts +++ b/common/deps.ts @@ -15,7 +15,7 @@ export type { Completion, CompletionResult } from "@codemirror/autocomplete"; export { styleTags, Tag, tagHighlighter, tags } from "@lezer/highlight"; -export * as YAML from "https://deno.land/std@0.189.0/yaml/mod.ts"; +export * as YAML from "https://esm.sh/js-yaml@4.1.0"; export * as path from "https://deno.land/std@0.189.0/path/mod.ts"; export type { diff --git a/common/syscalls/yaml.ts b/common/syscalls/yaml.ts index 636f7a1a..3546b6ba 100644 --- a/common/syscalls/yaml.ts +++ b/common/syscalls/yaml.ts @@ -1,15 +1,48 @@ import { SysCallMapping } from "../../plugos/system.ts"; import { YAML } from "../../web/deps.ts"; +type YamlStringifyOptions = { + /** indentation width to use (in spaces). */ + indent?: number; + /** when true, will not add an indentation level to array elements */ + noArrayIndent?: boolean; + /** do not throw on invalid types (like function in the safe schema) and skip pairs and single values with such types. */ + skipInvalid?: boolean; + /** specifies level of nesting, when to switch from block to flow style for collections. -1 means block style everwhere */ + flowLevel?: number; + /** if true, sort keys when dumping YAML. If a function, use the function to sort the keys. (default: false) */ + sortKeys?: boolean; + /** set max line width. (default: 80) */ + lineWidth?: number; + /** if true, don't convert duplicate objects into references (default: false) */ + noRefs?: boolean; + /** if true don't try to be compatible with older yaml versions. Currently: don't quote "yes", "no" and so on, as required for YAML 1.1 (default: false) */ + noCompatMode?: boolean; + /** + * if true flow sequences will be condensed, omitting the space between `key: value` or `a, b`. Eg. `'[a,b]'` or `{a:{b:c}}`. + * Can be useful when using yaml for pretty URL query params as spaces are %-encoded. (default: false). + */ + condenseFlow?: boolean; + /** strings will be quoted using this quoting style. If you specify single quotes, double quotes will still be used for non-printable characters. (default: `'`) */ + quotingType?: "'" | '"'; + /** if true, all non-key strings will be quoted even if they normally don't need to. (default: false) */ + forceQuotes?: boolean; +}; + export function yamlSyscalls(): SysCallMapping { return { "yaml.parse": (_ctx, text: string): any => { - return YAML.parse(text); + return YAML.load(text); }, - "yaml.stringify": (_ctx, obj: any): string => { - return YAML.stringify(obj, { - noArrayIndent: true, + "yaml.stringify": ( + _ctx, + obj: any, + options: YamlStringifyOptions = {}, + ): string => { + return YAML.dump(obj, { + quotingType: '"', noCompatMode: true, + ...options, }); }, }; diff --git a/common/util.ts b/common/util.ts index a63bf286..d85f906b 100644 --- a/common/util.ts +++ b/common/util.ts @@ -24,7 +24,7 @@ export function parseYamlSettings(settingsMarkdown: string): { } const yaml = match[1]; try { - return YAML.parse(yaml) as { + return YAML.load(yaml) as { [key: string]: any; }; } catch (e: any) { diff --git a/plug-api/app_event.ts b/plug-api/app_event.ts index 49b08f25..5ed624f2 100644 --- a/plug-api/app_event.ts +++ b/plug-api/app_event.ts @@ -41,7 +41,7 @@ export type IndexTreeEvent = { }; export type PublishEvent = { - uri: string; + uri?: string; // Page name name: string; }; diff --git a/plug-api/lib/attribute.ts b/plug-api/lib/attribute.ts index 481f7c7a..a9ac920f 100644 --- a/plug-api/lib/attribute.ts +++ b/plug-api/lib/attribute.ts @@ -4,7 +4,7 @@ import { replaceNodesMatchingAsync, } from "$sb/lib/tree.ts"; -import { YAML } from "$sb/plugos-syscall/mod.ts"; +import { YAML } from "$sb/syscalls.ts"; /** * Extracts attributes from a tree, optionally cleaning them out of the tree. diff --git a/plug-api/lib/syscall_mock.ts b/plug-api/lib/syscall_mock.ts index 37290953..4ed1a257 100644 --- a/plug-api/lib/syscall_mock.ts +++ b/plug-api/lib/syscall_mock.ts @@ -3,7 +3,7 @@ import { YAML } from "../../common/deps.ts"; globalThis.syscall = (name: string, ...args: readonly any[]) => { switch (name) { case "yaml.parse": - return Promise.resolve(YAML.parse(args[0])); + return Promise.resolve(YAML.load(args[0])); default: throw Error(`Not implemented in tests: ${name}`); } diff --git a/plugs/index/lint.ts b/plugs/index/lint.ts index 481506a3..94676350 100644 --- a/plugs/index/lint.ts +++ b/plugs/index/lint.ts @@ -51,7 +51,7 @@ export async function lintYAML({ tree }: LintEvent): Promise { return diagnostics; } -const errorRegex = /at line (\d+),? column (\d+)/; +const errorRegex = /\((\d+):(\d+)\)/; async function lintYaml( yamlText: string, diff --git a/plugs/share/publish.ts b/plugs/share/publish.ts index b023fbbd..12d5bed7 100644 --- a/plugs/share/publish.ts +++ b/plugs/share/publish.ts @@ -25,6 +25,9 @@ export async function publishCommand() { } async function publish(pageName: string, uris: string[]) { + const broadcastResults = await events.dispatchEvent(`share:_`, { + name: pageName, + } as PublishEvent); for (const uri of uris) { const publisher = uri.split(":")[0]; const results = await events.dispatchEvent( @@ -34,7 +37,7 @@ async function publish(pageName: string, uris: string[]) { name: pageName, } as PublishEvent, ); - if (results.length === 0) { + if (broadcastResults.length === 0 && results.length === 0) { throw new Error(`Unsupported publisher: ${publisher} for URI: ${uri}`); } } diff --git a/server/http_server.ts b/server/http_server.ts index adccfbb7..a198790f 100644 --- a/server/http_server.ts +++ b/server/http_server.ts @@ -505,7 +505,6 @@ export class HttpServer { return next(); } } - console.log("Requested path to proxy", url, request.method); if (url.startsWith("localhost")) { url = `http://${url}`; } else { @@ -574,3 +573,11 @@ function utcDateString(mtime: number): string { function authCookieName(host: string) { return `auth:${host}`; } + +function headersToJson(headers: Headers) { + let headersObj: any = {}; + for (const [key, value] of headers.entries()) { + headersObj[key] = value; + } + return JSON.stringify(headersObj); +}