Skip to content

Commit feabe44

Browse files
committed
Bundle the eventsource-parser module
Rather than including an external dependency we choose to bundle the dependency within the `vendor` directory.
1 parent 4ff434c commit feabe44

File tree

5 files changed

+209
-15
lines changed

5 files changed

+209
-15
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -811,3 +811,11 @@ You can call this method directly to make other requests to the API.
811811
## TypeScript
812812

813813
The `Replicate` constructor and all `replicate.*` methods are fully typed.
814+
815+
## Vendored Dependencies
816+
817+
We have a few dependencies that have been bundled into the vendor directory rather than adding external npm dependencies.
818+
819+
These have been generated using bundlejs.com and copied into the appropriate directory along with the license and repository information.
820+
821+
* [eventsource-parser/stream](https://bundlejs.com/bundle?q=eventsource-parser%401.1.2%2Fstream&config=%7B%22esbuild%22%3A%7B%22format%22%3A%22cjs%22%2C%22minify%22%3Afalse%2C%22platform%22%3A%22neutral%22%7D%7D)

lib/stream.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Attempt to use readable-stream if available, attempt to use the built-in stream module.
22

33
const ApiError = require("./error");
4-
const { EventSourceParserStream } = require("eventsource-parser/stream");
4+
const {
5+
EventSourceParserStream,
6+
} = require("../vendor/eventsource-parser/stream");
57

68
/**
79
* A server-sent event.

package-lock.json

-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

-3
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,5 @@
4545
"publint": "^0.2.7",
4646
"ts-jest": "^29.1.0",
4747
"typescript": "^5.0.2"
48-
},
49-
"dependencies": {
50-
"eventsource-parser": "^1.1.2"
5148
}
5249
}

vendor/eventsource-parser/stream.cjs

+198
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
// Source: https://github.com/rexxars/eventsource-parser/tree/v1.1.2
2+
//
3+
// MIT License
4+
//
5+
// Copyright (c) 2024 Espen Hovlandsdal <[email protected]>
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be included in all
15+
// copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
// SOFTWARE.
24+
var __defProp = Object.defineProperty;
25+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
26+
var __getOwnPropNames = Object.getOwnPropertyNames;
27+
var __hasOwnProp = Object.prototype.hasOwnProperty;
28+
var __export = (target, all) => {
29+
for (var name in all)
30+
__defProp(target, name, { get: all[name], enumerable: true });
31+
};
32+
var __copyProps = (to, from, except, desc) => {
33+
if ((from && typeof from === "object") || typeof from === "function") {
34+
for (let key of __getOwnPropNames(from))
35+
if (!__hasOwnProp.call(to, key) && key !== except)
36+
__defProp(to, key, {
37+
get: () => from[key],
38+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable,
39+
});
40+
}
41+
return to;
42+
};
43+
var __toCommonJS = (mod) =>
44+
__copyProps(__defProp({}, "__esModule", { value: true }), mod);
45+
46+
// /input.ts
47+
var input_exports = {};
48+
__export(input_exports, {
49+
EventSourceParserStream: () => EventSourceParserStream,
50+
});
51+
module.exports = __toCommonJS(input_exports);
52+
53+
// http-url:https://unpkg.com/[email protected]/dist/index.js
54+
function createParser(onParse) {
55+
let isFirstChunk;
56+
let buffer;
57+
let startingPosition;
58+
let startingFieldLength;
59+
let eventId;
60+
let eventName;
61+
let data;
62+
reset();
63+
return {
64+
feed,
65+
reset,
66+
};
67+
function reset() {
68+
isFirstChunk = true;
69+
buffer = "";
70+
startingPosition = 0;
71+
startingFieldLength = -1;
72+
eventId = void 0;
73+
eventName = void 0;
74+
data = "";
75+
}
76+
function feed(chunk) {
77+
buffer = buffer ? buffer + chunk : chunk;
78+
if (isFirstChunk && hasBom(buffer)) {
79+
buffer = buffer.slice(BOM.length);
80+
}
81+
isFirstChunk = false;
82+
const length = buffer.length;
83+
let position = 0;
84+
let discardTrailingNewline = false;
85+
while (position < length) {
86+
if (discardTrailingNewline) {
87+
if (buffer[position] === "\n") {
88+
++position;
89+
}
90+
discardTrailingNewline = false;
91+
}
92+
let lineLength = -1;
93+
let fieldLength = startingFieldLength;
94+
let character;
95+
for (
96+
let index = startingPosition;
97+
lineLength < 0 && index < length;
98+
++index
99+
) {
100+
character = buffer[index];
101+
if (character === ":" && fieldLength < 0) {
102+
fieldLength = index - position;
103+
} else if (character === "\r") {
104+
discardTrailingNewline = true;
105+
lineLength = index - position;
106+
} else if (character === "\n") {
107+
lineLength = index - position;
108+
}
109+
}
110+
if (lineLength < 0) {
111+
startingPosition = length - position;
112+
startingFieldLength = fieldLength;
113+
break;
114+
} else {
115+
startingPosition = 0;
116+
startingFieldLength = -1;
117+
}
118+
parseEventStreamLine(buffer, position, fieldLength, lineLength);
119+
position += lineLength + 1;
120+
}
121+
if (position === length) {
122+
buffer = "";
123+
} else if (position > 0) {
124+
buffer = buffer.slice(position);
125+
}
126+
}
127+
function parseEventStreamLine(lineBuffer, index, fieldLength, lineLength) {
128+
if (lineLength === 0) {
129+
if (data.length > 0) {
130+
onParse({
131+
type: "event",
132+
id: eventId,
133+
event: eventName || void 0,
134+
data: data.slice(0, -1),
135+
// remove trailing newline
136+
});
137+
data = "";
138+
eventId = void 0;
139+
}
140+
eventName = void 0;
141+
return;
142+
}
143+
const noValue = fieldLength < 0;
144+
const field = lineBuffer.slice(
145+
index,
146+
index + (noValue ? lineLength : fieldLength)
147+
);
148+
let step = 0;
149+
if (noValue) {
150+
step = lineLength;
151+
} else if (lineBuffer[index + fieldLength + 1] === " ") {
152+
step = fieldLength + 2;
153+
} else {
154+
step = fieldLength + 1;
155+
}
156+
const position = index + step;
157+
const valueLength = lineLength - step;
158+
const value = lineBuffer.slice(position, position + valueLength).toString();
159+
if (field === "data") {
160+
data += value ? "".concat(value, "\n") : "\n";
161+
} else if (field === "event") {
162+
eventName = value;
163+
} else if (field === "id" && !value.includes("\0")) {
164+
eventId = value;
165+
} else if (field === "retry") {
166+
const retry = parseInt(value, 10);
167+
if (!Number.isNaN(retry)) {
168+
onParse({
169+
type: "reconnect-interval",
170+
value: retry,
171+
});
172+
}
173+
}
174+
}
175+
}
176+
var BOM = [239, 187, 191];
177+
function hasBom(buffer) {
178+
return BOM.every((charCode, index) => buffer.charCodeAt(index) === charCode);
179+
}
180+
181+
// http-url:https://unpkg.com/[email protected]/dist/stream.js
182+
var EventSourceParserStream = class extends TransformStream {
183+
constructor() {
184+
let parser;
185+
super({
186+
start(controller) {
187+
parser = createParser((event) => {
188+
if (event.type === "event") {
189+
controller.enqueue(event);
190+
}
191+
});
192+
},
193+
transform(chunk) {
194+
parser.feed(chunk);
195+
},
196+
});
197+
}
198+
};

0 commit comments

Comments
 (0)