@yaks/hook 0.0.0-stage → 0.2.36

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/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2026 Jeff Peterson
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/README.md CHANGED
@@ -1,3 +1,127 @@
1
- # Temporary Holding Version
1
+ # @yaks/hook
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Defines a graph component for recording incoming webhook requests: their source,
4
+ event name, original HTTP body and headers, and signature-verification result;
5
+ `hooked()`, which turns a captured request into the bundles that record it; and
6
+ `checked()`/`refusal()`, which check a request's signature under a scheme and
7
+ secret the receiver supplies. It does not expose an HTTP endpoint or choose a
8
+ secret; [@yaks/mail](../mail/README.md) pulls requests from a mail edge's hook
9
+ paths and records them this way.
10
+
11
+ ## Stored data
12
+
13
+ `hook{source, event, payload, method, path, headers, verified}` stores:
14
+
15
+ - `source` and `event`: the sender and its event name.
16
+ - `payload` and `headers`: the original body and headers as strings, not parsed
17
+ objects. Both declare `store: "blob"`; load [@yaks/blob](../blob) to store
18
+ their text by content hash, or leave it unloaded for ordinary text properties.
19
+ - `method` and `path`: the HTTP method and request path.
20
+ - `verified`: whether the receiving application verified the sender's signature.
21
+
22
+ All these properties are `stamped`: ordinary graph writes cannot set them. The
23
+ trusted receiving application must supply them through the graph's stamping
24
+ mechanism. An unsigned request can still be recorded with `verified: false`;
25
+ readers decide whether to trust it. Recording an event does not authorize it.
26
+
27
+ The old fields `spool_id`, `received_at`, and `sig_ok` are not in this schema.
28
+ Use graph provenance components for arrival timestamps; the signature result is
29
+ `verified`.
30
+
31
+ ## Exports and use
32
+
33
+ The root import exports `hookDoc`, a JSON Schema document, and the recorder:
34
+
35
+ - `hooked(request, about?)` returns a `hook` bundle with a `doc` title naming
36
+ the event, plus an `about` link when you pass the entity it is for. Apply it
37
+ trusted, since every `hook` property is server-owned.
38
+ - `event(request)` names the event: the sender's `X-GitHub-Event` or
39
+ `X-Event-Key` header, else a JSON body's `event`, `type` or `action`, else the
40
+ method and path.
41
+ - `hookEid(request)` derives the entity id from `source` and the receiver's
42
+ request `id`, so recording one request twice writes one entity.
43
+
44
+ `@yaks/hook/vocab` also exports `docs: [hookDoc]` for plugin loaders.
45
+
46
+ ## Signatures
47
+
48
+ A scheme says how a sender signs. It is plain data, so a receiver can store it
49
+ beside the secret it goes with:
50
+
51
+ - `'stripe'`: the `Stripe-Signature` header, `t=<unix>,v1=<hex>`, an HMAC-SHA256
52
+ of `<t>.<raw body>`. Every `v1` is tried, so a secret being rolled still
53
+ verifies, and a timestamp more than `SKEW` seconds (300) from now is refused.
54
+ - `'github'`: the `X-Hub-Signature-256` header, `sha256=<hex>` of the body.
55
+ - `{header, prefix?, encoding?}`: an HMAC-SHA256 of the body in the named
56
+ header, written as hex (the default) or base64, after an optional prefix.
57
+ - `'telnyx'`: the `telnyx-signature-ed25519` header, a base64 Ed25519 signature
58
+ of `<telnyx-timestamp>|<raw body>`, refused beyond `SKEW` seconds.
59
+ - `'standard'`: [Standard Webhooks](https://www.standardwebhooks.com), as OpenAI
60
+ signs: `webhook-signature` carries `v1,<base64>`, several space apart while a
61
+ secret is rolled, each an HMAC-SHA256 of
62
+ `<webhook-id>.<webhook-timestamp>.<raw body>` under the bytes of the secret's
63
+ base64 after `whsec_`, refused beyond `SKEW` seconds.
64
+ - `{signature, timestamp, join}`: the same for any sender signing with Ed25519:
65
+ the signature header, the timestamp header, and what joins the timestamp to
66
+ the body. The secret is the sender's public key, base64.
67
+
68
+ The receiver passes the secret; this package never learns where it came from.
69
+
70
+ - `refusal(scheme, secret, body, headers, now?)` answers `''` when the signature
71
+ holds and otherwise the reason, such as `'no Stripe-Signature header'`,
72
+ `'the signature is not well formed'` or `'the signature does not match'`.
73
+ `headers` is anything with `get(name)`, such as a fetch `Headers`.
74
+ - `ours(reason)` says whether a refusal can mean the receiver's own secret is
75
+ wrong, and so is worth reporting. Only `'the signature does not match'` can: a
76
+ sender holding the secret always sends a complete, fresh, well-formed
77
+ signature (a 32-byte digest). No header, no timestamp, a stale timestamp or a
78
+ malformed digest is the caller's own bad input, which anyone can send; refuse
79
+ it and report nothing.
80
+ - `checked(request, scheme, secret, now?)` returns the captured request with
81
+ `verified` set, ready for `hooked()`.
82
+
83
+ The body must be the raw text that arrived: a signature covers bytes, and a
84
+ parsed and re-serialized body is not what the sender signed. Every compare goes
85
+ through WebCrypto's `verify`, so it runs in constant time.
86
+
87
+ ```ts
88
+ import { assertObjectMatch } from '@std/assert'
89
+ import { checked, hooked } from '@yaks/hook'
90
+
91
+ // What GitHub sends: the body, signed with the secret the receiver shares.
92
+ let secret = 'a shared secret'
93
+ let body = '{"action":"opened"}'
94
+ // The GitHub-style signature for this exact body and secret.
95
+ let sent = {
96
+ 'x-hub-signature-256':
97
+ 'sha256=5607d3374e7be3c79bb6530a2776fcf125a622b670b1ae24188ff4910a0b9221',
98
+ }
99
+
100
+ let request = {
101
+ id: 'r1',
102
+ source: 'github',
103
+ body,
104
+ headers: JSON.stringify(sent),
105
+ }
106
+ let [hook] = hooked(await checked(request, 'github', secret))
107
+ assertObjectMatch(hook, { hook: { verified: true } })
108
+ ```
109
+
110
+ ```ts
111
+ import { hookDoc } from '@yaks/hook'
112
+ import { docDoc } from '@yaks/doc'
113
+ import { loadVocab } from '@yaks/vocab'
114
+
115
+ const vocab = loadVocab([docDoc, hookDoc])
116
+ // Give vocab to your graph and storage adapter. Once your receiver has recorded
117
+ // requests, read them with: await g.read('.hook.verified=true')
118
+ ```
119
+
120
+ An entity is a record identified by `entity.eid`. A component, such as `hook`,
121
+ is a named object stored on that record. `docDoc` provides the optional title
122
+ and body component and the display kind referenced by this vocabulary.
123
+
124
+ ## Compatibility
125
+
126
+ Deno, Node, browsers and Workers. `hooked()` is pure: no runtime calls and no
127
+ storage implementation. The signature checks use WebCrypto.
@@ -0,0 +1,28 @@
1
+ import type { Bundle, Eid } from "@yaks/graph";
2
+ /** A delivered request, as its receiver captured it. */ export type Request = {
3
+ /** the id the receiver gave it — with `source`, what makes it one entity */ id: string;
4
+ /** who sent it */ source: string;
5
+ /** the HTTP method it arrived by */ method?: string;
6
+ /** the path it arrived on */ path?: string;
7
+ /** the headers as sent: a JSON object, as text */ headers?: string;
8
+ /** the body exactly as delivered */ body?: string;
9
+ /** whether the sender's signature verified, where the receiver checked */ verified?: boolean;
10
+ };
11
+ /**
12
+ * The event a request reports, best first.
13
+ *
14
+ * ```ts
15
+ * import { event } from '@yaks/hook'
16
+ * event({ id: '1', source: 'gh', headers: '{"X-GitHub-Event":"push"}' }) // 'push'
17
+ * event({ id: '2', source: 'ph', body: '{"type":"issue"}' }) // 'issue'
18
+ * event({ id: '3', source: 'x', method: 'POST', path: '/hook/a' }) // 'POST /hook/a'
19
+ * ```
20
+ */ export declare let event: (r: Request) => string;
21
+ /** The id a request is recorded under: the same request, the same entity. */ export declare let hookEid: (r: Request) => Eid;
22
+ /**
23
+ * A delivered request → the bundles that record it: a `hook` carrying it as it
24
+ * arrived, a `doc` title naming it, and an `about` link to the entity it is
25
+ * for when the receiver knows one. Every `hook` property is server-owned, so
26
+ * apply these trusted.
27
+ */ export declare let hooked: (r: Request, about?: Eid) => Bundle[];
28
+ //# sourceMappingURL=hooked.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hooked.d.ts","sources":["../hooked.ts"],"names":[],"mappings":"AAeA,cAAc,MAAM,EAAE,GAAG,2BAAgC;AAKzD,sDAAsD,GACtD,YAAY;EACV,0EAA0E,GAC1E,IAAI,MAAM;EACV,gBAAgB,GAChB,QAAQ,MAAM;EACd,kCAAkC,GAClC,SAAS,MAAM;EACf,2BAA2B,GAC3B,OAAO,MAAM;EACb,gDAAgD,GAChD,UAAU,MAAM;EAChB,kCAAkC,GAClC,OAAO,MAAM;EACb,wEAAwE,GACxE,WAAW,OAAO;;AA2BpB;;;;;;;;;CASC,GACD,OAAO,YAAI,QAAS,GAAG,YAAU,MAAM,CAEK;AAE5C,2EAA2E,GAC3E,OAAO,YAAI,UAAW,GAAG,YAAU,IAA6C;AAEhF;;;;;CAKC,GACD,OAAO,YAAI,SAAU,GAAG,SAAS,QAAQ,QAAM,SAmB9C"}
package/_dist/mod.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ /**
2
+ * An event another system delivered. The payload is kept as it arrived —
3
+ * body, method, path, headers — beside the result of checking its signature,
4
+ * because an unsigned hook is still something that happened, and whoever reads
5
+ * it decides what to trust.
6
+ *
7
+ * The components; `hooked()`: a captured request in, the bundles that record
8
+ * it out; and `checked()`/`refusal()`/`ours()`: whether a request is signed
9
+ * under a scheme and secret the receiver names, and whether a refusal can mean
10
+ * the receiver's secret is wrong.
11
+ */ export { hookDoc } from "./vocab.js";
12
+ export { event, hooked, hookEid, type Request } from "./hooked.js";
13
+ export { checked, type HeaderLookup, type Hmac, ours, refusal, type Scheme, type SecretHeader, SKEW } from "./signed.js";
14
+ //# sourceMappingURL=mod.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.d.ts","sources":["../mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;CAUC,GAED,SAAS,OAAO,qBAAoB;AACpC,SAAS,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,OAAO,sBAAqB;AAClE,SACE,OAAO,EACP,KAAK,YAAY,EACjB,KAAK,IAAI,EACT,IAAI,EACJ,OAAO,EACP,KAAK,MAAM,EACX,KAAK,YAAY,EACjB,IAAI,sBACc"}
@@ -0,0 +1,51 @@
1
+ import { type Request } from "./hooked.js";
2
+ /** An HMAC-SHA256 of the raw body, in the named header. */ export type Hmac = {
3
+ /** the header carrying the signature */ header: string;
4
+ /** what precedes the digest in the header, such as `sha256=` */ prefix?: string;
5
+ /** how the digest is written: hex (the default) or base64 */ encoding?: 'hex' | 'base64';
6
+ /** A timestamp signed as `[version + separator] + time + separator + body`. */ timestamp?: {
7
+ header: string;
8
+ separator: string;
9
+ version?: string;
10
+ };
11
+ };
12
+ /** An Ed25519 signature over `<timestamp><join><raw body>`, base64 in one
13
+ * header, the timestamp (unix seconds) in another. The secret it is checked
14
+ * under is the sender's public key, base64. */ export type Ed25519 = {
15
+ /** the header carrying the signature */ signature: string;
16
+ /** the header carrying the unix time it was signed */ timestamp: string;
17
+ /** what stands between the timestamp and the body in what was signed */ join: string;
18
+ };
19
+ /** A provider that authenticates with a secret header rather than a signature. */ export type SecretHeader = {
20
+ header: string;
21
+ token: true;
22
+ };
23
+ /** Signature verification or bearer-secret authentication, specified as data. */ export type Scheme = 'stripe' | 'github' | 'telnyx' | 'standard' | Hmac | Ed25519 | SecretHeader;
24
+ /** Where a signature's header is read from: a fetch `Headers` is one. */ export type HeaderLookup = {
25
+ get(name: string): string | null;
26
+ };
27
+ export declare let SKEW: number;
28
+ /**
29
+ * Why a request is not signed under this scheme and secret, or '' when it is.
30
+ *
31
+ * ```ts
32
+ * import { refusal } from '@yaks/hook'
33
+ * await refusal('github', 'a secret', '{}', new Headers())
34
+ * // 'no X-Hub-Signature-256 header'
35
+ * ```
36
+ */ export declare let refusal: (scheme: Scheme, secret: string, body: string, headers: HeaderLookup, now?: number) => Promise<string>;
37
+ /**
38
+ * Whether a {@link refusal} can mean the receiver's own secret is wrong, and so
39
+ * is worth reporting: only a complete, fresh, well-formed signature that does
40
+ * not match. Every other refusal is the caller's own bad input.
41
+ *
42
+ * ```ts
43
+ * import { ours, refusal } from '@yaks/hook'
44
+ * ours(await refusal('stripe', 'a secret', '{}', new Headers())) // false
45
+ * ```
46
+ */ export declare let ours: (why: string) => boolean;
47
+ /**
48
+ * A captured request with its `verified` set by checking it under this scheme
49
+ * and secret, ready for {@link hooked}.
50
+ */ export declare let checked: (r: Request, scheme: Scheme, secret: string, now?: number) => Promise<Request>;
51
+ //# sourceMappingURL=signed.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signed.d.ts","sources":["../signed.ts"],"names":[],"mappings":"AAwBA,SAAiB,KAAK,OAAO,sBAAqB;AAElD,yDAAyD,GACzD,YAAY;EACV,sCAAsC,GACtC,QAAQ,MAAM;EACd,8DAA8D,GAC9D,SAAS,MAAM;EACf,2DAA2D,GAC3D,WAAW,QAAQ;EACnB,6EAA6E,GAC7E;IAAc,QAAQ,MAAM;IAAE,WAAW,MAAM;IAAE,UAAU,MAAM;;;AAGnE;;6CAE6C,GAC7C,YAAY;EACV,sCAAsC,GACtC,WAAW,MAAM;EACjB,oDAAoD,GACpD,WAAW,MAAM;EACjB,sEAAsE,GACtE,MAAM,MAAM;;AAGd,gFAAgF,GAChF,YAAY;EAAiB,QAAQ,MAAM;EAAE,OAAO,IAAI;;AAExD,+EAA+E,GAC/E,YAAY,SACR,WACA,WACA,WACA,aACA,OACA,UACA;AAEJ,uEAAuE,GACvE,YAAY;EAAiB,IAAI,MAAM,MAAM,GAAG,MAAM,GAAG,IAAI;;AAM7D,OAAO,YAAI,aAAU;AAmMrB;;;;;;;;CAQC,GACD,OAAO,YAAI,UACT,QAAQ,QACR,QAAQ,MAAM,EACd,MAAM,MAAM,EACZ,SAAS,cACT,MAAK,MAAM,KACV,QAAQ,MAAM,EAW2D;AAE5E;;;;;;;;;CASC,GACD,OAAO,YAAI,OAAQ,KAAK,MAAM,KAAG,OAAO,CAAmB;AAE3D;;;CAGC,GACD,OAAO,YAAI,UACT,GAAG,SACH,QAAQ,QACR,QAAQ,MAAM,EACd,MAAK,MAAM,KACV,QAAQ,SAKT"}
@@ -0,0 +1,6 @@
1
+ import type { VocabDoc } from "@yaks/vocab";
2
+ /** The hook vocabulary, in the form `loadVocab` accepts. */ export declare let hookDoc: VocabDoc;
3
+ /** What this package is: its manifest's description, the one place it is
4
+ * written, which a host stamps on each document here. */ export declare let description: string;
5
+ /** Every document this plugin declares. */ export declare let docs: VocabDoc[];
6
+ //# sourceMappingURL=vocab.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vocab.d.ts","sources":["../vocab.ts"],"names":[],"mappings":"AAIA,cAAc,QAAQ,2BAAgC;AAItD,0DAA0D,GAC1D,OAAO,YAAI,SAAS,SAA0B;AAE9C;uDACuD,GACvD,OAAO,YAAI,aAAa,MAAM,CAAuB;AAErD,yCAAyC,GACzC,OAAO,YAAI,MAAM,WAAsB"}
package/deno.json ADDED
@@ -0,0 +1 @@
1
+ {"publish":{"exclude":["**/*_test.ts"]},"name":"@yaks/hook","version":"0.2.36","license":"Apache-2.0","description":"An event another system delivered, kept as it arrived: the payload, the route it came in on, and whether it was signed.","exports":{".":"./mod.ts","./vocab":"./vocab.ts"},"imports":{"@yaks/hook":"./mod.ts","@yaks/hook/vocab":"./vocab.ts"}}
package/hooked.js ADDED
@@ -0,0 +1,94 @@
1
+ // A request another system delivered, as the bundles that record it.
2
+ //
3
+ // Pure: a request in, bundles out. Who received it, and whether its signature
4
+ // held, are the receiver's to say; this names the event and keeps the rest as
5
+ // it arrived.
6
+ //
7
+ // The event name is read in order of how much the sender said: its own header
8
+ // (`x-github-event`), then a JSON body's `event`, `type` or `action`, then the
9
+ // route itself. A source nobody wrote a parser for still lands whole.
10
+ //
11
+ // The entity id is derived from where the request came from and the id the
12
+ // receiver gave it, so recording the same request twice writes one entity — a
13
+ // receiver that crashed after writing and before acknowledging only repeats
14
+ // itself.
15
+ import { derivedEid } from "@yaks/graph/identity";
16
+ import { DOC } from "@yaks/doc";
17
+ import { link } from "@yaks/edge";
18
+ // A header off the captured set, by name, case-insensitively.
19
+ export let header = (r, name)=>{
20
+ try {
21
+ let all = JSON.parse(r.headers ?? '{}');
22
+ let k = Object.keys(all).find((k)=>k.toLowerCase() == name.toLowerCase());
23
+ return k ? all[k] : undefined;
24
+ } catch {
25
+ return undefined;
26
+ }
27
+ };
28
+ // The event word a JSON body names, if it names one.
29
+ let bodyEvent = (r)=>{
30
+ try {
31
+ let b = JSON.parse(r.body ?? '');
32
+ let k = [
33
+ 'event',
34
+ 'type',
35
+ 'action'
36
+ ].find((k)=>typeof b[k] == 'string' && b[k]);
37
+ return k ? String(b[k]) : undefined;
38
+ } catch {
39
+ return undefined;
40
+ }
41
+ };
42
+ /**
43
+ * The event a request reports, best first.
44
+ *
45
+ * ```ts
46
+ * import { event } from '@yaks/hook'
47
+ * event({ id: '1', source: 'gh', headers: '{"X-GitHub-Event":"push"}' }) // 'push'
48
+ * event({ id: '2', source: 'ph', body: '{"type":"issue"}' }) // 'issue'
49
+ * event({ id: '3', source: 'x', method: 'POST', path: '/hook/a' }) // 'POST /hook/a'
50
+ * ```
51
+ */ export let event = (r)=>header(r, 'x-github-event') ?? header(r, 'x-event-key') ?? bodyEvent(r) ?? `${r.method ?? 'POST'} ${r.path ?? '/'}`;
52
+ /** The id a request is recorded under: the same request, the same entity. */ export let hookEid = (r)=>derivedEid(`hook|${r.source}|${r.id}`);
53
+ /**
54
+ * A delivered request → the bundles that record it: a `hook` carrying it as it
55
+ * arrived, a `doc` title naming it, and an `about` link to the entity it is
56
+ * for when the receiver knows one. Every `hook` property is server-owned, so
57
+ * apply these trusted.
58
+ */ export let hooked = (r, about)=>{
59
+ let eid = hookEid(r);
60
+ let said = event(r);
61
+ return [
62
+ {
63
+ entity: {
64
+ eid
65
+ },
66
+ [DOC]: {
67
+ title: `${r.source}: ${said}`
68
+ },
69
+ hook: {
70
+ source: r.source,
71
+ event: said,
72
+ ...r.body == null ? {} : {
73
+ payload: r.body
74
+ },
75
+ ...r.method == null ? {} : {
76
+ method: r.method
77
+ },
78
+ ...r.path == null ? {} : {
79
+ path: r.path
80
+ },
81
+ ...r.headers == null ? {} : {
82
+ headers: r.headers
83
+ },
84
+ ...r.verified == null ? {} : {
85
+ verified: r.verified
86
+ }
87
+ }
88
+ },
89
+ ...about ? [
90
+ link(eid, 'about', about)
91
+ ] : []
92
+ ];
93
+ };
94
+ //# sourceMappingURL=hooked.js.map
package/hooked.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hooked.js","sources":["./hooked.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,EAAE;AACF,8EAA8E;AAC9E,8EAA8E;AAC9E,cAAc;AACd,EAAE;AACF,8EAA8E;AAC9E,+EAA+E;AAC/E,sEAAsE;AACtE,EAAE;AACF,2EAA2E;AAC3E,8EAA8E;AAC9E,4EAA4E;AAC5E,UAAU;AAGV,SAAS,UAAU,oCAAyC;AAC5D,SAAS,GAAG,yBAA8B;AAC1C,SAAS,IAAI,0BAA+B;AAoB5C,8DAA8D;AAC9D,OAAO,IAAI,SAAS,CAAC,GAAY;EAC/B,IAAI;IACF,IAAI,MAAM,KAAK,KAAK,CAAC,EAAE,OAAO,IAAI;IAClC,IAAI,IAAI,OAAO,IAAI,CAAC,KAAK,IAAI,CAAC,CAAC,IAAM,EAAE,WAAW,MAAM,KAAK,WAAW;IACxE,OAAO,IAAI,GAAG,CAAC,EAAE,GAAG;EACtB,EAAE,OAAM;IACN,OAAO;EACT;AACF,EAAC;AAED,qDAAqD;AACrD,IAAI,YAAY,CAAC;EACf,IAAI;IACF,IAAI,IAAI,KAAK,KAAK,CAAC,EAAE,IAAI,IAAI;IAC7B,IAAI,IAAI;MAAC;MAAS;MAAQ;KAAS,CAAC,IAAI,CAAC,CAAC,IACxC,OAAO,CAAC,CAAC,EAAE,IAAI,YAAY,CAAC,CAAC,EAAE;IAEjC,OAAO,IAAI,OAAO,CAAC,CAAC,EAAE,IAAI;EAC5B,EAAE,OAAM;IACN,OAAO;EACT;AACF;AAEA;;;;;;;;;CASC,GACD,OAAO,IAAI,QAAQ,CAAC,IAClB,OAAO,GAAG,qBAAqB,OAAO,GAAG,kBAAkB,UAAU,MACnE,GAAG,EAAE,MAAM,IAAI,OAAO,CAAC,EAAE,EAAE,IAAI,IAAI,KAAK,CAAA;AAE5C,2EAA2E,GAC3E,OAAO,IAAI,UAAU,CAAC,IAAoB,WAAW,CAAC,KAAK,EAAE,EAAE,MAAM,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE,EAAC;AAEhF;;;;;CAKC,GACD,OAAO,IAAI,SAAS,CAAC,GAAY;EAC/B,IAAI,MAAM,QAAQ;EAClB,IAAI,OAAO,MAAM;EACjB,OAAO;IACL;MACE,QAAQ;QAAE;MAAI;MACd,CAAC,IAAI,EAAE;QAAE,OAAO,GAAG,EAAE,MAAM,CAAC,EAAE,EAAE,MAAM;MAAC;MACvC,MAAM;QACJ,QAAQ,EAAE,MAAM;QAChB,OAAO;QACP,GAAI,EAAE,IAAI,IAAI,OAAO,CAAC,IAAI;UAAE,SAAS,EAAE,IAAI;QAAC,CAAC;QAC7C,GAAI,EAAE,MAAM,IAAI,OAAO,CAAC,IAAI;UAAE,QAAQ,EAAE,MAAM;QAAC,CAAC;QAChD,GAAI,EAAE,IAAI,IAAI,OAAO,CAAC,IAAI;UAAE,MAAM,EAAE,IAAI;QAAC,CAAC;QAC1C,GAAI,EAAE,OAAO,IAAI,OAAO,CAAC,IAAI;UAAE,SAAS,EAAE,OAAO;QAAC,CAAC;QACnD,GAAI,EAAE,QAAQ,IAAI,OAAO,CAAC,IAAI;UAAE,UAAU,EAAE,QAAQ;QAAC,CAAC;MACxD;IACF;OACI,QAAQ;MAAC,KAAK,KAAK,SAAS;KAAO,GAAG,EAAE;GAC7C;AACH,EAAC"}
package/hooked.ts ADDED
@@ -0,0 +1,105 @@
1
+ // A request another system delivered, as the bundles that record it.
2
+ //
3
+ // Pure: a request in, bundles out. Who received it, and whether its signature
4
+ // held, are the receiver's to say; this names the event and keeps the rest as
5
+ // it arrived.
6
+ //
7
+ // The event name is read in order of how much the sender said: its own header
8
+ // (`x-github-event`), then a JSON body's `event`, `type` or `action`, then the
9
+ // route itself. A source nobody wrote a parser for still lands whole.
10
+ //
11
+ // The entity id is derived from where the request came from and the id the
12
+ // receiver gave it, so recording the same request twice writes one entity — a
13
+ // receiver that crashed after writing and before acknowledging only repeats
14
+ // itself.
15
+
16
+ import type { Bundle, Eid } from 'jsr:@yaks/graph@0.2.36'
17
+ import { derivedEid } from '@yaks/graph/identity'
18
+ import { DOC } from '@yaks/doc'
19
+ import { link } from '@yaks/edge'
20
+
21
+ /** A delivered request, as its receiver captured it. */
22
+ export type Request = {
23
+ /** the id the receiver gave it — with `source`, what makes it one entity */
24
+ id: string
25
+ /** who sent it */
26
+ source: string
27
+ /** the HTTP method it arrived by */
28
+ method?: string
29
+ /** the path it arrived on */
30
+ path?: string
31
+ /** the headers as sent: a JSON object, as text */
32
+ headers?: string
33
+ /** the body exactly as delivered */
34
+ body?: string
35
+ /** whether the sender's signature verified, where the receiver checked */
36
+ verified?: boolean
37
+ }
38
+
39
+ // A header off the captured set, by name, case-insensitively.
40
+ export let header = (r: Request, name: string): string | undefined => {
41
+ try {
42
+ let all = JSON.parse(r.headers ?? '{}') as Record<string, string>
43
+ let k = Object.keys(all).find((k) => k.toLowerCase() == name.toLowerCase())
44
+ return k ? all[k] : undefined
45
+ } catch {
46
+ return undefined
47
+ }
48
+ }
49
+
50
+ // The event word a JSON body names, if it names one.
51
+ let bodyEvent = (r: Request): string | undefined => {
52
+ try {
53
+ let b = JSON.parse(r.body ?? '') as Record<string, unknown>
54
+ let k = ['event', 'type', 'action'].find((k) =>
55
+ typeof b[k] == 'string' && b[k]
56
+ )
57
+ return k ? String(b[k]) : undefined
58
+ } catch {
59
+ return undefined
60
+ }
61
+ }
62
+
63
+ /**
64
+ * The event a request reports, best first.
65
+ *
66
+ * ```ts
67
+ * import { event } from '@yaks/hook'
68
+ * event({ id: '1', source: 'gh', headers: '{"X-GitHub-Event":"push"}' }) // 'push'
69
+ * event({ id: '2', source: 'ph', body: '{"type":"issue"}' }) // 'issue'
70
+ * event({ id: '3', source: 'x', method: 'POST', path: '/hook/a' }) // 'POST /hook/a'
71
+ * ```
72
+ */
73
+ export let event = (r: Request): string =>
74
+ header(r, 'x-github-event') ?? header(r, 'x-event-key') ?? bodyEvent(r) ??
75
+ `${r.method ?? 'POST'} ${r.path ?? '/'}`
76
+
77
+ /** The id a request is recorded under: the same request, the same entity. */
78
+ export let hookEid = (r: Request): Eid => derivedEid(`hook|${r.source}|${r.id}`)
79
+
80
+ /**
81
+ * A delivered request → the bundles that record it: a `hook` carrying it as it
82
+ * arrived, a `doc` title naming it, and an `about` link to the entity it is
83
+ * for when the receiver knows one. Every `hook` property is server-owned, so
84
+ * apply these trusted.
85
+ */
86
+ export let hooked = (r: Request, about?: Eid): Bundle[] => {
87
+ let eid = hookEid(r)
88
+ let said = event(r)
89
+ return [
90
+ {
91
+ entity: { eid },
92
+ [DOC]: { title: `${r.source}: ${said}` },
93
+ hook: {
94
+ source: r.source,
95
+ event: said,
96
+ ...(r.body == null ? {} : { payload: r.body }),
97
+ ...(r.method == null ? {} : { method: r.method }),
98
+ ...(r.path == null ? {} : { path: r.path }),
99
+ ...(r.headers == null ? {} : { headers: r.headers }),
100
+ ...(r.verified == null ? {} : { verified: r.verified }),
101
+ },
102
+ },
103
+ ...(about ? [link(eid, 'about', about)] : []),
104
+ ]
105
+ }