-
-
Notifications
You must be signed in to change notification settings - Fork 98
Fix npm entrypoints for @fedify/astro #701
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dahlia
wants to merge
6
commits into
fedify-dev:2.1-maintenance
Choose a base branch
from
dahlia:bugfix/astro
base: 2.1-maintenance
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+199
−2
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9623273
Fix typo
2chanhaeng 6b48053
Fix Astro npm entrypoints
dahlia cb59fb3
Tighten Astro package test typing
dahlia 7d91670
Satisfy lint in Astro package test
dahlia e43ca81
Cover Astro package entrypoints
dahlia 82b694f
Validate Astro package targets
dahlia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| import { deepStrictEqual, strictEqual } from "node:assert/strict"; | ||
| import { access, readFile } from "node:fs/promises"; | ||
| import { createRequire } from "node:module"; | ||
| import { dirname, resolve } from "node:path"; | ||
| import test from "node:test"; | ||
| import { fileURLToPath } from "node:url"; | ||
| import type { | ||
| Federation, | ||
| FederationFetchOptions, | ||
| } from "@fedify/fedify/federation"; | ||
| import type { APIContext } from "astro"; | ||
|
|
||
| const require = createRequire(import.meta.url); | ||
| const packageDir = resolve(dirname(fileURLToPath(import.meta.url)), ".."); | ||
|
|
||
| type MockFederation<TContextData> = Pick<Federation<TContextData>, "fetch">; | ||
|
|
||
| function toFederation<TContextData>( | ||
| federation: MockFederation<TContextData>, | ||
| ): Federation<TContextData> { | ||
| return federation as Federation<TContextData>; | ||
| } | ||
|
|
||
| function toApiContext(context: Pick<APIContext, "request">): APIContext { | ||
| return context as APIContext; | ||
| } | ||
|
|
||
| function expectResponse(response: void | Response): Response { | ||
| if (!(response instanceof Response)) { | ||
| throw new TypeError("Expected middleware to return a Response"); | ||
| } | ||
| return response; | ||
| } | ||
|
|
||
| function expectTarget(actual: unknown, expected: string, key: string): string { | ||
| strictEqual(actual, expected, `Expected ${key} to be ${expected}`); | ||
| return actual; | ||
| } | ||
|
|
||
| async function assertTargetExists(path: string): Promise<void> { | ||
| await access(resolve(packageDir, path)); | ||
| } | ||
|
|
||
| test("self-reference ESM import exposes working Astro integration API", async () => { | ||
| const mod = await import("@fedify/astro"); | ||
|
|
||
| strictEqual(typeof mod.fedifyIntegration, "function"); | ||
| strictEqual(typeof mod.fedifyMiddleware, "function"); | ||
|
|
||
|
dahlia marked this conversation as resolved.
|
||
| const integration = mod.fedifyIntegration(); | ||
| strictEqual(integration.name, "@fedify/astro"); | ||
|
|
||
| let capturedConfig: unknown; | ||
| ( | ||
| integration.hooks as Record< | ||
| "astro:config:setup", | ||
| (args: { updateConfig(config: unknown): void }) => void | ||
| > | ||
| )["astro:config:setup"]({ | ||
| updateConfig(config) { | ||
| capturedConfig = config; | ||
| }, | ||
| }); | ||
| deepStrictEqual(capturedConfig, { | ||
| vite: { | ||
| ssr: { | ||
| noExternal: ["@fedify/fedify", "@fedify/vocab"], | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| let capturedRequest: Request | undefined; | ||
| let capturedContextData: unknown; | ||
| const middleware = mod.fedifyMiddleware( | ||
| toFederation<string>({ | ||
| async fetch( | ||
| request: Request, | ||
| options: FederationFetchOptions<string>, | ||
| ) { | ||
| capturedRequest = request; | ||
| capturedContextData = options.contextData; | ||
| if (options.onNotAcceptable == null) { | ||
| throw new TypeError("Expected onNotAcceptable to be defined"); | ||
| } | ||
| return await options.onNotAcceptable(request); | ||
| }, | ||
| }), | ||
| () => "test-context", | ||
| ); | ||
|
|
||
| const request = new Request("https://example.com/"); | ||
| const response = expectResponse( | ||
| await middleware( | ||
| toApiContext({ request }), | ||
| () => Promise.resolve(new Response("Not found", { status: 404 })), | ||
| ), | ||
| ); | ||
| strictEqual(capturedRequest, request); | ||
| strictEqual(capturedContextData, "test-context"); | ||
| strictEqual(response.status, 406); | ||
| strictEqual(response.headers.get("Vary"), "Accept"); | ||
| }); | ||
|
|
||
| test( | ||
| "self-reference CommonJS require exposes working Astro middleware API", | ||
| { skip: "Deno" in globalThis }, | ||
| async () => { | ||
| const packageJson = JSON.parse( | ||
| await readFile(resolve(packageDir, "package.json"), "utf8"), | ||
| ); | ||
| const exportMap = packageJson.exports["."]; | ||
| const targets = [ | ||
| expectTarget(packageJson.main, "./dist/mod.cjs", "package.json main"), | ||
| expectTarget(packageJson.module, "./dist/mod.js", "package.json module"), | ||
| expectTarget(packageJson.types, "./dist/mod.d.ts", "package.json types"), | ||
| expectTarget( | ||
| exportMap.require.types, | ||
| "./dist/mod.d.cts", | ||
| 'package.json exports["."].require.types', | ||
| ), | ||
| expectTarget( | ||
| exportMap.require.default, | ||
| "./dist/mod.cjs", | ||
| 'package.json exports["."].require.default', | ||
| ), | ||
| expectTarget( | ||
| exportMap.import.types, | ||
| "./dist/mod.d.ts", | ||
| 'package.json exports["."].import.types', | ||
| ), | ||
| expectTarget( | ||
| exportMap.import.default, | ||
| "./dist/mod.js", | ||
| 'package.json exports["."].import.default', | ||
| ), | ||
| ]; | ||
|
|
||
| for (const target of new Set(targets)) { | ||
| await assertTargetExists(target); | ||
| } | ||
|
|
||
| const mod = require("@fedify/astro") as typeof import("@fedify/astro"); | ||
|
|
||
| strictEqual(typeof mod.fedifyIntegration, "function"); | ||
| strictEqual(typeof mod.fedifyMiddleware, "function"); | ||
|
|
||
| let nextCalled = false; | ||
| const middleware = mod.fedifyMiddleware( | ||
| toFederation<void>({ | ||
| async fetch( | ||
| _request: Request, | ||
| options: FederationFetchOptions<void>, | ||
| ) { | ||
| if (options.onNotFound == null) { | ||
| throw new TypeError("Expected onNotFound to be defined"); | ||
| } | ||
| return await options.onNotFound( | ||
| new Request("https://example.com/actor"), | ||
| ); | ||
| }, | ||
| }), | ||
| () => undefined, | ||
| ); | ||
|
|
||
| const response = expectResponse( | ||
| await middleware( | ||
| toApiContext({ request: new Request("https://example.com/inbox") }), | ||
| () => { | ||
| nextCalled = true; | ||
| return Promise.resolve(new Response("Handled by Astro")); | ||
| }, | ||
| ), | ||
| ); | ||
|
|
||
| strictEqual(nextCalled, true); | ||
| strictEqual(response.status, 200); | ||
| strictEqual(await response.text(), "Handled by Astro"); | ||
| }, | ||
| ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.