@weborigami/origami 0.0.58 → 0.0.59

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,9 @@
1
1
  /** @typedef {import("@weborigami/types").AsyncTree} AsyncTree */
2
- import { keyFunctionsForExtensions, map } from "@weborigami/async-tree";
2
+ import {
3
+ keyFunctionsForExtensions,
4
+ map,
5
+ toString,
6
+ } from "@weborigami/async-tree";
3
7
  import fileTypeOrigami from "../src/builtins/ori_handler.js";
4
8
  import { transformObject } from "../src/common/utilities.js";
5
9
  import PathTransform from "./PathTransform.js";
@@ -32,7 +36,7 @@ export default async function exportFile(src) {
32
36
  * @this {AsyncTree}
33
37
  */
34
38
  async function exportStatementForCode(codeBuffer, key) {
35
- const code = String(codeBuffer);
39
+ const code = toString(codeBuffer);
36
40
 
37
41
  const exportsAnything = code.match(/^export /m);
38
42
  if (!exportsAnything) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@weborigami/origami",
3
- "version": "0.0.58",
3
+ "version": "0.0.59",
4
4
  "description": "Web Origami language, CLI, framework, and server",
5
5
  "type": "module",
6
6
  "repository": {
@@ -13,22 +13,22 @@
13
13
  "main": "./exports/exports.js",
14
14
  "types": "./index.ts",
15
15
  "devDependencies": {
16
- "@types/node": "20.12.7",
17
- "typescript": "5.4.5"
16
+ "@types/node": "20.14.9",
17
+ "typescript": "5.5.3"
18
18
  },
19
19
  "dependencies": {
20
- "@weborigami/async-tree": "0.0.58",
21
- "@weborigami/language": "0.0.58",
22
- "@weborigami/types": "0.0.58",
20
+ "@weborigami/async-tree": "0.0.59",
21
+ "@weborigami/language": "0.0.59",
22
+ "@weborigami/types": "0.0.59",
23
23
  "exif-parser": "0.1.12",
24
24
  "graphviz-wasm": "3.0.2",
25
25
  "highlight.js": "11.9.0",
26
- "marked": "12.0.2",
27
- "marked-gfm-heading-id": "3.1.3",
28
- "marked-highlight": "2.1.1",
29
- "marked-smartypants": "1.1.6",
30
- "sharp": "0.33.3",
31
- "yaml": "2.4.2"
26
+ "marked": "13.0.1",
27
+ "marked-gfm-heading-id": "4.0.0",
28
+ "marked-highlight": "2.1.3",
29
+ "marked-smartypants": "1.1.7",
30
+ "sharp": "0.33.4",
31
+ "yaml": "2.4.5"
32
32
  },
33
33
  "scripts": {
34
34
  "build": "ori exports/buildExports.js src > exports/exports.js",
@@ -7,12 +7,12 @@ import imageFormatFn from "./formatFn.js";
7
7
  * @this {import("@weborigami/types").AsyncTree|null}
8
8
  *
9
9
  * @this {import("@weborigami/types").AsyncTree|null}
10
- * @param {Buffer} buffer
10
+ * @param {import("@weborigami/async-tree").Packed} input
11
11
  * @param {keyof import("sharp").FormatEnum|import("sharp").AvailableFormatInfo}
12
12
  * format
13
13
  * @param {any} options
14
14
  */
15
- export default async function imageFormat(buffer, format, options) {
15
+ export default async function imageFormat(input, format, options) {
16
16
  assertScopeIsDefined(this, "image/format");
17
- return imageFormatFn.call(this, format, options)(buffer);
17
+ return imageFormatFn.call(this, format, options)(input);
18
18
  }
@@ -5,10 +5,10 @@ import imageResizeFn from "./resizeFn.js";
5
5
  * Resize an image.
6
6
  *
7
7
  * @this {import("@weborigami/types").AsyncTree|null}
8
- * @param {Buffer} buffer
8
+ * @param {import("@weborigami/async-tree").Packed} input
9
9
  * @param {import("sharp").ResizeOptions} options
10
10
  */
11
- export default async function resize(buffer, options) {
11
+ export default async function resize(input, options) {
12
12
  assertScopeIsDefined(this, "image/resize");
13
- return imageResizeFn.call(this, options)(buffer);
13
+ return imageResizeFn.call(this, options)(input);
14
14
  }
@@ -1,6 +1,5 @@
1
1
  /** @typedef {import("@weborigami/types").AsyncTree} AsyncTree */
2
- import { isUnpackable } from "@weborigami/async-tree";
3
- import * as serialize from "../common/serialize.js";
2
+ import { isUnpackable, toPlainValue } from "@weborigami/async-tree";
4
3
  import assertScopeIsDefined from "../misc/assertScopeIsDefined.js";
5
4
 
6
5
  /**
@@ -24,7 +23,7 @@ export default async function json(obj) {
24
23
  if (isUnpackable(obj)) {
25
24
  obj = await obj.unpack();
26
25
  }
27
- const value = await serialize.toJsonValue(obj);
26
+ const value = await toPlainValue(obj);
28
27
  return JSON.stringify(value, null, 2);
29
28
  }
30
29
 
@@ -20,7 +20,8 @@ export default async function ori(
20
20
  options = { formatResult: true }
21
21
  ) {
22
22
  assertScopeIsDefined(this, "ori");
23
- // In case expression is a Buffer, cast it to a string.
23
+
24
+ // In case expression has come from a file, cast it to a string.
24
25
  expression = String(expression);
25
26
 
26
27
  // Obtain the scope from `this` or builtins.
@@ -14,5 +14,6 @@ export default async function perf(fn, count = 1) {
14
14
  await fn.call(this);
15
15
  }
16
16
  const end = performance.now();
17
- return end - start;
17
+ const milliseconds = Math.round(end - start);
18
+ return `${milliseconds} ms`;
18
19
  }
@@ -16,7 +16,13 @@ function readAll(readable) {
16
16
  });
17
17
 
18
18
  readable.on("end", () => {
19
- const buffer = Buffer.concat(chunks);
19
+ const size = chunks.reduce((acc, chunk) => acc + chunk.length, 0);
20
+ const buffer = new Uint8Array(size);
21
+ let offset = 0;
22
+ for (const chunk of chunks) {
23
+ buffer.set(chunk, offset);
24
+ offset += chunk.length;
25
+ }
20
26
  resolve(buffer);
21
27
  });
22
28
  });
@@ -1,7 +1,7 @@
1
1
  import assertScopeIsDefined from "../misc/assertScopeIsDefined.js";
2
2
 
3
3
  /**
4
- * Unpack a packed format like a Buffer or ArrayBuffer to a usable form like
4
+ * Unpack a packed format like a Uint8Array or ArrayBuffer to a usable form like
5
5
  * text or a plain JavaScript object.
6
6
  *
7
7
  * @typedef {import("@weborigami/types").AsyncTree} AsyncTree
@@ -1,7 +1,6 @@
1
1
  /** @typedef {import("@weborigami/types").AsyncTree} AsyncTree */
2
- import { isUnpackable } from "@weborigami/async-tree";
2
+ import { isUnpackable, toPlainValue } from "@weborigami/async-tree";
3
3
  import YAML from "yaml";
4
- import * as serialize from "../common/serialize.js";
5
4
  import assertScopeIsDefined from "../misc/assertScopeIsDefined.js";
6
5
 
7
6
  /**
@@ -25,7 +24,7 @@ export default async function toYaml(obj) {
25
24
  if (isUnpackable(obj)) {
26
25
  obj = await obj.unpack();
27
26
  }
28
- const value = await serialize.toJsonValue(obj);
27
+ const value = await toPlainValue(obj);
29
28
  return YAML.stringify(value);
30
29
  }
31
30
 
@@ -17,6 +17,10 @@ export default {
17
17
 
18
18
  /** @type {import("@weborigami/language").UnpackFunction} */
19
19
  async unpack(packed, options) {
20
+ if (packed instanceof Uint8Array) {
21
+ // Downgrade to old Node Buffer for exif-parser.
22
+ packed = Buffer.from(packed);
23
+ }
20
24
  const parser = exifParser.create(packed);
21
25
  parser.enableTagNames(true);
22
26
  parser.enableSimpleValues(true);
@@ -4,6 +4,4 @@ import type { JsonValue } from "../../index.ts";
4
4
  export function evaluateYaml(text: string, parent?: AsyncTree|null): Promise<JsonValue>;
5
5
  export function parseYaml(text: string): JsonValue|AsyncTree;
6
6
  export function toJson(obj: JsonValue | AsyncTree): Promise<string>;
7
- export function toJsonValue(obj: any): Promise<JsonValue>;
8
- export function toValue(obj: any, jsonValuesOnly?: boolean): Promise<JsonValue>;
9
7
  export function toYaml(obj: JsonValue | AsyncTree): Promise<string>;
@@ -5,12 +5,9 @@
5
5
  * @typedef {import("@weborigami/types").AsyncTree} AsyncTree
6
6
  */
7
7
 
8
- import { Tree, isStringLike } from "@weborigami/async-tree";
8
+ import { Tree, toPlainValue } from "@weborigami/async-tree";
9
9
  import * as YAMLModule from "yaml";
10
10
 
11
- const textDecoder = new TextDecoder();
12
- const TypedArray = Object.getPrototypeOf(Uint8Array);
13
-
14
11
  // The "yaml" package doesn't seem to provide a default export that the browser can
15
12
  // recognize, so we have to handle two ways to accommodate Node and the browser.
16
13
  // @ts-ignore
@@ -31,21 +28,6 @@ export async function evaluateYaml(text, parent) {
31
28
  }
32
29
  }
33
30
 
34
- /**
35
- * @param {any} obj
36
- * @returns {obj is JsonValue}
37
- */
38
- function isJsonValue(obj) {
39
- const t = typeof obj;
40
- return (
41
- t === "boolean" ||
42
- t === "number" ||
43
- t === "string" ||
44
- obj instanceof Date ||
45
- obj === null
46
- );
47
- }
48
-
49
31
  /**
50
32
  * @param {string} text
51
33
  * @returns {JsonValue|AsyncTree}
@@ -60,79 +42,10 @@ export function parseYaml(text) {
60
42
  * @param {any} obj
61
43
  */
62
44
  export async function toJson(obj) {
63
- const serializable = await toJsonValue(obj);
45
+ const serializable = await toPlainValue(obj);
64
46
  return JSON.stringify(serializable, null, 2);
65
47
  }
66
48
 
67
- /**
68
- * Convert the given object to a corresponding JSON value that can be
69
- * represented as JSON or YAML.
70
- *
71
- * @param {any} object
72
- * @returns {Promise<JsonValue>}
73
- */
74
- export async function toJsonValue(object) {
75
- return toValue(object, true);
76
- }
77
-
78
- /**
79
- * Convert the given input to the plainest possible JavaScript value. This
80
- * helper is intended for functions that want to accept an argument from the ori
81
- * CLI, which could a string, a file buffer, an ArrayBuffer from a URL, or some
82
- * other kind of JavaScript object.
83
- *
84
- * If the input implements the `unpack()` method, the input will be unpacked and
85
- * before processing.
86
- *
87
- * If the input is treelike, it will be converted to a plain JavaScript object,
88
- * recursively traversing the tree and converting all values to plain types.
89
- *
90
- * If the input is stringlike, its text will be returned.
91
- *
92
- * If the input is a Buffer or ArrayBuffer, it will be interpreted as UTF-8
93
- * text.
94
- *
95
- * If the input has a custom class instance, its public properties will be
96
- * returned as a plain object.
97
- *
98
- * The `jsonValuesOnly` parameter can be set to `true` to ensure that the
99
- * returned value can be represented as JSON. If the input can't be represented
100
- * as JSON, an error is thrown.
101
- *
102
- * @param {any} input
103
- * @param {boolean} [jsonValuesOnly]
104
- * @returns {Promise<any>}
105
- */
106
- export async function toValue(input, jsonValuesOnly = false) {
107
- if (input instanceof Promise) {
108
- // Resolve promise before processing.
109
- return toValue(await input, jsonValuesOnly);
110
- } else if (isJsonValue(input)) {
111
- return input;
112
- } else if (typeof input !== "object") {
113
- if (jsonValuesOnly) {
114
- throw new TypeError(`Couldn't serialize value to JSON: ${input}`);
115
- } else {
116
- return input;
117
- }
118
- } else if (isStringLike(input) && !(input instanceof Array)) {
119
- return String(input);
120
- } else if (Tree.isTreelike(input)) {
121
- const mapped = await Tree.map(input, (value) => toValue(value));
122
- return Tree.plain(mapped);
123
- } else if (input instanceof ArrayBuffer || input instanceof TypedArray) {
124
- // Interpret input as UTF-8 text.
125
- return textDecoder.decode(input);
126
- } else {
127
- // Some other kind of class instance; return its public properties.
128
- const plain = {};
129
- for (const [key, value] of Object.entries(input)) {
130
- plain[key] = await toValue(value);
131
- }
132
- return plain;
133
- }
134
- }
135
-
136
49
  /**
137
50
  * Serializes an object as a JSON string.
138
51
  *
@@ -140,6 +53,6 @@ export async function toValue(input, jsonValuesOnly = false) {
140
53
  * @returns {Promise<string>}
141
54
  */
142
55
  export async function toYaml(obj) {
143
- const serializable = await toJsonValue(obj);
56
+ const serializable = await toPlainValue(obj);
144
57
  return YAML.stringify(serializable);
145
58
  }
@@ -62,7 +62,7 @@ export default async function constructResponse(request, resource) {
62
62
  !isStringLike(resource)
63
63
  ) {
64
64
  // The request is for a JSON or YAML result, and the resource we got isn't
65
- // yet a string or Buffer: convert the resource to JSON or YAML now.
65
+ // yet a string: convert the resource to JSON or YAML now.
66
66
  const tree = Tree.from(resource);
67
67
  resource =
68
68
  mediaType === "text/yaml"
@@ -103,7 +103,7 @@ export default async function constructResponse(request, resource) {
103
103
  if (!validResponse) {
104
104
  const typeName = body?.constructor?.name ?? typeof body;
105
105
  console.error(
106
- `A served tree must return a string or a TypedArray (such as a Buffer) but returned an instance of ${typeName}.`
106
+ `A served tree must return a string or a TypedArray but returned an instance of ${typeName}.`
107
107
  );
108
108
  return null;
109
109
  }
@@ -123,8 +123,8 @@ export default async function constructResponse(request, resource) {
123
123
  */
124
124
  function textOrObject(object) {
125
125
  if (object instanceof ArrayBuffer) {
126
- // Convert to Buffer.
127
- return Buffer.from(object);
126
+ // Convert to Uint8Array so we can write it to the Response.
127
+ return new Uint8Array(object);
128
128
  } else if (object instanceof TypedArray) {
129
129
  // Return typed arrays as is.
130
130
  return object;