-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathindex.ts
More file actions
190 lines (179 loc) · 7.71 KB
/
Copy pathindex.ts
File metadata and controls
190 lines (179 loc) · 7.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Prevents server from starting in worker threads if this was directly imported from a non-server user thread
import workerThreads from 'node:worker_threads';
if (!workerThreads.isMainThread) {
// @ts-expect-error - Idk this has been here for a while. Types say its readonly, but that must not be true.
if (!workerThreads.workerData) workerThreads.workerData = {};
workerThreads.workerData.noServerStart = true;
}
// Regular exports (don't require the same initialization as the globals at the end of this file do)
export { RequestTarget } from './resources/RequestTarget.ts';
export { flushDatabases } from './resources/databases.ts';
export { getContext, getResponse, getUser } from './security/jsLoader.ts';
// Code-first schema authoring: declare a table as a TypeScript value; the returned
// handle is the live, registered table class with per-verb shapes inferred from the definition.
export { defineTable, types } from './resources/defineTable.ts';
// The per-method request contract. `defineResource`/`Resource.withSchema` type
// handlers from a runtime contract and feed validation + OpenAPI + MCP from one declaration; `t` and
// `schemaOf` are the built-in query/body vocabulary (both reduce to a JsonSchemaFragment).
export { defineResource, t, schemaOf, projectTableFragment } from './resources/defineResource.ts';
// Type only exports.
// Anything exported here will only be available as TypeScript types, not as values.
// For exporting values see below.
export type {
Query,
Context,
Session,
SourceContext,
SubscriptionRequest,
RequestTargetOrId,
ResourceInterface,
} from './resources/ResourceInterface.ts';
export type { User } from './security/user.ts';
export type { RecordObject } from './resources/RecordEncoder.ts';
export type { IterableEventQueue } from './resources/IterableEventQueue.ts';
export type { Table } from './resources/databases.ts';
export type { Attribute } from './resources/Table.ts';
// Code-first schema types: the table handle and field model. Per-verb record shapes are
// discoverable on the handle itself: (typeof Track)['$record' | '$insert' | '$upsert' | '$patch' | '$query'].
export type {
TableHandle,
Field,
DateField,
RelationField,
Shape,
Flags,
DefineTableOptions,
} from './resources/defineTable.ts';
// Request-contract types.
export type {
Contract,
VerbSchemas,
VerbName,
Schema,
SchemaSource,
Projection,
PathParams,
TypedTarget,
TypedSearchParams,
ImplFor,
SchemaClass,
} from './resources/defineResource.ts';
export type { Scope } from './components/Scope.ts';
export type {
ModelBackend,
ModelCapabilities,
DefineBackendSpec,
Capability,
ModelRouter,
RouteRequest,
EmbedOpts,
GenerateOpts,
GenerateInput,
GenerateResult,
GenerateChunk,
BackendOpts,
AccountingContext,
ModelCallResult,
TokenUsage,
Message,
ToolDef,
ToolCall,
ToolHandler,
ToolHandlerContext,
ToolTraceEntry,
ConversationAppender,
ConversationTurn,
} from './resources/models/types.ts';
export type { FilesOption, FilesOptionObject } from './components/deriveGlobOptions.ts';
export type { FileAndURLPathConfig } from './components/Component.ts';
export type { OptionsWatcher, Config, ConfigValue } from './components/OptionsWatcher.ts';
export type {
EntryHandler,
BaseEntry,
FileEntry,
EntryEvent,
AddFileEvent,
ChangeFileEvent,
UnlinkFileEvent,
FileEntryEvent,
AddDirectoryEvent,
UnlinkDirectoryEvent,
DirectoryEntryEvent,
} from './components/EntryHandler.ts';
// Globals and values
// This section is responsible for creating the CJS exports map (for static analysis)
// as well as defining the globals and values exports.
// The stuff exported here are actually functional pieces of code.
// Importantly, do not import any values directly.
// For example, `import { tables } from './resources/databases.ts';` is NOT OKAY!
// This breaks Harper's dynamic runtime assignment of exports
// You MUST import as a type and then use `export declare const` instead.
// This results in the types being written to dist/index.d.ts, but not dist/index.js
// And for my sanity please keep these alphabetically sorted so we can ensure nothing is missing.
import type { contentTypes as ContentTypesImport } from './server/serverHelpers/contentTypes.ts';
import type { createBlob as CreateBlobImport } from './resources/blob.ts';
import type { databases as DatabasesImport } from './resources/databases.ts';
import type { Logger } from './utility/logging/logger.ts';
import type { models as ModelsImport } from './resources/models/Models.ts';
import type { operation as OperationImport } from './server/serverHelpers/serverUtilities.ts';
import type { Resource as ResourceImport } from './resources/Resource.ts';
import type { SecretsView as SecretsImport } from './components/componentSecrets.ts'; // per-component secrets view (#1550)
import type { server as ServerImport } from './server/Server.ts';
import type { tables as TablesImport } from './resources/databases.ts';
type ThreadsImport = unknown[]; // TODO: figure out actual type for this
import type { transaction as TransactionImport } from './resources/transaction.ts';
// These names are exposed TWO ways that resolve to the SAME live, process-wide value:
// 1. as ambient globals (the `declare global` block below), and
// 2. as named exports of the `harper` package (the `export declare const` block below).
// At runtime each is populated in place by `_assignPackageExport(name, value)` (see globals.js),
// which assigns BOTH `global[name]` and `exports[name]` to the one shared instance. So
// `tables`/`databases`/etc. are not per-module or per-compartment copies: the bare global `tables`
// and `import { tables } from 'harper'` are the same object, available in any module Harper loads —
// resources, plugins, and e.g. a Vite SSR entry (whose `node_modules/harper` is symlinked to this
// running install, see components/componentLoader.ts). Application VM compartments are *seeded* from
// this process global, not given an alternate set (see security/jsLoader.ts `getGlobalObject`).
declare global {
const contentTypes: typeof ContentTypesImport;
const createBlob: typeof CreateBlobImport;
const databases: typeof DatabasesImport;
const logger: Logger;
const models: typeof ModelsImport;
const operation: typeof OperationImport;
const Resource: typeof ResourceImport;
const secrets: SecretsImport;
const server: typeof ServerImport;
const tables: typeof TablesImport;
const threads: ThreadsImport;
const transaction: typeof TransactionImport;
}
// Declare constant types so these are defined in `index.d.ts`
export declare const contentTypes: typeof ContentTypesImport;
export declare const createBlob: typeof CreateBlobImport;
export declare const databases: typeof DatabasesImport;
export declare const logger: Logger;
export declare const models: typeof ModelsImport;
export declare const operation: typeof OperationImport;
export declare const Resource: typeof ResourceImport;
export declare const secrets: SecretsImport;
export declare const server: typeof ServerImport;
export declare const tables: typeof TablesImport;
export declare const threads: ThreadsImport;
export declare const transaction: typeof TransactionImport;
// Actual define the values on the `exports` for CJS static analysis
exports.contentTypes = null;
exports.createBlob = undefined;
exports.databases = {};
exports.logger = {};
exports.models = undefined;
exports.operation = undefined;
exports.Resource = undefined;
exports.secrets = undefined;
exports.server = {};
exports.tables = {};
exports.threads = [];
exports.transaction = undefined;
// And finally assign globals to exports.
// These values are populated at runtime by `_assignPackageExport()` in their respective modules
// (e.g. Resource.ts, databases.ts, Server.ts, etc.)
import { globals } from './server/threads/threadServer.js';
Object.assign(exports, globals);