@ricsam/quickjs-core 0.2.13 → 0.2.15

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.
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Coercion system for QuickJS class instances
3
+ *
4
+ * Provides a Zod-like API for detecting and extracting values from
5
+ * class instances, plain objects, and primitive values.
6
+ *
7
+ * This enables consistent handling of class instances across the codebase,
8
+ * reducing duplication and ensuring reliable type detection.
9
+ */
10
+ /**
11
+ * Result of a coercion attempt
12
+ */
13
+ export type CoercionResult<T> = {
14
+ success: true;
15
+ value: T;
16
+ } | {
17
+ success: false;
18
+ error: string;
19
+ };
20
+ /**
21
+ * A coercer that can transform unknown values into a specific type
22
+ */
23
+ export interface Coercer<T> {
24
+ /** The name of this coercer (for error messages) */
25
+ readonly name: string;
26
+ /** Attempt to coerce a value, returning a result object */
27
+ safeParse(value: unknown): CoercionResult<T>;
28
+ /** Coerce a value, throwing on failure */
29
+ parse(value: unknown): T;
30
+ /** Check if a value can be coerced without actually coercing */
31
+ is(value: unknown): boolean;
32
+ /** Create a new coercer with a fallback */
33
+ or<U>(other: Coercer<U>): Coercer<T | U>;
34
+ /** Transform the coerced value */
35
+ transform<U>(fn: (value: T) => U): Coercer<U>;
36
+ /** Make the coercer optional (allows undefined/null) */
37
+ optional(): Coercer<T | undefined>;
38
+ }
39
+ /**
40
+ * Create a base coercer from detection and extraction functions
41
+ */
42
+ export declare function createCoercer<T>(name: string, detect: (value: unknown) => boolean, extract: (value: unknown) => T): Coercer<T>;
43
+ /**
44
+ * Create a coercer for a QuickJS class instance
45
+ */
46
+ export declare function classCoercer<TState>(className: string): Coercer<TState>;
47
+ /**
48
+ * Coerce a value that could be a class instance OR have a specific shape
49
+ */
50
+ export declare function instanceOrShape<TState, TShape>(className: string, shapeCheck: (value: unknown) => value is TShape, shapeToState: (value: TShape) => TState): Coercer<TState>;
51
+ export interface URLCoerced {
52
+ href: string;
53
+ protocol: string;
54
+ host: string;
55
+ hostname: string;
56
+ port: string;
57
+ pathname: string;
58
+ search: string;
59
+ hash: string;
60
+ origin: string;
61
+ username: string;
62
+ password: string;
63
+ }
64
+ /** Coerce a value to URL data */
65
+ export declare const coerceURL: Coercer<URLCoerced>;
66
+ /** Get just the href string from a URL-like value */
67
+ export declare function coerceToURLString(value: unknown): string;
68
+ export interface HeadersCoerced {
69
+ headers: Map<string, string[]>;
70
+ }
71
+ /** Coerce a value to Headers data */
72
+ export declare const coerceHeaders: Coercer<HeadersCoerced>;
73
+ /** Coerce body to Uint8Array */
74
+ export declare function coerceBody(value: unknown): Uint8Array | null;
75
+ export interface RequestInitCoerced {
76
+ method?: string;
77
+ headersState?: HeadersCoerced;
78
+ body?: Uint8Array | null;
79
+ cache?: string;
80
+ credentials?: string;
81
+ destination?: string;
82
+ integrity?: string;
83
+ keepalive?: boolean;
84
+ mode?: string;
85
+ redirect?: string;
86
+ referrer?: string;
87
+ referrerPolicy?: string;
88
+ signal?: unknown;
89
+ }
90
+ /** Coerce a value to Request init data (for use as RequestInit second arg) */
91
+ export declare const coerceRequestInit: Coercer<RequestInitCoerced>;
92
+ export interface ResponseInitCoerced {
93
+ status?: number;
94
+ statusText?: string;
95
+ headersState?: HeadersCoerced;
96
+ }
97
+ /** Coerce a value to Response init data */
98
+ export declare const coerceResponseInit: Coercer<ResponseInitCoerced>;
@@ -36,8 +36,8 @@ export { marshal, isHandle, getHandleType } from "./marshal.ts";
36
36
  export { unmarshal, cleanupUnmarshaledHandles } from "./unmarshal.ts";
37
37
  export { defineFunction, defineAsyncFunction } from "./function-builder.ts";
38
38
  export { defineClass, createStateMap, getState, setState, getInstanceState, setInstanceState, getInstanceStateById, cleanupInstanceState, clearAllInstanceState, } from "./class-builder.ts";
39
- export { isDefineClassInstance, isInstanceOf, getClassInstanceState, getInstanceId, getClassName, } from "./class-helpers.ts";
40
- export type { DefineClassInstance } from "./class-helpers.ts";
39
+ export { isDefineClassInstance, isInstanceOf, getClassInstanceState, getInstanceId, getClassName, createClassTypeGuard, isUnmarshalledRequest, isUnmarshalledResponse, isUnmarshalledHeaders, isUnmarshalledFormData, } from "./class-helpers.ts";
40
+ export type { DefineClassInstance, TypedClassInstance } from "./class-helpers.ts";
41
41
  export { nextInstanceId, registerInstance, getInstanceMetadata, getInstanceClassName, cleanupInstanceState as cleanupInstanceStateById, } from "./instance-state.ts";
42
42
  export type { InstanceMetadata } from "./instance-state.ts";
43
43
  export { createReadableStream, consumeReadableStream, } from "./streams/readable-stream.ts";
@@ -46,5 +46,7 @@ export { createBlob } from "./blob.ts";
46
46
  export { createFile } from "./file.ts";
47
47
  export { createURLSearchParamsClass } from "./url-search-params.ts";
48
48
  export type { URLSearchParamsState } from "./url-search-params.ts";
49
- export { createURLClass, addURLSearchParamsGetter } from "./url.ts";
49
+ export { createURLClass, addURLSearchParamsLinkage, addURLSearchParamsGetter } from "./url.ts";
50
50
  export type { URLState } from "./url.ts";
51
+ export { createCoercer, classCoercer, instanceOrShape, coerceURL, coerceToURLString, coerceHeaders, coerceBody, coerceRequestInit, coerceResponseInit, } from "./coerce.ts";
52
+ export type { Coercer, CoercionResult, URLCoerced, HeadersCoerced, RequestInitCoerced, ResponseInitCoerced, } from "./coerce.ts";
@@ -26,5 +26,13 @@ export declare function createURLClass(context: QuickJSContext, stateMap: StateM
26
26
  /**
27
27
  * Add searchParams getter to URL prototype using evalCode
28
28
  * This must be called after both URL and URLSearchParams are registered as globals
29
+ *
30
+ * The getter creates a URLSearchParams instance on first access and wraps its
31
+ * mutating methods (set, append, delete, sort) to sync changes back to the URL.
32
+ * This eliminates cross-module coupling by keeping all sync logic in JavaScript.
33
+ */
34
+ export declare function addURLSearchParamsLinkage(context: QuickJSContext): void;
35
+ /**
36
+ * @deprecated Use addURLSearchParamsLinkage instead
29
37
  */
30
- export declare function addURLSearchParamsGetter(context: QuickJSContext): void;
38
+ export declare const addURLSearchParamsGetter: typeof addURLSearchParamsLinkage;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ricsam/quickjs-core",
3
- "version": "0.2.13",
3
+ "version": "0.2.15",
4
4
  "main": "./dist/cjs/index.cjs",
5
5
  "types": "./dist/types/index.d.ts",
6
6
  "exports": {