Skip to content

Commit

Permalink
🛢️ Refine client jsx transform
Browse files Browse the repository at this point in the history
  • Loading branch information
KimlikDAO-bot committed Feb 14, 2025
1 parent 7c38ce5 commit 8d84ebc
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 40 deletions.
2 changes: 2 additions & 0 deletions crosschain/unlockable.d.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ crosschain.Unlockable = function () { }

/** @type {string} */
crosschain.Unlockable.prototype.userPrompt;

export default crosschain;
26 changes: 19 additions & 7 deletions kastro/transpiler/jsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,17 @@ const transpile = (isEntry, file, content, domIdMapper, globals) => {
if (elem.openingElement) {
/** @const {string} */
const tagName = getJsxTagName(elem);
if (tagName.charCodeAt(0) < 91 && !assetComponents.has(tagName)) {
if (assetComponents.has(tagName)) return 0;
if (tagName.charCodeAt(0) < 91) {
const info = specifierInfo[tagName];
if (info && info.state == SpecifierState.Remove)
info.state = SpecifierState.PinRemove;
if (styleSheetComponents.has(tagName)) return 0;

const props = {};
let keepImport = false;
let instance = null;
let hasOptionalProps = false;
for (const attr of elem.openingElement.attributes) {
if (attr.type !== 'JSXAttribute') continue;
const name = attr.name.name;
Expand All @@ -145,6 +148,8 @@ const transpile = (isEntry, file, content, domIdMapper, globals) => {
instance = content.slice(attr.value.start + 1, attr.value.end - 1);
} else if (!name.endsWith("$"))
props[name] = attr.value;
else
hasOptionalProps = true;
}
if ((tagName in specifierInfo || localComponents.has(tagName)) && !styleSheetComponents.has(tagName)) {
keepImport = true;
Expand All @@ -160,7 +165,7 @@ const transpile = (isEntry, file, content, domIdMapper, globals) => {
traverse(v.expression, null);
return content.slice(v.start + 1, v.end - 1);
}
const callParams = Object.keys(props).length
const callParams = (Object.keys(props).length || hasOptionalProps)
? `{\n ${Object.entries(props).map(([k, v]) => `${k}: ${serialize(v)}`).join(",\n ")}\n }`
: "";
const call = `${tagName}(${callParams})`;
Expand All @@ -173,6 +178,7 @@ const transpile = (isEntry, file, content, domIdMapper, globals) => {
for (const attr of elem.openingElement.attributes) {
const name = attr.name.name;
if (name.startsWith("on") && name.charCodeAt(2) < 91) {
traverse(attr.value.expression, attr.value);
const value = content.slice(attr.value.start + 1, attr.value.end - 1);
const element = `${getJsxTagName(parent)}.children[${childIndex}]`;
statements.push(`${element}.${name.toLowerCase()} = ${value}`);
Expand All @@ -189,11 +195,14 @@ const transpile = (isEntry, file, content, domIdMapper, globals) => {
return 1;
}

const processInlineCss = (node) => {
if (node.tag.type != "Identifier" || node.tag.name != "css") return;
const processInlineCss = (node, parent) => {
/** @const {string} */
const strippedCss = content.slice(node.start + 4, node.end - 1)
.replace(/\$\{[^}]*\}/g, "a"); // Template literals cannot be exported identifiers, simply replace them with a placeholder

if (parent.type == "VariableDeclarator" && parent.id.type == "Identifier")
styleSheetComponents.add(parent.id.name);

updates.push({
beg: node.start,
end: node.end,
Expand All @@ -213,8 +222,10 @@ const transpile = (isEntry, file, content, domIdMapper, globals) => {
if (!isPropertyName && !isDestructured)
specifierInfo[node.name].state = SpecifierState.Keep;
} else if (node.type === "TaggedTemplateExpression") {
processInlineCss(node);
return;
if (node.tag.type == "Identifier" && node.tag.name == "css") {
processInlineCss(node, parent);
return;
}
} else if (node.type === "JSXElement" || node.type === "JSXFragment") {
const statements = [];
processJsxElement(node, parent, 0, statements);
Expand Down Expand Up @@ -303,7 +314,8 @@ const transpile = (isEntry, file, content, domIdMapper, globals) => {
/** @const {!Array<string>} */
const identifiers = block.match(IdentPattern) || [];
for (const ident of identifiers) {
const info = specifierInfo[ident];
const rootIdent = ident.split('.')[0];
const info = specifierInfo[rootIdent];
if (info) info.state = SpecifierState.Keep;
}
}
Expand Down
36 changes: 18 additions & 18 deletions node/ipfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,48 +71,48 @@ const CID = (hash) => {
* @param {!Uint8Array} cidByte
* @return {!Promise<string>}
*/
const cidBytetanOku = (nodeUrl, cidByte) => {
const readWithCIDBytes = (nodeUrl, cidByte) => {
/** @const {string} */
const yerelCID = CID(cidByte);
return fetch(nodeUrl + "/ipfs/" + yerelCID)
const localCID = CID(cidByte);
return fetch(nodeUrl + "/ipfs/" + localCID)
.then((res) => res.arrayBuffer())
.then((/** @type {!ArrayBuffer} */ buf) => hash(new Uint8Array(buf))
.then((gelenByte) => CID(gelenByte) === yerelCID
.then((gelenByte) => CID(gelenByte) === localCID
? new TextDecoder().decode(buf)
: Promise.reject("IPFS hash'i tutmadı"))
: Promise.reject("IPFS hash mismatch"))
);
}

/**
* @param {string} nodeUrl
* @param {string} veri IPFS'e yazılacak veri.
* @param {string} veriŞekli Yazılacak verinin şekli, mimetype standardında
* @return {!Promise<!Uint8Array>} onaylanmış IPFS cidByte.
* @param {string} data Data to be written to IPFS.
* @param {string} dataType Mime type of the data.
* @return {!Promise<!Uint8Array>} Validated IPFS cidByte's of the data.
*/
const yaz = (nodeUrl, veri, veriŞekli) => {
const write = (nodeUrl, data, dataType) => {
/** @const {!Uint8Array} */
const encoded = new TextEncoder().encode(veri);
const encoded = new TextEncoder().encode(data);
/** @const {!FormData} */
const formData = new FormData()
formData.set("blob", new Blob([encoded], { type: veriŞekli }));
formData.set("blob", new Blob([encoded], { type: dataType }));
/** @const {!Promise<string>} */
const gelenSöz = fetch(nodeUrl + "/api/v0/add", {
const remoteHashPromise = fetch(nodeUrl + "/api/v0/add", {
method: "POST",
body: formData
})
.then((res) => res.json())
.then((/** @type {node.ipfs.AddResult} */ res) => res.Hash)

return Promise.all([hash(encoded), gelenSöz])
.then(([/** !Uint8Array */ yerel, /** string */ gelen]) => CID(yerel) == gelen
? yerel
: Promise.reject(`IPFS'ten farklı sonuç döndü. Yerel: ${CID(yerel)}, Gelen: ${gelen}`)
return Promise.all([hash(encoded), remoteHashPromise])
.then(([/** !Uint8Array */ local, /** string */ remote]) => CID(local) == remote
? local
: Promise.reject(`IPFS hash mismatch. Local: ${CID(local)}, Remote: ${remote}`)
)
}

export default {
CID,
cidBytetanOku,
readWithCIDBytes,
hash,
yaz,
write,
};
26 changes: 12 additions & 14 deletions node/test/ipfs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@ test("CID test", () => {
/** @const {!TextEncoder} */
const encoder = new TextEncoder();

return Promise.all([
ipfs.hash(encoder.encode("a".repeat(2680)))
.then((/** @type {!Uint8Array} */ hash) => expect(ipfs.CID(hash))
.toBe("Qmawd3DRAY5YwtCzQe8gBumMXA1JCrzvbH2WQR6ZTykVoG")),
ipfs.hash(encoder.encode("KimlikDAO\n"))
.then((/** @type {!Uint8Array} */ hash) => expect(ipfs.CID(hash))
.toBe("QmafCiqeYQtiXokAEUB4ToMcZJREhJcShbzvjrYmC1WCsi")),
ipfs.hash(encoder.encode("foo\n"))
.then((/** @type {!Uint8Array} */ hash) => expect(ipfs.CID(hash))
.toBe("QmYNmQKp6SuaVrpgWRsPTgCQCnpxUYGq76YEKBXuj2N4H6")),
ipfs.hash(encoder.encode("a".repeat(31337)))
.then((/** @type {!Uint8Array} */ hash) => expect(ipfs.CID(hash))
.toBe("Qmbq6rxwg5uKYAEhdFvPnBqzbJWAPfhB4LwF4yGamvzWSR")),
]).then((_) => { });
ipfs.hash(encoder.encode("a".repeat(2680)))
.then((/** @type {!Uint8Array} */ hash) => expect(ipfs.CID(hash))
.toBe("Qmawd3DRAY5YwtCzQe8gBumMXA1JCrzvbH2WQR6ZTykVoG"));
ipfs.hash(encoder.encode("KimlikDAO\n"))
.then((/** @type {!Uint8Array} */ hash) => expect(ipfs.CID(hash))
.toBe("QmafCiqeYQtiXokAEUB4ToMcZJREhJcShbzvjrYmC1WCsi"));
ipfs.hash(encoder.encode("foo\n"))
.then((/** @type {!Uint8Array} */ hash) => expect(ipfs.CID(hash))
.toBe("QmYNmQKp6SuaVrpgWRsPTgCQCnpxUYGq76YEKBXuj2N4H6"));
ipfs.hash(encoder.encode("a".repeat(31337)))
.then((/** @type {!Uint8Array} */ hash) => expect(ipfs.CID(hash))
.toBe("Qmbq6rxwg5uKYAEhdFvPnBqzbJWAPfhB4LwF4yGamvzWSR"));
});
9 changes: 8 additions & 1 deletion util/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { LangCode } from "./i18n";
const GEN = true;

/** @define {LangCode} */
const Lang = LangCode.TR;
const Lang = LangCode.EN;

/**
* @nosideeffects
Expand Down Expand Up @@ -59,6 +59,12 @@ const gösterGizle = (birim, göster) => birim.style.display = göster ? "" : "n
*/
const adlaGizle = (ad) => adla(ad).style.display = "none";

/**
* @noinline
* @param {string} ad
*/
const hideById = adlaGizle;

/**
* @noinline
* @param {string} ad
Expand Down Expand Up @@ -262,6 +268,7 @@ export default {
gösterGizle,
show,
hide,
hideById,
// Widgets
bindDropdown,
düğmeDurdur,
Expand Down

0 comments on commit 8d84ebc

Please sign in to comment.