Skip to content

Commit 51a0f5d

Browse files
committed
fix(rune): output serialization bug
1 parent 3d2d687 commit 51a0f5d

File tree

5 files changed

+124
-143
lines changed

5 files changed

+124
-143
lines changed

src/services/ipfs/utils/content.ts

Lines changed: 94 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -85,121 +85,121 @@ export const parseArrayLikeToDetails = async (
8585
cid: string,
8686
onProgress?: onProgressCallback
8787
): Promise<IPFSContentDetails> => {
88-
try {
89-
if (!content || !content?.result) {
90-
return {
91-
gateway: true,
92-
text: cid.toString(),
93-
cid,
94-
};
95-
}
88+
// try {
89+
if (!content || !content?.result) {
90+
return {
91+
gateway: true,
92+
text: cid.toString(),
93+
cid,
94+
};
95+
}
9696

97-
const { result, meta } = content;
97+
const { result, meta } = content;
9898

99-
const mime = meta?.mime;
99+
const mime = meta?.mime;
100100

101-
if (!mime) {
102-
return {
103-
cid,
104-
gateway: true,
105-
text: `Can't detect MIME for ${cid.toString()}`,
106-
};
107-
}
108-
const contentType = mimeToBaseContentType(mime);
109-
const contentCid = content.cid;
110-
111-
const response: IPFSContentDetails = {
112-
link: `/ipfs/${cid}`,
113-
gateway: false,
114-
cid: contentCid,
115-
type: contentType,
101+
if (!mime) {
102+
return {
103+
cid,
104+
gateway: true,
105+
text: `Can't detect MIME for ${cid.toString()}`,
116106
};
107+
}
108+
const contentType = mimeToBaseContentType(mime);
109+
const contentCid = content.cid;
110+
111+
const response: IPFSContentDetails = {
112+
link: `/ipfs/${cid}`,
113+
gateway: false,
114+
cid: contentCid,
115+
type: contentType,
116+
};
117+
118+
if (detectGatewayContentType(mime)) {
119+
return { ...response, gateway: true };
120+
}
117121

118-
if (detectGatewayContentType(mime)) {
119-
return { ...response, gateway: true };
120-
}
122+
const rawData =
123+
typeof result !== 'string'
124+
? await getResponseResult(result, onProgress)
125+
: result;
121126

122-
const rawData =
123-
typeof result !== 'string'
124-
? await getResponseResult(result, onProgress)
125-
: result;
127+
const isStringData = typeof rawData === 'string';
126128

127-
const isStringData = typeof rawData === 'string';
129+
// console.log(rawData);
130+
if (!rawData) {
131+
return {
132+
...response,
133+
gateway: true,
134+
text: `Can't parse content for ${cid.toString()}`,
135+
};
136+
}
128137

129-
// console.log(rawData);
130-
if (!rawData) {
138+
// clarify text-content subtypes
139+
if (response.type === 'text') {
140+
// render svg as image
141+
if (!isStringData && isSvg(Buffer.from(rawData))) {
131142
return {
132143
...response,
133-
gateway: true,
134-
text: `Can't parse content for ${cid.toString()}`,
144+
type: 'image',
145+
content: createImgData(rawData, 'image/svg+xml'),
135146
};
136147
}
137148

138-
// clarify text-content subtypes
139-
if (response.type === 'text') {
140-
// render svg as image
141-
if (!isStringData && isSvg(Buffer.from(rawData))) {
142-
return {
143-
...response,
144-
type: 'image',
145-
content: createImgData(rawData, 'image/svg+xml'),
146-
};
147-
}
148-
149-
const str = isStringData ? rawData : uint8ArrayToAsciiString(rawData);
150-
151-
if (str.match(PATTERN_IPFS_HASH)) {
152-
return {
153-
...response,
154-
type: 'cid',
155-
content: str,
156-
};
157-
}
158-
if (str.match(PATTERN_HTTP)) {
159-
return {
160-
...response,
161-
type: 'link',
162-
content: str,
163-
};
164-
}
165-
if (isHtml(str)) {
166-
return {
167-
...response,
168-
type: 'html',
169-
gateway: true,
170-
content: cid.toString(),
171-
};
172-
}
173-
174-
// TODO: search can bel longer for 42???!
175-
// also cover ipns links
149+
const str = isStringData ? rawData : uint8ArrayToAsciiString(rawData);
150+
151+
if (str.match(PATTERN_IPFS_HASH)) {
176152
return {
177153
...response,
178-
link: str.length > 42 ? `/ipfs/${cid}` : `/search/${str}`,
179-
type: 'text',
180-
text: shortenString(str),
154+
type: 'cid',
181155
content: str,
182156
};
183157
}
184-
185-
if (!isStringData) {
186-
if (response.type === 'image') {
187-
return { ...response, content: createImgData(rawData, mime) }; // file
188-
}
189-
if (response.type === 'pdf') {
190-
return {
191-
...response,
192-
content: createObjectURL(rawData, mime),
193-
gateway: true,
194-
}; // file
195-
}
158+
if (str.match(PATTERN_HTTP)) {
159+
return {
160+
...response,
161+
type: 'link',
162+
content: str,
163+
};
164+
}
165+
if (isHtml(str)) {
166+
return {
167+
...response,
168+
type: 'html',
169+
gateway: true,
170+
content: cid.toString(),
171+
};
196172
}
197173

198-
return response;
199-
} catch (e) {
200-
console.log('----parseRawIpfsData', e, cid);
201-
return undefined;
174+
// TODO: search can bel longer for 42???!
175+
// also cover ipns links
176+
return {
177+
...response,
178+
link: str.length > 42 ? `/ipfs/${cid}` : `/search/${str}`,
179+
type: 'text',
180+
text: shortenString(str),
181+
content: str,
182+
};
202183
}
184+
185+
if (!isStringData) {
186+
if (response.type === 'image') {
187+
return { ...response, content: createImgData(rawData, mime) }; // file
188+
}
189+
if (response.type === 'pdf') {
190+
return {
191+
...response,
192+
content: createObjectURL(rawData, mime),
193+
gateway: true,
194+
}; // file
195+
}
196+
}
197+
198+
return response;
199+
// } catch (e) {
200+
// console.log('----parseRawIpfsData', e, cid);
201+
// return undefined;
202+
// }
203203
};
204204

205205
export const contentToUint8Array = async (

src/services/ipfs/utils/stream.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,27 +132,32 @@ export const getResponseResult = async (
132132
return readArray;
133133
}
134134

135-
const reader = response[Symbol.asyncIterator]();
136-
137-
// if (cid === 'QmRqms6Utkk6L4mtyLQXY2spcQ8Pk7fBBTNjvxa9jTNrXp') {
138-
// debugger;
139-
// }
140-
// eslint-disable-next-line no-restricted-syntax
141-
for await (const chunk of reader) {
142-
if (chunk instanceof Uint8Array) {
143-
chunks.push(chunk);
144-
bytesDownloaded += chunk.byteLength;
145-
onProgress && onProgress(bytesDownloaded);
135+
if (Symbol.asyncIterator in response) {
136+
const reader = response[Symbol.asyncIterator]();
137+
138+
// if (cid === 'QmRqms6Utkk6L4mtyLQXY2spcQ8Pk7fBBTNjvxa9jTNrXp') {
139+
// debugger;
140+
// }
141+
// eslint-disable-next-line no-restricted-syntax
142+
for await (const chunk of reader) {
143+
if (chunk instanceof Uint8Array) {
144+
chunks.push(chunk);
145+
bytesDownloaded += chunk.byteLength;
146+
onProgress && onProgress(bytesDownloaded);
147+
}
146148
}
149+
const result = uint8ArrayConcat(chunks);
150+
return result;
147151
}
148-
const result = uint8ArrayConcat(chunks);
149-
return result;
152+
return undefined;
150153
} catch (error) {
151154
console.error(
152155
`Error reading stream/iterable.\r\n Probably Hot reload error!`,
153156
error
154157
);
155158

159+
// throw error;
160+
156161
return undefined;
157162
}
158163
};

src/services/ipfs/utils/utils-ipfs.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,6 @@ const fetchIPFSContentFromGateway = async (
175175
method: 'GET',
176176
signal: controller?.signal,
177177
});
178-
if (cid === 'QmakRbRoKh5Nss8vbg9qnNN2Bcsr7jUX1nbDeMT5xe8xa1') {
179-
console.log(
180-
'----fetchIPFSContentFromGateway',
181-
response,
182-
contentUrl,
183-
cid,
184-
node,
185-
controller?.signal
186-
);
187-
}
188178
if (response && response.body) {
189179
// fetch doesn't provide any headers in our case :(
190180

src/services/scripting/engine.ts

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,18 @@ import initAsync, { compile } from 'cyb-rune-wasm';
22

33
import { v4 as uuidv4 } from 'uuid';
44

5-
import { CyberClient } from '@cybercongress/cyber-js';
6-
import {
7-
OfflineSigner,
8-
SigningCyberClient,
9-
} from '@cybercongress/cyber-js/build/signingcyberclient';
105
import { TabularKeyValues } from 'src/types/data';
116
import { keyValuesToObject } from 'src/utils/localStorage';
127

138
import { mapObjIndexed } from 'ramda';
9+
import { removeBrokenUnicode } from 'src/utils/string';
10+
1411
import { extractRuneScript } from './helpers';
15-
import { RemoteIpfsApi } from '../backend/workers/background/worker';
1612

1713
import {
1814
ScriptCallback,
1915
ScriptParticleParams,
2016
ScriptContext,
21-
ScriptMyParticleResult,
2217
ScriptParticleResult,
2318
// ScriptMyParticleParams,
2419
ScriptEntrypoints,
@@ -29,7 +24,6 @@ import {
2924
} from './types';
3025

3126
import runtimeScript from './rune/runtime.rn';
32-
import { deserializeString, serializeString } from 'src/utils/string';
3327

3428
const compileConfig = {
3529
budget: 1_000_000,
@@ -46,23 +40,9 @@ type CompilerParams = {
4640
config: typeof compileConfig;
4741
};
4842

49-
// | { name: 'ipfs'; item: AppIPFS }
50-
// | { name: 'queryClient'; item: CyberClient }
51-
// | {
52-
// name: 'signer';
53-
// item: { signer?: OfflineSigner; signingClient: SigningCyberClient };
54-
// };
55-
5643
const toRecord = (item: TabularKeyValues) =>
5744
keyValuesToObject(Object.values(item));
5845

59-
// type EngineDeps = {
60-
// ipfs?: RemoteIpfsApi;
61-
// queryClient?: CyberClient;
62-
// signer?: OfflineSigner;
63-
// signingClient?: SigningCyberClient;
64-
// };
65-
6646
export type LoadParams = {
6747
entrypoints: ScriptEntrypoints;
6848
secrets: TabularKeyValues;
@@ -184,7 +164,7 @@ function enigine(): RuneEngine {
184164
...outputData,
185165
error,
186166
result: result
187-
? JSON.parse(result)
167+
? JSON.parse(removeBrokenUnicode(result))
188168
: { action: 'error', message: 'No result' },
189169
};
190170
} catch (e) {
@@ -233,21 +213,21 @@ function enigine(): RuneEngine {
233213
const { cid, contentType, content } = params;
234214
const output = await run(script, {
235215
funcName: 'personal_processor',
236-
funcParams: [cid, contentType, serializeString(content || '')], //params as EntrypointParams,
216+
funcParams: [cid, contentType, content], //params as EntrypointParams,
237217
});
238218

239219
const { action, content: outputContent } = output.result;
240220

241221
if (action === 'error') {
242222
console.error(
243-
`[rune].personalProcessor error: ${params.cid}`,
223+
`RUNE: personalProcessor error: ${params.cid}`,
244224
params,
245225
output
246226
);
247227
}
248228

249229
if (outputContent) {
250-
return { ...output.result, content: deserializeString(outputContent) };
230+
return { ...output.result, content: outputContent };
251231
}
252232

253233
return output.result;

src/utils/string.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,9 @@ export function deserializeString(serialized: string): string {
2525
.replace(/\\\\/g, '\\') // Unescape backslashes
2626
.replace(/\\!!/g, '#'); // Unescape # cozo comment
2727
}
28+
29+
const specialCharsRegexe = /\\u\{[a-fA-F0-9]+\}/g;
30+
31+
export function removeBrokenUnicode(string: string): string {
32+
return string.replace(specialCharsRegexe, '');
33+
}

0 commit comments

Comments
 (0)