这是indexloc提供的服务,不要输入任何密码
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4,526 changes: 2,300 additions & 2,226 deletions src/bundles/c/core/core.c

Large diffs are not rendered by default.

64,892 changes: 32,467 additions & 32,425 deletions src/bundles/c/core/polyfills.c

Large diffs are not rendered by default.

50 changes: 45 additions & 5 deletions src/js/core/sockets.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { readableStreamForHandle, writableStreamForHandle } from './stream-utils

const core = globalThis[Symbol.for('tjs.internal.core')];

const kOnConnection = Symbol('kOnConnection');

export async function connect(transport, host, port, options = {}) {
const addr = await resolveAddress(transport, host, port);
Expand Down Expand Up @@ -67,18 +68,28 @@ export async function listen(transport, host, port, options = {}) {
}

handle.bind(addr, flags);
handle.listen(options.backlog);

return new Listener(handle);
const l=new Listener(handle);

handle.listen(handle=>{
l[kOnConnection](handle);
},options.backlog);

return l;
}

case 'pipe': {
const handle = new core.Pipe();

handle.bind(addr);
handle.listen(options.backlog);

return new Listener(handle);
const l=new Listener(handle);

handle.listen(handle=>{
l[kOnConnection](handle);
},options.backlog);

return l;
}

case 'udp': {
Expand Down Expand Up @@ -204,6 +215,16 @@ class Listener {
this[kHandle] = handle;
}

#handleQueue=[];
#acceptQueue=[];
[kOnConnection](handle) {
if (this.#acceptQueue.length>0) {
this.#acceptQueue.shift().resolve(handle);
} else {
this.#handleQueue.push(handle);
}
}

get localAddress() {
if (!this[kLocalAddress]) {
this[kLocalAddress] = this[kHandle].getsockname();
Expand All @@ -213,17 +234,36 @@ class Listener {
}

async accept() {
const handle = await this[kHandle].accept();
let handle;

if (this.#handleQueue.length>0) {
handle=this.#handleQueue.shift();
} else {
handle=await new Promise((resolve,reject)=>{
this.#acceptQueue.push({ resolve,reject });
});
}


if (typeof handle === 'undefined') {
return;
}

if (handle instanceof Error) {
throw handle;
}

return new Connection(handle);
}

close() {
this[kHandle].close();

for (let v1 of this.#acceptQueue) {
v1.reject(new Error('closed'));
}

this.#handleQueue=[];
}

// Async iterator.
Expand Down
2 changes: 1 addition & 1 deletion src/js/polyfills/event-target.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class Event {
* @type {EventTarget}
*/
get target() {
return null;
return this.currentTarget;
}

/**
Expand Down
162 changes: 63 additions & 99 deletions src/js/polyfills/fetch/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ function isDataView(obj) {
}

function consumed(body) {
if (body._noBody) {
if (body._bodySize===0) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please run the linter? There are a number of style errors in the JS code.

return;
}

Expand All @@ -14,48 +14,6 @@ function consumed(body) {
body.bodyUsed = true;
}

function fileReaderReady(reader) {
return new Promise(function(resolve, reject) {
reader.onload = function() {
resolve(reader.result);
};

reader.onerror = function() {
reject(reader.error);
};
});
}

function readBlobAsArrayBuffer(blob) {
var reader = new FileReader();
var promise = fileReaderReady(reader);

reader.readAsArrayBuffer(blob);

return promise;
}

function readBlobAsText(blob) {
var reader = new FileReader();
var promise = fileReaderReady(reader);
var match = /charset=([A-Za-z0-9_-]+)/.exec(blob.type);
var encoding = match ? match[1] : 'utf-8';

reader.readAsText(blob, encoding);

return promise;
}

function readArrayBufferAsText(buf) {
var view = new Uint8Array(buf);
var chars = new Array(view.length);

for (var i = 0; i < view.length; i++) {
chars[i] = String.fromCharCode(view[i]);
}

return chars.join('');
}

function bufferClone(buf) {
if (buf.slice) {
Expand All @@ -80,24 +38,50 @@ export const BodyMixin = {
this._bodyInit = body;

if (!body) {
this._noBody = true;
this._bodyText = '';
this._bodySize = 0;
this._bodyReadable = ReadableStream.from([ new Uint8Array(0) ]);
} else if (typeof body === 'string') {
this._bodyText = body;
const bodyBuffer=new TextEncoder().encode(body);

this._bodySize=bodyBuffer.byteLength;
this._bodyReadable = ReadableStream.from([ bodyBuffer ]);
} else if (isPrototypeOf(Blob.prototype, body)) {
this._bodyBlob = body;
this._bodySize=body.size;
this._bodyReadable = body.stream;
} else if (isPrototypeOf(FormData.prototype, body)) {
this._bodyFormData = body;
// formdata polyfill
const bodyBuffer = body['_blob']();

this._bodySize=bodyBuffer.byteLength;
this._bodyReadable = ReadableStream.from([ bodyBuffer ]);
} else if (isPrototypeOf(URLSearchParams.prototype, body)) {
this._bodyText = body.toString();
const textEncoder=new TextEncoder();
const bodyBuffer=textEncoder.encode(body);

this._bodySize=bodyBuffer.byteLength;
this._bodyReadable = ReadableStream.from([ textEncoder.encode(body.toString()) ]);
} else if (isDataView(body)) {
this._bodyArrayBuffer = bufferClone(body.buffer);
const bodyBuffer=new Uint8Array(bufferClone(body.buffer));

this._bodySize=bodyBuffer.byteLength;
this._bodyReadable = ReadableStream.from([ bodyBuffer ]);
} else if (isPrototypeOf(ArrayBuffer.prototype, body) || ArrayBuffer.isView(body)) {
this._bodyArrayBuffer = bufferClone(body);
const bodyBuffer=new Uint8Array(bufferClone(body.buffer));

this._bodySize=bodyBuffer.byteLength;
this._bodyReadable = ReadableStream.from([ bodyBuffer ]);
} else if (isPrototypeOf(ReadableStream.prototype, body)) {
this._bodySize=-1;
this._bodyReadable = body;
} else {
this._bodyText = body = Object.prototype.toString.call(body);
const bodyBuffer=new TextEncoder().encode(body.toString());

this._bodySize=bodyBuffer.byteLength;
this._bodyReadable = ReadableStream.from([ bodyBuffer ]);
}

this.body=this._bodyReadable;

if (!this.headers.get('content-type')) {
if (typeof body === 'string') {
this.headers.set('content-type', 'text/plain;charset=UTF-8');
Expand All @@ -109,70 +93,50 @@ export const BodyMixin = {
}
},

blob() {
async blob() {
const rejected = consumed(this);

if (rejected) {
return rejected;
await rejected;
}

if (this._bodyBlob) {
return Promise.resolve(this._bodyBlob);
} else if (this._bodyArrayBuffer) {
return Promise.resolve(new Blob([ this._bodyArrayBuffer ]));
} else if (this._bodyFormData) {
throw new Error('could not read FormData body as blob');
} else {
return Promise.resolve(new Blob([ this._bodyText ]));
if (!this._bodyReadable) {
throw new Error('Unknown body type');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we throw earlier?

}
},

arrayBuffer() {
if (this._bodyArrayBuffer) {
var isConsumed = consumed(this);

if (isConsumed) {
return isConsumed;
} else if (ArrayBuffer.isView(this._bodyArrayBuffer)) {
return Promise.resolve(
this._bodyArrayBuffer.buffer.slice(
this._bodyArrayBuffer.byteOffset,
this._bodyArrayBuffer.byteOffset + this._bodyArrayBuffer.byteLength
)
);
} else {
return Promise.resolve(this._bodyArrayBuffer);
const parts=[];
const reader=this._bodyReadable.getReader();

for (;;) {
const next=await reader.read();

if (next.done) {
break;
}
} else {
return this.blob().then(readBlobAsArrayBuffer);

parts.push(next.value);
}
},

text() {
const rejected = consumed(this);
return new Blob(parts);
},

if (rejected) {
return rejected;
}
async arrayBuffer() {
// TODO: expose Blob.parts to reduce memeory copy?
return await (await this.blob()).arrayBuffer();
},

if (this._bodyBlob) {
return readBlobAsText(this._bodyBlob);
} else if (this._bodyArrayBuffer) {
return Promise.resolve(readArrayBufferAsText(this._bodyArrayBuffer));
} else if (this._bodyFormData) {
throw new Error('could not read FormData body as text');
} else {
return Promise.resolve(this._bodyText);
}
async text() {
return new TextDecoder().decode(await this.arrayBuffer());
},

formData() {
return this.text().then(decode);
async formData() {
return decode(await this.text());
},

json() {
return this.text().then(JSON.parse);
async json() {
return JSON.parse(await this.text());
},

};

function decode(body) {
Expand Down
Loading