Skip to content

Commit

Permalink
Inject error codes from Devtools (#1281)
Browse files Browse the repository at this point in the history
* Add functionality to inject error and log messages.

* move injection code into separate file

* remove unused devDependency

* fix linter

* Update .changeset/gentle-kiwis-admire.md

Co-authored-by: Jerel Miller <[email protected]>

---------

Co-authored-by: Jerel Miller <[email protected]>
  • Loading branch information
phryneas and jerelmiller authored Mar 21, 2024
1 parent 7d3d445 commit badb1f9
Show file tree
Hide file tree
Showing 14 changed files with 3,239 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/gentle-kiwis-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"apollo-client-devtools": minor
---

Add the ability for devtools to provide full error and log messages without the need to need to call `loadDevMessages` or `loadErrorMessages`.
37 changes: 37 additions & 0 deletions .github/workflows/update-errorcodes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Create PR for new error codes

on: [workflow_dispatch]

jobs:
build:
defaults:
run:
working-directory: ./all-clients
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: "18.x"
- run: |
npm run rebuild
npm install
npm run encode
if git diff --exit-code --quiet HEAD errorcodes.json; then
echo "No new error codes were added."
exit 0
fi
cat <<EOF > ../.changeset/$(md5 < errorcodes.json).md
"apollo-client-devtools": patch
---
add error codes for new Apollo Client version
EOF
- name: Create Pull Request
uses: peter-evans/create-pull-request@v6
with:
title: "[chore] add error codes for new Apollo Client version"
commit-message: "[chore] add error codes for new Apollo Client version"
branch: "pr/update-errorcodes"
delete-branch: true
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
all-clients/errorcodes.json
82 changes: 82 additions & 0 deletions all-clients/encode-errorcodes.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import pkg from "./package.json" with { type: "json" };
import { writeFile } from "node:fs/promises";
import { restoreErrorCodes } from "./restore-errorcodes.mjs";
import assert from "node:assert";
import { gt } from "semver";

function getLookupArray() {
const map = new Map();
const array = [null];
function lookup(value) {
if (map.has(value)) {
return map.get(value);
} else {
const index = array.push(value) - 1;
map.set(value, index);
return index;
}
}
return [array, lookup];
}

const [allMessages, getMessageIndex] = getLookupArray();
const [allFiles, getFileIndex] = getLookupArray();
const [allConditions, getConditionIndex] = getLookupArray();
const byVersion = {};

const prefix = "@apollo-client/";
for (const partialPath of Object.keys(pkg.dependencies).sort((a, b) =>
gt(a.substring(prefix.length), b.substring(prefix.length)) ? 1 : -1
)) {
const match = new RegExp("/([^/]*)$").exec(partialPath);
if (!match) continue;
const version = match[1];
const from = import.meta.resolve(`${partialPath}/invariantErrorCodes.js`);

const { devDebug, devLog, devWarn, devError, errorCodes } = await import(
from
);
const combinedEntries = Object.entries({
...devDebug,
...devLog,
...devWarn,
...devError,
...errorCodes,
});

const collected = new Uint16Array(
(Math.max(...Object.keys(combinedEntries)) + 1) * 3
);

for (const entry of combinedEntries) {
const code = entry[0] - 1;
/** @type {{file: string, condition?: string, message: string}} */
const { file, condition, message } = entry[1];

collected[code * 3] = getMessageIndex(message);
collected[code * 3 + 1] = getFileIndex(file);
collected[code * 3 + 2] = getConditionIndex(condition);
}
byVersion[version] = Buffer.from(collected).toString("base64");

// we immediately restore to verify a full roundtrip
assert.deepStrictEqual(
restoreErrorCodes(
{ allMessages, allConditions, allFiles, byVersion },
version
),
Object.fromEntries(combinedEntries)
);
}
const encoded = JSON.stringify(
{
allMessages,
allConditions,
allFiles,
byVersion,
},
undefined,
2
);

writeFile("errorcodes.json", encoded, "utf-8");
235 changes: 235 additions & 0 deletions all-clients/errorcodes.json

Large diffs are not rendered by default.

Loading

0 comments on commit badb1f9

Please sign in to comment.