@vite-hub/source 0.0.4 → 0.1.0

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.
Files changed (48) hide show
  1. package/README.md +219 -82
  2. package/dist/{auth-Ddw55OUG.js → auth-B-tX_rp0.js} +255 -90
  3. package/dist/{client-CDfTmtQu.js → client-BwstxtdY.js} +584 -207
  4. package/dist/client.d.ts +27 -10
  5. package/dist/client.js +71 -13
  6. package/dist/collection-B_4w_J8h.d.ts +257 -0
  7. package/dist/{collection-B8pXGM2d.js → collection-cursor-WzV1Jd6v.js} +37 -54
  8. package/dist/collection-route-BplWPxHR.js +10 -0
  9. package/dist/drizzle.d.ts +41 -0
  10. package/dist/drizzle.js +89 -0
  11. package/dist/error-diagnostics-X0kuFjBT.js +51 -0
  12. package/dist/file-BX0FSvgE.js +60 -0
  13. package/dist/{file-EQ3AjVM6.d.ts → file-DadN9dbN.d.ts} +2 -3
  14. package/dist/file.d.ts +1 -1
  15. package/dist/file.js +1 -1
  16. package/dist/files.d.ts +15 -0
  17. package/dist/files.js +38 -0
  18. package/dist/github.d.ts +8 -5
  19. package/dist/github.js +725 -426
  20. package/dist/glob-BmFqTvJY.js +993 -0
  21. package/dist/glob.d.ts +12 -4
  22. package/dist/glob.js +1 -894
  23. package/dist/index.d.ts +118 -27
  24. package/dist/index.js +623 -14
  25. package/dist/markdown.d.ts +4 -6
  26. package/dist/markdown.js +2 -2
  27. package/dist/mcp.d.ts +3 -4
  28. package/dist/mcp.js +302 -5920
  29. package/dist/{file-BSWQg9Qv.js → mrmime-40h5OOIp.js} +1 -56
  30. package/dist/{path-CUhvUREm.js → path-BS8rYm0n.js} +60 -40
  31. package/dist/{path-BzmU4Eni.js → path-DOBpV8ol.js} +3 -7
  32. package/dist/server.d.ts +256 -6
  33. package/dist/server.js +90 -12
  34. package/dist/{sse-CE9Q2JqM.js → sse-D9gqqNPj.js} +50 -9
  35. package/dist/{streamableHttp-BszaXLmg.js → streamableHttp-BL-E7pl5.js} +40 -22
  36. package/dist/types-BigOktIj.d.ts +93 -0
  37. package/dist/{types-AIBmt_m6.js → types-CoWcW8oz.js} +2502 -1187
  38. package/dist/vite.d.ts +134 -13
  39. package/dist/vite.js +1734 -79
  40. package/package.json +55 -36
  41. package/dist/collection-CF2ON__k.d.ts +0 -92
  42. package/dist/collection-route-Dw7QLFyx.js +0 -6
  43. package/dist/content/client.d.ts +0 -3
  44. package/dist/content/client.js +0 -2
  45. package/dist/content.d.ts +0 -44
  46. package/dist/content.js +0 -157
  47. package/dist/registry-C8skfCSV.js +0 -120
  48. package/dist/types-Bu3WvNX9.d.ts +0 -71
package/package.json CHANGED
@@ -1,15 +1,17 @@
1
1
  {
2
2
  "name": "@vite-hub/source",
3
- "version": "0.0.4",
3
+ "version": "0.1.0",
4
4
  "description": "Typed source retrieval primitives for ViteHub.",
5
5
  "keywords": [
6
6
  "content",
7
7
  "files",
8
8
  "github",
9
+ "retrieval",
9
10
  "source",
11
+ "typescript",
10
12
  "vitehub"
11
13
  ],
12
- "homepage": "https://github.com/vite-hub/vitehub#readme",
14
+ "homepage": "https://vitehub.dev",
13
15
  "bugs": "https://github.com/vite-hub/vitehub/issues",
14
16
  "license": "Apache-2.0",
15
17
  "author": "onmax <maximogarciamtnez@gmail.com>",
@@ -30,84 +32,101 @@
30
32
  "exports": {
31
33
  ".": {
32
34
  "types": "./dist/index.d.ts",
33
- "import": "./dist/index.js"
35
+ "import": "./dist/index.js",
36
+ "default": "./dist/index.js"
34
37
  },
35
38
  "./client": {
36
39
  "types": "./dist/client.d.ts",
37
- "import": "./dist/client.js"
40
+ "import": "./dist/client.js",
41
+ "default": "./dist/client.js"
38
42
  },
39
- "./content": {
40
- "types": "./dist/content.d.ts",
41
- "import": "./dist/content.js"
42
- },
43
- "./content/client": {
44
- "types": "./dist/content/client.d.ts",
45
- "import": "./dist/content/client.js"
43
+ "./drizzle": {
44
+ "types": "./dist/drizzle.d.ts",
45
+ "import": "./dist/drizzle.js",
46
+ "default": "./dist/drizzle.js"
46
47
  },
47
48
  "./file": {
48
49
  "types": "./dist/file.d.ts",
49
- "import": "./dist/file.js"
50
+ "import": "./dist/file.js",
51
+ "default": "./dist/file.js"
52
+ },
53
+ "./files": {
54
+ "types": "./dist/files.d.ts",
55
+ "import": "./dist/files.js",
56
+ "default": "./dist/files.js"
50
57
  },
51
58
  "./github": {
52
59
  "types": "./dist/github.d.ts",
53
- "import": "./dist/github.js"
60
+ "import": "./dist/github.js",
61
+ "default": "./dist/github.js"
54
62
  },
55
63
  "./glob": {
56
64
  "types": "./dist/glob.d.ts",
57
- "import": "./dist/glob.js"
65
+ "import": "./dist/glob.js",
66
+ "default": "./dist/glob.js"
58
67
  },
59
68
  "./markdown": {
60
69
  "types": "./dist/markdown.d.ts",
61
- "import": "./dist/markdown.js"
70
+ "import": "./dist/markdown.js",
71
+ "default": "./dist/markdown.js"
62
72
  },
63
73
  "./mcp": {
64
74
  "types": "./dist/mcp.d.ts",
65
- "import": "./dist/mcp.js"
75
+ "import": "./dist/mcp.js",
76
+ "default": "./dist/mcp.js"
66
77
  },
67
78
  "./server": {
68
79
  "types": "./dist/server.d.ts",
69
- "import": "./dist/server.js"
80
+ "import": "./dist/server.js",
81
+ "default": "./dist/server.js"
70
82
  },
71
83
  "./vite": {
72
84
  "types": "./dist/vite.d.ts",
73
- "import": "./dist/vite.js"
85
+ "import": "./dist/vite.js",
86
+ "default": "./dist/vite.js"
74
87
  },
75
88
  "./package.json": "./package.json",
76
89
  "./tsconfig": "./tsconfig.vite.json"
77
90
  },
78
91
  "dependencies": {
79
92
  "@standard-schema/spec": "^1.1.0",
80
- "@vite-hub/runtime": "0.0.4",
81
- "effect": "4.0.0-beta.99",
82
- "h3": "2.0.1-rc.25",
93
+ "@vite-hub/forge": "0.1.0",
94
+ "@vite-hub/runtime": "0.1.0",
95
+ "h3": "2.0.1-rc.33",
83
96
  "mlly": "^1.8.2",
97
+ "nostics": "1.3.0",
84
98
  "ofetch": "^1.5.1"
85
99
  },
86
100
  "devDependencies": {
87
- "@modelcontextprotocol/sdk": "^1.29.0",
88
- "@types/node": "^24.13.3",
101
+ "@modelcontextprotocol/sdk": "^1.32.1",
102
+ "@types/node": "^24.19.1",
89
103
  "@types/picomatch": "^4.0.3",
90
- "comark-content": "0.3.0",
104
+ "drizzle-orm": "^0.45.3",
91
105
  "mrmime": "^2.0.1",
92
- "ocache": "^0.1.5",
93
- "picomatch": "^4.0.5",
94
- "publint": "^0.3.21",
106
+ "nitro": "3.0.260903-beta",
107
+ "ocache": "^0.3.0",
108
+ "picomatch": "^4.0.7",
109
+ "publint": "^0.3.25",
95
110
  "tinyglobby": "^0.2.17",
96
111
  "typescript": "^6.0.3",
97
- "vite": "^8.0.8",
98
- "valibot": "^1.4.2",
99
- "vite-plus": "^0.1.24",
100
- "vitest": "^4.1.4",
101
- "vue": "^3.5.0",
112
+ "vite": "^8.2.2",
113
+ "valibot": "^1.5.0",
114
+ "vite-plus": "^0.3.3",
115
+ "vitest": "^4.1.11",
116
+ "vue": "3.5.44",
102
117
  "@vite-hub/internal": "0.0.1"
103
118
  },
104
119
  "peerDependencies": {
105
- "comark-content": "0.3.0",
120
+ "drizzle-orm": "^0.45.3",
121
+ "nitro": "3.0.260903-beta",
106
122
  "vite": ">=8.0.0",
107
- "vue": "^3.5.0"
123
+ "vue": "3.5.44"
108
124
  },
109
125
  "peerDependenciesMeta": {
110
- "comark-content": {
126
+ "drizzle-orm": {
127
+ "optional": true
128
+ },
129
+ "nitro": {
111
130
  "optional": true
112
131
  },
113
132
  "vite": {
@@ -123,7 +142,7 @@
123
142
  "scripts": {
124
143
  "build": "vp pack",
125
144
  "dev": "vp pack --watch",
126
- "typecheck": "tsc --noEmit -p ./tsconfig.json",
145
+ "typecheck": "vp run --cache -t @vite-hub/source#build && node ../../test/package-typecheck.mjs --noEmit -p ./tsconfig.json",
127
146
  "test": "vp run -t @vite-hub/source#build && vp test"
128
147
  }
129
148
  }
@@ -1,92 +0,0 @@
1
- import { StandardSchemaV1 } from "@standard-schema/spec";
2
-
3
- //#region src/core/collection.d.ts
4
- declare const collectionQueryInput: unique symbol;
5
- type CollectionRequestQuery = Record<string, string | readonly string[] | undefined>;
6
- type CollectionCursorValue = boolean | null | number | string | readonly CollectionCursorValue[] | {
7
- readonly [key: string]: CollectionCursorValue;
8
- };
9
- interface CollectionLoadOptions<TQuery extends object, TCursor extends CollectionCursorValue> {
10
- cursor?: TCursor;
11
- limit: number;
12
- query: TQuery;
13
- signal?: AbortSignal;
14
- }
15
- interface CollectionPageOptions<TQuery extends object> {
16
- cursor?: string;
17
- limit?: number;
18
- query: TQuery;
19
- signal?: AbortSignal;
20
- }
21
- interface CollectionPage<TItem> {
22
- items: TItem[];
23
- nextCursor: string | null;
24
- }
25
- interface Collection<TItem, TQuery extends object = CollectionRequestQuery, TQueryInput extends object = TQuery> {
26
- readonly [collectionQueryInput]?: TQueryInput;
27
- page(options: CollectionPageOptions<TQuery>): Promise<CollectionPage<TItem>>;
28
- parseQuery(input: CollectionRequestQuery): Promise<TQuery>;
29
- }
30
- type AnyCollection = Collection<any, any, any>;
31
- type CollectionItem<TCollection extends AnyCollection> = TCollection extends Collection<infer TItem, any, any> ? TItem : never;
32
- type JSONOmitted = undefined | ((...args: any[]) => any) | symbol;
33
- type JSONUnsupportedObject = ArrayBuffer | ArrayBufferView | Date | Error | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown> | RegExp | SharedArrayBuffer | URL | WeakMap<object, unknown> | WeakSet<object>;
34
- type JSONOmittedBranch<T> = T extends {
35
- toJSON(): infer TJSON;
36
- } ? TJSON extends JSONOmitted ? TJSON : never : T extends JSONOmitted ? T : never;
37
- type JSONSerializedValue<T> = T extends {
38
- toJSON(): infer TJSON;
39
- } ? JSONSerializedPostToJSON<TJSON> : JSONSerializedPostToJSON<T>;
40
- type JSONSerializedPostToJSON<T> = T extends bigint | JSONUnsupportedObject ? never : T extends number ? number | null : T extends boolean | null | string ? T : T extends readonly (infer TItem)[] ? Array<JSONSerializedArrayItem<TItem>> : T extends object ? JSONSerializedObject<T> : never;
41
- type JSONSerialized<T> = T extends unknown ? JSONSerializedValue<T> : never;
42
- type JSONSerializedArrayItem<T> = T extends {
43
- toJSON(): infer TJSON;
44
- } ? JSONSerializedArrayValue<TJSON> : JSONSerializedArrayValue<T>;
45
- type JSONSerializedArrayValue<T> = T extends JSONOmitted ? null : JSONSerializedPostToJSON<T>;
46
- type Simplify<T> = { [TKey in keyof T]: T[TKey] };
47
- type JSONSerializedObject<T extends object> = Simplify<{ [TKey in keyof T as TKey extends symbol ? never : [JSONOmittedBranch<T[TKey]>] extends [never] ? TKey : never]: JSONSerialized<T[TKey]> } & { [TKey in keyof T as TKey extends symbol ? never : [JSONOmittedBranch<T[TKey]>] extends [never] ? never : [JSONSerialized<T[TKey]>] extends [never] ? never : TKey]?: JSONSerialized<T[TKey]> }>;
48
- type CollectionClientItem<TCollection extends AnyCollection> = JSONSerializedArrayItem<CollectionItem<TCollection>>;
49
- type CollectionQuery<TCollection extends AnyCollection> = TCollection extends Collection<any, any, infer TQueryInput> ? TQueryInput : never;
50
- type CollectionLoader<TSourceItem, TQuery extends object, TCursor extends CollectionCursorValue> = (options: CollectionLoadOptions<TQuery, TCursor>) => Promise<readonly TSourceItem[]>;
51
- type CollectionTransform<TSourceItem> = (item: NoInfer<TSourceItem>) => unknown;
52
- interface CollectionOptions<TSourceItem, TQuery extends object, TCursorInput extends CollectionCursorValue, TCursorOutput extends CollectionCursorValue = TCursorInput> {
53
- cursor(item: NoInfer<TSourceItem>): Readonly<TCursorInput>;
54
- cursorSchema: StandardSchemaV1<TCursorInput, TCursorOutput>;
55
- defaultLimit?: number;
56
- maxLimit?: number;
57
- querySchema?: StandardSchemaV1<unknown, TQuery>;
58
- transform?: CollectionTransform<TSourceItem>;
59
- }
60
- type CollectionDefinition<TSourceItem, TQuery extends object, TCursorInput extends CollectionCursorValue> = Omit<CollectionOptions<TSourceItem, TQuery, TCursorInput>, "cursorSchema" | "querySchema" | "transform">;
61
- type CursorInput<TSchema extends StandardSchemaV1> = [StandardSchemaV1.InferInput<TSchema>] extends [CollectionCursorValue] ? StandardSchemaV1.InferInput<TSchema> : never;
62
- type CursorOutput<TSchema extends StandardSchemaV1> = [StandardSchemaV1.InferOutput<TSchema>] extends [CollectionCursorValue] ? StandardSchemaV1.InferOutput<TSchema> : never;
63
- type RepeatedQueryValues = [string, string, ...string[]];
64
- type AmbiguousArrayQueryKey<TInput extends object> = { [TKey in keyof TInput]-?: [Extract<TInput[TKey], readonly string[]>] extends [never] ? never : RepeatedQueryValues extends TInput[TKey] ? string extends TInput[TKey] ? undefined extends TInput[TKey] ? never : TKey : TKey : TKey }[keyof TInput];
65
- type ReservedCollectionQueryKey = "cursor" | "limit";
66
- type CollectionQueryInput<TInput> = TInput extends object ? [Exclude<keyof TInput, string>] extends [never] ? [Extract<ReservedCollectionQueryKey, keyof TInput>] extends [never] ? [TInput[keyof TInput]] extends [CollectionRequestQuery[string]] ? [AmbiguousArrayQueryKey<TInput>] extends [never] ? TInput : never : never : never : never : never;
67
- type QueryInput<TSchema extends StandardSchemaV1> = CollectionQueryInput<StandardSchemaV1.InferInput<TSchema>>;
68
- declare class CollectionCursorError extends TypeError {
69
- constructor(message?: string, options?: ErrorOptions);
70
- }
71
- declare function defineCollection<TSourceItem, TCursorSchema extends StandardSchemaV1, TQuerySchema extends StandardSchemaV1<unknown, object>, TTransform extends CollectionTransform<TSourceItem>>(load: CollectionLoader<TSourceItem, StandardSchemaV1.InferOutput<TQuerySchema>, CursorOutput<TCursorSchema>>, options: CollectionDefinition<TSourceItem, StandardSchemaV1.InferOutput<TQuerySchema>, CursorInput<TCursorSchema>> & {
72
- cursorSchema: TCursorSchema;
73
- querySchema: TQuerySchema;
74
- transform: TTransform;
75
- }): Collection<Awaited<ReturnType<TTransform>>, StandardSchemaV1.InferOutput<TQuerySchema>, QueryInput<TQuerySchema>>;
76
- declare function defineCollection<TSourceItem, TCursorSchema extends StandardSchemaV1, TQuerySchema extends StandardSchemaV1<unknown, object>>(load: CollectionLoader<TSourceItem, StandardSchemaV1.InferOutput<TQuerySchema>, CursorOutput<TCursorSchema>>, options: CollectionDefinition<TSourceItem, StandardSchemaV1.InferOutput<TQuerySchema>, CursorInput<TCursorSchema>> & {
77
- cursorSchema: TCursorSchema;
78
- querySchema: TQuerySchema;
79
- transform?: undefined;
80
- }): Collection<TSourceItem, StandardSchemaV1.InferOutput<TQuerySchema>, QueryInput<TQuerySchema>>;
81
- declare function defineCollection<TSourceItem, TCursorSchema extends StandardSchemaV1, TTransform extends CollectionTransform<TSourceItem>>(load: CollectionLoader<TSourceItem, CollectionRequestQuery, CursorOutput<TCursorSchema>>, options: CollectionDefinition<TSourceItem, CollectionRequestQuery, CursorInput<TCursorSchema>> & {
82
- cursorSchema: TCursorSchema;
83
- querySchema?: undefined;
84
- transform: TTransform;
85
- }): Collection<Awaited<ReturnType<TTransform>>, CollectionRequestQuery, CollectionRequestQuery>;
86
- declare function defineCollection<TSourceItem, TCursorSchema extends StandardSchemaV1>(load: CollectionLoader<TSourceItem, CollectionRequestQuery, CursorOutput<TCursorSchema>>, options: CollectionDefinition<TSourceItem, CollectionRequestQuery, CursorInput<TCursorSchema>> & {
87
- cursorSchema: TCursorSchema;
88
- querySchema?: undefined;
89
- transform?: undefined;
90
- }): Collection<TSourceItem, CollectionRequestQuery, CollectionRequestQuery>;
91
- //#endregion
92
- export { CollectionCursorValue as a, CollectionLoader as c, CollectionPageOptions as d, CollectionQuery as f, defineCollection as h, CollectionCursorError as i, CollectionOptions as l, CollectionRequestQuery as m, Collection as n, CollectionItem as o, CollectionQueryInput as p, CollectionClientItem as r, CollectionLoadOptions as s, AnyCollection as t, CollectionPage as u };
@@ -1,6 +0,0 @@
1
- //#region src/internal/collection-route.ts
2
- function encodeCollectionRouteSegment(segment) {
3
- return encodeURIComponent(segment).replaceAll("*", "%2A");
4
- }
5
- //#endregion
6
- export { encodeCollectionRouteSegment as t };
@@ -1,3 +0,0 @@
1
- import { createContentClient, defineContentClientPlugin } from "comark-content/client";
2
- export type * from "comark-content/client";
3
- export { createContentClient, defineContentClientPlugin };
@@ -1,2 +0,0 @@
1
- import { createContentClient, defineContentClientPlugin } from "comark-content/client";
2
- export { createContentClient, defineContentClientPlugin };
package/dist/content.d.ts DELETED
@@ -1,44 +0,0 @@
1
- import { m as SourceReader, p as SourceName } from "./types-Bu3WvNX9.js";
2
- import { ComarkContent, ContentOptions, ContentPlugin, JsonSchema, Source } from "comark-content";
3
-
4
- //#region src/content.d.ts
5
- interface ContentHandler {
6
- (event: unknown): Promise<unknown>;
7
- fetch(input: Request | URL | string): Promise<Response>;
8
- }
9
- interface ContentHandlerEvent {
10
- method?: string;
11
- node?: {
12
- req: NodeContentRequest;
13
- };
14
- req: Request | NodeContentRequest;
15
- }
16
- interface ContentSourceOptions {
17
- prefix?: string;
18
- schema?: JsonSchema;
19
- }
20
- type NodeContentRequest = {
21
- aborted: boolean;
22
- headers: Record<string, string | string[] | undefined>;
23
- method?: string;
24
- url?: string;
25
- [Symbol.asyncIterator](): AsyncIterator<Uint8Array>;
26
- once(event: "aborted", listener: () => void): unknown;
27
- };
28
- type ContentSourceInput = SourceName | SourceReader | (() => SourceReader) | Source;
29
- type PluginMethods<TPlugin> = TPlugin extends ContentPlugin<infer TMethods, any> ? TMethods : unknown;
30
- type UnionToIntersection<T> = (T extends unknown ? (value: T) => void : never) extends ((value: infer TIntersection) => void) ? TIntersection : never;
31
- type ContentMethods<TPlugins extends ReadonlyArray<ContentPlugin<any, any>>> = TPlugins["length"] extends 0 ? unknown : UnionToIntersection<PluginMethods<TPlugins[number]>>;
32
- type DefineContentOptions<TPlugins extends ReadonlyArray<ContentPlugin<any, any>> = ReadonlyArray<ContentPlugin<any, any>>> = Omit<ContentOptions, "basePath" | "plugins" | "source" | "sources"> & {
33
- plugins?: TPlugins;
34
- source?: ContentSourceInput;
35
- sources?: Record<string, ContentSourceInput>;
36
- };
37
- /** Adapt a registered ViteHub Source or Source Reader to the interface consumed by Comark Content. */
38
- declare function contentSource(input: ContentSourceInput, options?: ContentSourceOptions): Source;
39
- /** Define the Comark Content runtime served by ViteHub from `server/content.ts`. */
40
- declare function defineContent<const TPlugins extends ReadonlyArray<ContentPlugin<any, any>> = []>(options?: DefineContentOptions<TPlugins>): ComarkContent & ContentMethods<TPlugins>;
41
- /** Adapt a Comark Content Web handler to an H3/Nitro route. */
42
- declare function defineContentHandler(content: Pick<ComarkContent, "handler">): ContentHandler;
43
- //#endregion
44
- export { ContentHandler, ContentHandlerEvent, ContentSourceInput, ContentSourceOptions, DefineContentOptions, contentSource, defineContent, defineContentHandler };
package/dist/content.js DELETED
@@ -1,157 +0,0 @@
1
- import { n as normalizeSafeSourcePath } from "./path-BzmU4Eni.js";
2
- import { a as useSource } from "./registry-C8skfCSV.js";
3
- import { comarkContent } from "comark-content";
4
- import { defineEventHandler } from "h3";
5
- //#region src/content.ts
6
- function contentPath(item) {
7
- return normalizeSafeSourcePath(item.path || item.key);
8
- }
9
- function textContent(item) {
10
- if (item.content instanceof Uint8Array) return new TextDecoder().decode(item.content);
11
- if (item.content !== void 0) return item.content;
12
- if (item.data !== void 0) {
13
- const serialized = JSON.stringify(item.data);
14
- if (serialized !== void 0) return serialized;
15
- }
16
- throw new TypeError(`[vitehub] contentSource() cannot read ${JSON.stringify(item.key)} as content.`);
17
- }
18
- function isRuntimeFunction(value) {
19
- if (value === null || Object(value) !== value) return false;
20
- try {
21
- Function.prototype.toString.call(value);
22
- return true;
23
- } catch {
24
- return false;
25
- }
26
- }
27
- function isRuntimeObject(value) {
28
- return value !== null && Object(value) === value && !isRuntimeFunction(value);
29
- }
30
- function isComarkContentSource(input) {
31
- return isRuntimeObject(input) && "getItem" in input && isRuntimeFunction(input.getItem) && "getItemRaw" in input && isRuntimeFunction(input.getItemRaw) && "keys" in input && isRuntimeFunction(input.keys);
32
- }
33
- function isSourceReaderFactory(input) {
34
- return input instanceof Function;
35
- }
36
- function isSourceName(input) {
37
- return !(input instanceof Object);
38
- }
39
- function configuredContentSource(input, options) {
40
- const source = {
41
- getItem: input.getItem.bind(input),
42
- getItemRaw: input.getItemRaw.bind(input),
43
- keys: input.keys.bind(input),
44
- prefix: options.prefix ?? input.prefix,
45
- schema: options.schema ?? input.schema
46
- };
47
- if (input.watch) source.watch = input.watch.bind(input);
48
- return source;
49
- }
50
- /** Adapt a registered ViteHub Source or Source Reader to the interface consumed by Comark Content. */
51
- function contentSource(input, options = {}) {
52
- if (isComarkContentSource(input)) return options.prefix === void 0 && options.schema === void 0 ? input : configuredContentSource(input, options);
53
- const sourceInput = input;
54
- const pendingLoads = [];
55
- let latestItems;
56
- async function loadItems() {
57
- const nextItems = /* @__PURE__ */ new Map();
58
- const currentReader = isSourceName(sourceInput) ? useSource(sourceInput) : isSourceReaderFactory(sourceInput) ? sourceInput() : sourceInput;
59
- for (const item of await currentReader.items()) {
60
- const path = contentPath(item);
61
- if (nextItems.has(path)) throw new TypeError(`[vitehub] contentSource() received duplicate content path ${JSON.stringify(path)}.`);
62
- nextItems.set(path, item);
63
- }
64
- return nextItems;
65
- }
66
- async function findItem(key) {
67
- const path = normalizeSafeSourcePath(key);
68
- const loadIndex = pendingLoads.findIndex((items) => items.has(path));
69
- if (loadIndex !== -1) {
70
- const items = pendingLoads[loadIndex];
71
- const item = items.get(path);
72
- items.delete(path);
73
- if (items.size === 0) pendingLoads.splice(loadIndex, 1);
74
- return item;
75
- }
76
- return (await loadItems()).get(path);
77
- }
78
- const source = {
79
- async keys() {
80
- const items = await loadItems();
81
- latestItems = items;
82
- const pendingItems = new Map(items);
83
- pendingLoads.push(pendingItems);
84
- setTimeout(() => {
85
- const loadIndex = pendingLoads.indexOf(pendingItems);
86
- if (loadIndex !== -1) pendingLoads.splice(loadIndex, 1);
87
- }, 0);
88
- return [...items.keys()];
89
- },
90
- async getItem(key) {
91
- const item = await findItem(key);
92
- if (!item) throw new TypeError(`[vitehub] contentSource() could not find ${JSON.stringify(key)}.`);
93
- return textContent(item);
94
- },
95
- async getItemRaw(key) {
96
- const path = normalizeSafeSourcePath(key);
97
- const item = (latestItems ?? await loadItems()).get(path);
98
- if (!item) return;
99
- return item.data ?? item.content;
100
- }
101
- };
102
- if (options.prefix !== void 0) source.prefix = options.prefix;
103
- if (options.schema !== void 0) source.schema = options.schema;
104
- return source;
105
- }
106
- /** Define the Comark Content runtime served by ViteHub from `server/content.ts`. */
107
- function defineContent(options = {}) {
108
- const source = options.source ? contentSource(options.source) : void 0;
109
- const sources = options.sources ? Object.fromEntries(Object.entries(options.sources).map(([name, input]) => [name, contentSource(input)])) : void 0;
110
- return comarkContent({
111
- ...options,
112
- basePath: "/api/content",
113
- source,
114
- sources
115
- });
116
- }
117
- /** Adapt a Comark Content Web handler to an H3/Nitro route. */
118
- function defineContentHandler(content) {
119
- return defineEventHandler(async (event) => {
120
- if (event.req instanceof Request) return await content.handler(event.req);
121
- if (!event.node) throw new TypeError("[vitehub] Content received an unsupported non-Web request.");
122
- const nodeRequest = event.node.req;
123
- const method = event.method || nodeRequest.method || "GET";
124
- const headers = new Headers();
125
- for (const [name, value] of Object.entries(nodeRequest.headers)) {
126
- if (value === void 0) continue;
127
- if (value instanceof Array) for (const entry of value) headers.append(name, entry);
128
- else headers.set(name, value);
129
- }
130
- const protocol = headers.get("x-forwarded-proto")?.split(",", 1)[0]?.trim() || "http";
131
- const host = headers.get("x-forwarded-host")?.split(",", 1)[0]?.trim() || headers.get("host") || "localhost";
132
- const url = new URL(nodeRequest.url || "/", `${protocol}://${host}`);
133
- let body;
134
- if (method !== "GET" && method !== "HEAD") {
135
- const chunks = [];
136
- for await (const chunk of nodeRequest) chunks.push(chunk);
137
- const length = chunks.reduce((total, chunk) => total + chunk.byteLength, 0);
138
- body = new Uint8Array(length);
139
- let offset = 0;
140
- for (const chunk of chunks) {
141
- body.set(chunk, offset);
142
- offset += chunk.byteLength;
143
- }
144
- }
145
- const abort = new AbortController();
146
- if (nodeRequest.aborted) abort.abort();
147
- else nodeRequest.once("aborted", () => abort.abort());
148
- return await content.handler(new Request(url, {
149
- body,
150
- headers,
151
- method,
152
- signal: abort.signal
153
- }));
154
- });
155
- }
156
- //#endregion
157
- export { contentSource, defineContent, defineContentHandler };
@@ -1,120 +0,0 @@
1
- import { a as sourceError, n as normalizeSafeSourcePath, o as sourceNotFoundError, r as normalizeSourcePath, t as decodeSourceContent } from "./path-BzmU4Eni.js";
2
- //#region src/core/registry.ts
3
- const sourceRegistry = /* @__PURE__ */ new Map();
4
- function registerSource(name, source) {
5
- sourceRegistry.set(name, source);
6
- return source;
7
- }
8
- function registerSources(sources) {
9
- for (const [name, source] of Object.entries(sources)) registerSource(name, source);
10
- return sources;
11
- }
12
- function getRegisteredSource(name) {
13
- const source = sourceRegistry.get(name);
14
- if (!source) throw sourceNotFoundError(name);
15
- return source;
16
- }
17
- function clearSources() {
18
- sourceRegistry.clear();
19
- }
20
- function createSourceContext(name, context = {}) {
21
- return {
22
- abortSignal: context.abortSignal,
23
- revision: context.revision,
24
- rootDir: context.rootDir || process.cwd(),
25
- source: context.source || name,
26
- sourceRootDir: context.sourceRootDir,
27
- workspace: context.workspace
28
- };
29
- }
30
- function createMissingContentError(key) {
31
- return sourceError(`[vitehub] Source item ${JSON.stringify(key)} does not provide readable content.`);
32
- }
33
- function isNestedUnder(path, prefix) {
34
- return !prefix || path === prefix || path.startsWith(`${prefix}/`);
35
- }
36
- function createDirectorySet(paths) {
37
- const directories = /* @__PURE__ */ new Set();
38
- for (const path of paths) {
39
- const segments = normalizeSourcePath(path).split("/").filter(Boolean);
40
- for (let index = 1; index < segments.length; index++) directories.add(segments.slice(0, index).join("/"));
41
- }
42
- return directories;
43
- }
44
- function useSource(name, context) {
45
- const source = getRegisteredSource(name);
46
- const ctx = createSourceContext(name, context);
47
- let revisionPromise;
48
- let preparePromise;
49
- async function revision() {
50
- if (ctx.revision) return ctx.revision;
51
- revisionPromise ||= source.resolveRevision?.(ctx) ?? Promise.resolve(void 0);
52
- const resolved = await revisionPromise;
53
- if (resolved) ctx.revision = resolved;
54
- return resolved;
55
- }
56
- async function ensurePrepared() {
57
- await revision();
58
- if (!source.prepare) return;
59
- preparePromise ||= source.prepare(ctx);
60
- await preparePromise;
61
- }
62
- async function keys() {
63
- await ensurePrepared();
64
- return await source.getKeys(ctx);
65
- }
66
- async function get(key) {
67
- await ensurePrepared();
68
- return await source.getItem(key, ctx);
69
- }
70
- return {
71
- revision,
72
- keys,
73
- get,
74
- async items() {
75
- await ensurePrepared();
76
- if (source.getItems) return await source.getItems(ctx);
77
- return await Promise.all((await keys()).map(get));
78
- },
79
- async read(key, options) {
80
- const item = await get(key);
81
- if (typeof item.content === "undefined") throw createMissingContentError(key);
82
- return decodeSourceContent(item.content, options);
83
- },
84
- async meta(key) {
85
- await ensurePrepared();
86
- return await source.getMeta?.(key, ctx);
87
- },
88
- async exists(key) {
89
- return (await keys()).includes(key);
90
- },
91
- async list(prefix = "") {
92
- await ensurePrepared();
93
- const normalizedPrefix = prefix ? normalizeSafeSourcePath(prefix, { allowEmpty: true }) : "";
94
- const sourceKeys = await keys();
95
- const directories = createDirectorySet(sourceKeys);
96
- const result = /* @__PURE__ */ new Map();
97
- for (const directory of directories) {
98
- if (directory === normalizedPrefix) continue;
99
- if (!isNestedUnder(directory, normalizedPrefix)) continue;
100
- if (normalizeSourcePath(directory.slice(normalizedPrefix.length)).replace(/^\//, "").includes("/")) continue;
101
- result.set(directory, {
102
- key: directory,
103
- type: "directory"
104
- });
105
- }
106
- for (const key of sourceKeys) {
107
- if (key === normalizedPrefix) continue;
108
- if (!isNestedUnder(key, normalizedPrefix)) continue;
109
- if (normalizeSourcePath(key.slice(normalizedPrefix.length)).replace(/^\//, "").includes("/")) continue;
110
- result.set(key, {
111
- key,
112
- type: "file"
113
- });
114
- }
115
- return [...result.values()].sort((left, right) => left.key.localeCompare(right.key));
116
- }
117
- };
118
- }
119
- //#endregion
120
- export { useSource as a, registerSources as i, getRegisteredSource as n, registerSource as r, clearSources as t };