@funcd-dev/shim 0.4.1 → 0.4.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.
Files changed (3) hide show
  1. package/dist/blob.js +14 -2
  2. package/dist/kv.js +15 -3
  3. package/package.json +1 -1
package/dist/blob.js CHANGED
@@ -16,10 +16,12 @@ function request(method, path, body) {
16
16
  headers['content-length'] = body.byteLength;
17
17
  const req = http.request({ socketPath, path, method, headers }, (res) => {
18
18
  const chunks = [];
19
+ // A connection that drops mid-body errors the response, not the request, and never ends it.
20
+ res.on('error', (err) => reject(new Error(`context.blob ${method} ${path} failed: connection closed before the reply ended`, { cause: err })));
19
21
  res.on('data', (c) => chunks.push(c));
20
22
  res.on('end', () => resolve({ status: res.statusCode ?? 0, body: Buffer.concat(chunks) }));
21
23
  });
22
- req.on('error', reject);
24
+ req.on('error', (err) => reject(new Error(`context.blob ${method} ${path} failed: ${err.message}`, { cause: err })));
23
25
  if (body)
24
26
  req.end(body);
25
27
  else
@@ -31,6 +33,16 @@ const enc = encodeURIComponent;
31
33
  const keyPath = (binding, key) => `/blob/${enc(binding)}/${key.split('/').map(enc).join('/')}`;
32
34
  const fail = (verb, r) => new Error(`context.blob.${verb} failed: ${r.status} ${r.body.toString('utf8')}`);
33
35
  const ok = (r) => r.status >= 200 && r.status < 300;
36
+ const json = (verb, r, text) => {
37
+ try {
38
+ return JSON.parse(text);
39
+ }
40
+ catch (err) {
41
+ throw new Error(`context.blob.${verb} failed: ${r.status} reply is not JSON: ${err.message}`, {
42
+ cause: err,
43
+ });
44
+ }
45
+ };
34
46
  /** Build the context.blob client. */
35
47
  export function makeBlob() {
36
48
  return {
@@ -57,7 +69,7 @@ export function makeBlob() {
57
69
  const r = await request('GET', `/blob/${enc(binding)}${q}`);
58
70
  if (!ok(r))
59
71
  throw fail('list', r);
60
- return JSON.parse(r.body.toString('utf8') || '[]');
72
+ return json('list', r, r.body.toString('utf8') || '[]');
61
73
  },
62
74
  async signedUrl(binding, key, opts) {
63
75
  let path = `${keyPath(binding, key)}?sign=1&method=${enc(opts?.method ?? 'GET')}`;
package/dist/kv.js CHANGED
@@ -14,10 +14,12 @@ function request(method, path, body) {
14
14
  headers['content-length'] = body.byteLength;
15
15
  const req = http.request({ socketPath, path, method, headers }, (res) => {
16
16
  const chunks = [];
17
+ // A connection that drops mid-body errors the response, not the request, and never ends it.
18
+ res.on('error', (err) => reject(new Error(`context.kv ${method} ${path} failed: connection closed before the reply ended`, { cause: err })));
17
19
  res.on('data', (c) => chunks.push(c));
18
20
  res.on('end', () => resolve({ status: res.statusCode ?? 0, body: Buffer.concat(chunks) }));
19
21
  });
20
- req.on('error', reject);
22
+ req.on('error', (err) => reject(new Error(`context.kv ${method} ${path} failed: ${err.message}`, { cause: err })));
21
23
  if (body)
22
24
  req.end(body);
23
25
  else
@@ -29,6 +31,16 @@ const enc = encodeURIComponent;
29
31
  const keyPath = (binding, key) => `/kv/${enc(binding)}/${key.split('/').map(enc).join('/')}`;
30
32
  const fail = (verb, r) => new Error(`context.kv.${verb} failed: ${r.status} ${r.body.toString('utf8')}`);
31
33
  const ok = (r) => r.status >= 200 && r.status < 300;
34
+ const json = (verb, r, text) => {
35
+ try {
36
+ return JSON.parse(text);
37
+ }
38
+ catch (err) {
39
+ throw new Error(`context.kv.${verb} failed: ${r.status} reply is not JSON: ${err.message}`, {
40
+ cause: err,
41
+ });
42
+ }
43
+ };
32
44
  /** Build the context.kv client. */
33
45
  export function makeKV() {
34
46
  return {
@@ -54,7 +66,7 @@ export function makeKV() {
54
66
  return null;
55
67
  if (!ok(r))
56
68
  throw fail('get', r);
57
- return JSON.parse(r.body.toString('utf8'));
69
+ return json('getJSON', r, r.body.toString('utf8'));
58
70
  },
59
71
  async put(binding, key, value) {
60
72
  const buf = typeof value === 'string' ? Buffer.from(value, 'utf8') : Buffer.from(value);
@@ -72,7 +84,7 @@ export function makeKV() {
72
84
  const r = await request('GET', `/kv/${enc(binding)}${q}`);
73
85
  if (!ok(r))
74
86
  throw fail('list', r);
75
- return JSON.parse(r.body.toString('utf8') || '[]');
87
+ return json('list', r, r.body.toString('utf8') || '[]');
76
88
  },
77
89
  };
78
90
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funcd-dev/shim",
3
- "version": "0.4.1",
3
+ "version": "0.4.3",
4
4
  "type": "module",
5
5
  "description": "Types and the contract build for writing funcd functions in TypeScript",
6
6
  "license": "Apache-2.0",