Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

turbo-stream

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

turbo-stream - npm Package Compare versions

Comparing version 1.1.1 to 1.2.0

dist/turbo-stream.mjs

40

dist/flatten.js

@@ -1,3 +0,6 @@

import { HOLE, NAN, NEGATIVE_INFINITY, NEGATIVE_ZERO, POSITIVE_INFINITY, UNDEFINED, TYPE_BIGINT, TYPE_DATE, TYPE_ERROR, TYPE_MAP, TYPE_NULL_OBJECT, TYPE_PROMISE, TYPE_REGEXP, TYPE_SET, TYPE_SYMBOL, TYPE_URL, } from "./utils.js";
export function flatten(input) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.flatten = void 0;
const utils_js_1 = require("./utils.js");
function flatten(input) {
const { indices } = this;

@@ -8,11 +11,11 @@ const existing = indices.get(input);

if (input === undefined)
return UNDEFINED;
return utils_js_1.UNDEFINED;
if (Number.isNaN(input))
return NAN;
return utils_js_1.NAN;
if (input === Infinity)
return POSITIVE_INFINITY;
return utils_js_1.POSITIVE_INFINITY;
if (input === -Infinity)
return NEGATIVE_INFINITY;
return utils_js_1.NEGATIVE_INFINITY;
if (input === 0 && 1 / input < 0)
return NEGATIVE_ZERO;
return utils_js_1.NEGATIVE_ZERO;
const index = this.index++;

@@ -23,2 +26,3 @@ indices.set(input, index);

}
exports.flatten = flatten;
function stringify(input, index) {

@@ -37,3 +41,3 @@ const { deferred, plugins } = this;

case "bigint":
str[index] = `["${TYPE_BIGINT}","${input}"]`;
str[index] = `["${utils_js_1.TYPE_BIGINT}","${input}"]`;
break;

@@ -44,3 +48,3 @@ case "symbol":

throw new Error("Cannot encode symbol unless created with Symbol.for()");
str[index] = `["${TYPE_SYMBOL}",${JSON.stringify(keyFor)}]`;
str[index] = `["${utils_js_1.TYPE_SYMBOL}",${JSON.stringify(keyFor)}]`;
break;

@@ -76,16 +80,16 @@ case "object":

(i ? "," : "") +
(i in input ? flatten.call(this, input[i]) : HOLE);
(i in input ? flatten.call(this, input[i]) : utils_js_1.HOLE);
str[index] = result + "]";
}
else if (input instanceof Date) {
str[index] = `["${TYPE_DATE}",${input.getTime()}]`;
str[index] = `["${utils_js_1.TYPE_DATE}",${input.getTime()}]`;
}
else if (input instanceof URL) {
str[index] = `["${TYPE_URL}",${JSON.stringify(input.href)}]`;
str[index] = `["${utils_js_1.TYPE_URL}",${JSON.stringify(input.href)}]`;
}
else if (input instanceof RegExp) {
str[index] = `["${TYPE_REGEXP}",${JSON.stringify(input.source)},${JSON.stringify(input.flags)}]`;
str[index] = `["${utils_js_1.TYPE_REGEXP}",${JSON.stringify(input.source)},${JSON.stringify(input.flags)}]`;
}
else if (input instanceof Set) {
str[index] = `["${TYPE_SET}",${[...input]
str[index] = `["${utils_js_1.TYPE_SET}",${[...input]
.map((val) => flatten.call(this, val))

@@ -95,3 +99,3 @@ .join(",")}]`;

else if (input instanceof Map) {
str[index] = `["${TYPE_MAP}",${[...input]
str[index] = `["${utils_js_1.TYPE_MAP}",${[...input]
.flatMap(([k, v]) => [flatten.call(this, k), flatten.call(this, v)])

@@ -101,7 +105,7 @@ .join(",")}]`;

else if (input instanceof Promise) {
str[index] = `["${TYPE_PROMISE}",${index}]`;
str[index] = `["${utils_js_1.TYPE_PROMISE}",${index}]`;
deferred[index] = input;
}
else if (input instanceof Error) {
str[index] = `["${TYPE_ERROR}",${JSON.stringify(input.message)}`;
str[index] = `["${utils_js_1.TYPE_ERROR}",${JSON.stringify(input.message)}`;
if (input.name !== "Error") {

@@ -113,3 +117,3 @@ str[index] += `,${JSON.stringify(input.name)}`;

else if (Object.getPrototypeOf(input) === null) {
str[index] = `["${TYPE_NULL_OBJECT}",{${partsForObj(input)}}]`;
str[index] = `["${utils_js_1.TYPE_NULL_OBJECT}",{${partsForObj(input)}}]`;
}

@@ -116,0 +120,0 @@ else if (isPlainObject(input)) {

@@ -1,8 +0,11 @@

import { flatten } from "./flatten.js";
import { unflatten } from "./unflatten.js";
import { Deferred, TYPE_ERROR, TYPE_PROMISE, createLineSplittingTransform, } from "./utils.js";
export async function decode(readable, plugins) {
const done = new Deferred();
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.encode = exports.decode = void 0;
const flatten_js_1 = require("./flatten.js");
const unflatten_js_1 = require("./unflatten.js");
const utils_js_1 = require("./utils.js");
async function decode(readable, plugins) {
const done = new utils_js_1.Deferred();
const reader = readable
.pipeThrough(createLineSplittingTransform())
.pipeThrough((0, utils_js_1.createLineSplittingTransform)())
.getReader();

@@ -36,2 +39,3 @@ const decoder = {

}
exports.decode = decode;
async function decodeInitial(reader) {

@@ -51,3 +55,3 @@ const read = await reader.read();

done: read.done,
value: unflatten.call(this, line),
value: unflatten_js_1.unflatten.call(this, line),
};

@@ -62,3 +66,3 @@ }

switch (line[0]) {
case TYPE_PROMISE: {
case utils_js_1.TYPE_PROMISE: {
const colonIndex = line.indexOf(":");

@@ -78,7 +82,7 @@ const deferredId = Number(line.slice(1, colonIndex));

}
const value = unflatten.call(this, jsonLine);
const value = unflatten_js_1.unflatten.call(this, jsonLine);
deferred.resolve(value);
break;
}
case TYPE_ERROR: {
case utils_js_1.TYPE_ERROR: {
const colonIndex = line.indexOf(":");

@@ -98,3 +102,3 @@ const deferredId = Number(line.slice(1, colonIndex));

}
const value = unflatten.call(this, jsonLine);
const value = unflatten_js_1.unflatten.call(this, jsonLine);
deferred.reject(value);

@@ -109,3 +113,3 @@ break;

}
export function encode(input, plugins) {
function encode(input, plugins) {
const encoder = {

@@ -122,3 +126,3 @@ deferred: {},

async start(controller) {
const id = flatten.call(encoder, input);
const id = flatten_js_1.flatten.call(encoder, input);
if (id < 0) {

@@ -138,5 +142,5 @@ controller.enqueue(textEncoder.encode(`${id}\n`));

.then((resolved) => {
const id = flatten.call(encoder, resolved);
const id = flatten_js_1.flatten.call(encoder, resolved);
if (id < 0) {
controller.enqueue(textEncoder.encode(`${TYPE_PROMISE}${deferredId}:${id}\n`));
controller.enqueue(textEncoder.encode(`${utils_js_1.TYPE_PROMISE}${deferredId}:${id}\n`));
}

@@ -147,3 +151,3 @@ else {

.join(",");
controller.enqueue(textEncoder.encode(`${TYPE_PROMISE}${deferredId}:[${values}]\n`));
controller.enqueue(textEncoder.encode(`${utils_js_1.TYPE_PROMISE}${deferredId}:[${values}]\n`));
lastSentIndex = encoder.stringified.length - 1;

@@ -157,5 +161,5 @@ }

}
const id = flatten.call(encoder, reason);
const id = flatten_js_1.flatten.call(encoder, reason);
if (id < 0) {
controller.enqueue(textEncoder.encode(`${TYPE_ERROR}${deferredId}:${id}\n`));
controller.enqueue(textEncoder.encode(`${utils_js_1.TYPE_ERROR}${deferredId}:${id}\n`));
}

@@ -166,3 +170,3 @@ else {

.join(",");
controller.enqueue(textEncoder.encode(`${TYPE_ERROR}${deferredId}:[${values}]\n`));
controller.enqueue(textEncoder.encode(`${utils_js_1.TYPE_ERROR}${deferredId}:[${values}]\n`));
lastSentIndex = encoder.stringified.length - 1;

@@ -183,1 +187,2 @@ }

}
exports.encode = encode;

@@ -1,2 +0,5 @@

import { Deferred, HOLE, NAN, NEGATIVE_INFINITY, NEGATIVE_ZERO, POSITIVE_INFINITY, UNDEFINED, TYPE_BIGINT, TYPE_DATE, TYPE_ERROR, TYPE_MAP, TYPE_NULL_OBJECT, TYPE_PROMISE, TYPE_REGEXP, TYPE_SET, TYPE_SYMBOL, TYPE_URL, } from "./utils.js";
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.unflatten = void 0;
const utils_js_1 = require("./utils.js");
const globalObj = (typeof window !== "undefined"

@@ -7,3 +10,3 @@ ? window

: undefined);
export function unflatten(parsed) {
function unflatten(parsed) {
const { hydrated, values } = this;

@@ -19,14 +22,15 @@ if (typeof parsed === "number")

}
exports.unflatten = unflatten;
function hydrate(index) {
const { hydrated, values, deferred, plugins } = this;
switch (index) {
case UNDEFINED:
case utils_js_1.UNDEFINED:
return;
case NAN:
case utils_js_1.NAN:
return NaN;
case POSITIVE_INFINITY:
case utils_js_1.POSITIVE_INFINITY:
return Infinity;
case NEGATIVE_INFINITY:
case utils_js_1.NEGATIVE_INFINITY:
return -Infinity;
case NEGATIVE_ZERO:
case utils_js_1.NEGATIVE_ZERO:
return -0;

@@ -51,13 +55,13 @@ }

switch (type) {
case TYPE_DATE:
case utils_js_1.TYPE_DATE:
return (hydrated[index] = new Date(b));
case TYPE_URL:
case utils_js_1.TYPE_URL:
return (hydrated[index] = new URL(b));
case TYPE_BIGINT:
case utils_js_1.TYPE_BIGINT:
return (hydrated[index] = BigInt(b));
case TYPE_REGEXP:
case utils_js_1.TYPE_REGEXP:
return (hydrated[index] = new RegExp(b, c));
case TYPE_SYMBOL:
case utils_js_1.TYPE_SYMBOL:
return (hydrated[index] = Symbol.for(b));
case TYPE_SET:
case utils_js_1.TYPE_SET:
const set = new Set();

@@ -68,3 +72,3 @@ hydrated[index] = set;

return set;
case TYPE_MAP:
case utils_js_1.TYPE_MAP:
const map = new Map();

@@ -76,3 +80,3 @@ hydrated[index] = map;

return map;
case TYPE_NULL_OBJECT:
case utils_js_1.TYPE_NULL_OBJECT:
const obj = Object.create(null);

@@ -83,3 +87,3 @@ hydrated[index] = obj;

return obj;
case TYPE_PROMISE:
case utils_js_1.TYPE_PROMISE:
if (hydrated[b]) {

@@ -89,7 +93,7 @@ return (hydrated[index] = hydrated[b]);

else {
const d = new Deferred();
const d = new utils_js_1.Deferred();
deferred[b] = d;
return (hydrated[index] = d.promise);
}
case TYPE_ERROR:
case utils_js_1.TYPE_ERROR:
const [, message, errorType] = value;

@@ -110,3 +114,3 @@ let error = errorType && globalObj && globalObj[errorType]

const n = value[i];
if (n !== HOLE)
if (n !== utils_js_1.HOLE)
array[i] = hydrate.call(this, n);

@@ -113,0 +117,0 @@ }

@@ -1,18 +0,21 @@

export const HOLE = -1;
export const NAN = -2;
export const NEGATIVE_INFINITY = -4;
export const NEGATIVE_ZERO = -5;
export const POSITIVE_INFINITY = -3;
export const UNDEFINED = -1;
export const TYPE_BIGINT = "B";
export const TYPE_DATE = "D";
export const TYPE_ERROR = "E";
export const TYPE_MAP = "M";
export const TYPE_NULL_OBJECT = "N";
export const TYPE_PROMISE = "P";
export const TYPE_REGEXP = "R";
export const TYPE_SET = "S";
export const TYPE_SYMBOL = "Y";
export const TYPE_URL = "U";
export class Deferred {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createLineSplittingTransform = exports.Deferred = exports.TYPE_URL = exports.TYPE_SYMBOL = exports.TYPE_SET = exports.TYPE_REGEXP = exports.TYPE_PROMISE = exports.TYPE_NULL_OBJECT = exports.TYPE_MAP = exports.TYPE_ERROR = exports.TYPE_DATE = exports.TYPE_BIGINT = exports.UNDEFINED = exports.POSITIVE_INFINITY = exports.NEGATIVE_ZERO = exports.NEGATIVE_INFINITY = exports.NAN = exports.HOLE = void 0;
exports.HOLE = -1;
exports.NAN = -2;
exports.NEGATIVE_INFINITY = -4;
exports.NEGATIVE_ZERO = -5;
exports.POSITIVE_INFINITY = -3;
exports.UNDEFINED = -1;
exports.TYPE_BIGINT = "B";
exports.TYPE_DATE = "D";
exports.TYPE_ERROR = "E";
exports.TYPE_MAP = "M";
exports.TYPE_NULL_OBJECT = "N";
exports.TYPE_PROMISE = "P";
exports.TYPE_REGEXP = "R";
exports.TYPE_SET = "S";
exports.TYPE_SYMBOL = "Y";
exports.TYPE_URL = "U";
class Deferred {
promise;

@@ -28,3 +31,4 @@ resolve;

}
export function createLineSplittingTransform() {
exports.Deferred = Deferred;
function createLineSplittingTransform() {
let decoder = new TextDecoder();

@@ -50,1 +54,2 @@ let leftover = "";

}
exports.createLineSplittingTransform = createLineSplittingTransform;
{
"name": "turbo-stream",
"version": "1.1.1",
"version": "1.2.0",
"description": "A streaming data transport format that aims to support built-in features such as Promises, Dates, RegExps, Maps, Sets and more.",
"type": "module",
"files": [

@@ -15,2 +14,4 @@ "dist",

"types": "./dist/turbo-stream.d.ts",
"import": "./dist/turbo-stream.mjs",
"require": "./dist/turbo-stream.js",
"default": "./dist/turbo-stream.js"

@@ -21,6 +22,8 @@ },

"scripts": {
"build": "tsc --outDir dist --project tsconfig.lib.json",
"build": "pnpm build:esm && pnpm build:cjs",
"build:cjs": "tsc --outDir dist --project tsconfig.lib.json",
"build:esm": "esbuild --bundle --platform=node --target=node16 --format=esm --outfile=dist/turbo-stream.mjs src/turbo-stream.ts",
"test": "node --no-warnings --loader tsm --enable-source-maps --test-reporter tap --test src/*.spec.ts",
"test-typecheck": "tsc --noEmit --project tsconfig.spec.json",
"prepublish": "attw $(npm pack) --ignore-rules cjs-resolves-to-esm"
"prepublish": "attw $(npm pack) --ignore-rules false-cjs"
},

@@ -44,2 +47,3 @@ "keywords": [

"@types/node": "^20.8.7",
"esbuild": "^0.19.5",
"expect": "^29.7.0",

@@ -46,0 +50,0 @@ "tsm": "^2.3.0",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc