@netlify/plugin-nextjs 4.40.0 → 4.40.2

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 (164) hide show
  1. package/lib/helpers/edge.js +4 -2
  2. package/lib/templates/getHandler.js +1 -2
  3. package/lib/templates/handlerUtils.js +14 -1
  4. package/lib/templates/server.js +9 -8
  5. package/lib/templates/vendor.js +20 -0
  6. package/package.json +7 -4
  7. package/src/templates/edge/next-dev.js +3 -3
  8. package/src/templates/edge/shims.js +6 -6
  9. package/src/templates/edge-shared/next-utils.ts +3 -3
  10. package/src/templates/edge-shared/utils.ts +1 -1
  11. package/src/templates/vendor/deno.land/std@0.134.0/fmt/colors.ts +536 -0
  12. package/src/templates/vendor/deno.land/std@0.134.0/testing/_diff.ts +360 -0
  13. package/src/templates/vendor/deno.land/std@0.134.0/testing/asserts.ts +866 -0
  14. package/src/templates/vendor/deno.land/std@0.175.0/_util/asserts.ts +25 -0
  15. package/src/templates/vendor/deno.land/std@0.175.0/_util/os.ts +23 -0
  16. package/src/templates/vendor/deno.land/std@0.175.0/async/abortable.ts +149 -0
  17. package/src/templates/vendor/deno.land/std@0.175.0/async/deadline.ts +30 -0
  18. package/src/templates/vendor/deno.land/std@0.175.0/async/debounce.ts +79 -0
  19. package/src/templates/vendor/deno.land/std@0.175.0/async/deferred.ts +48 -0
  20. package/src/templates/vendor/deno.land/std@0.175.0/async/delay.ts +67 -0
  21. package/src/templates/vendor/deno.land/std@0.175.0/async/mod.ts +18 -0
  22. package/src/templates/vendor/deno.land/std@0.175.0/async/mux_async_iterator.ts +97 -0
  23. package/src/templates/vendor/deno.land/std@0.175.0/async/pool.ts +95 -0
  24. package/src/templates/vendor/deno.land/std@0.175.0/async/retry.ts +81 -0
  25. package/src/templates/vendor/deno.land/std@0.175.0/async/tee.ts +100 -0
  26. package/src/templates/vendor/deno.land/std@0.175.0/bytes/index_of_needle.ts +49 -0
  27. package/src/templates/vendor/deno.land/std@0.175.0/crypto/timing_safe_equal.ts +29 -0
  28. package/src/templates/vendor/deno.land/std@0.175.0/datetime/to_imf.ts +45 -0
  29. package/src/templates/vendor/deno.land/std@0.175.0/encoding/base64.ts +144 -0
  30. package/src/templates/vendor/deno.land/std@0.175.0/encoding/base64url.ts +70 -0
  31. package/src/templates/vendor/deno.land/std@0.175.0/flags/mod.ts +785 -0
  32. package/src/templates/vendor/deno.land/std@0.175.0/fmt/colors.ts +569 -0
  33. package/src/templates/vendor/deno.land/std@0.175.0/fmt/printf.ts +939 -0
  34. package/src/templates/vendor/deno.land/std@0.175.0/http/cookie.ts +403 -0
  35. package/src/templates/vendor/deno.land/std@0.175.0/node/_core.ts +77 -0
  36. package/src/templates/vendor/deno.land/std@0.175.0/node/_events.d.ts +848 -0
  37. package/src/templates/vendor/deno.land/std@0.175.0/node/_events.mjs +1033 -0
  38. package/src/templates/vendor/deno.land/std@0.175.0/node/_global.d.ts +66 -0
  39. package/src/templates/vendor/deno.land/std@0.175.0/node/_next_tick.ts +173 -0
  40. package/src/templates/vendor/deno.land/std@0.175.0/node/_process/exiting.ts +4 -0
  41. package/src/templates/vendor/deno.land/std@0.175.0/node/_process/process.ts +131 -0
  42. package/src/templates/vendor/deno.land/std@0.175.0/node/_process/stdio.mjs +7 -0
  43. package/src/templates/vendor/deno.land/std@0.175.0/node/_process/streams.mjs +146 -0
  44. package/src/templates/vendor/deno.land/std@0.175.0/node/_stream.d.ts +1488 -0
  45. package/src/templates/vendor/deno.land/std@0.175.0/node/_stream.mjs +746 -0
  46. package/src/templates/vendor/deno.land/std@0.175.0/node/_util/_util_callbackify.ts +129 -0
  47. package/src/templates/vendor/deno.land/std@0.175.0/node/_utils.ts +206 -0
  48. package/src/templates/vendor/deno.land/std@0.175.0/node/assert.ts +940 -0
  49. package/src/templates/vendor/deno.land/std@0.175.0/node/assertion_error.ts +579 -0
  50. package/src/templates/vendor/deno.land/std@0.175.0/node/async_hooks.ts +331 -0
  51. package/src/templates/vendor/deno.land/std@0.175.0/node/buffer.ts +13 -0
  52. package/src/templates/vendor/deno.land/std@0.175.0/node/events.ts +14 -0
  53. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/buffer.d.ts +2074 -0
  54. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/buffer.mjs +2607 -0
  55. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/crypto/_keys.ts +16 -0
  56. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/crypto/constants.ts +5 -0
  57. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/error_codes.ts +7 -0
  58. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/errors.ts +2867 -0
  59. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/fixed_queue.ts +123 -0
  60. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/hide_stack_frames.ts +16 -0
  61. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/net.ts +95 -0
  62. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/normalize_encoding.mjs +72 -0
  63. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/options.ts +45 -0
  64. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/primordials.mjs +30 -0
  65. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/process/per_thread.mjs +272 -0
  66. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/readline/callbacks.mjs +137 -0
  67. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/readline/utils.mjs +580 -0
  68. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/streams/destroy.mjs +320 -0
  69. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/streams/end-of-stream.mjs +229 -0
  70. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/streams/utils.mjs +242 -0
  71. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/util/comparisons.ts +669 -0
  72. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/util/debuglog.ts +118 -0
  73. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/util/inspect.mjs +2237 -0
  74. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/util/types.ts +113 -0
  75. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/util.mjs +143 -0
  76. package/src/templates/vendor/deno.land/std@0.175.0/node/internal/validators.mjs +317 -0
  77. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/_libuv_winerror.ts +229 -0
  78. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/_listen.ts +16 -0
  79. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/_node.ts +18 -0
  80. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/_timingSafeEqual.ts +12 -0
  81. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/_utils.ts +86 -0
  82. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/_winerror.ts +16873 -0
  83. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/ares.ts +66 -0
  84. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/async_wrap.ts +152 -0
  85. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/buffer.ts +130 -0
  86. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/cares_wrap.ts +541 -0
  87. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/config.ts +3 -0
  88. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/connection_wrap.ts +80 -0
  89. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/constants.ts +900 -0
  90. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/contextify.ts +3 -0
  91. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/credentials.ts +3 -0
  92. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/crypto.ts +14 -0
  93. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/errors.ts +3 -0
  94. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/fs.ts +3 -0
  95. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/fs_dir.ts +3 -0
  96. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/fs_event_wrap.ts +3 -0
  97. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/handle_wrap.ts +50 -0
  98. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/heap_utils.ts +3 -0
  99. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/http_parser.ts +3 -0
  100. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/icu.ts +3 -0
  101. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/inspector.ts +3 -0
  102. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/js_stream.ts +3 -0
  103. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/messaging.ts +3 -0
  104. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/mod.ts +108 -0
  105. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/module_wrap.ts +3 -0
  106. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/native_module.ts +3 -0
  107. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/natives.ts +3 -0
  108. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/node_file.ts +84 -0
  109. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/node_options.ts +39 -0
  110. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/options.ts +3 -0
  111. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/os.ts +3 -0
  112. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/performance.ts +3 -0
  113. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/pipe_wrap.ts +392 -0
  114. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/process_methods.ts +3 -0
  115. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/report.ts +3 -0
  116. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/serdes.ts +3 -0
  117. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/signal_wrap.ts +3 -0
  118. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/spawn_sync.ts +3 -0
  119. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/stream_wrap.ts +354 -0
  120. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/string_decoder.ts +15 -0
  121. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/symbols.ts +27 -0
  122. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/task_queue.ts +3 -0
  123. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/tcp_wrap.ts +488 -0
  124. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/timers.ts +3 -0
  125. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/tls_wrap.ts +3 -0
  126. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/trace_events.ts +3 -0
  127. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/tty_wrap.ts +3 -0
  128. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/types.ts +186 -0
  129. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/udp_wrap.ts +496 -0
  130. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/url.ts +3 -0
  131. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/util.ts +126 -0
  132. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/uv.ts +437 -0
  133. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/v8.ts +3 -0
  134. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/worker.ts +3 -0
  135. package/src/templates/vendor/deno.land/std@0.175.0/node/internal_binding/zlib.ts +3 -0
  136. package/src/templates/vendor/deno.land/std@0.175.0/node/process.ts +705 -0
  137. package/src/templates/vendor/deno.land/std@0.175.0/node/stream.ts +37 -0
  138. package/src/templates/vendor/deno.land/std@0.175.0/node/string_decoder.ts +337 -0
  139. package/src/templates/vendor/deno.land/std@0.175.0/node/util/types.ts +4 -0
  140. package/src/templates/vendor/deno.land/std@0.175.0/node/util.ts +289 -0
  141. package/src/templates/vendor/deno.land/std@0.175.0/path/_constants.ts +49 -0
  142. package/src/templates/vendor/deno.land/std@0.175.0/path/_interface.ts +30 -0
  143. package/src/templates/vendor/deno.land/std@0.175.0/path/_util.ts +194 -0
  144. package/src/templates/vendor/deno.land/std@0.175.0/path/common.ts +40 -0
  145. package/src/templates/vendor/deno.land/std@0.175.0/path/glob.ts +418 -0
  146. package/src/templates/vendor/deno.land/std@0.175.0/path/mod.ts +53 -0
  147. package/src/templates/vendor/deno.land/std@0.175.0/path/posix.ts +487 -0
  148. package/src/templates/vendor/deno.land/std@0.175.0/path/separator.ts +7 -0
  149. package/src/templates/vendor/deno.land/std@0.175.0/path/win32.ts +962 -0
  150. package/src/templates/vendor/deno.land/std@0.175.0/streams/write_all.ts +64 -0
  151. package/src/templates/vendor/deno.land/std@0.175.0/testing/_diff.ts +440 -0
  152. package/src/templates/vendor/deno.land/std@0.175.0/testing/_format.ts +23 -0
  153. package/src/templates/vendor/deno.land/std@0.175.0/testing/asserts.ts +906 -0
  154. package/src/templates/vendor/deno.land/std@0.175.0/types.d.ts +89 -0
  155. package/src/templates/vendor/deno.land/x/html_rewriter@v0.1.0-pre.17/index.ts +133 -0
  156. package/src/templates/vendor/deno.land/x/html_rewriter@v0.1.0-pre.17/vendor/asyncify.js +112 -0
  157. package/src/templates/vendor/deno.land/x/html_rewriter@v0.1.0-pre.17/vendor/html_rewriter.d.ts +88 -0
  158. package/src/templates/vendor/deno.land/x/html_rewriter@v0.1.0-pre.17/vendor/html_rewriter.js +974 -0
  159. package/src/templates/vendor/deno.land/x/path_to_regexp@v6.2.1/index.ts +621 -0
  160. package/src/templates/vendor/esm.sh/v91/next@12.2.5/deno/dist/compiled/cookie.js +13 -0
  161. package/src/templates/vendor/esm.sh/v91/next@12.2.5/deno/dist/server/web/spec-extension/request.js +12 -0
  162. package/src/templates/vendor/esm.sh/v91/next@12.2.5/deno/dist/server/web/spec-extension/response.js +5 -0
  163. package/src/templates/vendor/import_map.json +13 -0
  164. package/src/templates/vendor/raw.githubusercontent.com/worker-tools/resolvable-promise/master/index.ts +50 -0
@@ -0,0 +1,25 @@
1
+ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
2
+ // This module is browser compatible.
3
+
4
+ /**
5
+ * All internal non-test code, that is files that do not have `test` or `bench` in the name, must use the assertion functions within `_utils/asserts.ts` and not `testing/asserts.ts`. This is to create a separation of concerns between internal and testing assertions.
6
+ */
7
+
8
+ export class DenoStdInternalError extends Error {
9
+ constructor(message: string) {
10
+ super(message);
11
+ this.name = "DenoStdInternalError";
12
+ }
13
+ }
14
+
15
+ /** Make an assertion, if not `true`, then throw. */
16
+ export function assert(expr: unknown, msg = ""): asserts expr {
17
+ if (!expr) {
18
+ throw new DenoStdInternalError(msg);
19
+ }
20
+ }
21
+
22
+ /** Use this to assert unreachable code. */
23
+ export function unreachable(): never {
24
+ throw new DenoStdInternalError("unreachable");
25
+ }
@@ -0,0 +1,23 @@
1
+ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
2
+ // This module is browser compatible.
3
+
4
+ export type OSType = "windows" | "linux" | "darwin" | "freebsd";
5
+
6
+ export const osType: OSType = (() => {
7
+ // deno-lint-ignore no-explicit-any
8
+ const { Deno } = globalThis as any;
9
+ if (typeof Deno?.build?.os === "string") {
10
+ return Deno.build.os;
11
+ }
12
+
13
+ // deno-lint-ignore no-explicit-any
14
+ const { navigator } = globalThis as any;
15
+ if (navigator?.appVersion?.includes?.("Win")) {
16
+ return "windows";
17
+ }
18
+
19
+ return "linux";
20
+ })();
21
+
22
+ export const isWindows = osType === "windows";
23
+ export const isLinux = osType === "linux";
@@ -0,0 +1,149 @@
1
+ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
2
+ import { deferred } from "./deferred.ts";
3
+
4
+ /**
5
+ * Make Promise abortable with the given signal.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * import { abortable } from "https://deno.land/std@$STD_VERSION/async/mod.ts";
10
+ * import { delay } from "https://deno.land/std@$STD_VERSION/async/mod.ts";
11
+ *
12
+ * const p = delay(1000);
13
+ * const c = new AbortController();
14
+ * setTimeout(() => c.abort(), 100);
15
+ *
16
+ * // Below throws `DOMException` after 100 ms
17
+ * await abortable(p, c.signal);
18
+ * ```
19
+ */
20
+ export function abortable<T>(p: Promise<T>, signal: AbortSignal): Promise<T>;
21
+ /**
22
+ * Make AsyncIterable abortable with the given signal.
23
+ *
24
+ * @example
25
+ * ```typescript
26
+ * import { abortable } from "https://deno.land/std@$STD_VERSION/async/mod.ts";
27
+ * import { delay } from "https://deno.land/std@$STD_VERSION/async/mod.ts";
28
+ *
29
+ * const p = async function* () {
30
+ * yield "Hello";
31
+ * await delay(1000);
32
+ * yield "World";
33
+ * };
34
+ * const c = new AbortController();
35
+ * setTimeout(() => c.abort(), 100);
36
+ *
37
+ * // Below throws `DOMException` after 100 ms
38
+ * // and items become `["Hello"]`
39
+ * const items: string[] = [];
40
+ * for await (const item of abortable(p(), c.signal)) {
41
+ * items.push(item);
42
+ * }
43
+ * ```
44
+ */
45
+ export function abortable<T>(
46
+ p: AsyncIterable<T>,
47
+ signal: AbortSignal,
48
+ ): AsyncGenerator<T>;
49
+ export function abortable<T>(
50
+ p: Promise<T> | AsyncIterable<T>,
51
+ signal: AbortSignal,
52
+ ): Promise<T> | AsyncIterable<T> {
53
+ if (p instanceof Promise) {
54
+ return abortablePromise(p, signal);
55
+ } else {
56
+ return abortableAsyncIterable(p, signal);
57
+ }
58
+ }
59
+
60
+ /**
61
+ * Make Promise abortable with the given signal.
62
+ *
63
+ * @example
64
+ * ```typescript
65
+ * import { abortablePromise } from "https://deno.land/std@$STD_VERSION/async/mod.ts";
66
+ *
67
+ * const request = fetch("https://example.com");
68
+ *
69
+ * const c = new AbortController();
70
+ * setTimeout(() => c.abort(), 100);
71
+ *
72
+ * const p = abortablePromise(request, c.signal);
73
+ *
74
+ * // The below throws if the request didn't resolve in 100ms
75
+ * await p;
76
+ * ```
77
+ */
78
+ export function abortablePromise<T>(
79
+ p: Promise<T>,
80
+ signal: AbortSignal,
81
+ ): Promise<T> {
82
+ if (signal.aborted) {
83
+ return Promise.reject(createAbortError(signal.reason));
84
+ }
85
+ const waiter = deferred<never>();
86
+ const abort = () => waiter.reject(createAbortError(signal.reason));
87
+ signal.addEventListener("abort", abort, { once: true });
88
+ return Promise.race([
89
+ waiter,
90
+ p.finally(() => {
91
+ signal.removeEventListener("abort", abort);
92
+ }),
93
+ ]);
94
+ }
95
+
96
+ /**
97
+ * Make AsyncIterable abortable with the given signal.
98
+ *
99
+ * @example
100
+ * ```typescript
101
+ * import { abortableAsyncIterable } from "https://deno.land/std@$STD_VERSION/async/mod.ts";
102
+ * import { delay } from "https://deno.land/std@$STD_VERSION/async/mod.ts";
103
+ *
104
+ * const p = async function* () {
105
+ * yield "Hello";
106
+ * await delay(1000);
107
+ * yield "World";
108
+ * };
109
+ * const c = new AbortController();
110
+ * setTimeout(() => c.abort(), 100);
111
+ *
112
+ * // Below throws `DOMException` after 100 ms
113
+ * // and items become `["Hello"]`
114
+ * const items: string[] = [];
115
+ * for await (const item of abortableAsyncIterable(p(), c.signal)) {
116
+ * items.push(item);
117
+ * }
118
+ * ```
119
+ */
120
+ export async function* abortableAsyncIterable<T>(
121
+ p: AsyncIterable<T>,
122
+ signal: AbortSignal,
123
+ ): AsyncGenerator<T> {
124
+ if (signal.aborted) {
125
+ throw createAbortError(signal.reason);
126
+ }
127
+ const waiter = deferred<never>();
128
+ const abort = () => waiter.reject(createAbortError(signal.reason));
129
+ signal.addEventListener("abort", abort, { once: true });
130
+
131
+ const it = p[Symbol.asyncIterator]();
132
+ while (true) {
133
+ const { done, value } = await Promise.race([waiter, it.next()]);
134
+ if (done) {
135
+ signal.removeEventListener("abort", abort);
136
+ return;
137
+ }
138
+ yield value;
139
+ }
140
+ }
141
+
142
+ // This `reason` comes from `AbortSignal` thus must be `any`.
143
+ // deno-lint-ignore no-explicit-any
144
+ function createAbortError(reason?: any): DOMException {
145
+ return new DOMException(
146
+ reason ? `Aborted: ${reason}` : "Aborted",
147
+ "AbortError",
148
+ );
149
+ }
@@ -0,0 +1,30 @@
1
+ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
2
+ // This module is browser compatible.
3
+
4
+ import { deferred } from "./deferred.ts";
5
+
6
+ export class DeadlineError extends Error {
7
+ constructor() {
8
+ super("Deadline");
9
+ this.name = "DeadlineError";
10
+ }
11
+ }
12
+
13
+ /**
14
+ * Create a promise which will be rejected with {@linkcode DeadlineError} when a given delay is exceeded.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * import { deadline } from "https://deno.land/std@$STD_VERSION/async/deadline.ts";
19
+ * import { delay } from "https://deno.land/std@$STD_VERSION/async/delay.ts";
20
+ *
21
+ * const delayedPromise = delay(1000);
22
+ * // Below throws `DeadlineError` after 10 ms
23
+ * const result = await deadline(delayedPromise, 10);
24
+ * ```
25
+ */
26
+ export function deadline<T>(p: Promise<T>, delay: number): Promise<T> {
27
+ const d = deferred<never>();
28
+ const t = setTimeout(() => d.reject(new DeadlineError()), delay);
29
+ return Promise.race([p, d]).finally(() => clearTimeout(t));
30
+ }
@@ -0,0 +1,79 @@
1
+ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
2
+ // This module is browser compatible.
3
+
4
+ /**
5
+ * A debounced function that will be delayed by a given `wait`
6
+ * time in milliseconds. If the method is called again before
7
+ * the timeout expires, the previous call will be aborted.
8
+ */
9
+ export interface DebouncedFunction<T extends Array<unknown>> {
10
+ (...args: T): void;
11
+ /** Clears the debounce timeout and omits calling the debounced function. */
12
+ clear(): void;
13
+ /** Clears the debounce timeout and calls the debounced function immediately. */
14
+ flush(): void;
15
+ /** Returns a boolean whether a debounce call is pending or not. */
16
+ readonly pending: boolean;
17
+ }
18
+
19
+ /**
20
+ * Creates a debounced function that delays the given `func`
21
+ * by a given `wait` time in milliseconds. If the method is called
22
+ * again before the timeout expires, the previous call will be
23
+ * aborted.
24
+ *
25
+ * @example
26
+ * ```
27
+ * import { debounce } from "https://deno.land/std@$STD_VERSION/async/debounce.ts";
28
+ *
29
+ * const log = debounce(
30
+ * (event: Deno.FsEvent) =>
31
+ * console.log("[%s] %s", event.kind, event.paths[0]),
32
+ * 200,
33
+ * );
34
+ *
35
+ * for await (const event of Deno.watchFs("./")) {
36
+ * log(event);
37
+ * }
38
+ * // wait 200ms ...
39
+ * // output: Function debounced after 200ms with baz
40
+ * ```
41
+ *
42
+ * @param fn The function to debounce.
43
+ * @param wait The time in milliseconds to delay the function.
44
+ */
45
+ // deno-lint-ignore no-explicit-any
46
+ export function debounce<T extends Array<any>>(
47
+ fn: (this: DebouncedFunction<T>, ...args: T) => void,
48
+ wait: number,
49
+ ): DebouncedFunction<T> {
50
+ let timeout: number | null = null;
51
+ let flush: (() => void) | null = null;
52
+
53
+ const debounced: DebouncedFunction<T> = ((...args: T) => {
54
+ debounced.clear();
55
+ flush = () => {
56
+ debounced.clear();
57
+ fn.call(debounced, ...args);
58
+ };
59
+ timeout = setTimeout(flush, wait);
60
+ }) as DebouncedFunction<T>;
61
+
62
+ debounced.clear = () => {
63
+ if (typeof timeout === "number") {
64
+ clearTimeout(timeout);
65
+ timeout = null;
66
+ flush = null;
67
+ }
68
+ };
69
+
70
+ debounced.flush = () => {
71
+ flush?.();
72
+ };
73
+
74
+ Object.defineProperty(debounced, "pending", {
75
+ get: () => typeof timeout === "number",
76
+ });
77
+
78
+ return debounced;
79
+ }
@@ -0,0 +1,48 @@
1
+ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
2
+ // This module is browser compatible.
3
+
4
+ // TODO(ry) It'd be better to make Deferred a class that inherits from
5
+ // Promise, rather than an interface. This is possible in ES2016, however
6
+ // typescript produces broken code when targeting ES5 code.
7
+ // See https://github.com/Microsoft/TypeScript/issues/15202
8
+ // At the time of writing, the github issue is closed but the problem remains.
9
+ export interface Deferred<T> extends Promise<T> {
10
+ readonly state: "pending" | "fulfilled" | "rejected";
11
+ resolve(value?: T | PromiseLike<T>): void;
12
+ // deno-lint-ignore no-explicit-any
13
+ reject(reason?: any): void;
14
+ }
15
+
16
+ /**
17
+ * Creates a Promise with the `reject` and `resolve` functions placed as methods
18
+ * on the promise object itself.
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * import { deferred } from "https://deno.land/std@$STD_VERSION/async/deferred.ts";
23
+ *
24
+ * const p = deferred<number>();
25
+ * // ...
26
+ * p.resolve(42);
27
+ * ```
28
+ */
29
+ export function deferred<T>(): Deferred<T> {
30
+ let methods;
31
+ let state = "pending";
32
+ const promise = new Promise<T>((resolve, reject) => {
33
+ methods = {
34
+ async resolve(value: T | PromiseLike<T>) {
35
+ await value;
36
+ state = "fulfilled";
37
+ resolve(value);
38
+ },
39
+ // deno-lint-ignore no-explicit-any
40
+ reject(reason?: any) {
41
+ state = "rejected";
42
+ reject(reason);
43
+ },
44
+ };
45
+ });
46
+ Object.defineProperty(promise, "state", { get: () => state });
47
+ return Object.assign(promise, methods) as Deferred<T>;
48
+ }
@@ -0,0 +1,67 @@
1
+ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
2
+ // This module is browser compatible.
3
+
4
+ export interface DelayOptions {
5
+ /** Signal used to abort the delay. */
6
+ signal?: AbortSignal;
7
+ /** Indicates whether the process should continue to run as long as the timer exists.
8
+ *
9
+ * @default {true}
10
+ */
11
+ persistent?: boolean;
12
+ }
13
+
14
+ /**
15
+ * Resolve a Promise after a given amount of milliseconds.
16
+ *
17
+ * @example
18
+ *
19
+ * ```typescript
20
+ * import { delay } from "https://deno.land/std@$STD_VERSION/async/delay.ts";
21
+ *
22
+ * // ...
23
+ * const delayedPromise = delay(100);
24
+ * const result = await delayedPromise;
25
+ * // ...
26
+ * ```
27
+ *
28
+ * To allow the process to continue to run as long as the timer exists. Requires
29
+ * `--unstable` flag.
30
+ *
31
+ * ```typescript
32
+ * import { delay } from "https://deno.land/std@$STD_VERSION/async/delay.ts";
33
+ *
34
+ * // ...
35
+ * await delay(100, { persistent: false });
36
+ * // ...
37
+ * ```
38
+ */
39
+ export function delay(ms: number, options: DelayOptions = {}): Promise<void> {
40
+ const { signal, persistent } = options;
41
+ if (signal?.aborted) {
42
+ return Promise.reject(new DOMException("Delay was aborted.", "AbortError"));
43
+ }
44
+ return new Promise((resolve, reject) => {
45
+ const abort = () => {
46
+ clearTimeout(i);
47
+ reject(new DOMException("Delay was aborted.", "AbortError"));
48
+ };
49
+ const done = () => {
50
+ signal?.removeEventListener("abort", abort);
51
+ resolve();
52
+ };
53
+ const i = setTimeout(done, ms);
54
+ signal?.addEventListener("abort", abort, { once: true });
55
+ if (persistent === false) {
56
+ try {
57
+ // @ts-ignore For browser compatibility
58
+ Deno.unrefTimer(i);
59
+ } catch (error) {
60
+ if (!(error instanceof ReferenceError)) {
61
+ throw error;
62
+ }
63
+ console.error("`persistent` option is only available in Deno");
64
+ }
65
+ }
66
+ });
67
+ }
@@ -0,0 +1,18 @@
1
+ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
2
+
3
+ /**
4
+ * Provide help with asynchronous tasks like delays, debouncing, deferring, or
5
+ * pooling.
6
+ *
7
+ * @module
8
+ */
9
+
10
+ export * from "./abortable.ts";
11
+ export * from "./deadline.ts";
12
+ export * from "./debounce.ts";
13
+ export * from "./deferred.ts";
14
+ export * from "./delay.ts";
15
+ export * from "./mux_async_iterator.ts";
16
+ export * from "./pool.ts";
17
+ export * from "./tee.ts";
18
+ export * from "./retry.ts";
@@ -0,0 +1,97 @@
1
+ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
2
+ // This module is browser compatible.
3
+
4
+ import { Deferred, deferred } from "./deferred.ts";
5
+
6
+ interface TaggedYieldedValue<T> {
7
+ iterator: AsyncIterator<T>;
8
+ value: T;
9
+ }
10
+
11
+ /**
12
+ * The MuxAsyncIterator class multiplexes multiple async iterators into a single
13
+ * stream. It currently makes an assumption that the final result (the value
14
+ * returned and not yielded from the iterator) does not matter; if there is any
15
+ * result, it is discarded.
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * import { MuxAsyncIterator } from "https://deno.land/std@$STD_VERSION/async/mod.ts";
20
+ *
21
+ * async function* gen123(): AsyncIterableIterator<number> {
22
+ * yield 1;
23
+ * yield 2;
24
+ * yield 3;
25
+ * }
26
+ *
27
+ * async function* gen456(): AsyncIterableIterator<number> {
28
+ * yield 4;
29
+ * yield 5;
30
+ * yield 6;
31
+ * }
32
+ *
33
+ * const mux = new MuxAsyncIterator<number>();
34
+ * mux.add(gen123());
35
+ * mux.add(gen456());
36
+ * for await (const value of mux) {
37
+ * // ...
38
+ * }
39
+ * // ..
40
+ * ```
41
+ */
42
+ export class MuxAsyncIterator<T> implements AsyncIterable<T> {
43
+ #iteratorCount = 0;
44
+ #yields: Array<TaggedYieldedValue<T>> = [];
45
+ // deno-lint-ignore no-explicit-any
46
+ #throws: any[] = [];
47
+ #signal: Deferred<void> = deferred();
48
+
49
+ add(iterable: AsyncIterable<T>) {
50
+ ++this.#iteratorCount;
51
+ this.#callIteratorNext(iterable[Symbol.asyncIterator]());
52
+ }
53
+
54
+ async #callIteratorNext(
55
+ iterator: AsyncIterator<T>,
56
+ ) {
57
+ try {
58
+ const { value, done } = await iterator.next();
59
+ if (done) {
60
+ --this.#iteratorCount;
61
+ } else {
62
+ this.#yields.push({ iterator, value });
63
+ }
64
+ } catch (e) {
65
+ this.#throws.push(e);
66
+ }
67
+ this.#signal.resolve();
68
+ }
69
+
70
+ async *iterate(): AsyncIterableIterator<T> {
71
+ while (this.#iteratorCount > 0) {
72
+ // Sleep until any of the wrapped iterators yields.
73
+ await this.#signal;
74
+
75
+ // Note that while we're looping over `yields`, new items may be added.
76
+ for (let i = 0; i < this.#yields.length; i++) {
77
+ const { iterator, value } = this.#yields[i];
78
+ yield value;
79
+ this.#callIteratorNext(iterator);
80
+ }
81
+
82
+ if (this.#throws.length) {
83
+ for (const e of this.#throws) {
84
+ throw e;
85
+ }
86
+ this.#throws.length = 0;
87
+ }
88
+ // Clear the `yields` list and reset the `signal` promise.
89
+ this.#yields.length = 0;
90
+ this.#signal = deferred();
91
+ }
92
+ }
93
+
94
+ [Symbol.asyncIterator](): AsyncIterator<T> {
95
+ return this.iterate();
96
+ }
97
+ }
@@ -0,0 +1,95 @@
1
+ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
2
+
3
+ export const ERROR_WHILE_MAPPING_MESSAGE = "Threw while mapping.";
4
+
5
+ /**
6
+ * pooledMap transforms values from an (async) iterable into another async
7
+ * iterable. The transforms are done concurrently, with a max concurrency
8
+ * defined by the poolLimit.
9
+ *
10
+ * If an error is thrown from `iterableFn`, no new transformations will begin.
11
+ * All currently executing transformations are allowed to finish and still
12
+ * yielded on success. After that, the rejections among them are gathered and
13
+ * thrown by the iterator in an `AggregateError`.
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * import { pooledMap } from "https://deno.land/std@$STD_VERSION/async/pool.ts";
18
+ *
19
+ * const results = pooledMap(
20
+ * 2,
21
+ * [1, 2, 3],
22
+ * (i) => new Promise((r) => setTimeout(() => r(i), 1000)),
23
+ * );
24
+ *
25
+ * for await (const value of results) {
26
+ * // ...
27
+ * }
28
+ * ```
29
+ *
30
+ * @param poolLimit The maximum count of items being processed concurrently.
31
+ * @param array The input array for mapping.
32
+ * @param iteratorFn The function to call for every item of the array.
33
+ */
34
+ export function pooledMap<T, R>(
35
+ poolLimit: number,
36
+ array: Iterable<T> | AsyncIterable<T>,
37
+ iteratorFn: (data: T) => Promise<R>,
38
+ ): AsyncIterableIterator<R> {
39
+ // Create the async iterable that is returned from this function.
40
+ const res = new TransformStream<Promise<R>, R>({
41
+ async transform(
42
+ p: Promise<R>,
43
+ controller: TransformStreamDefaultController<R>,
44
+ ) {
45
+ try {
46
+ const s = await p;
47
+ controller.enqueue(s);
48
+ } catch (e) {
49
+ if (
50
+ e instanceof AggregateError &&
51
+ e.message == ERROR_WHILE_MAPPING_MESSAGE
52
+ ) {
53
+ controller.error(e as unknown);
54
+ }
55
+ }
56
+ },
57
+ });
58
+ // Start processing items from the iterator
59
+ (async () => {
60
+ const writer = res.writable.getWriter();
61
+ const executing: Array<Promise<unknown>> = [];
62
+ try {
63
+ for await (const item of array) {
64
+ const p = Promise.resolve().then(() => iteratorFn(item));
65
+ // Only write on success. If we `writer.write()` a rejected promise,
66
+ // that will end the iteration. We don't want that yet. Instead let it
67
+ // fail the race, taking us to the catch block where all currently
68
+ // executing jobs are allowed to finish and all rejections among them
69
+ // can be reported together.
70
+ writer.write(p);
71
+ const e: Promise<unknown> = p.then(() =>
72
+ executing.splice(executing.indexOf(e), 1)
73
+ );
74
+ executing.push(e);
75
+ if (executing.length >= poolLimit) {
76
+ await Promise.race(executing);
77
+ }
78
+ }
79
+ // Wait until all ongoing events have processed, then close the writer.
80
+ await Promise.all(executing);
81
+ writer.close();
82
+ } catch {
83
+ const errors = [];
84
+ for (const result of await Promise.allSettled(executing)) {
85
+ if (result.status == "rejected") {
86
+ errors.push(result.reason);
87
+ }
88
+ }
89
+ writer.write(Promise.reject(
90
+ new AggregateError(errors, ERROR_WHILE_MAPPING_MESSAGE),
91
+ )).catch(() => {});
92
+ }
93
+ })();
94
+ return res.readable[Symbol.asyncIterator]();
95
+ }