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

@internetarchive/lazy-loader-service

Package Overview
Dependencies
Maintainers
13
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@internetarchive/lazy-loader-service - npm Package Compare versions

Comparing version 0.2.0-alpha.3 to 0.2.0-alpha.4

29

dist/src/lazy-loader-service.js
import { __awaiter } from "tslib";
import { createNanoEvents } from 'nanoevents';
import { promisedSleep } from './promised-sleep';
var ScriptTagAttributes;
(function (ScriptTagAttributes) {
ScriptTagAttributes["retryNumber"] = "retryNumber";
ScriptTagAttributes["owner"] = "owner";
ScriptTagAttributes["dynamicImportLoaded"] = "dynamicImportLoaded";
ScriptTagAttributes["hasBeenRetried"] = "hasBeenRetried";
})(ScriptTagAttributes || (ScriptTagAttributes = {}));
const scriptOwnerName = 'lazyLoaderService';
export class LazyLoaderService {

@@ -54,6 +62,6 @@ /**

const retryNumber = (_a = options.retryNumber) !== null && _a !== void 0 ? _a : 0;
const scriptSelector = `script[src='${options.src}'][async][retryCount='${retryNumber}']`;
const scriptSelector = `script[src='${options.src}'][async][${ScriptTagAttributes.owner}='${scriptOwnerName}'][${ScriptTagAttributes.retryNumber}='${retryNumber}']`;
let script = this.container.querySelector(scriptSelector);
if (!script) {
script = this.getScriptTag(options);
script = this.getScriptTag(Object.assign(Object.assign({}, options), { retryNumber }));
this.container.appendChild(script);

@@ -63,3 +71,3 @@ }

// script has already been loaded, just resolve
if (script.getAttribute('dynamicImportLoaded')) {
if (script.getAttribute(ScriptTagAttributes.dynamicImportLoaded)) {
resolve();

@@ -76,3 +84,3 @@ return;

originalOnLoad === null || originalOnLoad === void 0 ? void 0 : originalOnLoad(event);
script.setAttribute('dynamicImportLoaded', 'true');
script.setAttribute(ScriptTagAttributes.dynamicImportLoaded, 'true');
resolve();

@@ -82,5 +90,5 @@ };

script.onerror = (error) => __awaiter(this, void 0, void 0, function* () {
const hasBeenRetried = script.getAttribute('hasBeenRetried');
const hasBeenRetried = script.getAttribute(ScriptTagAttributes.hasBeenRetried);
if (retryNumber < this.retryCount && !hasBeenRetried) {
script.setAttribute('hasBeenRetried', 'true');
script.setAttribute(ScriptTagAttributes.hasBeenRetried, 'true');
yield promisedSleep(this.retryInterval * 1000);

@@ -112,10 +120,11 @@ const newRetryNumber = retryNumber + 1;

getScriptTag(options) {
var _a, _b;
var _a;
const fixedSrc = options.src.replace("'", '"');
const script = document.createElement('script');
const retryCount = (_a = options.retryCount) !== null && _a !== void 0 ? _a : 0;
const retryNumber = options.retryNumber;
script.setAttribute(ScriptTagAttributes.owner, scriptOwnerName);
script.setAttribute('src', fixedSrc);
script.setAttribute('retryCount', retryCount.toString());
script.setAttribute(ScriptTagAttributes.retryNumber, retryNumber.toString());
script.async = true;
const attributes = (_b = options.attributes) !== null && _b !== void 0 ? _b : {};
const attributes = (_a = options.attributes) !== null && _a !== void 0 ? _a : {};
Object.keys(attributes).forEach(key => {

@@ -122,0 +131,0 @@ script.setAttribute(key, attributes[key]);

{
"name": "@internetarchive/lazy-loader-service",
"version": "0.2.0-alpha.3",
"version": "0.2.0-alpha.4",
"description": "A small library to lazy load javascript with a Promise",

@@ -5,0 +5,0 @@ "license": "AGPL-3.0-only",

@@ -9,2 +9,17 @@ import { createNanoEvents, Unsubscribe } from 'nanoevents';

/**
* Attributes used to identify different script states
*/
enum ScriptTagAttributes {
retryNumber = 'retryNumber',
owner = 'owner',
dynamicImportLoaded = 'dynamicImportLoaded',
hasBeenRetried = 'hasBeenRetried',
}
/**
* Used to identify scripts loaded by the LazyLoaderService
*/
const scriptOwnerName = 'lazyLoaderService';
export interface LazyLoaderServiceOptions {

@@ -103,3 +118,3 @@ /**

const retryNumber = options.retryNumber ?? 0;
const scriptSelector = `script[src='${options.src}'][async][retryCount='${retryNumber}']`;
const scriptSelector = `script[src='${options.src}'][async][${ScriptTagAttributes.owner}='${scriptOwnerName}'][${ScriptTagAttributes.retryNumber}='${retryNumber}']`;
let script = this.container.querySelector(

@@ -109,3 +124,3 @@ scriptSelector

if (!script) {
script = this.getScriptTag(options);
script = this.getScriptTag({ ...options, retryNumber });
this.container.appendChild(script);

@@ -116,3 +131,3 @@ }

// script has already been loaded, just resolve
if (script.getAttribute('dynamicImportLoaded')) {
if (script.getAttribute(ScriptTagAttributes.dynamicImportLoaded)) {
resolve();

@@ -134,3 +149,3 @@ return;

originalOnLoad?.(event);
script.setAttribute('dynamicImportLoaded', 'true');
script.setAttribute(ScriptTagAttributes.dynamicImportLoaded, 'true');
resolve();

@@ -143,5 +158,7 @@ };

script.onerror = async (error): Promise<void> => {
const hasBeenRetried = script.getAttribute('hasBeenRetried');
const hasBeenRetried = script.getAttribute(
ScriptTagAttributes.hasBeenRetried
);
if (retryNumber < this.retryCount && !hasBeenRetried) {
script.setAttribute('hasBeenRetried', 'true');
script.setAttribute(ScriptTagAttributes.hasBeenRetried, 'true');
await promisedSleep(this.retryInterval * 1000);

@@ -177,3 +194,3 @@ const newRetryNumber = retryNumber + 1;

src: string;
retryCount?: number;
retryNumber: number;
bundleType?: BundleType;

@@ -184,5 +201,9 @@ attributes?: Record<string, string>;

const script = document.createElement('script') as HTMLScriptElement;
const retryCount = options.retryCount ?? 0;
const retryNumber = options.retryNumber;
script.setAttribute(ScriptTagAttributes.owner, scriptOwnerName);
script.setAttribute('src', fixedSrc);
script.setAttribute('retryCount', retryCount.toString());
script.setAttribute(
ScriptTagAttributes.retryNumber,
retryNumber.toString()
);
script.async = true;

@@ -189,0 +210,0 @@

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