brain-memory 0.4.0 → 0.5.0

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.
@@ -8,6 +8,8 @@
8
8
 
9
9
  const crypto = require('crypto');
10
10
  const fs = require('fs');
11
+ const http = require('http');
12
+ const https = require('https');
11
13
  const os = require('os');
12
14
  const path = require('path');
13
15
  const zlib = require('zlib');
@@ -65,20 +67,77 @@ function sha256(buf) {
65
67
 
66
68
  // -- a thin client -----------------------------------------------------------
67
69
 
70
+ /**
71
+ * One HTTP request on a socket of its own, closed when the response ends.
72
+ *
73
+ * Deliberately not `fetch`: fetch keeps connections alive in a pool, and on
74
+ * Node 18 a pooled socket to a server that has just been shut down could keep a
75
+ * finished test process from exiting, which hung CI. With `agent: false` and
76
+ * `Connection: close` nothing outlives the request, on any Node version.
77
+ *
78
+ * @returns {Promise<{status: number, headers: {get: (name: string) => string|null}, body: Buffer}>}
79
+ */
80
+ function rawRequest(method, url, { headers, body } = {}) {
81
+ return new Promise((resolve, reject) => {
82
+ const target = new URL(url);
83
+ const mod = target.protocol === 'https:' ? https : http;
84
+ const req = mod.request(target, {
85
+ method,
86
+ agent: false,
87
+ headers: {
88
+ Connection: 'close',
89
+ ...(headers || {}),
90
+ ...(body != null ? { 'Content-Length': Buffer.byteLength(body) } : {}),
91
+ },
92
+ }, (res) => {
93
+ const chunks = [];
94
+ res.on('data', (chunk) => chunks.push(chunk));
95
+ res.on('error', reject);
96
+ res.on('end', () => resolve({
97
+ status: res.statusCode,
98
+ headers: {
99
+ get(name) {
100
+ const value = res.headers[String(name).toLowerCase()];
101
+ if (value == null) return null;
102
+ return Array.isArray(value) ? value.join(', ') : value;
103
+ },
104
+ },
105
+ body: Buffer.concat(chunks),
106
+ }));
107
+ });
108
+ req.on('error', reject);
109
+ req.setTimeout(30000, () => req.destroy(new Error(`${method} ${url} timed out`)));
110
+ req.end(body != null ? body : undefined);
111
+ });
112
+ }
113
+
114
+ function parseBody(buf) {
115
+ const text = buf.toString('utf8');
116
+ try { return text ? JSON.parse(text) : null; } catch { return text; }
117
+ }
118
+
119
+ /** The subset of `fetch` the OIDC verifier uses, without a connection pool. */
120
+ async function plainFetch(url, opts = {}) {
121
+ const res = await rawRequest(opts.method || 'GET', String(url), { headers: opts.headers, body: opts.body });
122
+ return {
123
+ ok: res.status >= 200 && res.status < 300,
124
+ status: res.status,
125
+ headers: res.headers,
126
+ json: async () => JSON.parse(res.body.toString('utf8')),
127
+ text: async () => res.body.toString('utf8'),
128
+ };
129
+ }
130
+
68
131
  function client(baseUrl, token) {
69
132
  const base = baseUrl.replace(/\/$/, '');
70
133
  const auth = token ? { Authorization: `Bearer ${token}` } : {};
71
134
 
72
135
  async function json(method, pathname, body, headers) {
73
- const res = await fetch(base + pathname, {
74
- method,
136
+ const res = await rawRequest(method, base + pathname, {
75
137
  headers: { ...auth, ...(body ? { 'Content-Type': 'application/json' } : {}), ...(headers || {}) },
76
138
  body: body ? JSON.stringify(body) : undefined,
77
139
  });
78
- const text = await res.text();
79
- let data = null;
80
- try { data = text ? JSON.parse(text) : null; } catch { data = text; }
81
- return { status: res.status, headers: res.headers, data };
140
+ return { status: res.status, headers: res.headers, data: parseBody(res.body) };
82
141
  }
83
142
 
84
143
  async function upload(brainId, archive, { headers, query, field } = {}) {
@@ -89,23 +148,18 @@ function client(baseUrl, token) {
89
148
  'Content-Type: application/gzip\r\n\r\n'
90
149
  );
91
150
  const tail = Buffer.from(`\r\n--${boundary}--\r\n`);
92
- const res = await fetch(`${base}/api/brains/${brainId}/sync${query || ''}`, {
93
- method: 'PUT',
151
+ const res = await rawRequest('PUT', `${base}/api/brains/${brainId}/sync${query || ''}`, {
94
152
  headers: { ...auth, 'Content-Type': `multipart/form-data; boundary=${boundary}`, ...(headers || {}) },
95
153
  body: Buffer.concat([head, archive, tail]),
96
154
  });
97
- const text = await res.text();
98
- let data = null;
99
- try { data = text ? JSON.parse(text) : null; } catch { data = text; }
100
- return { status: res.status, headers: res.headers, data };
155
+ return { status: res.status, headers: res.headers, data: parseBody(res.body) };
101
156
  }
102
157
 
103
158
  async function download(brainId, { headers, query } = {}) {
104
- const res = await fetch(`${base}/api/brains/${brainId}/sync${query || ''}`, {
159
+ const res = await rawRequest('GET', `${base}/api/brains/${brainId}/sync${query || ''}`, {
105
160
  headers: { ...auth, ...(headers || {}) },
106
161
  });
107
- const body = Buffer.from(await res.arrayBuffer());
108
- return { status: res.status, headers: res.headers, body };
162
+ return { status: res.status, headers: res.headers, body: res.body };
109
163
  }
110
164
 
111
165
  return { json, upload, download };
@@ -142,4 +196,4 @@ async function startReferenceStore(options = {}) {
142
196
  };
143
197
  }
144
198
 
145
- module.exports = { makeArchive, sampleArchive, sha256, client, startReferenceStore };
199
+ module.exports = { makeArchive, sampleArchive, sha256, client, plainFetch, startReferenceStore };
@@ -11,14 +11,19 @@ const crypto = require('crypto');
11
11
 
12
12
  const { createVerifier, looksLikeJwt, OidcError } = require('../lib/oidc');
13
13
  const { startMockIssuer, makeKey, sign } = require('./mock-issuer');
14
- const { client, startReferenceStore, sampleArchive } = require('../conformance/helpers');
14
+ const { client, plainFetch, startReferenceStore, sampleArchive } = require('../conformance/helpers');
15
+
16
+ // Every verifier here talks to the mock issuer through plainFetch rather than the
17
+ // global fetch, so no pooled connection outlives the test process (see
18
+ // conformance/helpers.js). The default-fetch path is covered by the connector's
19
+ // OIDC test, which runs on a newer Node.
15
20
 
16
21
  describe('ID-token verification', () => {
17
22
  let idp;
18
23
  let verifier;
19
24
  before(async () => {
20
25
  idp = await startMockIssuer();
21
- verifier = createVerifier({ issuer: idp.issuer, audience: idp.clientId });
26
+ verifier = createVerifier({ issuer: idp.issuer, audience: idp.clientId, fetch: plainFetch });
22
27
  });
23
28
  after(() => idp.close());
24
29
 
@@ -113,7 +118,7 @@ describe('issuer safeguards', () => {
113
118
  const idp = await startMockIssuer();
114
119
  try {
115
120
  const fetchLying = async (url) => {
116
- const res = await fetch(url);
121
+ const res = await plainFetch(url);
117
122
  const body = await res.json();
118
123
  if (body.issuer) body.issuer = 'https://somewhere-else.example';
119
124
  return { ok: true, status: 200, json: async () => body };
@@ -132,22 +137,22 @@ describe('allow-lists', () => {
132
137
  after(() => idp.close());
133
138
 
134
139
  it('admits a verified address in an allowed domain, and nobody else', async () => {
135
- const v = createVerifier({ issuer: idp.issuer, audience: idp.clientId, allowedDomains: 'example.org, other.test' });
140
+ const v = createVerifier({ issuer: idp.issuer, audience: idp.clientId, allowedDomains: 'example.org, other.test', fetch: plainFetch });
136
141
  await v.verify(idp.idToken());
137
142
  await assert.rejects(v.verify(idp.idToken({ email: 'mallory@evil.test' })), /allow-list/);
138
143
  await assert.rejects(v.verify(idp.idToken({ email: 'x@notexample.org' })), /allow-list/);
139
144
  });
140
145
 
141
146
  it('ignores an address the issuer has not verified', async () => {
142
- const v = createVerifier({ issuer: idp.issuer, audience: idp.clientId, allowedDomains: 'example.org' });
147
+ const v = createVerifier({ issuer: idp.issuer, audience: idp.clientId, allowedDomains: 'example.org', fetch: plainFetch });
143
148
  await assert.rejects(v.verify(idp.idToken({ email_verified: false })), /allow-list/);
144
149
  await assert.rejects(v.verify(idp.idToken({ email_verified: undefined })), /allow-list/);
145
150
  });
146
151
 
147
152
  it('admits by Google hosted-domain claim and by exact address', async () => {
148
- const byHd = createVerifier({ issuer: idp.issuer, audience: idp.clientId, allowedDomains: 'corp.test' });
153
+ const byHd = createVerifier({ issuer: idp.issuer, audience: idp.clientId, allowedDomains: 'corp.test', fetch: plainFetch });
149
154
  await byHd.verify(idp.idToken({ hd: 'corp.test', email: 'someone@gmail.test' }));
150
- const byEmail = createVerifier({ issuer: idp.issuer, audience: idp.clientId, allowedEmails: ['Alice@Example.org'] });
155
+ const byEmail = createVerifier({ issuer: idp.issuer, audience: idp.clientId, allowedEmails: ['Alice@Example.org'], fetch: plainFetch });
151
156
  await byEmail.verify(idp.idToken());
152
157
  await assert.rejects(byEmail.verify(idp.idToken({ email: 'bob@example.org' })), /allow-list/);
153
158
  });
@@ -158,7 +163,7 @@ describe('the store accepts ID tokens', () => {
158
163
  let ref;
159
164
  before(async () => {
160
165
  idp = await startMockIssuer();
161
- ref = await startReferenceStore({ oidc: { issuer: idp.issuer, audience: idp.clientId } });
166
+ ref = await startReferenceStore({ oidc: { issuer: idp.issuer, audience: idp.clientId, fetch: plainFetch } });
162
167
  });
163
168
  after(async () => { await ref.close(); await idp.close(); });
164
169