Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,5 @@ jobs:
- run: pnpm install --frozen-lockfile
- name: Lint
run: pnpm run lint
- name: Type Check
run: pnpm run build:types
- name: Test
- name: Build and test
run: pnpm run test
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@codex-data/sdk",
"version": "2.4.2",
"version": "2.4.3",
"engines": {
"node": ">=17.5.0"
},
Expand All @@ -25,10 +25,11 @@
"prebuild": "pnpm run clean && pnpm run generate:configs",
"clean": "rm -rf dist dist-esm",
"build": "pnpm run copy:schema && pnpm run generate:graphql && pnpm run build:sdk && pnpm run codegen && pnpm run lint ./src/sdk --fix && pnpm run build:types",
"build:types": "pnpm run build:cjs && pnpm run build:esm && pnpm run build:rename-esm",
"build:types": "pnpm run build:cjs && pnpm run build:esm && pnpm run build:rename-esm && pnpm run build:edge",
"build:cjs": "tsc",
"build:esm": "tsc -p tsconfig.esm.json",
"build:rename-esm": "find dist-esm -name '*.js' -exec sh -c 'mv \"$1\" \"${1%.js}.mjs\"' _ {} \\; && cp -r dist-esm/* dist/ && rm -rf dist-esm",
"build:edge": "esbuild src/edge.ts --bundle --format=esm --platform=neutral --packages=external --target=es2020 --outfile=dist/edge.mjs",
"fetch:schema": "curl -s https://graph.codex.io/schema/latest.graphql --output src/resources/schema.graphql && cp src/resources/schema.graphql schema.graphql",
"copy:schema": "cp src/resources/schema.graphql schema.graphql",
"schema:diff": "tsx src/scripts/schemaDiff.ts",
Expand All @@ -37,7 +38,7 @@
"build:sdk": "tsx src/scripts/buildSdk.ts",
"codegen": "graphql-codegen --config codegen.ts",
"lint": "eslint --ext .ts src",
"test": "jest",
"test": "pnpm run build:types && jest",
"format": "prettier --write .",
"prepublishOnly": "pnpm run build",
"publish:beta": "pnpm publish --tag beta --access public",
Expand Down Expand Up @@ -92,6 +93,7 @@
"chalk": "^4.1.2",
"cli-spinners": "^2.9.2",
"dotenv": "^16.4.5",
"esbuild": "^0.25.12",
"eslint": "^8.50.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-import": "^2.28.1",
Expand Down
8 changes: 6 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

103 changes: 103 additions & 0 deletions src/edge.bundle.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { execFileSync } from "node:child_process";
import { mkdirSync, mkdtempSync, rmSync, symlinkSync } from "node:fs";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";

import { build, type Metafile } from "esbuild";

const packageRoot = resolve(__dirname, "..");
let fixture: string;
let bundleBytes: number;
let metafile: Metafile;

beforeAll(async () => {
fixture = mkdtempSync(join(tmpdir(), "codex-sdk-edge-package-"));
const archive = join(fixture, "sdk.tgz");
execFileSync(
"pnpm",
["--config.ignore-scripts=true", "pack", "--out", archive],
{ cwd: packageRoot, stdio: "pipe" },
);
const packageDir = join(fixture, "node_modules", "@codex-data", "sdk");
mkdirSync(packageDir, { recursive: true });
execFileSync("tar", [
"-xzf",
archive,
"--strip-components=1",
"-C",
packageDir,
]);
for (const dependency of ["graphql", "graphql-ws"]) {
symlinkSync(
join(packageRoot, "node_modules", dependency),
join(fixture, "node_modules", dependency),
"junction",
);
}
const result = await build({
stdin: {
contents: `export { createClient, createEdgeClient, DEFAULT_EDGE_URL, withEdge } from "@codex-data/sdk/edge";`,
resolveDir: fixture,
sourcefile: "consumer.js",
},
bundle: true,
minify: true,
platform: "browser",
format: "esm",
write: false,
metafile: true,
});
bundleBytes = result.outputFiles[0].contents.byteLength;
metafile = result.metafile;
}, 30_000);

afterAll(() => {
rmSync(fixture, { recursive: true, force: true });
});

it("keeps the packed edge browser bundle below 60 KiB", () => {
expect(bundleBytes).toBeLessThan(60 * 1024);
});

it("excludes GraphQL server code and generated SDK documents from the bundle", () => {
const retained = Object.values(metafile.outputs).flatMap((output) =>
Object.entries(output.inputs)
.filter(([, input]) => input.bytesInOutput > 0)
.map(([file]) => file.replace(/\\/g, "/")),
);
expect(
retained.filter(
(file) =>
/\/graphql\/(execution|validation|type)\//.test(file) ||
/\/graphql-ws\/lib\/server\./.test(file) ||
file.includes("/sdk/generated/"),
),
).toEqual([]);
});

it.each(["import", "require"])(
"loads the packed public edge export with Node %s",
(mode) => {
const imports =
mode === "import"
? `import assert from "node:assert/strict";
import * as sdk from "@codex-data/sdk/edge";
import { createClient } from "graphql-ws";`
: `const assert = require("node:assert/strict");
const sdk = require("@codex-data/sdk/edge");
const { createClient } = require("graphql-ws");`;
execFileSync(
process.execPath,
[
`--input-type=${mode === "import" ? "module" : "commonjs"}`,
"--eval",
`${imports}
assert.equal(sdk.createClient, createClient);
assert.equal(typeof sdk.createEdgeClient, "function");
assert.equal(typeof sdk.withEdge, "function");
assert.equal(sdk.DEFAULT_EDGE_URL, "wss://fra.edge.codex.io/graphql");`,
],
{ cwd: fixture, stdio: "pipe" },
);
},
);
Loading