@warp-drive/core 5.8.0-alpha.3 → 5.8.0-alpha.5

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.
package/dist/index.js CHANGED
@@ -1,384 +1,7 @@
1
- import { setLogging, getRuntimeConfig } from './types/runtime.js';
2
- import { a as cloneResponseProperties, I as IS_CACHE_HANDLER, b as assertValidRequest, e as executeNextHandler, d as getRequestResult, u as upgradePromise, s as setPromiseResult, f as clearRequestResult } from "./context-C_7OLieY.js";
3
- import { macroCondition, getGlobalConfig } from '@embroider/macros';
4
- import { w as waitFor } from "./configure-C3x8YXzL.js";
5
- import { peekUniversalTransient, setUniversalTransient } from './types/-private.js';
6
- import { EnableHydration } from './types/request.js';
7
- export { C as CacheHandler, S as Store, r as recordIdentifierFor, U as setIdentifierForgetMethod, Q as setIdentifierGenerationMethod, V as setIdentifierResetMethod, T as setIdentifierUpdateMethod, W as setKeyInfoForResource, s as storeFor } from "./request-state-CUuZzgvE.js";
1
+ import './types/runtime.js';
2
+ export { C as CacheHandler, U as Fetch, V as RequestManager, S as Store, r as recordIdentifierFor, Y as setIdentifierForgetMethod, W as setIdentifierGenerationMethod, Z as setIdentifierResetMethod, X as setIdentifierUpdateMethod, $ as setKeyInfoForResource, s as storeFor } from "./index-MiSBsI57.js";
8
3
  import '@ember/debug';
4
+ import '@embroider/macros';
9
5
  import './utils/string.js';
10
6
  import "./symbols-sql1_mdx.js";
11
-
12
- // Lazily close over fetch to avoid breaking Mirage
13
- const _fetch = typeof fetch !== 'undefined' ? (...args) => fetch(...args) : typeof FastBoot !== 'undefined' ? (...args) => FastBoot.require('node-fetch')(...args) : () => {
14
- throw new Error('No Fetch Implementation Found');
15
- };
16
-
17
- // clones a response in a way that should still
18
- // allow it to stream
19
- function cloneResponse(response, overrides) {
20
- const props = cloneResponseProperties(response);
21
- return new Response(response.body, Object.assign(props, overrides));
22
- }
23
- let IS_MAYBE_MIRAGE = () => false;
24
- if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
25
- IS_MAYBE_MIRAGE = () => Boolean(typeof window !== 'undefined' && (window.server?.pretender || window.fetch.toString().replace(/\s+/g, '') !== 'function fetch() { [native code] }'.replace(/\s+/g, '')));
26
- }
27
- const MUTATION_OPS = new Set(['updateRecord', 'createRecord', 'deleteRecord']);
28
- const ERROR_STATUS_CODE_FOR = new Map([[400, 'Bad Request'], [401, 'Unauthorized'], [402, 'Payment Required'], [403, 'Forbidden'], [404, 'Not Found'], [405, 'Method Not Allowed'], [406, 'Not Acceptable'], [407, 'Proxy Authentication Required'], [408, 'Request Timeout'], [409, 'Conflict'], [410, 'Gone'], [411, 'Length Required'], [412, 'Precondition Failed'], [413, 'Payload Too Large'], [414, 'URI Too Long'], [415, 'Unsupported Media Type'], [416, 'Range Not Satisfiable'], [417, 'Expectation Failed'], [419, 'Page Expired'], [420, 'Enhance Your Calm'], [421, 'Misdirected Request'], [422, 'Unprocessable Entity'], [423, 'Locked'], [424, 'Failed Dependency'], [425, 'Too Early'], [426, 'Upgrade Required'], [428, 'Precondition Required'], [429, 'Too Many Requests'], [430, 'Request Header Fields Too Large'], [431, 'Request Header Fields Too Large'], [450, 'Blocked By Windows Parental Controls'], [451, 'Unavailable For Legal Reasons'], [500, 'Internal Server Error'], [501, 'Not Implemented'], [502, 'Bad Gateway'], [503, 'Service Unavailable'], [504, 'Gateway Timeout'], [505, 'HTTP Version Not Supported'], [506, 'Variant Also Negotiates'], [507, 'Insufficient Storage'], [508, 'Loop Detected'], [509, 'Bandwidth Limit Exceeded'], [510, 'Not Extended'], [511, 'Network Authentication Required']]);
29
-
30
- /**
31
- * ```ts
32
- * import { Fetch } from '@warp-drive/core';
33
- * ```
34
- *
35
- * A basic Fetch Handler which converts a request into a
36
- * `fetch` call presuming the response to be `json`.
37
- *
38
- * ```ts
39
- * import { RequestManager, Fetch } from '@warp-drive/core';
40
- *
41
- * const manager = new RequestManager()
42
- * .use([Fetch]);
43
- * ```
44
- *
45
- * @public
46
- */
47
- const Fetch = {
48
- async request(context) {
49
- let response;
50
- try {
51
- macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
52
- if (!test) {
53
- throw new Error('The Fetch handler expects the request to have a URL, none was provided.');
54
- }
55
- })(context.request.url && typeof context.request.url === 'string') : {};
56
- response = await _fetch(context.request.url, context.request);
57
- } catch (e) {
58
- if (e instanceof DOMException && e.name === 'AbortError') {
59
- e.statusText = 'Aborted';
60
- e.status = 20;
61
- e.isRequestError = true;
62
- } else {
63
- e.statusText = 'Unknown Network Error';
64
- e.status = 0;
65
- if (!(e instanceof DOMException)) {
66
- e.code = 0;
67
- }
68
- e.isRequestError = true;
69
- }
70
- throw e;
71
- }
72
- const isError = !response.ok || response.status >= 400;
73
- const op = context.request.op;
74
- const isMutationOp = Boolean(op && MUTATION_OPS.has(op));
75
- if (!isError && !isMutationOp && response.status !== 204 && !response.headers.has('date')) {
76
- if (IS_MAYBE_MIRAGE()) {
77
- response.headers.set('date', new Date().toUTCString());
78
- } else {
79
- const headers = new Headers(response.headers);
80
- headers.set('date', new Date().toUTCString());
81
- response = cloneResponse(response, {
82
- headers
83
- });
84
- }
85
- }
86
- context.setResponse(response);
87
- if (response.status === 204) {
88
- return null;
89
- }
90
- let text = '';
91
- // if we are in a mirage context, we cannot support streaming
92
- if (IS_MAYBE_MIRAGE()) {
93
- text = await response.text();
94
- } else {
95
- const reader = response.body.getReader();
96
- const decoder = new TextDecoder();
97
- let isStreaming = context.hasRequestedStream;
98
- let stream = isStreaming ? new TransformStream() : null;
99
- let writer = stream?.writable.getWriter();
100
- if (isStreaming) {
101
- // Listen for the abort event on the AbortSignal
102
- context.request.signal?.addEventListener('abort', () => {
103
- if (!isStreaming) {
104
- return;
105
- }
106
- void stream.writable.abort('Request Aborted');
107
- void stream.readable.cancel('Request Aborted');
108
- });
109
- context.setStream(stream.readable);
110
- }
111
- while (true) {
112
- // we manually read the stream instead of using `response.json()`
113
- // or `response.text()` because if we need to stream the body
114
- // we need to be able to pass the stream along efficiently.
115
- const {
116
- done,
117
- value
118
- } = await reader.read();
119
- if (done) {
120
- if (isStreaming) {
121
- isStreaming = false;
122
- await writer.ready;
123
- await writer.close();
124
- }
125
- break;
126
- }
127
- text += decoder.decode(value, {
128
- stream: true
129
- });
130
-
131
- // if we are streaming, we want to pass the stream along
132
- if (isStreaming) {
133
- await writer.ready;
134
- await writer.write(value);
135
- } else if (context.hasRequestedStream) {
136
- const encode = new TextEncoder();
137
- isStreaming = true;
138
- stream = new TransformStream();
139
- // Listen for the abort event on the AbortSignal
140
- // eslint-disable-next-line @typescript-eslint/no-loop-func
141
- context.request.signal?.addEventListener('abort', () => {
142
- if (!isStreaming) {
143
- return;
144
- }
145
- void stream.writable.abort('Request Aborted');
146
- void stream.readable.cancel('Request Aborted');
147
- });
148
- context.setStream(stream.readable);
149
- writer = stream.writable.getWriter();
150
- await writer.ready;
151
- await writer.write(encode.encode(text));
152
- await writer.ready;
153
- await writer.write(value);
154
- }
155
- }
156
- if (isStreaming) {
157
- isStreaming = false;
158
- await writer.ready;
159
- await writer.close();
160
- }
161
- }
162
- // if we are an error, we will want to throw
163
- if (isError) {
164
- let errorPayload;
165
- try {
166
- errorPayload = JSON.parse(text);
167
- } catch {
168
- // void;
169
- }
170
- // attempt errors discovery
171
- const errors = Array.isArray(errorPayload) ? errorPayload : isDict(errorPayload) && Array.isArray(errorPayload.errors) ? errorPayload.errors : null;
172
- const statusText = response.statusText || ERROR_STATUS_CODE_FOR.get(response.status) || 'Unknown Request Error';
173
- const msg = `[${response.status} ${statusText}] ${context.request.method ?? 'GET'} (${response.type}) - ${response.url}`;
174
- const error = errors ? new AggregateError(errors, msg) : new Error(msg);
175
- error.status = response.status;
176
- error.statusText = statusText;
177
- error.isRequestError = true;
178
- error.code = error.status;
179
- error.name = error.statusText.replaceAll(' ', '') + 'Error';
180
- error.content = errorPayload;
181
- throw error;
182
- } else {
183
- return JSON.parse(text);
184
- }
185
- }
186
- };
187
- function isDict(v) {
188
- return v !== null && typeof v === 'object';
189
- }
190
- class RequestManager {
191
- /** @internal */
192
-
193
- /** @internal */
194
-
195
- /**
196
- * A map of pending requests from request.id to their
197
- * associated CacheHandler promise.
198
- *
199
- * This queue is managed by the CacheHandler
200
- *
201
- * @internal
202
- */
203
-
204
- /** @internal */
205
-
206
- constructor(options) {
207
- Object.assign(this, options);
208
- this._pending = new Map();
209
- this._deduped = new Map();
210
- this._handlers = [];
211
- }
212
-
213
- /**
214
- * Register a handler to use for primary cache intercept.
215
- *
216
- * Only one such handler may exist. If using the same
217
- * RequestManager as the Store instance the Store
218
- * registers itself as a Cache handler.
219
- *
220
- * @public
221
- */
222
- useCache(cacheHandler) {
223
- if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
224
- if (this._hasCacheHandler) {
225
- throw new Error(`\`RequestManager.useCache(<handler>)\` May only be invoked once.`);
226
- }
227
- if (Object.isFrozen(this._handlers)) {
228
- throw new Error(`\`RequestManager.useCache(<handler>)\` May only be invoked prior to any request having been made.`);
229
- }
230
- this._hasCacheHandler = true;
231
- }
232
- cacheHandler[IS_CACHE_HANDLER] = true;
233
- this._handlers.unshift(cacheHandler);
234
- return this;
235
- }
236
-
237
- /**
238
- * Register handler(s) to use when a request is issued.
239
- *
240
- * Handlers will be invoked in the order they are registered.
241
- * Each Handler is given the opportunity to handle the request,
242
- * curry the request, or pass along a modified request.
243
- *
244
- * @public
245
- */
246
- use(newHandlers) {
247
- const handlers = this._handlers;
248
- if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
249
- if (Object.isFrozen(handlers)) {
250
- throw new Error(`Cannot add a Handler to a RequestManager after a request has been made`);
251
- }
252
- if (!Array.isArray(newHandlers)) {
253
- throw new Error(`\`RequestManager.use(<Handler[]>)\` expects an array of handlers, but was called with \`${typeof newHandlers}\``);
254
- }
255
- newHandlers.forEach((handler, index) => {
256
- if (!handler || typeof handler !== 'function' && typeof handler !== 'object' || typeof handler.request !== 'function') {
257
- throw new Error(`\`RequestManager.use(<Handler[]>)\` expected to receive an array of handler objects with request methods, by the handler at index ${index} does not conform.`);
258
- }
259
- });
260
- }
261
- handlers.push(...newHandlers);
262
- return this;
263
- }
264
-
265
- /**
266
- * Issue a Request.
267
- *
268
- * Returns a Future that fulfills with a StructuredDocument
269
- *
270
- * @public
271
- */
272
- request(request) {
273
- const handlers = this._handlers;
274
- if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
275
- if (!Object.isFrozen(handlers)) {
276
- Object.freeze(handlers);
277
- }
278
- assertValidRequest(request, true);
279
- }
280
- const controller = request.controller || new AbortController();
281
- if (request.controller) {
282
- delete request.controller;
283
- }
284
- const requestId = peekUniversalTransient('REQ_ID') ?? 0;
285
- setUniversalTransient('REQ_ID', requestId + 1);
286
- const context = {
287
- controller,
288
- response: null,
289
- stream: null,
290
- hasRequestedStream: false,
291
- id: requestId,
292
- identifier: null,
293
- requester: request[EnableHydration] && request.store ? request.store : this
294
- };
295
- const promise = executeNextHandler(handlers, request, 0, context);
296
-
297
- // the cache handler will set the result of the request synchronously
298
- // if it is able to fulfill the request from the cache
299
- const cacheResult = getRequestResult(requestId);
300
- if (macroCondition(getGlobalConfig().WarpDrive.env.TESTING)) {
301
- if (!request.disableTestWaiter) {
302
- const newPromise = waitFor(promise);
303
- const finalPromise = upgradePromise(newPromise.then(result => {
304
- setPromiseResult(finalPromise, {
305
- isError: false,
306
- result
307
- });
308
- clearRequestResult(requestId);
309
- return result;
310
- }, error => {
311
- setPromiseResult(finalPromise, {
312
- isError: true,
313
- result: error
314
- });
315
- clearRequestResult(requestId);
316
- throw error;
317
- }), promise);
318
- if (cacheResult) {
319
- setPromiseResult(finalPromise, cacheResult);
320
- }
321
- return finalPromise;
322
- }
323
- }
324
-
325
- // const promise1 = store.request(myRequest);
326
- // const promise2 = store.request(myRequest);
327
- // promise1 === promise2; // false
328
- // either we need to make promise1 === promise2, or we need to make sure that
329
- // we need to have a way to key from request to result
330
- // such that we can lookup the result here and return it if it exists
331
- const finalPromise = upgradePromise(promise.then(result => {
332
- setPromiseResult(finalPromise, {
333
- isError: false,
334
- result
335
- });
336
- clearRequestResult(requestId);
337
- return result;
338
- }, error => {
339
- setPromiseResult(finalPromise, {
340
- isError: true,
341
- result: error
342
- });
343
- clearRequestResult(requestId);
344
- throw error;
345
- }), promise);
346
- if (cacheResult) {
347
- setPromiseResult(finalPromise, cacheResult);
348
- }
349
- return finalPromise;
350
- }
351
-
352
- /**
353
- * This method exists so that the RequestManager can be created
354
- * can be created by container/factory systems that expect to
355
- * call a static `create` method to instantiate the class.
356
- *
357
- * Using `new RequestManager()` directly is preferred.
358
- *
359
- * @private
360
- */
361
- static create(options) {
362
- return new this(options);
363
- }
364
- }
365
-
366
- /**
367
- * This type exists for internal use only for
368
- * where intimate contracts still exist either for
369
- * the Test Suite or for Legacy code.
370
- *
371
- * @private
372
- */
373
-
374
- /**
375
- * @module
376
- * @mergeModuleWith <project>
377
- */
378
-
379
- // @ts-expect-error adding to globalThis
380
- globalThis.setWarpDriveLogging = setLogging;
381
-
382
- // @ts-expect-error adding to globalThis
383
- globalThis.getWarpDriveRuntimeConfig = getRuntimeConfig;
384
- export { Fetch, RequestManager };
7
+ import "./configure-C3x8YXzL.js";