@kirkelliott/zap 0.1.28 → 0.1.29

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/demo/index.zap CHANGED
@@ -47,7 +47,7 @@ export default async (req) => {
47
47
  <body>
48
48
  <div class="wrap">
49
49
  <h1>zap</h1>
50
- <p class="sub">Drop a .zap file in S3. It becomes an endpoint. &mdash; <a href="https://github.com/dmvjs/s3node">github</a> &mdash; <a href="https://npmjs.com/package/@kirkelliott/zap">npm</a></p>
50
+ <p class="sub">Drop a .zap file in S3. It becomes an endpoint. &mdash; <a href="https://github.com/dmvjs/zap">github</a> &mdash; <a href="https://npmjs.com/package/@kirkelliott/zap">npm</a></p>
51
51
 
52
52
  <div class="explainer">
53
53
  <p>A <strong>.zap file</strong> is a JavaScript file you drop in an S3 bucket. The filename becomes the URL path. The function inside handles the request.</p>
package/demo/lib/page.zap CHANGED
@@ -24,7 +24,7 @@ export default (title, content) => ({
24
24
  <body>
25
25
  <h1>${title}</h1>
26
26
  <div class="card">${content}</div>
27
- <p class="dim">powered by <a href="https://github.com/dmvjs/s3node">zap</a></p>
27
+ <p class="dim">powered by <a href="https://github.com/dmvjs/zap">zap</a></p>
28
28
  </body>
29
29
  </html>`,
30
30
  })
package/dist/cli.js CHANGED
File without changes
package/dist/handler.js CHANGED
@@ -42,12 +42,27 @@ const handler = async (event, context) => {
42
42
  headers: e.headers,
43
43
  body: e.body ?? null,
44
44
  };
45
- const key = req.path === '/' ? 'index' : req.path.replace(/^\//, '');
45
+ const key = req.path === '/' ? 'index' : req.path.replace(/^\//, '').replace(/\/$/, '');
46
+ const resolve = async () => {
47
+ const segments = key.split('/');
48
+ for (let i = segments.length; i >= 1; i--) {
49
+ const candidate = segments.slice(0, i).join('/');
50
+ try {
51
+ const source = await loader(candidate);
52
+ return { source, handler: candidate };
53
+ }
54
+ catch (err) {
55
+ if (err.name !== 'NoSuchKey')
56
+ throw err;
57
+ }
58
+ }
59
+ throw Object.assign(new Error(`No handler for ${req.path}`), { name: 'NoSuchKey' });
60
+ };
46
61
  try {
47
- const source = await loader(key);
62
+ const { source, handler } = await resolve();
48
63
  const res = await (0, eval_1.run)(source, req, loader);
49
64
  const status = res.status ?? 200;
50
- console.log(JSON.stringify({ handler: key, method: req.method, status, ms: Date.now() - start, requestId: context.awsRequestId }));
65
+ console.log(JSON.stringify({ handler, method: req.method, status, ms: Date.now() - start, requestId: context.awsRequestId }));
51
66
  return { statusCode: status, headers: res.headers, body: (0, types_1.serialize)(res.body) };
52
67
  }
53
68
  catch (err) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kirkelliott/zap",
3
- "version": "0.1.28",
3
+ "version": "0.1.29",
4
4
  "description": "Drop a .zap file in S3. It becomes an API endpoint.",
5
5
  "main": "dist/handler.js",
6
6
  "bin": {
@@ -20,9 +20,9 @@
20
20
  ],
21
21
  "repository": {
22
22
  "type": "git",
23
- "url": "git+https://github.com/dmvjs/s3node.git"
23
+ "url": "git+https://github.com/dmvjs/zap.git"
24
24
  },
25
- "homepage": "https://github.com/dmvjs/s3node",
25
+ "homepage": "https://github.com/dmvjs/zap",
26
26
  "license": "MIT",
27
27
  "engines": {
28
28
  "node": ">=20"