@zyro-sec/util 1.0.2 → 1.0.3
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/docs/layer-node.md +155 -155
- package/package.json +1 -1
- package/script/build.ts +9 -9
- package/script/pack.ts +5 -5
- package/src/abort.ts +9 -9
- package/src/activity-calendar.ts +50 -50
- package/src/artifact.ts +95 -95
- package/src/base64.ts +29 -29
- package/src/binary.ts +30 -30
- package/src/bom.ts +46 -46
- package/src/cross-spawn-spawner.ts +694 -694
- package/src/duration.ts +22 -22
- package/src/effect/app-node-platform.ts +40 -40
- package/src/effect/app-node.ts +14 -14
- package/src/effect/layer-node.ts +360 -360
- package/src/effect-flock.ts +313 -313
- package/src/encode.ts +39 -39
- package/src/execute-output.ts +23 -23
- package/src/flock.ts +346 -346
- package/src/fs-util.ts +271 -271
- package/src/glob.ts +38 -38
- package/src/global-roots.ts +19 -19
- package/src/global-roots.workerd.ts +16 -16
- package/src/global.ts +88 -88
- package/src/hash.ts +11 -11
- package/src/heap-snapshot.ts +44 -44
- package/src/no-timeout-fetch.ts +5 -5
- package/src/npm-config.ts +41 -41
- package/src/npm.ts +557 -557
- package/src/observability/logging.ts +179 -179
- package/src/observability/otlp.ts +109 -109
- package/src/observability/shared.ts +8 -8
- package/src/observability.ts +60 -60
- package/src/open.ts +12 -12
- package/src/patch.ts +382 -382
- package/src/path.ts +149 -149
- package/src/process-signal.ts +23 -23
- package/src/process.ts +270 -270
- package/src/qr.ts +323 -323
- package/src/record.ts +3 -3
- package/src/retry.ts +51 -51
- package/src/runtime/import.bun.ts +10 -10
- package/src/runtime/import.node.ts +61 -61
- package/src/runtime/import.workerd.ts +9 -9
- package/src/runtime-import.ts +1 -1
- package/src/session-mention.ts +24 -24
- package/src/session-title-fallback.ts +36 -36
- package/src/session-turn.ts +82 -82
- package/src/teardown.ts +19 -19
- package/src/text-boundary.ts +36 -36
- package/src/uuid.ts +15 -15
- package/test/fixtures/qr.json +625 -625
- package/tsconfig.build.json +12 -12
- package/tsconfig.dist.json +10 -10
- package/tsconfig.json +14 -14
- package/src/abort.test.ts +0 -16
- package/src/activity-calendar.test.ts +0 -136
- package/src/artifact.test.ts +0 -29
- package/src/base64.test.ts +0 -12
- package/src/binary.test.ts +0 -19
- package/src/bom.test.ts +0 -21
- package/src/encode.test.ts +0 -32
- package/src/execute-output.test.ts +0 -36
- package/src/no-timeout-fetch.test.ts +0 -33
- package/src/open.test.ts +0 -16
- package/src/path.test.ts +0 -89
- package/src/process-signal.test.ts +0 -38
- package/src/retry.test.ts +0 -87
- package/src/session-mention.test.ts +0 -19
- package/src/session-turn.test.ts +0 -131
- package/src/text-boundary.test.ts +0 -39
- package/src/uuid.test.ts +0 -80
- package/test/glob.test.ts +0 -11
- package/test/global-roots.test.ts +0 -62
- package/test/global.test.ts +0 -99
- package/test/observability-otlp.test.ts +0 -45
- package/test/qr.test.ts +0 -113
- package/test/session-title-fallback.test.ts +0 -49
package/docs/layer-node.md
CHANGED
|
@@ -1,155 +1,155 @@
|
|
|
1
|
-
# Layer Graphs
|
|
2
|
-
|
|
3
|
-
`LayerNode` describes replaceable wiring for ordinary Effect Layers. Declarations
|
|
4
|
-
and compilation do not acquire resources. Effect still owns acquisition,
|
|
5
|
-
memoization, scopes, and finalization.
|
|
6
|
-
|
|
7
|
-
There are three values to compose:
|
|
8
|
-
|
|
9
|
-
- A **Node** is a replaceable declaration with a fixed output, error, and tag
|
|
10
|
-
contract. `make` supplies its default implementation; `unbound` leaves that
|
|
11
|
-
implementation for the caller to supply.
|
|
12
|
-
- A **Graph** is a node or an ordered `group` of graphs. Grouping exposes the
|
|
13
|
-
selected roots, not their transitive dependencies. Groups are not replacement
|
|
14
|
-
targets.
|
|
15
|
-
- A **Replacement** is a checked instruction made by `node.replace(...)`.
|
|
16
|
-
Replacement arrays can be stored and concatenated without losing their checks.
|
|
17
|
-
|
|
18
|
-
```ts
|
|
19
|
-
import { Context, Effect, Layer } from "effect"
|
|
20
|
-
import { LayerNode } from "@zyro-sec/util/effect/layer-node"
|
|
21
|
-
|
|
22
|
-
class Database extends Context.Service<Database, { readonly name: string }>()("example/Database") {}
|
|
23
|
-
class Users extends Context.Service<Users, { readonly name: string }>()("example/Users") {}
|
|
24
|
-
|
|
25
|
-
const tags = LayerNode.tags({ global: [], location: ["global"] })
|
|
26
|
-
const global = tags.make("global")
|
|
27
|
-
const location = tags.make("location")
|
|
28
|
-
|
|
29
|
-
const database = global({
|
|
30
|
-
service: Database,
|
|
31
|
-
layer: Layer.succeed(Database, { name: "live" }),
|
|
32
|
-
deps: [],
|
|
33
|
-
})
|
|
34
|
-
const users = location({
|
|
35
|
-
service: Users,
|
|
36
|
-
layer: Layer.effect(
|
|
37
|
-
Users,
|
|
38
|
-
Effect.map(Database, (db) => ({ name: db.name })),
|
|
39
|
-
),
|
|
40
|
-
deps: [database],
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
const replacements: LayerNode.Replacements = [database.replace(Layer.succeed(Database, { name: "test" }))]
|
|
44
|
-
|
|
45
|
-
const layer = LayerNode.compile(LayerNode.group([database, users]), {
|
|
46
|
-
replacements,
|
|
47
|
-
shared: tags.values.global,
|
|
48
|
-
})
|
|
49
|
-
// Layer<Database | Users>: both selected roots remain available.
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
## Substitution
|
|
53
|
-
|
|
54
|
-
Replacements target the exact declaration, not its display name or service key.
|
|
55
|
-
Two independently declared nodes can provide the same service in different
|
|
56
|
-
branches without capturing each other's overrides.
|
|
57
|
-
|
|
58
|
-
Keep the original declaration as the target when configuring a default:
|
|
59
|
-
|
|
60
|
-
```ts
|
|
61
|
-
const replacements = [Database.node.replace(Database.configured(options.database)), ...profile.replacements]
|
|
62
|
-
```
|
|
63
|
-
|
|
64
|
-
The replacement may be a closed Layer or another node. It must provide all the
|
|
65
|
-
source's outputs, may provide additional outputs, must not introduce new errors,
|
|
66
|
-
and must retain the source's tag. An open Layer must first be wired into a node.
|
|
67
|
-
Replacing a node prunes its original dependencies; replacing it with another node
|
|
68
|
-
uses that node's dependencies instead.
|
|
69
|
-
|
|
70
|
-
The rules are:
|
|
71
|
-
|
|
72
|
-
- The last replacement for the same source wins, before any traversal.
|
|
73
|
-
- Replacements also apply inside replacement dependency graphs and through
|
|
74
|
-
replacement chains.
|
|
75
|
-
- Replacing a node with itself is the identity operation.
|
|
76
|
-
- Unreachable replacements neither acquire resources nor traverse their graphs.
|
|
77
|
-
- Unbound nodes and cycles in the effective graph fail during compilation.
|
|
78
|
-
|
|
79
|
-
A node's contract is invariant. It cannot be widened or narrowed to authorize a
|
|
80
|
-
replacement that would violate another use of the original declaration. A graph
|
|
81
|
-
can forget outputs like an ordinary Layer, but cannot be used as a weaker
|
|
82
|
-
replacement handle.
|
|
83
|
-
|
|
84
|
-
Choice and composition are different: `group([a, b])` provides both roots, while
|
|
85
|
-
`group([condition ? a : b])` promises only outputs common to both alternatives.
|
|
86
|
-
The same rule applies to conditional implementation Layers. Use tuples for known
|
|
87
|
-
roots; a dynamic array can be empty and therefore guarantees no service outputs.
|
|
88
|
-
Every alternative of a conditional replacement must satisfy the source contract.
|
|
89
|
-
|
|
90
|
-
## Decoration
|
|
91
|
-
|
|
92
|
-
Use `mapLayer` to decorate a default implementation without duplicating or
|
|
93
|
-
inspecting its dependencies:
|
|
94
|
-
|
|
95
|
-
```ts
|
|
96
|
-
const observed = database.mapLayer((layer) => layer.pipe(Layer.tap(() => Effect.logDebug("database ready"))))
|
|
97
|
-
const replacements = [database.replace(observed)]
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
The callback is parametric in the Layer's requirements: it must preserve the
|
|
101
|
-
declared wiring and service/error contract. `mapLayer` returns a new node; it does
|
|
102
|
-
not mutate or automatically override the original. Only implemented nodes offer
|
|
103
|
-
this operation, not unbound slots or groups.
|
|
104
|
-
|
|
105
|
-
## Lifetimes
|
|
106
|
-
|
|
107
|
-
Without `shared`, compilation uses Effect's enclosing memo map throughout.
|
|
108
|
-
With `shared: tag`, matching nodes and their dependency subgraphs use that map;
|
|
109
|
-
remaining wiring uses one fresh map per build. A shared service's dependencies
|
|
110
|
-
must live at least as long as the service, including untagged helpers. This keeps application globals shared while isolating each
|
|
111
|
-
Location, without exposing incomplete graphs or losing shared root outputs.
|
|
112
|
-
|
|
113
|
-
The enclosing memo map remains ambient during construction, so a lazy LayerMap
|
|
114
|
-
captures the shared ancestry rather than a Location's private cache. Cache
|
|
115
|
-
selection is internal metadata, not an extra service output of each dependency.
|
|
116
|
-
|
|
117
|
-
Native Layer identity still controls acquisition within each memo map. Use a
|
|
118
|
-
distinct implementation Layer for differently wired definitions; compilation
|
|
119
|
-
rejects conflicting wiring of the same Layer within one graph's memo domain.
|
|
120
|
-
Compiled wiring is memoized once per build and memo domain, so a shared dependency
|
|
121
|
-
graph is not expanded into a tree during acquisition. Original Layer identities
|
|
122
|
-
still determine service sharing across separately compiled builds. An explicitly
|
|
123
|
-
fresh implementation refreshes its acquisition when its wiring is built.
|
|
124
|
-
|
|
125
|
-
Groups preserve root startup order and associate transparently. A node's
|
|
126
|
-
dependencies are built concurrently through native `Layer.provide`. Named
|
|
127
|
-
startup actions and Context references are supported even when their required
|
|
128
|
-
service output type is `never`.
|
|
129
|
-
|
|
130
|
-
## Migration
|
|
131
|
-
|
|
132
|
-
| Previous API | Current API |
|
|
133
|
-
| ------------------------------------------------ | ----------------------------------------------- |
|
|
134
|
-
| `[source, replacement]` | `source.replace(replacement)` |
|
|
135
|
-
| `compile(graph, replacements)` | `compile(graph, { replacements })` |
|
|
136
|
-
| `hoist`, compile both halves, `fresh`, `provide` | `compile(graph, { replacements, shared: tag })` |
|
|
137
|
-
| Spread a node and overwrite `implementation` | `node.mapLayer(transform)` |
|
|
138
|
-
| Generic root parameter `Node<A, E, any>` | `Graph<A, E>` |
|
|
139
|
-
|
|
140
|
-
An application can prepend a default binding and append caller overrides instead
|
|
141
|
-
of inspecting the original graph with `hasUnbound`. Unused defaults are pruned,
|
|
142
|
-
and defaults introduced as dependencies by an override are found naturally.
|
|
143
|
-
|
|
144
|
-
Code that reconstructed a replacement source by name must retain the original
|
|
145
|
-
declaration instead. Compiler representation fields are no longer public.
|
|
146
|
-
|
|
147
|
-
`PersistentPty.configured(options)` now returns a wired node, like the other Core
|
|
148
|
-
configuration factories. `PersistentPty.layer` remains the default raw Layer.
|
|
149
|
-
|
|
150
|
-
## Verification
|
|
151
|
-
|
|
152
|
-
Run `bun run typecheck:dist` in `packages/util` to build the package and check the
|
|
153
|
-
same positive and negative contract tests against its emitted declarations.
|
|
154
|
-
This ensures package consumers receive the same inference and replacement checks
|
|
155
|
-
as callers importing the source.
|
|
1
|
+
# Layer Graphs
|
|
2
|
+
|
|
3
|
+
`LayerNode` describes replaceable wiring for ordinary Effect Layers. Declarations
|
|
4
|
+
and compilation do not acquire resources. Effect still owns acquisition,
|
|
5
|
+
memoization, scopes, and finalization.
|
|
6
|
+
|
|
7
|
+
There are three values to compose:
|
|
8
|
+
|
|
9
|
+
- A **Node** is a replaceable declaration with a fixed output, error, and tag
|
|
10
|
+
contract. `make` supplies its default implementation; `unbound` leaves that
|
|
11
|
+
implementation for the caller to supply.
|
|
12
|
+
- A **Graph** is a node or an ordered `group` of graphs. Grouping exposes the
|
|
13
|
+
selected roots, not their transitive dependencies. Groups are not replacement
|
|
14
|
+
targets.
|
|
15
|
+
- A **Replacement** is a checked instruction made by `node.replace(...)`.
|
|
16
|
+
Replacement arrays can be stored and concatenated without losing their checks.
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { Context, Effect, Layer } from "effect"
|
|
20
|
+
import { LayerNode } from "@zyro-sec/util/effect/layer-node"
|
|
21
|
+
|
|
22
|
+
class Database extends Context.Service<Database, { readonly name: string }>()("example/Database") {}
|
|
23
|
+
class Users extends Context.Service<Users, { readonly name: string }>()("example/Users") {}
|
|
24
|
+
|
|
25
|
+
const tags = LayerNode.tags({ global: [], location: ["global"] })
|
|
26
|
+
const global = tags.make("global")
|
|
27
|
+
const location = tags.make("location")
|
|
28
|
+
|
|
29
|
+
const database = global({
|
|
30
|
+
service: Database,
|
|
31
|
+
layer: Layer.succeed(Database, { name: "live" }),
|
|
32
|
+
deps: [],
|
|
33
|
+
})
|
|
34
|
+
const users = location({
|
|
35
|
+
service: Users,
|
|
36
|
+
layer: Layer.effect(
|
|
37
|
+
Users,
|
|
38
|
+
Effect.map(Database, (db) => ({ name: db.name })),
|
|
39
|
+
),
|
|
40
|
+
deps: [database],
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
const replacements: LayerNode.Replacements = [database.replace(Layer.succeed(Database, { name: "test" }))]
|
|
44
|
+
|
|
45
|
+
const layer = LayerNode.compile(LayerNode.group([database, users]), {
|
|
46
|
+
replacements,
|
|
47
|
+
shared: tags.values.global,
|
|
48
|
+
})
|
|
49
|
+
// Layer<Database | Users>: both selected roots remain available.
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Substitution
|
|
53
|
+
|
|
54
|
+
Replacements target the exact declaration, not its display name or service key.
|
|
55
|
+
Two independently declared nodes can provide the same service in different
|
|
56
|
+
branches without capturing each other's overrides.
|
|
57
|
+
|
|
58
|
+
Keep the original declaration as the target when configuring a default:
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
const replacements = [Database.node.replace(Database.configured(options.database)), ...profile.replacements]
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
The replacement may be a closed Layer or another node. It must provide all the
|
|
65
|
+
source's outputs, may provide additional outputs, must not introduce new errors,
|
|
66
|
+
and must retain the source's tag. An open Layer must first be wired into a node.
|
|
67
|
+
Replacing a node prunes its original dependencies; replacing it with another node
|
|
68
|
+
uses that node's dependencies instead.
|
|
69
|
+
|
|
70
|
+
The rules are:
|
|
71
|
+
|
|
72
|
+
- The last replacement for the same source wins, before any traversal.
|
|
73
|
+
- Replacements also apply inside replacement dependency graphs and through
|
|
74
|
+
replacement chains.
|
|
75
|
+
- Replacing a node with itself is the identity operation.
|
|
76
|
+
- Unreachable replacements neither acquire resources nor traverse their graphs.
|
|
77
|
+
- Unbound nodes and cycles in the effective graph fail during compilation.
|
|
78
|
+
|
|
79
|
+
A node's contract is invariant. It cannot be widened or narrowed to authorize a
|
|
80
|
+
replacement that would violate another use of the original declaration. A graph
|
|
81
|
+
can forget outputs like an ordinary Layer, but cannot be used as a weaker
|
|
82
|
+
replacement handle.
|
|
83
|
+
|
|
84
|
+
Choice and composition are different: `group([a, b])` provides both roots, while
|
|
85
|
+
`group([condition ? a : b])` promises only outputs common to both alternatives.
|
|
86
|
+
The same rule applies to conditional implementation Layers. Use tuples for known
|
|
87
|
+
roots; a dynamic array can be empty and therefore guarantees no service outputs.
|
|
88
|
+
Every alternative of a conditional replacement must satisfy the source contract.
|
|
89
|
+
|
|
90
|
+
## Decoration
|
|
91
|
+
|
|
92
|
+
Use `mapLayer` to decorate a default implementation without duplicating or
|
|
93
|
+
inspecting its dependencies:
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
const observed = database.mapLayer((layer) => layer.pipe(Layer.tap(() => Effect.logDebug("database ready"))))
|
|
97
|
+
const replacements = [database.replace(observed)]
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
The callback is parametric in the Layer's requirements: it must preserve the
|
|
101
|
+
declared wiring and service/error contract. `mapLayer` returns a new node; it does
|
|
102
|
+
not mutate or automatically override the original. Only implemented nodes offer
|
|
103
|
+
this operation, not unbound slots or groups.
|
|
104
|
+
|
|
105
|
+
## Lifetimes
|
|
106
|
+
|
|
107
|
+
Without `shared`, compilation uses Effect's enclosing memo map throughout.
|
|
108
|
+
With `shared: tag`, matching nodes and their dependency subgraphs use that map;
|
|
109
|
+
remaining wiring uses one fresh map per build. A shared service's dependencies
|
|
110
|
+
must live at least as long as the service, including untagged helpers. This keeps application globals shared while isolating each
|
|
111
|
+
Location, without exposing incomplete graphs or losing shared root outputs.
|
|
112
|
+
|
|
113
|
+
The enclosing memo map remains ambient during construction, so a lazy LayerMap
|
|
114
|
+
captures the shared ancestry rather than a Location's private cache. Cache
|
|
115
|
+
selection is internal metadata, not an extra service output of each dependency.
|
|
116
|
+
|
|
117
|
+
Native Layer identity still controls acquisition within each memo map. Use a
|
|
118
|
+
distinct implementation Layer for differently wired definitions; compilation
|
|
119
|
+
rejects conflicting wiring of the same Layer within one graph's memo domain.
|
|
120
|
+
Compiled wiring is memoized once per build and memo domain, so a shared dependency
|
|
121
|
+
graph is not expanded into a tree during acquisition. Original Layer identities
|
|
122
|
+
still determine service sharing across separately compiled builds. An explicitly
|
|
123
|
+
fresh implementation refreshes its acquisition when its wiring is built.
|
|
124
|
+
|
|
125
|
+
Groups preserve root startup order and associate transparently. A node's
|
|
126
|
+
dependencies are built concurrently through native `Layer.provide`. Named
|
|
127
|
+
startup actions and Context references are supported even when their required
|
|
128
|
+
service output type is `never`.
|
|
129
|
+
|
|
130
|
+
## Migration
|
|
131
|
+
|
|
132
|
+
| Previous API | Current API |
|
|
133
|
+
| ------------------------------------------------ | ----------------------------------------------- |
|
|
134
|
+
| `[source, replacement]` | `source.replace(replacement)` |
|
|
135
|
+
| `compile(graph, replacements)` | `compile(graph, { replacements })` |
|
|
136
|
+
| `hoist`, compile both halves, `fresh`, `provide` | `compile(graph, { replacements, shared: tag })` |
|
|
137
|
+
| Spread a node and overwrite `implementation` | `node.mapLayer(transform)` |
|
|
138
|
+
| Generic root parameter `Node<A, E, any>` | `Graph<A, E>` |
|
|
139
|
+
|
|
140
|
+
An application can prepend a default binding and append caller overrides instead
|
|
141
|
+
of inspecting the original graph with `hasUnbound`. Unused defaults are pruned,
|
|
142
|
+
and defaults introduced as dependencies by an override are found naturally.
|
|
143
|
+
|
|
144
|
+
Code that reconstructed a replacement source by name must retain the original
|
|
145
|
+
declaration instead. Compiler representation fields are no longer public.
|
|
146
|
+
|
|
147
|
+
`PersistentPty.configured(options)` now returns a wired node, like the other Core
|
|
148
|
+
configuration factories. `PersistentPty.layer` remains the default raw Layer.
|
|
149
|
+
|
|
150
|
+
## Verification
|
|
151
|
+
|
|
152
|
+
Run `bun run typecheck:dist` in `packages/util` to build the package and check the
|
|
153
|
+
same positive and negative contract tests against its emitted declarations.
|
|
154
|
+
This ensures package consumers receive the same inference and replacement checks
|
|
155
|
+
as callers importing the source.
|
package/package.json
CHANGED
package/script/build.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
2
|
-
|
|
3
|
-
import { $ } from "bun"
|
|
4
|
-
import { fileURLToPath } from "node:url"
|
|
5
|
-
|
|
6
|
-
process.chdir(fileURLToPath(new URL("..", import.meta.url)))
|
|
7
|
-
|
|
8
|
-
await $`rm -rf dist`
|
|
9
|
-
await $`bun tsc -p tsconfig.build.json`
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
|
|
3
|
+
import { $ } from "bun"
|
|
4
|
+
import { fileURLToPath } from "node:url"
|
|
5
|
+
|
|
6
|
+
process.chdir(fileURLToPath(new URL("..", import.meta.url)))
|
|
7
|
+
|
|
8
|
+
await $`rm -rf dist`
|
|
9
|
+
await $`bun tsc -p tsconfig.build.json`
|
package/script/pack.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
2
|
-
import path from "node:path"
|
|
3
|
-
import { pack } from "@zyro-sec/script/pack"
|
|
4
|
-
|
|
5
|
-
await pack(path.resolve(import.meta.dir, ".."))
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
import path from "node:path"
|
|
3
|
+
import { pack } from "@zyro-sec/script/pack"
|
|
4
|
+
|
|
5
|
+
await pack(path.resolve(import.meta.dir, ".."))
|
package/src/abort.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
// `controller.abort()` makes a new DOMException that records the caller's stack, and V8 keeps every frame's function
|
|
2
|
-
// and closure alive until something reads `.stack`. Aborting from a Solid cleanup or update then pins the whole batch
|
|
3
|
-
// it ran in, disposed computations and detached DOM included, for as long as the signal is reachable. This reason is
|
|
4
|
-
// made once, so its stack holds only this module's load.
|
|
5
|
-
const reason = new DOMException("signal is aborted without reason", "AbortError")
|
|
6
|
-
|
|
7
|
-
export function abort(controller: AbortController | undefined) {
|
|
8
|
-
controller?.abort(reason)
|
|
9
|
-
}
|
|
1
|
+
// `controller.abort()` makes a new DOMException that records the caller's stack, and V8 keeps every frame's function
|
|
2
|
+
// and closure alive until something reads `.stack`. Aborting from a Solid cleanup or update then pins the whole batch
|
|
3
|
+
// it ran in, disposed computations and detached DOM included, for as long as the signal is reachable. This reason is
|
|
4
|
+
// made once, so its stack holds only this module's load.
|
|
5
|
+
const reason = new DOMException("signal is aborted without reason", "AbortError")
|
|
6
|
+
|
|
7
|
+
export function abort(controller: AbortController | undefined) {
|
|
8
|
+
controller?.abort(reason)
|
|
9
|
+
}
|
package/src/activity-calendar.ts
CHANGED
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
export function activityCalendar(input: {
|
|
2
|
-
activity: readonly { date: string; steps: number }[]
|
|
3
|
-
from: number
|
|
4
|
-
to: number
|
|
5
|
-
maxWeeks: number
|
|
6
|
-
}) {
|
|
7
|
-
// Extract local dates first, then use UTC ordinals so DST cannot add or drop cells.
|
|
8
|
-
const ordinal = (time: number) => {
|
|
9
|
-
const date = new Date(time)
|
|
10
|
-
return Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()) / 86_400_000
|
|
11
|
-
}
|
|
12
|
-
const from = ordinal(input.from)
|
|
13
|
-
const to = ordinal(input.to - 1)
|
|
14
|
-
const monday = from - ((new Date(from * 86_400_000).getUTCDay() + 6) % 7)
|
|
15
|
-
const total = input.to <= input.from ? 0 : Math.floor((to - monday) / 7) + 1
|
|
16
|
-
const count = Math.min(total, 53, Math.max(1, Math.floor(input.maxWeeks)))
|
|
17
|
-
const start = monday + Math.max(0, total - count) * 7
|
|
18
|
-
const values = new Map(input.activity.map((day) => [day.date, day.steps]))
|
|
19
|
-
const levels = [...new Set([...values.values()].filter((steps) => steps > 0))].sort((a, b) => a - b)
|
|
20
|
-
const weeks = Array.from({ length: count }, (_, week) =>
|
|
21
|
-
Array.from({ length: 7 }, (_, day) => {
|
|
22
|
-
const date = start + week * 7 + day
|
|
23
|
-
const key = new Date(date * 86_400_000).toISOString().slice(0, 10)
|
|
24
|
-
const steps = values.get(key) ?? 0
|
|
25
|
-
return {
|
|
26
|
-
date: key,
|
|
27
|
-
steps,
|
|
28
|
-
level:
|
|
29
|
-
date < from || date > to
|
|
30
|
-
? -1
|
|
31
|
-
: steps === 0
|
|
32
|
-
? 0
|
|
33
|
-
: Math.max(1, Math.ceil(((levels.indexOf(steps) + 1) / levels.length) * 4)),
|
|
34
|
-
}
|
|
35
|
-
}),
|
|
36
|
-
)
|
|
37
|
-
const formatter = new Intl.DateTimeFormat("en-US", { month: "short", timeZone: "UTC" })
|
|
38
|
-
// Thursday identifies the month owning most of a week; partial edge weeks stay inside the range.
|
|
39
|
-
const months = weeks
|
|
40
|
-
.map((_, week) => ({
|
|
41
|
-
week,
|
|
42
|
-
label: formatter.format(Math.min(to, Math.max(from, start + week * 7 + 3)) * 86_400_000),
|
|
43
|
-
}))
|
|
44
|
-
.filter((month, index, months) => index === 0 || month.label !== months[index - 1].label)
|
|
45
|
-
return {
|
|
46
|
-
weeks,
|
|
47
|
-
months: months.map((month, index) => ({ ...month, span: (months[index + 1]?.week ?? count) - month.week })),
|
|
48
|
-
clipped: count < total,
|
|
49
|
-
}
|
|
50
|
-
}
|
|
1
|
+
export function activityCalendar(input: {
|
|
2
|
+
activity: readonly { date: string; steps: number }[]
|
|
3
|
+
from: number
|
|
4
|
+
to: number
|
|
5
|
+
maxWeeks: number
|
|
6
|
+
}) {
|
|
7
|
+
// Extract local dates first, then use UTC ordinals so DST cannot add or drop cells.
|
|
8
|
+
const ordinal = (time: number) => {
|
|
9
|
+
const date = new Date(time)
|
|
10
|
+
return Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()) / 86_400_000
|
|
11
|
+
}
|
|
12
|
+
const from = ordinal(input.from)
|
|
13
|
+
const to = ordinal(input.to - 1)
|
|
14
|
+
const monday = from - ((new Date(from * 86_400_000).getUTCDay() + 6) % 7)
|
|
15
|
+
const total = input.to <= input.from ? 0 : Math.floor((to - monday) / 7) + 1
|
|
16
|
+
const count = Math.min(total, 53, Math.max(1, Math.floor(input.maxWeeks)))
|
|
17
|
+
const start = monday + Math.max(0, total - count) * 7
|
|
18
|
+
const values = new Map(input.activity.map((day) => [day.date, day.steps]))
|
|
19
|
+
const levels = [...new Set([...values.values()].filter((steps) => steps > 0))].sort((a, b) => a - b)
|
|
20
|
+
const weeks = Array.from({ length: count }, (_, week) =>
|
|
21
|
+
Array.from({ length: 7 }, (_, day) => {
|
|
22
|
+
const date = start + week * 7 + day
|
|
23
|
+
const key = new Date(date * 86_400_000).toISOString().slice(0, 10)
|
|
24
|
+
const steps = values.get(key) ?? 0
|
|
25
|
+
return {
|
|
26
|
+
date: key,
|
|
27
|
+
steps,
|
|
28
|
+
level:
|
|
29
|
+
date < from || date > to
|
|
30
|
+
? -1
|
|
31
|
+
: steps === 0
|
|
32
|
+
? 0
|
|
33
|
+
: Math.max(1, Math.ceil(((levels.indexOf(steps) + 1) / levels.length) * 4)),
|
|
34
|
+
}
|
|
35
|
+
}),
|
|
36
|
+
)
|
|
37
|
+
const formatter = new Intl.DateTimeFormat("en-US", { month: "short", timeZone: "UTC" })
|
|
38
|
+
// Thursday identifies the month owning most of a week; partial edge weeks stay inside the range.
|
|
39
|
+
const months = weeks
|
|
40
|
+
.map((_, week) => ({
|
|
41
|
+
week,
|
|
42
|
+
label: formatter.format(Math.min(to, Math.max(from, start + week * 7 + 3)) * 86_400_000),
|
|
43
|
+
}))
|
|
44
|
+
.filter((month, index, months) => index === 0 || month.label !== months[index - 1].label)
|
|
45
|
+
return {
|
|
46
|
+
weeks,
|
|
47
|
+
months: months.map((month, index) => ({ ...month, span: (months[index + 1]?.week ?? count) - month.week })),
|
|
48
|
+
clipped: count < total,
|
|
49
|
+
}
|
|
50
|
+
}
|