javascript-solid-server 0.0.218 → 0.0.219

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.
@@ -417,9 +417,12 @@ Each entry:
417
417
  the app owns authentication below it (see the section above). Must start
418
418
  with `/`; invalid prefixes fail startup.
419
419
  - `config` — passed to the plugin verbatim as `api.config`.
420
- - `id` — optional stable identifier (defaults to a name derived from
421
- `module`); names the plugin's private data dir, so set it explicitly if
422
- you load two plugins whose specifiers reduce to the same name.
420
+ - `id` — optional stable identifier that names the plugin's private data
421
+ dir. Defaults to a name derived from `module`: the file's basename, or —
422
+ for a generic basename like `plugin.js`/`index.js` — its parent directory
423
+ (`relay/plugin.js` → `relay`), so the conventional `<name>/plugin.js`
424
+ layout yields distinct ids with none set. Set it explicitly only if two
425
+ specifiers still reduce to the same name.
423
426
 
424
427
  `activate(api)` receives: `api.fastify` (register routes here),
425
428
  `api.prefix`, `api.config`, `api.log`, `api.auth.getAgent(request)`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "javascript-solid-server",
3
- "version": "0.0.218",
3
+ "version": "0.0.219",
4
4
  "description": "A minimal, fast Solid server",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
package/src/plugins.js CHANGED
@@ -123,11 +123,37 @@ export function normalizePrefix(p) {
123
123
  return trimmed.startsWith('/') && trimmed.length > 1 ? trimmed : '';
124
124
  }
125
125
 
126
+ // Basenames too generic to name a plugin: the near-universal
127
+ // '<name>/plugin.js' convention means every such file would derive the same
128
+ // id and collide. For these, the id falls back to the parent directory (#596).
129
+ const GENERIC_BASENAMES = new Set(['plugin', 'index']);
130
+
131
+ /**
132
+ * The distinguishing name of a plugin FILE: its basename, unless that is
133
+ * generic ('plugin.js', 'index.js'), in which case the immediate parent
134
+ * directory ('relay/plugin.js' -> 'relay'). The parent directory
135
+ * distinguishes '<name>/plugin.js' files from each other WITHOUT pinning the
136
+ * machine-specific path prefix (which would move the id — and pluginDir —
137
+ * with the deployment). Falls back to the basename when there is no usable
138
+ * parent (e.g. './plugin.js' at the cwd root). (#596)
139
+ */
140
+ export function fileStem(module) {
141
+ const withoutExt = path.basename(module).replace(/\.[cm]?js$/, '');
142
+ if (GENERIC_BASENAMES.has(withoutExt.toLowerCase())) {
143
+ const parent = path.basename(path.dirname(module));
144
+ // The parent directory name is returned AS-IS — a directory legitimately
145
+ // named 'foo.js' must stay 'foo.js', not be extension-stripped to 'foo'.
146
+ if (parent && parent !== '.' && parent !== '..') return parent;
147
+ }
148
+ return withoutExt;
149
+ }
150
+
126
151
  /**
127
152
  * Directory-safe plugin id: entry.id, or derived from the module spec.
128
153
  * Bare package specifiers keep their full path ('@scope/pkg/plugin.js' ->
129
154
  * 'scope-pkg-plugin') so same-named files in different packages don't
130
- * collide; file paths use the basename, because a machine-specific
155
+ * collide; file paths use the basename — or, for a generic basename, the
156
+ * parent directory (see fileStem, #596) — because a machine-specific
131
157
  * directory prefix must not name the plugin's data dir (the id — and with
132
158
  * it pluginDir — would change whenever the deployment moves). The loader
133
159
  * additionally rejects duplicate ids, so any residual collision fails the
@@ -135,12 +161,17 @@ export function normalizePrefix(p) {
135
161
  */
136
162
  export function pluginId(spec) {
137
163
  const module = String(spec.module);
138
- const raw = typeof spec.id === 'string' && spec.id
139
- ? spec.id
140
- : (module.startsWith('.') || path.isAbsolute(module)
141
- ? path.basename(module)
142
- : module
143
- ).replace(/\.[cm]?js$/, '');
164
+ // The extension is stripped where it applies — the file basename (via
165
+ // fileStem) and a bare specifier's tail — but NOT a parent directory name
166
+ // that fileStem may return for a generic basename.
167
+ let raw;
168
+ if (typeof spec.id === 'string' && spec.id) {
169
+ raw = spec.id.replace(/\.[cm]?js$/, '');
170
+ } else if (module.startsWith('.') || path.isAbsolute(module)) {
171
+ raw = fileStem(module);
172
+ } else {
173
+ raw = module.replace(/\.[cm]?js$/, '');
174
+ }
144
175
  const id = raw.toLowerCase().replace(/[^a-z0-9_-]+/g, '-').replace(/^-+|-+$/g, '');
145
176
  if (!id) throw new Error(`plugins: cannot derive an id from ${JSON.stringify(spec.module)}; set entry.id`);
146
177
  return id;
@@ -229,6 +229,53 @@ describe('plugin loader (#206)', () => {
229
229
  );
230
230
  });
231
231
 
232
+ it('a generic basename (plugin.js/index.js) derives the parent dir, not "plugin" (#596)', async () => {
233
+ // The near-universal '<name>/plugin.js' convention: without this fallback
234
+ // every such file collides on the id 'plugin' (and 'index' for index.js).
235
+ assert.strictEqual(pluginId({ module: './relay/plugin.js' }), 'relay');
236
+ assert.strictEqual(pluginId({ module: '/abs/path/dashboard/plugin.js' }), 'dashboard');
237
+ assert.strictEqual(pluginId({ module: './chat/index.js' }), 'chat');
238
+ assert.strictEqual(pluginId({ module: './My-App/Plugin.mjs' }), 'my-app');
239
+ // A non-generic basename is unchanged (still the basename).
240
+ assert.strictEqual(pluginId({ module: './relay/relay.js' }), 'relay');
241
+ assert.strictEqual(pluginId({ module: '/some/machine/path/foo.js' }), 'foo');
242
+ // No usable parent → falls back to the basename; an explicit id still wins.
243
+ assert.strictEqual(pluginId({ module: './plugin.js' }), 'plugin');
244
+ assert.strictEqual(pluginId({ module: './relay/plugin.js', id: 'custom' }), 'custom');
245
+ // A parent directory whose own name ends in .js keeps it (only the FILE's
246
+ // extension is stripped, never the directory name) — so 'foo.js/' and
247
+ // 'foo/' don't both collapse to the same 'foo'.
248
+ assert.strictEqual(pluginId({ module: './foo.js/plugin.js' }), 'foo-js');
249
+ assert.strictEqual(pluginId({ module: './foo/plugin.js' }), 'foo');
250
+ });
251
+
252
+ it('two <name>/plugin.js files load together with no explicit ids (#596)', async () => {
253
+ // The exact case that failed before: the CLI '--plugin' form (which can't
254
+ // set an id) loading two conventionally-named plugins.
255
+ await fs.emptyDir(`${FIXTURE_DIR}/alpha`);
256
+ await fs.emptyDir(`${FIXTURE_DIR}/beta`);
257
+ await fs.writeFile(`${FIXTURE_DIR}/alpha/plugin.js`,
258
+ `export async function activate(api) { api.fastify.get('/alpha/ping', async () => ({ id: 'alpha' })); }`);
259
+ await fs.writeFile(`${FIXTURE_DIR}/beta/plugin.js`,
260
+ `export async function activate(api) { api.fastify.get('/beta/ping', async () => ({ id: 'beta' })); }`);
261
+
262
+ await fs.emptyDir(TEST_DATA_DIR);
263
+ server = createServer({
264
+ logger: false,
265
+ forceCloseConnections: true,
266
+ root: TEST_DATA_DIR,
267
+ plugins: [
268
+ { module: `${FIXTURE_DIR}/alpha/plugin.js`, prefix: '/alpha' },
269
+ { module: `${FIXTURE_DIR}/beta/plugin.js`, prefix: '/beta' },
270
+ ],
271
+ });
272
+ // Boots (ids 'alpha' and 'beta', no collision) and both routes answer.
273
+ await server.listen({ port: 0, host: '127.0.0.1' });
274
+ const url = `http://127.0.0.1:${server.server.address().port}`;
275
+ assert.strictEqual((await (await fetch(`${url}/alpha/ping`)).json()).id, 'alpha');
276
+ assert.strictEqual((await (await fetch(`${url}/beta/ping`)).json()).id, 'beta');
277
+ });
278
+
232
279
  it('an invalid prefix fails listen() loudly', async () => {
233
280
  // Any provided prefix must validate — including falsy ones, which would
234
281
  // otherwise mount the app without its WAC exemption.