Socket
Socket
Sign inDemoInstall

@segment/analytics-node

Package Overview
Dependencies
Maintainers
250
Versions
43
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@segment/analytics-node - npm Package Compare versions

Comparing version 1.0.0-beta.20 to 1.0.0-beta.21

2

dist/cjs/generated/version.js

@@ -5,3 +5,3 @@ "use strict";

// This file is generated.
exports.version = '1.0.0-beta.20';
exports.version = '1.0.0-beta.21';
//# sourceMappingURL=version.js.map

@@ -1,14 +0,11 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Analytics = void 0;
const analytics_core_1 = require("@segment/analytics-core");
const settings_1 = require("./settings");
const version_1 = require("../generated/version");
const segmentio_1 = require("../plugins/segmentio");
const event_factory_1 = require("./event-factory");
const dispatch_emit_1 = require("./dispatch-emit");
const emitter_1 = require("./emitter");
const context_1 = require("./context");
const event_queue_1 = require("./event-queue");
class Analytics extends emitter_1.NodeEmitter {
import { bindAll, pTimeout } from '@segment/analytics-core';
import { validateSettings } from './settings';
import { version } from '../generated/version';
import { createConfiguredNodePlugin } from '../plugins/segmentio';
import { NodeEventFactory } from './event-factory';
import { dispatchAndEmit } from './dispatch-emit';
import { NodeEmitter } from './emitter';
import { Context } from './context';
import { NodeEventQueue } from './event-queue';
export class Analytics extends NodeEmitter {
constructor(settings) {

@@ -18,8 +15,8 @@ super();

this._pendingEvents = 0;
(0, settings_1.validateSettings)(settings);
this._eventFactory = new event_factory_1.NodeEventFactory();
this._queue = new event_queue_1.NodeEventQueue();
validateSettings(settings);
this._eventFactory = new NodeEventFactory();
this._queue = new NodeEventQueue();
const flushInterval = settings.flushInterval ?? 10000;
this._closeAndFlushDefaultTimeout = flushInterval * 1.25; // add arbitrary multiplier in case an event is in a plugin.
const { plugin, publisher } = (0, segmentio_1.createConfiguredNodePlugin)({
const { plugin, publisher } = createConfiguredNodePlugin({
writeKey: settings.writeKey,

@@ -36,6 +33,6 @@ host: settings.host,

this.emit('initialize', settings);
(0, analytics_core_1.bindAll)(this);
bindAll(this);
}
get VERSION() {
return version_1.version;
return version;
}

@@ -58,3 +55,3 @@ /**

});
return timeout ? (0, analytics_core_1.pTimeout)(promise, timeout).catch(() => undefined) : promise;
return timeout ? pTimeout(promise, timeout).catch(() => undefined) : promise;
}

@@ -67,3 +64,3 @@ _dispatch(segmentEvent, callback) {

this._pendingEvents++;
(0, dispatch_emit_1.dispatchAndEmit)(segmentEvent, this._queue, this, callback)
dispatchAndEmit(segmentEvent, this._queue, this, callback)
.catch((ctx) => ctx)

@@ -155,3 +152,3 @@ .finally(() => {

return this._queue.criticalTasks.run(async () => {
const ctx = context_1.Context.system();
const ctx = Context.system();
const registrations = plugins.map((xt) => this._queue.register(ctx, xt, this));

@@ -167,3 +164,3 @@ await Promise.all(registrations);

async deregister(...pluginNames) {
const ctx = context_1.Context.system();
const ctx = Context.system();
const deregistrations = pluginNames.map((pl) => {

@@ -182,3 +179,2 @@ const plugin = this._queue.plugins.find((p) => p.name === pl);

}
exports.Analytics = Analytics;
//# sourceMappingURL=analytics-node.js.map

@@ -1,8 +0,5 @@

"use strict";
// create a derived class since we may want to add node specific things to Context later
Object.defineProperty(exports, "__esModule", { value: true });
exports.Context = void 0;
const analytics_core_1 = require("@segment/analytics-core");
import { CoreContext } from '@segment/analytics-core';
// While this is not a type, it is a definition
class Context extends analytics_core_1.CoreContext {
export class Context extends CoreContext {
static system() {

@@ -12,3 +9,2 @@ return new this({ type: 'track', event: 'system' });

}
exports.Context = Context;
//# sourceMappingURL=context.js.map

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

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.dispatchAndEmit = void 0;
const analytics_core_1 = require("@segment/analytics-core");
const context_1 = require("./context");
import { dispatch } from '@segment/analytics-core';
import { Context } from './context';
const normalizeDispatchCb = (cb) => (ctx) => {

@@ -11,6 +8,6 @@ const failedDelivery = ctx.failedDelivery();

/* Dispatch function, but swallow promise rejections and use event emitter instead */
const dispatchAndEmit = async (event, queue, emitter, callback) => {
export const dispatchAndEmit = async (event, queue, emitter, callback) => {
try {
const context = new context_1.Context(event);
const ctx = await (0, analytics_core_1.dispatch)(context, queue, emitter, {
const context = new Context(event);
const ctx = await dispatch(context, queue, emitter, {
...(callback ? { callback: normalizeDispatchCb(callback) } : {}),

@@ -37,3 +34,2 @@ });

};
exports.dispatchAndEmit = dispatchAndEmit;
//# sourceMappingURL=dispatch-emit.js.map

@@ -1,8 +0,4 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NodeEmitter = void 0;
const analytics_core_1 = require("@segment/analytics-core");
class NodeEmitter extends analytics_core_1.Emitter {
import { Emitter } from '@segment/analytics-core';
export class NodeEmitter extends Emitter {
}
exports.NodeEmitter = NodeEmitter;
//# sourceMappingURL=emitter.js.map

@@ -1,12 +0,8 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NodeEventFactory = void 0;
const analytics_core_1 = require("@segment/analytics-core");
const get_message_id_1 = require("../lib/get-message-id");
class NodeEventFactory extends analytics_core_1.EventFactory {
import { EventFactory } from '@segment/analytics-core';
import { createMessageId } from '../lib/get-message-id';
export class NodeEventFactory extends EventFactory {
constructor() {
super({ createMessageId: get_message_id_1.createMessageId });
super({ createMessageId });
}
}
exports.NodeEventFactory = NodeEventFactory;
//# sourceMappingURL=event-factory.js.map

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

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NodeEventQueue = void 0;
const analytics_core_1 = require("@segment/analytics-core");
class NodePriorityQueue extends analytics_core_1.PriorityQueue {
import { CoreEventQueue, PriorityQueue } from '@segment/analytics-core';
class NodePriorityQueue extends PriorityQueue {
constructor() {

@@ -18,3 +15,3 @@ super(1, []);

}
class NodeEventQueue extends analytics_core_1.CoreEventQueue {
export class NodeEventQueue extends CoreEventQueue {
constructor() {

@@ -24,3 +21,2 @@ super(new NodePriorityQueue());

}
exports.NodeEventQueue = NodeEventQueue;
//# sourceMappingURL=event-queue.js.map

@@ -1,11 +0,7 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateSettings = void 0;
const analytics_core_1 = require("@segment/analytics-core");
const validateSettings = (settings) => {
import { ValidationError } from '@segment/analytics-core';
export const validateSettings = (settings) => {
if (!settings.writeKey) {
throw new analytics_core_1.ValidationError('writeKey', 'writeKey is missing.');
throw new ValidationError('writeKey', 'writeKey is missing.');
}
};
exports.validateSettings = validateSettings;
//# sourceMappingURL=settings.js.map

@@ -1,20 +0,4 @@

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
__exportStar(require("./params"), exports);
__exportStar(require("./segment-event"), exports);
__exportStar(require("./plugin"), exports);
export * from './params';
export * from './segment-event';
export * from './plugin';
//# sourceMappingURL=index.js.map

@@ -1,3 +0,2 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
export {};
//# sourceMappingURL=params.js.map

@@ -1,3 +0,2 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
export {};
//# sourceMappingURL=plugin.js.map

@@ -1,3 +0,2 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
export {};
//# sourceMappingURL=segment-event.js.map

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

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.version = void 0;
// This file is generated.
exports.version = '1.0.0-beta.20';
export const version = '1.0.0-beta.21';
//# sourceMappingURL=version.js.map

@@ -1,11 +0,6 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Context = exports.Analytics = void 0;
var analytics_node_1 = require("./app/analytics-node");
Object.defineProperty(exports, "Analytics", { enumerable: true, get: function () { return analytics_node_1.Analytics; } });
var context_1 = require("./app/context");
Object.defineProperty(exports, "Context", { enumerable: true, get: function () { return context_1.Context; } });
export { Analytics } from './app/analytics-node';
export { Context } from './app/context';
// export Analytics as both a named export and a default export (for backwards-compat. reasons)
const analytics_node_2 = require("./app/analytics-node");
exports.default = analytics_node_2.Analytics;
import { Analytics } from './app/analytics-node';
export default Analytics;
//# sourceMappingURL=index.js.map

@@ -1,9 +0,6 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.abortSignalAfterTimeout = void 0;
/**
* use non-native event emitter for the benefit of non-node runtimes like CF workers.
*/
const analytics_core_1 = require("@segment/analytics-core");
const env_1 = require("./env");
import { Emitter } from '@segment/analytics-core';
import { detectRuntime } from './env';
/**

@@ -16,3 +13,3 @@ * adapted from: https://www.npmjs.com/package/node-abort-controller

this.aborted = false;
this.eventEmitter = new analytics_core_1.Emitter();
this.eventEmitter = new Emitter();
}

@@ -65,4 +62,4 @@ toString() {

*/
const abortSignalAfterTimeout = (timeoutMs) => {
if ((0, env_1.detectRuntime)() === 'cloudflare-worker') {
export const abortSignalAfterTimeout = (timeoutMs) => {
if (detectRuntime() === 'cloudflare-worker') {
return []; // TODO: this is broken in cloudflare workers, otherwise results in "A hanging Promise was canceled..." error.

@@ -78,3 +75,2 @@ }

};
exports.abortSignalAfterTimeout = abortSignalAfterTimeout;
//# sourceMappingURL=abort.js.map

@@ -1,8 +0,5 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.b64encode = void 0;
/**
* Base64 encoder that works in browser, worker, node runtimes.
*/
const b64encode = (str) => {
export const b64encode = (str) => {
if (

@@ -18,3 +15,2 @@ // in node env

};
exports.b64encode = b64encode;
//# sourceMappingURL=base-64-encode.js.map

@@ -1,4 +0,1 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.tryCreateFormattedUrl = void 0;
const stripTrailingSlash = (str) => str.replace(/\/$/, '');

@@ -11,6 +8,5 @@ /**

*/
const tryCreateFormattedUrl = (host, path) => {
export const tryCreateFormattedUrl = (host, path) => {
return stripTrailingSlash(new URL(path || '', host).href);
};
exports.tryCreateFormattedUrl = tryCreateFormattedUrl;
//# sourceMappingURL=create-url.js.map

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

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.detectRuntime = void 0;
const detectRuntime = () => {
export const detectRuntime = () => {
if (typeof process === 'object' && process && process.env) {

@@ -24,3 +21,2 @@ return 'node';

};
exports.detectRuntime = detectRuntime;
//# sourceMappingURL=env.js.map

@@ -1,8 +0,5 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractPromiseParts = void 0;
/**
* Returns a promise and its associated `resolve` and `reject` methods.
*/
function extractPromiseParts() {
export function extractPromiseParts() {
let resolver;

@@ -20,3 +17,2 @@ let rejecter;

}
exports.extractPromiseParts = extractPromiseParts;
//# sourceMappingURL=extract-promise-parts.js.map

@@ -1,9 +0,3 @@

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetch = void 0;
const node_fetch_1 = __importDefault(require("node-fetch"));
exports.fetch = globalThis.fetch || node_fetch_1.default;
import { default as _fetch } from 'node-fetch';
export const fetch = globalThis.fetch || _fetch;
//# sourceMappingURL=fetch.js.map

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

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createMessageId = void 0;
const secure_1 = require("@lukeed/uuid/secure");
import { v4 } from '@lukeed/uuid/secure';
/**

@@ -10,6 +7,5 @@ * get a unique messageId with a very low chance of collisions

*/
const createMessageId = () => {
return `node-next-${Date.now()}-${(0, secure_1.v4)()}`;
export const createMessageId = () => {
return `node-next-${Date.now()}-${v4()}`;
};
exports.createMessageId = createMessageId;
//# sourceMappingURL=get-message-id.js.map

@@ -1,10 +0,7 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContextBatch = void 0;
const uuid_1 = require("@lukeed/uuid");
import { v4 as uuid } from '@lukeed/uuid';
const MAX_EVENT_SIZE_IN_KB = 32;
const MAX_BATCH_SIZE_IN_KB = 480; // (500 KB is the limit, leaving some padding)
class ContextBatch {
export class ContextBatch {
constructor(maxEventCount) {
this.id = (0, uuid_1.v4)();
this.id = uuid();
this.items = [];

@@ -54,3 +51,2 @@ this.sizeInBytes = 0;

}
exports.ContextBatch = ContextBatch;
//# sourceMappingURL=context-batch.js.map

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

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createConfiguredNodePlugin = exports.createNodePlugin = void 0;
const publisher_1 = require("./publisher");
const version_1 = require("../../generated/version");
const env_1 = require("../../lib/env");
import { Publisher } from './publisher';
import { version } from '../../generated/version';
import { detectRuntime } from '../../lib/env';
function normalizeEvent(ctx) {
ctx.updateEvent('context.library.name', '@segment/analytics-node');
ctx.updateEvent('context.library.version', version_1.version);
const runtime = (0, env_1.detectRuntime)();
ctx.updateEvent('context.library.version', version);
const runtime = detectRuntime();
if (runtime === 'node') {

@@ -16,3 +13,3 @@ ctx.updateEvent('_metadata.nodeVersion', process.versions.node);

}
function createNodePlugin(publisher) {
export function createNodePlugin(publisher) {
function action(ctx) {

@@ -36,5 +33,4 @@ normalizeEvent(ctx);

}
exports.createNodePlugin = createNodePlugin;
const createConfiguredNodePlugin = (props, emitter) => {
const publisher = new publisher_1.Publisher(props, emitter);
export const createConfiguredNodePlugin = (props, emitter) => {
const publisher = new Publisher(props, emitter);
return {

@@ -45,3 +41,2 @@ publisher: publisher,

};
exports.createConfiguredNodePlugin = createConfiguredNodePlugin;
//# sourceMappingURL=index.js.map

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

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Publisher = void 0;
const analytics_core_1 = require("@segment/analytics-core");
const abort_1 = require("../../lib/abort");
const create_url_1 = require("../../lib/create-url");
const extract_promise_parts_1 = require("../../lib/extract-promise-parts");
const fetch_1 = require("../../lib/fetch");
const context_batch_1 = require("./context-batch");
const base_64_encode_1 = require("../../lib/base-64-encode");
import { backoff } from '@segment/analytics-core';
import { abortSignalAfterTimeout } from '../../lib/abort';
import { tryCreateFormattedUrl } from '../../lib/create-url';
import { extractPromiseParts } from '../../lib/extract-promise-parts';
import { fetch } from '../../lib/fetch';
import { ContextBatch } from './context-batch';
import { b64encode } from '../../lib/base-64-encode';
function sleep(timeoutInMs) {

@@ -18,3 +15,3 @@ return new Promise((resolve) => setTimeout(resolve, timeoutInMs));

*/
class Publisher {
export class Publisher {
constructor({ host, path, maxRetries, maxEventsInBatch, flushInterval, writeKey, httpRequestTimeout, }, emitter) {

@@ -25,4 +22,4 @@ this._emitter = emitter;

this._flushInterval = flushInterval;
this._auth = (0, base_64_encode_1.b64encode)(`${writeKey}:`);
this._url = (0, create_url_1.tryCreateFormattedUrl)(host ?? 'https://api.segment.io', path ?? '/v1/batch');
this._auth = b64encode(`${writeKey}:`);
this._url = tryCreateFormattedUrl(host ?? 'https://api.segment.io', path ?? '/v1/batch');
this._httpRequestTimeout = httpRequestTimeout ?? 10000;

@@ -32,3 +29,3 @@ }

this.pendingFlushTimeout && clearTimeout(this.pendingFlushTimeout);
const batch = new context_batch_1.ContextBatch(this._maxEventsInBatch);
const batch = new ContextBatch(this._maxEventsInBatch);
this._batch = batch;

@@ -74,3 +71,3 @@ this.pendingFlushTimeout = setTimeout(() => {

const batch = this._batch ?? this.createBatch();
const { promise: ctxPromise, resolve } = (0, extract_promise_parts_1.extractPromiseParts)();
const { promise: ctxPromise, resolve } = extractPromiseParts();
const pendingItem = {

@@ -135,3 +132,3 @@ context: ctx,

let failureReason;
const [signal, timeoutId] = (0, abort_1.abortSignalAfterTimeout)(this._httpRequestTimeout);
const [signal, timeoutId] = abortSignalAfterTimeout(this._httpRequestTimeout);
try {

@@ -154,3 +151,3 @@ const requestInit = {

});
const response = await (0, fetch_1.fetch)(this._url, requestInit);
const response = await fetch(this._url, requestInit);
clearTimeout(timeoutId);

@@ -183,3 +180,3 @@ if (response.ok) {

// Retry after attempt-based backoff.
await sleep((0, analytics_core_1.backoff)({
await sleep(backoff({
attempt: currentAttempt,

@@ -192,3 +189,2 @@ minTimeout: 25,

}
exports.Publisher = Publisher;
function resolveFailedBatch(batch, reason) {

@@ -195,0 +191,0 @@ batch.getContexts().forEach((ctx) => ctx.setFailedDelivery({ reason }));

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

export declare const version = "1.0.0-beta.20";
export declare const version = "1.0.0-beta.21";
//# sourceMappingURL=version.d.ts.map
{
"name": "@segment/analytics-node",
"version": "1.0.0-beta.20",
"version": "1.0.0-beta.21",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/types/index.d.ts",
"exports": {
"require": "./dist/cjs/index.js",
"import": "./dist/esm/index.js",
"types": "./dist/types/index.d.ts"
},
"files": [

@@ -13,0 +8,0 @@ "dist/",

// This file is generated.
export const version = '1.0.0-beta.20'
export const version = '1.0.0-beta.21'

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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