Repository navigation
build: add opt-in Node SDK next prereleases #802
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
roncohen
wants to merge
2
commits into
main
Choose a base branch
from
build/node-sdk-next-prerelease
base: main
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| name: Node SDK next prerelease | ||
|
|
||
| on: | ||
| workflow_dispatch: | ||
| inputs: | ||
| source_sha: | ||
| description: Exact 40-character commit SHA with passing Package CI | ||
| required: true | ||
| type: string | ||
| version: | ||
| description: Expected Node manifest version (2.0.0-next.N) | ||
| required: true | ||
| default: 2.0.0-next.0 | ||
| type: string | ||
| publish: | ||
| description: Publish the verified tarball to npm under next (false only prepares an artifact) | ||
| required: true | ||
| default: false | ||
| type: boolean | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| concurrency: | ||
| group: node-sdk-next-prerelease | ||
| cancel-in-progress: false | ||
|
|
||
| jobs: | ||
| prepare: | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 20 | ||
| permissions: | ||
| contents: read | ||
| checks: read | ||
| actions: read | ||
| outputs: | ||
| sha256: ${{ steps.prepare.outputs.sha256 }} | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| with: | ||
| ref: ${{ github.sha }} | ||
| path: release-tools | ||
| persist-credentials: false | ||
| - name: Validate dispatch and candidate identity | ||
| env: | ||
| WORKFLOW_REF: ${{ github.ref }} | ||
| SOURCE_SHA: ${{ inputs.source_sha }} | ||
| VERSION: ${{ inputs.version }} | ||
| run: | | ||
| test "$WORKFLOW_REF" = refs/heads/main | ||
| node release-tools/scripts/node-sdk-prerelease.mjs validate --version "$VERSION" --sha "$SOURCE_SHA" | ||
| - name: Require successful CI on the exact candidate | ||
| env: | ||
| GH_TOKEN: ${{ github.token }} | ||
| SOURCE_SHA: ${{ inputs.source_sha }} | ||
| VERSION: ${{ inputs.version }} | ||
| run: | | ||
| gh api --paginate --slurp "repos/$GITHUB_REPOSITORY/commits/$SOURCE_SHA/check-runs?per_page=100" > "$RUNNER_TEMP/candidate-checks.json" | ||
| node release-tools/scripts/node-sdk-prerelease.mjs ci --version "$VERSION" --sha "$SOURCE_SHA" --checks "$RUNNER_TEMP/candidate-checks.json" | ||
| - name: Require publication approval protection | ||
| if: ${{ inputs.publish }} | ||
| env: | ||
| GH_TOKEN: ${{ github.token }} | ||
| run: | | ||
| gh api "repos/$GITHUB_REPOSITORY/environments/npm-node-prerelease" > "$RUNNER_TEMP/prerelease-environment.json" | ||
| node release-tools/scripts/node-sdk-prerelease.mjs environment --environment "$RUNNER_TEMP/prerelease-environment.json" | ||
| - uses: actions/checkout@v4 | ||
| with: | ||
| ref: ${{ inputs.source_sha }} | ||
| path: candidate | ||
| persist-credentials: false | ||
| - name: Enable corepack | ||
| run: corepack enable | ||
| - uses: actions/setup-node@v6 | ||
| with: | ||
| node-version-file: release-tools/.nvmrc | ||
| # Never save candidate-controlled dependencies into main's cache scope. | ||
| package-manager-cache: false | ||
| - name: Build, test and verify the actual packed package | ||
| id: prepare | ||
| env: | ||
| SOURCE_SHA: ${{ inputs.source_sha }} | ||
| VERSION: ${{ inputs.version }} | ||
| HUSKY: "0" | ||
| run: | | ||
| node release-tools/scripts/node-sdk-prerelease.mjs prepare \ | ||
| --source "$GITHUB_WORKSPACE/candidate" --output "$RUNNER_TEMP/node-sdk-next" \ | ||
| --version "$VERSION" --sha "$SOURCE_SHA" | ||
| - uses: actions/upload-artifact@v4 | ||
| with: | ||
| name: node-sdk-next | ||
| path: ${{ runner.temp }}/node-sdk-next/ | ||
| if-no-files-found: error | ||
| retention-days: 7 | ||
|
|
||
| publish: | ||
| if: ${{ inputs.publish && github.ref == 'refs/heads/main' }} | ||
| needs: prepare | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 5 | ||
| environment: npm-node-prerelease | ||
| permissions: | ||
| contents: read | ||
| id-token: write | ||
| steps: | ||
| # Only trusted workflow tools are checked out here. Candidate code and its | ||
| # dependency/lifecycle scripts never execute in this OIDC-enabled job. | ||
| - uses: actions/checkout@v4 | ||
| with: | ||
| ref: ${{ github.sha }} | ||
| persist-credentials: false | ||
| - uses: actions/setup-node@v6 | ||
| with: | ||
| node-version-file: .nvmrc | ||
| package-manager-cache: false | ||
| registry-url: https://registry.npmjs.org | ||
| scope: "@reflag" | ||
| - uses: actions/download-artifact@v4 | ||
| with: | ||
| name: node-sdk-next | ||
| path: ${{ runner.temp }}/node-sdk-next | ||
| - name: Verify immutable artifact and current registry state | ||
| env: | ||
| SOURCE_SHA: ${{ inputs.source_sha }} | ||
| VERSION: ${{ inputs.version }} | ||
| SHA256: ${{ needs.prepare.outputs.sha256 }} | ||
| run: | | ||
| node -e 'const [major,minor,patch] = require("node:child_process").execFileSync("npm", ["--version"], {encoding:"utf8"}).trim().split(".").map(Number); if (!(major > 11 || (major === 11 && (minor > 5 || (minor === 5 && patch >= 1))))) throw new Error("npm >=11.5.1 is required for trusted publishing");' | ||
| node scripts/node-sdk-prerelease.mjs verify \ | ||
| --output "$RUNNER_TEMP/node-sdk-next" --version "$VERSION" --sha "$SOURCE_SHA" --sha256 "$SHA256" | ||
| - name: Publish only the verified Node tarball under next | ||
| env: | ||
| VERSION: ${{ inputs.version }} | ||
| run: | | ||
| npm publish "$RUNNER_TEMP/node-sdk-next/reflag-node-sdk-$VERSION.tgz" \ | ||
| --tag next --access public --provenance --ignore-scripts --registry https://registry.npmjs.org | ||
| - name: Confirm next, artifact integrity and unchanged latest | ||
| env: | ||
| SOURCE_SHA: ${{ inputs.source_sha }} | ||
| VERSION: ${{ inputs.version }} | ||
| SHA256: ${{ needs.prepare.outputs.sha256 }} | ||
| run: | | ||
| # Registry/CDN reads may briefly lag a successful immutable publish. | ||
| for attempt in 1 2 3 4 5; do | ||
| if node scripts/node-sdk-prerelease.mjs verify --published \ | ||
| --output "$RUNNER_TEMP/node-sdk-next" --version "$VERSION" --sha "$SOURCE_SHA" --sha256 "$SHA256"; then | ||
| printf '\n### Published\n\n`@reflag/node-sdk@%s` under `next`; `latest` remains unchanged.\n' "$VERSION" >> "$GITHUB_STEP_SUMMARY" | ||
| exit 0 | ||
| fi | ||
| if [ "$attempt" = 5 ]; then exit 1; fi | ||
| sleep 2 | ||
| done | ||
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,43 @@ | ||
| import { ReflagClient, type FlagValue } from "@reflag/node-sdk"; | ||
|
|
||
| interface Settings { | ||
| model: string; | ||
| limits: { maxItems: number }; | ||
| } | ||
| declare module "@reflag/node-sdk" { | ||
| interface Flags { | ||
| settings: Settings; | ||
| model: "quick" | "quality"; | ||
| list: number[]; | ||
| arbitrary: FlagValue; | ||
| } | ||
| } | ||
| const client = new ReflagClient({ offline: true }); | ||
| const defaults: Settings = { model: "quick", limits: { maxItems: 10 } }; | ||
| const value: Settings = client.getFlag({}, "settings", defaults); | ||
| const mutable = client.bindClient({}).getFlag("settings", { | ||
| model: "quick", | ||
| limits: { maxItems: 10 }, | ||
| } as const); | ||
| mutable.limits.maxItems = 11; | ||
| const enabled: boolean = client.getFlag({}, "settings"); | ||
| const arbitrary: FlagValue = client.getFlag({}, "settings", null); | ||
| const model: string = client.getFlag({}, "model", "quick"); | ||
| client.getFlag({}, "arbitrary", { anyShape: [false, null, 0] }); | ||
| // @ts-expect-error unknown registered key | ||
| client.getFlag({}, "unknown"); | ||
| // @ts-expect-error missing required property | ||
| client.getFlag({}, "settings", { model: "quick" }); | ||
| // @ts-expect-error incompatible nested property | ||
| client.getFlag({}, "settings", { model: "quick", limits: { maxItems: "ten" } }); | ||
| // @ts-expect-error registered category mismatch | ||
| client.getFlag({}, "settings", 0); | ||
| // @ts-expect-error unknown registered literal | ||
| client.getFlag({}, "model", "unknown"); | ||
| // @ts-expect-error wrong array element | ||
| client.bindClient({}).getFlag("list", ["bad"]); | ||
| // @ts-expect-error remote defaults obey the same registered schema | ||
| client.getFlagRemote("settings", { model: "quick" }); | ||
| // @ts-expect-error direct values have no legacy config wrapper | ||
| void value.config.payload; | ||
| void [value, mutable, enabled, arbitrary, model]; |
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,146 @@ | ||
| const assert = require("node:assert/strict"); | ||
| const { createServer } = require("node:http"); | ||
| const { createRequire } = require("node:module"); | ||
| const { ReflagClient } = require("@reflag/node-sdk"); | ||
|
|
||
| const version = process.argv[2]; | ||
| const sdkRequire = createRequire(require.resolve("@reflag/node-sdk")); | ||
| assert.equal(require("@reflag/node-sdk/package.json").version, version); | ||
| assert.equal( | ||
| sdkRequire("@reflag/flag-evaluation/package.json").version, | ||
| "2.0.0", | ||
| ); | ||
| for (const key of Object.keys(process.env)) | ||
| if (key.startsWith("REFLAG_")) delete process.env[key]; | ||
|
|
||
| function flag(key, value) { | ||
| return { | ||
| key, | ||
| sourceVersionId: `version:${key}`, | ||
| variants: { selected: value }, | ||
| rules: [ | ||
| { | ||
| id: `rule:${key}`, | ||
| filter: { type: "constant", value: true }, | ||
| result: { type: "variant", variantKey: "selected" }, | ||
| }, | ||
| ], | ||
| }; | ||
| } | ||
| const events = []; | ||
| const requests = []; | ||
| const noop = () => undefined; | ||
| const server = createServer(async (request, response) => { | ||
| try { | ||
| requests.push({ | ||
| path: request.url, | ||
| version: request.headers["reflag-sdk-version"], | ||
| }); | ||
| response.setHeader("content-type", "application/json"); | ||
| if (request.url === "/flags") { | ||
| response.end( | ||
| JSON.stringify({ | ||
| success: true, | ||
| protocolVersion: 2, | ||
| flagStateVersion: 42, | ||
| segments: [], | ||
| flags: [ | ||
| flag("enabled", true), | ||
| flag("settings", { model: "quality", limits: { maxItems: 0 } }), | ||
| flag("false", false), | ||
| flag("zero", 0), | ||
| flag("empty", ""), | ||
| flag("nullable", null), | ||
| ], | ||
| }), | ||
| ); | ||
| } else if (request.url === "/bulk") { | ||
| let body = ""; | ||
| for await (const chunk of request) body += chunk; | ||
| events.push(...JSON.parse(body)); | ||
| response.end(JSON.stringify({ success: true })); | ||
| } else { | ||
| response.statusCode = 404; | ||
| response.end(JSON.stringify({ success: false })); | ||
| } | ||
| } catch (error) { | ||
| response.statusCode = 500; | ||
| response.end(JSON.stringify({ success: false })); | ||
| console.error(error); | ||
| } | ||
| }); | ||
|
|
||
| (async () => { | ||
| await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); | ||
| const client = new ReflagClient({ | ||
| secretKey: "prereleaseFixtureSecretMoreThan22Chars", | ||
| apiBaseUrl: `http://127.0.0.1:${server.address().port}/`, | ||
| offline: false, | ||
| flagsSyncMode: "in-request", | ||
| flagsFetchRetries: 0, | ||
| batchOptions: { flushOnExit: false, intervalMs: 1000000 }, | ||
| logger: { debug: noop, info: noop, warn: noop, error: noop }, | ||
| }); | ||
| try { | ||
| await client.initialize(); | ||
| assert.equal(client.getFlag({}, "enabled"), true); | ||
| const settings = client.getFlag({}, "settings", { | ||
| model: "quick", | ||
| limits: { maxItems: 10 }, | ||
| }); | ||
| assert.deepEqual(settings, { model: "quality", limits: { maxItems: 0 } }); | ||
| settings.limits.maxItems = 99; | ||
| assert.equal( | ||
| client.getFlag({}, "settings", { | ||
| model: "quick", | ||
| limits: { maxItems: 10 }, | ||
| }).limits.maxItems, | ||
| 0, | ||
| ); | ||
| assert.equal(client.getFlag({}, "settings"), false); | ||
| assert.equal(client.getFlag({}, "false", true), false); | ||
| assert.equal(client.getFlag({}, "zero", 99), 0); | ||
| assert.equal(client.getFlag({}, "empty", "fallback"), ""); | ||
| assert.equal(client.getFlag({}, "nullable", null), null); | ||
| assert.equal(client.getFlag({}, "missing"), false); | ||
| await client.flush(); | ||
| const checksBeforeBootstrap = events.length; | ||
| assert.equal(client.getFlagsForBootstrap({}).protocolVersion, 2); | ||
| await client.flush(); | ||
| assert.equal(events.length, checksBeforeBootstrap); | ||
| assert.ok( | ||
| requests.some( | ||
| (request) => | ||
| request.path === "/flags" && | ||
| request.version === `node-sdk/${version}`, | ||
| ), | ||
| ); | ||
| assert.ok( | ||
| events.some( | ||
| (event) => | ||
| event.action === "check" && | ||
| event.key === "enabled" && | ||
| event.sourceVersionId === "version:enabled" && | ||
| event.evalResult === "selected", | ||
| ), | ||
| ); | ||
| assert.ok( | ||
| events.some( | ||
| (event) => | ||
| event.action === "check" && | ||
| event.key === "missing" && | ||
| !Object.hasOwn(event, "evalResult"), | ||
| ), | ||
| ); | ||
| console.log( | ||
| `Installed ${version}: direct values, defaults, copies, v2 transport, root checks and version header passed`, | ||
| ); | ||
| } finally { | ||
| client.destroy(); | ||
| server.closeAllConnections(); | ||
| await new Promise((resolve) => server.close(resolve)); | ||
| } | ||
| })().catch((error) => { | ||
| console.error(error); | ||
| process.exitCode = 1; | ||
| }); |
Oops, something went wrong.
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.