mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-11-03 07:54:40 +08:00
Merge remote-tracking branch 'origin/ni/i7-routing' into HEAD
# Conflicts: # packages/libnode-common/bridge/scr_node_bridge.cc
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
// @requires-libnode
|
||||
// structuredClone in a Node island of native objects passed in clones
|
||||
// their data, as under Node; arrays of plain values the island returns,
|
||||
// directly or through a promise, arrive as arrays.
|
||||
import { cloneClaims, cloneFails, cloneNested, listFiles, matrix, names } from "data-pkg";
|
||||
|
||||
interface Claims {
|
||||
sub: string;
|
||||
aud: string;
|
||||
exp: number;
|
||||
}
|
||||
|
||||
class Session {
|
||||
user = "ada";
|
||||
level = 2;
|
||||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const claims: Claims = { sub: "user-1", aud: "api", exp: 1700000000 };
|
||||
console.log(cloneClaims(claims));
|
||||
console.log("native unchanged", claims.sub);
|
||||
console.log(cloneNested({ list: [1, 2], session: new Session() }));
|
||||
console.log(cloneNested([claims, claims]));
|
||||
const tags: string[] = ["a", "b"];
|
||||
console.log(cloneNested({ tags, claims }), cloneNested(tags));
|
||||
console.log(cloneFails());
|
||||
const files = await listFiles("/tmp");
|
||||
for (const f of files) console.log("file", f);
|
||||
console.log("count", files.length, files.join(","));
|
||||
console.log("names", names().join("|"), names().length);
|
||||
const m = await matrix();
|
||||
console.log("matrix", m.length, m[0].join("+"), m[1][0]);
|
||||
}
|
||||
|
||||
main();
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
export declare function cloneClaims(payload: object): string;
|
||||
export declare function cloneNested(value: unknown): string;
|
||||
export declare function cloneFails(): string;
|
||||
export declare function listFiles(dir: string): Promise<string[]>;
|
||||
export declare function names(): string[];
|
||||
export declare function matrix(): Promise<number[][]>;
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
"use strict";
|
||||
function cloneClaims(payload) {
|
||||
const copy = structuredClone(payload);
|
||||
copy.sub = "changed";
|
||||
return `${JSON.stringify(copy)} same=${copy === payload} proto=${Object.getPrototypeOf(copy) === Object.prototype}`;
|
||||
}
|
||||
function cloneNested(value) {
|
||||
const copy = structuredClone({ value, when: new Date(0) });
|
||||
return `${JSON.stringify(copy.value)} date=${copy.when instanceof Date}`;
|
||||
}
|
||||
function cloneFails() {
|
||||
try {
|
||||
structuredClone({ f() {} });
|
||||
return "cloned";
|
||||
} catch (e) {
|
||||
return `${e.name}`;
|
||||
}
|
||||
}
|
||||
async function listFiles(dir) {
|
||||
await null;
|
||||
return [`${dir}/a.txt`, `${dir}/b.txt`];
|
||||
}
|
||||
function names() {
|
||||
return ["x", "y", "z"];
|
||||
}
|
||||
async function matrix() {
|
||||
return [[1, 2], [3]];
|
||||
}
|
||||
module.exports = { cloneClaims, cloneNested, cloneFails, listFiles, names, matrix };
|
||||
Generated
Vendored
+1
@@ -0,0 +1 @@
|
||||
{ "name": "data-pkg", "version": "1.0.0", "main": "index.js", "types": "index.d.ts" }
|
||||
@@ -0,0 +1 @@
|
||||
{ "name": "island-clone-and-arrays", "private": true, "type": "module", "scriptc": { "packages": { "data-pkg": "node" } } }
|
||||
@@ -0,0 +1 @@
|
||||
{ "plain": 2000, "san": 30000 }
|
||||
Reference in New Issue
Block a user